source: trunk/Cubes/drawMomentCutout.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: 4.8 KB
Line 
1#include <iostream>
2#include <cpgplot.h>
3#include <math.h>
4#include <wcs.h>
5#include <Cubes/cubes.hh>
6#include <Utils/utils.hh>
7
8void drawMomentCutout(Cube &cube, Detection &object)
9{
10  float x1,x2,y1,y2;
11  cpgqwin(&x1,&x2,&y1,&y2);
12
13  long size = (object.getXmax()-object.getXmin()+1);
14  if(size<(object.getYmax()-object.getYmin()+1))
15    size = object.getYmax()-object.getYmin()+1;
16  size += 20;
17
18  float blankVal = cube.pars().getBlankPixVal();
19
20  long xmin = (object.getXmax()+object.getXmin())/2 - size/2 + 1;
21  long xmax = (object.getXmax()+object.getXmin())/2 + size/2;
22  long ymin = (object.getYmax()+object.getYmin())/2 - size/2 + 1;
23  long ymax = (object.getYmax()+object.getYmin())/2 + size/2;
24  long zmin = object.getZmin();
25  long zmax = object.getZmax();
26
27  float *image = new float[size*size];
28  for(int i=0;i<size*size;i++) image[i]=blankVal;
29  long *dim = new long[3];
30  cube.getDimArray(dim);
31
32  int imPos,cubePos;
33  float val;
34  for(int z=zmin; z<=zmax; z++){
35    for(int x=xmin; x<=xmax; x++){
36      for(int y=ymin; y<=ymax; y++){
37        imPos = (y-ymin) * size + (x-xmin);
38        cubePos = (z)*dim[0]*dim[1]+(y)*dim[0]+(x);
39        if((x<0)||(x>=dim[0])||(y<0)||(y>=dim[1])) // if outside the boundaries
40          image[imPos] = blankVal;
41        else{
42          val = cube.getPixValue(cubePos);
43          if (!cube.pars().isBlank(val)) // if pixel's not blank
44            image[imPos] += val;
45        }
46      }
47    }
48  }
49
50  for(int i=0;i<size*size;i++){
51    if(image[i]!=blankVal){ // if there is some signal on this pixel
52      image[i]-=blankVal;   // remove the starting value so we just have the signal
53      image[i] /= float(zmax-zmin+1); // normalise by the velocity width
54    }
55  }
56
57  // now work out the greyscale display limits, excluding blank pixels where necessary.
58  float z1,z2,median,madfm;
59  int ct=0;
60  while(image[ct]==cube.pars().getBlankPixVal()) ct++;
61  z1 = z2 = image[ct];
62  for(int i=1;i<size*size;i++){
63    if(// (!cube.pars().getFlagBlankPix())||
64       (image[i]!=blankVal)){
65      if(image[i]<z1) z1=image[i];
66      if(image[i]>z2) z2=image[i];
67    }
68  }
69
70  float tr[6] = {xmin-1,1.,0.,ymin-1,0.,1.};
71
72  cpgswin(xmin-0.5,xmax-0.5,ymin-0.5,ymax-0.5);
73  cpggray(image, size, size, 1, size, 1, size, z1, z2, tr);
74
75  delete [] image;
76  delete [] dim;
77
78  // Draw the borders around the object
79  int ci;
80  cpgqci(&ci);
81  cpgsci(4);
82  cpgsfs(2);
83  if(cube.pars().drawBorders())
84    object.drawBorders(xmin,ymin);
85  else
86    cpgrect(object.getXmin()-xmin+0.5,object.getXmax()-xmin+1.5,
87            object.getYmin()-ymin+0.5,object.getYmax()-ymin+1.5);
88  /*
89    To get the borders localised correctly, we need to subtract (xmin-1) from the
90    X values. We then subtract 0.5 for the left hand border (to place it on the
91    pixel border), and add 0.5 for the right hand border. Similarly for y.
92  */
93  cpgsci(ci);
94
95  if(cube.isWCS()){
96    // Now draw a tick mark to indicate size -- 30 arcmin in length
97    wcsprm *wcs = new wcsprm;
98    wcs = cube.getWCS();
99
100    double *pix   = new double[3];
101    double *world = new double[3];
102    pix[0] = double(xmin) + double(object.getXOffset()) + 2.;
103    pix[1] = double(ymin) + double(object.getYOffset()) + 2.;
104    pix[2] = object.getZcentre();
105    pixToWCSSingle(wcs,pix,world);
106    world[0] -= 0.25;
107    wcsToPixSingle(wcs,world,pix);
108    wcsfree(wcs);
109
110    float tickpt1 = xmin + 2.;
111    float tickpt2 = pix[0] - object.getXOffset();
112    float tickpt3 = ymin + 2.;
113    cpgsci(2);
114    int thick;
115    cpgqlw(&thick);
116    cpgslw(3);
117    cpgerrx(1,&tickpt1,&tickpt2,&tickpt3,2.);
118    //   cpgtext(0.5*(tickpt1+tickpt2)-1,tickpt3+2.,"30\'");
119    cpgslw(thick);
120    cpgsci(1);
121
122    delete [] pix;
123    delete [] world;
124  }
125
126  cpgsci(1);
127  cpgswin(x1,x2,y1,y2);
128
129}
130
131// void Detection::drawBorders(Detection &object, int xoffset, int yoffset)
132void Detection::drawBorders(int xoffset, int yoffset)
133{
134
135  float x1,x2,y1,y2;
136  cpgqwin(&x1,&x2,&y1,&y2);
137  int xsize = int(x2 - x1) + 1;
138  int ysize = int(y2 - y1) + 1;
139
140  bool *isObj = new bool[xsize*ysize];
141  for(int i=0;i<xsize*ysize;i++) isObj[i]=false;
142  for(int i=0;i<this->pix.size();i++)
143    isObj[ (this->pix[i].getY()-yoffset)*xsize + (this->pix[i].getX()-xoffset) ] = true;
144 
145
146  cpgswin(0,xsize-1,0,ysize-1);
147  for(int x=this->xmin; x<=this->xmax; x++){
148    // for each column...
149    for(int y=1;y<ysize;y++){
150      int current = y*xsize + (x-xoffset);
151      int previous = (y-1)*xsize + (x-xoffset);
152      if((isObj[current]&&!isObj[previous])||(!isObj[current]&&isObj[previous])){
153        cpgmove(x-xoffset+0, y+0);
154        cpgdraw(x-xoffset+1, y+0);
155      }
156    }
157  }
158  for(int y=this->ymin; y<=this->ymax; y++){
159    // now for each row...
160    for(int x=1;x<xsize;x++){
161      int current = (y-yoffset)*xsize + x;
162      int previous = (y-yoffset)*xsize + x - 1;
163      if((isObj[current]&&!isObj[previous])||(!isObj[current]&&isObj[previous])){
164        cpgmove(x+0, y-yoffset+0);
165        cpgdraw(x+0, y-yoffset+1);
166      }
167    }
168  }
169  cpgswin(x1,x2,y1,y2);
170 
171  delete [] isObj;
172
173}   
Note: See TracBrowser for help on using the repository browser.