Debian Codegraph @

src/write_pixmap.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-2013 Daniel Price. All rights reserved.
19 ! Contact: [email protected]
20 !
21 !-----------------------------------------------------------------
22
23 !-----------------------------------------------------------------
24 ! module containing output routines for writing pixel map
25 ! to output file in various formats
26 !
27 ! (c) D. Price 21/09/07
28 ! Added read routines June 2009
29 !-----------------------------------------------------------------
30 module write_pixmap
31 use filenames, only:fileprefix,tagline
32 implicit none
33 logical, public :: iwritepixmap = .false.
34 logical, public :: ireadpixmap = .false.
35 character(len=5), public :: pixmapformat = ' '
36 character(len=5), public :: readpixformat = ' '
37 public :: writepixmap,write_pixmap_ppm,write_pixmap_ascii
38 public :: isinputformat,isoutputformat
39 public :: readpixmap
40
41 private
42
43 contains
44
45 !-----------------------------------------------------------------
46 ! utility to check if an output format selection is valid
47 !-----------------------------------------------------------------
48 logical function isoutputformat(string)
49 character(len=*), intent(in) :: string
50
51 isoutputformat = .false.
52 select case(trim(string))
53 case('ascii','ppm','pfm')
54 isoutputformat = .true.
55 end select
56
57 if (.not.isoutputformat) then
58 print "(a)",' possible formats for -o option: '
59 print "(a)",' -o ppm : dump pixel map to portable pixel map file'
60 print "(a)",' -o pfm : dump pixel map to portable float map file'
61 print "(a)",' -o ascii : dump pixel map to ascii file'
62 print "(a)",' use -p to change the prefix for the filenames'
63 endif
64
65 return
66 end function isoutputformat
67
68 !-----------------------------------------------------------------
69 ! utility to check if an input format selection is valid
70 !-----------------------------------------------------------------
71 logical function isinputformat(string)
72 character(len=*), intent(in) :: string
73
74 isinputformat = .false.
75 select case(trim(string))
76 case('ascii','ftn','ftn512','chf')
77 isinputformat = .true.
78 end select
79
80 if (.not.isinputformat) then
81 print "(a)",' possible formats for -readpix option: '
82 print "(a)",' -readpix ascii : read pixel maps from ascii file'
83 print "(a)",' -readpix ftn : read pixel maps from unformatted fortran file "read(1) dat(:,:)"'
84 print "(a)",' -readpix ftn512 : read pixel maps from unformatted fortran file "read(1) dat(1:512,1:512)"'
85 endif
86
87 return
88 end function isinputformat
89
90 !-----------------------------------------------------------------
91 ! wrapper routine for all output formats
92 !-----------------------------------------------------------------
93 subroutine writepixmap(datpix,npixx,npixy,xmin,ymin,dx,datmin,datmax,label,labu,istep,xsec,dumpfile,time)
94 use write_pfm, only:write_pixmap_pfm
95 use iso_c_binding, only:c_float
96 use labels, only:shortlabel
97 integer, intent(in) :: npixx,npixy
98 real, intent(in), dimension(npixx,npixy) :: datpix
99 real, intent(in) :: xmin,ymin,dx,datmin,datmax,time
100 logical, intent(in) :: xsec
101 character(len=*), intent(in) :: label,labu,dumpfile
102 integer, intent(in) :: istep
103 character(len=len(dumpfile)+10) :: filename
104 real(c_float), allocatable :: dattmp(:,:)
105 integer :: ierr
106
107 select case(trim(pixmapformat))
108 case('ascii')
109 call get_pixmap_filename(filename,dumpfile,shortlabel(label,labu),'.pix',xsec)
110 call write_pixmap_ascii(datpix,npixx,npixy,xmin,ymin,dx,datmin,datmax,label,filename,time)
111 case('ppm')
112 call write_pixmap_ppm(datpix,npixx,npixy,xmin,ymin,dx,datmin,datmax,label,istep)
113 case('pfm')
114 call get_pixmap_filename(filename,dumpfile,shortlabel(label,labu),'.pfm',xsec)
115 write(*,"(a)") '> writing pixel map to file '//trim(filename)//' ...'
116 ! convert to single precision
117 allocate(dattmp(npixx,npixy))
118 dattmp = real(datpix,kind=c_float)
119 call write_pixmap_pfm(filename,npixx,npixy,dattmp,ierr)
120 deallocate(dattmp)
121 if (ierr /= 0) print "(a)",' ERROR writing '//trim(filename)
122 ! case('fits')
123 ! call write_pixmap_fits(datpix,npixx,npixy,xmin,ymin,dx,datmin,datmax,label)
124 case default
125 print "(a)",' ERROR: invalid output format for pixel map '
126 end select
127
128 end subroutine writepixmap
129
130 !-----------------------------------------------------------------
131 ! output pixmap as an ascii file
132 !-----------------------------------------------------------------
133 subroutine write_pixmap_ascii(datpix,npixx,npixy,xmin,ymin,dx,datmin,datmax,label,filename,time)
134 integer, intent(in) :: npixx,npixy
135 real, intent(in), dimension(npixx,npixy) :: datpix
136 real, intent(in) :: xmin,ymin,dx,datmin,datmax,time
137 character(len=*), intent(in) :: label,filename
138 character(len=10) :: stringx,stringy
139 character(len=30) :: fmtstring
140 integer :: ierr,j
141 integer, parameter :: iunit = 166
142 !
143 !--write ascii file
144 !
145 open(unit=iunit,file=filename,status='replace',form='formatted',iostat=ierr)
146 if (ierr /=0) then
147 print*,'error opening '//trim(filename)
148 return
149 endif
150 write(*,"(a)",ADVANCE='NO') '> writing pixel map to file '//trim(filename)//' ...'
151
152 write(stringx,"(i10)") npixx
153 write(stringy,"(i10)") npixy
154 write(iunit,"(a)",err=66) '# '//trim(adjustl(filename))//' created by '//trim(tagline)
155 write(iunit,"(a)",err=66) '# Contains 2D pixel array '//trim(adjustl(stringx))//' x '//trim(adjustl(stringy))//' written as '
156 write(iunit,"(a)",err=66) '# do j=1,'//trim(adjustl(stringy))
157 write(iunit,"(a)",err=66) '# write(*,*) dat(1:'//trim(adjustl(stringx))//',j)'
158 write(iunit,"(a)",err=66) '# enddo'
159 write(iunit,"(a,1pe14.6,a,1pe14.6)",err=66) '# '//trim(label)//': min = ',datmin,' max = ',datmax
160 write(iunit,"(a,1pe14.6,a,1pe14.6)",err=66) '# x axis: min = ',xmin,' max = ',xmin+(npixx-1)*dx
161 write(iunit,"(a,1pe14.6,a,1pe14.6)",err=66) '# y axis: min = ',ymin,' max = ',ymin+(npixy-1)*dx
162 write(iunit,"(a,1pe14.6)", err=66) '# time = ',time
163 write(iunit,"(a)",err=66) '# '//trim(adjustl(stringx))//' '//trim(adjustl(stringy))
164
165 write(fmtstring,"(a,i6,a)",iostat=ierr) '(',npixx,'(1pe14.6))'
166 if (ierr /= 0) then
167 do j=1,npixy
168 write(iunit,*,err=66) datpix(1:npixx,j)
169 enddo
170 else
171 do j=1,npixy
172 write(iunit,fmtstring,err=66) datpix(1:npixx,j)
173 enddo
174 endif
175 close(iunit)
176 print "(a)",'OK'
177 return
178
179 66 continue
180 print "(a)",' ERROR during write '
181 close(iunit)
182 return
183
184 end subroutine write_pixmap_ascii
185
186 !-----------------------------------------------------------------
187 ! output pixmap as a raw .ppm file
188 !-----------------------------------------------------------------
189 subroutine write_pixmap_ppm(datpix,npixx,npixy,xmin,ymin,dx,datmin,datmax,label,istep,brightness)
190 use colours, only:rgbtable,ncolours
191 integer, intent(in) :: npixx,npixy
192 real, intent(in), dimension(npixx,npixy) :: datpix
193 real, intent(in), dimension(npixx,npixy), optional :: brightness
194 real, intent(in) :: xmin,ymin,dx,datmin,datmax
195 character(len=*), intent(in) :: label
196 integer, intent(in) :: istep
197 character(len=120) :: filename
198 real, dimension(3) :: rgbi,drgb
199 real :: dati,ddatrange,datfraci,ftable
200 integer :: ipix,jpix,ir,ib,ig,ierr,maxcolour,indexi
201 integer, parameter :: iunit = 167
202 !
203 !--check for errors
204 !
205 if (abs(datmax-datmin) > tiny(datmin)) then
206 ddatrange = 1./abs(datmax-datmin)
207 else
208 print "(a)",'error: datmin=datmax : pointless writing ppm file'
209 return
210 endif
211 !
212 !--write PPM--
213 !
214 write(filename,"(a,i5.5,a)") trim(fileprefix)//'_',istep,'.ppm'
215 open(unit=iunit,file=filename,status='replace',form='formatted',iostat=ierr)
216 if (ierr /=0) then
217 print*,'error opening ppm file'
218 return
219 endif
220 write(*,"(a)",ADVANCE='NO') '> writing pixel map to file '//trim(filename)//' ...'
221 !
222 !--PPM header
223 !
224 maxcolour = 255
225 write(iunit,"(a)",err=66) 'P3'
226 write(iunit,"(a)",err=66) '# '//trim(adjustl(filename))//' created by '//trim(tagline)
227 write(iunit,"(a,1pe14.6,a,1pe14.6)",err=66) '# '//trim(label)//': min = ',datmin,' max = ',datmax
228 write(iunit,"(a,1pe14.6,a,1pe14.6)",err=66) '# x axis: min = ',xmin,' max = ',xmin+(npixx-1)*dx
229 write(iunit,"(a,1pe14.6,a,1pe14.6)",err=66) '# y axis: min = ',ymin,' max = ',ymin+(npixy-1)*dx
230 write(iunit,"(i4,1x,i4)",err=66) npixx, npixy
231 write(iunit,"(i3)",err=66) maxcolour
232 !--pixel information
233 do jpix = npixy,1,-1
234 do ipix = 1,npixx
235 dati = datpix(ipix,jpix)
236 datfraci = (dati - datmin)*ddatrange
237 datfraci = max(datfraci,0.)
238 datfraci = min(datfraci,1.)
239 !--define colour for current particle
240 ftable = datfraci*ncolours
241 indexi = int(ftable) + 1
242 indexi = min(indexi,ncolours)
243 if (indexi < ncolours) then
244 !--do linear interpolation from colour table
245 drgb(:) = rgbtable(:,indexi+1) - rgbtable(:,indexi)
246 rgbi(:) = rgbtable(:,indexi) + (ftable - int(ftable))*drgb(:)
247 else
248 rgbi(:) = rgbtable(:,indexi)
249 endif
250 if (present(brightness)) then
251 rgbi(:) = rgbi(:)*min(brightness(ipix,jpix),1.0)
252 endif
253 ir = max(min(int(rgbi(1)*maxcolour),maxcolour),0)
254 ig = max(min(int(rgbi(2)*maxcolour),maxcolour),0)
255 ib = max(min(int(rgbi(3)*maxcolour),maxcolour),0)
256 write(iunit,"(i3,1x,i3,1x,i3,2x)",err=66) ir,ig,ib
257 enddo
258 enddo
259 close(unit=iunit)
260 print "(a)",'OK'
261 return
262
263 66 continue
264 print "(a)",' ERROR during write '
265 close(iunit)
266 return
267 end subroutine write_pixmap_ppm
268
269
270 !-----------------------------------------------------------------
271 ! read in pixels from file
272 !-----------------------------------------------------------------
273 subroutine readpixmap(datpix,npixx,npixy,dumpfile,label,istep,xsec,ierr)
274 use asciiutils, only:get_nheaderlines
275 real, intent(out), dimension(:,:), allocatable :: datpix
276 integer, intent(out) :: npixx,npixy,ierr
277 character(len=*), intent(in) :: dumpfile
278 character(len=*), intent(in) :: label
279 integer, intent(in) :: istep
280 logical, intent(in) :: xsec
281 integer :: i,j,nheader,nerr
282 integer, parameter :: iunit = 168
283 character(len=128) :: filename
284 character(len=2) :: char
285 logical :: iexist
286
287 ierr = 0
288 select case(trim(adjustl(readpixformat)))
289 case('ascii') ! splash pixmap output files
290 call check_for_pixmap_files(filename,dumpfile,label,'.pix',istep,xsec,iexist)
291 if (.not.iexist) then
292 print "('*',a,'*',/,72('*'))",' Create a file with one of these names (or a soft link) and try again '
293 ierr = 1
294 return
295 endif
296
297 open(unit=iunit,file=filename,status='old',form='formatted',iostat=ierr)
298 if (ierr /=0) then
299 print*,'error opening '//trim(filename)
300 return
301 else
302 npixx = 0
303 npixy = 0
304 nheader = get_nheaderlines(iunit)
305 rewind(iunit)
306 do i=1,nheader-1
307 read(iunit,*,iostat=ierr)
308 enddo
309 read(iunit,*,iostat=ierr) char,npixx,npixy
310 if (ierr /= 0 .or. npixx <= 0 .or. npixy <= 0) then
311 print*,'ERROR reading size of pixel map, got nx = ',npixx,' ny = ',npixy,&
312 ', skipped ',nheader,' header lines'
313 else
314 print "(a,i5,a,i5,a)",' reading',npixx,' x',npixy,' pixel map from '//trim(filename)
315 endif
316 allocate(datpix(npixx,npixy),stat=ierr)
317 if (ierr /= 0) then
318 print "(a)",' ERROR allocating memory for pixel map'
319 close(iunit)
320 return
321 endif
322 nerr = 0
323 do j=1,npixy
324 read(iunit,*,iostat=ierr) datpix(1:npixx,j)
325 if (ierr /= 0) nerr = nerr + 1
326 enddo
327 if (nerr /= 0) print "(a,i3,a,i3)",' WARNING: ',nerr,' errors reading pixel map from '//trim(filename)//' on unit ',iunit
328
329 close(iunit)
330 endif
331
332 case('ftn','ftn512','chf') ! Christoph Federrath files
333 npixx = 512
334 npixy = 512
335 !
336 !--cycle through possible filenames
337 !
338 call check_for_pixmap_files(filename,dumpfile,label,'.pix',istep,xsec,iexist)
339 if (.not.iexist) then
340 print "('*',a,'*',/,72('*'))",' Create a file with one of these names (or a soft link) and try again '
341 ierr = 1
342 return
343 endif
344
345 open(unit=iunit,file=filename,status='old',form='unformatted',iostat=ierr)
346 if (ierr /= 0) then
347 print "(a)",' ERROR: cannot open '//trim(filename)
348 ierr = 2
349 return
350 else
351 print "(a)",' reading pixel map from '//trim(filename)
352 allocate(datpix(npixx,npixy),stat=ierr)
353 read(iunit,iostat=ierr) datpix
354 if (ierr /= 0) print "(a,i3)",' WARNING: ERRORS reading pixel map from '//trim(filename)//' on unit ',iunit
355 close(iunit)
356 endif
357 case default
358 if (len_trim(readpixformat) <= 0) then
359 print "(a)",' ERROR: pixel format not set prior to read_pixmap call'
360 else
361 print "(a)",' ERROR: unknown pixmap format '//trim(adjustl(readpixformat))
362 endif
363 ierr = 1
364 end select
365
366 end subroutine readpixmap
367
368 !-----------------------------------------------------------------
369 ! look for pixel map files matching a variety of naming schemes
370 !-----------------------------------------------------------------
371 subroutine get_pixmap_filename(filename,dumpfile,label,ext,xsec)
372 use asciiutils, only:basename,safename
373 character(len=*), intent(out) :: filename
374 character(len=*), intent(in) :: dumpfile,label,ext
375 logical, intent(in) :: xsec
376
377 if (xsec) then
378 filename = trim(basename(dumpfile))//'_'//trim(safename(label))//'_slice'//trim(ext)
379 else
380 filename = trim(basename(dumpfile))//'_'//trim(safename(label))//'_proj'//trim(ext)
381 endif
382
383 end subroutine get_pixmap_filename
384
385 !-----------------------------------------------------------------
386 ! look for pixel map files matching a variety of naming schemes
387 !-----------------------------------------------------------------
388 subroutine check_for_pixmap_files(filename,dumpfile,label,ext,istep,xsec,iexist)
389 use asciiutils, only:safename,basename
390 character(len=*), intent(out) :: filename
391 character(len=*), intent(in) :: dumpfile,label,ext
392 integer, intent(in) :: istep
393 logical, intent(in) :: xsec
394 logical, intent(out) :: iexist
395 character(len=len(dumpfile)) :: dumpfilei
396 integer :: i,maxnames
397 logical :: printinfo
398
399 dumpfilei = dumpfile
400 iexist = .false.
401 i = 0
402 maxnames = 5
403 printinfo = .false.
404
405 do while (.not.iexist .and. i < maxnames)
406 i = i + 1
407 select case(i)
408 case(1)
409 if (xsec) then
410 filename = trim(dumpfilei)//'_'//trim(safename(label))//'_slice'//trim(ext)
411 else
412 filename = trim(dumpfilei)//'_'//trim(safename(label))//'_proj'//trim(ext)
413 endif
414 case(2)
415 filename = trim(dumpfilei)//'_'//trim(safename(label))//trim(ext)
416 case(3)
417 if (xsec) then
418 filename = trim(dumpfilei)//'_slice'//trim(ext)
419 else
420 filename = trim(dumpfilei)//'_proj'//trim(ext)
421 endif
422 case(4)
423 filename = trim(dumpfilei)//trim(ext)
424 case(5)
425 write(filename,"(a,i5.5,a)") trim(fileprefix)//'_',istep,trim(ext)
426 end select
427 !
428 !--query to see if file exists
429 !
430 if (printinfo) then
431 print "('*',a,'*')",' no file '//filename(1:60)//''
432 else
433 inquire(file=filename,exist=iexist)
434 endif
435 if (.not.iexist) then
436 !
437 !--try the same files again but in the current directory
438 ! instead of the directory in which the dump files are located
439 !
440 if (i==maxnames) then
441 if (len_trim(dumpfilei) /= len_trim(basename(dumpfile))) then
442 i = 0
443 dumpfilei = basename(dumpfile)
444 elseif (.not.printinfo) then
445 print "(72('*'),/,'*',a,12x,'*')", &
446 ' ERROR: could not find any '//trim(ext)//' files with matching names:'
447 dumpfilei = dumpfile
448 printinfo = .true.
449 i = 0
450 endif
451 endif
452 endif
453 enddo
454
455 end subroutine check_for_pixmap_files
456
457 end module write_pixmap