source: trunk/Cubes/outputSpectra.cc @ 9

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

Added code in outputSpectra.cc to widen the y-range in the
zoomed spectral plot, so that the max & min are not lost
against the axes.

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