source: tags/release-1.1.9/src/Cubes/spectraUtils.cc @ 1441

Last change on this file since 1441 was 623, checked in by MatthewWhiting, 15 years ago

Changing references of unsigned ints to size_t, or to use of iterators. Also fixing one bug in Object2D::getNumDistinctX, where we weren't looking at all scans. This
should fix ticket #63.

File size: 10.2 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 <math.h>
34#include <wcslib/wcs.h>
35#include <duchamp/Cubes/cubeUtils.hh>
36#include <duchamp/param.hh>
37#include <duchamp/duchamp.hh>
38#include <duchamp/fitsHeader.hh>
39#include <duchamp/PixelMap/Object3D.hh>
40#include <duchamp/Cubes/cubes.hh>
41#include <duchamp/Utils/utils.hh>
42
43using namespace PixelInfo;
44
45namespace duchamp
46{
47
48  void getSpecAbscissae(Detection &object, FitsHeader &head, long zdim, float *abscissae)
49  {
50    /// @details
51    ///  A function that returns an array of
52    ///  frequency/velocity/channel/etc values (that can be used as the
53    ///  abscissae on the spectral plot).
54    ///  \param object The object on which our spectrum is centered (in
55    ///  case the spectral value changes with x & y
56    ///  \param head The FitsHeader set of parameters that determine the coordinate transformation.
57    ///  \param zdim The length of the spectral axis
58    ///  \param abscissae The array of spectral values -- must be allocated first
59
60    getSpecAbscissae(head,object.getXcentre(),object.getYcentre(),zdim, abscissae);
61  }
62
63  void getSpecAbscissae(FitsHeader &head, float xpt, float ypt, long zdim, float *abscissae)
64  {
65    /// @details
66    ///  A function that returns an array of
67    ///  frequency/velocity/channel/etc values (that can be used as the
68    ///  horizontal axis on the spectral plot).
69    ///  \param head The FitsHeader set of parameters that determine the coordinate transformation.
70    ///  \param xpt The x-value of the spatial position on which our spectrum is centred.
71    ///  \param ypt The y-value of the spatial position on which our spectrum is centred.
72    ///  \param zdim The length of the spectral axis
73    ///  \param abscissae The array of spectral values -- must be allocated first.
74
75    if(head.isWCS()){
76      double xval = double(xpt);
77      double yval = double(ypt);
78      for(double zval=0;zval<zdim;zval++)
79        abscissae[int(zval)] = head.pixToVel(xval,yval,zval);
80    }
81    else
82      for(double zval=0;zval<zdim;zval++) abscissae[int(zval)] = zval;
83
84  }
85  //--------------------------------------------------------------------
86
87  void getIntSpec(Detection &object, float *fluxArray, long *dimArray, bool *mask,
88                  float beamCorrection, float *spec)
89  {
90    /// @details
91    ///  The base function that extracts an integrated spectrum for a
92    ///  given object from a pixel array. The spectrum is returned as
93    ///  the integrated flux, corrected for the beam using the given
94    ///  correction factor.
95    ///   \param object The Detection in question
96    ///   \param fluxArray The full array of pixel values.
97    ///   \param dimArray The axis dimensions for the fluxArray
98    ///   \param mask A mask array indicating whether given pixels are valid
99    ///   \param beamCorrection How much to divide the summed spectrum
100    ///   by to return the integrated flux.
101    ///   \param spec The integrated spectrum for the object -- must be allocated first.
102
103    for(int i=0;i<dimArray[2];i++) spec[i] = 0.;
104    long xySize = dimArray[0]*dimArray[1];
105    bool *done = new bool[xySize];
106    for(int i=0;i<xySize;i++) done[i]=false;
107    std::vector<Voxel> voxlist = object.getPixelSet();
108    std::vector<Voxel>::iterator vox;
109    for(vox=voxlist.begin();vox<voxlist.end();vox++){
110      int pos = vox->getX() + dimArray[0] * vox->getY();
111      if(!done[pos]){
112        done[pos] = true;
113        for(int 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    delete [] done;
121
122  }
123  //--------------------------------------------------------------------
124
125  void getPeakSpec(Detection &object, float *fluxArray, long *dimArray, bool *mask, float *spec)
126  {
127    /// @details
128    ///  The base function that extracts an peak spectrum for a
129    ///  given object from a pixel array. The spectrum is returned as
130    ///  the integrated flux, corrected for the beam using the given
131    ///  correction factor.
132    ///   \param object The Detection in question
133    ///   \param fluxArray The full array of pixel values.
134    ///   \param dimArray The axis dimensions for the fluxArray
135    ///   \param mask A mask array indicating whether given pixels are valid
136    ///   \param spec The peak spectrum for the object -- must be allocated first
137
138    long xySize = dimArray[0]*dimArray[1];
139    int pos = object.getXPeak() + dimArray[0]*object.getYPeak();
140    for(int z=0;z<dimArray[2];z++){
141      if(mask[pos + z*xySize])
142        spec[z] = fluxArray[pos + z*xySize];
143    }
144  }
145  //--------------------------------------------------------------------
146
147
148  void Cube::getSpectralArrays(int objNum, float *specx, float *specy,
149                               float *specRecon, float *specBase)
150  {
151    /// @details
152    ///  A utility function that goes and calculates, for a given
153    ///  Detection, the spectral arrays, according to whether we want
154    ///  the peak or integrated flux. The arrays can be used by
155    ///  Cube::plotSpectrum() and Cube::writeSpectralData(). The arrays
156    ///  calculated are listed below. Their length is given by the
157    ///  length of the Cube's spectral dimension.
158    ///
159    ///  Note that the arrays need to be allocated prior to calling
160    ///  this function.
161    ///
162    ///  \param objNum The number of the object under consideration
163    ///  \param specx The array of frequency/velocity/channel/etc
164    ///         values (the x-axis on the spectral plot).
165    ///  \param specy The array of flux values, matching the specx
166    ///         array.
167    ///  \param specRecon The reconstructed or smoothed array, done in
168    ///         the same way as specy.
169    ///  \param specBase The fitted baseline values, done in the same
170    ///         way as specy.
171
172    long xdim = this->axisDim[0];
173    long ydim = this->axisDim[1];
174    long zdim = this->axisDim[2];
175       
176    for(int i=0;i<zdim;i++) specy[i]     = 0.;
177    for(int i=0;i<zdim;i++) specRecon[i] = 0.;
178    for(int i=0;i<zdim;i++) specBase[i]  = 0.;
179       
180    if(this->head.isWCS()){
181      double xval = double(this->objectList->at(objNum).getXcentre());
182      double yval = double(this->objectList->at(objNum).getYcentre());
183      for(double zval=0;zval<zdim;zval++)
184        specx[int(zval)] = this->head.pixToVel(xval,yval,zval);
185    }
186    else
187      for(double zval=0;zval<zdim;zval++) specx[int(zval)] = zval;
188       
189    float beamCorrection;
190    if(this->header().needBeamSize())
191      beamCorrection = this->par.getBeamSize();
192    else beamCorrection = 1.;
193       
194    if(this->par.getSpectralMethod()=="sum"){
195      bool *done = new bool[xdim*ydim];
196      for(int i=0;i<xdim*ydim;i++) done[i]=false;
197      std::vector<Voxel> voxlist = this->objectList->at(objNum).getPixelSet();
198      std::vector<Voxel>::iterator vox;
199      for(vox=voxlist.begin();vox<voxlist.end();vox++){
200        int pos = vox->getX() + xdim * vox->getY();
201        if(!done[pos]){
202          done[pos] = true;
203          for(int z=0;z<zdim;z++){
204            if(!(this->isBlank(pos+z*xdim*ydim))){
205              specy[z] += this->array[pos + z*xdim*ydim] / beamCorrection;
206              if(this->reconExists)
207                specRecon[z] += this->recon[pos + z*xdim*ydim] / beamCorrection;
208              if(this->par.getFlagBaseline())
209                specBase[z] += this->baseline[pos + z*xdim*ydim] / beamCorrection;
210            }       
211          }
212        }
213      }
214      delete [] done;
215    }
216    else {// if(par.getSpectralMethod()=="peak"){
217      int pos = this->objectList->at(objNum).getXPeak() +
218        xdim*this->objectList->at(objNum).getYPeak();
219      for(int z=0;z<zdim;z++){
220        specy[z] = this->array[pos + z*xdim*ydim];
221        if(this->reconExists)
222          specRecon[z] = this->recon[pos + z*xdim*ydim];
223        if(this->par.getFlagBaseline())
224          specBase[z] = this->baseline[pos + z*xdim*ydim];
225      }
226    }
227
228//     long zdim = this->axisDim[2];
229//     Detection obj = this->objectList->at(objNum);
230//     getSpecAbscissae(obj, this->head, zdim, specx);
231
232//     float beamCorrection;
233//     if(this->header().needBeamSize())
234//       beamCorrection = this->par.getBeamSize();
235//     else beamCorrection = 1.;
236
237//     bool *mask = this->makeBlankMask();
238//     if(!this->reconExists)
239//       for(int i=0;i<this->axisDim[2];i++) specRecon[i] = 0.;
240//     if(!this->par.getFlagBaseline())
241//       for(int i=0;i<this->axisDim[2];i++) specBase[i]  = 0.;
242
243//     if(this->par.getSpectralMethod()=="sum"){
244//       getIntSpec(obj, this->array, this->axisDim, mask, beamCorrection, specy);
245//       if(this->reconExists){
246//      getIntSpec(obj, this->recon, this->axisDim, mask, beamCorrection, specRecon);
247//       }
248//       if(this->par.getFlagBaseline()){
249//      getIntSpec(obj, this->baseline, this->axisDim, mask, beamCorrection, specBase);
250//       }
251//     }
252//     else{ // if(.getSpectralMethod()=="peak"){
253//       getPeakSpec(obj, this->array, this->axisDim, mask, specy);
254//       if(this->reconExists)
255//      getPeakSpec(obj, this->recon, this->axisDim, mask, specRecon);
256//       if(this->par.getFlagBaseline())
257//      getPeakSpec(obj, this->baseline, this->axisDim, mask, specBase);
258//     }
259
260  }
261  //--------------------------------------------------------------------
262
263}
Note: See TracBrowser for help on using the repository browser.