Debian Codegraph @

src/render.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-2021 Daniel Price. All rights reserved.
19 ! Contact: [email protected]
20 !
21 !-----------------------------------------------------------------
22
23 !------------------------------------------------------------------------
24 ! Module containing "interface" routines between the calculated
25 ! pixel arrays and the plot library routines which do the actual rendering
26 !------------------------------------------------------------------------
27 module render
28 use colourbar, only:plotcolourbar
29 implicit none
30 public :: render_pix, render_vec, set_transparency
31 private
32
33 contains
34
35 !------------------------------------------------------------------------
36 ! this subroutine takes a 2D grid of data and renders it via the
37 ! plotting library. Rendering is either:
38 ! - contours (icolouropt = 0)
39 ! - greyscale (icolouropt = 1)
40 ! - colour (icolouropt>1)
41 ! contouring plots nc contours between datmin and datmax.
42 !------------------------------------------------------------------------
43 subroutine render_pix(datpix,datmin,datmax,label, &
44 xmin,ymin,dx,dy,icolouropt,iplotcont,iColourBarStyle,ncontours,log, &
45 ilabelcont,contmin,contmax,blank,transparent,alpha)
46 use plotutils, only:formatreal
47 use plotlib, only:plot_imag,plot_conb,plot_cons,plot_qch,plot_sch,&
48 plot_qch,plot_sch,plot_conl,plot_gray,plot_imag_transparent,&
49 plot_imag_alpha
50 use contours_module, only:read_contours,contours_list,contourtitles
51 integer, intent(in) :: ncontours,icolouropt
52 real, intent(in) :: xmin,ymin,datmin,datmax,dx,dy
53 real, dimension(:,:), intent(in) :: datpix
54 logical, intent(in) :: iplotcont,log,ilabelcont
55 integer, intent(in) :: iColourBarStyle
56 character(len=*), intent(in) :: label
57 real, intent(in), optional :: contmin,contmax,blank
58 logical, intent(in), optional :: transparent
59 real, dimension(:,:), intent(in), optional :: alpha
60
61 integer :: i,ierr,nc,npixx,npixy
62 real :: trans(6),levels(ncontours),dcont,charheight,cmin,cmax
63 character(len=12) :: string
64 logical :: iuse_transparent,ifixed_contours
65 !
66 !--set up grid for rendering
67 !
68 trans(1) = xmin - 0.5*dx ! this is for the pgimag call
69 trans(2) = dx ! see help for pgimag/pggray/pgcont
70 trans(3) = 0.0
71 trans(4) = ymin - 0.5*dy
72 trans(5) = 0.0
73 trans(6) = dy
74
75 iuse_transparent = .false.
76 if (present(transparent)) iuse_transparent = transparent
77
78 npixx = size(datpix,1)
79 npixy = size(datpix,2)
80
81 if (abs(icolouropt)==1) then ! greyscale
82
83 if (iColourBarStyle > 0) call plotcolourbar(iColourBarstyle,icolouropt,datmin,datmax,trim(label),log,0.)
84
85 if (icolouropt==1) then
86 call plot_gray(datpix,npixx,npixy,1,npixx,1,npixy,datmin,datmax,trans)
87 else !--allow inverse greyscale
88 call plot_imag(datpix,npixx,npixy,1,npixx,1,npixy,datmin,datmax,trans)
89 endif
90 elseif (abs(icolouropt) > 0) then ! colour
91 !
92 !--plot colour bar
93 !
94 if (iColourBarStyle > 0) call plotcolourbar(iColourBarstyle,icolouropt,datmin,datmax,trim(label),log,0.)
95 !
96 !--plot pixel map
97 !
98 if (iuse_transparent) then
99 call plot_imag_transparent(datpix,npixx,npixy,1,npixx,1,npixy,datmin,datmax,trans)
100 else
101 if (present(alpha)) then
102 call plot_imag_alpha(datpix,alpha,npixx,npixy,1,npixx,1,npixy,datmin,datmax,trans)
103 else
104 call plot_imag(datpix,npixx,npixy,1,npixx,1,npixy,datmin,datmax,trans)
105 endif
106 endif
107 endif
108 !
109 !--contours
110 !
111 if (iplotcont) then
112 nc = ncontours
113 if (present(contmin)) then
114 cmin = contmin
115 else
116 cmin = datmin
117 endif
118 if (present(contmax)) then
119 cmax = contmax
120 else
121 cmax = datmax
122 endif
123 !
124 !--set contour levels: first attempt to read these
125 ! from a file. If file does not exist or errors during read
126 ! then we construct the default levels as usual.
127 !
128 call read_contours(nc,ierr)
129 if (ierr==0 .and. nc > 0) then
130 ifixed_contours = .true.
131 else
132 nc = ncontours
133 ifixed_contours = .false.
134 endif
135
136 if (ifixed_contours) then
137 do i=1,min(nc,ncontours)
138 print*,"contour @ ", contours_list(i), ": ", trim(contourtitles(i))
139 levels(i) = contours_list(i)
140 enddo
141 dcont = 0.
142 elseif (nc <= 0) then
143 print*,'ERROR: cannot plot contours with ',nc,' levels'
144 return
145 elseif (nc==1) then
146 levels(1) = cmin
147 dcont = 0.
148 else
149 dcont = (cmax-cmin)/real(nc-1) ! even contour levels
150 do i=1,nc
151 levels(i) = cmin + real(i-1)*dcont
152 enddo
153 endif
154 !
155 !--plot contours (use pgcont if pgcons causes trouble)
156 ! with blanking if blank is input
157 !
158 if (present(blank)) then
159 if (.not.ifixed_contours) then
160 print 10,nc,' contours (with blanking)',levels(1),levels(nc),dcont
161 print 20,levels(1:nc)
162 endif
163 !print*,' blanking = ',blank,'min,max = ',datmin,datmax
164 call plot_conb(datpix,npixx,npixy,1,npixx,1,npixy,levels,nc,trans,blank)
165 else
166 if (.not.ifixed_contours) then
167 print 10,nc,' contours',levels(1),levels(nc),dcont
168 print 20,levels(1:nc)
169 endif
170 call plot_cons(datpix,npixx,npixy,1,npixx,1,npixy,levels,nc,trans)
171 endif
172 10 format(1x,'plotting ',i4,a,' between ',es10.2,' and ',es10.2,', every ',es10.2,':')
173 20 format(10(6(1x,es9.2),/))
174 !
175 !--labelling of contour levels
176 !
177 if (ilabelcont) then
178 call plot_qch(charheight) ! query character height
179 call plot_sch(0.75*charheight) ! shrink character height
180
181 do i=1,nc
182 if (ifixed_contours) then
183 string=trim(adjustl(contourtitles(i)))
184 else
185 call formatreal(levels(i),string)
186 endif
187 call plot_conl(datpix,npixx,npixy,1,npixx,1,npixy,levels(i),trans,trim(string),npixx/2,30)
188 enddo
189 call plot_sch(charheight) ! restore character height
190 endif
191 !
192 !--this line prints the label inside the contour plot
193 ! (now obsolete-- this functionality can be achieved using plot titles)
194 ! call pgmtxt('T',-2.0,0.05,0.0,trim(label))
195
196 endif
197
198 end subroutine render_pix
199
200 !--------------------------------------------------------------------------
201 ! this subroutine takes a 2D grid of vector data (ie. x and y components)
202 ! and plots an arrow map of it
203 !--------------------------------------------------------------------------
204
205 subroutine render_vec(vecpixx,vecpixy,vecmax,npixx,npixy, &
206 xmin,ymin,dx,dy,label,unitslabel,plotlegend)
207 use legends, only:legend_vec
208 use settings_vecplot, only:hposlegendvec,vposlegendvec,&
209 iplotarrowheads,iallarrowssamelength
210 use plotlib, only:plot_sah,plot_qch,plot_sch,plot_vect
211 integer, intent(in) :: npixx,npixy
212 real, intent(in) :: xmin,ymin,dx,dy
213 real, intent(inout) :: vecmax
214 real, dimension(npixx,npixy), intent(in) :: vecpixx,vecpixy
215 real, dimension(npixx,npixy) :: dvmag
216 character(len=*), intent(in) :: label,unitslabel
217 logical, intent(in) :: plotlegend
218 real :: trans(6),scale
219 real :: charheight
220
221 !set up grid for rendering
222
223 trans(1) = xmin - 0.5*dx ! this is for the pgimag call
224 trans(2) = dx ! see help for pgimag/pggray/pgcont
225 trans(3) = 0.0
226 trans(4) = ymin - 0.5*dy
227 trans(5) = 0.0
228 trans(6) = dy
229
230 print*,'vector plot..',npixx,'x',npixy,'=',size(vecpixx),' pixels'
231 !!print*,'max(x component) = ',maxval(vecpixx),'max(y component) = ',maxval(vecpixy)
232
233 if (iplotarrowheads) then
234 call plot_sah(2,45.0,0.7) ! arrow style
235 else
236 call plot_sah(2,0.0,1.0)
237 endif
238 call plot_qch(charheight)
239 call plot_sch(0.3*charheight) ! size of arrow head
240
241 if (iallarrowssamelength) then
242 !!if (vecmax <= 0.0) vecmax = 1.0 ! adaptive limits
243 scale=0.9*dx !!/vecmax
244 print*,trim(label),' showing direction only: max = ',vecmax
245
246 where (abs(vecpixx) > tiny(vecpixx) .and. abs(vecpixy) > tiny(vecpixy))
247 dvmag(:,:) = 1./sqrt(vecpixx**2 + vecpixy**2)
248 elsewhere
249 dvmag(:,:) = 0.
250 end where
251
252 call plot_vect(vecpixx(:,:)*dvmag(:,:),vecpixy(:,:)*dvmag(:,:),npixx,npixy, &
253 1,npixx,1,npixy,scale,0,trans,0.0)
254 else
255 if (vecmax <= 0.0) then ! adaptive limits
256 scale = 0.0
257 vecmax = max(maxval(vecpixx(:,:)),maxval(vecpixy(:,:)))
258 if (vecmax > 0.) scale = dx/vecmax
259 else
260 scale=dx/vecmax
261 endif
262 print*,trim(label),' max = ',vecmax
263
264 call plot_vect(vecpixx(:,:),vecpixy(:,:),npixx,npixy, &
265 1,npixx,1,npixy,scale,0,trans,0.0)
266
267 if (plotlegend) then
268 call legend_vec(label,unitslabel,vecmax,dx,hposlegendvec,vposlegendvec,charheight)
269 endif
270 endif
271
272 call plot_sch(charheight)
273
274 end subroutine render_vec
275
276 !--------------------------------------------------------------------------
277 ! set opacity for double-rendering, i.e.:
278 ! - data values below colour bar minimum are transparent
279 ! - data values above 25% of colour bar range are opaque
280 ! - linear scaling in between
281 !--------------------------------------------------------------------------
282 subroutine set_transparency(nx,ny,datpix,brightness,dmin,dmax)
283 integer, intent(in) :: nx,ny
284 real, intent(in) :: datpix(nx,ny)
285 real, intent(out), allocatable :: brightness(:,:)
286 real, intent(in) :: dmin,dmax
287
288 if (.not.allocated(brightness)) allocate(brightness(nx,ny))
289
290 if (abs(dmax-dmin) < tiny(0.)) then
291 brightness = 1. ! avoid divide by zero if max=min
292 else
293 brightness = max(min(4.*(datpix-dmin)/(dmax-dmin),1.0),0.)
294 endif
295
296 end subroutine set_transparency
297
298 end module render