source: trunk/src/Detection/detection.cc @ 782

Last change on this file since 782 was 781, checked in by MatthewWhiting, 13 years ago

Slight streamlining of a loop. Also removing unused commented-out code.

File size: 31.1 KB
Line 
1// -----------------------------------------------------------------------
2// detection.cc : Member functions for the Detection class.
3// -----------------------------------------------------------------------
4// Copyright (C) 2006, Matthew Whiting, ATNF
5//
6// This program is free software; you can redistribute it and/or modify it
7// under the terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 2 of the License, or (at your
9// option) any later version.
10//
11// Duchamp is distributed in the hope that it will be useful, but WITHOUT
12// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14// for more details.
15//
16// You should have received a copy of the GNU General Public License
17// along with Duchamp; if not, write to the Free Software Foundation,
18// Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
19//
20// Correspondence concerning Duchamp may be directed to:
21//    Internet email: Matthew.Whiting [at] atnf.csiro.au
22//    Postal address: Dr. Matthew Whiting
23//                    Australia Telescope National Facility, CSIRO
24//                    PO Box 76
25//                    Epping NSW 1710
26//                    AUSTRALIA
27// -----------------------------------------------------------------------
28#include <iostream>
29#include <iomanip>
30#include <vector>
31#include <string>
32#include <wcslib/wcs.h>
33#include <math.h>
34#include <duchamp/duchamp.hh>
35#include <duchamp/param.hh>
36#include <duchamp/fitsHeader.hh>
37#include <duchamp/Utils/utils.hh>
38#include <duchamp/PixelMap/Voxel.hh>
39#include <duchamp/PixelMap/Object3D.hh>
40#include <duchamp/Detection/detection.hh>
41#include <duchamp/Cubes/cubeUtils.hh>
42#include <duchamp/Detection/columns.hh>
43
44using namespace PixelInfo;
45
46namespace duchamp
47{
48
49  void Detection::defaultDetection()
50  {
51    this->xSubOffset = 0;
52    this->ySubOffset = 0;
53    this->zSubOffset = 0;
54    this->haveParams = false;
55    this->totalFlux = 0.;
56    this->peakFlux = 0.;
57    this->intFlux = 0.;
58    this->xpeak = 0;
59    this->ypeak = 0;
60    this->zpeak = 0;
61    this->peakSNR = 0.;
62    this->xCentroid = 0.;
63    this->yCentroid = 0.;
64    this->zCentroid = 0.;
65    this->centreType="centroid";
66    this->negSource = false;
67    this->flagText="";
68    this->id = -1;
69    this->name = "";
70    this->flagWCS=false;
71    this->specOK = true;
72    this->raS = "";
73    this->decS = "";
74    this->ra = 0.;
75    this->dec = 0.;
76    this->raWidth = 0.;
77    this->decWidth = 0.;
78    this->majorAxis = 0.;
79    this->minorAxis = 0.;
80    this->posang = 0.;
81    this->specUnits = "";
82    this->fluxUnits = "";
83    this->intFluxUnits = "";
84    this->lngtype = "RA";
85    this->lattype = "DEC";
86    this->vel = 0.;
87    this->velWidth = 0.;
88    this->velMin = 0.;
89    this->velMax = 0.;
90    this->w20 = 0.;
91    this->v20min = 0.;
92    this->v20max = 0.;
93    this->w50 = 0.;
94    this->v50min = 0.;
95    this->v50max = 0.;
96    this->posPrec = Column::prPOS;
97    this->xyzPrec = Column::prXYZ;
98    this->fintPrec = Column::prFLUX;
99    this->fpeakPrec = Column::prFLUX;
100    this->velPrec = Column::prVEL;
101    this->snrPrec = Column::prSNR;
102  }
103
104  Detection::Detection():
105    Object3D()
106  {
107    this->defaultDetection();
108  }
109
110  Detection::Detection(const Object3D& o):
111    Object3D(o)
112  {
113    this->defaultDetection();
114  }
115
116  Detection::Detection(const Detection& d):
117    Object3D(d)
118  {
119    operator=(d);
120  }
121
122  Detection& Detection::operator= (const Detection& d)
123  {
124    ((Object3D &) *this) = d;
125    this->xSubOffset   = d.xSubOffset;
126    this->ySubOffset   = d.ySubOffset;
127    this->zSubOffset   = d.zSubOffset;
128    this->haveParams   = d.haveParams;
129    this->totalFlux    = d.totalFlux;
130    this->intFlux      = d.intFlux;
131    this->peakFlux     = d.peakFlux;
132    this->xpeak        = d.xpeak;
133    this->ypeak        = d.ypeak;
134    this->zpeak        = d.zpeak;
135    this->peakSNR      = d.peakSNR;
136    this->xCentroid    = d.xCentroid;
137    this->yCentroid    = d.yCentroid;
138    this->zCentroid    = d.zCentroid;
139    this->centreType   = d.centreType;
140    this->negSource    = d.negSource;
141    this->flagText     = d.flagText;
142    this->id           = d.id;
143    this->name         = d.name;
144    this->flagWCS      = d.flagWCS;
145    this->specOK       = d.specOK;
146    this->raS          = d.raS;
147    this->decS         = d.decS;
148    this->ra           = d.ra;
149    this->dec          = d.dec;
150    this->raWidth      = d.raWidth;
151    this->decWidth     = d.decWidth;
152    this->majorAxis    = d.majorAxis;
153    this->minorAxis    = d.minorAxis;
154    this->posang       = d.posang;
155    this->specUnits    = d.specUnits;
156    this->fluxUnits    = d.fluxUnits;
157    this->intFluxUnits = d.intFluxUnits;
158    this->lngtype      = d.lngtype;
159    this->lattype      = d.lattype;
160    this->vel          = d.vel;
161    this->velWidth     = d.velWidth;
162    this->velMin       = d.velMin;
163    this->velMax       = d.velMax;
164    this->w20          = d.w20;
165    this->v20min       = d.v20min;
166    this->v20max       = d.v20max;
167    this->w50          = d.w50;
168    this->v50min       = d.v50min;
169    this->v50max       = d.v50max;
170    this->posPrec      = d.posPrec;
171    this->xyzPrec      = d.xyzPrec;
172    this->fintPrec     = d.fintPrec;
173    this->fpeakPrec    = d.fpeakPrec;
174    this->velPrec      = d.velPrec;
175    this->snrPrec      = d.snrPrec;
176    return *this;
177  }
178
179  //--------------------------------------------------------------------
180  float Detection::getXcentre()
181  {
182    if(this->centreType=="peak") return this->xpeak;
183    else if(this->centreType=="average") return this->getXaverage();
184    else return this->xCentroid;
185  }
186
187  float Detection::getYcentre()
188  {
189    if(this->centreType=="peak") return this->ypeak;
190    else if(this->centreType=="average") return this->getYaverage();
191    else return this->yCentroid;
192  }
193
194  float Detection::getZcentre()
195  {
196    if(this->centreType=="peak") return this->zpeak;
197    else if(this->centreType=="average") return this->getZaverage();
198    else return this->zCentroid;
199  }
200
201  //--------------------------------------------------------------------
202
203  bool Detection::voxelListsMatch(std::vector<Voxel> voxelList)
204  {
205    /// @details
206    ///  A test to see whether there is a 1-1 correspondence between
207    ///  the given list of Voxels and the voxel positions contained in
208    ///  this Detection's pixel list. No testing of the fluxes of the
209    ///  Voxels is done.
210    ///
211    /// \param voxelList The std::vector list of Voxels to be tested.
212
213    bool listsMatch = true;
214    // compare sizes
215    listsMatch = listsMatch && (voxelList.size() == this->getSize());
216    if(!listsMatch) return listsMatch;
217
218    // make sure all Detection pixels are in voxel list
219    listsMatch = listsMatch && this->voxelListCovered(voxelList);
220
221    // make sure all voxels are in Detection
222    std::vector<Voxel>::iterator vox;
223    for(vox=voxelList.begin();vox<voxelList.end();vox++)
224      listsMatch = listsMatch && this->isInObject(*vox);
225
226    return listsMatch;
227
228  }
229  //--------------------------------------------------------------------
230
231  bool Detection::voxelListCovered(std::vector<Voxel> voxelList)
232  {
233    ///  @details
234    ///  A test to see whether the given list of Voxels contains each
235    ///  position in this Detection's pixel list. It does not look for
236    ///  a 1-1 correspondence: the given list can be a super-set of the
237    ///  Detection. No testing of the fluxes of the Voxels is done.
238    ///
239    /// \param voxelList The std::vector list of Voxels to be tested.
240
241    bool listsMatch = true;
242
243    // make sure all Detection pixels are in voxel list
244    size_t v1=0;
245    std::vector<Voxel> detpixlist = this->getPixelSet();
246    while(listsMatch && v1<detpixlist.size()){
247      bool inList = false;
248      size_t v2=0;
249      while(!inList && v2<voxelList.size()){
250        inList = inList || detpixlist[v1].match(voxelList[v2]);
251        v2++;
252      }
253      listsMatch = listsMatch && inList;
254      v1++;
255    }
256
257    return listsMatch;
258
259  }
260  //--------------------------------------------------------------------
261
262  void Detection::calcFluxes(std::vector<Voxel> voxelList)
263  {
264    ///  @details
265    ///  A function that calculates total & peak fluxes (and the location
266    ///  of the peak flux) for a Detection.
267    ///
268    ///  \param fluxArray The array of flux values to calculate the
269    ///  flux parameters from.
270    ///  \param dim The dimensions of the flux array.
271   
272    //    this->haveParams = true;
273
274    this->totalFlux = this->peakFlux = 0;
275    this->xCentroid = this->yCentroid = this->zCentroid = 0.;
276
277    // first check that the voxel list and the Detection's pixel list
278    // have a 1-1 correspondence
279
280    if(!this->voxelListCovered(voxelList)){
281      duchampError("Detection::calcFluxes","Voxel list provided does not match");
282      return;
283    }
284
285    std::vector<Voxel>::iterator vox;
286    for(vox=voxelList.begin();vox<voxelList.end();vox++){
287      if(this->isInObject(*vox)){
288        long x = vox->getX();
289        long y = vox->getY();
290        long z = vox->getZ();
291        float f = vox->getF();
292        this->totalFlux += f;
293        this->xCentroid += x*f;
294        this->yCentroid += y*f;
295        this->zCentroid += z*f;
296        if( (vox==voxelList.begin()) ||  //first time round
297            (this->negSource&&(f<this->peakFlux)) ||
298            (!this->negSource&&(f>this->peakFlux))   )
299          {
300            this->peakFlux = f;
301            this->xpeak =    x;
302            this->ypeak =    y;
303            this->zpeak =    z;
304          }
305      }
306    }
307
308    this->xCentroid /= this->totalFlux;
309    this->yCentroid /= this->totalFlux;
310    this->zCentroid /= this->totalFlux;
311  }
312  //--------------------------------------------------------------------
313
314  void Detection::calcFluxes(float *fluxArray, long *dim)
315  {
316    ///  @details
317    ///  A function that calculates total & peak fluxes (and the location
318    ///  of the peak flux) for a Detection.
319    ///
320    ///  \param fluxArray The array of flux values to calculate the
321    ///  flux parameters from.
322    ///  \param dim The dimensions of the flux array.
323
324    //    this->haveParams = true;
325
326    this->totalFlux = this->peakFlux = 0;
327    this->xCentroid = this->yCentroid = this->zCentroid = 0.;
328
329    std::vector<Voxel> voxList = this->getPixelSet();
330    std::vector<Voxel>::iterator vox=voxList.begin();
331    for(;vox<voxList.end();vox++){
332
333      long x=vox->getX();
334      long y=vox->getY();
335      long z=vox->getZ();
336      long ind = vox->arrayIndex(dim);
337      float f = fluxArray[ind];
338      this->totalFlux += f;
339      this->xCentroid += x*f;
340      this->yCentroid += y*f;
341      this->zCentroid += z*f;
342      if( (vox==voxList.begin()) ||
343          (this->negSource&&(f<this->peakFlux)) ||
344          (!this->negSource&&(f>this->peakFlux))   )
345        {
346          this->peakFlux = f;
347          this->xpeak = x;
348          this->ypeak = y;
349          this->zpeak = z;
350        }
351 
352    }
353
354    this->xCentroid /= this->totalFlux;
355    this->yCentroid /= this->totalFlux;
356    this->zCentroid /= this->totalFlux;
357  }
358  //--------------------------------------------------------------------
359
360  void Detection::calcWCSparams(FitsHeader &head)
361  {
362    ///  @details
363    ///  Use the input wcs to calculate the position and velocity
364    ///    information for the Detection.
365    ///  Quantities calculated:
366    ///  <ul><li> RA: ra [deg], ra (string), ra width.
367    ///      <li> Dec: dec [deg], dec (string), dec width.
368    ///      <li> Vel: vel [km/s], min & max vel, vel width.
369    ///      <li> coord type for all three axes, nuRest,
370    ///      <li> name (IAU-style, in equatorial or Galactic)
371    ///  </ul>
372    ///
373    ///  Note that the regular parameters are NOT recalculated!
374    ///
375    ///  \param head FitsHeader object that contains the WCS information.
376
377    if(head.isWCS()){
378
379      double *pixcrd = new double[15];
380      double *world  = new double[15];
381      /*
382        define a five-point array in 3D:
383        (x,y,z), (x,y,z1), (x,y,z2), (x1,y1,z), (x2,y2,z)
384        [note: x = central point, x1 = minimum x, x2 = maximum x etc.]
385        and convert to world coordinates.   
386      */
387      pixcrd[0]  = pixcrd[3] = pixcrd[6] = this->getXcentre();
388      pixcrd[9]  = this->getXmin()-0.5;
389      pixcrd[12] = this->getXmax()+0.5;
390      pixcrd[1]  = pixcrd[4] = pixcrd[7] = this->getYcentre();
391      pixcrd[10] = this->getYmin()-0.5;
392      pixcrd[13] = this->getYmax()+0.5;
393      pixcrd[2] = pixcrd[11] = pixcrd[14] = this->getZcentre();
394      pixcrd[5] = this->getZmin();
395      pixcrd[8] = this->getZmax();
396      int flag = head.pixToWCS(pixcrd, world, 5);
397      delete [] pixcrd;
398      if(flag!=0) duchampError("calcWCSparams",
399                               "Error in calculating the WCS for this object.\n");
400      else{
401
402        // world now has the WCS coords for the five points
403        //    -- use this to work out WCS params
404 
405        this->haveParams = true;
406
407        this->specOK = head.canUseThirdAxis();
408        this->lngtype = head.WCS().lngtyp;
409        this->lattype = head.WCS().lattyp;
410        this->specUnits = head.getSpectralUnits();
411        this->fluxUnits = head.getFluxUnits();
412        // if fluxUnits are eg. Jy/beam, make intFluxUnits = Jy km/s
413        this->intFluxUnits = head.getIntFluxUnits();
414        this->ra   = world[0];
415        this->dec  = world[1];
416        this->raS  = decToDMS(this->ra, this->lngtype);
417        this->decS = decToDMS(this->dec,this->lattype);
418        this->raWidth = angularSeparation(world[9],world[1],
419                                          world[12],world[1]) * 60.;
420        this->decWidth  = angularSeparation(world[0],world[10],
421                                            world[0],world[13]) * 60.;
422
423        Object2D spatMap = this->getSpatialMap();
424        std::pair<double,double> axes = spatMap.getPrincipleAxes();
425        this->majorAxis = std::max(axes.first,axes.second) * head.getAvPixScale();
426        this->minorAxis = std::min(axes.first,axes.second) * head.getAvPixScale();
427        this->posang = spatMap.getPositionAngle() * 180. / M_PI;
428
429        this->name = head.getIAUName(this->ra, this->dec);
430        this->vel    = head.specToVel(world[2]);
431        this->velMin = head.specToVel(world[5]);
432        this->velMax = head.specToVel(world[8]);
433        this->velWidth = fabs(this->velMax - this->velMin);
434
435        this->flagWCS = true;
436      }
437      delete [] world;
438
439    }
440  }
441  //--------------------------------------------------------------------
442
443  void Detection::calcIntegFlux(long zdim, std::vector<Voxel> voxelList, FitsHeader &head)
444  {
445    ///  @details
446    ///  Uses the input WCS to calculate the velocity-integrated flux,
447    ///   putting velocity in units of km/s.
448    ///  The fluxes used are taken from the Voxels, rather than an
449    ///   array of flux values.
450    ///  Integrates over full spatial and velocity range as given
451    ///   by the extrema calculated by calcWCSparams.
452    ///
453    ///  If the flux units end in "/beam" (eg. Jy/beam), then the flux is
454    ///  corrected by the beam size (in pixels). This is done by
455    ///  multiplying the integrated flux by the number of spatial pixels,
456    ///  and dividing by the beam size in pixels (e.g. Jy/beam * pix /
457    ///  pix/beam --> Jy)
458    ///
459    ///  \param zdim The size of the spectral axis (needed to find the velocity widths)
460    ///  \param voxelList The list of Voxels with flux information
461    ///  \param head FitsHeader object that contains the WCS information.
462
463    const int border = 1;
464
465    if(!this->voxelListCovered(voxelList)){
466      duchampError("Detection::calcIntegFlux","Voxel list provided does not match");
467      return;
468    }
469
470    if(!head.is2D()){
471
472      this->haveParams = true;
473
474      // include one pixel either side in each direction
475      long xsize = (this->getXmax()-this->getXmin()+border*2+1);
476      long ysize = (this->getYmax()-this->getYmin()+border*2+1);
477      long zsize = (this->getZmax()-this->getZmin()+border*2+1);
478      long size = xsize*ysize*zsize;
479      std::vector <bool> isObj(size,false);
480      double *localFlux = new double[size];
481      for(int i=0;i<size;i++) localFlux[i]=0.;
482
483      std::vector<Voxel>::iterator vox;
484      for(vox=voxelList.begin();vox<voxelList.end();vox++){
485        if(this->isInObject(*vox)){
486          long x = vox->getX();
487          long y = vox->getY();
488          long z = vox->getZ();
489          long pos = (x-this->getXmin()+border) + (y-this->getYmin()+border)*xsize
490            + (z-this->getZmin()+border)*xsize*ysize;
491          localFlux[pos] = vox->getF();
492          isObj[pos] = true;
493        }
494      }
495 
496      // work out the WCS coords for each pixel
497      double *world  = new double[size];
498      double xpt,ypt,zpt;
499      for(int i=0;i<xsize*ysize*zsize;i++){
500        xpt = double( this->getXmin() - border + i%xsize );
501        ypt = double( this->getYmin() - border + (i/xsize)%ysize );
502        zpt = double( this->getZmin() - border + i/(xsize*ysize) );
503        world[i] = head.pixToVel(xpt,ypt,zpt);
504      }
505
506      double integrated = 0.;
507      for(int pix=0; pix<xsize*ysize; pix++){ // loop over each spatial pixel.
508        for(int z=0; z<zsize; z++){
509          int pos =  z*xsize*ysize + pix;
510          if(isObj[pos]){ // if it's an object pixel...
511            double deltaVel;
512            if(z==0)
513              deltaVel = (world[pos+xsize*ysize] - world[pos]);
514            else if(z==(zsize-1))
515              deltaVel = (world[pos] - world[pos-xsize*ysize]);
516            else
517              deltaVel = (world[pos+xsize*ysize] - world[pos-xsize*ysize]) / 2.;
518            integrated += localFlux[pos] * fabs(deltaVel);
519          }
520        }
521      }
522      this->intFlux = integrated;
523
524      delete [] world;
525      delete [] localFlux;
526
527      calcVelWidths(zdim,voxelList,head);
528
529    }
530    else // in this case there is just a 2D image.
531      this->intFlux = this->totalFlux;
532
533    if(head.isWCS()){
534      // correct for the beam size if the flux units string ends in "/beam"
535      if(head.needBeamSize()) this->intFlux  /= head.getBeamSize();
536    }
537
538  }
539  //--------------------------------------------------------------------
540
541  void Detection::calcIntegFlux(float *fluxArray, long *dim, FitsHeader &head)
542  {
543    ///  @details
544    ///  Uses the input WCS to calculate the velocity-integrated flux,
545    ///   putting velocity in units of km/s.
546    ///  Integrates over full spatial and velocity range as given
547    ///   by the extrema calculated by calcWCSparams.
548    ///
549    ///  If the flux units end in "/beam" (eg. Jy/beam), then the flux is
550    ///  corrected by the beam size (in pixels). This is done by
551    ///  multiplying the integrated flux by the number of spatial pixels,
552    ///  and dividing by the beam size in pixels (e.g. Jy/beam * pix /
553    ///  pix/beam --> Jy)
554    ///
555    ///  \param fluxArray The array of flux values.
556    ///  \param dim The dimensions of the flux array.
557    ///  \param head FitsHeader object that contains the WCS information.
558
559    if(!head.is2D()){
560
561      this->haveParams = true;
562
563      // include one pixel either side in each direction
564      long xsize = (this->xmax-this->xmin+3);
565      long ysize = (this->ymax-this->ymin+3);
566      long zsize = (this->zmax-this->zmin+3);
567      long size = xsize*ysize*zsize;
568      std::vector <bool> isObj(size,false);
569      double *localFlux = new double[size];
570      for(int i=0;i<size;i++) localFlux[i]=0.;
571      // work out which pixels are object pixels
572      std::vector<Voxel> voxlist = this->getPixelSet();
573      for(std::vector<Voxel>::iterator v=voxlist.begin();v<voxlist.end();v++){
574        long pos=(v->getX()-this->xmin+1) + (v->getY()-this->ymin+1)*xsize
575          + (v->getZ()-this->zmin+1)*xsize*ysize;
576        localFlux[pos] = fluxArray[v->arrayIndex(dim)];
577        isObj[pos] = true;
578      }
579 
580      // work out the WCS coords for each pixel
581      double *world  = new double[size];
582      double xpt,ypt,zpt;
583      // for(int i=0;i<xsize*ysize*zsize;i++){
584      //        xpt = double( this->getXmin() -1 + i%xsize );
585      //        ypt = double( this->getYmin() -1 + (i/xsize)%ysize );
586      //        zpt = double( this->getZmin() -1 + i/(xsize*ysize) );
587      //        world[i] = head.pixToVel(xpt,ypt,zpt);
588      // }
589      int i=0;
590      for(int z=0;z<zsize;z++){
591        for(int y=0;y<ysize;y++){
592          for(int x=0;x<xsize;x++){
593            xpt=double(this->xmin - 1 + x);
594            ypt=double(this->ymin - 1 + y);
595            zpt=double(this->zmin - 1 + z);
596            world[i++] = head.pixToVel(xpt,ypt,zpt);
597          }
598        }
599      }
600
601      double integrated = 0.;
602      for(int pix=0; pix<xsize*ysize; pix++){ // loop over each spatial pixel.
603        for(int z=0; z<zsize; z++){
604          int pos =  z*xsize*ysize + pix;
605          if(isObj[pos]){ // if it's an object pixel...
606            double deltaVel;
607            if(z==0)
608              deltaVel = (world[pos+xsize*ysize] - world[pos]);
609            else if(z==(zsize-1))
610              deltaVel = (world[pos] - world[pos-xsize*ysize]);
611            else
612              deltaVel = (world[pos+xsize*ysize] - world[pos-xsize*ysize]) / 2.;
613            integrated += localFlux[pos] * fabs(deltaVel);
614          }
615        }
616      }
617      this->intFlux = integrated;
618
619      delete [] world;
620      delete [] localFlux;
621
622      calcVelWidths(fluxArray, dim, head);
623
624    }
625    else // in this case there is just a 2D image.
626      this->intFlux = this->totalFlux;
627
628    if(head.isWCS()){
629      // correct for the beam size if the flux units string ends in "/beam"
630      if(head.needBeamSize()) this->intFlux  /= head.getBeamSize();
631    }
632
633  }
634  //--------------------------------------------------------------------
635
636  void Detection::calcVelWidths(long zdim, std::vector<Voxel> voxelList, FitsHeader &head)
637  {
638    ///  @details
639    /// Calculates the widths of the detection at 20% and 50% of the
640    /// peak integrated flux. The procedure is as follows: first
641    /// generate an integrated flux spectrum (using all given voxels
642    /// that lie in the object's spatial map); find the peak; starting
643    /// at the spectral edges of the detection, move in or out until
644    /// you reach the 20% or 50% peak flux level. Linear interpolation
645    /// between points is done.
646    ///
647    ///  \param zdim The size of the spectral axis in the cube
648    ///  \param voxelList The list of Voxels with flux information
649    ///  \param head FitsHeader object that contains the WCS information.
650
651    float *intSpec = new float[zdim];
652    for(int i=0;i<zdim;i++) intSpec[i]=0;
653       
654    Object2D spatMap = this->getSpatialMap();
655    for(int s=0;s<spatMap.getNumScan();s++){
656      std::vector<Voxel>::iterator vox;
657      for(vox=voxelList.begin();vox<voxelList.end();vox++){
658        if(spatMap.isInObject(*vox)){
659          intSpec[vox->getZ()] += vox->getF();
660        }
661      }
662    }
663   
664    calcVelWidths(zdim, intSpec, head);
665
666    delete [] intSpec;
667
668  }
669
670  //--------------------------------------------------------------------
671
672  void Detection::calcVelWidths(long zdim, float *intSpec, FitsHeader &head)
673  {
674
675      // finding the 20% & 50% points.  Start at the velmin & velmax
676      //  points. Then, if the int flux there is above the 20%/50%
677      //  limit, go out, otherwise go in. This is to deal with the
678      //  problems from double- (or multi-) peaked sources.
679
680    this->haveParams = true;
681
682    int z=this->getZmin();
683    double zpt,xpt=double(this->getXcentre()),ypt=double(this->getXcentre());
684    bool goLeft;
685   
686    float peak=0.;
687    int peakLoc=0;
688    for(int z=0;z<zdim;z++) {
689      if(z==0 || peak<intSpec[z]){
690        peak = intSpec[z];
691        peakLoc = z;
692      }
693    }
694   
695    goLeft = intSpec[z]>peak*0.5;
696    if(goLeft) while(z>0 && intSpec[z]>peak*0.5) z--;
697    else       while(z<peakLoc && intSpec[z]<peak*0.5) z++;
698    if(z==0) this->v50min = this->velMin;
699    else{
700      if(goLeft) zpt = z + (peak*0.5-intSpec[z])/(intSpec[z+1]-intSpec[z]);
701      else       zpt = z - (peak*0.5-intSpec[z])/(intSpec[z-1]-intSpec[z]);
702      this->v50min = head.pixToVel(xpt,ypt,zpt);
703    }
704    z=this->getZmax();
705    goLeft = intSpec[z]<peak*0.5;
706    if(goLeft) while(z>peakLoc && intSpec[z]<peak*0.5) z--;
707    else       while(z<zdim    && intSpec[z]>peak*0.5) z++;
708    if(z==zdim) this->v50max = this->velMax;
709    else{
710      if(goLeft) zpt = z + (peak*0.5-intSpec[z])/(intSpec[z+1]-intSpec[z]);
711      else       zpt = z - (peak*0.5-intSpec[z])/(intSpec[z-1]-intSpec[z]);
712      this->v50max = head.pixToVel(xpt,ypt,zpt);
713    }
714    z=this->getZmin();
715    goLeft = intSpec[z]>peak*0.2;
716    if(goLeft) while(z>0 && intSpec[z]>peak*0.2) z--;
717    else       while(z<peakLoc && intSpec[z]<peak*0.2) z++;
718    if(z==0) this->v20min = this->velMin;
719    else{
720      if(goLeft) zpt = z + (peak*0.2-intSpec[z])/(intSpec[z+1]-intSpec[z]);
721      else       zpt = z - (peak*0.2-intSpec[z])/(intSpec[z-1]-intSpec[z]);
722      this->v20min = head.pixToVel(xpt,ypt,zpt);
723    }
724    z=this->getZmax();
725    goLeft = intSpec[z]<peak*0.2;
726    if(goLeft) while(z>peakLoc && intSpec[z]<peak*0.2) z--;
727    else       while(z<zdim    && intSpec[z]>peak*0.2) z++;
728    if(z==zdim) this->v20max = this->velMax;
729    else{
730      if(goLeft) zpt = z + (peak*0.2-intSpec[z])/(intSpec[z+1]-intSpec[z]);
731      else       zpt = z - (peak*0.2-intSpec[z])/(intSpec[z-1]-intSpec[z]);
732      this->v20max = head.pixToVel(xpt,ypt,zpt);
733    }
734
735    this->w20 = fabs(this->v20min - this->v20max);
736    this->w50 = fabs(this->v50min - this->v50max);
737
738
739  }
740  //--------------------------------------------------------------------
741
742  void Detection::calcVelWidths(float *fluxArray, long *dim, FitsHeader &head)
743  {
744    ///  @details
745    /// Calculates the widths of the detection at 20% and 50% of the
746    /// peak integrated flux. The procedure is as follows: first
747    /// generate an integrated flux spectrum (summing each spatial
748    /// pixel's spectrum); find the peak; starting at the spectral
749    /// edges of the detection, move in or out until you reach the 20%
750    /// or 50% peak flux level. Linear interpolation between points is
751    /// done.
752    ///
753    ///  \param fluxArray The array of flux values.
754    ///  \param dim The dimensions of the flux array.
755    ///  \param head FitsHeader object that contains the WCS information.
756
757    if(dim[2] > 2){
758
759      float *intSpec = new float[dim[2]];
760      long size=dim[0]*dim[1]*dim[2];
761      std::vector<bool> mask(size,true);
762      getIntSpec(*this,fluxArray,dim,mask,1.,intSpec);
763
764      this->calcVelWidths(dim[2],intSpec,head);
765
766      delete [] intSpec;
767
768    }
769    else{
770      this->v50min = this->v20min = this->velMin;
771      this->v50max = this->v20max = this->velMax;
772      this->w20 = fabs(this->v20min - this->v20max);
773      this->w50 = fabs(this->v50min - this->v50max);
774    }
775
776  }
777  //--------------------------------------------------------------------
778
779  void Detection::setOffsets(Param &par)
780  {
781    ///  @details
782    /// This function stores the values of the offsets for each cube axis.
783    /// The offsets are the starting values of the cube axes that may differ from
784    ///  the default value of 0 (for instance, if a subsection is being used).
785    /// The values will be used when the detection is outputted.
786
787    this->xSubOffset = par.getXOffset();
788    this->ySubOffset = par.getYOffset();
789    this->zSubOffset = par.getZOffset();
790  }
791  //--------------------------------------------------------------------
792
793  bool Detection::hasEnoughChannels(int minNumber)
794  {
795    ///  @details
796    /// A function to determine if the Detection has enough
797    /// contiguous channels to meet the minimum requirement
798    /// given as the argument.
799    /// \param minNumber How many channels is the minimum acceptable number?
800    /// \return True if there is at least one occurence of minNumber consecutive
801    /// channels present to return true. False otherwise.
802
803    // Preferred method -- need a set of minNumber consecutive channels present.
804
805    int numChan = this->getMaxAdjacentChannels();
806    bool result = (numChan >= minNumber);
807
808    return result;
809 
810  }
811  //--------------------------------------------------------------------
812
813  std::vector<int> Detection::getVertexSet()
814  {
815    ///  @details
816    /// Gets a list of points being the end-points of 1-pixel long
817    /// segments drawing a border around the spatial extend of a
818    /// detection. The vector is a series of 4 integers, being: x_0,
819    /// y_0, x_1, y_1.
820    /// \return The vector of vertex positions.
821
822    std::vector<int> vertexSet;
823
824    int xmin = this->getXmin() - 1;
825    int xmax = this->getXmax() + 1;
826    int ymin = this->getYmin() - 1;
827    int ymax = this->getYmax() + 1;
828    int xsize = xmax - xmin + 1;
829    int ysize = ymax - ymin + 1;
830
831    std::vector<Voxel> voxlist = this->getPixelSet();
832    std::vector<bool> isObj(xsize*ysize,false);
833    std::vector<Voxel>::iterator vox;
834    for(vox=voxlist.begin();vox<voxlist.end();vox++){
835      int pos = (vox->getX()-xmin) +
836        (vox->getY()-ymin)*xsize;
837      isObj[pos] = true;
838    }
839    voxlist.clear();
840   
841    for(int x=xmin; x<=xmax; x++){
842      // for each column...
843      for(int y=ymin+1;y<=ymax;y++){
844        int current  = (y-ymin)*xsize + x-xmin;
845        int previous = (y-ymin-1)*xsize + x-xmin;
846        if((isObj[current]&&!isObj[previous])   ||
847           (!isObj[current]&&isObj[previous])){
848          vertexSet.push_back(x);
849          vertexSet.push_back(y);
850          vertexSet.push_back(x+1);
851          vertexSet.push_back(y);
852        }
853      }
854    }
855    for(int y=ymin; y<=ymax; y++){
856      // now for each row...
857      for(int x=xmin+1;x<=xmax;x++){
858        int current  = (y-ymin)*xsize + x-xmin;
859        int previous = (y-ymin)*xsize + x-xmin - 1;
860        if((isObj[current]&&!isObj[previous])   ||
861           (!isObj[current]&&isObj[previous])){
862          vertexSet.push_back(x);
863          vertexSet.push_back(y);
864          vertexSet.push_back(x);
865          vertexSet.push_back(y+1);
866        }
867      }
868    }
869
870    return vertexSet;
871 
872  }
873
874 
875  void Detection::addDetection(Detection &other)
876  {
877    for(std::map<long, Object2D>::iterator it = other.chanlist.begin(); it!=other.chanlist.end();it++)
878      //      this->addChannel(*it);
879      this->addChannel(it->first, it->second);
880    this->haveParams = false; // make it appear as if the parameters haven't been calculated, so that we can re-calculate them 
881  }
882
883  Detection operator+ (Detection &lhs, Detection &rhs)
884  {
885    Detection output = lhs;
886    for(std::map<long, Object2D>::iterator it = rhs.chanlist.begin(); it!=rhs.chanlist.end();it++)
887      output.addChannel(it->first, it->second);
888    output.haveParams = false; // make it appear as if the parameters haven't been calculated, so that we can re-calculate them
889    return output;
890  }
891   
892
893  bool Detection::canMerge(Detection &other, Param &par)
894  {
895    bool near = this->isNear(other,par);
896    if(near) return this->isClose(other,par);
897    else return near;
898  }
899
900  bool Detection::isNear(Detection &other, Param &par)
901  {
902
903    bool flagAdj = par.getFlagAdjacent();
904    float threshS = par.getThreshS();
905    float threshV = par.getThreshV();
906
907    long gap;
908    if(flagAdj) gap = 1;
909    else gap = long( ceil(threshS) );
910
911    bool areNear;
912    // Test X ranges
913    if((this->xmin-gap)<other.xmin) areNear=((this->xmax+gap)>=other.xmin);
914    else areNear=(other.xmax>=(this->xmin-gap));
915    // Test Y ranges
916    if(areNear){
917      if((this->ymin-gap)<other.ymin) areNear=areNear&&((this->ymax+gap)>=other.ymin);
918      else areNear=areNear&&(other.ymax>=(this->ymin-gap));
919    }
920    // Test Z ranges
921    if(areNear){
922      gap = long(ceil(threshV));
923      if((this->zmin-gap)<other.zmin) areNear=areNear&&((this->zmax+gap)>=other.zmin);
924      else areNear=areNear&&(other.zmax>=(this->zmin-gap));
925    }
926   
927    return areNear;
928
929  }
930
931  bool Detection::isClose(Detection &other, Param &par)
932  {
933    bool close = false;   // this will be the value returned
934   
935    bool flagAdj = par.getFlagAdjacent();
936    float threshS = par.getThreshS();
937    float threshV = par.getThreshV();
938
939    //
940    // If we get to here, the pixel ranges overlap -- so we do a
941    // pixel-by-pixel comparison to make sure they are actually
942    // "close" according to the thresholds.  Otherwise, close=false,
943    // and so don't need to do anything else before returning.
944    //
945
946    std::vector<long> zlist1 = this->getChannelList();
947    std::vector<long> zlist2 = other.getChannelList();
948    Scan test1,test2;
949
950    for(size_t ct1=0; (!close && (ct1<zlist1.size())); ct1++){
951      for(size_t ct2=0; (!close && (ct2<zlist2.size())); ct2++){
952       
953        if(abs(zlist1[ct1]-zlist2[ct2])<=threshV){
954             
955          Object2D temp1 = this->getChanMap(zlist1[ct1]);
956          Object2D temp2 = other.getChanMap(zlist2[ct2]);
957
958          close = temp1.canMerge(temp2,threshS,flagAdj);
959
960        }
961
962      }
963    }
964       
965    return close;
966   
967  }
968
969
970
971}
Note: See TracBrowser for help on using the repository browser.