1 | #ifndef PLOTS_H |
---|
2 | #define PLOTS_H |
---|
3 | |
---|
4 | #include <iostream> |
---|
5 | #include <sstream> |
---|
6 | #include <string> |
---|
7 | #include <stdlib.h> |
---|
8 | #include <math.h> |
---|
9 | #include <cpgplot.h> |
---|
10 | |
---|
11 | using std::string; |
---|
12 | using std::stringstream; |
---|
13 | |
---|
14 | namespace Plot |
---|
15 | { |
---|
16 | const float inchToCm=2.54; // Conversion factor from inches to centimetres. |
---|
17 | const float a4width=21.0; // A4 width in cm |
---|
18 | const float a4height=29.7; // A4 height in cm |
---|
19 | const float psHoffset=0.35; // The default offset applied to ps files by pgplot |
---|
20 | const float psVoffset=0.25; // The default offset applied to ps files by pgplot |
---|
21 | |
---|
22 | |
---|
23 | //*************************************************************************** |
---|
24 | |
---|
25 | class SpectralPlot |
---|
26 | { |
---|
27 | /** |
---|
28 | * SpectralPlot class |
---|
29 | * A class to hold the dimensions and set up for the plotting of the spectra |
---|
30 | * (including the full spectra, the zoomed in part, and the moment map). |
---|
31 | * The physical dimensions (in inches) of the plot and the elements within |
---|
32 | * it are stored, based on the assumption that the plot will go on an A4 page. |
---|
33 | * Simple accessor functions are provided to enable access to quantities needed |
---|
34 | * for pgplot routines. |
---|
35 | */ |
---|
36 | public: |
---|
37 | SpectralPlot(){ |
---|
38 | paperWidth=a4width/inchToCm - 2*psHoffset; paperHeight = paperWidth*M_SQRT2; |
---|
39 | if(paperHeight+2*psVoffset > a4height){ |
---|
40 | paperHeight = a4height - 2*psVoffset; |
---|
41 | paperWidth = paperHeight / M_SQRT2; |
---|
42 | } |
---|
43 | spectraCount=0; |
---|
44 | numOnPage = 5; |
---|
45 | indexSize = 0.6; |
---|
46 | labelSize = 0.7;}; |
---|
47 | ~SpectralPlot(){}; |
---|
48 | |
---|
49 | int setUpPlot(string pgDestination){ |
---|
50 | /** SpectralPlot::setUpPlot |
---|
51 | * Opens the designated pgplot device. |
---|
52 | * Scales the paper so that it fits on an A4 sheet (using known values of |
---|
53 | * the default pgplot offsets). |
---|
54 | * Returns the value returned by cpgopen -- if <= 0, then an error has occurred. |
---|
55 | */ |
---|
56 | int flag = cpgopen(pgDestination.c_str()); |
---|
57 | if(flag>0) cpgpap(paperWidth, paperHeight/paperWidth); // make paper size to fit on A4. |
---|
58 | return flag; |
---|
59 | } |
---|
60 | |
---|
61 | void calcCoords(){ |
---|
62 | /** SpectralPlot::calcCoords() |
---|
63 | * Calculates the boundaries for the various boxes, in inches measured |
---|
64 | * from the lower left corner. |
---|
65 | * Based on the fact that there are numOnPage spectra shown on each page, |
---|
66 | * going down the page in increasing number -- given by spectraCount. |
---|
67 | */ |
---|
68 | int posOnPage = (numOnPage - (spectraCount%numOnPage))%numOnPage; |
---|
69 | mainCoords[0] = 2.0/inchToCm; |
---|
70 | mainCoords[1] = 13.7/inchToCm; |
---|
71 | zoomCoords[0] = 15.0/inchToCm; |
---|
72 | zoomCoords[1] = 16.8/inchToCm; |
---|
73 | mainCoords[2] = zoomCoords[2] = mapCoords[2] = |
---|
74 | posOnPage*paperHeight/float(numOnPage) + 1.8/inchToCm; |
---|
75 | mainCoords[3] = zoomCoords[3] = mapCoords[3] = |
---|
76 | posOnPage*paperHeight/float(numOnPage) + 3.8/inchToCm; |
---|
77 | mapCoords[0] = 17.0/inchToCm; |
---|
78 | mapCoords[1] = mapCoords[0] + (mapCoords[3]-mapCoords[2]); |
---|
79 | } |
---|
80 | |
---|
81 | void gotoHeader(string xlabel){ |
---|
82 | /** SpectralPlot::gotoHeader(string) |
---|
83 | * Calls calcCoords, to calculate correct coordinates for this spectrum. |
---|
84 | * Defines the region for the header information, making it centred |
---|
85 | * on the page. |
---|
86 | * Also writes the velocity (x axis) label, given by the string argument. |
---|
87 | */ |
---|
88 | if(spectraCount%numOnPage==0) cpgpage(); |
---|
89 | spectraCount++; |
---|
90 | calcCoords(); |
---|
91 | cpgvsiz(0., paperWidth, mainCoords[2], mainCoords[3]); |
---|
92 | cpgsch(labelSize); |
---|
93 | cpgmtxt("b",3.,0.5,0.5,xlabel.c_str()); |
---|
94 | } |
---|
95 | |
---|
96 | /** |
---|
97 | * Header line functions |
---|
98 | * Functions to write the header information above the boxes. |
---|
99 | * One for each line (position/velocity, widths & fluxes, pixel coords). |
---|
100 | */ |
---|
101 | void firstHeaderLine(string line){ cpgmtxt("t",3.8,0.5,0.5,line.c_str());}; |
---|
102 | void secondHeaderLine(string line){ cpgmtxt("t",2.3,0.5,0.5,line.c_str());}; |
---|
103 | void thirdHeaderLine(string line){ cpgmtxt("t",0.8,0.5,0.5,line.c_str());}; |
---|
104 | |
---|
105 | void gotoMainSpectrum(float x1, float x2, float y1, float y2, string ylabel){ |
---|
106 | /** SpectralPlot::gotoMainSpectrum() |
---|
107 | * Defines the region for the main spectrum. |
---|
108 | * Draws the box, with tick marks, and |
---|
109 | * writes the flux (y axis) label, given by the string argument. |
---|
110 | */ |
---|
111 | cpgvsiz(mainCoords[0],mainCoords[1],mainCoords[2],mainCoords[3]); |
---|
112 | cpgsch(indexSize); |
---|
113 | cpgswin(x1,x2,y1,y2); |
---|
114 | cpgbox("1bcnst",0.,0,"bcnst1v",0.,0); |
---|
115 | cpgsch(labelSize); |
---|
116 | cpgmtxt("l",4.,0.5,0.5,ylabel.c_str()); |
---|
117 | } |
---|
118 | |
---|
119 | void gotoZoomSpectrum(float x1, float x2, float y1, float y2){ |
---|
120 | /** SpectralPlot::gotoZoomSpectrum() |
---|
121 | * Defines the region for the zoomed-in part of the spectrum. |
---|
122 | * Draws the box, with special tick marks on the bottom axis. |
---|
123 | */ |
---|
124 | cpgvsiz(zoomCoords[0],zoomCoords[1],zoomCoords[2],zoomCoords[3]); |
---|
125 | cpgsch(indexSize); |
---|
126 | cpgswin(x1,x2,y1,y2); |
---|
127 | cpgbox("bc",0.,0,"bcstn1v",0.,0); |
---|
128 | float lengthL,lengthR,disp,tickpt; |
---|
129 | stringstream label; |
---|
130 | for(int i=1;i<10;i++){ |
---|
131 | tickpt = x1+(x2-x1)*float(i)/10.; // spectral coord of the tick |
---|
132 | switch(i) |
---|
133 | { |
---|
134 | case 2: |
---|
135 | case 8: |
---|
136 | lengthL = lengthR = 0.5; |
---|
137 | disp = 0.3 + float(i-2)/6.; // i==2 --> disp=0.3, i==8 --> disp=1.3 |
---|
138 | label.str(""); |
---|
139 | label << tickpt; |
---|
140 | // do a labelled tick mark |
---|
141 | cpgtick(x1,y1,x2,y1,float(i)/10.,lengthL,lengthR,disp,0.,label.str().c_str()); |
---|
142 | break; |
---|
143 | default: |
---|
144 | label.str(""); |
---|
145 | lengthL = 0.25; |
---|
146 | lengthR = 0.; |
---|
147 | disp = 0.; // not used in this case, but set it anyway. |
---|
148 | break; |
---|
149 | } |
---|
150 | // first the bottom axis, just the tick |
---|
151 | cpgaxis("", |
---|
152 | tickpt-0.001*(x2-x1), y1, |
---|
153 | tickpt+0.001*(x2-x1), y1, |
---|
154 | tickpt-0.001*(x2-x1), tickpt+0.001*(x2-x1), |
---|
155 | tickpt, -1, lengthL,lengthR, 0.5, disp, 0.); |
---|
156 | // and now the top -- no labels, just tick marks |
---|
157 | cpgtick(x1,y2,x2,y2,float(i)/10.,lengthL,lengthR,0.,0.,""); |
---|
158 | } |
---|
159 | } |
---|
160 | |
---|
161 | void gotoMap(){ |
---|
162 | /** SpectralPlot::gotoMap() |
---|
163 | * Defines the region for the moment map. |
---|
164 | */ |
---|
165 | cpgvsiz(mapCoords[0],mapCoords[1],mapCoords[2],mapCoords[3]); |
---|
166 | cpgsch(indexSize); |
---|
167 | } |
---|
168 | |
---|
169 | void drawVelRange(float v1, float v2){ |
---|
170 | /** SpectralPlot::drawVelRange(float v1, float v2) |
---|
171 | * Draws two vertical lines at the limits of velocity given by the arguments. |
---|
172 | */ |
---|
173 | int ci,ls; |
---|
174 | float dud,min,max; |
---|
175 | cpgqwin(&dud,&dud,&min,&max); |
---|
176 | cpgqci(&ci); |
---|
177 | cpgqls(&ls); |
---|
178 | cpgsci(4); |
---|
179 | cpgsls(2); |
---|
180 | cpgmove(v1,min); cpgdraw(v1,max); |
---|
181 | cpgmove(v2,min); cpgdraw(v2,max); |
---|
182 | cpgsci(ci); |
---|
183 | cpgsls(ls); |
---|
184 | } |
---|
185 | |
---|
186 | int getNumOnPage(){return numOnPage;}; |
---|
187 | void setNumOnPage(int i){numOnPage=i;}; |
---|
188 | float getPaperWidth(){return paperWidth;}; |
---|
189 | void setPaperWidth(float f){paperWidth=f;}; |
---|
190 | float getPaperHeight(){return paperHeight;}; |
---|
191 | void setPaperHeight(float f){paperHeight=f;}; |
---|
192 | |
---|
193 | private: |
---|
194 | int numOnPage; // Number of spectra to put on one page. |
---|
195 | int spectraCount; // Number of spectra done so far -- where on the page? |
---|
196 | float mainCoords[4]; // Boundaries for the main spectrum [inches] |
---|
197 | float zoomCoords[4]; // Boundaries for the zoomed-in spectrum [inches] |
---|
198 | float mapCoords[4]; // Boundaries for the map box [inches] |
---|
199 | float paperWidth; // Width of the plottable region of the paper [inches] |
---|
200 | float paperHeight; // Height of the plottable region of the paper [inches] |
---|
201 | float indexSize; // PGPlot character height for tick mark labels |
---|
202 | float labelSize; // PGPlot character height for axis labels. |
---|
203 | |
---|
204 | }; |
---|
205 | |
---|
206 | //*************************************************************************** |
---|
207 | |
---|
208 | class ImagePlot |
---|
209 | { |
---|
210 | /** |
---|
211 | * ImagePlot class |
---|
212 | * A class to hold the dimensions and set up for the plots used by the two |
---|
213 | * functions below. |
---|
214 | * The physical dimensions (in inches) of the plot and the elements within |
---|
215 | * it are stored, including maximum widths and heights (so that the plot will |
---|
216 | * fit on an A4 page). |
---|
217 | * Simple accessor functions are provided to enable access to quantities needed |
---|
218 | * for pgplot routines. |
---|
219 | */ |
---|
220 | public: |
---|
221 | ImagePlot(){ |
---|
222 | paperWidth = 7.5; maxPaperHeight = 10.; marginWidth = 0.8; wedgeWidth = 0.7; |
---|
223 | }; |
---|
224 | ~ImagePlot(){}; |
---|
225 | |
---|
226 | int setUpPlot(string pgDestination, float x, float y){ |
---|
227 | /** |
---|
228 | * setUpPlot(string pgDestination, float x, float y) |
---|
229 | * Opens a pgplot device and scales it to the correct shape. |
---|
230 | * In doing so, the dimensions for the image are set, and the required aspect ratios |
---|
231 | * of the image and of the plot are calculated. |
---|
232 | * If the resulting image is going to be tall enough to exceed the maximum height |
---|
233 | * (given the default width), then scale everything down by enough to make the |
---|
234 | * height equal to maxPaperHeight. |
---|
235 | * Returns the value returned by cpgopen -- if <= 0, then an error has occurred. |
---|
236 | */ |
---|
237 | xdim = x; |
---|
238 | ydim = y; |
---|
239 | imageRatio= ydim / xdim; |
---|
240 | aspectRatio = (imageRatio*imageWidth() + 2*marginWidth) / paperWidth; |
---|
241 | if((imageRatio*imageWidth() + 2*marginWidth) > maxPaperHeight){ |
---|
242 | float correction = maxPaperHeight / (imageRatio*imageWidth() + 2*marginWidth); |
---|
243 | paperWidth *= correction; |
---|
244 | marginWidth *= correction; |
---|
245 | wedgeWidth *= correction; |
---|
246 | } |
---|
247 | int flag = cpgopen(pgDestination.c_str()); |
---|
248 | if(flag>0) cpgpap(paperWidth, aspectRatio); |
---|
249 | return flag; |
---|
250 | } |
---|
251 | |
---|
252 | void drawMapBox(float x1, float x2, float y1, float y2, string xlabel, string ylabel){ |
---|
253 | /** |
---|
254 | * drawMapBox() |
---|
255 | * Defines the region that the box containing the map is to go in, |
---|
256 | * and draws the box with limits given by the arguments. |
---|
257 | * The labels for the x and y axes are also given as arguments. |
---|
258 | */ |
---|
259 | cpgvsiz(marginWidth, marginWidth + imageWidth(), |
---|
260 | marginWidth, marginWidth + (imageWidth()*imageRatio)); |
---|
261 | cpgslw(2); |
---|
262 | cpgswin(x1,x2,y1,y2); |
---|
263 | cpgbox("bcst",0.,0,"bcst",0.,0); |
---|
264 | cpgslw(1); |
---|
265 | cpgbox("bcnst",0.,0,"bcnst",0.,0); |
---|
266 | cpglab(xlabel.c_str(), ylabel.c_str(), ""); |
---|
267 | } |
---|
268 | void makeTitle(string title){ |
---|
269 | /** |
---|
270 | * makeTitle(string) |
---|
271 | * Writes the title for the plot, making it centred for the entire plot |
---|
272 | * and not just the map. |
---|
273 | */ |
---|
274 | cpgvstd(); |
---|
275 | cpgmtxt("t", 2.7, 0.5, 0.5, title.c_str()); |
---|
276 | } |
---|
277 | |
---|
278 | float imageWidth(){ |
---|
279 | /** |
---|
280 | * imageWidth() |
---|
281 | * Returns the calculated total width of the image part of the plot [inches] |
---|
282 | */ |
---|
283 | return paperWidth - 2*marginWidth - wedgeWidth; |
---|
284 | }; |
---|
285 | |
---|
286 | float cmToCoord(float cm){return (cm/inchToCm) * ydim / (imageWidth()*imageRatio);}; |
---|
287 | float getMargin() {return marginWidth;}; |
---|
288 | float getPaperWidth() {return paperWidth;}; |
---|
289 | float getImageHeight(){return imageWidth()*imageRatio;}; |
---|
290 | float getAspectRatio(){return aspectRatio;}; |
---|
291 | |
---|
292 | |
---|
293 | private: |
---|
294 | float paperWidth; // Default (maximum) width of "paper" [inches] |
---|
295 | float maxPaperHeight; // Maximum allowed height of paper [inches] |
---|
296 | float marginWidth; // Width allowed for margins around main plot (ie. label & numbers) [inches] |
---|
297 | float wedgeWidth; // Width allowed for placement of wedge on right hand side of plot. [inches] |
---|
298 | float imageRatio; // Aspect ratio of the image only (ie. y-value range / x-value range). |
---|
299 | float aspectRatio; // Aspect ratio of whole plot. |
---|
300 | float xdim; // Width of main plot, in display units. |
---|
301 | float ydim; // Height of main plot, in display units. |
---|
302 | }; |
---|
303 | |
---|
304 | } |
---|
305 | |
---|
306 | #endif |
---|
307 | |
---|