1 | #include <iostream> |
---|
2 | #include <sstream> |
---|
3 | #include <string> |
---|
4 | #include <math.h> |
---|
5 | #include <cpgplot.h> |
---|
6 | #include <param.hh> |
---|
7 | #include <Utils/mycpgplot.hh> |
---|
8 | #include <Cubes/plots.hh> |
---|
9 | |
---|
10 | using std::stringstream; |
---|
11 | using namespace mycpgplot; |
---|
12 | |
---|
13 | namespace Plot |
---|
14 | { |
---|
15 | |
---|
16 | //---------------------------------------------------------- |
---|
17 | // SpectralPlot functions |
---|
18 | //---------------------------------------------------------- |
---|
19 | |
---|
20 | SpectralPlot::SpectralPlot(){ |
---|
21 | paperWidth=a4width/inchToCm - 2*psHoffset; |
---|
22 | spectraCount=0; |
---|
23 | numOnPage = 5; |
---|
24 | }; |
---|
25 | |
---|
26 | SpectralPlot::~SpectralPlot(){}; |
---|
27 | //---------------------------------------------------------- |
---|
28 | int SpectralPlot::setUpPlot(std::string pgDestination){ |
---|
29 | /** |
---|
30 | * Opens the designated pgplot device. Scales the paper so that |
---|
31 | * it fits on an A4 sheet (using known values of the default |
---|
32 | * pgplot offsets). |
---|
33 | * |
---|
34 | * \param pgDestination The std::string indicating the PGPLOT device to |
---|
35 | * be written to. |
---|
36 | * |
---|
37 | * \return The value returned by cpgopen. If <= 0, then an error |
---|
38 | * has occurred. |
---|
39 | */ |
---|
40 | paperHeight = paperWidth*M_SQRT2; |
---|
41 | if(paperHeight+2*psVoffset > a4height){ |
---|
42 | paperHeight = a4height - 2*psVoffset; |
---|
43 | paperWidth = paperHeight / M_SQRT2; |
---|
44 | } |
---|
45 | identifier = cpgopen(pgDestination.c_str()); |
---|
46 | if(identifier>0) cpgpap(paperWidth, paperHeight/paperWidth); |
---|
47 | // make paper size to fit on A4. |
---|
48 | return identifier; |
---|
49 | } |
---|
50 | //---------------------------------------------------------- |
---|
51 | void SpectralPlot::calcCoords(){ |
---|
52 | /** |
---|
53 | * Calculates the boundaries for the various boxes, in inches measured |
---|
54 | * from the lower left corner. |
---|
55 | * Based on the fact that there are numOnPage spectra shown on each |
---|
56 | * page, going down the page in increasing number (given by |
---|
57 | * SpectralPlot::spectraCount). |
---|
58 | */ |
---|
59 | int posOnPage = (numOnPage - (spectraCount%numOnPage))%numOnPage; |
---|
60 | mainCoords[0] = Plot::spMainX1/inchToCm; |
---|
61 | mainCoords[1] = Plot::spMainX2/inchToCm; |
---|
62 | zoomCoords[0] = Plot::spZoomX1/inchToCm; |
---|
63 | zoomCoords[1] = Plot::spZoomX2/inchToCm; |
---|
64 | mainCoords[2] = zoomCoords[2] = mapCoords[2] = |
---|
65 | posOnPage*paperHeight/float(numOnPage) + Plot::spMainY1/inchToCm; |
---|
66 | mainCoords[3] = zoomCoords[3] = mapCoords[3] = |
---|
67 | posOnPage*paperHeight/float(numOnPage) + Plot::spMainY2/inchToCm; |
---|
68 | mapCoords[0] = Plot::spMapX1/inchToCm; |
---|
69 | mapCoords[1] = mapCoords[0] + (mapCoords[3]-mapCoords[2]); |
---|
70 | } |
---|
71 | //---------------------------------------------------------- |
---|
72 | void SpectralPlot::gotoHeader(std::string xlabel){ |
---|
73 | /** |
---|
74 | * Calls calcCoords, to calculate correct coordinates for this spectrum. |
---|
75 | * Defines the region for the header information, making it centred |
---|
76 | * on the page. |
---|
77 | * Also writes the velocity (x axis) label, given by the string argument. |
---|
78 | * \param xlabel Label to go on the velocity/spectral axis. |
---|
79 | */ |
---|
80 | if(spectraCount%numOnPage==0) cpgpage(); |
---|
81 | spectraCount++; |
---|
82 | calcCoords(); |
---|
83 | cpgvsiz(0., paperWidth, mainCoords[2], mainCoords[3]); |
---|
84 | cpgsch(spLabelSize); |
---|
85 | cpgmtxt("b",Plot::spXlabelOffset,0.5,0.5,xlabel.c_str()); |
---|
86 | } |
---|
87 | //---------------------------------------------------------- |
---|
88 | void SpectralPlot::gotoMainSpectrum(float x1, float x2, float y1, float y2, std::string ylabel){ |
---|
89 | /** |
---|
90 | * Defines the region for the main spectrum. |
---|
91 | * Draws the box, with tick marks, and |
---|
92 | * writes the flux (y axis) label, given by the string argument. |
---|
93 | * \param x1 Minimum X-coordinate of box. |
---|
94 | * \param x2 Maximum X-coordinate of box. |
---|
95 | * \param y1 Minimum Y-coordinate of box. |
---|
96 | * \param y2 Maximum Y-coordinate of box. |
---|
97 | * \param ylabel Label for the flux (Y) axis. |
---|
98 | */ |
---|
99 | cpgvsiz(mainCoords[0],mainCoords[1],mainCoords[2],mainCoords[3]); |
---|
100 | cpgsch(spIndexSize); |
---|
101 | cpgswin(x1,x2,y1,y2); |
---|
102 | cpgbox("1bcnst",0.,0,"bcnst1v",0.,0); |
---|
103 | cpgsch(spLabelSize); |
---|
104 | cpgmtxt("l",Plot::spYlabelOffset,0.5,0.5,ylabel.c_str()); |
---|
105 | } |
---|
106 | //---------------------------------------------------------- |
---|
107 | void SpectralPlot::gotoZoomSpectrum(float x1, float x2, float y1, float y2){ |
---|
108 | /** |
---|
109 | * Defines the region for the zoomed-in part of the spectrum. |
---|
110 | * Draws the box, with special tick marks on the bottom axis. |
---|
111 | * \param x1 Minimum X-coordinate of box. |
---|
112 | * \param x2 Maximum X-coordinate of box. |
---|
113 | * \param y1 Minimum Y-coordinate of box. |
---|
114 | * \param y2 Maximum Y-coordinate of box. |
---|
115 | */ |
---|
116 | cpgvsiz(zoomCoords[0],zoomCoords[1],zoomCoords[2],zoomCoords[3]); |
---|
117 | cpgsch(spIndexSize); |
---|
118 | cpgswin(x1,x2,y1,y2); |
---|
119 | cpgbox("bc",0.,0,"bcstn1v",0.,0); |
---|
120 | float lengthL,lengthR,disp,tickpt,step; |
---|
121 | stringstream label; |
---|
122 | for(int i=1;i<10;i++){ |
---|
123 | tickpt = x1+(x2-x1)*float(i)/10.; // spectral coord of the tick |
---|
124 | switch(i) |
---|
125 | { |
---|
126 | case 2: |
---|
127 | case 8: |
---|
128 | lengthL = lengthR = 0.5; |
---|
129 | disp = 0.3 + float(i-2)/6.; // i==2 --> disp=0.3, i==8 --> disp=1.3 |
---|
130 | label.str(""); |
---|
131 | label << tickpt; |
---|
132 | // do a labelled tick mark |
---|
133 | cpgtick(x1,y1,x2,y1,float(i)/10.,lengthL,lengthR, |
---|
134 | disp, 0., label.str().c_str()); |
---|
135 | break; |
---|
136 | default: |
---|
137 | label.str(""); |
---|
138 | lengthL = 0.25; |
---|
139 | lengthR = 0.; |
---|
140 | disp = 0.; // not used in this case, but set it anyway. |
---|
141 | break; |
---|
142 | } |
---|
143 | // first the bottom axis, just the ticks |
---|
144 | if(fabs(tickpt)<(x2-x1)/1.e4) step = 2.*(x2-x1); |
---|
145 | else step = tickpt; |
---|
146 | cpgaxis("", |
---|
147 | tickpt-0.001*(x2-x1), y1, |
---|
148 | tickpt+0.001*(x2-x1), y1, |
---|
149 | tickpt-0.001*(x2-x1), tickpt+0.001*(x2-x1), |
---|
150 | step, -1, lengthL,lengthR, 0.5, disp, 0.); |
---|
151 | //and now the top -- no labels, just tick marks |
---|
152 | cpgtick(x1,y2,x2,y2,float(i)/10.,lengthL,lengthR,0.,0.,""); |
---|
153 | } |
---|
154 | } |
---|
155 | //---------------------------------------------------------- |
---|
156 | void SpectralPlot::gotoMap(){ |
---|
157 | cpgvsiz(mapCoords[0],mapCoords[1],mapCoords[2],mapCoords[3]); |
---|
158 | cpgsch(spIndexSize); |
---|
159 | } |
---|
160 | //---------------------------------------------------------- |
---|
161 | void SpectralPlot::drawVelRange(float v1, float v2){ |
---|
162 | /** |
---|
163 | * Draws two vertical lines at the limits of velocity |
---|
164 | * given by the arguments. |
---|
165 | * \param v1 Minimum velocity. |
---|
166 | * \param v2 Maximum velocity. |
---|
167 | */ |
---|
168 | int ci,ls; |
---|
169 | float dud,min,max; |
---|
170 | cpgqwin(&dud,&dud,&min,&max); |
---|
171 | cpgqci(&ci); |
---|
172 | cpgqls(&ls); |
---|
173 | cpgsci(BLUE); |
---|
174 | cpgsls(DASHED); |
---|
175 | cpgmove(v1,min); cpgdraw(v1,max); |
---|
176 | cpgmove(v2,min); cpgdraw(v2,max); |
---|
177 | cpgsci(ci); |
---|
178 | cpgsls(ls); |
---|
179 | } |
---|
180 | //---------------------------------------------------------- |
---|
181 | void SpectralPlot::drawMWRange(float v1, float v2){ |
---|
182 | /** |
---|
183 | * Draws a box showing the extent of channels masked by the |
---|
184 | * Milky Way parameters |
---|
185 | * \param v1 Minimum velocity of the Milky Way range. |
---|
186 | * \param v2 Maximum velocity of the Milky Way range. |
---|
187 | */ |
---|
188 | int ci,fs; |
---|
189 | float dud,min,max,height; |
---|
190 | cpgqwin(&dud,&dud,&min,&max); |
---|
191 | height = max-min; |
---|
192 | max += 0.01*height; |
---|
193 | min -= 0.01*height; |
---|
194 | cpgqci(&ci); |
---|
195 | cpgqfs(&fs); |
---|
196 | setDarkGreen(); |
---|
197 | cpgsci(DARKGREEN); |
---|
198 | cpgsfs(HATCHED); |
---|
199 | cpgrect(v1,v2,min,max); |
---|
200 | cpgsfs(OUTLINE); |
---|
201 | cpgrect(v1,v2,min,max); |
---|
202 | cpgsci(ci); |
---|
203 | cpgsfs(fs); |
---|
204 | } |
---|
205 | |
---|
206 | //---------------------------------------------------------- |
---|
207 | //---------------------------------------------------------- |
---|
208 | // ImagePlot functions |
---|
209 | //---------------------------------------------------------- |
---|
210 | |
---|
211 | ImagePlot::ImagePlot(){ |
---|
212 | paperWidth = 7.5; |
---|
213 | maxPaperHeight = 10.; |
---|
214 | marginWidth = 0.8; |
---|
215 | wedgeWidth = 0.7; |
---|
216 | }; |
---|
217 | |
---|
218 | ImagePlot::~ImagePlot(){}; |
---|
219 | //---------------------------------------------------------- |
---|
220 | |
---|
221 | int ImagePlot::setUpPlot(std::string pgDestination, float x, float y){ |
---|
222 | /** |
---|
223 | * Opens a pgplot device and scales it to the correct shape. |
---|
224 | * In doing so, the dimensions for the image are set, and the required |
---|
225 | * aspect ratios of the image and of the plot are calculated. |
---|
226 | * If the resulting image is going to be tall enough to exceed the |
---|
227 | * maximum height (given the default width), then scale everything |
---|
228 | * down by enough to make the height equal to maxPaperHeight. |
---|
229 | * \param pgDestination The string indicating the PGPLOT device to be |
---|
230 | * written to. |
---|
231 | * \param x The length of the X-axis. |
---|
232 | * \param y The length of the Y-axis. |
---|
233 | * \return The value returned by cpgopen: if <= 0, then an error |
---|
234 | * has occurred. |
---|
235 | */ |
---|
236 | xdim = x; |
---|
237 | ydim = y; |
---|
238 | imageRatio= ydim / xdim; |
---|
239 | aspectRatio = (imageRatio*imageWidth() + 2*marginWidth) / paperWidth; |
---|
240 | float correction; |
---|
241 | if((imageRatio*imageWidth() + 2*marginWidth) > maxPaperHeight){ |
---|
242 | correction = maxPaperHeight / (imageRatio*imageWidth()+2*marginWidth); |
---|
243 | paperWidth *= correction; |
---|
244 | marginWidth *= correction; |
---|
245 | wedgeWidth *= correction; |
---|
246 | } |
---|
247 | identifier = cpgopen(pgDestination.c_str()); |
---|
248 | if(identifier>0) cpgpap(paperWidth, aspectRatio); |
---|
249 | return identifier; |
---|
250 | } |
---|
251 | //---------------------------------------------------------- |
---|
252 | void ImagePlot::drawMapBox(float x1, float x2, float y1, float y2, |
---|
253 | std::string xlabel, std::string ylabel){ |
---|
254 | /** |
---|
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 | * Also writes labels on both X- and Y-axes. |
---|
258 | * \param x1 Minimum X-axis value. |
---|
259 | * \param x2 Maximum X-axis value. |
---|
260 | * \param y1 Minimum Y-axis value. |
---|
261 | * \param y2 Maximum Y-axis value. |
---|
262 | * \param xlabel The label to be put on the X-axis. |
---|
263 | * \param ylabel The label to be put on the Y-axis. |
---|
264 | */ |
---|
265 | cpgvsiz(marginWidth, marginWidth + imageWidth(), |
---|
266 | marginWidth, marginWidth + (imageWidth()*imageRatio)); |
---|
267 | cpgslw(2); |
---|
268 | cpgswin(x1,x2,y1,y2); |
---|
269 | cpgbox("bcst",0.,0,"bcst",0.,0); |
---|
270 | cpgslw(1); |
---|
271 | cpgbox("bcnst",0.,0,"bcnst",0.,0); |
---|
272 | cpglab(xlabel.c_str(), ylabel.c_str(), ""); |
---|
273 | } |
---|
274 | //---------------------------------------------------------- |
---|
275 | void ImagePlot::makeTitle(std::string title){ |
---|
276 | /** |
---|
277 | * Writes the title for the plot, making it centred for the entire |
---|
278 | * plot and not just the map. |
---|
279 | * \param title String with title for plot. |
---|
280 | */ |
---|
281 | cpgvstd(); |
---|
282 | cpgmtxt("t", Plot::imTitleOffset, 0.5, 0.5, title.c_str()); |
---|
283 | } |
---|
284 | |
---|
285 | |
---|
286 | } |
---|