source: tags/release-1.1.9/src/Detection/detection.cc

Last change on this file was 681, checked in by MatthewWhiting, 14 years ago

A fix from ASKAP development. The Detection class now has a flag that records whether the WCS/flux parameters have been set. If they have, the calcObjectWCSparams functions
will not set them a second time. There's also a function to return that flag value, so that external functions can check whether parameters are set.

File size: 31.2 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(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 voxelList The list of Voxels with flux information
460    ///  \param head FitsHeader object that contains the WCS information.
461
462    const int border = 1;
463
464    if(!this->voxelListCovered(voxelList)){
465      duchampError("Detection::calcIntegFlux","Voxel list provided does not match");
466      return;
467    }
468
469    if(!head.is2D()){
470
471      this->haveParams = true;
472
473      // include one pixel either side in each direction
474      long xsize = (this->getXmax()-this->getXmin()+border*2+1);
475      long ysize = (this->getYmax()-this->getYmin()+border*2+1);
476      long zsize = (this->getZmax()-this->getZmin()+border*2+1);
477      long size = xsize*ysize*zsize;
478      std::vector <bool> isObj(size,false);
479      double *localFlux = new double[size];
480      for(int i=0;i<size;i++) localFlux[i]=0.;
481
482      std::vector<Voxel>::iterator vox;
483      for(vox=voxelList.begin();vox<voxelList.end();vox++){
484        if(this->isInObject(*vox)){
485          long x = vox->getX();
486          long y = vox->getY();
487          long z = vox->getZ();
488          long pos = (x-this->getXmin()+border) + (y-this->getYmin()+border)*xsize
489            + (z-this->getZmin()+border)*xsize*ysize;
490          localFlux[pos] = vox->getF();
491          isObj[pos] = true;
492        }
493      }
494 
495      // work out the WCS coords for each pixel
496      double *world  = new double[size];
497      double xpt,ypt,zpt;
498      for(int i=0;i<xsize*ysize*zsize;i++){
499        xpt = double( this->getXmin() - border + i%xsize );
500        ypt = double( this->getYmin() - border + (i/xsize)%ysize );
501        zpt = double( this->getZmin() - border + i/(xsize*ysize) );
502        world[i] = head.pixToVel(xpt,ypt,zpt);
503      }
504
505      double integrated = 0.;
506      for(int pix=0; pix<xsize*ysize; pix++){ // loop over each spatial pixel.
507        for(int z=0; z<zsize; z++){
508          int pos =  z*xsize*ysize + pix;
509          if(isObj[pos]){ // if it's an object pixel...
510            double deltaVel;
511            if(z==0)
512              deltaVel = (world[pos+xsize*ysize] - world[pos]);
513            else if(z==(zsize-1))
514              deltaVel = (world[pos] - world[pos-xsize*ysize]);
515            else
516              deltaVel = (world[pos+xsize*ysize] - world[pos-xsize*ysize]) / 2.;
517            integrated += localFlux[pos] * fabs(deltaVel);
518          }
519        }
520      }
521      this->intFlux = integrated;
522
523      delete [] world;
524      delete [] localFlux;
525
526      calcVelWidths(voxelList,head);
527
528    }
529    else // in this case there is just a 2D image.
530      this->intFlux = this->totalFlux;
531
532    if(head.isWCS()){
533      // correct for the beam size if the flux units string ends in "/beam"
534      if(head.needBeamSize()) this->intFlux  /= head.getBeamSize();
535    }
536
537  }
538  //--------------------------------------------------------------------
539
540  void Detection::calcIntegFlux(float *fluxArray, long *dim, FitsHeader &head)
541  {
542    ///  @details
543    ///  Uses the input WCS to calculate the velocity-integrated flux,
544    ///   putting velocity in units of km/s.
545    ///  Integrates over full spatial and velocity range as given
546    ///   by the extrema calculated by calcWCSparams.
547    ///
548    ///  If the flux units end in "/beam" (eg. Jy/beam), then the flux is
549    ///  corrected by the beam size (in pixels). This is done by
550    ///  multiplying the integrated flux by the number of spatial pixels,
551    ///  and dividing by the beam size in pixels (e.g. Jy/beam * pix /
552    ///  pix/beam --> Jy)
553    ///
554    ///  \param fluxArray The array of flux values.
555    ///  \param dim The dimensions of the flux array.
556    ///  \param head FitsHeader object that contains the WCS information.
557
558    if(!head.is2D()){
559
560      this->haveParams = true;
561
562      // include one pixel either side in each direction
563      long xsize = (this->xmax-this->xmin+3);
564      long ysize = (this->ymax-this->ymin+3);
565      long zsize = (this->zmax-this->zmin+3);
566      long size = xsize*ysize*zsize;
567      std::vector <bool> isObj(size,false);
568      double *localFlux = new double[size];
569      for(int i=0;i<size;i++) localFlux[i]=0.;
570      // work out which pixels are object pixels
571      for(std::map<long,Object2D>::iterator it=this->chanlist.begin();
572          it!=this->chanlist.end(); it++){
573        long z = it->first;
574        for(int s=0; s<it->second.getNumScan();s++){
575          long y = it->second.getScan(s).getY();
576          for(long x=it->second.getScan(s).getX();
577              x<=it->second.getScan(s).getXmax();
578              x++){
579            long pos = (x-this->xmin+1) + (y-this->ymin+1)*xsize
580              + (z-this->zmin+1)*xsize*ysize;
581            localFlux[pos] = fluxArray[x + y*dim[0] + z*dim[0]*dim[1]];
582            isObj[pos] = true;
583          }
584        }
585      }
586
587 
588      // work out the WCS coords for each pixel
589      double *world  = new double[size];
590      double xpt,ypt,zpt;
591      for(int i=0;i<xsize*ysize*zsize;i++){
592        xpt = double( this->getXmin() -1 + i%xsize );
593        ypt = double( this->getYmin() -1 + (i/xsize)%ysize );
594        zpt = double( this->getZmin() -1 + i/(xsize*ysize) );
595        world[i] = head.pixToVel(xpt,ypt,zpt);
596      }
597
598      double integrated = 0.;
599      for(int pix=0; pix<xsize*ysize; pix++){ // loop over each spatial pixel.
600        for(int z=0; z<zsize; z++){
601          int pos =  z*xsize*ysize + pix;
602          if(isObj[pos]){ // if it's an object pixel...
603            double deltaVel;
604            if(z==0)
605              deltaVel = (world[pos+xsize*ysize] - world[pos]);
606            else if(z==(zsize-1))
607              deltaVel = (world[pos] - world[pos-xsize*ysize]);
608            else
609              deltaVel = (world[pos+xsize*ysize] - world[pos-xsize*ysize]) / 2.;
610            integrated += localFlux[pos] * fabs(deltaVel);
611          }
612        }
613      }
614      this->intFlux = integrated;
615
616      delete [] world;
617      delete [] localFlux;
618
619      calcVelWidths(fluxArray, dim, head);
620
621    }
622    else // in this case there is just a 2D image.
623      this->intFlux = this->totalFlux;
624
625    if(head.isWCS()){
626      // correct for the beam size if the flux units string ends in "/beam"
627      if(head.needBeamSize()) this->intFlux  /= head.getBeamSize();
628    }
629
630  }
631  //--------------------------------------------------------------------
632
633  void Detection::calcVelWidths(std::vector<Voxel> voxelList, FitsHeader &head)
634  {
635    ///  @details
636    /// Calculates the widths of the detection at 20% and 50% of the
637    /// peak integrated flux. The procedure is as follows: first
638    /// generate an integrated flux spectrum (using all given voxels
639    /// that lie in the object's spatial map); find the peak; starting
640    /// at the spectral edges of the detection, move in or out until
641    /// you reach the 20% or 50% peak flux level. Linear interpolation
642    /// between points is done.
643    ///
644    ///  \param voxelList The list of Voxels with flux information
645    ///  \param head FitsHeader object that contains the WCS information.
646
647    this->haveParams = true;
648
649    const int border = 1;
650    long zsize = (this->getZmax()-this->getZmin()+border*2+1);
651    double xpt = double(this->getXcentre());
652    double ypt = double(this->getYcentre());
653    double zpt;
654
655    float *intSpec = new float[zsize];
656    for(int i=0;i<zsize;i++) intSpec[i]=0;
657       
658    Object2D spatMap = this->getSpatialMap();
659    for(int s=0;s<spatMap.getNumScan();s++){
660      std::vector<Voxel>::iterator vox;
661      for(vox=voxelList.begin();vox<voxelList.end();vox++){
662        if(spatMap.isInObject(*vox)){
663          if(vox->getZ()>=this->getZmin()-border &&
664             vox->getZ()<=this->getZmax()+border)
665            intSpec[vox->getZ()-this->getZmin()+1] += vox->getF();
666        }
667      }
668    }
669   
670    std::vector<std::pair<int,float> > goodPix;
671    float peak=0.;
672    int peakLoc=0;
673    for(int z=0;z<zsize;z++) {
674      if(z==0 || peak<intSpec[z]){
675        peak = intSpec[z];
676        peakLoc = z;
677      }
678      goodPix.push_back(std::pair<int,float>(z,intSpec[z]));
679    }
680
681    // finding the 20% & 50% points.  Start at the velmin & velmax
682    //  points. Then, if the int flux there is above the 20%/50%
683    //  limit, go out, otherwise go in. This is to deal with the
684    //  problems from double peaked sources.
685
686    int z;
687    bool goLeft;
688    z=border;
689    goLeft = intSpec[z]>peak*0.5;
690    if(goLeft) while(z>0 && intSpec[z]>peak*0.5) z--;
691    else       while(z<peakLoc && intSpec[z]<peak*0.5) z++;
692    if(z==0) this->v50min = this->velMin;
693    else{
694      if(goLeft) zpt = z + (peak*0.5-intSpec[z])/(intSpec[z+1]-intSpec[z]) + this->getZmin() - border;
695      else       zpt = z - (peak*0.5-intSpec[z])/(intSpec[z-1]-intSpec[z]) + this->getZmin() - border;
696      this->v50min = head.pixToVel(xpt,ypt,zpt);
697    }
698    z=this->getZmax()-this->getZmin();
699    goLeft = intSpec[z]<peak*0.5;
700    if(goLeft) while(z>peakLoc && intSpec[z]<peak*0.5) z--;
701    else       while(z<zsize && intSpec[z]>peak*0.5) z++;
702    if(z==zsize) this->v50max = this->velMax;
703    else{
704      if(goLeft) zpt = z + (peak*0.5-intSpec[z])/(intSpec[z+1]-intSpec[z]) + this->getZmin() - border;
705      else       zpt = z - (peak*0.5-intSpec[z])/(intSpec[z-1]-intSpec[z]) + this->getZmin() - border;
706      this->v50max = head.pixToVel(xpt,ypt,zpt);
707    }
708    z=border;
709    goLeft = intSpec[z]>peak*0.2;
710    if(goLeft) while(z>0 && intSpec[z]>peak*0.2) z--;
711    else       while(z<peakLoc && intSpec[z]<peak*0.2) z++;
712    if(z==0) this->v20min = this->velMin;
713    else{
714      if(goLeft) zpt = z + (peak*0.2-intSpec[z])/(intSpec[z+1]-intSpec[z]) + this->getZmin() - border;
715      else       zpt = z - (peak*0.2-intSpec[z])/(intSpec[z-1]-intSpec[z]) + this->getZmin() - border;
716      this->v20min = head.pixToVel(xpt,ypt,zpt);
717    }
718    z=this->getZmax()-this->getZmin();
719    goLeft = intSpec[z]<peak*0.2;
720    if(goLeft) while(z>peakLoc && intSpec[z]<peak*0.2) z--;
721    else       while(z<zsize && intSpec[z]>peak*0.2) z++;
722    if(z==zsize) this->v20max = this->velMax;
723    else{
724      if(goLeft) zpt = z + (peak*0.2-intSpec[z])/(intSpec[z+1]-intSpec[z]) + this->getZmin() - border;
725      else       zpt = z - (peak*0.2-intSpec[z])/(intSpec[z-1]-intSpec[z]) + this->getZmin() - border;
726      this->v20max = head.pixToVel(xpt,ypt,zpt);
727    }
728
729    this->w20 = fabs(this->v20min - this->v20max);
730    this->w50 = fabs(this->v50min - this->v50max);
731
732    delete [] intSpec;
733
734  }
735
736  //--------------------------------------------------------------------
737
738  void Detection::calcVelWidths(float *fluxArray, long *dim, FitsHeader &head)
739  {
740    ///  @details
741    /// Calculates the widths of the detection at 20% and 50% of the
742    /// peak integrated flux. The procedure is as follows: first
743    /// generate an integrated flux spectrum (summing each spatial
744    /// pixel's spectrum); find the peak; starting at the spectral
745    /// edges of the detection, move in or out until you reach the 20%
746    /// or 50% peak flux level. Linear interpolation between points is
747    /// done.
748    ///
749    ///  \param fluxArray The array of flux values.
750    ///  \param dim The dimensions of the flux array.
751    ///  \param head FitsHeader object that contains the WCS information.
752
753    if(dim[2] > 2){
754
755      this->haveParams = true;
756
757      double xpt = double(this->getXcentre());
758      double ypt = double(this->getYcentre());
759      double zpt;
760
761      float *intSpec = new float[dim[2]];
762      bool *mask = new bool[dim[0]*dim[1]*dim[2]];
763      for(int i=0;i<dim[0]*dim[1]*dim[2];i++) mask[i] = true;
764      getIntSpec(*this,fluxArray,dim,mask,1.,intSpec);
765
766      std::vector<std::pair<int,float> > goodPix;
767      float peak=0.;
768      int peakLoc=0;
769      for(int z=this->getZmin();z<=this->getZmax();z++) {
770        if(z==this->getZmin() || peak<intSpec[z]){
771          peak = intSpec[z];
772          peakLoc = z;
773        }
774        goodPix.push_back(std::pair<int,float>(z,intSpec[z]));
775      }
776
777      // finding the 20% & 50% points.  Start at the velmin & velmax
778      //  points. Then, if the int flux there is above the 20%/50%
779      //  limit, go out, otherwise go in. This is to deal with the
780      //  problems from double- (or multi-) peaked sources.
781
782      int z;
783      bool goLeft;
784      z=this->getZmin();
785      goLeft = intSpec[z]>peak*0.5;
786      if(goLeft) while(z>0 && intSpec[z]>peak*0.5) z--;
787      else       while(z<peakLoc && intSpec[z]<peak*0.5) z++;
788      if(z==0) this->v50min = this->velMin;
789      else{
790        if(goLeft) zpt = z + (peak*0.5-intSpec[z])/(intSpec[z+1]-intSpec[z]);
791        else       zpt = z - (peak*0.5-intSpec[z])/(intSpec[z-1]-intSpec[z]);
792        this->v50min = head.pixToVel(xpt,ypt,zpt);
793      }
794      z=this->getZmax();
795      goLeft = intSpec[z]<peak*0.5;
796      if(goLeft) while(z>peakLoc && intSpec[z]<peak*0.5) z--;
797      else       while(z<dim[2] && intSpec[z]>peak*0.5) z++;
798      if(z==dim[2]) this->v50max = this->velMax;
799      else{
800        if(goLeft) zpt = z + (peak*0.5-intSpec[z])/(intSpec[z+1]-intSpec[z]);
801        else       zpt = z - (peak*0.5-intSpec[z])/(intSpec[z-1]-intSpec[z]);
802        this->v50max = head.pixToVel(xpt,ypt,zpt);
803      }
804      z=this->getZmin();
805      goLeft = intSpec[z]>peak*0.2;
806      if(goLeft) while(z>0 && intSpec[z]>peak*0.2) z--;
807      else       while(z<peakLoc && intSpec[z]<peak*0.2) z++;
808      if(z==0) this->v20min = this->velMin;
809      else{
810        if(goLeft) zpt = z + (peak*0.2-intSpec[z])/(intSpec[z+1]-intSpec[z]);
811        else       zpt = z - (peak*0.2-intSpec[z])/(intSpec[z-1]-intSpec[z]);
812        this->v20min = head.pixToVel(xpt,ypt,zpt);
813      }
814      z=this->getZmax();
815      goLeft = intSpec[z]<peak*0.2;
816      if(goLeft) while(z>peakLoc && intSpec[z]<peak*0.2) z--;
817      else       while(z<dim[2] && intSpec[z]>peak*0.2) z++;
818      if(z==dim[2]) this->v20max = this->velMax;
819      else{
820        if(goLeft) zpt = z + (peak*0.2-intSpec[z])/(intSpec[z+1]-intSpec[z]);
821        else       zpt = z - (peak*0.2-intSpec[z])/(intSpec[z-1]-intSpec[z]);
822        this->v20max = head.pixToVel(xpt,ypt,zpt);
823      }
824
825      delete [] intSpec;
826      delete [] mask;
827
828    }
829    else{
830      this->v50min = this->v20min = this->velMin;
831      this->v50max = this->v20max = this->velMax;
832    }
833
834    this->w20 = fabs(this->v20min - this->v20max);
835    this->w50 = fabs(this->v50min - this->v50max);
836
837  }
838  //--------------------------------------------------------------------
839
840  void Detection::setOffsets(Param &par)
841  {
842    ///  @details
843    /// This function stores the values of the offsets for each cube axis.
844    /// The offsets are the starting values of the cube axes that may differ from
845    ///  the default value of 0 (for instance, if a subsection is being used).
846    /// The values will be used when the detection is outputted.
847
848    this->xSubOffset = par.getXOffset();
849    this->ySubOffset = par.getYOffset();
850    this->zSubOffset = par.getZOffset();
851  }
852  //--------------------------------------------------------------------
853
854  bool Detection::hasEnoughChannels(int minNumber)
855  {
856    ///  @details
857    /// A function to determine if the Detection has enough
858    /// contiguous channels to meet the minimum requirement
859    /// given as the argument.
860    /// \param minNumber How many channels is the minimum acceptable number?
861    /// \return True if there is at least one occurence of minNumber consecutive
862    /// channels present to return true. False otherwise.
863
864    // Preferred method -- need a set of minNumber consecutive channels present.
865
866    int numChan = this->getMaxAdjacentChannels();
867    bool result = (numChan >= minNumber);
868
869    return result;
870 
871  }
872  //--------------------------------------------------------------------
873
874  std::vector<int> Detection::getVertexSet()
875  {
876    ///  @details
877    /// Gets a list of points being the end-points of 1-pixel long
878    /// segments drawing a border around the spatial extend of a
879    /// detection. The vector is a series of 4 integers, being: x_0,
880    /// y_0, x_1, y_1.
881    /// \return The vector of vertex positions.
882
883    std::vector<int> vertexSet;
884
885    int xmin = this->getXmin() - 1;
886    int xmax = this->getXmax() + 1;
887    int ymin = this->getYmin() - 1;
888    int ymax = this->getYmax() + 1;
889    int xsize = xmax - xmin + 1;
890    int ysize = ymax - ymin + 1;
891
892    std::vector<Voxel> voxlist = this->getPixelSet();
893    std::vector<bool> isObj(xsize*ysize,false);
894    std::vector<Voxel>::iterator vox;
895    for(vox=voxlist.begin();vox<voxlist.end();vox++){
896      int pos = (vox->getX()-xmin) +
897        (vox->getY()-ymin)*xsize;
898      isObj[pos] = true;
899    }
900    voxlist.clear();
901   
902    for(int x=xmin; x<=xmax; x++){
903      // for each column...
904      for(int y=ymin+1;y<=ymax;y++){
905        int current  = (y-ymin)*xsize + x-xmin;
906        int previous = (y-ymin-1)*xsize + x-xmin;
907        if((isObj[current]&&!isObj[previous])   ||
908           (!isObj[current]&&isObj[previous])){
909          vertexSet.push_back(x);
910          vertexSet.push_back(y);
911          vertexSet.push_back(x+1);
912          vertexSet.push_back(y);
913        }
914      }
915    }
916    for(int y=ymin; y<=ymax; y++){
917      // now for each row...
918      for(int x=xmin+1;x<=xmax;x++){
919        int current  = (y-ymin)*xsize + x-xmin;
920        int previous = (y-ymin)*xsize + x-xmin - 1;
921        if((isObj[current]&&!isObj[previous])   ||
922           (!isObj[current]&&isObj[previous])){
923          vertexSet.push_back(x);
924          vertexSet.push_back(y);
925          vertexSet.push_back(x);
926          vertexSet.push_back(y+1);
927        }
928      }
929    }
930
931    return vertexSet;
932 
933  }
934
935
936  Detection operator+ (Detection lhs, Detection rhs)
937  {
938    Detection output = lhs;
939    for(std::map<long, Object2D>::iterator it = rhs.chanlist.begin(); it!=rhs.chanlist.end();it++)
940      output.addChannel(it->first, it->second);
941    output.haveParams = false; // make it appear as if the parameters haven't been calculated, so that we can re-calculate them
942    return output;
943  }
944   
945
946
947
948}
Note: See TracBrowser for help on using the repository browser.