source: trunk/src/Cubes/spectraUtils.cc @ 1441

Last change on this file since 1441 was 1431, checked in by MatthewWhiting, 7 years ago

Fixing #525, where we need to define xloc & yloc for both strands of
the if clause. This stops us having a WCS error.

File size: 12.7 KB
Line 
1// -----------------------------------------------------------------------
2// spectraUtils.cc: Utility functions to obtain & manipulate spectra
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 <fstream>
30#include <iomanip>
31#include <sstream>
32#include <string>
33#include <vector>
34#include <math.h>
35#include <wcslib/wcs.h>
36#include <duchamp/Cubes/cubeUtils.hh>
37#include <duchamp/param.hh>
38#include <duchamp/duchamp.hh>
39#include <duchamp/fitsHeader.hh>
40#include <duchamp/PixelMap/Object3D.hh>
41#include <duchamp/Cubes/cubes.hh>
42#include <duchamp/Utils/utils.hh>
43
44using namespace PixelInfo;
45
46namespace duchamp
47{
48
49  void getSpecAbscissae(Detection &object, FitsHeader &head, size_t zdim, float *abscissae)
50  {
51    /// @details
52    ///  A function that returns an array of
53    ///  frequency/velocity/channel/etc values (that can be used as the
54    ///  abscissae on the spectral plot).
55    ///  \param object The object on which our spectrum is centered (in
56    ///  case the spectral value changes with x & y
57    ///  \param head The FitsHeader set of parameters that determine the coordinate transformation.
58    ///  \param zdim The length of the spectral axis
59    ///  \param abscissae The array of spectral values -- must be allocated first
60
61    getSpecAbscissae(head,object.getXcentre(),object.getYcentre(),zdim, abscissae);
62  }
63
64  void getSpecAbscissae(FitsHeader &head, float xpt, float ypt, size_t zdim, float *abscissae)
65  {
66    /// @details
67    ///  A function that returns an array of
68    ///  frequency/velocity/channel/etc values (that can be used as the
69    ///  horizontal axis on the spectral plot).
70    ///  \param head The FitsHeader set of parameters that determine the coordinate transformation.
71    ///  \param xpt The x-value of the spatial position on which our spectrum is centred.
72    ///  \param ypt The y-value of the spatial position on which our spectrum is centred.
73    ///  \param zdim The length of the spectral axis
74    ///  \param abscissae The array of spectral values -- must be allocated first.
75
76    if(head.isWCS()){
77      double xval = double(xpt);
78      double yval = double(ypt);
79      for(double zval=0;zval<zdim;zval++)
80        abscissae[int(zval)] = head.pixToVel(xval,yval,zval);
81    }
82    else
83      for(double zval=0;zval<zdim;zval++) abscissae[int(zval)] = zval;
84
85  }
86  //--------------------------------------------------------------------
87
88    void getIntSpec(Detection &object, float *fluxArray, size_t *dimArray, std::vector<bool> mask,
89                  float beamCorrection, float *spec)
90  {
91    /// @details
92    ///  The base function that extracts an integrated spectrum for a
93    ///  given object from a pixel array. The spectrum is returned as
94    ///  the integrated flux, corrected for the beam using the given
95    ///  correction factor.
96    ///   \param object The Detection in question
97    ///   \param fluxArray The full array of pixel values.
98    ///   \param dimArray The axis dimensions for the fluxArray
99    ///   \param mask A mask array indicating whether given pixels are valid
100    ///   \param beamCorrection How much to divide the summed spectrum
101    ///   by to return the integrated flux.
102    ///   \param spec The integrated spectrum for the object -- must be allocated first.
103
104    for(size_t i=0;i<dimArray[2];i++) spec[i] = 0.;
105    size_t xySize = dimArray[0]*dimArray[1];
106    std::vector<bool> done(xySize,false);
107    std::vector<Voxel> voxlist = object.getPixelSet();
108    std::vector<Voxel>::iterator vox;
109    for(vox=voxlist.begin();vox<voxlist.end();vox++){
110      size_t pos = vox->getX() + dimArray[0] * vox->getY();
111      if(!done[pos]){
112        done[pos] = true;
113        for(size_t z=0;z<dimArray[2];z++){
114          if(mask[pos+z*xySize]){
115            spec[z] += fluxArray[pos + z*xySize] / beamCorrection;
116          }         
117        }
118      }
119    }
120
121  }
122  //--------------------------------------------------------------------
123
124    void getPeakSpec(Detection &object, float *fluxArray, size_t *dimArray, std::vector<bool> mask, float *spec)
125  {
126    /// @details
127    ///  The base function that extracts an peak spectrum for a
128    ///  given object from a pixel array. The spectrum is returned as
129    ///  the integrated flux, corrected for the beam using the given
130    ///  correction factor.
131    ///   \param object The Detection in question
132    ///   \param fluxArray The full array of pixel values.
133    ///   \param dimArray The axis dimensions for the fluxArray
134    ///   \param mask A mask array indicating whether given pixels are valid
135    ///   \param spec The peak spectrum for the object -- must be allocated first
136
137    if((object.getXPeak()<0 || object.getXPeak()>=int(dimArray[0])) || (object.getYPeak()<0 || object.getYPeak()>=int(dimArray[1]))){
138      DUCHAMPWARN("getPeakSpec","Object peak outside array boundaries");
139      for (size_t z=0;z<dimArray[2];z++) spec[z]=0.;
140    }
141    else{
142      size_t xySize = dimArray[0]*dimArray[1];
143      size_t pos = object.getXPeak() + dimArray[0]*object.getYPeak();
144      for(size_t z=0;z<dimArray[2];z++){
145        if(mask[pos + z*xySize])
146          spec[z] = fluxArray[pos + z*xySize];
147      }
148    }
149  }
150  //--------------------------------------------------------------------
151
152
153  void Cube::getSpectralArrays(int objNum, float *specx, float *specy,
154                               float *specRecon, float *specBase)
155  {
156    /// @details
157    ///  A utility function that goes and calculates, for a given
158    ///  Detection, the spectral arrays, according to whether we want
159    ///  the peak or integrated flux. The arrays can be used by
160    ///  Cube::plotSpectrum() and Cube::writeSpectralData(). The arrays
161    ///  calculated are listed below. Their length is given by the
162    ///  length of the Cube's spectral dimension.
163    ///
164    ///  Note that the arrays need to be allocated prior to calling
165    ///  this function.
166    ///
167    ///  \param objNum The number of the object under
168    ///         consideration. If negative, we extract the single
169    ///         spectrum at (x,y)=(0,0) (useful for the 1D case).
170    ///  \param specx The array of frequency/velocity/channel/etc
171    ///         values (the x-axis on the spectral plot).
172    ///  \param specy The array of flux values, matching the specx
173    ///         array.
174    ///  \param specRecon The reconstructed or smoothed array, done in
175    ///         the same way as specy.
176    ///  \param specBase The fitted baseline values, done in the same
177    ///         way as specy.
178
179    size_t xdim = this->axisDim[0];
180    size_t ydim = this->axisDim[1];
181    size_t zdim = this->axisDim[2];
182       
183    for(size_t i=0;i<zdim;i++){
184      specy[i]     = 0.;
185      specRecon[i] = 0.;
186      specBase[i]  = 0.;
187    }
188
189    double xloc,yloc;
190    size_t spatpos=0;
191    std::vector<Voxel> voxlist;
192    if(objNum>=0){
193      if(this->par.getSpectralMethod()=="sum"){
194        xloc=double(this->objectList->at(objNum).getXcentre());
195        yloc=double(this->objectList->at(objNum).getYcentre());
196        voxlist = this->objectList->at(objNum).getPixelSet();
197      }
198      else{
199        spatpos = this->objectList->at(objNum).getXPeak() +
200        xdim*this->objectList->at(objNum).getYPeak();
201        xloc = spatpos % xdim;
202        yloc = spatpos / xdim;
203      }
204    }
205
206    if(this->head.isWCS()){
207      for(double zval=0;zval<zdim;zval++)
208        specx[int(zval)] = this->head.pixToVel(xloc,yloc,zval);
209    }
210    else {
211      for(double zval=0;zval<zdim;zval++) specx[int(zval)] = zval;
212    }
213       
214    float beamCorrection;
215    if(this->header().needBeamSize())
216      beamCorrection = this->head.beam().area();
217    else beamCorrection = 1.;
218       
219    if(objNum>=0 && this->par.getSpectralMethod()=="sum"){
220      std::vector<bool> done(xdim*ydim,false);
221      std::vector<Voxel>::iterator vox;
222      for(vox=voxlist.begin();vox<voxlist.end();vox++){
223        spatpos = vox->getX() + xdim * vox->getY();
224        if(!done[spatpos]){
225          done[spatpos] = true;
226          for(size_t z=0;z<zdim;z++){
227            if(!(this->isBlank(spatpos+z*xdim*ydim))){
228              specy[z] += this->array[spatpos + z*xdim*ydim] / beamCorrection;
229              if(this->reconExists)
230                specRecon[z] += this->recon[spatpos + z*xdim*ydim] / beamCorrection;
231              if(this->par.getFlagBaseline())
232                specBase[z] += this->baseline[spatpos + z*xdim*ydim] / beamCorrection;
233            }       
234          }
235        }
236      }
237    }
238    else {// if(par.getSpectralMethod()=="peak"){
239//       size_t spatpos = this->objectList->at(objNum).getXPeak() +
240//      xdim*this->objectList->at(objNum).getYPeak();
241      for(size_t z=0;z<zdim;z++){
242        specy[z] = this->array[spatpos + z*xdim*ydim];
243        if(this->reconExists)
244          specRecon[z] = this->recon[spatpos + z*xdim*ydim];
245        if(this->par.getFlagBaseline())
246          specBase[z] = this->baseline[spatpos + z*xdim*ydim];
247      }
248    }
249
250  }
251  //--------------------------------------------------------------------
252
253  void getSmallVelRange(Detection &obj, FitsHeader &head,
254                        double *minvel, double *maxvel)
255  {
256    ///  @details
257    ///  Routine to calculate the velocity range for the zoomed-in region.
258    ///  This range should be the maximum of 20 pixels, or 3x the wdith of
259    ///   the detection.
260    ///  Need to :
261    ///      Calculate pixel width of a 3x-detection-width region.
262    ///      If smaller than 20, calculate velocities of central vel +- 10 pixels
263    ///      If not, use the 3x-detection-width
264    ///  Range returned via "minvel" and "maxvel" parameters.
265    ///  \param obj Detection under examination.
266    ///  \param head FitsHeader, containing the WCS information.
267    ///  \param minvel Returned value of minimum velocity
268    ///  \param maxvel Returned value of maximum velocity
269
270    double *pixcrd = new double[3];
271    double *world  = new double[3];
272    float minpix,maxpix;
273    // define new velocity extrema
274    //    -- make it 3x wider than the width of the detection.
275    *minvel = 0.5*(obj.getVelMin()+obj.getVelMax()) - 1.5*obj.getVelWidth();
276    *maxvel = 0.5*(obj.getVelMin()+obj.getVelMax()) + 1.5*obj.getVelWidth();
277    // Find velocity range in number of pixels:
278    world[0] = obj.getRA();
279    world[1] = obj.getDec();
280    world[2] = head.velToSpec(*minvel);
281    head.wcsToPix(world,pixcrd);
282    minpix = pixcrd[2];
283    world[2] = head.velToSpec(*maxvel);
284    head.wcsToPix(world,pixcrd);
285    maxpix = pixcrd[2];
286    if(maxpix<minpix) std::swap(maxpix,minpix);
287   
288    if((maxpix - minpix + 1) < 20){
289      pixcrd[0] = double(obj.getXcentre());
290      pixcrd[1] = double(obj.getYcentre());
291      pixcrd[2] = obj.getZcentre() - 10.;
292      head.pixToWCS(pixcrd,world);
293      //    *minvel = setVel_kms(wcs,world[2]);
294      *minvel = head.specToVel(world[2]);
295      pixcrd[2] = obj.getZcentre() + 10.;
296      head.pixToWCS(pixcrd,world);
297      //     *maxvel = setVel_kms(wcs,world[2]);
298      *maxvel = head.specToVel(world[2]);
299      if(*maxvel<*minvel) std::swap(*maxvel,*minvel);
300    }
301    delete [] pixcrd;
302    delete [] world;
303
304  }
305  //--------------------------------------------------------------------
306
307  void getSmallZRange(Detection &obj, double *minz, double *maxz)
308  {
309    ///  @details
310    ///  Routine to calculate the pixel range for the zoomed-in spectrum.
311    ///  This range should be the maximum of 20 pixels, or 3x the width
312    ///   of the detection.
313    ///  Need to :
314    ///      Calculate pixel width of a 3x-detection-width region.
315    ///       If smaller than 20, use central pixel +- 10 pixels
316    ///  Range returned via "minz" and "maxz" parameters.
317    ///  \param obj Detection under examination.
318    ///  \param minz Returned value of minimum z-pixel coordinate
319    ///  \param maxz Returned value of maximum z-pixel coordinate
320
321    *minz = 2.*obj.getZmin() - obj.getZmax();
322    *maxz = 2.*obj.getZmax() - obj.getZmin();
323   
324    if((*maxz - *minz + 1) < 20){
325      *minz = obj.getZcentre() - 10.;
326      *maxz = obj.getZcentre() + 10.;
327    }
328
329  }
330  //--------------------------------------------------------------------
331
332}
Note: See TracBrowser for help on using the repository browser.