source: trunk/src/Cubes/plotting.cc @ 668

Last change on this file since 668 was 668, checked in by MatthewWhiting, 14 years ago

Moving the moment map calculation to a separate function, so that it can be called from multiple places.

File size: 16.5 KB
Line 
1// -----------------------------------------------------------------------
2// plotting.cc: Plot the moment map and detection maps, showing the
3//              location of the detected objects.
4// -----------------------------------------------------------------------
5// Copyright (C) 2006, Matthew Whiting, ATNF
6//
7// This program is free software; you can redistribute it and/or modify it
8// under the terms of the GNU General Public License as published by the
9// Free Software Foundation; either version 2 of the License, or (at your
10// option) any later version.
11//
12// Duchamp is distributed in the hope that it will be useful, but WITHOUT
13// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15// for more details.
16//
17// You should have received a copy of the GNU General Public License
18// along with Duchamp; if not, write to the Free Software Foundation,
19// Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
20//
21// Correspondence concerning Duchamp may be directed to:
22//    Internet email: Matthew.Whiting [at] atnf.csiro.au
23//    Postal address: Dr. Matthew Whiting
24//                    Australia Telescope National Facility, CSIRO
25//                    PO Box 76
26//                    Epping NSW 1710
27//                    AUSTRALIA
28// -----------------------------------------------------------------------
29#include <iostream>
30#include <iomanip>
31#include <sstream>
32#include <math.h>
33#include <string.h>
34#include <cpgplot.h>
35#include <wcslib/cpgsbox.h>
36#include <wcslib/pgwcsl.h>
37#include <wcslib/wcs.h>
38#include <duchamp/duchamp.hh>
39#include <duchamp/param.hh>
40#include <duchamp/fitsHeader.hh>
41#include <duchamp/PixelMap/Object3D.hh>
42#include <duchamp/Cubes/cubes.hh>
43#include <duchamp/Cubes/cubeUtils.hh>
44#include <duchamp/Cubes/plots.hh>
45#include <duchamp/Utils/utils.hh>
46#include <duchamp/Utils/mycpgplot.hh>
47
48using namespace mycpgplot;
49using namespace PixelInfo;
50
51namespace duchamp
52{
53
54  void Cube::plotDetectionMap(std::string pgDestination)
55  {
56    ///  @details
57    ///  Creates a map of the spatial locations of the detections, which is
58    ///   written to the PGPlot device given by pgDestination.
59    ///  The map is done in greyscale, where the scale indicates the number of
60    ///   velocity channels that each spatial pixel is detected in.
61    ///  The boundaries of each detection are drawn, and each object is numbered
62    ///   (to match the output list and spectra).
63    ///  The primary grid scale is pixel coordinate, and if the WCS is valid,
64    ///   the correct WCS gridlines are also drawn.
65    /// \param pgDestination The PGPLOT device to be opened, in the typical PGPLOT format.
66
67    // These are the minimum values for the X and Y ranges of the box drawn by
68    //   pgplot (without the half-pixel difference).
69    // The -1 is necessary because the arrays we are dealing with start at 0
70    //  index, while the ranges given in the subsection start at 1...
71    float boxXmin = this->par.getXOffset() - 1;
72    float boxYmin = this->par.getYOffset() - 1;
73
74    long xdim=this->axisDim[0];
75    long ydim=this->axisDim[1];
76    Plot::ImagePlot newplot;
77    int flag = newplot.setUpPlot(pgDestination.c_str(),float(xdim),float(ydim));
78
79    if(flag<=0){
80      duchampError("Plot Detection Map",
81                   "Could not open PGPlot device " + pgDestination + ".\n");
82    }
83    else{
84
85      // get the list of objects that should be plotted. Only applies to outlines and labels.
86      std::vector<bool> objectChoice = this->par.getObjectChoices(this->objectList->size());
87
88      newplot.makeTitle(this->pars().getImageFile());
89
90      newplot.drawMapBox(boxXmin+0.5,boxXmin+xdim+0.5,
91                         boxYmin+0.5,boxYmin+ydim+0.5,
92                         "X pixel","Y pixel");
93
94      //     if(this->objectList.size()>0){
95      // if there are no detections, there will be nothing to plot here
96
97      // Define a float equivalent of this->detectMap that can be plotted by cpggray.
98      // Also find the maximum value, so that we can get the greyscale right and plot a colour wedge.
99      float *detectionMap = new float[xdim*ydim];
100      int maxNum = this->detectMap[0];
101      detectionMap[0] = float(maxNum);
102      for(int pix=1;pix<xdim*ydim;pix++){
103        detectionMap[pix] = float(this->detectMap[pix]); 
104        if(this->detectMap[pix] > maxNum)  maxNum = this->detectMap[pix];
105      }
106
107      if(maxNum>0){ // if there are no detections, it will be 0.
108
109        maxNum = 5 * ((maxNum-1)/5 + 1);  // move to next multiple of 5
110
111        float tr[6] = {boxXmin,1.,0.,boxYmin,0.,1.};
112        cpggray(detectionMap,xdim,ydim,1,xdim,1,ydim,maxNum,0,tr); 
113        cpgbox("bcnst",0.,0,"bcnst",0.,0);
114        cpgsch(1.5);
115        cpgwedg("rg",3.2,2,maxNum,0,"Number of detected channels");
116      }
117
118      delete [] detectionMap;
119     
120      drawBlankEdges(this->array,this->axisDim[0],this->axisDim[1],this->par);
121     
122      if(this->head.isWCS()) this->plotWCSaxes();
123 
124      if(this->objectList->size()>0){
125        // now show and label each detection, drawing over the WCS lines.
126
127        cpgsch(1.0);
128        cpgslw(2);   
129        float xoff=0.;
130        float yoff=newplot.cmToCoord(0.5);
131        if(this->par.drawBorders()){
132          cpgsci(DUCHAMP_OBJECT_OUTLINE_COLOUR);
133          for(size_t i=0;i<this->objectList->size();i++)
134            if(objectChoice[i]) this->objectList->at(i).drawBorders(0,0);
135        }
136        cpgsci(DUCHAMP_ID_TEXT_COLOUR);
137        std::stringstream label;
138        cpgslw(1);
139        for(size_t i=0;i<this->objectList->size();i++){
140          if(objectChoice[i]) {
141            cpgpt1(this->par.getXOffset()+this->objectList->at(i).getXPeak(),
142                   this->par.getYOffset()+this->objectList->at(i).getYPeak(),
143                   CROSS);
144            label.str("");
145            label << this->objectList->at(i).getID();
146            cpgptxt(this->par.getXOffset()+this->objectList->at(i).getXPeak()-xoff,
147                    this->par.getYOffset()+this->objectList->at(i).getYPeak()-yoff,
148                    0, 0.5, label.str().c_str());
149          }
150        }
151
152      }
153
154      cpgclos();
155    }
156  }
157
158  /*********************************************************/
159
160  void Cube::plotMomentMap(std::string pgDestination)
161  {
162    ///  @details
163    ///  Uses the other function
164    ///  Cube::plotMomentMap(std::vector<std::string>) to plot the moment
165    ///  map.
166    /// \param pgDestination The PGPLOT device that the map is to be written to.
167
168    std::vector<std::string> devicelist;
169    devicelist.push_back(pgDestination);
170    this->plotMomentMap(devicelist);
171  }
172
173  /*********************************************************/
174
175  void Cube::plotMomentMap(std::vector<std::string> pgDestination)
176  {
177    ///  @details
178    ///  Creates a 0th moment map of the detections, which is written to each
179    ///   of the PGPlot devices mentioned in pgDestination.
180    ///  The advantage of this function is that the map is only calculated once,
181    ///   even if multiple maps are required.
182    ///  The map is done in greyscale, where the scale indicates the integrated
183    ///   flux at each spatial pixel.
184    ///  The boundaries of each detection are drawn, and each object is numbered
185    ///   (to match the output list and spectra).
186    ///  The primary grid scale is pixel coordinate, and if the WCS is valid,
187    ///   the correct WCS gridlines are also drawn.
188    /// \param pgDestination A set of PGPLOT devices that are to be
189    /// opened, each in the typical PGPLOT format.
190
191    float boxXmin = this->par.getXOffset() - 1;
192    float boxYmin = this->par.getYOffset() - 1;
193
194    long xdim=this->axisDim[0];
195    long ydim=this->axisDim[1];
196
197    int numPlots = pgDestination.size();
198    std::vector<Plot::ImagePlot> plotList(numPlots);
199    std::vector<int> plotFlag(numPlots,0);
200    std::vector<bool> doPlot(numPlots,false);
201    bool plotNeeded = false;
202
203    for(int i=0;i<numPlots;i++){
204   
205      plotFlag[i] = plotList[i].setUpPlot(pgDestination[i],
206                                          float(xdim),float(ydim));
207       
208      if(plotFlag[i]<=0) duchampError("Plot Moment Map",
209                                      "Could not open PGPlot device "
210                                      + pgDestination[i] + ".\n");
211      else{
212        doPlot[i] = true;
213        plotNeeded = true;
214      }
215
216    }
217
218    if(plotNeeded){
219
220      if(this->objectList->size()==0){
221        // if there are no detections, we plot an empty field.
222
223        for(int iplot=0; iplot<numPlots; iplot++){
224          plotList[iplot].goToPlot();
225          plotList[iplot].makeTitle(this->pars().getImageFile());
226       
227          plotList[iplot].drawMapBox(boxXmin+0.5,boxXmin+xdim+0.5,
228                                     boxYmin+0.5,boxYmin+ydim+0.5,
229                                     "X pixel","Y pixel");
230       
231          drawBlankEdges(this->array,this->axisDim[0],this->axisDim[1],this->par);
232       
233          if(this->head.isWCS()) this->plotWCSaxes();
234        }
235
236      }
237      else { 
238        // if there are some detections, do the calculations first before
239        //  plotting anything.
240 
241        // get the list of objects that should be plotted. Only applies to outlines and labels.
242        std::vector<bool> objectChoice = this->par.getObjectChoices(this->objectList->size());
243
244        for(int iplot=0; iplot<numPlots; iplot++){
245          // Although plot the axes so that the user knows something is
246          //  being done (at least, they will if there is an /xs plot)
247          plotList[iplot].goToPlot();
248          plotList[iplot].makeTitle(this->pars().getImageFile());
249   
250          plotList[iplot].drawMapBox(boxXmin+0.5,boxXmin+xdim+0.5,
251                                     boxYmin+0.5,boxYmin+ydim+0.5,
252                                     "X pixel","Y pixel");
253       
254          if(pgDestination[iplot]=="/xs")
255            cpgptxt(boxXmin+0.5+xdim/2., boxYmin+0.5+ydim/2., 0, 0.5,
256                    "Calculating map...");
257        }
258
259//      bool *isObj = new bool[xdim*ydim*zdim];
260//      for(int i=0;i<xdim*ydim*zdim;i++) isObj[i] = false;
261//      for(size_t i=0;i<this->objectList->size();i++){
262//        if(objectChoice[i]){
263//          std::vector<Voxel> voxlist = this->objectList->at(i).getPixelSet();
264//          std::vector<Voxel>::iterator vox;
265//          for(vox=voxlist.begin();vox<voxlist.end();vox++){
266//            int pixelpos = vox->getX() + xdim*vox->getY() + xdim*ydim*vox->getZ();
267//            isObj[pixelpos] = true;
268//          }
269//        }
270//      }
271
272//      float *momentMap = new float[xdim*ydim];
273//      // Initialise to zero
274//      for(int i=0;i<xdim*ydim;i++) momentMap[i] = 0.;
275
276//      // if we are looking for negative features, we need to invert the
277//      //  detected pixels for the moment map
278//      float sign = 1.;
279//      if(this->pars().getFlagNegative()) sign = -1.;
280
281//      float deltaVel;
282//      double x,y;
283
284//      double *zArray  = new double[zdim];
285//      for(int z=0; z<zdim; z++) zArray[z] = double(z);
286   
287//      for(int pix=0; pix<xdim*ydim; pix++){
288
289//        x = double(pix%xdim);
290//        y = double(pix/xdim);
291
292//        if(!this->isBlank(pix)){ // only do this for non-blank pixels. Judge this by the first pixel of the channel.
293
294//         double * world = this->head.pixToVel(x,y,zArray,zdim);
295     
296//          for(int z=0; z<zdim; z++){     
297//            int pos =  z*xdim*ydim + pix;  // the voxel in the cube
298//            if(isObj[pos]){ // if it's an object pixel...
299//              // delta-vel is half the distance between adjacent channels.
300//              // if at end, then just use 0-1 or (zdim-1)-(zdim-2) distance
301//              if(z==0){
302//                if(zdim==1) deltaVel=1.; // pathological case -- if 2D image.
303//                else deltaVel = world[z+1] - world[z];
304//              }
305//              else if(z==(zdim-1)) deltaVel = world[z-1] - world[z];
306//              else deltaVel = (world[z+1] - world[z-1]) / 2.;
307
308//              momentMap[pix] += sign * this->array[pos] * fabs(deltaVel);
309
310//            }
311//          }
312//          delete [] world;
313//        }
314
315//      }
316   
317//      delete [] zArray;
318
319//      int count=0;
320//      float z1=0.,z2=0.;
321//      for(int i=0;i<xdim*ydim;i++) {
322//        if(momentMap[i]>0.){
323//          float logmm = log10(momentMap[i]);
324//          bool addPixel = false;
325//          for(int z=0;z<zdim;z++) addPixel = addPixel || isObj[z*xdim*ydim+i];
326//          if(addPixel){
327//            if(count==0) z1 = z2 = logmm;
328//            else{
329//              if(logmm < z1) z1 = logmm;
330//              if(logmm > z2) z2 = logmm;
331//            }
332//            count++;
333//          }
334//        }
335//      }
336
337//      for(int i=0;i<xdim*ydim;i++) {
338//        bool addPixel = false;
339//        for(int z=0;z<zdim;z++) addPixel = addPixel || isObj[z*xdim*ydim+i];
340//        addPixel = addPixel && (momentMap[i]>0.);
341//        if(!addPixel) momentMap[i] = z1-1.;
342//        else momentMap[i] = log10(momentMap[i]);
343//      }
344
345//      delete [] isObj;
346
347
348        float *momentMap = new float[xdim*ydim];
349        float z1=0.,z2=0.;
350        this->getMomentMapForPlot(momentMap,z1,z2);
351
352
353        // Have now done all necessary calculations for moment map.
354        // Now produce the plot
355
356        for(int iplot=0; iplot<numPlots; iplot++){
357          plotList[iplot].goToPlot();
358     
359          float tr[6] = {boxXmin,1.,0.,boxYmin,0.,1.};
360          cpggray(momentMap,xdim,ydim,1,xdim,1,ydim,z2,z1,tr);
361          cpgbox("bcnst",0.,0,"bcnst",0.,0);
362          cpgsch(1.5);
363          std::string wedgeLabel = "Integrated Flux ";
364          if(this->par.getFlagNegative())
365            wedgeLabel = "-1. " + times + " " + wedgeLabel;
366          if(this->head.isWCS())
367            wedgeLabel += "[" + this->head.getIntFluxUnits() + "]";
368          else wedgeLabel += "[" + this->head.getFluxUnits() + "]";
369          cpgwedglog("rg",3.2,2,z2,z1,wedgeLabel.c_str());
370
371
372          drawBlankEdges(this->array,this->axisDim[0],this->axisDim[1],this->par);
373
374          if(this->head.isWCS()) this->plotWCSaxes();
375 
376          // now show and label each detection, drawing over the WCS lines.
377          cpgsch(1.0);
378          cpgslw(2);
379          float xoff=0.;
380          float yoff=plotList[iplot].cmToCoord(0.5);
381          if(this->par.drawBorders()){
382            cpgsci(DUCHAMP_OBJECT_OUTLINE_COLOUR);
383            for(size_t i=0;i<this->objectList->size();i++)
384              if(objectChoice[i]) this->objectList->at(i).drawBorders(0,0);
385          }
386          cpgsci(DUCHAMP_ID_TEXT_COLOUR);
387          std::stringstream label;
388          cpgslw(1);
389          for(size_t i=0;i<this->objectList->size();i++){
390            if(objectChoice[i]) {
391              cpgpt1(this->par.getXOffset()+this->objectList->at(i).getXPeak(),
392                     this->par.getYOffset()+this->objectList->at(i).getYPeak(),
393                     CROSS);
394              label.str("");
395              label << this->objectList->at(i).getID();
396              cpgptxt(this->par.getXOffset()+this->objectList->at(i).getXPeak()-xoff,
397                      this->par.getYOffset()+this->objectList->at(i).getYPeak()-yoff,
398                      0, 0.5, label.str().c_str());
399            }
400          }
401
402        } // end of iplot loop over number of devices
403
404        delete [] momentMap;
405
406      } // end of else (from if(numdetections==0) )
407
408 
409      for(int iplot=0; iplot<numPlots; iplot++){
410        plotList[iplot].goToPlot();
411        cpgclos();
412      }
413   
414    }
415 
416  }
417
418  /*********************************************************/
419
420//   void Cube::plotWCSaxes(int textColour, int axisColour)
421//   {
422//     duchamp::plotWCSaxes(this->head.getWCS(), this->axisDim, textColour, axisColour);
423//   }
424 
425 
426  void plotWCSaxes(struct wcsprm *wcs, long *axes, int textColour, int axisColour)
427  {
428
429    /// @details
430    ///  A front-end to the cpgsbox command, to draw the gridlines for the WCS
431    ///    over the current plot.
432    ///  Lines are drawn in dark green over the full plot area, and the axis
433    ///    labels are written on the top and on the right hand sides, so as not
434    ///    to conflict with other labels.
435    ///  \param textColour The colour index to use for the text labels --
436    ///  defaults to duchamp::DUCHAMP_ID_TEXT_COLOUR
437    ///  \param axisColour The colour index to use for the axes --
438    ///  defaults to duchamp::DUCHAMP_WCS_AXIS_COLOUR
439
440    float boxXmin=0,boxYmin=0;
441
442    char idents[3][80], opt[2], nlcprm[1];
443
444    strcpy(idents[0], wcs->lngtyp);
445    strcpy(idents[1], wcs->lattyp);
446    strcpy(idents[2], "");
447    if(strcmp(wcs->lngtyp,"RA")==0) opt[0] = 'G';
448    else opt[0] = 'D';
449    opt[1] = 'E';
450
451    float  blc[2], trc[2];
452    //   float  scl; // --> unused here.
453    blc[0] = boxXmin + 0.5;
454    blc[1] = boxYmin + 0.5;
455    trc[0] = boxXmin + axes[0]+0.5;
456    trc[1] = boxYmin + axes[1]+0.5;
457 
458    int existingLineWidth;
459    cpgqlw(&existingLineWidth);
460    int existingColour;
461    cpgqci(&existingColour);
462    float existingSize;
463    cpgqch(&existingSize);
464    cpgsci(textColour);
465    cpgsch(0.8);
466    int    c0[7], ci[7], gcode[2], ic, ierr;
467    for(int i=0;i<7;i++) c0[i] = -1;
468    /* define the WCS axes colour */
469    setWCSGreen();
470
471    gcode[0] = 2;  // type of grid to draw: 0=none, 1=ticks only, 2=full grid
472    gcode[1] = 2;
473
474    double cache[257][4], grid1[9], grid2[9], nldprm[8];
475    grid1[0] = 0.0; 
476    grid2[0] = 0.0;
477
478    // Draw the celestial grid with no intermediate tick marks.
479    // Set LABCTL=2100 to write 1st coord on top, and 2nd on right
480
481    //Colour indices used by cpgsbox: make it all the same colour for thin
482    // line case.
483    ci[0] = axisColour; // grid lines, coord 1
484    ci[1] = axisColour; // grid lines, coord 2
485    ci[2] = axisColour; // numeric labels, coord 1
486    ci[3] = axisColour; // numeric labels, coord 2
487    ci[4] = axisColour; // axis annotation, coord 1
488    ci[5] = axisColour; // axis annotation, coord 2
489    ci[6] = axisColour; // title
490
491    cpgsbox(blc, trc, idents, opt, 2100, 0, ci, gcode, 0.0, 0, grid1, 0, grid2,
492            0, pgwcsl_, 1, WCSLEN, 1, nlcprm, (int *)wcs,
493            nldprm, 256, &ic, cache, &ierr);
494
495    cpgsci(existingColour);
496    cpgsch(existingSize);
497    cpgslw(existingLineWidth);
498  }
499
500
501
502}
503
Note: See TracBrowser for help on using the repository browser.