Debian Codegraph @

src/write_griddata.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 !-----------------------------------------------------------------
24 ! Module implementing "splash to grid" operation, writing
25 ! 3D gridded data in various output formats
26 !-----------------------------------------------------------------
27 module readwrite_griddata
28 implicit none
29 integer, parameter :: doub_prec = kind(0.d0)
30
31 public :: isgridformat,print_gridformats
32 public :: open_gridfile_w,open_gridfile_r
33 public :: write_grid,read_gridcolumn,write_gridlimits
34
35 !
36 !--generic interface for reading grid column data
37 ! into 1D and 3D arrays
38 !
39 interface read_gridcolumn
40 module procedure read_gridcolumn3D,read_gridcolumn1D
41 end interface read_gridcolumn
42
43 private
44
45 contains
46
47 !-----------------------------------------------------------------
48 ! utility to check if a format selection is valid
49 !-----------------------------------------------------------------
50 logical function isgridformat(string)
51 use asciiutils, only:lcase
52 character(len=*), intent(in) :: string
53
54 isgridformat = .false.
55 select case(trim(lcase(string)))
56 case('grid')
57 isgridformat = .true.
58 case('gridascii')
59 isgridformat = .true.
60 case('gridbinary','gridbin')
61 isgridformat = .true.
62 case('gridbytestream','gridstream','gridbinary2','gridbenoit')
63 isgridformat = .true.
64 case('gridascii2')
65 isgridformat = .true.
66 end select
67
68 end function isgridformat
69
70 !-----------------------------------------------------------------
71 ! print grid limits
72 !-----------------------------------------------------------------
73 subroutine write_gridlimits(ndim,xmin,xmax,labelx)
74 integer, intent(in) :: ndim
75 real, intent(in) :: xmin(ndim),xmax(ndim)
76 character(len=*), intent(in) :: labelx(ndim)
77 integer :: i
78
79 print "(a)",' grid dimensions (edit .limits file to change):'
80 do i=1,ndim
81 if (maxval(abs(xmax)) < 1.e7) then
82 print "(1x,a,': ',f14.6,' -> ',f14.6)",trim(labelx(i)),xmin(i),xmax(i)
83 else
84 print "(1x,a,': ',es14.6,' -> ',es14.6)",trim(labelx(i)),xmin(i),xmax(i)
85 endif
86 enddo
87
88 end subroutine write_gridlimits
89
90 !-----------------------------------------------------------------
91 ! print usage if format selection not valid
92 !-----------------------------------------------------------------
93 subroutine print_gridformats(string)
94 character(len=*), intent(in) :: string
95
96 if (trim(string)=='short') then
97 print "(/,a)",'Grid conversion mode: '
98 print "(a)",' splash to grid : interpolate to grid; type "splash to" for details'
99 else
100 print "(/,a)",' Grid conversion mode ("splash to X dumpfiles"): '
101 print "(a)",' splash to grid : interpolate basic SPH data (density, plus velocity if present in data)'
102 print "(a)",' to 2D or 3D grid, write grid data to file (using default output=ascii)'
103 print "(a)",' to gridascii : as above, grid data written in ascii format'
104 print "(a)",' to gridascii2 : grid data written in ascii format, all in one file'
105 print "(a)",' to gridbinary : as above, grid data in simple unformatted binary format:'
106 print "(a)",' write(unit) nx,ny,nz,ncolumns,time,xmin,xmax,ymin,ymax,zmin,zmax [ 4x4,7x8 bytes ]'
107 print "(a)",' write(unit) (((rho(i,j,k),i=1,nx),j=1,ny),k=1,nz) [ 8 bytes each ]'
108 print "(a)",' write(unit) (((vx(i,j,k), i=1,nx),j=1,ny),k=1,nz) [ 8 bytes each ]'
109 print "(a)",' write(unit) (((vy(i,j,k), i=1,nx),j=1,ny),k=1,nz) [ 8 bytes each ]'
110 print "(a)",' write(unit) (((...(i,j,k),i=1,nx),j=1,ny),k=1,nz) [ 8 bytes each ]'
111 print "(a)",' to gridstream : grid data in byte-stream binary format (e.g. for python):'
112 print "(a)",' nx,ny,nz,ncolumns,time,xmin,xmax,ymin,ymax,zmin,zmax,rho [ 4,4,4,4,8*7,8*nx*ny*nz ]'
113 print "(a)",' allto grid : as above, interpolating *all* columns to the grid (and output file)'
114 print "(a)",' allto gridascii : as above, with ascii output'
115 print "(a)",' allto gridbinary : as above, with binary output'
116 endif
117
118 return
119 end subroutine print_gridformats
120
121 !------------------------------------------------------
122 ! open grid file for (write) output, write header
123 !------------------------------------------------------
124 subroutine open_gridfile_w(iunit,filenamein,outformat,ndim,ncolumns,npixels,xmin,xmax,time,ierr)
125 use asciiutils, only:lcase
126 integer, intent(in) :: iunit
127 character(len=*), intent(in) :: filenamein,outformat
128 character(len=len(filenamein)+10) :: filename
129 integer, intent(in) :: ndim,ncolumns
130 integer, dimension(ndim), intent(in) :: npixels
131 real(doub_prec), intent(in) :: xmin(ndim),xmax(ndim)
132 real(doub_prec), intent(in) :: time
133 integer, intent(out) :: ierr
134 integer :: idim
135 !
136 !--Only have to do something here for formats
137 ! that have all columns in the same file
138 !
139 ierr = 0
140 select case(trim(lcase(outformat)))
141 case('gridascii','grid')
142 !
143 !--ascii output uses individual files
144 !
145 print "(/,a,i2)",'-----> WRITING TO ASCII OUTPUT FILES'
146
147 case('gridbinary','gridbin')
148 !
149 !--simple unformatted binary format
150 !
151 filename = trim(filenamein)//'.grid'
152 print "(/,a,i2,a)",'----> WRITING TO '//trim(filename)//' on unit ',iunit,' (unformatted binary)'
153 open(unit=iunit,file=trim(filename),form='unformatted',status='replace',iostat=ierr)
154 if (ierr /= 0) then
155 print "(a)",' ERROR opening '//trim(filename)//' for output'
156 return
157 endif
158
159 write(iunit,iostat=ierr) npixels(1:ndim),ncolumns,time,(xmin(idim),xmax(idim),idim=1,ndim)
160 if (ierr /= 0) then
161 print "(a)",' ERROR writing header to file'
162 return
163 endif
164
165 case('gridbytestream','gridstream','gridbinary2','gridbenoit')
166 !
167 !--byte stream binary format
168 !
169 filename = trim(filenamein)//'.gridstream'
170 print "(/,a,i2,a)",'----> WRITING TO '//trim(filename)//' on unit ',iunit,' (unformatted binary stream)'
171 open(unit=iunit,file=trim(filename),form='unformatted',status='replace',access='stream',iostat=ierr)
172 if (ierr /= 0) then
173 print "(a)",' ERROR opening '//trim(filename)//' for output'
174 return
175 endif
176
177 write(iunit,iostat=ierr) npixels(1:ndim),ncolumns,time,(xmin(idim),xmax(idim),idim=1,ndim)
178 if (ierr /= 0) then
179 print "(a)",' ERROR writing header to file'
180 return
181 endif
182
183 case('gridascii2')
184
185 print "(/,a,i2)",'-----> WRITING TO ASCII OUTPUT FILES (WITH X, Y, Z, COL)'
186
187 case('hdf5')
188
189 case default
190 ! return error if bad format
191 print "(a)",' ERROR: unknown output format '''//trim(outformat)//''' in open_gridfile'
192 ierr = 1
193 return
194 end select
195
196 end subroutine open_gridfile_w
197
198
199 !------------------------------------------------------
200 ! open grid file for reading, read header
201 !------------------------------------------------------
202 subroutine open_gridfile_r(iunit,filename,informat,ndim,ncolumns,npixels,time,ierr)
203 use asciiutils, only:lcase
204 integer, intent(in) :: iunit,ndim
205 character(len=*), intent(in) :: filename
206 character(len=*), intent(in) :: informat
207 integer, intent(out) :: ncolumns
208 integer, dimension(ndim), intent(out) :: npixels
209 real(doub_prec), intent(out) :: time
210 integer, intent(out) :: ierr
211 !
212 !--read only implemented for binary grid format at present
213 !
214 ierr = 0
215 select case(trim(lcase(informat)))
216 case('gridbinary','gridbin')
217 !
218 !--simple unformatted binary format
219 !
220 print "(/,a,i2,a)",'----> READING '//trim(filename)//' on unit ',iunit,' (unformatted binary)'
221 open(unit=iunit,file=trim(filename),form='unformatted',status='old',iostat=ierr)
222 if (ierr /= 0) then
223 print "(a)",' ERROR opening '//trim(filename)//' for read'
224 return
225 endif
226
227 read(iunit,iostat=ierr) npixels(1:ndim),ncolumns,time
228 if (ierr /= 0) then
229 print "(a)",' ERROR reading header'
230 return
231 endif
232
233 case('gridbytestream','gridstream','gridbinary2','gridbenoit')
234 print "(/,a,i2,a)",'----> READING '//trim(filename)//' on unit ',iunit,' (unformatted bytestream)'
235 open(unit=iunit,file=trim(filename),form='unformatted',status='old',access='stream',iostat=ierr)
236 if (ierr /= 0) then
237 print "(a)",' ERROR opening '//trim(filename)//' for reading'
238 return
239 endif
240
241 read(iunit,iostat=ierr) npixels(1:ndim),ncolumns,time
242 if (ierr /= 0) then
243 print "(a)",' ERROR reading header'
244 return
245 endif
246
247 case default
248 ! return error if bad format
249 print "(a)",' ERROR: cannot read grid format '''//trim(informat)//''' in open_gridfile_r'
250 ierr = 1
251 return
252 end select
253
254 end subroutine open_gridfile_r
255
256 !------------------------------------------------------
257 ! write a particular column to the grid output file
258 !------------------------------------------------------
259 subroutine write_grid(iunit,filenamein,outformat,ndim,ncolgrid,npixels,label,&
260 labelcoordsys,xlab,time,pixwidth,xmin,xmax,ierr,dat,dat3D,dat2D,label3D,tagline,origin)
261 use asciiutils, only:ucase,lcase,safename
262 integer, intent(in) :: iunit
263 character(len=*), intent(in) :: filenamein,outformat
264 integer, intent(in) :: ndim,ncolgrid
265 integer, dimension(ndim), intent(in) :: npixels
266 character(len=*), intent(in) :: label,labelcoordsys
267 character(len=*), dimension(3), intent(in) :: xlab
268 real(doub_prec), intent(in) :: time
269 real, dimension(3), intent(in) :: xmin,xmax,pixwidth
270 integer, intent(out) :: ierr
271 character(len=len(filenamein)+20) :: filename
272 real(doub_prec), dimension(:,:,:), intent(in), optional :: dat
273 real(doub_prec), dimension(:,:,:,:), intent(in), optional :: dat3D
274 real, dimension(:,:), intent(in), optional :: dat2D
275 character(len=*), intent(in), optional :: label3D(ncolgrid),tagline,origin
276 integer :: i,j,k,n,idim
277 real :: xi,yi,zi
278
279 ierr = 0
280 if (ndim==3 .and. .not.(present(dat3D) .or. present(dat))) then
281 print "(a)",' ERROR in call to write_grid: ndim=3 but 3D grid not passed'
282 ierr = 1
283 elseif (ndim==2 .and. .not.present(dat2D)) then
284 print "(a)",' ERROR in call to write_grid: ndim=2 but 2D grid not passed'
285 ierr = 1
286 elseif (.not.(ndim==2 .or. ndim==3)) then
287 print "(a,i2,a)",' ERROR in call to write_grid: cannot write grid for ',ndim,' dimensions'
288 ierr = 2
289 endif
290 if (ierr /= 0) return
291
292 select case(trim(lcase(outformat)))
293 case('gridascii','grid')
294 if (ncolgrid > 1) then
295 filename = trim(filenamein)//'_grid.dat'
296 else
297 filename = trim(filenamein)//'_'//trim(safename(label))//'_grid.dat'
298 endif
299 print "(/,a)",'-----> WRITING to '//trim(filename)
300
301 !
302 !--open ascii file
303 !
304 open(unit=iunit,file=trim(filename),form='formatted',status='replace',iostat=ierr)
305 if (ierr /= 0) then
306 print "(a)",' ERROR OPENING FILE FOR WRITING'
307 return
308 endif
309 if (present(tagline)) then
310 write(iunit,"(a)",err=100) '# '//trim(tagline)
311 else
312 write(iunit,"(a)",err=100) '# gridded data file'
313 endif
314 if (present(origin)) then
315 write(iunit,"(a)",err=100) &
316 '# '//trim(filename)//' produced using '//trim(origin)
317 else
318 write(iunit,"(a)",err=100) '# '//trim(filename)
319 endif
320 write(iunit,"(a)",err=100) '#'
321 write(iunit,"(a)",err=100) '# time:'
322 write(iunit,"(a,es15.7)",iostat=ierr) '# ',time
323 write(iunit,"(a)",err=100) '#'
324 write(iunit,"(a,5(a,','),a,':')",err=100) &
325 '# ',(trim(xlab(idim))//'min',trim(xlab(idim))//'max',idim=1,ndim)
326 write(iunit,"(a,6(es15.7,1x))",iostat=ierr) '# ',(xmin(idim),xmax(idim),idim=1,ndim)
327 write(iunit,"(a)",err=100) '#'
328 write(iunit,"(a)",err=100) '# file contains:'
329 write(iunit,"(a,i1,a)",err=100) '# '//trim(label)//' interpolated to ',ndim,'D '//trim(labelcoordsys)//' grid '
330 write(iunit,"(a)",err=100) '#'
331 write(iunit,"(a)",err=100) '# written in the form: '
332 if (ndim==3) then
333 write(iunit,"(a)",err=100) '# do k=1,n'//trim(xlab(3))
334 write(iunit,"(a)",err=100) '# do j=1,n'//trim(xlab(2))
335 write(iunit,"(a)",err=100) '# write(*,*) (dat(i,j,k),i=1,n'//trim(xlab(1))//')'
336 write(iunit,"(a)",err=100) '# enddo'
337 write(iunit,"(a)",err=100) '# enddo'
338 else
339 write(iunit,"(a)",err=100) '# do j=1,ny'
340 write(iunit,"(a)",err=100) '# write(*,*) (dat(i,j),i=1,nx)'
341 write(iunit,"(a)",err=100) '# enddo'
342 endif
343 write(iunit,"(a)",err=100) '#'
344 write(iunit,"(a)",err=100) '# grid dimensions:'
345 if (present(dat)) then
346 write(iunit,"(a,3(a,3x))",err=100) '# ',('n'//trim(xlab(i)),i=1,3)
347 write(iunit,*,err=100) npixels(1:ndim)
348 do k=1,npixels(3)
349 do j=1,npixels(2)
350 write(iunit,"(2048(es14.6,1x))",err=100) (dat(i,j,k),i=1,npixels(1))
351 enddo
352 enddo
353 elseif (present(dat3D)) then
354 write(iunit,"(a,3(a,3x))",err=100) '# ',('n'//trim(xlab(i)),i=1,3)
355 write(iunit,*,err=100) npixels(1:ndim)
356 do k=1,npixels(3)
357 do j=1,npixels(2)
358 write(iunit,"(2048(es14.6,1x))",err=100) (dat3D(1,i,j,k),i=1,npixels(1))
359 enddo
360 enddo
361 elseif (present(dat2D)) then
362 write(iunit,"(a)",err=100) '# nx ny'
363 write(iunit,*,err=100) npixels(1:ndim)
364 do j=1,npixels(2)
365 write(iunit,"(2048(es14.6,1x))",err=100) (dat2D(i,j),i=1,npixels(1))
366 enddo
367 endif
368 close(unit=iunit)
369 return
370
371 case('gridbinary','gridbin','gridbytestream','gridstream','gridbinary2','gridbenoit')
372 print "(a)",'-----> WRITING '//trim(ucase(label))
373 if (present(dat)) then
374 write(iunit,iostat=ierr) ((dat(1:npixels(1),j,k),j=1,npixels(2)),k=1,npixels(3))
375 elseif (present(dat3D)) then
376 write(iunit,iostat=ierr) ((((dat3D(n,i,j,k),i=1,npixels(1)),j=1,npixels(2)),k=1,npixels(3)),n=1,ncolgrid)
377 elseif (present(dat2D)) then
378 write(iunit,iostat=ierr) ((dat2D(i,j),i=1,npixels(1)),j=1,npixels(2))
379 endif
380 case('gridascii2')
381 if (ncolgrid > 1) then
382 filename = trim(filenamein)//'_grid.dat'
383 print "(a)",'-----> WRITING to '//trim(filename)
384 else
385 filename = trim(filenamein)//'_'//trim(safename(label))//'_grid.dat'
386 print "(a)",'-----> WRITING '//trim(ucase(label))//' to '//trim(filename)
387 endif
388
389 !
390 !--open ascii file
391 !
392 open(unit=iunit,file=trim(filename),form='formatted',status='replace',iostat=ierr)
393 if (ierr /= 0) then
394 print "(a)",' ERROR OPENING FILE FOR WRITING'
395 return
396 endif
397 write(iunit,"(a)",err=100) '# '//trim(tagline)
398 write(iunit,"(a)",err=100) &
399 '# '//trim(filename)//' produced using "splash to '//trim(outformat)// &
400 '" on file '//trim(filenamein)
401 write(iunit,"(a)",err=100) '#'
402 write(iunit,"(a)",err=100) '# time:'
403 write(iunit,"(a,es15.7)",iostat=ierr) '# ',time
404 write(iunit,"(a)",err=100) '#'
405 write(iunit,"(a)",err=100) '# grid dimensions:'
406 if (present(dat3D) .and. present(dat)) then
407 write(iunit,"(a,3(a,3x))",err=100) '# ',('n'//trim(xlab(i)),i=1,3)
408 write(iunit,"(a,3(i5,1x))",err=100) '# ',npixels(1:3)
409 write(iunit,"('#',64('[',a13,']'))",err=100) xlab,trim(label),(trim(label3D(n)),n=1,ncolgrid)
410 do k=1,npixels(3)
411 write(*,"('.')",ADVANCE='NO')
412 zi = xmin(3) + (k-0.5)*pixwidth(3)
413 do j=1,npixels(2)
414 yi = xmin(2) + (j-0.5)*pixwidth(2)
415 do i=1,npixels(1)
416 xi = xmin(1) + (i-0.5)*pixwidth(1)
417 write(iunit,"(64(es14.6,1x))") xi,yi,zi,dat(i,j,k),(dat3D(n,i,j,k),n=1,ncolgrid)
418 enddo
419 enddo
420 enddo
421 elseif (present(dat3D)) then
422 write(iunit,"(a,3(a,3x))",err=100) '# ',('n'//trim(xlab(i)),i=1,3)
423 write(iunit,"(a,3(i5,1x))",err=100) '# ',npixels(1:3)
424 write(iunit,"('#',64('[',a13,']'))",err=100) xlab,(trim(label3D(n)),n=1,ncolgrid)
425 do k=1,npixels(3)
426 write(*,"('.')",ADVANCE='NO')
427 zi = xmin(3) + (k-0.5)*pixwidth(3)
428 do j=1,npixels(2)
429 yi = xmin(2) + (j-0.5)*pixwidth(2)
430 do i=1,npixels(1)
431 xi = xmin(1) + (i-0.5)*pixwidth(1)
432 write(iunit,"(64(es14.6,1x))") xi,yi,zi,(dat3D(n,i,j,k),n=1,ncolgrid)
433 enddo
434 enddo
435 enddo
436 elseif (present(dat)) then
437 write(iunit,"(a,3(a,3x))",err=100) '# ',('n'//trim(xlab(i)),i=1,3)
438 write(iunit,"(a,3(i5,1x))",err=100) '# ',npixels(1:3)
439 write(iunit,"('#',4('[',a13,']'))",err=100) xlab,trim(label)
440 do k=1,npixels(3)
441 write(*,"('.')",ADVANCE='NO')
442 zi = xmin(3) + (k-0.5)*pixwidth(3)
443 do j=1,npixels(2)
444 yi = xmin(2) + (j-0.5)*pixwidth(2)
445 do i=1,npixels(1)
446 xi = xmin(1) + (i-0.5)*pixwidth(1)
447 write(iunit,"(64(es14.6,1x))") xi,yi,zi,dat(i,j,k)
448 enddo
449 enddo
450 enddo
451 elseif (present(dat2D)) then
452 write(iunit,"(a)",err=100) '# nx ny '
453 write(iunit,"(a,2(i5,1x))",err=100) '# ',npixels(1:2)
454 write(iunit,"('#',3('[',a13,']'))",err=100) 'x','y',trim(label)
455 do j=1,npixels(2)
456 write(*,"('.')",ADVANCE='NO')
457 yi = xmin(2) + (j-0.5)*pixwidth(2)
458 do i=1,npixels(1)
459 xi = xmin(1) + (i-0.5)*pixwidth(1)
460 write(iunit,"(3(es14.6,1x))") xi,yi,dat2D(i,j)
461 enddo
462 enddo
463 endif
464 write(*,*)
465
466 case('hdf5')
467
468 case default
469 print "(a)",' ERROR: unknown output format '''//trim(outformat)//''' in write_grid'
470 return
471 end select
472
473 return
474 !
475 !--error handling during write
476 !
477 100 continue
478 print "(a)",' ERROR writing grid file'
479 close(unit=iunit)
480 return
481
482 end subroutine write_grid
483
484 !------------------------------------------------------------------
485 ! read a particular column from the grid output file into 3D array
486 !------------------------------------------------------------------
487 subroutine read_gridcolumn3D(iunit,dat,npixels,ierr)
488 integer, intent(in) :: iunit
489 real(doub_prec), intent(out) :: dat(:,:,:)
490 integer, dimension(3), intent(in) :: npixels
491 integer, intent(out) :: ierr
492 integer :: i,j,k
493
494 print "(a,i4,'x',i4,'x',i4,a)",'-----> READING ',npixels(:),' data points'
495 read(iunit,iostat=ierr) (((dat(i,j,k),i=1,npixels(1)),j=1,npixels(2)),k=1,npixels(3))
496
497 end subroutine read_gridcolumn3D
498
499 !------------------------------------------------------------------
500 ! read a particular column from the grid output file into 1D array
501 !------------------------------------------------------------------
502 subroutine read_gridcolumn1D(iunit,dat,ngrid,ierr)
503 integer, intent(in) :: iunit
504 real(doub_prec), intent(out) :: dat(:)
505 integer, intent(in) :: ngrid
506 integer, intent(out) :: ierr
507 integer :: i
508
509 print "(a,i10,a)",'-----> READING ',ngrid,' data points'
510 read(iunit,iostat=ierr) (dat(i),i=1,ngrid)
511
512 end subroutine read_gridcolumn1D
513
514 end module readwrite_griddata