-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathioparams.f90
2236 lines (1854 loc) · 65.7 KB
/
ioparams.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
! +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
! Automatically generated module by nml2f90
! History: nml2f90.py namelist.nml ioparams --io-nml --command-line --set-get-param -v
!
! https://github.com/perrette/nml-to-f90
! version: 0+untagged.118.g3e2c2de.dirty
!
! Features included : io_nml, command_line, set_get_param
! +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
module type_conversion
! =============================================================
!
! Type conversion functions (Courtesy of Alex Robinson's nml module)
! ==> useful to read array (lists) from command list argument)
!
! UPDATE M.P. 20161116: string_to_array :: assume comma-separated values
!
! =============================================================
implicit none
integer, parameter :: Float = kind(0.d0)
interface string_to_array
!! array as comma-separated values
module procedure :: string_to_array_string
module procedure :: string_to_array_integer
module procedure :: string_to_array_double
module procedure :: string_to_array_logical
end interface
contains
subroutine signal_error(iostat)
integer, optional :: iostat
if(present(iostat)) then
iostat = -1
return
else
stop
endif
end subroutine
pure function strip_brackets(s1) result(s2)
character(len=*), intent(in) :: s1
character(len=256) :: s2
if (len_trim(s1) < 2) return
! head
if (s1(1:2) == "(/" .and. s1(len_trim(s1)-1:) == "/)") then
s2 = adjustl(s1(3:len_trim(s1)-2))
elseif (s1(1:1) == "[" .and. s1(len_trim(s1):len_trim(s1)) == "]") then
s2 = adjustl(s1(2:len_trim(s1)-1))
elseif (s1(1:1) == "(" .and. s1(len_trim(s1):len_trim(s1)) == ")") then
s2 = adjustl(s1(2:len_trim(s1)-1))
else
s2 = s1
endif
end function
subroutine string_to_array_string (string, value, iostat)
character(len=*), intent(in) :: string
character(len=*), intent(out) :: value(:)
integer, optional :: iostat
character(len=256) :: tmpstr
integer :: q, q2
tmpstr = strip_brackets(trim(adjustl(string)))
do q=1,size(value)
q2 = index(tmpstr,',')
if (q2 == 0) then
q2 = len(tmpstr)+1
if (q /= size(value)) then
write(*,*) "command-line :: expected array of size",size(value),', &
& got:', q
call signal_error(iostat)
return
endif
else
if (q == size(value)) then
write(*,*) "command-line :: array size exceeded",size(value)
call signal_error(iostat)
return
endif
endif
read(tmpstr(:q2-1), *, iostat=iostat) value(q)
if (present(iostat)) then
if (iostat /= 0) return
endif
tmpstr = tmpstr(q2+1:)
enddo
end subroutine
subroutine string_to_array_integer (string, value, iostat)
implicit none
character(len=*), intent(in) :: string
integer, intent(out) :: value(:)
integer, optional :: iostat
character(len=256) :: tmpvec(size(value))
integer :: q
call string_to_array_string (string, tmpvec, iostat)
if (present(iostat))then
if (iostat /=0) return
endif
do q=1,size(tmpvec)
read(tmpvec(q), *, iostat=iostat) value(q)
if (present(iostat))then
if (iostat /=0) return
endif
enddo
end subroutine
subroutine string_to_array_double (string, value, iostat)
implicit none
character(len=*), intent(in) :: string
real(Float), intent(out) :: value(:)
integer, optional :: iostat
character(len=256) :: tmpvec(size(value))
integer :: q
call string_to_array_string (string, tmpvec, iostat)
if (present(iostat))then
if (iostat /=0) return
endif
do q=1,size(tmpvec)
read(tmpvec(q), *, iostat=iostat) value(q)
if (present(iostat))then
if (iostat /=0) return
endif
enddo
end subroutine
subroutine string_to_array_logical (string, value, iostat)
implicit none
character(len=*), intent(in) :: string
logical, intent(out) :: value(:)
integer, optional :: iostat
character(len=256) :: tmpvec(size(value))
integer :: q
call string_to_array_string (string, tmpvec, iostat)
if (present(iostat))then
if (iostat /=0) return
endif
do q=1,size(tmpvec)
read(tmpvec(q), *, iostat=iostat) value(q)
if (present(iostat))then
if (iostat /=0) return
endif
enddo
end subroutine
end module
module ioparams
use type_conversion
implicit none
private
public :: group1_t
public :: group2_t
public :: control_t
public :: read_nml
public :: write_nml
public :: read_nml_file
public :: parse_command_args
public :: print_help
public :: set_param_string
public :: has_param
public :: set_param
public :: get_param
public :: join_array
public :: command_argument_as_array
integer, parameter :: dp = 8
integer, parameter :: ip = 4
integer, parameter :: clen = 256
logical :: VERBOSE = .true.
type :: group1_t
character(len=clen) :: string1
character(len=clen), dimension(3) :: stringarr1
logical :: logical1
integer(kind=ip) :: integer1 ! Comment about integer1
integer(kind=ip) :: integer2 ! Another comment for integer2
character(len=clen) :: string2
end type
type :: group2_t
character(len=clen) :: string1
character(len=clen), dimension(3) :: stringarr1
logical :: logical1
integer(kind=ip) :: integer1 ! Comment about integer1
integer(kind=ip) :: integer2 ! Another comment for integer2
character(len=clen) :: string2 ! nasty " sign in comment
integer(kind=ip), dimension(7) :: intarr1
real(kind=dp) :: double1
real(kind=dp), dimension(5) :: dblarr1
logical, dimension(5) :: logarr1
end type
type :: control_t
logical :: print_nml ! print read namelist to string?
end type
interface read_nml
module procedure :: read_nml_group1
module procedure :: read_nml_group2
module procedure :: read_nml_control
end interface
interface write_nml
module procedure :: write_nml_group1
module procedure :: write_nml_group2
module procedure :: write_nml_control
end interface
interface read_nml_file
module procedure :: read_nml_file_group1
module procedure :: read_nml_file_group2
module procedure :: read_nml_file_control
end interface
interface parse_command_args
module procedure :: parse_command_args_group1
module procedure :: parse_command_args_group2
module procedure :: parse_command_args_control
end interface
interface print_help
module procedure :: print_help_group1
module procedure :: print_help_group2
module procedure :: print_help_control
end interface
interface set_param_string
module procedure :: set_param_string_group1
module procedure :: set_param_string_group2
module procedure :: set_param_string_control
end interface
interface has_param
module procedure :: has_param_group1
module procedure :: has_param_group2
module procedure :: has_param_control
end interface
interface set_param
module procedure :: set_param_group1_character
module procedure :: set_param_group1_character_arr
module procedure :: set_param_group1_integer
module procedure :: set_param_group1_logical
module procedure :: set_param_group2_character
module procedure :: set_param_group2_character_arr
module procedure :: set_param_group2_integer
module procedure :: set_param_group2_integer_arr
module procedure :: set_param_group2_logical
module procedure :: set_param_group2_logical_arr
module procedure :: set_param_group2_real
module procedure :: set_param_group2_real_arr
module procedure :: set_param_control_logical
end interface
interface get_param
module procedure :: get_param_group1_character
module procedure :: get_param_group1_character_arr
module procedure :: get_param_group1_integer
module procedure :: get_param_group1_logical
module procedure :: get_param_group2_character
module procedure :: get_param_group2_character_arr
module procedure :: get_param_group2_integer
module procedure :: get_param_group2_integer_arr
module procedure :: get_param_group2_logical
module procedure :: get_param_group2_logical_arr
module procedure :: get_param_group2_real
module procedure :: get_param_group2_real_arr
module procedure :: get_param_control_logical
end interface
contains
function join_array(arr, sep, quotes) result(str)
! String representation of a character array
character(len=*), intent(in) :: arr(:)
character(len=len(arr)*size(arr)) :: str
integer :: i, n
logical, intent(in), optional :: quotes
character(len=*), intent(in), optional :: sep
character(len=50) :: sep_opt
character(len=1) :: q
integer :: len_sep
q = "'"
if (present(quotes)) then
if (.not. quotes) then
q = ""
endif
endif
if (present(sep)) then
sep_opt = sep
len_sep = len(sep)
else
sep_opt = ", "
len_sep = 2
endif
str = ""
do i=1,size(arr)
str = trim(str)//sep_opt(:len_sep)//q//trim(arr(i))//q
enddo
if (size(arr) > 0) then
str = str(len_sep+1:)
endif
end function
subroutine command_argument_as_array(args)
character(len=*), intent(out), allocatable :: args(:)
integer :: i, n
n = command_argument_count()
allocate(args(n))
do i=1,n
call get_command_argument(i, args(i))
enddo
end subroutine
! Namelist I/O ******************************************
subroutine read_nml_group1 (iounit, params, iostat)
!+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
! Read the group1 group in a namelist file and assign to type
!+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
integer, intent(in) :: iounit
type(group1_t), intent(inout) :: params
integer, optional, intent(out) :: iostat
character(len=clen) :: string1
character(len=clen), dimension(3) :: stringarr1
logical :: logical1
integer(kind=ip) :: integer1
integer(kind=ip) :: integer2
character(len=clen) :: string2
namelist / group1 / string1, stringarr1, logical1, integer1, integer2, &
& string2
! initialize variables
string1 = params%string1
stringarr1 = params%stringarr1
logical1 = params%logical1
integer1 = params%integer1
integer2 = params%integer2
string2 = params%string2
! read all
if (.not. present(iostat)) then
read(unit=iounit, nml=group1)
else
read(unit=iounit, nml=group1, iostat=iostat)
if (iostat /= 0 .and. VERBOSE) then
write(*, *) "Failed to read namelist block: group1"
endif
endif
! assign back to type
params%string1 = string1
params%stringarr1 = stringarr1
params%logical1 = logical1
params%integer1 = integer1
params%integer2 = integer2
params%string2 = string2
end subroutine
subroutine write_nml_group1 (iounit, params)
!+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
! Read the group1 group in a namelist file and assign to type
!+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
integer, intent(in) :: iounit
type(group1_t), intent(inout) :: params
character(len=clen) :: string1
character(len=clen), dimension(3) :: stringarr1
logical :: logical1
integer(kind=ip) :: integer1
integer(kind=ip) :: integer2
character(len=clen) :: string2
namelist / group1 / string1, stringarr1, logical1, integer1, integer2, &
& string2
! initialize variables
string1 = params%string1
stringarr1 = params%stringarr1
logical1 = params%logical1
integer1 = params%integer1
integer2 = params%integer2
string2 = params%string2
! write_all
write(unit=iounit, nml=group1)
end subroutine
subroutine read_nml_file_group1 (file, params, iostat)
character(len=*), intent(in) :: file
type(group1_t), intent(inout) :: params
integer, optional, intent(out) :: iostat
integer :: unit = 4563
open(file=file, unit=unit, status='OLD')
call read_nml_group1 (unit, params, iostat)
close(unit)
end subroutine
subroutine read_nml_group2 (iounit, params, iostat)
!+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
! Read the group2 group in a namelist file and assign to type
!+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
integer, intent(in) :: iounit
type(group2_t), intent(inout) :: params
integer, optional, intent(out) :: iostat
character(len=clen) :: string1
character(len=clen), dimension(3) :: stringarr1
logical :: logical1
integer(kind=ip) :: integer1
integer(kind=ip) :: integer2
character(len=clen) :: string2
integer(kind=ip), dimension(7) :: intarr1
real(kind=dp) :: double1
real(kind=dp), dimension(5) :: dblarr1
logical, dimension(5) :: logarr1
namelist / group2 / string1, stringarr1, logical1, integer1, integer2, &
& string2, intarr1, double1, dblarr1, logarr1
! initialize variables
string1 = params%string1
stringarr1 = params%stringarr1
logical1 = params%logical1
integer1 = params%integer1
integer2 = params%integer2
string2 = params%string2
intarr1 = params%intarr1
double1 = params%double1
dblarr1 = params%dblarr1
logarr1 = params%logarr1
! read all
if (.not. present(iostat)) then
read(unit=iounit, nml=group2)
else
read(unit=iounit, nml=group2, iostat=iostat)
if (iostat /= 0 .and. VERBOSE) then
write(*, *) "Failed to read namelist block: group2"
endif
endif
! assign back to type
params%string1 = string1
params%stringarr1 = stringarr1
params%logical1 = logical1
params%integer1 = integer1
params%integer2 = integer2
params%string2 = string2
params%intarr1 = intarr1
params%double1 = double1
params%dblarr1 = dblarr1
params%logarr1 = logarr1
end subroutine
subroutine write_nml_group2 (iounit, params)
!+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
! Read the group2 group in a namelist file and assign to type
!+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
integer, intent(in) :: iounit
type(group2_t), intent(inout) :: params
character(len=clen) :: string1
character(len=clen), dimension(3) :: stringarr1
logical :: logical1
integer(kind=ip) :: integer1
integer(kind=ip) :: integer2
character(len=clen) :: string2
integer(kind=ip), dimension(7) :: intarr1
real(kind=dp) :: double1
real(kind=dp), dimension(5) :: dblarr1
logical, dimension(5) :: logarr1
namelist / group2 / string1, stringarr1, logical1, integer1, integer2, &
& string2, intarr1, double1, dblarr1, logarr1
! initialize variables
string1 = params%string1
stringarr1 = params%stringarr1
logical1 = params%logical1
integer1 = params%integer1
integer2 = params%integer2
string2 = params%string2
intarr1 = params%intarr1
double1 = params%double1
dblarr1 = params%dblarr1
logarr1 = params%logarr1
! write_all
write(unit=iounit, nml=group2)
end subroutine
subroutine read_nml_file_group2 (file, params, iostat)
character(len=*), intent(in) :: file
type(group2_t), intent(inout) :: params
integer, optional, intent(out) :: iostat
integer :: unit = 4563
open(file=file, unit=unit, status='OLD')
call read_nml_group2 (unit, params, iostat)
close(unit)
end subroutine
subroutine read_nml_control (iounit, params, iostat)
!+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
! Read the control group in a namelist file and assign to type
!+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
integer, intent(in) :: iounit
type(control_t), intent(inout) :: params
integer, optional, intent(out) :: iostat
logical :: print_nml
namelist / control / print_nml
! initialize variables
print_nml = params%print_nml
! read all
if (.not. present(iostat)) then
read(unit=iounit, nml=control)
else
read(unit=iounit, nml=control, iostat=iostat)
if (iostat /= 0 .and. VERBOSE) then
write(*, *) "Failed to read namelist block: control"
endif
endif
! assign back to type
params%print_nml = print_nml
end subroutine
subroutine write_nml_control (iounit, params)
!+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
! Read the control group in a namelist file and assign to type
!+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
integer, intent(in) :: iounit
type(control_t), intent(inout) :: params
logical :: print_nml
namelist / control / print_nml
! initialize variables
print_nml = params%print_nml
! write_all
write(unit=iounit, nml=control)
end subroutine
subroutine read_nml_file_control (file, params, iostat)
character(len=*), intent(in) :: file
type(control_t), intent(inout) :: params
integer, optional, intent(out) :: iostat
integer :: unit = 4563
open(file=file, unit=unit, status='OLD')
call read_nml_control (unit, params, iostat)
close(unit)
end subroutine
! Command-line argument passing
subroutine parse_command_args_group1 (params, args, unmatched, &
& stop_on_help, iostat)
!+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
! Maybe assign ith command line argument to the params type
! Input:
! params : the paramter type
! Output:
! iostat : integer, optional
! 0 : all arguments were found
! >0 : number of unmatched arguments
! -1 : error when reading
! -2 : --help was printed
! If not provided, execution stop if iostat /= 0
!+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
type(group1_t), intent(inout) :: params
integer, intent(out), optional :: iostat
character(len=*), dimension(:), intent(in), optional :: args
character(len=*), dimension(:), allocatable, intent(inout), optional :: &
& unmatched
character(len=clen), dimension(:), allocatable :: args_opt, unmatched_opt
logical, intent(in), optional :: stop_on_help
character(len=clen) :: argn, argv
logical :: missing_value, stop_on_help_opt
integer :: i,j, n, parsed
if (present(stop_on_help)) then
stop_on_help_opt = stop_on_help
else
stop_on_help_opt = .true.
endif
! Define a list of command line arguments args_opt
if (present(args)) then
! ... provided as subroutine argument
args_opt = args
else
! ... provided as command-line argument
call command_argument_as_array(args_opt)
endif
n = size(args_opt)
if (allocated(unmatched_opt)) deallocate(unmatched_opt)
allocate(unmatched_opt(n))
parsed = 0
i = 1
j = 1
do while (i <= n)
argn = args_opt(i)
! Print HELP ?
if (argn == '--help' .or. argn=='-h') then
call print_help_group1(params)
if (present(iostat)) then
iostat = -2
return
elseif (stop_on_help_opt) then
stop('End of help message : exit')
endif
endif
if (argn(1:2) /= "--") then
if (.not.present(iostat) .and. .not.present(unmatched)) then
write(*,*) "i=",i, "; Got: ",trim(argn)
stop("ERROR::ioparams type-specific command line &
& arguments must start with '--'")
else
unmatched_opt(j) = trim(argn)
j = j + 1
i = i + 1 ! check next argument
cycle
endif
endif
if (has_param_group1(params, trim(argn(3:)))) then
! +++++ present
! only "--name value" is tolerated
! check if value is missing
missing_value = .false.
if (i+1 <= n) then
argv = args_opt(i+1)
if ( len(argv) > 1) then
! ...next argument starts with '--'
if (argv(1:2) == "--" ) then
missing_value = .true.
endif
endif
else
! ...end of command line
missing_value = .true.
endif
if (missing_value) then
write(*,*) "ERROR::ioparams::group1: &
& missing value for "//trim(argn(3:))
if (present(iostat)) then
iostat = -1
return
else
stop
endif
endif
call set_param_string_group1(params, trim(argn(3:)), trim(argv), &
& iostat=iostat)
if (present(iostat)) then
if (iostat /= 0) then
return ! error
endif
endif
parsed = parsed + 2
i = i + 2
else
! +++++ not found
if (.not. present(iostat) .and. .not. present(unmatched)) then
write(*,*) "ERROR: unknown parameter in group1 : ",trim(argn)
write(*,*) ""
write(*,*) "-h or --help for HELP"
stop
endif
unmatched_opt(j) = trim(argn)
j = j + 1
i = i + 1
endif
enddo
! At this point, any type error or --help message cases are already sorted
! out
if (present(iostat)) then
iostat = n - parsed
endif
if (present(unmatched)) then
unmatched = unmatched_opt(:n-parsed)
endif
deallocate(args_opt, unmatched_opt)
end subroutine
subroutine print_help_group1(params, iounit, default)
!+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
! Print HELP on a derived type, useful for command-line arg
!+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
type(group1_t), intent(in) :: params
integer, optional :: iounit
integer :: io
logical, optional :: default
logical :: def
character(len=2000) :: valuestr
character(len=20) :: valuelen
character(len=2000) :: nameshort
if (present(iounit)) then
io = iounit
else
io = 6 ! screen
endif
if (present(default)) then
def = default
else
def = .true. ! by default do show default values
endif
write(io, *) " "
write(io, *) "+++++++++++++++++ group1 ++++++++++++++++++"
write(io, *) " "
write(nameshort, *) "string1"
if (def) then
write(valuestr, *) params%string1
write(valuelen, *) max(1,len(trim(adjustl(valuestr))))
write(io, '(" --",A20," (default: ",A'//trim(valuelen)//',")")') &
& adjustl(nameshort), trim(adjustl(valuestr))
else
write(io, '(" --",A20,"")') adjustl(nameshort)
endif
write(nameshort, *) "stringarr1"
if (def) then
write(valuestr, *) params%stringarr1(1) ! only first element
write(valuelen, *) max(1,len(trim(adjustl(valuestr))))
write(io, '(" --",A20," (default: &
[",A'//trim(valuelen)//',", ...], size=",I2,")")') adjustl(nameshort), &
& trim(adjustl(valuestr)), size(params%stringarr1)
else
write(io, '(" --",A20,"")') adjustl(nameshort)
endif
write(nameshort, *) "logical1"
if (def) then
write(valuestr, *) params%logical1
write(valuelen, *) max(1,len(trim(adjustl(valuestr))))
write(io, '(" --",A20," (default: ",A'//trim(valuelen)//',")")') &
& adjustl(nameshort), trim(adjustl(valuestr))
else
write(io, '(" --",A20,"")') adjustl(nameshort)
endif
write(nameshort, *) "integer1"
if (def) then
write(valuestr, *) params%integer1
write(valuelen, *) max(1,len(trim(adjustl(valuestr))))
write(io, '(" --",A20,"Comment about integer1 (default: &
& ",A'//trim(valuelen)//',")")') adjustl(nameshort), trim(adjustl(valuestr))
else
write(io, '(" --",A20,"Comment about integer1")') adjustl(nameshort)
endif
write(nameshort, *) "integer2"
if (def) then
write(valuestr, *) params%integer2
write(valuelen, *) max(1,len(trim(adjustl(valuestr))))
write(io, '(" --",A20,"Another comment for integer2 (default: &
& ",A'//trim(valuelen)//',")")') adjustl(nameshort), trim(adjustl(valuestr))
else
write(io, '(" --",A20,"Another comment for integer2")') &
& adjustl(nameshort)
endif
write(nameshort, *) "string2"
if (def) then
write(valuestr, *) params%string2
write(valuelen, *) max(1,len(trim(adjustl(valuestr))))
write(io, '(" --",A20," (default: ",A'//trim(valuelen)//',")")') &
& adjustl(nameshort), trim(adjustl(valuestr))
else
write(io, '(" --",A20,"")') adjustl(nameshort)
endif
end subroutine
subroutine set_param_string_group1 (params, name, string, iostat)
!+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
! Set one field of the group1 type from a string argument
!+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
type(group1_t), intent(inout) :: params
character(len=*), intent(in) :: name
character(len=*), intent(in) :: string
integer, intent(out), optional :: iostat
integer :: io !! local...
if (present(iostat)) then
iostat = 0
endif
select case (name)
case ('string1', 'group1%string1')
params%string1 = trim(string)
if (VERBOSE) write(*,*) "group1%string1 = ", params%string1
case ('stringarr1', 'group1%stringarr1')
call string_to_array(string, params%stringarr1, iostat=io)
if (VERBOSE) write(*,*) "group1%stringarr1 = ", &
& trim(params%stringarr1(1)),', ...'
if (io /= 0) then
if (trim(string) == "") then
write(*,*) "ERROR: missing parameter value for &
& --group1%stringarr1"
else
write(*,*) "ERROR converting string to character(len=clen), &
& dimension(3) :: stringarr1: --group1%stringarr1 ",trim(string)
endif
stop
endif
case ('logical1', 'group1%logical1')
read(string, *, iostat=io) params%logical1
if (VERBOSE .or. io/=0) write(*,*) "group1%logical1 = ", params%logical1
if (io /= 0) then
if (trim(string) == "") then
write(*,*) "ERROR: missing parameter value for --group1%logical1"
else
write(*,*) "ERROR converting string to logical :: logical1: &
& --group1%logical1 ",trim(string)
endif
stop
endif
case ('integer1', 'group1%integer1')
read(string, *, iostat=io) params%integer1
if (VERBOSE .or. io/=0) write(*,*) "group1%integer1 = ", params%integer1
if (io /= 0) then
if (trim(string) == "") then
write(*,*) "ERROR: missing parameter value for --group1%integer1"
else
write(*,*) "ERROR converting string to integer(kind=ip) :: &
& integer1: --group1%integer1 ",trim(string)
endif
stop
endif
case ('integer2', 'group1%integer2')
read(string, *, iostat=io) params%integer2
if (VERBOSE .or. io/=0) write(*,*) "group1%integer2 = ", params%integer2
if (io /= 0) then
if (trim(string) == "") then
write(*,*) "ERROR: missing parameter value for --group1%integer2"
else
write(*,*) "ERROR converting string to integer(kind=ip) :: &
& integer2: --group1%integer2 ",trim(string)
endif
stop
endif
case ('string2', 'group1%string2')
params%string2 = trim(string)
if (VERBOSE) write(*,*) "group1%string2 = ", params%string2
case default
write(*,*) "ERROR::ioparams::group1: &
& unknown member :: ", trim(name)
if (present(iostat)) then
iostat = -1
else
stop
endif
end select
end subroutine
function has_param_group1 (params, name) result(has_param)
!+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
! Test whether an attribute name is member of the type group1
!+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
type(group1_t), intent(inout) :: params
character(len=*), intent(in) :: name
logical :: has_param
has_param = .true.
select case (name)
case ('string1', 'group1%string1')
case ('stringarr1', 'group1%stringarr1')
case ('logical1', 'group1%logical1')
case ('integer1', 'group1%integer1')
case ('integer2', 'group1%integer2')
case ('string2', 'group1%string2')
case default
has_param = .false.
end select
end function
subroutine parse_command_args_group2 (params, args, unmatched, &
& stop_on_help, iostat)
!+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
! Maybe assign ith command line argument to the params type
! Input:
! params : the paramter type
! Output:
! iostat : integer, optional
! 0 : all arguments were found
! >0 : number of unmatched arguments
! -1 : error when reading
! -2 : --help was printed
! If not provided, execution stop if iostat /= 0
!+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
type(group2_t), intent(inout) :: params
integer, intent(out), optional :: iostat
character(len=*), dimension(:), intent(in), optional :: args
character(len=*), dimension(:), allocatable, intent(inout), optional :: &
& unmatched
character(len=clen), dimension(:), allocatable :: args_opt, unmatched_opt
logical, intent(in), optional :: stop_on_help
character(len=clen) :: argn, argv
logical :: missing_value, stop_on_help_opt
integer :: i,j, n, parsed
if (present(stop_on_help)) then
stop_on_help_opt = stop_on_help
else
stop_on_help_opt = .true.