Debian Codegraph @

src/setpage.f90

raw
1 !-----------------------------------------------------------------
2 !
3 ! This file is (or was) part of SPLASH, a visualisation tool
4 ! for Smoothed Particle Hydrodynamics written by Daniel Price:
5 !
6 ! http://users.monash.edu.au/~dprice/splash
7 !
8 ! SPLASH comes with ABSOLUTELY NO WARRANTY.
9 ! This is free software; and you are welcome to redistribute
10 ! it under the terms of the GNU General Public License
11 ! (see LICENSE file for details) and the provision that
12 ! this notice remains intact. If you modify this file, please
13 ! note section 2a) of the GPLv2 states that:
14 !
15 ! a) You must cause the modified files to carry prominent notices
16 ! stating that you changed the files and the date of any change.
17 !
18 ! Copyright (C) 2005-2019 Daniel Price. All rights reserved.
19 ! Contact: [email protected]
20 !
21 !-----------------------------------------------------------------
22
23 module pagesetup
24 implicit none
25 public :: redraw_axes, setpage2
26 real, parameter, public :: xlabeloffset = 2.5, ylabeloffset = 4.5
27
28 private
29
30 contains
31 !
32 !--this subroutine determines the setup of the plotting page
33 ! sorts out labelling of axes, positioning of windows etc
34 ! can be used as a replacement for PGENV and PGLABEL
35 !
36 ! divides up a single page into subpanels
37 !
38 !
39 ! option to tile graphs appropriately on a page:
40 ! divides up a single panel into subpanels, with a margin at the edge
41 ! should replace the call to pgenv and pglabel
42 !
43 ! for tiled plots the page setup looks like this:
44 !
45 ! | | | |
46 ! --+---+---+---+--
47 ! | 1 | 2 | 3 |
48 ! --+---+---+---+--
49 ! | 4 | 5 | 6 |
50 ! --+---+---+---+--
51 ! | | | |
52 !
53 ! (ie. with margins in x and y)
54 ! note that we divide up a single panel, so pgbeg should be called with nx=1,ny=1
55 !
56 ! arguments:
57 ! iplot : current plot number
58 ! nx : number of panels in x direction
59 ! ny : number of panels in y direction
60 ! xmin, : xmax, ymin, ymax : plot limits (if tiled should be same for all plots)
61 ! labelx : x axis label (if tiled should be same for all plots)
62 ! labely : y axis label (if tiled should be same for all plots)
63 ! title : current plot title (can differ between plots)
64 ! just : just=1 gives equal aspect ratios (same as in pgenv)
65 ! axis : axes options (same as in pgenv with a few extra)
66 ! vmarginleft,right,bottom,top : initial margin sizes (% of page (if tiled) or panel (if not))
67 ! (default should be zero for these)
68 ! tile : assumes all plots can be tiled
69 !
70 ! This version by Daniel Price, July 2006
71 !
72 subroutine setpage2(iplotin,nx,ny,xmin,xmax,ymin,ymax,labelx,labely,title,just,axis, &
73 vmarginleftin,vmarginrightin,vmarginbottomin,vmargintopin, &
74 colourbarwidth,titleoffset,isamexaxis,tile,adjustlimits,lastrow,lastplot,&
75 yscale,labelyalt,itransy)
76 use plotlib,only:plot_svp,plot_swin,plot_box,plot_qvsz,plot_annotate, &
77 plot_page,plot_qcs,plot_wnad,plot_set_exactpixelboundaries, &
78 plot_qvp
79 use asciiutils, only:string_delete
80 integer, intent(in) :: iplotin,nx,ny,just,axis,itransy
81 real, intent(inout) :: xmin, xmax, ymin, ymax
82 real, intent(in) :: colourbarwidth, titleoffset
83 real, intent(in) :: vmarginleftin,vmarginrightin,vmargintopin,vmarginbottomin
84 real, intent(in) :: yscale
85 character(len=*), intent(in) :: labelx,labely,title,labelyalt
86 logical, intent(in) :: isamexaxis,tile,adjustlimits,lastrow,lastplot
87 integer iplot,ix,iy
88 real vptsizeeffx,vptsizeeffy,panelsizex,panelsizey
89 real vmargintop,vmarginbottom,vmarginleft,vmarginright
90 real vptxmin,vptxmax,vptymin,vptymax
91 real aspectratio,devaspectratio,x1,x2,y1,y2
92 real xch,ych,dx,dy,xcen,ycen
93 character(len=10) :: xopts, yopts
94 logical, parameter :: useexactpixelboundaries = .true.
95 logical :: plot_alt_y_axis
96
97 if (axis==4) then
98 plot_alt_y_axis = .true.
99 else
100 plot_alt_y_axis = .false.
101 endif
102 !
103 ! new page if iplot > number of plots on page
104 !
105 if (iplotin > nx*ny) then
106 if (mod(iplotin,nx*ny)==1) call plot_page
107 iplot = iplotin - (nx*ny)*((iplotin-1)/(nx*ny))
108 elseif (iplotin <= 0) then
109 return
110 else
111 iplot = iplotin
112 endif
113 !
114 ! check for errors in input
115 !
116 if (nx <= 0 .or. ny <= 0) return
117 !
118 ! for tiled plots, adjust effective viewport size if just=1 and graphs are not square
119 !
120 if (tile .and. just==1) then
121 if (abs(ymax-ymin) < tiny(ymin)) then
122 print*,'setpage: error tiling plots: ymax=ymin'
123 return
124 endif
125 !
126 ! query the current aspect ratio of the device and set aspect ratio appropriately
127 !
128 call plot_qvsz(3,x1,x2,y1,y2)
129 devaspectratio = (x2-x1)/(y2-y1)
130 if (.not.adjustlimits) then
131 aspectratio = abs(((xmax-xmin)*nx)/((ymax-ymin)*ny))/devaspectratio
132 else
133 aspectratio = 1.0
134 endif
135 else
136 aspectratio = 1.0
137 endif
138 !
139 ! set positions of x and y labels in units of character height from edge
140 !
141 ! xlabeloffset = 3.0
142 ! ylabeloffset = 4.5
143 !
144 ! query the character height as fraction of viewport
145 !
146 call plot_qcs(0,xch,ych)
147 !
148 ! set margin size in units of viewport dimensions
149 ! allow enough room for the plot labels if they are drawn
150 ! nb: pgplot sets the character height as some fraction of the smallest
151 ! dimension
152 !
153 ! for tiled plots, these margins apply to the whole page
154 ! otherwise, these are applied to each panel individually
155 !
156 vmargintop = vmargintopin
157 vmarginright = vmarginrightin
158 if (axis >= 0) then
159 !--if we are drawing an axis
160 ! leave a minimum of half a character
161 ! spacing (or 0.7 for antialiasing)
162 ! so that axis numbers are not chopped
163 ! in half at the edges of the viewport
164 vmargintop = max(vmargintop,0.7*ych)
165 vmarginright = max(vmarginright,0.7*xch)
166 !--leave space for labels
167 if (axis /= 3) then
168 vmarginleft = vmarginleftin + (ylabeloffset+1.5)*xch
169 vmarginbottom = vmarginbottomin + (xlabeloffset+1.0)*ych
170 else
171 vmarginleft = vmarginleftin + 2.0*xch
172 vmarginbottom = vmarginbottomin + 1.5*ych
173 endif
174 if (plot_alt_y_axis) vmarginright = vmarginleft
175
176 if (.not.tile) then
177 if (ny > 1 .and. .not.isamexaxis) then
178 vmarginbottom = vmarginbottom + 0.5*ych
179 elseif (ny > 1) then
180 vmarginbottom = vmarginbottom + 0.25*ych
181 endif
182 endif
183 else
184 vmarginleft = vmarginleftin
185 vmarginbottom = vmarginbottomin
186 endif
187 !
188 !--set size of each panel
189 !
190 ix = iplot - ((iplot-1)/nx)*nx
191 iy = (iplot-1)/nx + 1
192
193 if (tile) then
194 !--also leave room for title if necessary
195 if (titleoffset >= 0.) then
196 vmargintop = vmargintop + (titleoffset+1.)*ych
197 endif
198
199 !
200 ! effective viewport size = size - margins (only used for tiled
201 !
202 vptsizeeffx = 1.0 - vmarginright - vmarginleft
203 vptsizeeffy = 1.0 - vmargintop - vmarginbottom
204 ! reduce x or y size if just=1 to get right aspect ratio
205 if (.not.adjustlimits) then
206 if (aspectratio <= 1.0 .and. just==1) then
207 if (aspectratio*vptsizeeffy < vptsizeeffx) then
208 vptsizeeffx = aspectratio*vptsizeeffy
209 ! but this could still be bigger than the margins allow...
210 else
211 vptsizeeffy = vptsizeeffx/aspectratio
212 endif
213 elseif (aspectratio > 1.0 .and. just==1) then
214 if (vptsizeeffx/aspectratio < vptsizeeffy) then
215 vptsizeeffy = vptsizeeffx/aspectratio
216 ! but this could still be bigger than the margins allow...
217 else
218 vptsizeeffx = vptsizeeffy*aspectratio
219 endif
220 endif
221 endif
222
223 panelsizex = vptsizeeffx/nx
224 panelsizey = vptsizeeffy/ny
225 ! print*,ix,iy,nx,ny
226 ! print*,panelsizex,panelsizey,vptsizeeffx,vptsizeeffy
227
228 ! print*,'margins = ',vmarginleft,vmarginright
229 vptxmin = vmarginleft + (ix-1)*panelsizex
230 vptxmax = vptxmin + panelsizex
231 vptymax = (1.0 - vmargintop) - (iy-1)*panelsizey
232 vptymin = vptymax - panelsizey
233 else
234 !--use full page for non-tiled plots, then set margins inside each panel
235 panelsizex = 1.0/nx
236 panelsizey = 1.0/ny
237 vptxmin = (ix-1)*panelsizex + vmarginleft
238 vptxmax = ix*panelsizex - vmarginright
239 vptymax = 1.0 - (iy-1)*panelsizey - vmargintop
240 vptymin = 1.0 - iy*panelsizey + vmarginbottom
241
242
243 !--also leave room for title if necessary
244 if (titleoffset >= 0.) then
245 vptymax = vptymax - (titleoffset+1.)*ych
246 endif
247
248 !--also leave room for colour bar if necessary
249 if (colourbarwidth > 0.) then
250 vptxmax = vptxmax - (colourbarwidth + 1.6)*xch
251 endif
252
253 endif
254 ! print*,vptxmin,vptxmax,vptymin,vptymax
255 !
256 ! set viewport
257 !
258 call plot_svp(vptxmin,vptxmax,vptymin,vptymax)
259 !
260 ! set axes
261 !
262 if (just==1) then
263 if (adjustlimits) then
264 !--query viewport aspect ratio
265 call plot_qvp(3,x1,x2,y1,y2)
266 devaspectratio = abs((x2-x1)/(y2-y1))
267
268 !--adjust limits to match viewport aspect ratio
269 dx = (xmax - xmin)/nx
270 dy = (ymax - ymin)/ny
271 if (devaspectratio*dy/dx >= 1.) then
272 xcen = 0.5*(xmin + xmax)
273 xmin = xcen - 0.5*devaspectratio*dy*ny
274 xmax = xcen + 0.5*devaspectratio*dy*ny
275 !print*,' auto-adjusting xmin = ',xmin,' xmax = ',xmax
276 else
277 ycen = 0.5*(ymin + ymax)
278 ymin = ycen - 0.5*dx*nx/devaspectratio
279 ymax = ycen + 0.5*dx*nx/devaspectratio
280 !print*,' auto-adjusting ymin = ',ymin,' ymax = ',ymax
281 endif
282 endif
283 call plot_wnad(xmin,xmax,ymin,ymax)
284 else
285 call plot_swin(xmin,xmax,ymin,ymax)
286 endif
287 !
288 ! adjust viewport to lie exactly on pixel boundaries
289 !
290 if (useexactpixelboundaries) call plot_set_exactpixelboundaries()
291 !
292 ! option to return before actually doing anything
293 !
294 if (trim(title)=='NOPGBOX') return
295 !
296 ! set options for call to pgbox (draws axes) and label axes where appropriate
297 ! (options are exactly as in pgenv apart from axis=-3,-4 which i have added)
298 !
299 yopts = '*'
300 select case(axis)
301 case(-4)
302 xopts = 'BCT'
303 case(-3)
304 xopts = 'BCST'
305 case(-2)
306 xopts = ' '
307 case(-1)
308 xopts = 'BC'
309 case(0,4)
310 xopts = 'BCST'
311 case(1)
312 xopts = 'ABCST'
313 case(2)
314 xopts = 'ABCGST'
315 case(3)
316 xopts = 'BCST'
317 case(5)
318 xopts = 'BCT'
319 case(10)
320 xopts = 'BCSTL'
321 yopts = 'BCST'
322 case(20)
323 xopts = 'BCST'
324 yopts = 'BCSTL'
325 case(30)
326 xopts = 'BCSTL'
327 yopts = 'BCSTL'
328 case default
329 print*,'setpage: illegal axis argument.'
330 xopts = 'BCNST'
331 end select
332 if (yopts=='*') yopts = xopts
333
334 if (plot_alt_y_axis) call string_delete(yopts,'C')
335 !
336 ! label plot
337 !
338 if (tile) then
339 !
340 ! decide whether to number and label the y axis
341 !
342 if (ix==nx .and. axis >= 0) then
343 !
344 !--apply label to right hand side axis if used
345 !
346 if (plot_alt_y_axis) then
347 call plot_second_y_axis(yopts,just,axis,itransy,yscale,ylabeloffset,labelyalt)
348 endif
349 endif
350 if (ix==1 .and. axis >= 0) then
351 !
352 !--label "normal" y axis
353 !
354 if (axis==3) then
355 yopts = 'N'//trim(yopts)
356 else
357 yopts = 'VN'//trim(yopts)
358 call plot_annotate('L',ylabeloffset,0.5,0.5,labely)
359 endif
360 elseif (axis >= 0) then
361 !yopts = trim(yopts)//'N'
362 endif
363 !
364 ! decide whether to number and label the x axis
365 !
366 if ((iy==ny .or. lastplot .or. lastrow) .and. axis >= 0) then
367 xopts = 'N'//trim(xopts)
368 if (axis /= 3) call plot_annotate('B',xlabeloffset,0.5,0.5,labelx)
369 endif
370 !
371 ! plot the title if inside the plot boundaries
372 !
373 if (titleoffset < 0.) call plot_annotate('t',-titleoffset,0.96,1.0,title)
374
375 elseif (axis >= 0) then
376 !
377 !--label x axis only if on last row
378 ! or if x axis quantities are different
379 !
380 if (((ny*nx-iplot) < nx).or.(.not.isamexaxis).or.lastplot) then
381 if (axis /= 3) call plot_annotate('B',xlabeloffset,0.5,0.5,labelx)
382 endif
383 !--always plot numbers
384 xopts = 'N'//trim(xopts)
385 !
386 !--apply label to right hand side axis if used
387 !
388 if (plot_alt_y_axis) then
389 call plot_second_y_axis(yopts,just,axis,itransy,yscale,ylabeloffset,labelyalt)
390 endif
391 !
392 !--always label y axis
393 !
394 if (axis==3) then
395 yopts = 'N'//trim(yopts)
396 else
397 yopts = 'VN'//trim(yopts)
398 call plot_annotate('L',ylabeloffset,0.5,0.5,labely)
399 endif
400 !
401 !--always plot title
402 !
403 call plot_annotate('T',-titleoffset,0.5,0.5,title)
404
405 endif
406
407 call plot_box(xopts,0.0,0,yopts,0.0,0)
408
409 end subroutine
410
411 !
412 !--this subroutine is a cut down version of the above, which ONLY redraws the axes
413 ! (so that axes can be redrawn on *top* of what has been plotted).
414 !
415 ! inputs:
416 ! axis : axes options (same as in PGENV, with axis=-4,-3,+3 added)
417 !
418
419 subroutine redraw_axes(iaxis,just,yscale,itransy)
420 use plotlib, only:plot_box
421 integer, intent(in) :: iaxis,just,itransy
422 character(len=10) :: xopts, yopts
423 real, intent(in) :: yscale
424 !
425 !--set plot axes (options are exactly as in PGENV, with axis=-4,-3,+3 added)
426 !
427 yopts = '*'
428 select case(iaxis)
429 case(-4)
430 xopts = 'BCT'
431 case(-3)
432 xopts = 'BCST'
433 case(-2)
434 xopts = ' '
435 case(-1)
436 xopts = 'BC'
437 case(0)
438 xopts = 'BCST'
439 case(1)
440 xopts = 'ABCST'
441 case(2)
442 xopts = 'ABCGST'
443 case(3)
444 xopts = 'BCST'
445 case(4)
446 xopts = 'BCST'
447 yopts = 'BST'
448 case(5)
449 xopts = 'BCT'
450 case(10)
451 xopts = 'BCSTL'
452 yopts = 'BCST'
453 case(20)
454 xopts = 'BCST'
455 yopts = 'BCSTL'
456 case(30)
457 xopts = 'BCSTL'
458 yopts = 'BCSTL'
459 case default
460 print*,'redraw_axes: illegal AXIS argument.'
461 xopts = 'BCST'
462 end select
463 if (yopts=='*') yopts = xopts
464
465 if (iaxis==4) call plot_second_y_axis(yopts,just,iaxis,itransy,yscale)
466 call plot_box(xopts,0.0,0,yopts,0.0,0)
467
468 end subroutine redraw_axes
469
470 subroutine plot_second_y_axis(yopts,just,iaxis,itransy,yscale,ylabeloffset,labely)
471 use plotlib, only:plot_box,plot_annotate,plot_qwin,plot_swin,plot_wnad
472 use asciiutils, only:string_delete
473 use transforms, only:transform,transform_inverse,transform_label
474 character(len=*), intent(in) :: yopts
475 real, intent(in) :: yscale
476 real, intent(in), optional :: ylabeloffset
477 character(len=*), intent(in), optional :: labely
478 integer, intent(in) :: just,iaxis,itransy
479 character(len=10) :: yoptsi
480 character(len=120) :: labelyalt
481 real :: xmin,xmax,ymin,ymax,yminalt,ymaxalt
482
483 yoptsi = yopts
484 call string_delete(yoptsi,'B')
485 call string_delete(yoptsi,'N')
486
487 !--save plot window settings
488 call plot_qwin(xmin,xmax,ymin,ymax)
489
490 !--scaling of y axis: multiplication in un-transformed space
491 yminalt = ymin
492 ymaxalt = ymax
493 if (itransy > 0) call transform_inverse(yminalt,ymaxalt,itransy)
494 yminalt = yminalt*yscale
495 ymaxalt = ymaxalt*yscale
496 if (itransy > 0) call transform(yminalt,ymaxalt,itransy)
497
498 !--set plot window to new scaled y axis
499 call plot_swin(xmin,xmax,yminalt,ymaxalt)
500
501 !--draw axes and label on right hand side of box
502 if (iaxis==3) then
503 call plot_box(' ',0.0,0,'1MC'//trim(yoptsi),0.0,0)
504 else
505 call plot_box(' ',0.0,0,'1VMC'//trim(yoptsi),0.0,0)
506 endif
507 if (present(labely) .and. present(ylabeloffset)) then
508 labelyalt = labely
509 if (itransy > 0) labelyalt = transform_label(labely,itransy)
510 call plot_annotate('R',ylabeloffset,0.5,0.5,labelyalt)
511 endif
512
513 !--reset plot window
514 if (just==1) then
515 call plot_wnad(xmin,xmax,ymin,ymax)
516 else
517 call plot_swin(xmin,xmax,ymin,ymax)
518 endif
519
520 end subroutine plot_second_y_axis
521
522 end module pagesetup