-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils_infiles.f90
1225 lines (1109 loc) · 37.7 KB
/
utils_infiles.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
!--------------------------------------------------------------------------!
! The Phantom Smoothed Particle Hydrodynamics code, by Daniel Price et al. !
! Copyright (c) 2007-2014 The Authors (see AUTHORS) !
! See LICENCE file for usage and distribution conditions !
! http://users.monash.edu.au/~dprice/phantom !
!--------------------------------------------------------------------------!
!+
! MODULE: infile_utils
!
! DESCRIPTION:
! This module contains utility routines for reading
! and writing of input files
!
! REFERENCES: None
!
! OWNER: Daniel Price
!
! $Id: f3e1229ba59986e2cab980a1abd897c8adcd8646 $
!
! RUNTIME PARAMETERS: None
!
! DEPENDENCIES: None
!+
!--------------------------------------------------------------------------
module infile_utils
implicit none
public :: write_inopt, read_inopt
public :: read_next_inopt, get_inopt
public :: write_infile_series, check_infile, contains_loop
!
! generic interface write_inopt to write an input option of any type
!
interface write_inopt
module procedure write_inopt_int,write_inopt_real4,write_inopt_real8,write_inopt_string,write_inopt_logical
end interface
!
! generic interface read_inopt to read an input option of any type
!
interface read_inopt
module procedure read_inopt_int,read_inopt_real,read_inopt_string,read_inopt_logical
end interface
!
! generic interface get_inopt to read a specific input variable
!
interface get_inopt
module procedure get_inopt_int,get_inopt_real,get_inopt_string,get_inopt_logical
end interface
!
! maximum length for input strings
! (if you change this, must also change format statements below)
!
integer, parameter, private :: maxlen = 20 ! max length of string containing variable
integer, parameter, private :: maxlenstring = 120 ! max length of string variable
integer, parameter, private :: maxlenline = 120 ! maximum line length
!
! structure for input options database
!
type inopts
character(len=maxlen) :: tag
character(len=maxlen) :: val
logical :: retrieved
end type
public :: inopts,open_db_from_file,close_db,errtext
integer, parameter, private :: &
ierr_notfound = -1, &
ierr_inread = 1, &
ierr_rangemin = 2, &
ierr_rangemax = 3
private
contains
!-----------------------------------------------------------------
!+
! write integer variable to input file
!+
!-----------------------------------------------------------------
subroutine write_inopt_int(ival,name,descript,iunit,ierr)
integer, intent(in) :: ival
character(len=*), intent(in) :: name,descript
integer, intent(in) :: iunit
integer, intent(out), optional :: ierr
integer :: ierror
write(iunit,"(a20,' = ',1x,i10,4x,'! ',a)",iostat=ierror) name,ival,descript
if (present(ierr)) ierr = ierror
end subroutine write_inopt_int
!-----------------------------------------------------------------
!+
! write logical variable to input file
!+
!-----------------------------------------------------------------
subroutine write_inopt_logical(lval,name,descript,iunit,ierr)
logical, intent(in) :: lval
character(len=*), intent(in) :: name,descript
integer, intent(in) :: iunit
integer, intent(out), optional :: ierr
integer :: ierror
write(iunit,"(a20,' = ',1x,l10,4x,'! ',a)",iostat=ierror) name,lval,descript
if (present(ierr)) ierr = ierror
end subroutine write_inopt_logical
!-----------------------------------------------------------------
!+
! write real variable to input file
!+
!-----------------------------------------------------------------
subroutine write_inopt_real4(rval,name,descript,iunit,exp,time,ierr)
real(kind=4), intent(in) :: rval
character(len=*), intent(in) :: name,descript
integer, intent(in) :: iunit
logical, intent(in), optional :: exp,time
integer, intent(out), optional :: ierr
logical :: doexp,dotime
integer :: nhr,nmin !,nsec
character(len=6) :: fmtstring
character(len=14) :: tmpstring
real(kind=4) :: trem
integer :: ierror
doexp = .false.
if (present(exp)) then
if (exp) doexp = .true.
endif
dotime = .false.
if (present(time)) then
if (time) dotime = .true.
endif
if (dotime) then
trem = rval
nhr = int(trem/3600.)
if (nhr > 0) trem = trem - nhr*3600._4
nmin = int(trem/60.)
if (nmin > 0) trem = trem - nmin*60._4
!nsec = int(trem)
write(iunit,"(a20,' = ',5x,i3.3,':',i2.2,4x,'! ',a)",iostat=ierror) &
name,nhr,nmin,descript
else
if (doexp .or. (abs(rval) < 1.e-4 .and. abs(rval) > tiny(rval)) &
.or. (abs(rval) >= 1.e4)) then
fmtstring = 'es10.3'
write(iunit,"(a20,' = ',1x,"//fmtstring//",4x,'! ',a)",iostat=ierror) name,rval,descript
else
if (abs(rval) < 1.e-1) then
write(tmpstring,"(f10.7)",iostat=ierror) rval
tmpstring = adjustl(strip_zeros(tmpstring,3))
elseif (abs(rval) >= 1.e1) then
write(tmpstring,"(g14.7)",iostat=ierror) rval
tmpstring = adjustl(strip_zeros(tmpstring,0))
else
write(tmpstring,"(g14.7)",iostat=ierror) rval
tmpstring = adjustl(strip_zeros(tmpstring,3))
endif
write(iunit,"(a20,' = ',1x,a10,4x,'! ',a)",iostat=ierror) name,adjustr(trim(tmpstring)),descript
endif
endif
if (present(ierr)) ierr = ierror
end subroutine write_inopt_real4
!-----------------------------------------------------------------
!+
! write real variable to input file
!+
!-----------------------------------------------------------------
subroutine write_inopt_real8(rval,name,descript,iunit,exp,time,ierr)
real(kind=8), intent(in) :: rval
character(len=*), intent(in) :: name,descript
integer, intent(in) :: iunit
logical, intent(in), optional :: exp,time
integer, intent(out), optional :: ierr
logical :: doexp,dotime
integer :: nhr,nmin !,nsec
character(len=16) :: tmpstring
real(kind=8) :: trem
integer :: ierror
doexp = .false.
if (present(exp)) then
if (exp) doexp = .true.
endif
dotime = .false.
if (present(time)) then
if (time) dotime = .true.
endif
if (dotime) then
trem = rval
nhr = int(trem/3600.d0)
if (nhr > 0) trem = trem - nhr*3600.d0
nmin = int(trem/60.d0)
if (nmin > 0) trem = trem - nmin*60.d0
!nsec = int(trem)
write(iunit,"(a20,' = ',5x,i3.3,':',i2.2,4x,'! ',a)",iostat=ierror) &
name,nhr,nmin,descript
else
if (doexp .or. (abs(rval) < 1.e-3 .and. abs(rval) > tiny(rval)) &
.or. (abs(rval) >= 1.e4)) then
write(iunit,"(a20,' = ',1x,es10.3,4x,'! ',a)",iostat=ierror) &
name,rval,descript
else
if (abs(rval) < 1.e-1) then
write(tmpstring,"(f16.13)",iostat=ierror) rval
tmpstring = adjustl(strip_zeros(tmpstring,3))
elseif (abs(rval) >= 1.e1) then
write(tmpstring,"(g16.9)",iostat=ierror) rval
tmpstring = adjustl(strip_zeros(tmpstring,0))
else
write(tmpstring,"(g16.9)",iostat=ierror) rval
tmpstring = adjustl(strip_zeros(tmpstring,3))
endif
if (len_trim(tmpstring) > 10) then
write(iunit,"(a20,' = ',1x,a,2x,'! ',a)",iostat=ierror) name,adjustr(trim(tmpstring)),descript
else
write(iunit,"(a20,' = ',1x,a10,4x,'! ',a)",iostat=ierror) name,adjustr(trim(tmpstring)),descript
endif
endif
endif
if (present(ierr)) ierr = ierror
end subroutine write_inopt_real8
!-----------------------------------------------------------------
!+
! internal function to strip zeros from the end of
! a number after the decimal place
!+
!-----------------------------------------------------------------
function strip_zeros(string,n)
character(len=*), intent(in) :: string
integer, intent(in) :: n ! keep at least this many zeros
character(len=len(string)) :: strip_zeros
integer :: idot,i
strip_zeros = string
idot = index(string,'.',back=.true.)
if (idot > 0) then ! allow max of nstrip zeros after decimal place
over_string: do i=len_trim(string),idot+n+1,-1
if (string(i:i)=='0') then
strip_zeros(i:i) = ' '
else
exit over_string
endif
enddo over_string
endif
!strip_zeros = adjustl(strip_zeros)
end function strip_zeros
!-----------------------------------------------------------------
!+
! write string to input file
!+
!-----------------------------------------------------------------
subroutine write_inopt_string(sval,name,descript,iunit,ierr)
character(len=*), intent(in) :: sval,name,descript
integer, intent(in) :: iunit
integer, intent(out), optional :: ierr
character(len=40) :: fmtstring
integer :: ierror
if (len_trim(sval) > 10) then
fmtstring = '(a20,'' = '',1x,a,3x,''! '',a)'
else
fmtstring = '(a20,'' = '',1x,a10,4x,''! '',a)'
endif
write(iunit,fmtstring,iostat=ierror) name,trim(sval),trim(descript)
if (present(ierr)) ierr = ierror
end subroutine write_inopt_string
!-----------------------------------------------------------------
!+
! open an input file, read options into a database and
! return the handle
!+
!-----------------------------------------------------------------
subroutine open_db_from_file(db,filename,iunit,ierr)
character(len=*), intent(in) :: filename
type(inopts), allocatable, intent(out) :: db(:)
integer, intent(out) :: ierr
integer, intent(in), optional :: iunit
character(len=maxlen) :: name,valstring
integer :: n,i
open(unit=iunit,file=trim(filename),status='old',form='formatted',iostat=ierr)
if (ierr /= 0) then
print "(a)",' ERROR opening '//trim(filename)
return
endif
!
! work out the number of valid entries in the input file
!
n = 0
do while(ierr==0)
call read_next_inopt(name,valstring,iunit,ierr)
if (ierr==0) n = n + 1
enddo
if (n > 0) then
!
! allocate memory for database
!
print "(a,i2,a)",' opening database from '//trim(filename)//' with ',n,' entries'
if (allocated(db)) deallocate(db)
allocate(db(n),stat=ierr)
if (ierr /= 0) return
!
! initialise all entries as blank
!
db(:)%tag = ' '
db(:)%val = ' '
db(:)%retrieved = .false.
!
! read file
!
rewind(iunit)
ierr = 0
do i=1,n
call read_next_inopt(db(i)%tag,db(i)%val,iunit,ierr)
if (ierr /= 0) then
print*,'INTERNAL ERROR: open_db_from_file: error: ierr should be zero here'
return
endif
enddo
else
print "(a)",' ERROR: no valid database entries in '//trim(filename)
ierr = -1
endif
close(unit=iunit)
end subroutine open_db_from_file
!-----------------------------------------------------------------
!+
! close a database (just deallocates memory)
!+
!-----------------------------------------------------------------
subroutine close_db(db)
type(inopts), allocatable, intent(inout) :: db(:)
if (allocated(db)) deallocate(db)
end subroutine close_db
!-----------------------------------------------------
!+
! translation of ierr value from read_inopt routines
! into human-readable text
!+
!-----------------------------------------------------
function errtext(ierr)
character(len=25) :: errtext
integer, intent(in) :: ierr
select case(ierr)
case(ierr_notfound)
errtext = 'not found'
case(ierr_rangemin)
errtext = 'too small'
case(ierr_rangemax)
errtext = 'too big'
case(0)
errtext = 'read OK'
case default
errtext = 'unknown error'
end select
end function errtext
!---------------------------------------------------------
!+
! print error message for errors during read_inopt calls
!+
!-=-------------------------------------------------------
subroutine print_error(tag,valstring,chmin,chmax,ierr)
character(len=*), intent(in) :: tag,valstring,chmin,chmax
integer, intent(in) :: ierr
if (ierr == ierr_rangemin .or. ierr == ierr_rangemax) then
print "(a,1x,a,' [',a,':',a,']')", &
' ERROR: '//trim(adjustl(tag))//' = '//trim(adjustl(valstring)), &
trim(errtext(ierr)),trim(adjustl(chmin)),trim(adjustl(chmax))
else
print "(a,1x,a)",' ERROR: '//trim(adjustl(tag))//' '//trim(errtext(ierr))
endif
end subroutine print_error
!-----------------------------------------------------------------
!+
! read an integer variable from an input options database
!+
!-----------------------------------------------------------------
subroutine read_inopt_int(ival,tag,db,err,errcount,min,max)
integer, intent(out) :: ival
character(len=*), intent(in) :: tag
type(inopts), allocatable, intent(inout) :: db(:)
integer, intent(out), optional :: err
integer, intent(inout), optional :: errcount
integer, intent(in), optional :: min,max
character(len=maxlen) :: valstring
character(len=16) :: chmin,chmax
integer :: ioerr,ierr
chmin = ''
chmax = ''
valstring = ''
ierr = 0
if (match_inopt_in_db(db,tag,valstring)) then
read(valstring,*,iostat=ioerr) ival
if (ioerr /= 0) ierr = ierr_inread
else
ierr = ierr_notfound
endif
if (ierr==0) then
if (present(min)) then
write(chmin,"(g10.0)") min
if (ival < min) ierr = ierr_rangemin
endif
if (present(max)) then
write(chmax,"(g10.0)") max
if (ival > max) ierr = ierr_rangemax
endif
endif
! print error message unless err argument is given
if (present(err)) then
err = ierr
elseif (ierr /= 0) then
call print_error(tag,valstring,chmin,chmax,ierr)
endif
if (present(errcount)) then
if (ierr /= 0) errcount = errcount + 1
endif
end subroutine read_inopt_int
!-----------------------------------------------------------------
!+
! read a real variable from an input options database
!+
!-----------------------------------------------------------------
subroutine read_inopt_real(val,tag,db,err,errcount,min,max)
real, intent(out) :: val
character(len=*), intent(in) :: tag
type(inopts), allocatable, intent(inout) :: db(:)
integer, intent(out), optional :: err
integer, intent(inout), optional :: errcount
real, intent(in), optional :: min,max
character(len=maxlen) :: valstring
character(len=16) :: chmin,chmax
integer :: ioerr,ierr
chmin = ''
chmax = ''
valstring = ''
ierr = 0
if (match_inopt_in_db(db,tag,valstring)) then
read(valstring,*,iostat=ioerr) val
if (ioerr /= 0) ierr = ierr_inread
else
ierr = ierr_notfound
endif
if (ierr==0) then
if (present(min)) then
write(chmin,"(g13.4)") min
if (val < min) ierr = ierr_rangemin
endif
if (present(max)) then
write(chmax,"(g13.4)") max
if (val > max) ierr = ierr_rangemax
endif
endif
if (present(err)) then
err = ierr
elseif (ierr /= 0) then
call print_error(tag,valstring,chmin,chmax,ierr)
endif
if (present(errcount)) then
if (ierr /= 0) errcount = errcount + 1
endif
end subroutine read_inopt_real
!-----------------------------------------------------------------
!+
! read a real variable from an input options database
!+
!-----------------------------------------------------------------
subroutine read_inopt_string(valstring,tag,db,err,errcount)
character(len=*), intent(out) :: valstring
character(len=*), intent(in) :: tag
type(inopts), allocatable, intent(inout) :: db(:)
integer, intent(out), optional :: err
integer, intent(inout), optional :: errcount
integer :: ierr
ierr = 0
if (.not.match_inopt_in_db(db,tag,valstring)) ierr = -1
if (present(err)) then
err = ierr
elseif (ierr /= 0) then
call print_error(tag,valstring,'','',ierr)
endif
if (present(errcount)) then
if (ierr /= 0) errcount = errcount + 1
endif
end subroutine read_inopt_string
!-----------------------------------------------------------------
!+
! read a logical variable from an input options database
!+
!-----------------------------------------------------------------
subroutine read_inopt_logical(lval,tag,db,err,errcount)
logical, intent(out) :: lval
character(len=*), intent(in) :: tag
type(inopts), allocatable, intent(inout) :: db(:)
integer, intent(out), optional :: err
integer, intent(inout), optional :: errcount
character(len=maxlen) :: valstring
integer :: ierr
ierr = 0
if (match_inopt_in_db(db,tag,valstring)) then
read(valstring,*,iostat=ierr) lval
else
ierr = -1
endif
if (present(err)) then
err = ierr
elseif (ierr /= 0) then
call print_error(tag,valstring,'','',ierr)
endif
if (present(errcount)) then
if (ierr /= 0) errcount = errcount + 1
endif
end subroutine read_inopt_logical
!-----------------------------------------------------------------
!+
! extract a matching entry from an input options database
! returns the string containing the value for the matching tag
!+
!-----------------------------------------------------------------
logical function match_inopt_in_db(db,tag,valstring)
type(inopts), allocatable, intent(inout) :: db(:)
character(len=*), intent(in) :: tag
character(len=*), intent(out) :: valstring
integer :: n
match_inopt_in_db = .false.
if (.not.allocated(db)) then
valstring = ' '
return
endif
do n=1,size(db)
if (trim(db(n)%tag)==trim(adjustl(tag))) then
match_inopt_in_db = .true.
valstring = db(n)%val
!--mark entry in database as retrieved
db(n)%retrieved = .true.
endif
enddo
end function match_inopt_in_db
!-----------------------------------------------------------------
!+
! read a line from the input file and extract the tag and the
! string section containing the data value
!+
!-----------------------------------------------------------------
subroutine read_next_inopt(tag,valstring,iunit,ierr,nlinesread)
character(len=maxlenline) :: line
character(len=*), intent(out) :: tag,valstring
integer, intent(in) :: iunit
integer, intent(out) :: ierr ! not optional for reads
integer, intent(out), optional :: nlinesread
integer :: ierrtemp
tag = ' '
valstring = ' '
line = ' '
if (present(nlinesread)) nlinesread = 0
do while((len_trim(line)==0 .or. line(1:1)=='#') .and. ierr==0)
if (present(nlinesread)) nlinesread = nlinesread + 1
read(iunit,"(a)",iostat=ierr,end=88) line
enddo
call read_inopt_from_line(line,tag,valstring,ierrtemp)
if (ierrtemp /= 0) ierr = ierrtemp
return
88 continue
if (present(nlinesread)) nlinesread = nlinesread - 1
return
end subroutine read_next_inopt
!-----------------------------------------------------------------
!+
! extract the tag and the string section containing the data value
! from the line read from the file
!+
!-----------------------------------------------------------------
subroutine read_inopt_from_line(line,name,valstring,ierr,comment)
character(len=*), intent(in) :: line
character(len=*), intent(out) :: name
character(len=*), intent(out) :: valstring
integer, intent(out) :: ierr
character(len=*), intent(out), optional :: comment
integer :: iequal, icomment,nhr,nmin,nsec
ierr = 0
name = ' '
valstring = ' '
!
!--find position of the equals sign
!
iequal = index(line,'=') ! location of first '=' in line
if (iequal==0) then
ierr = 1
return
endif
!
!--the name is what lies to the left of the equals sign
!
name = line(1:min(iequal-1,len(name),len(line)))
name = trim(adjustl(name))
!
!--find starting position of the comment (exclamation mark)
!
icomment = index(line,'!') ! location of '!' in line
if (icomment <= 0) icomment = len_trim(line)+1 ! if no '!', read to end of line
!
!--the value is what lies between the equals sign
! and either the comment or the end of the line
!
valstring = trim(adjustl(line(iequal+1:icomment-1)))
! print*,' name= '//trim(name)//' val = '//trim(valstring)
!
!--for time strings, assume they are of the form hh:mm:ss
! convert to a number of seconds as a real
!
if (index(valstring,':') /= 0) then
nsec = 0
read(valstring,"(i3.3,1x,i2.2,1x,i2.2)",iostat=ierr) nhr,nmin,nsec
if (ierr/=0) then
read(valstring,"(i2.2,1x,i2.2,1x,i2.2)",iostat=ierr) nhr,nmin,nsec
end if
!print*,'hh,mm,ss=',nhr,nmin,nsec,((nhr*60. + nmin)*60. + nsec),ierr
write(valstring,"(es11.4)",iostat=ierr) ((nhr*60. + nmin)*60. + nsec)
endif
if (present(comment)) then
comment = ' '
if (len_trim(line) > icomment+1) then
comment = line(icomment+1:len_trim(line))
endif
endif
return
end subroutine read_inopt_from_line
!-----------------------------------------------------------------
!+
! internal subroutine: get the value string matching a
! supplied input tag: ierr = -1 if string not found
!+
!-----------------------------------------------------------------
subroutine get_inopt_string(valstring,varname,infile,iunit,ierr)
character(len=*), intent(out) :: valstring
character(len=*), intent(in) :: varname,infile
integer, intent(in) :: iunit
integer, intent(out) :: ierr
character(len=maxlenstring) :: name
logical :: imatch
open(unit=iunit,file=infile,form='formatted',status='old',iostat=ierr)
imatch = .false.
do while(.not.imatch .and. ierr==0)
call read_next_inopt(name,valstring,iunit,ierr)
if (trim(name)==trim(varname)) imatch = .true.
enddo
ierr = 0
if (.not.imatch) ierr = -1
close(iunit)
return
end subroutine get_inopt_string
!-----------------------------------------------------------------
!+
! read a real variable matching the varname tag from an input file
!+
!-----------------------------------------------------------------
subroutine get_inopt_real(rval,varname,infile,iunit,ierr)
real, intent(out) :: rval
character(len=*), intent(in) :: varname,infile
integer, intent(in) :: iunit
integer, intent(out) :: ierr
character(len=maxlenstring) :: valstring
call get_inopt_string(valstring,varname,infile,iunit,ierr)
read(valstring,*,iostat=ierr) rval
return
end subroutine get_inopt_real
!-----------------------------------------------------------------
!+
! read an integer variable matching the varname tag from an input file
!+
!-----------------------------------------------------------------
subroutine get_inopt_int(ival,varname,infile,iunit,ierr)
integer, intent(out) :: ival
character(len=*), intent(in) :: varname,infile
integer, intent(in) :: iunit
integer, intent(out) :: ierr
character(len=maxlenstring) :: valstring
call get_inopt_string(valstring,varname,infile,iunit,ierr)
read(valstring,*,iostat=ierr) ival
return
end subroutine get_inopt_int
!-----------------------------------------------------------------
!+
! read a logical variable matching the varname tag from an input file
!+
!-----------------------------------------------------------------
subroutine get_inopt_logical(lval,varname,infile,iunit,ierr)
logical, intent(out) :: lval
character(len=*), intent(in) :: varname,infile
integer, intent(in) :: iunit
integer, intent(out) :: ierr
character(len=maxlenstring) :: valstring
call get_inopt_string(valstring,varname,infile,iunit,ierr)
read(valstring,*,iostat=ierr) lval
return
end subroutine get_inopt_logical
!--------------------------------------------------------------------
!+
! Function that determines whether or not an input file contains
! a loop (public)
!+
!--------------------------------------------------------------------
subroutine check_infile(infile,lu_read,containsloop,ierrline)
character(len=*), intent(in) :: infile
integer, intent(in) :: lu_read ! unit to read file on
logical, intent(out) :: containsloop
integer, intent(out) :: ierrline
character(len=maxlen) :: name
character(len=maxlenstring) :: valstring
integer :: nlinesread,line,ierr
containsloop = .false.
ierrline = 0
line = 0
open(unit=lu_read,iostat=ierr,file=infile,status='old',form='formatted')
if (ierr /= 0) ierrline = -1
do while (ierr == 0)
call read_next_inopt(name,valstring,lu_read,ierr,nlinesread)
line = line + nlinesread
if (ierr > 0) ierrline = line
if (ierr==0 .and. contains_loop(valstring)) containsloop = .true.
enddo
close(unit=lu_read)
end subroutine check_infile
!--------------------------------------------------------------------
!+
! see if there are any loops implied in the value string
! (presence of ' to ' or ', step')
!+
!--------------------------------------------------------------------
logical function contains_loop(valstring)
character(len=*), intent(in) :: valstring
if (index(valstring,' to ') > 1 .or. &
index(valstring,' step ') > 1) then
contains_loop = .true.
else
contains_loop = .false.
endif
end function contains_loop
!--------------------------------------------------------------------
!+
! extract information from a loop implied in the value string
!+
!--------------------------------------------------------------------
subroutine get_loopinfo_real(valstring,rvalstart,rvalend,rstep,njobs,log,ierr)
character(len=*), intent(in) :: valstring
real, intent(out) :: rvalstart,rvalend,rstep
integer, intent(out) :: njobs,ierr
logical, intent(out) :: log
integer :: ito,istep,idstep,instep,inlogstep,ilogstep
ito = index(valstring,' to ')
idstep = index(valstring,' step ')
instep = index(valstring,' nstep ')
inlogstep = index(valstring,' nlogstep ')
ilogstep = index(valstring,' logstep ')
ierr = 0
njobs = 0
log = .false.
istep = max(idstep,instep,inlogstep,ilogstep)
if (ito <= 0 .or. istep <= ito) then
ierr = 1
else
!--check that only one step variable is in the string
if (idstep*instep > 0 .or. idstep*inlogstep > 0 .or. instep*inlogstep > 0) then
ierr = 2
return
endif
!--extract the start and end values
read(valstring(1:ito),*,iostat=ierr) rvalstart
read(valstring(ito+4:istep-1),*,iostat=ierr) rvalend
if (instep > 0) then
!
!--here the number of steps between the end points has been specified
!
read(valstring(istep+7:),*,iostat=ierr) njobs
if (njobs > 1) then
rstep = (rvalend - rvalstart)/real(njobs-1)
else
rstep = 0.
endif
elseif (inlogstep > 0) then
!
!--here the number of steps is specified and the loop is in log space
!
read(valstring(istep+10:),*,iostat=ierr) njobs
log = .true.
if (njobs > 1) then
rstep = (log10(rvalend/rvalstart))/real(njobs-1)
else
rstep = 0.
endif
elseif (ilogstep > 0) then
!
!--here the step interval has been specified in log space
!
read(valstring(istep+9:),*,iostat=ierr) rstep
log = .true.
if (rvalend <= rvalstart) then
njobs = 0
else
njobs = int((log10(rvalend) - log10(rvalstart) + tiny(0.))/rstep) + 1
endif
else
!
!--here the step interval has been specified
!
read(valstring(istep+6:),*,iostat=ierr) rstep
if (rvalend <= rvalstart) then
njobs = 0
else
njobs = int((rvalend-rvalstart + tiny(0.))/rstep) + 1
endif
endif
endif
return
end subroutine get_loopinfo_real
!--------------------------------------------------------------------
!+
! extract information from a loop implied in the value string
!+
!--------------------------------------------------------------------
subroutine get_loopinfo_int(valstring,ivalstart,ivalend,istep,ierr)
character(len=*), intent(in) :: valstring
integer, intent(out) :: ivalstart,ivalend,istep,ierr
integer :: ito,isteppos
ito = index(valstring,' to ')
isteppos = index(valstring,' step ')
ierr = 0
if (ito <= 0 .or. isteppos <= ito) then
ierr = 1
else
read(valstring(1:ito),*,iostat=ierr) ivalstart
read(valstring(ito+4:isteppos-1),*,iostat=ierr) ivalend
read(valstring(isteppos+6:),*,iostat=ierr) istep
endif
return
end subroutine get_loopinfo_int
!--------------------------------------------------------------------
!+
! work out whether values supplied for a loop are real variables
!+
!--------------------------------------------------------------------
logical function isrealloop(valstring)
character(len=*), intent(in) :: valstring
integer :: ito,istep,instep,inlogstep,ilogstep
isrealloop = .false.
ito = index(valstring,' to ')
istep = index(valstring,' step ')
instep = index(valstring,' nstep ')
inlogstep = index(valstring,' nlogstep ')
ilogstep = index(valstring,' logstep ')
if (ito <= 0 .or. (istep <= ito .and. instep <= ito &
.and. inlogstep <= ito .and. ilogstep <= ito)) then
return
!istep = max(istep,instep,inlogstep,ilogstep)
!
!--look for decimal places in the string before and after the ' to '
! and after the ' step '
!
elseif (index(valstring(1:ito-1),'.') /= 0 .or. &
index(valstring(ito+4:istep),'.') /= 0 .or. &
index(valstring(istep+6:),'.') /= 0) then
isrealloop = .true.
endif
end function isrealloop
!--------------------------------------------------------------------
!+
! work out whether values supplied for a loop are integers
!+
!--------------------------------------------------------------------
logical function isintloop(valstring)
character(len=*), intent(in) :: valstring
integer :: ito,istep,inum,i,i1,i2
logical :: isintvalarr(3)
isintloop = .false.
isintvalarr = .false.
ito = index(valstring,' to ')
istep = index(valstring,' step ')
if (ito <= 0 .or. istep <= ito) then
return
!
!--look for numbers
!
else
i1 = 1
i2 = 0
do inum=1,3
select case(inum)
case(1)
i1 = 1
i2 = ito-1
case(2)
i1 = ito+4
i2 = istep
case(3)
i1 = istep+6
i2 = len(valstring)
end select
do i=i1,i2
select case(valstring(i:i))
case('0','1','2','3','4','5','6','7','8','9')
isintvalarr(inum) = .true.
end select
enddo
enddo
if (all(isintvalarr)) isintloop = .true.
endif
end function isintloop
!--------------------------------------------------------------------