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

Last change on this file since 500 was 489, checked in by MatthewWhiting, 16 years ago

Moving some pgplot-related commands

File size: 10.1 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    /**
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    /**
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
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, long *dimArray, bool *mask,
89                  float beamCorrection, float *spec)
90  {
91    /**
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
105    for(int i=0;i<dimArray[2];i++) spec[i] = 0.;
106    long xySize = dimArray[0]*dimArray[1];
107    bool *done = new bool[xySize];
108    for(int i=0;i<xySize;i++) done[i]=false;
109    std::vector<Voxel> voxlist = object.pixels().getPixelSet();
110    for(int pix=0;pix<voxlist.size();pix++){
111      int pos = voxlist[pix].getX() + dimArray[0] * voxlist[pix].getY();
112      if(!done[pos]){
113        done[pos] = true;
114        for(int z=0;z<dimArray[2];z++){
115          if(mask[pos+z*xySize]){
116            spec[z] += fluxArray[pos + z*xySize] / beamCorrection;
117          }         
118        }
119      }
120    }
121    delete [] done;
122
123  }
124  //--------------------------------------------------------------------
125
126  void getPeakSpec(Detection &object, float *fluxArray, long *dimArray, bool *mask, float *spec)
127  {
128    /**
129     *  The base function that extracts an peak spectrum for a
130     *  given object from a pixel array. The spectrum is returned as
131     *  the integrated flux, corrected for the beam using the given
132     *  correction factor.
133     *   \param object The Detection in question
134     *   \param fluxArray The full array of pixel values.
135     *   \param dimArray The axis dimensions for the fluxArray
136     *   \param mask A mask array indicating whether given pixels are valid
137     *   \param spec The peak spectrum for the object -- must be allocated first
138     */
139
140    long xySize = dimArray[0]*dimArray[1];
141    int pos = object.getXPeak() + dimArray[0]*object.getYPeak();
142    for(int z=0;z<dimArray[2];z++){
143      if(mask[pos + z*xySize])
144        spec[z] = fluxArray[pos + z*xySize];
145    }
146  }
147  //--------------------------------------------------------------------
148
149
150  void Cube::getSpectralArrays(int objNum, float *specx, float *specy,
151                               float *specRecon, float *specBase)
152  {
153    /**
154     *  A utility function that goes and calculates, for a given
155     *  Detection, the spectral arrays, according to whether we want
156     *  the peak or integrated flux. The arrays can be used by
157     *  Cube::plotSpectrum() and Cube::writeSpectralData(). The arrays
158     *  calculated are listed below. Their length is given by the
159     *  length of the Cube's spectral dimension.
160     *
161     *  Note that the arrays need to be allocated prior to calling
162     *  this function.
163     *
164     *  \param objNum The number of the object under consideration
165     *  \param specx The array of frequency/velocity/channel/etc
166     *         values (the x-axis on the spectral plot).
167     *  \param specy The array of flux values, matching the specx
168     *         array.
169     *  \param specRecon The reconstructed or smoothed array, done in
170     *         the same way as specy.
171     *  \param specBase The fitted baseline values, done in the same
172     *         way as specy.
173     */
174
175    long xdim = this->axisDim[0];
176    long ydim = this->axisDim[1];
177    long zdim = this->axisDim[2];
178       
179    for(int i=0;i<zdim;i++) specy[i]     = 0.;
180    for(int i=0;i<zdim;i++) specRecon[i] = 0.;
181    for(int i=0;i<zdim;i++) specBase[i]  = 0.;
182       
183    if(this->head.isWCS()){
184      double xval = double(this->objectList->at(objNum).getXcentre());
185      double yval = double(this->objectList->at(objNum).getYcentre());
186      for(double zval=0;zval<zdim;zval++)
187        specx[int(zval)] = this->head.pixToVel(xval,yval,zval);
188    }
189    else
190      for(double zval=0;zval<zdim;zval++) specx[int(zval)] = zval;
191       
192    float beamCorrection;
193    if(this->header().needBeamSize())
194      beamCorrection = this->par.getBeamSize();
195    else beamCorrection = 1.;
196       
197    if(this->par.getSpectralMethod()=="sum"){
198      bool *done = new bool[xdim*ydim];
199      for(int i=0;i<xdim*ydim;i++) done[i]=false;
200      std::vector<Voxel> voxlist = this->objectList->at(objNum).pixels().getPixelSet();
201      for(int pix=0;pix<voxlist.size();pix++){
202        int pos = voxlist[pix].getX() + xdim * voxlist[pix].getY();
203        if(!done[pos]){
204          done[pos] = true;
205          for(int z=0;z<zdim;z++){
206            if(!(this->isBlank(pos+z*xdim*ydim))){
207              specy[z] += this->array[pos + z*xdim*ydim] / beamCorrection;
208              if(this->reconExists)
209                specRecon[z] += this->recon[pos + z*xdim*ydim] / beamCorrection;
210              if(this->par.getFlagBaseline())
211                specBase[z] += this->baseline[pos + z*xdim*ydim] / beamCorrection;
212            }       
213          }
214        }
215      }
216      delete [] done;
217    }
218    else {// if(par.getSpectralMethod()=="peak"){
219      int pos = this->objectList->at(objNum).getXPeak() +
220        xdim*this->objectList->at(objNum).getYPeak();
221      for(int z=0;z<zdim;z++){
222        specy[z] = this->array[pos + z*xdim*ydim];
223        if(this->reconExists)
224          specRecon[z] = this->recon[pos + z*xdim*ydim];
225        if(this->par.getFlagBaseline())
226          specBase[z] = this->baseline[pos + z*xdim*ydim];
227      }
228    }
229
230//     long zdim = this->axisDim[2];
231//     Detection obj = this->objectList->at(objNum);
232//     getSpecAbscissae(obj, this->head, zdim, specx);
233
234//     float beamCorrection;
235//     if(this->header().needBeamSize())
236//       beamCorrection = this->par.getBeamSize();
237//     else beamCorrection = 1.;
238
239//     bool *mask = this->makeBlankMask();
240//     if(!this->reconExists)
241//       for(int i=0;i<this->axisDim[2];i++) specRecon[i] = 0.;
242//     if(!this->par.getFlagBaseline())
243//       for(int i=0;i<this->axisDim[2];i++) specBase[i]  = 0.;
244
245//     if(this->par.getSpectralMethod()=="sum"){
246//       getIntSpec(obj, this->array, this->axisDim, mask, beamCorrection, specy);
247//       if(this->reconExists){
248//      getIntSpec(obj, this->recon, this->axisDim, mask, beamCorrection, specRecon);
249//       }
250//       if(this->par.getFlagBaseline()){
251//      getIntSpec(obj, this->baseline, this->axisDim, mask, beamCorrection, specBase);
252//       }
253//     }
254//     else{ // if(.getSpectralMethod()=="peak"){
255//       getPeakSpec(obj, this->array, this->axisDim, mask, specy);
256//       if(this->reconExists)
257//      getPeakSpec(obj, this->recon, this->axisDim, mask, specRecon);
258//       if(this->par.getFlagBaseline())
259//      getPeakSpec(obj, this->baseline, this->axisDim, mask, specBase);
260//     }
261
262  }
263  //--------------------------------------------------------------------
264
265}
Note: See TracBrowser for help on using the repository browser.