source: trunk/Cubes/outputSpectra.cc @ 53

Last change on this file since 53 was 49, checked in by Matthew Whiting, 18 years ago

Added code to the spectral-plotting routine that allows plotting of the spectra
of WCS-less data -- the spectra are plotted as a function of z-pixel.
The information returned is just the pixel and flux information, both as the
spectrum header and the results printed to screen and to the output file.
The main file was changed so that it does not do the spectral plotting only if
there is no z-dimension, or there are no objects found.

File size: 6.8 KB
Line 
1#include <iostream>
2#include <iomanip>
3#include <sstream>
4#include <string>
5#include <stdio.h>
6#include <cpgplot.h>
7#include <math.h>
8#include <wcs.h>
9#include <Cubes/cubes.hh>
10#include <Cubes/plots.hh>
11#include <Utils/utils.hh>
12
13void getSmallVelRange(Detection &obj, wcsprm *wcs, float *minvel, float *maxvel);
14void getSmallZRange(Detection &obj, float *minz, float *maxz);
15void Cube::outputSpectra()
16{
17
18  string spectrafile = this->par.getSpectraFile() + "/vcps";
19  Plot::SpectralPlot newplot;
20  newplot.setUpPlot(spectrafile.c_str());
21
22  long xdim = this->axisDim[0];
23  long ydim = this->axisDim[1];
24  long zdim = this->axisDim[2];
25  int numObjects = this->objectList.size();
26  float beam = this->par.getBeamSize();
27
28  float *specx  = new float[zdim];
29  float *specy  = new float[zdim];
30  for(int i=0;i<zdim;i++) specy[i] = 0.;
31  float *specy2 = new float[zdim];
32  for(int i=0;i<zdim;i++) specy2[i] = 0.;
33
34  for(int nobj=0;nobj<numObjects;nobj++){
35    // for each object in the cube:
36    Detection *obj = new Detection;
37    *obj = this->objectList[nobj];
38    obj->calcParams();
39
40    for(int i=0;i<zdim;i++) specy[i] = 0.;
41    if(this->par.getFlagATrous()) 
42      for(int i=0;i<zdim;i++) specy2[i] = 0.;
43   
44    double x = double(obj->getXcentre());
45    double y = double(obj->getYcentre());
46    if(this->flagWCS)
47      for(double z=0;z<zdim;z++) specx[int(z)] = pixelToVelocity(this->wcs,x,y,z);
48    else
49      for(double z=0;z<zdim;z++) specx[int(z)] = z;
50
51    string fluxLabel = "Flux";
52
53    if(par.getSpectralMethod()=="sum"){
54      if(this->flagWCS) fluxLabel += " [Jy]";
55      bool *done = new bool[xdim*ydim];
56      for(int i=0;i<xdim*ydim;i++) done[i]=false;
57      int thisSize = obj->getSize();
58      for(int pix=0;pix<thisSize;pix++){
59        int pos = obj->getX(pix) + xdim * obj->getY(pix);
60        if(!done[pos]){
61          done[pos] = true;
62          for(int z=0;z<zdim;z++){
63            if(!(this->par.isBlank(this->array[pos+z*xdim*ydim]))){
64              specy[z] += this->array[pos + z*xdim*ydim] / beam;
65              if(this->par.getFlagATrous())
66                specy2[z] += this->recon[pos + z*xdim*ydim] / beam;
67            }
68          }
69        }
70      }
71      delete [] done;
72    }
73    else {// if(par.getSpectralMethod()=="peak"){
74      if(this->flagWCS) fluxLabel += " [Jy/beam]";
75      for(int z=0;z<zdim;z++){
76        int pos = obj->getXPeak() + xdim*obj->getYPeak();
77        specy[z] = this->array[pos + z*xdim*ydim];
78        if(this->par.getFlagATrous()) specy2[z] = this->recon[pos + z*xdim*ydim];
79      }
80    }
81   
82    float vmax,vmin;
83    vmax = vmin = specx[0];
84    for(int i=1;i<zdim;i++){
85      if(specx[i]>vmax) vmax=specx[i];
86      if(specx[i]<vmin) vmin=specx[i];
87    }
88    float max,min;
89    int loc=0;
90    max = min = specy[0];
91    for(int i=1;i<zdim;i++){
92      if(specy[i]>max) max=specy[i];
93      if(specy[i]<min){
94        min=specy[i];
95        loc = i;
96      }
97    }
98
99    // now plot the resulting spectrum
100    if(this->flagWCS) newplot.gotoHeader("Velocity [km s\\u-1\\d]");
101    else newplot.gotoHeader("Z-pixel value");
102
103    string label;
104    if(this->flagWCS){
105      label = obj->outputLabelWCS();
106      newplot.firstHeaderLine(label);
107    }
108    label = obj->outputLabelInfo();
109    newplot.secondHeaderLine(label);
110    label = obj->outputLabelPix();
111    newplot.thirdHeaderLine(label);
112   
113    newplot.gotoMainSpectrum(vmin,vmax,min,max,fluxLabel);
114    cpgline(zdim,specx,specy);
115    if(this->par.getFlagATrous()){
116      cpgsci(2);
117      cpgline(zdim,specx,specy2);   
118      cpgsci(1);
119    }
120    if(this->flagWCS) newplot.drawVelRange(obj->getVelMin(),obj->getVelMax());
121    else newplot.drawVelRange(obj->getZmin(),obj->getZmax());
122
123    /**************************/
124    // ZOOM IN SPECTRALLY ON THE DETECTION.
125
126    float minvel,maxvel;
127    if(this->flagWCS) getSmallVelRange(*obj,this->wcs,&minvel,&maxvel);
128    else getSmallZRange(*obj,&minvel,&maxvel);
129
130    // Find new max & min flux values
131    swap(max,min);
132    int ct = 0;
133    for(int i=0;i<zdim;i++){
134      if((specx[i]>=minvel)&&(specx[i]<=maxvel)){
135        ct++;
136        if(specy[i]>max) max=specy[i];
137        if(specy[i]<min) min=specy[i];
138      }
139    }
140    // widen the flux range slightly so that the top & bottom don't lie on the axes.
141    float width = max - min;
142    max += width * 0.05;
143    min -= width * 0.05;
144
145    newplot.gotoZoomSpectrum(minvel,maxvel,min,max);
146    cpgline(zdim,specx,specy);
147    if(this->par.getFlagATrous()){
148      cpgsci(2);
149      cpgline(zdim,specx,specy2);   
150      cpgsci(1);
151    }
152    if(this->flagWCS) newplot.drawVelRange(obj->getVelMin(),obj->getVelMax());
153    else newplot.drawVelRange(obj->getZmin(),obj->getZmax());
154   
155    /**************************/
156
157    // DRAW THE MOMENT MAP OF THE DETECTION -- SUMMED OVER ALL CHANNELS
158    newplot.gotoMap();
159    drawMomentCutout(*this,*obj);
160
161    delete obj;
162
163  }// end of loop over objects.
164
165  delete [] specx;
166  delete [] specy;
167  delete [] specy2;
168 
169}
170
171void getSmallVelRange(Detection &obj, wcsprm *wcs, float *minvel, float *maxvel)
172{
173  // Want the velocity range for the zoomed in region to be maximum of
174  // 20 pixels or 3x width of detection. Need to:
175  //   * Calculate pixel width of a 3x-detection-width region.
176  //   * If smaller than 20, calculate velocities of central vel +- 10 pixels
177  //   * If not, use those velocities
178
179  double *pixcrd = new double[3];
180  double *world  = new double[3];
181  float minpix,maxpix;
182  // define new velocity extrema -- make it 3x wider than the width of the detection.
183  *minvel = 0.5*(obj.getVelMin()+obj.getVelMax()) - 1.5*obj.getVelWidth();
184  *maxvel = 0.5*(obj.getVelMin()+obj.getVelMax()) + 1.5*obj.getVelWidth();
185  // Find velocity range in number of pixels:
186  world[0] = obj.getRA();
187  world[1] = obj.getDec();
188  world[2] = velToCoord(wcs,*minvel);
189  wcsToPixSingle(wcs,world,pixcrd);
190  minpix = pixcrd[2];
191  world[2] = velToCoord(wcs,*maxvel);
192  wcsToPixSingle(wcs,world,pixcrd);
193  maxpix = pixcrd[2];
194  if(maxpix<minpix) swap(maxpix,minpix);
195   
196  if((maxpix - minpix + 1) < 20){
197    pixcrd[0] = double(obj.getXcentre());
198    pixcrd[1] = double(obj.getYcentre());
199    pixcrd[2] = obj.getZcentre() - 10.;
200    pixToWCSSingle(wcs,pixcrd,world);
201    *minvel = setVel_kms(wcs,world[2]);
202    pixcrd[2] = obj.getZcentre() + 10.;
203    pixToWCSSingle(wcs,pixcrd,world);
204    *maxvel = setVel_kms(wcs,world[2]);
205    if(*maxvel<*minvel) swap(*maxvel,*minvel);
206  }
207  delete [] pixcrd;
208  delete [] world;
209
210}
211
212void getSmallZRange(Detection &obj, float *minz, float *maxz)
213{
214  // Want the z-pixel range for the zoomed in region to be maximum of
215  // 20 pixels or 3x width of detection. Need to:
216  //   * Calculate pixel width of a 3x-detection-width region.
217  //   * If smaller than 20, use central pixel +- 10 pixels
218
219
220  // define new velocity extrema -- make it 3x wider than the width of the detection.
221  *minz = 2.*obj.getZmin() - obj.getZmax();
222  *maxz = 2.*obj.getZmax() - obj.getZmin();
223   
224  if((*maxz - *minz + 1) < 20){
225    *minz = obj.getZcentre() - 10.;
226    *maxz = obj.getZcentre() + 10.;
227  }
228
229}
Note: See TracBrowser for help on using the repository browser.