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 | void drawMWRange(float v1, float v2){ |
---|
187 | /** SpectralPlot::drawMWRange(float v1, float v2) |
---|
188 | * Draws a box showing the extent of channels masked by the Milky Way parameters |
---|
189 | */ |
---|
190 | int ci,fs; |
---|
191 | float dud,min,max,height; |
---|
192 | cpgqwin(&dud,&dud,&min,&max); |
---|
193 | height = max-min; |
---|
194 | max += 0.01*height; |
---|
195 | min -= 0.01*height; |
---|
196 | cpgqci(&ci); |
---|
197 | cpgqfs(&fs); |
---|
198 | cpgscr(16,0.,0.7,0.); |
---|
199 | cpgsci(16); |
---|
200 | cpgsfs(3); |
---|
201 | cpgrect(v1,v2,min,max); |
---|
202 | cpgsfs(2); |
---|
203 | cpgrect(v1,v2,min,max); |
---|
204 | cpgsci(ci); |
---|
205 | cpgsfs(fs); |
---|
206 | } |
---|
207 | |
---|
208 | int getNumOnPage(){return numOnPage;}; |
---|
209 | void setNumOnPage(int i){numOnPage=i;}; |
---|
210 | float getPaperWidth(){return paperWidth;}; |
---|
211 | void setPaperWidth(float f){paperWidth=f;}; |
---|
212 | float getPaperHeight(){return paperHeight;}; |
---|
213 | void setPaperHeight(float f){paperHeight=f;}; |
---|
214 | |
---|
215 | private: |
---|
216 | int numOnPage; // Number of spectra to put on one page. |
---|
217 | int spectraCount; // Number of spectra done so far -- where on the page? |
---|
218 | float mainCoords[4]; // Boundaries for the main spectrum [inches] |
---|
219 | float zoomCoords[4]; // Boundaries for the zoomed-in spectrum [inches] |
---|
220 | float mapCoords[4]; // Boundaries for the map box [inches] |
---|
221 | float paperWidth; // Width of the plottable region of the paper [inches] |
---|
222 | float paperHeight; // Height of the plottable region of the paper [inches] |
---|
223 | float indexSize; // PGPlot character height for tick mark labels |
---|
224 | float labelSize; // PGPlot character height for axis labels. |
---|
225 | |
---|
226 | }; |
---|
227 | |
---|
228 | //*************************************************************************** |
---|
229 | |
---|
230 | class ImagePlot |
---|
231 | { |
---|
232 | /** |
---|
233 | * ImagePlot class |
---|
234 | * A class to hold the dimensions and set up for the plots used by the two |
---|
235 | * functions below. |
---|
236 | * The physical dimensions (in inches) of the plot and the elements within |
---|
237 | * it are stored, including maximum widths and heights (so that the plot will |
---|
238 | * fit on an A4 page). |
---|
239 | * Simple accessor functions are provided to enable access to quantities needed |
---|
240 | * for pgplot routines. |
---|
241 | */ |
---|
242 | public: |
---|
243 | ImagePlot(){ |
---|
244 | paperWidth = 7.5; maxPaperHeight = 10.; marginWidth = 0.8; wedgeWidth = 0.7; |
---|
245 | }; |
---|
246 | ~ImagePlot(){}; |
---|
247 | |
---|
248 | int setUpPlot(string pgDestination, float x, float y){ |
---|
249 | /** |
---|
250 | * setUpPlot(string pgDestination, float x, float y) |
---|
251 | * Opens a pgplot device and scales it to the correct shape. |
---|
252 | * In doing so, the dimensions for the image are set, and the required aspect ratios |
---|
253 | * of the image and of the plot are calculated. |
---|
254 | * If the resulting image is going to be tall enough to exceed the maximum height |
---|
255 | * (given the default width), then scale everything down by enough to make the |
---|
256 | * height equal to maxPaperHeight. |
---|
257 | * Returns the value returned by cpgopen -- if <= 0, then an error has occurred. |
---|
258 | */ |
---|
259 | xdim = x; |
---|
260 | ydim = y; |
---|
261 | imageRatio= ydim / xdim; |
---|
262 | aspectRatio = (imageRatio*imageWidth() + 2*marginWidth) / paperWidth; |
---|
263 | if((imageRatio*imageWidth() + 2*marginWidth) > maxPaperHeight){ |
---|
264 | float correction = maxPaperHeight / (imageRatio*imageWidth() + 2*marginWidth); |
---|
265 | paperWidth *= correction; |
---|
266 | marginWidth *= correction; |
---|
267 | wedgeWidth *= correction; |
---|
268 | } |
---|
269 | int flag = cpgopen(pgDestination.c_str()); |
---|
270 | if(flag>0) cpgpap(paperWidth, aspectRatio); |
---|
271 | return flag; |
---|
272 | } |
---|
273 | |
---|
274 | void drawMapBox(float x1, float x2, float y1, float y2, string xlabel, string ylabel){ |
---|
275 | /** |
---|
276 | * drawMapBox() |
---|
277 | * Defines the region that the box containing the map is to go in, |
---|
278 | * and draws the box with limits given by the arguments. |
---|
279 | * The labels for the x and y axes are also given as arguments. |
---|
280 | */ |
---|
281 | cpgvsiz(marginWidth, marginWidth + imageWidth(), |
---|
282 | marginWidth, marginWidth + (imageWidth()*imageRatio)); |
---|
283 | cpgslw(2); |
---|
284 | cpgswin(x1,x2,y1,y2); |
---|
285 | cpgbox("bcst",0.,0,"bcst",0.,0); |
---|
286 | cpgslw(1); |
---|
287 | cpgbox("bcnst",0.,0,"bcnst",0.,0); |
---|
288 | cpglab(xlabel.c_str(), ylabel.c_str(), ""); |
---|
289 | } |
---|
290 | void makeTitle(string title){ |
---|
291 | /** |
---|
292 | * makeTitle(string) |
---|
293 | * Writes the title for the plot, making it centred for the entire plot |
---|
294 | * and not just the map. |
---|
295 | */ |
---|
296 | cpgvstd(); |
---|
297 | cpgmtxt("t", 2.7, 0.5, 0.5, title.c_str()); |
---|
298 | } |
---|
299 | |
---|
300 | float imageWidth(){ |
---|
301 | /** |
---|
302 | * imageWidth() |
---|
303 | * Returns the calculated total width of the image part of the plot [inches] |
---|
304 | */ |
---|
305 | return paperWidth - 2*marginWidth - wedgeWidth; |
---|
306 | }; |
---|
307 | |
---|
308 | float cmToCoord(float cm){return (cm/inchToCm) * ydim / (imageWidth()*imageRatio);}; |
---|
309 | float getMargin() {return marginWidth;}; |
---|
310 | float getPaperWidth() {return paperWidth;}; |
---|
311 | float getImageHeight(){return imageWidth()*imageRatio;}; |
---|
312 | float getAspectRatio(){return aspectRatio;}; |
---|
313 | |
---|
314 | |
---|
315 | private: |
---|
316 | float paperWidth; // Default (maximum) width of "paper" [inches] |
---|
317 | float maxPaperHeight; // Maximum allowed height of paper [inches] |
---|
318 | float marginWidth; // Width allowed for margins around main plot (ie. label & numbers) [inches] |
---|
319 | float wedgeWidth; // Width allowed for placement of wedge on right hand side of plot. [inches] |
---|
320 | float imageRatio; // Aspect ratio of the image only (ie. y-value range / x-value range). |
---|
321 | float aspectRatio; // Aspect ratio of whole plot. |
---|
322 | float xdim; // Width of main plot, in display units. |
---|
323 | float ydim; // Height of main plot, in display units. |
---|
324 | }; |
---|
325 | |
---|
326 | } |
---|
327 | |
---|
328 | #endif |
---|
329 | |
---|