source: trunk/src/Cubes/plots.cc @ 1012

Last change on this file since 1012 was 1012, checked in by MatthewWhiting, 12 years ago

Better way of plotting the detected pixels in the 1D detection spectrum

File size: 26.8 KB
Line 
1// -----------------------------------------------------------------------
2// plots.cc: Functions defining SpectralPlot and ImagePlot classes.
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 <sstream>
30#include <string>
31#include <math.h>
32#include <cpgplot.h>
33#include <duchamp/param.hh>
34#include <duchamp/fitsHeader.hh>
35#include <duchamp/duchamp.hh>
36#include <duchamp/Utils/mycpgplot.hh>
37#include <duchamp/Utils/Statistics.hh>
38#include <duchamp/Cubes/plots.hh>
39
40using std::stringstream;
41using namespace mycpgplot;
42
43namespace duchamp
44{
45
46  namespace Plot
47  {
48
49    //----------------------------------------------------------
50    // SpectralPlot functions
51    //----------------------------------------------------------
52
53    SpectralPlot::SpectralPlot(){
54      this->paperWidth=a4width/inchToCm - 2*psHoffset;
55      this->aspectRatio = M_SQRT2;
56      this->spectraCount=0;
57      this->numOnPage = 5;
58    }
59
60    SpectralPlot::~SpectralPlot(){}
61
62    SpectralPlot::SpectralPlot(const SpectralPlot& p)
63    {
64      operator=(p);
65    }
66
67    SpectralPlot& SpectralPlot::operator=(const SpectralPlot& p)
68    {
69      if(this==&p) return *this;
70      this->numOnPage = p.numOnPage;
71      this->spectraCount = p.spectraCount; 
72      for(int i=0;i<4;i++) this->mainCoords[i] = p.mainCoords[i];
73      for(int i=0;i<4;i++) this->zoomCoords[i] = p.zoomCoords[i];
74      for(int i=0;i<4;i++) this->mapCoords[i] = p.mapCoords[i];
75      this->paperWidth = p.paperWidth;   
76      this->paperHeight = p.paperHeight;   
77      this->aspectRatio = p.aspectRatio;
78      this->identifier = p.identifier;   
79      return *this;
80    }
81
82    //----------------------------------------------------------
83    int SpectralPlot::setUpPlot(std::string pgDestination)
84    {
85      ///  @details
86      /// Opens the designated pgplot device.  Scales the paper so that
87      /// it fits on an A4 sheet (using known values of the default
88      /// pgplot offsets). 
89      ///
90      /// \param pgDestination The std::string indicating the PGPLOT device to
91      /// be written to.
92      ///
93      /// \return The value returned by mycpgopen. If <= 0, then an error
94      /// has occurred.
95
96      this->paperHeight = this->paperWidth*this->aspectRatio;
97      if(this->paperHeight+2*Plot::psVoffset > Plot::a4height){
98        this->paperHeight = Plot::a4height - 2*Plot::psVoffset;
99        this->paperWidth = this->paperHeight / this->aspectRatio;
100      }
101      this->identifier = mycpgopen(pgDestination);
102      if(this->identifier>0) cpgpap(this->paperWidth, this->aspectRatio);
103      // make paper size to fit on A4.
104      return this->identifier;
105    }
106    //----------------------------------------------------------
107    void SpectralPlot::close()
108    {
109      cpgclos();
110    }
111    //----------------------------------------------------------
112    void SpectralPlot::calcCoords()
113    {
114      /// @details
115      /// Calculates the boundaries for the various boxes, in inches measured
116      ///  from the lower left corner.
117      /// Based on the fact that there are numOnPage spectra shown on each
118      ///  page, going down the page in increasing number (given by
119      ///  SpectralPlot::spectraCount).
120
121      int posOnPage = (this->numOnPage -
122                       (this->spectraCount%this->numOnPage))
123        %this->numOnPage;
124      this->mainCoords[0] = Plot::spMainX1/inchToCm;
125      this->mainCoords[1] = Plot::spMainX2/inchToCm;
126      this->zoomCoords[0] = Plot::spZoomX1/inchToCm;
127      this->zoomCoords[1] = Plot::spZoomX2/inchToCm;
128      this->mainCoords[2] = this->zoomCoords[2] = this->mapCoords[2] =
129        posOnPage*paperHeight/float(this->numOnPage) + Plot::spMainY1/inchToCm;
130      this->mainCoords[3] = this->zoomCoords[3] = this->mapCoords[3] =
131        posOnPage*paperHeight/float(this->numOnPage) + Plot::spMainY2/inchToCm;
132      this->mapCoords[0]  = Plot::spMapX1/inchToCm;
133      this->mapCoords[1]  = this->mapCoords[0] + (this->mapCoords[3]-this->mapCoords[2]);
134    }
135    //----------------------------------------------------------
136    void SpectralPlot::gotoHeader(std::string xlabel)
137    {
138      /// @details
139      /// Calls calcCoords, to calculate correct coordinates for this spectrum.
140      /// Defines the region for the header information, making it centred
141      ///  on the page.
142      /// Also writes the velocity (x axis) label, given by the string argument.
143      /// \param xlabel Label to go on the velocity/spectral axis.
144
145      if(spectraCount%numOnPage==0) cpgpage();
146      spectraCount++;
147      this->calcCoords();
148      cpgvsiz(0., this->paperWidth, this->mainCoords[2], this->mainCoords[3]); 
149      cpgsch(Plot::spLabelSize);
150      cpgmtxt("b",Plot::spXlabelOffset,0.5,0.5,xlabel.c_str());
151    }
152    //----------------------------------------------------------
153    void SpectralPlot::gotoMainSpectrum(float x1, float x2, float y1, float y2, std::string ylabel)
154    {
155      /// @details
156      ///  Defines the region for the main spectrum.
157      ///  Draws the box, with tick marks, and
158      ///   writes the flux (y axis) label, given by the string argument.
159      /// \param x1 Minimum X-coordinate of box.
160      /// \param x2 Maximum X-coordinate of box.
161      /// \param y1 Minimum Y-coordinate of box.
162      /// \param y2 Maximum Y-coordinate of box.
163      /// \param ylabel Label for the flux (Y) axis.
164
165      cpgvsiz(this->mainCoords[0],this->mainCoords[1],
166              this->mainCoords[2],this->mainCoords[3]);
167      cpgsch(Plot::spIndexSize);
168      cpgswin(x1,x2,y1,y2);
169      cpgbox("1bcnst",0.,0,"bcnst1v",0.,0);
170      cpgsch(Plot::spLabelSize);
171      cpgmtxt("l",Plot::spYlabelOffset,0.5,0.5,ylabel.c_str());
172    }
173    //----------------------------------------------------------
174    void SpectralPlot::gotoZoomSpectrum(float x1, float x2, float y1, float y2)
175    {
176      /// @details
177      ///   Defines the region for the zoomed-in part of the spectrum.
178      ///   Draws the box, with special tick marks on the bottom axis.
179      /// \param x1 Minimum X-coordinate of box.
180      /// \param x2 Maximum X-coordinate of box.
181      /// \param y1 Minimum Y-coordinate of box.
182      /// \param y2 Maximum Y-coordinate of box.
183
184      cpgvsiz(this->zoomCoords[0],this->zoomCoords[1],
185              this->zoomCoords[2],this->zoomCoords[3]);
186      cpgsch(Plot::spIndexSize);
187      cpgswin(x1,x2,y1,y2);
188      cpgbox("bc",0.,0,"bcstn1v",0.,0);
189      float lengthL,lengthR,disp,tickpt,step;
190      stringstream label;
191      for(int i=1;i<10;i++){
192        tickpt = x1+(x2-x1)*float(i)/10.;  // spectral coord of the tick
193        switch(i)
194          {
195          case 2:
196          case 8:
197            lengthL = lengthR = 0.5;
198            disp = 0.3 + float(i-2)/6.; // i==2 --> disp=0.3, i==8 --> disp=1.3
199            label.str("");
200            label << tickpt;
201            // do a labelled tick mark
202            cpgtick(x1,y1,x2,y1,float(i)/10.,lengthL,lengthR,
203                    disp, 0., label.str().c_str());
204            break;
205          default:
206            label.str("");
207            lengthL = 0.25;
208            lengthR = 0.;
209            disp = 0.;  // not used in this case, but set it anyway.
210            break;
211          }
212        // first the bottom axis, just the ticks
213        if(fabs(tickpt)<(x2-x1)/1.e4) step = 2.*(x2-x1);
214        else step = tickpt;
215        cpgaxis("",
216                tickpt-0.001*(x2-x1), y1,
217                tickpt+0.001*(x2-x1), y1,
218                tickpt-0.001*(x2-x1), tickpt+0.001*(x2-x1),
219                step, -1, lengthL,lengthR, 0.5, disp, 0.);
220        //and now the top -- no labels, just tick marks
221        cpgtick(x1,y2,x2,y2,float(i)/10.,lengthL,lengthR,0.,0.,"");
222      }
223    }
224    //----------------------------------------------------------
225    void SpectralPlot::gotoMap()
226    {
227      cpgvsiz(this->mapCoords[0],this->mapCoords[1],
228              this->mapCoords[2],this->mapCoords[3]);
229      cpgsch(Plot::spIndexSize);
230    }
231    //----------------------------------------------------------
232    void SpectralPlot::drawVelRange(float v1, float v2)
233    {
234      /// @details
235      /// Draws two vertical lines at the limits of velocity
236      ///  given by the arguments.
237      /// \param v1 Minimum velocity.
238      /// \param v2 Maximum velocity.
239
240      int ci,ls;
241      float dud,min,max;
242      cpgqwin(&dud,&dud,&min,&max);
243      cpgqci(&ci);
244      cpgqls(&ls);
245      cpgsci(DUCHAMP_OBJECT_OUTLINE_COLOUR);
246      cpgsls(DASHED);
247      cpgmove(v1,min);  cpgdraw(v1,max);
248      cpgmove(v2,min);  cpgdraw(v2,max);
249      cpgsci(ci);
250      cpgsls(ls);
251    }
252    //----------------------------------------------------------
253    void SpectralPlot::drawMWRange(float v1, float v2)
254    {
255      ///  @details
256      /// Draws a box showing the extent of channels masked by the
257      ///  Milky Way parameters
258      /// \param v1 Minimum velocity of the Milky Way range.
259      /// \param v2 Maximum velocity of the Milky Way range.
260
261      int ci,fs;
262      float dud,min,max,height;
263      cpgqwin(&dud,&dud,&min,&max);
264      height = max-min;
265      max += 0.01*height;
266      min -= 0.01*height;
267      cpgqci(&ci);
268      cpgqfs(&fs);
269      setDarkGreen();
270      cpgsci(DUCHAMP_MILKY_WAY_COLOUR);
271      cpgsfs(HATCHED);
272      cpgrect(v1,v2,min,max);
273      cpgsfs(OUTLINE);
274      cpgrect(v1,v2,min,max);
275      cpgsci(ci);
276      cpgsfs(fs);
277    }
278    //----------------------------------------------------------
279    void SpectralPlot::drawThresholds(Param &par, Statistics::StatsContainer<float> &stats)
280    {
281
282      float dud,vmin,vmax;
283      cpgqwin(&vmin,&vmax,&dud,&dud);
284        cpgsci(RED);
285        cpgsls(DASHED);
286        float thresh = stats.getThreshold();
287        if(par.getFlagNegative()) thresh *= -1.;
288        cpgmove(vmin,thresh);
289        cpgdraw(vmax,thresh);
290        if(par.getFlagGrowth()){
291          if(par.getFlagUserGrowthThreshold()) thresh= par.getGrowthThreshold();
292          else thresh= stats.snrToValue(par.getGrowthCut());
293          if(par.getFlagNegative()) thresh *= -1.;     
294          cpgsls(DOTTED);
295          cpgmove(vmin,thresh);
296          cpgdraw(vmax,thresh);
297        }
298        cpgsci(FOREGND);
299        cpgsls(SOLID);
300
301    }
302    //----------------------------------------------------------
303    // SpectralPlot functions...
304    //----------------------------------------------------------
305    void  SpectralPlot::firstHeaderLine(std::string line)
306    {
307      cpgsch(Plot::spTitleSize);
308      cpgmtxt("t",Plot::spTitleOffset1*Plot::spLabelSize/Plot::spTitleSize,
309              0.5,0.5,line.c_str());
310    }
311    void  SpectralPlot::secondHeaderLine(std::string line)
312    {
313      cpgsch(Plot::spLabelSize);
314      cpgmtxt("t",Plot::spTitleOffset2,0.5,0.5,line.c_str());
315    }
316    void  SpectralPlot::thirdHeaderLine(std::string line)
317    {
318      cpgsch(Plot::spLabelSize);
319      cpgmtxt("t",Plot::spTitleOffset3,0.5,0.5,line.c_str());
320    }
321    void  SpectralPlot::fourthHeaderLine(std::string line)
322    {
323      cpgsch(Plot::spLabelSize);
324      cpgmtxt("t",Plot::spTitleOffset4,0.5,0.5,line.c_str());
325    }
326    void  SpectralPlot::goToPlot(){cpgslct(this->identifier);}
327
328    //----------------------------------------------------------
329    // SimpleSpectralPlot functions
330    //----------------------------------------------------------
331
332    SimpleSpectralPlot::SimpleSpectralPlot(){
333      this->paperWidth=a4width/inchToCm - 2*psHoffset;
334      this->aspectRatio = M_SQRT2/5.;
335    }
336
337    SimpleSpectralPlot::~SimpleSpectralPlot(){}
338
339    SimpleSpectralPlot::SimpleSpectralPlot(const SimpleSpectralPlot& p)
340    {
341      operator=(p);
342    }
343
344    SimpleSpectralPlot& SimpleSpectralPlot::operator=(const SimpleSpectralPlot& p)
345    {
346      if(this==&p) return *this;
347      for(int i=0;i<4;i++) this->mainCoords[i] = p.mainCoords[i];
348      this->paperWidth = p.paperWidth;   
349      this->paperHeight = p.paperHeight;   
350      this->aspectRatio = p.aspectRatio;
351      this->identifier = p.identifier;   
352      return *this;
353    }
354
355    //----------------------------------------------------------
356    int SimpleSpectralPlot::setUpPlot(std::string pgDestination)
357    {
358      ///  @details
359      /// Opens the designated pgplot device.  Scales the paper so that
360      /// it fits on an A4 sheet (using known values of the default
361      /// pgplot offsets). 
362      ///
363      /// \param pgDestination The std::string indicating the PGPLOT device to
364      /// be written to.
365      ///
366      /// \return The value returned by mycpgopen. If <= 0, then an error
367      /// has occurred.
368
369      if(pgDestination == "/xs") this->paperWidth=12.;
370
371      this->paperHeight = this->paperWidth*this->aspectRatio;
372      this->identifier = mycpgopen(pgDestination);
373      if(this->identifier>0) cpgpap(this->paperWidth, this->aspectRatio);
374      // make paper size to fit on A4.
375      float scaling = this->paperWidth*inchToCm / a4width;
376      this->mainCoords[0] = Plot::spMainX1/inchToCm * scaling;
377      this->mainCoords[1] = (Plot::spMapX1+Plot::spMainY2-Plot::spMainY1)/inchToCm * scaling;
378      this->mainCoords[2] = Plot::spMainY1/inchToCm * scaling;
379      this->mainCoords[3] = Plot::spMainY2/inchToCm * scaling;
380      return this->identifier;
381    }
382    //----------------------------------------------------------
383    void SimpleSpectralPlot::close()
384    {
385      cpgclos();
386    }
387    void SimpleSpectralPlot::label(std::string xlabel,std::string ylabel, std::string title)
388    {
389      /// @details
390      /// Calls calcCoords, to calculate correct coordinates for this spectrum.
391      /// Defines the region for the header information, making it centred
392      ///  on the page.
393      /// Also writes the velocity (x axis) label, given by the string argument.
394      /// \param xlabel Label to go on the velocity/spectral axis.
395
396      cpgvsiz(this->mainCoords[0],this->mainCoords[1],this->mainCoords[2],this->mainCoords[3]);
397      cpgsch(2.);
398      cpgmtxt("B",3.,0.5,0.5,xlabel.c_str());
399      cpgmtxt("L",4.,0.5,0.5,ylabel.c_str());
400      cpgmtxt("T",2.,0.5,0.5,title.c_str());
401    }
402    //----------------------------------------------------------
403    void SimpleSpectralPlot::gotoMainSpectrum(float x1, float x2, float y1, float y2)
404    {
405      /// @details
406      ///  Defines the region for the main spectrum.
407      ///  Draws the box, with tick marks, and
408      ///   writes the flux (y axis) label, given by the string argument.
409      /// \param x1 Minimum X-coordinate of box.
410      /// \param x2 Maximum X-coordinate of box.
411      /// \param y1 Minimum Y-coordinate of box.
412      /// \param y2 Maximum Y-coordinate of box.
413      /// \param ylabel Label for the flux (Y) axis.
414
415      cpgvsiz(this->mainCoords[0],this->mainCoords[1],this->mainCoords[2],this->mainCoords[3]);
416      cpgsch(2.);
417      cpgswin(x1,x2,y1,y2);
418      cpgbox("1bcnst",0.,0,"bcnst1v",0.,0);
419    }
420    //----------------------------------------------------------
421    void SimpleSpectralPlot::drawDetectPixel(int z, FitsHeader &head)
422    {
423      float v1,v2;
424      double zero=0.;
425      if(head.isWCS()){
426        double zpt=double(z-0.5);
427        v1=head.pixToVel(zero,zero,zpt);
428        zpt=double(z+0.5);
429        v2=head.pixToVel(zero,zero,zpt);
430      }
431      else{
432        v1=float(z-0.5);
433        v2=float(z+0.5);
434      }
435      float x1,x2,y1,y2;
436      cpgqwin(&x1,&x2,&y1,&y2);
437      float ymax=y2-0.04*(y2-y1);
438      float ymin=y2-0.06*(y2-y1);
439      int lw,fs;
440      cpgqlw(&lw);
441      cpgqfs(&fs);
442      cpgslw(3);
443      cpgsfs(1);
444      cpgrect(v1,v2,ymin,ymax);
445      cpgslw(lw);
446      cpgsfs(fs);
447    }
448    //----------------------------------------------------------
449    void SimpleSpectralPlot::drawVelRange(float v1, float v2)
450    {
451      /// @details
452      /// Draws two vertical lines at the limits of velocity
453      ///  given by the arguments.
454      /// \param v1 Minimum velocity.
455      /// \param v2 Maximum velocity.
456
457      int ci,ls;
458      float dud,min,max;
459      cpgqwin(&dud,&dud,&min,&max);
460      cpgqci(&ci);
461      cpgqls(&ls);
462      cpgsci(DUCHAMP_OBJECT_OUTLINE_COLOUR);
463      cpgsls(DASHED);
464      cpgmove(v1,min);  cpgdraw(v1,max);
465      cpgmove(v2,min);  cpgdraw(v2,max);
466      cpgsci(ci);
467      cpgsls(ls);
468    }
469    //----------------------------------------------------------
470    void SimpleSpectralPlot::drawMWRange(float v1, float v2)
471    {
472      ///  @details
473      /// Draws a box showing the extent of channels masked by the
474      ///  Milky Way parameters
475      /// \param v1 Minimum velocity of the Milky Way range.
476      /// \param v2 Maximum velocity of the Milky Way range.
477
478      int ci,fs;
479      float dud,min,max,height;
480      cpgqwin(&dud,&dud,&min,&max);
481      height = max-min;
482      max += 0.01*height;
483      min -= 0.01*height;
484      cpgqci(&ci);
485      cpgqfs(&fs);
486      setDarkGreen();
487      cpgsci(DUCHAMP_MILKY_WAY_COLOUR);
488      cpgsfs(HATCHED);
489      cpgrect(v1,v2,min,max);
490      cpgsfs(OUTLINE);
491      cpgrect(v1,v2,min,max);
492      cpgsci(ci);
493      cpgsfs(fs);
494    }
495
496
497    //----------------------------------------------------------
498    //----------------------------------------------------------
499    // ImagePlot functions
500    //----------------------------------------------------------
501
502    ImagePlot::ImagePlot(){
503      this->paperWidth = 7.5;
504      this->maxPaperHeight = 10.;
505      this->marginWidth = 0.8;
506      this->wedgeWidth = 0.7;
507    }
508
509    ImagePlot::~ImagePlot(){}
510
511    ImagePlot::ImagePlot(const ImagePlot& p)
512    {
513      operator=(p);
514    }
515
516    ImagePlot& ImagePlot::operator=(const ImagePlot& p)
517    {
518      if(this==&p) return *this;
519      this->paperWidth = p.paperWidth;   
520      this->maxPaperHeight = p.maxPaperHeight;   
521      this->marginWidth = p.marginWidth;   
522      this->wedgeWidth = p.wedgeWidth;
523      this->imageRatio = p.imageRatio;
524      this->aspectRatio = p.aspectRatio;
525      this->xdim = p.xdim;
526      this->ydim = p.ydim;
527      this->identifier = p.identifier;   
528      return *this;
529    }
530
531    //----------------------------------------------------------
532
533    int ImagePlot::setUpPlot(std::string pgDestination, float x, float y)
534    {
535      /// @details
536      ///  Opens a pgplot device and scales it to the correct shape.
537      ///  In doing so, the dimensions for the image are set, and the required
538      ///   aspect ratios of the image and of the plot are calculated.
539      ///  If the resulting image is going to be tall enough to exceed the
540      ///   maximum height (given the default width), then scale everything
541      ///   down by enough to make the height equal to maxPaperHeight.
542      /// \param pgDestination  The string indicating the PGPLOT device to be
543      ///   written to.
544      /// \param x The length of the X-axis.
545      /// \param y The length of the Y-axis.
546      /// \return The value returned by mycpgopen: if <= 0, then an error
547      ///  has occurred.
548
549      this->xdim = x;
550      this->ydim = y;
551      this->imageRatio= this->ydim / this->xdim;
552      this->aspectRatio =  (this->imageRatio*this->imageWidth() + 2*this->marginWidth)
553        / this->paperWidth;
554      float correction;
555      if((this->imageRatio*this->imageWidth() + 2*this->marginWidth) >
556         this->maxPaperHeight){
557        correction = this->maxPaperHeight /
558          (this->imageRatio*this->imageWidth()+2*this->marginWidth);
559        this->paperWidth *= correction;
560        this->marginWidth *= correction;
561        this->wedgeWidth *= correction;
562      }
563      this->identifier = mycpgopen(pgDestination);
564      if(this->identifier>0) cpgpap(this->paperWidth, this->aspectRatio);
565      return this->identifier;
566    }
567    //----------------------------------------------------------
568    void ImagePlot::close()
569    {
570      cpgclos();
571    }
572    //----------------------------------------------------------
573    void ImagePlot::drawMapBox(float x1, float x2, float y1, float y2,
574                               std::string xlabel, std::string ylabel)
575    {
576      /// @details
577      ///  Defines the region that the box containing the map is to go in,
578      ///  and draws the box with limits given by the arguments.
579      ///  Also writes labels on both X- and Y-axes.
580      /// \param x1 Minimum X-axis value.
581      /// \param x2 Maximum X-axis value.
582      /// \param y1 Minimum Y-axis value.
583      /// \param y2 Maximum Y-axis value.
584      /// \param xlabel The label to be put on the X-axis.
585      /// \param ylabel The label to be put on the Y-axis.
586
587      cpgvsiz(this->marginWidth, this->marginWidth + this->imageWidth(),
588              this->marginWidth, this->marginWidth + (this->imageWidth()*this->imageRatio) );
589      cpgslw(2);
590      cpgswin(x1,x2,y1,y2);
591      cpgbox("bcst",0.,0,"bcst",0.,0);
592      cpgslw(1);
593      cpgbox("bcnst",0.,0,"bcnst",0.,0);
594      cpglab(xlabel.c_str(), ylabel.c_str(), "");
595    }
596    //----------------------------------------------------------
597   
598    void ImagePlot::makeTitle(std::string title)
599    {
600      /// @details
601      ///   Writes the title for the plot, making it centred for the entire
602      ///    plot and not just the map.
603      ///  \param title String with title for plot.
604
605      cpgvstd();
606      cpgmtxt("t", Plot::imTitleOffset, 0.5, 0.5, title.c_str());
607    }
608
609    void  ImagePlot::goToPlot(){cpgslct(this->identifier);}
610
611    float ImagePlot::imageWidth(){
612      return this->paperWidth - 2*this->marginWidth - this->wedgeWidth;
613    }
614
615    float ImagePlot::cmToCoord(float cm){
616      /** \param cm Distance to be converted.*/
617      return (cm/Plot::inchToCm) * this->ydim / (this->imageWidth()*this->imageRatio);
618    }
619
620
621    //----------------------------------------------------------
622    //----------------------------------------------------------
623    // CutoutPlot functions
624    //----------------------------------------------------------
625    //----------------------------------------------------------
626
627    CutoutPlot::CutoutPlot(){
628      this->paperWidth=a4width/inchToCm - 2*psHoffset;
629      this->sourceCount=0;
630      this->numOnPage = 7;
631    }
632
633    CutoutPlot::~CutoutPlot(){}
634
635    CutoutPlot::CutoutPlot(const CutoutPlot& p)
636    {
637      operator=(p);
638    }
639
640    CutoutPlot& CutoutPlot::operator=(const CutoutPlot& p)
641    {
642      if(this==&p) return *this;
643      this->numOnPage = p.numOnPage;
644      this->sourceCount = p.sourceCount; 
645      for(int i=0;i<4;i++) this->mainCoords[i] = p.mainCoords[i];
646      for(int i=0;i<4;i++) this->mapCoords[i] = p.mapCoords[i];
647      this->paperWidth = p.paperWidth;   
648      this->paperHeight = p.paperHeight;   
649      this->identifier = p.identifier;   
650      return *this;
651    }
652
653    //----------------------------------------------------------
654    int CutoutPlot::setUpPlot(std::string pgDestination)
655    {
656      ///  @details
657      /// Opens the designated pgplot device.  Scales the paper so that
658      /// it fits on an A4 sheet (using known values of the default
659      /// pgplot offsets). 
660      ///
661      /// \param pgDestination The std::string indicating the PGPLOT device to
662      /// be written to.
663      ///
664      /// \return The value returned by mycpgopen. If <= 0, then an error
665      /// has occurred.
666
667      this->paperHeight = this->paperWidth*M_SQRT2;
668      if(this->paperHeight+2*Plot::psVoffset > Plot::a4height){
669        this->paperHeight = Plot::a4height - 2*Plot::psVoffset;
670        this->paperWidth = this->paperHeight / M_SQRT2;
671      }
672      this->identifier = mycpgopen(pgDestination);
673      if(this->identifier>0) cpgpap(this->paperWidth, this->paperHeight/this->paperWidth);
674      // make paper size to fit on A4.
675      return this->identifier;
676    }
677    //----------------------------------------------------------
678    void CutoutPlot::calcCoords()
679    {
680      /// @details
681      /// Calculates the boundaries for the various boxes, in inches measured
682      ///  from the lower left corner.
683      /// Based on the fact that there are numOnPage spectra shown on each
684      ///  page, going down the page in increasing number (given by
685      ///  CutoutPlot::spectraCount).
686
687      int posOnPage = (this->numOnPage -
688                       (this->sourceCount%this->numOnPage))
689        %this->numOnPage;
690      this->mainCoords[0] = Plot::cuMainX1/inchToCm;
691      this->mainCoords[1] = Plot::cuMainX2/inchToCm;
692      this->mainCoords[2] = this->mapCoords[2] =
693        posOnPage*paperHeight/float(this->numOnPage) + Plot::cuMainY1/inchToCm;
694      this->mainCoords[3] = this->mapCoords[3] =
695        posOnPage*paperHeight/float(this->numOnPage) + Plot::cuMainY2/inchToCm;
696      this->mapCoords[0]  = Plot::cuMapX1/inchToCm;
697      this->mapCoords[1]  = this->mapCoords[0] + (this->mapCoords[3]-this->mapCoords[2]);
698    }
699    //----------------------------------------------------------
700    void CutoutPlot::gotoHeader()
701    {
702      ///  @details
703      /// Calls calcCoords, to calculate correct coordinates for this spectrum.
704      /// Defines the region for the header information, making it centred
705      ///  on the page.
706      /// Also writes the velocity (x axis) label, given by the string argument.
707      /// \param xlabel Label to go on the velocity/spectral axis.
708
709      if(sourceCount%numOnPage==0) cpgpage();
710      sourceCount++;
711      this->calcCoords();
712      //     cpgvsiz(0., this->paperWidth, this->mainCoords[2], this->mainCoords[3]); 
713      cpgvsiz(this->mainCoords[0], this->mainCoords[1],
714              this->mainCoords[2], this->mainCoords[3]); 
715      cpgsch(Plot::spLabelSize);
716      //   cpgmtxt("b",Plot::spXlabelOffset,0.5,0.5,xlabel.c_str());
717      cpgswin(0.,1.,0.,1.);
718      //    cpgbox("bc",0,0,"bc",0,0);
719    }
720    //----------------------------------------------------------
721
722    void CutoutPlot::gotoMap(){
723      cpgvsiz(this->mapCoords[0],this->mapCoords[1],
724              this->mapCoords[2],this->mapCoords[3]);
725      cpgsch(Plot::spIndexSize);
726    }
727    //----------------------------------------------------------
728    // CutoutPlot functions...
729    //----------------------------------------------------------
730    void  CutoutPlot::firstHeaderLine(std::string line)
731    {
732      cpgsch(Plot::spTitleSize);
733      //     cpgmtxt("t",Plot::spTitleOffset1*Plot::spLabelSize/Plot::spTitleSize,
734      //            0.5,0.5,line.c_str());
735      cpgptxt(0.5,0.8,0.,0.5,line.c_str());
736    }
737    void  CutoutPlot::secondHeaderLine(std::string line)
738    {
739      cpgsch(Plot::spLabelSize);
740      //    cpgmtxt("t",Plot::spTitleOffset2,0.5,0.5,line.c_str());
741      cpgptxt(0.5,0.6,0.,0.5,line.c_str());
742    }
743    void  CutoutPlot::thirdHeaderLine(std::string line)
744    {
745      cpgsch(Plot::spLabelSize);
746      //    cpgmtxt("t",Plot::spTitleOffset3,0.5,0.5,line.c_str());
747      cpgptxt(0.5,0.4,0.,0.5,line.c_str());
748    }
749    void  CutoutPlot::fourthHeaderLine(std::string line)
750    {
751      cpgsch(Plot::spLabelSize);
752      //    cpgmtxt("t",Plot::spTitleOffset4,0.5,0.5,line.c_str());
753      cpgptxt(0.5,0.2,0.,0.5,line.c_str());
754    }
755    void  CutoutPlot::goToPlot(){cpgslct(this->identifier);}
756
757  }
758
759}
Note: See TracBrowser for help on using the repository browser.