Debian Codegraph @

src/read_data_ascii.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-2023 Daniel Price. All rights reserved.
19 ! Contact: [email protected]
20 !
21 !-----------------------------------------------------------------
22
23 !-------------------------------------------------------------------------
24 ! this subroutine reads from the data file(s)
25 ! change this to change the format of data input
26 !
27 ! THIS VERSION IS FOR GENERAL ASCII DATA FORMATS
28 !
29 ! SOME CHOICES FOR THIS FORMAT CAN BE SET USING THE FOLLOWING COMMAND LINE FLAGS:
30 !
31 ! --columnsfile gives the location of the default 'columns' file
32 ! (overridden by the presence of a `columns' file in the working directory)
33 !
34 ! --ncolumns can be used to override the automatic ncolumns choice
35 !
36 ! e.g. --ncolumns=10
37 !
38 ! --nheaderlines can be used to override the automatic number of header line determination
39 !
40 ! e.g. --nheaderlines=1
41 !
42 ! --time=1.0 can be used to set the time (fixed for all files)
43 ! --gamma=1.6 can be used to set gamma (fixed for all files)
44 ! --timeheader=3 can be used to set the header line where the time is listed
45 ! --gammaheader=5 can be used to set the header line where gamma is listed
46 !
47 ! the data is stored in the global array dat
48 !
49 ! >> this subroutine must return values for the following: <<
50 !
51 ! ncolumns : number of data columns
52 ! ndim, ndimV : number of spatial, velocity dimensions
53 ! nstepsread : number of steps read from this file
54 !
55 ! dat(maxplot,maxpart,maxstep) : main data array
56 !
57 ! npartoftype(1:6,maxstep) : number of particles of each type in each timestep
58 ! ntot(maxstep) : total number of particles in each timestep
59 !
60 ! time(maxstep) : time at each step
61 ! gamma(maxstep) : gamma at each step
62 !
63 ! most of these values are stored in global arrays
64 ! in the module 'particle_data'
65 !-------------------------------------------------------------------------
66 module asciiread
67 use labels, only:lenlabel,label
68 integer :: icoltype
69 character(len=lenlabel), dimension(size(label)) :: label_orig
70
71 end module asciiread
72
73
74 module readdata_ascii
75 implicit none
76
77 public :: read_data_ascii, set_labels_ascii
78
79 private
80 contains
81
82 subroutine read_data_ascii(rootname,indexstart,ipos,nstepsread)
83 use particle_data, only:dat,npartoftype,time,gamma,maxpart,maxcol,maxstep,iamtype
84 use params
85 use settings_data, only:ndim,ndimV,ncolumns,ncalc,iverbose,ntypes,debugmode
86 use mem_allocation, only:alloc
87 use asciiutils, only:get_ncolumns,read_column_labels,isdigit,readline_csv
88 use system_utils, only:ienvironment,renvironment
89 use asciiread, only:icoltype,label_orig
90 use labels, only:labeltype,print_types
91 use, intrinsic :: ieee_arithmetic
92 integer, intent(in) :: indexstart,ipos
93 integer, intent(out) :: nstepsread
94 character(len=*), intent(in) :: rootname
95 integer :: i,j,ierr,iunit,ncolstep,ncolenv,nerr,iheader_time,iheader_gamma
96 integer :: nprint,npart_max,nstep_max,nheaderlines,nheaderenv,itype,nlabels
97 integer :: noftype(maxparttypes),iverbose_was
98 logical :: iexist,timeset,gammaset,csv
99 real :: dummyreal
100 real, allocatable :: dattemp(:)
101 character(len=len(rootname)+4) :: dumpfile
102 character(len=4096) :: line
103 character(len=10) :: str,strc
104 integer, parameter :: notset = -66
105
106 nstepsread = 0
107 nstep_max = 0
108 npart_max = maxpart
109 iunit = 15 ! logical unit number for input
110
111 dumpfile = trim(rootname)
112
113 if (iverbose > 1) print "(1x,a)",'reading ascii format'
114 print "(26('>'),1x,a,1x,26('<'))",trim(dumpfile)
115 !
116 !--check if first data file exists
117 !
118 inquire(file=dumpfile,exist=iexist)
119 if (.not.iexist) then
120 print "(a)",' *** error: '//trim(dumpfile)//': file not found ***'
121 return
122 endif
123 !
124 !--fix number of spatial dimensions (0 means no particle coords)
125 !
126 ndim = 0
127 ndimV = 0
128
129 j = indexstart
130 nstepsread = 0
131 icoltype = 0 ! no particle type defined by default
132 csv = index(dumpfile,'.csv') > 0 ! if filename contains .csv
133 !
134 !--open the file and read the number of particles
135 !
136 open(unit=iunit,iostat=ierr,file=dumpfile,status='old',form='formatted')
137 if (ierr /= 0) then
138 print "(a)",'*** ERROR OPENING '//trim(dumpfile)//' ***'
139 return
140 else
141 call get_ncolumns(iunit,ncolstep,nheaderlines,csv=csv)
142 !--override header lines setting
143 nheaderenv = ienvironment('ASPLASH_NHEADERLINES',-1)
144 if (nheaderenv >= 0) then
145 if (iverbose > 0) print*,' setting nheader lines = ',nheaderenv,' from --nheaderlines flag'
146 nheaderlines = nheaderenv
147 endif
148 !--override columns setting with environment variable
149 ncolenv = ienvironment('ASPLASH_NCOLUMNS',-1)
150 if (ncolenv > 0) then
151 if (iverbose > 0) print "(a,i3,a)",' setting ncolumns = ',ncolenv,' from --ncolumns flag'
152 ncolstep = ncolenv
153 endif
154 if (ncolstep <= 1) then
155 !print "(a)",'*** ERROR: could not determine number of columns in file ***'
156 print "(/,a)",' Are you trying to read a non-ascii file? If so, use:'
157 print "(' ',/,a,/,' ')",' splash -f <format> '//trim(dumpfile)
158 print "(a)",' Use splash --formats for list of supported data formats '
159 return
160 endif
161
162 !--search through header for column labels
163 if (ncolstep > 1) call read_column_labels(iunit,nheaderlines,ncolstep,nlabels,label_orig,csv,debugmode)
164 rewind(iunit)
165
166 iverbose_was = iverbose
167 iverbose = 0
168 ncolumns = ncolstep
169 call set_labels_ascii() ! to see if types are defined
170 iverbose = iverbose_was
171
172 !
173 !--allocate memory initially
174 !
175 nprint = 101
176 nstep_max = max(nstep_max,indexstart,1)
177 if (.not.allocated(dat) .or. (nprint > npart_max) .or. (ncolstep+ncalc) > maxcol) then
178 npart_max = max(npart_max,INT(1.1*(nprint)))
179 call alloc(npart_max,nstep_max,ncolstep+ncalc,mixedtypes=(icoltype > 0))
180 endif
181 endif
182
183 npart_max = max(npart_max,nprint)
184 !
185 !--allocate/reallocate memory if j > maxstep
186 !
187 if (j > maxstep) then
188 call alloc(maxpart,j+1,maxcol,mixedtypes=(icoltype > 0))
189 endif
190 !
191 !--can set either set the time and gamma explicitly
192 ! using environment variables (fixed for all files)
193 ! or can specify on which header line the time appears
194 !
195 timeset = .false.
196 gammaset = .false.
197 dummyreal = renvironment('ASPLASH_TIME',errval=-1.)
198 if (dummyreal > 0.) then
199 time(j) = dummyreal
200 timeset = .true.
201 endif
202 dummyreal = renvironment('ASPLASH_GAMMA',errval=-1.)
203 if (dummyreal > 0.) then
204 gamma(j) = dummyreal
205 gammaset = .true.
206 endif
207 iheader_time = ienvironment('ASPLASH_TIMEHEADER',errval=notset)
208 iheader_gamma = ienvironment('ASPLASH_GAMMAHEADER',errval=notset)
209 !
210 !--read header lines, try to use it to set time
211 !
212 !if (nheaderlines > 0 .and. iverbose > 0) print*,'skipping ',nheaderlines,' header lines'
213 do i=1,nheaderlines
214 !--read header lines as character strings
215 ! so that blank lines are counted in nheaderlines
216 read(iunit,"(a)",iostat=ierr) line
217 if (line(1:1)=='#') then
218 read(line(2:),*,iostat=ierr) dummyreal
219 else
220 read(line,*,iostat=ierr) dummyreal
221 endif
222 if (i==iheader_time .and. .not.timeset) then
223 if (ierr==0) then
224 time(j) = dummyreal
225 timeset = .true.
226 print*,'setting time = ',dummyreal,' from header line ',i
227 print*,'(determined from --timeheader setting)'
228 else
229 print "(a,i2,a)",' ** ERROR reading time from header line ',i, &
230 ' (using --timeheader)'
231 endif
232 elseif (i==iheader_gamma .and. .not.gammaset) then
233 if (ierr==0) then
234 gamma(j) = dummyreal
235 gammaset = .true.
236 print*,'setting gamma = ',dummyreal,' from header line ',i
237 print*,'(determined from --gammaheader setting)'
238 else
239 print "(a,i2,a)",' ** ERROR reading gamma from header line ',i, &
240 ' (using --gammaheader)'
241 endif
242 elseif (timeset .and. .not.gammaset .and. ierr==0 .and. iheader_gamma==notset &
243 .and. dummyreal > 1.0 .and. dummyreal < 2.000001) then
244 print*,'setting gamma = ',dummyreal,' from header line ',i
245 gamma(j) = dummyreal
246 gammaset = .true.
247 elseif (ierr==0 .and. .not. timeset .and. iheader_time==notset .and. index(line,'.') /= 0) then
248 time(j) = dummyreal
249 timeset = .true.
250 print*,'setting time = ',dummyreal,' from header line ',i
251 endif
252 enddo
253 !
254 ! allocate temporary array to read each line
255 !
256 allocate(dattemp(ncolstep))
257 !
258 !--now read the timestep data in the dumpfile
259 !
260 i = 0
261 ierr = 0
262 nerr = 0
263 noftype(:) = 0
264 ntypes = 1
265 overparts: do while (ierr >= 0)
266 i = i + 1
267 if (i > npart_max) then ! reallocate memory if necessary
268 npart_max = 10*npart_max
269 call alloc(npart_max,nstep_max,ncolstep+ncalc,mixedtypes=(icoltype > 0))
270 endif
271 dattemp(1:ncolstep) = ieee_value(1., ieee_quiet_nan) ! NaN if not read
272 if (csv) then
273 read(iunit,"(a)",iostat=ierr) line
274 call readline_csv(line,ncolstep,dattemp)
275 else
276 read(iunit,*,iostat=ierr) dattemp(1:ncolstep)
277 endif
278 !print*,ncolstep,nheaderlines,'line ',i,' got',dattemp(1:10)
279 !read*
280 dat(i,1:ncolstep,j) = dattemp(1:ncolstep)
281 if (icoltype > 0 .and. icoltype <= ncolstep .and. ierr==0 .and. (size(iamtype(:,j)) > 1)) then
282 !--set particle type from type column
283 itype = nint(dat(i,icoltype,j))
284 if (itype > 0 .and. itype < maxparttypes) then
285 iamtype(i,j) = int(itype,kind=1)
286 else
287 iamtype(i,j) = 1
288 endif
289 itype = iamtype(i,j)
290 noftype(itype) = noftype(itype) + 1
291 ntypes = max(itype,ntypes)
292 else
293 iamtype(:,:) = 1
294 endif
295 if (ierr > 0) then
296 nerr = nerr + 1
297 if (nerr <= 10) print "(a,i8,a)",' ERROR reading data from line ',i+nheaderlines,', skipping'
298 i = i - 1 ! ignore lines with errors
299 endif
300 enddo overparts
301 if (allocated(dattemp)) deallocate(dattemp)
302
303 nprint = i - 1
304 nstepsread = nstepsread + 1
305
306 if (nerr > 10) then
307 print "(a,i8,a)",' *** WARNING: errors whilst reading file on ',nerr,' lines: skipped these ***'
308 endif
309 if (ierr < 0 .and. (icoltype <=0 .or. icoltype > ncolstep)) then
310 write(str,"(i10)") nprint
311 str = adjustl(str)
312 write(strc,"(i10)") ncolstep
313 strc = adjustl(strc)
314 if (nheaderlines > 0 .and. iverbose > 0) then
315 if (nheaderlines > 10) then
316 print "(a,i3,a)",' npts = '//trim(str)//', ncols = '//trim(strc)//', skipped ',nheaderlines,' header lines'
317 else
318 print "(a,i1,a)",' npts = '//trim(str)//', ncols = '//trim(strc)//', skipped ',nheaderlines,' header lines'
319 endif
320 else
321 print "(a)",' npts = '//trim(str)//', ncols = '//trim(strc)
322 endif
323 endif
324
325 npartoftype(:,j) = 0
326 if (icoltype > 0 .and. icoltype <= ncolstep) then
327 npartoftype(1:ntypes,j) = noftype(1:ntypes)
328 call print_types(npartoftype(:,j),labeltype)
329 else
330 npartoftype(1,j) = nprint
331 endif
332
333 close(iunit)
334
335 end subroutine read_data_ascii
336
337 !-------------------------------------------------------------------
338 ! set labels for each column of data
339 !
340 ! read these from a file called 'columns' in the current directory
341 ! then take sensible guesses as to which quantities are which
342 ! from the column labels
343 !
344 !-------------------------------------------------------------------
345 subroutine set_labels_ascii
346 use asciiutils, only:lcase,match_taglist,find_repeated_tags,add_escape_chars
347 use labels, only:label,labeltype,ix,irho,ipmass,ih,iutherm, &
348 ipr,ivx,iBfirst,iamvec,labelvec,lenlabel, &
349 make_vector_label
350 use settings_data, only:ncolumns,ndim,ndimV,UseTypeInRenderings,iverbose
351 use geometry, only:labelcoord
352 use system_utils, only:get_environment_or_flag
353 use filenames, only:fileprefix
354 use asciiread, only:icoltype,label_orig
355 integer :: i,ierr,ndimVtemp
356 character(len=120) :: columnfile
357 character(len=lenlabel) :: labeli
358 logical :: iexist,got_time
359 !
360 !--read column labels from the columns file if it exists
361 !
362 ! first look for a columns file in the current directory
363 ! either called splash.columns or just 'columns'
364 !
365 columnfile=trim(fileprefix)//'.columns'
366 inquire(file=trim(columnfile),exist=iexist)
367 if (.not.iexist) then
368 columnfile='columns'
369 inquire(file=trim(columnfile),exist=iexist)
370 endif
371 !
372 ! if it does not exist see if the environment variable is set
373 ! and the corresponding file exists
374 !
375 if (.not.iexist) then
376 call get_environment_or_flag('ASPLASH_COLUMNSFILE',columnfile)
377 if (len_trim(columnfile) > 0) then
378 inquire(file=trim(columnfile),exist=iexist)
379 if (iexist) then
380 if (iverbose > 0) print "(a)",' using --columnsfile='//trim(columnfile)
381 else
382 print "(a)",' ERROR: --columnsfile='//trim(columnfile)//' DOES NOT EXIST'
383 columnfile = 'columns'
384 endif
385 else
386 columnfile = 'columns'
387 endif
388 endif
389
390 open(unit=51,file=trim(columnfile),status='old',iostat=ierr)
391 if (ierr /=0) then
392 ! print*,'HERE ',label(1)
393 if (iverbose > 0 .and. len_trim(label_orig(1))==0) then
394 print "(3(/,a))",' WARNING: column labels not found in file header:',&
395 ' To change the labels, create a file called ''columns'' ',&
396 ' in the current directory with one label per line'
397 endif
398 else
399 overcols: do i=1,ncolumns
400 read(51,"(a)",iostat=ierr) label_orig(i)
401 if (ierr < 0) then
402 if (iverbose > 0) print "(a,i3)",' end of file in columns file: read to column ',i-1
403 exit overcols
404 elseif (ierr > 0) then
405 if (iverbose > 0) print "(a)",' *** error reading from columns file ***'
406 exit overcols
407 endif
408 enddo overcols
409 close(unit=51)
410 endif
411 !
412 !--re-copy the labels from their originals
413 ! this is to avoid trying to match tags on labels
414 ! which have already been modified by splash (e.g. vectors)
415 !
416 do i=1,ncolumns
417 if (len_trim(label_orig(i)) > 0) label(i) = trim(label_orig(i))
418 enddo
419 !
420 !--first, look for 'x','y','z' as consecutive labels
421 ! to determine the number of dimensions
422 !
423 call match_taglist((/'x','y','z'/),label(1:ncolumns),ix(1),ndim)
424 do i=2,ndim
425 ix(i) = ix(1)+i-1
426 enddo
427 call match_taglist((/'vx','vy','vz'/),lcase(label(1:ncolumns)),ivx,ndimV)
428 call match_taglist((/'bx','by','bz'/),lcase(label(1:ncolumns)),iBfirst,ndimVtemp)
429 if (ndimV==0 .and. ivx==0) call match_taglist((/'ux','uy','uz'/),lcase(label(1:ncolumns)),ivx,ndimV)
430 !
431 !--make labels safe for plotting
432 !
433 do i=1,ncolumns
434 if (len_trim(label_orig(i)) > 0) label(i) = trim(add_escape_chars(label_orig(i)))
435 enddo
436
437 got_time = .false.
438 do i=1,ncolumns
439 !
440 !--compare all strings in lower case, trimmed and with no preceding spaces
441 !
442 labeli = trim(adjustl(lcase(label(i))))
443 if (trim(labeli)=='t' .or. trim(labeli)=='time') got_time = .true.
444 !
445 !--guess positions of various quantities from the column labels
446 !
447 if (.not.got_time) then
448 if (ndim <= 0 .and. (trim(labeli)=='x' .or. trim(labeli)=='r' .or. labeli(1:3)=='rad')) then
449 ndim = 1
450 ix(1) = i
451 endif
452 if (ndim==1 .and. i==ix(1)+1 .and. (labeli(1:1)=='y' .or. labeli(1:1)=='z')) then
453 ndim = 2
454 ix(2) = i
455 endif
456 if (ndim==2 .and. i==ix(2)+1 .and. labeli(1:1)=='z') then
457 ndim = 3
458 ix(3) = i
459 endif
460 endif
461 if (labeli(1:3)=='den' .or. index(labeli,'rho') /= 0 .or. labeli(1:3)=='\gr' .or. &
462 (index(labeli,'density') /= 0 .and. irho==0)) then
463 irho = i
464 elseif (labeli(1:5)=='pmass' .or. labeli(1:13)=='particle mass' &
465 .or. trim(labeli)=='mass') then
466 ipmass = i
467 elseif (ipmass==0 .and. trim(labeli)=='m') then
468 ipmass = i
469 !--use first column labelled h as smoothing length
470 elseif (ih==0 .and. (labeli(1:1)=='h' &
471 .or. labeli(1:6)=='smooth')) then
472 ih = i
473 elseif (trim(labeli)=='u'.or.labeli(1:6)=='utherm' &
474 .or.(index(labeli,'internal energy') /= 0 .and. iutherm==0)) then
475 iutherm = i
476 elseif (labeli(1:2)=='pr' .or. trim(labeli)=='p' .or. &
477 (index(labeli,'pressure') /= 0 .and. ipr==0)) then
478 ipr = i
479 elseif (icoltype==0 .and. trim(labeli)=='particle type' .or. trim(labeli)=='itype' &
480 .or. trim(labeli)=='type' .or. trim(labeli)=='particle_type') then
481 icoltype = i
482 elseif (ivx==0 .and. ndim==1 .and. trim(labeli)=='v') then
483 ivx = i
484 endif
485 enddo
486 !
487 !--try to find vectors by looking for multiple entries starting with 'v'
488 !
489 if (ndim > 0 .and. ivx==0) call find_repeated_tags('v_',ncolumns,label,ivx,ndimV)
490 if (ndimVtemp > ndimV .and. iverbose > 0) &
491 print "(a)",' WARNING: possible confusion with vector dimensions'
492 !
493 !--ignore label identifications if no spatial coordinates found
494 !
495 if (ndim < 1) then
496 ndimV = 0
497 irho = 0
498 ipmass = 0
499 ih = 0
500 iutherm = 0
501 ipr = 0
502 ivx = 0
503 endif
504 if (iverbose > 0) then
505 if (ndim > 0) print "(a,i1,a,i2,a,i2)",' Assuming ',ndim,' dimensions, coords in cols ',ix(1),' to ',ix(ndim)
506 !if (ndimV > 0) print "(a,i1)",' Assuming vectors have dimension = ',ndimV
507 if (ndim > 0 .and. (irho > 0 .or. ipmass>0 .or. ih > 0)) write(*,"(a)",advance='no') ' Assuming'
508 if (irho > 0) write(*,"(a,i2)",advance='no') ' density in column ',irho
509 if (ipmass > 0) write(*,"(a,i2)",advance='no') ', mass in ',ipmass
510 if (ih > 0) write(*,"(a,i2)",advance='no') ', h in ',ih
511 if (iutherm > 0) write(*,"(/,a,i2)") ' Assuming thermal energy in ',iutherm
512 if (iutherm==0 .and. ipr > 0) write(*,"(/,a)",advance='no') ' Assuming'
513 if (ipr > 0) write(*,"(a,i2)",advance='no') ' pressure in column ',ipr
514 if (ivx > 0) then
515 if (ipr==0 .and. iutherm==0) write(*,*)
516 if (ndimV > 1) then
517 print "(a,i2,a,i2)",' Assuming velocity in cols ',ivx,' to ',ivx+ndimV-1
518 else
519 print "(a,i2)",' Assuming velocity in column ',ivx
520 endif
521 endif
522 if (icoltype > 0) then
523 if (ipr==0 .and. iutherm==0 .and. ivx==0) write(*,*)
524 write(*,"(a,i2)") ' Assuming particle type in column ',icoltype
525 endif
526 if ((irho > 0 .or. ih > 0 .or. ipmass > 0) &
527 .and. ipr==0 .and. iutherm==0 .and. ivx==0 .and. icoltype==0) write(*,*)
528
529 if (ndim > 0 .and. (irho==0 .or. ipmass==0 .or. ih==0)) then
530 print "(2(/,a))",' NOTE: Rendering disabled until density, h and mass columns known', &
531 ' (i.e. label relevant columns in file header or columns file)'
532 endif
533 endif
534
535 call make_vector_label('v',ivx,ndimV,iamvec,labelvec,label,labelcoord(:,1))
536 call make_vector_label('B',iBfirst,ndimV,iamvec,labelvec,label,labelcoord(:,1))
537 !
538 !--set labels for each particle type
539 !
540 labeltype(1) = ''
541 UseTypeInRenderings(1) = .true.
542
543 end subroutine set_labels_ascii
544 end module readdata_ascii