-
Notifications
You must be signed in to change notification settings - Fork 0
/
MOM_file_parser.F90
1886 lines (1660 loc) · 94.3 KB
/
MOM_file_parser.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 MOM6 facility to parse input files for runtime parameters
module MOM_file_parser
! This file is part of MOM6. See LICENSE.md for the license.
use MOM_error_handler, only : MOM_error, FATAL, WARNING, MOM_mesg
use MOM_error_handler, only : stdlog, stdout
use MOM_string_functions, only : left_int, left_ints, slasher
use MOM_string_functions, only : left_real, left_reals
use MOM_document, only : doc_type, doc_param, doc_init, doc_openblock, doc_closeblock, doc_end, doc_module
implicit none ; private
integer, parameter, public :: MAX_PARAM_FILES = 5 !< Maximum number of parameter files.
integer, parameter :: INPUT_STR_LENGTH = 320 !< Maximum line length in parameter file.
integer, parameter :: FILENAME_LENGTH = 200 !< Maximum number of characters in file names.
! The all_PEs_read option should be eliminated with post-riga shared code.
logical :: all_PEs_read = .true. !< If true, all PEs read the input files
!! TODO: Eliminate this parameter
!>@{ Default values for parameters
logical, parameter :: report_unused_default = .true.
logical, parameter :: unused_params_fatal_default = .false.
logical, parameter :: log_to_stdout_default = .false.
logical, parameter :: complete_doc_default = .true.
logical, parameter :: minimal_doc_default = .true.
!>@}
!> The valid lines extracted from an input parameter file without comments
type, private :: file_data_type ; private
integer :: num_lines = 0 !< The number of lines in this type
character(len=INPUT_STR_LENGTH), pointer, dimension(:) :: line => NULL() !< The line content
logical, pointer, dimension(:) :: line_used => NULL() !< If true, the line has been read
end type file_data_type
!> A link in the list of variables that have already had override warnings issued
type :: link_parameter ; private
type(link_parameter), pointer :: next => NULL() !< Facilitates linked list
character(len=80) :: name !< Parameter name
logical :: hasIssuedOverrideWarning = .false. !< Has a default value
end type link_parameter
!> Specify the active parameter block
type :: parameter_block ; private
character(len=240) :: name = '' !< The active parameter block name
end type parameter_block
!> A structure that can be parsed to read and document run-time parameters.
type, public :: param_file_type ; private
integer :: nfiles = 0 !< The number of open files.
integer :: iounit(MAX_PARAM_FILES) !< The unit numbers of open files.
character(len=FILENAME_LENGTH) :: filename(MAX_PARAM_FILES) !< The names of the open files.
logical :: NetCDF_file(MAX_PARAM_FILES) !< If true, the input file is in NetCDF.
! This is not yet implemented.
type(file_data_type) :: param_data(MAX_PARAM_FILES) !< Structures that contain
!! the valid data lines from the parameter
!! files, enabling all subsequent reads of
!! parameter data to occur internally.
logical :: report_unused = report_unused_default !< If true, report any
!! parameter lines that are not used in the run.
logical :: unused_params_fatal = unused_params_fatal_default !< If true, kill
!! the run if there are any unused parameters.
logical :: log_to_stdout = log_to_stdout_default !< If true, all log
!! messages are also sent to stdout.
logical :: log_open = .false. !< True if the log file has been opened.
integer :: stdout !< The unit number from stdout().
integer :: stdlog !< The unit number from stdlog().
character(len=240) :: doc_file !< A file where all run-time parameters, their
!! settings and defaults are documented.
logical :: complete_doc = complete_doc_default !< If true, document all
!! run-time parameters.
logical :: minimal_doc = minimal_doc_default !< If true, document only those
!! run-time parameters that differ from defaults.
type(doc_type), pointer :: doc => NULL() !< A structure that contains information
!! related to parameter documentation.
type(link_parameter), pointer :: chain => NULL() !< Facilitates linked list
type(parameter_block), pointer :: blockName => NULL() !< Name of active parameter block
end type param_file_type
public read_param, open_param_file, close_param_file, log_param, log_version
public doc_param, get_param
public clearParameterBlock, openParameterBlock, closeParameterBlock
!> An overloaded interface to read various types of parameters
interface read_param
module procedure read_param_int, read_param_real, read_param_logical, &
read_param_char, read_param_char_array, &
read_param_int_array, read_param_real_array
end interface
!> An overloaded interface to log the values of various types of parameters
interface log_param
module procedure log_param_int, log_param_real, log_param_logical, &
log_param_char, &
log_param_int_array, log_param_real_array
end interface
!> An overloaded interface to read and log the values of various types of parameters
interface get_param
module procedure get_param_int, get_param_real, get_param_logical, &
get_param_char, get_param_char_array, &
get_param_int_array, get_param_real_array
end interface
!> An overloaded interface to log version information about modules
interface log_version
module procedure log_version_cs, log_version_plain
end interface
contains
!> Make the contents of a parameter input file availalble in a param_file_type
subroutine open_param_file(filename, CS, checkable, component, doc_file_dir)
character(len=*), intent(in) :: filename !< An input file name, optionally with the full path
type(param_file_type), intent(inout) :: CS !< The control structure for the file_parser module,
!! it is also a structure to parse for run-time parameters
logical, optional, intent(in) :: checkable !< If this is false, it disables checks of this
!! file for unused parameters. The default is True.
character(len=*), optional, intent(in) :: component !< If present, this component name is used
!! to generate parameter documentation file names; the default is"MOM"
character(len=*), optional, intent(in) :: doc_file_dir !< An optional directory in which to write out
!! the documentation files. The default is effectively './'.
! Local variables
logical :: file_exists, unit_in_use, Netcdf_file, may_check
integer :: ios, iounit, strlen, i
character(len=240) :: doc_path
type(parameter_block), pointer :: block => NULL()
may_check = .true. ; if (present(checkable)) may_check = checkable
! Check for non-blank filename
strlen = len_trim(filename)
if (strlen == 0) then
call MOM_error(FATAL, "open_param_file: Input file has not been specified.")
endif
! Check that this file has not already been opened
if (CS%nfiles > 0) then
inquire(file=trim(filename), number=iounit)
if (iounit /= -1) then
do i = 1, CS%nfiles
if (CS%iounit(i) == iounit) then
if (trim(CS%filename(1)) /= trim(filename)) then
call MOM_error(FATAL, &
"open_param_file: internal inconsistency! "//trim(filename)// &
" is registered as open but has the wrong unit number!")
else
call MOM_error(WARNING, &
"open_param_file: file "//trim(filename)// &
" has already been opened. This should NOT happen!"// &
" Did you specify the same file twice in a namelist?")
return
endif ! filenames
endif ! unit numbers
enddo ! i
endif
endif
! Check that the file exists to readstdlog
inquire(file=trim(filename), exist=file_exists)
if (.not.file_exists) call MOM_error(FATAL, &
"open_param_file: Input file "// trim(filename)//" does not exist.")
Netcdf_file = .false.
if (strlen > 3) then
if (filename(strlen-2:strlen) == ".nc") Netcdf_file = .true.
endif
if (Netcdf_file) &
call MOM_error(FATAL,"open_param_file: NetCDF files are not yet supported.")
if (all_PEs_read ) then
! Find an unused unit number.
do iounit=10,512
INQUIRE(iounit,OPENED=unit_in_use) ; if (.not.unit_in_use) exit
enddo
if (iounit >= 512) call MOM_error(FATAL, &
"open_param_file: No unused file unit could be found.")
! Open the parameter file.
open(iounit, file=trim(filename), access='SEQUENTIAL', &
form='FORMATTED', action='READ', position='REWIND', iostat=ios)
if (ios /= 0) call MOM_error(FATAL, "open_param_file: Error opening "// &
trim(filename))
else
iounit = 1
endif
! Store/register the unit and details
i = CS%nfiles + 1
CS%nfiles = i
CS%iounit(i) = iounit
CS%filename(i) = filename
CS%NetCDF_file(i) = Netcdf_file
allocate(block) ; block%name = '' ; CS%blockName => block
call MOM_mesg("open_param_file: "// trim(filename)// &
" has been opened successfully.", 5)
call populate_param_data(iounit, filename, CS%param_data(i))
call read_param(CS,"SEND_LOG_TO_STDOUT",CS%log_to_stdout)
call read_param(CS,"REPORT_UNUSED_PARAMS",CS%report_unused)
call read_param(CS,"FATAL_UNUSED_PARAMS",CS%unused_params_fatal)
CS%doc_file = "MOM_parameter_doc"
if (present(component)) CS%doc_file = trim(component)//"_parameter_doc"
call read_param(CS,"DOCUMENT_FILE", CS%doc_file)
if (.not.may_check) then
CS%report_unused = .false.
CS%unused_params_fatal = .false.
endif
! Open the log file.
CS%stdlog = stdlog() ; CS%stdout = stdout()
CS%log_open = (stdlog() > 0)
doc_path = CS%doc_file
if (len_trim(CS%doc_file) > 0) then
CS%complete_doc = complete_doc_default
call read_param(CS, "COMPLETE_DOCUMENTATION", CS%complete_doc)
CS%minimal_doc = minimal_doc_default
call read_param(CS, "MINIMAL_DOCUMENTATION", CS%minimal_doc)
if (present(doc_file_dir)) then ; if (len_trim(doc_file_dir) > 0) then
doc_path = trim(slasher(doc_file_dir))//trim(CS%doc_file)
endif ; endif
else
CS%complete_doc = .false.
CS%minimal_doc = .false.
endif
call doc_init(doc_path, CS%doc, minimal=CS%minimal_doc, complete=CS%complete_doc, &
layout=CS%complete_doc, debugging=CS%complete_doc)
end subroutine open_param_file
!> Close any open input files and deallocate memory associated with this param_file_type.
!! To use this type again, open_param_file would have to be called again.
subroutine close_param_file(CS, quiet_close, component)
type(param_file_type), intent(inout) :: CS !< The control structure for the file_parser module,
!! it is also a structure to parse for run-time parameters
logical, optional, intent(in) :: quiet_close !< if present and true, do not do any
!! logging with this call.
character(len=*), optional, intent(in) :: component !< If present, this component name is used
!! to generate parameter documentation file names
! Local variables
logical :: all_default
character(len=128) :: docfile_default
character(len=40) :: mdl ! This module's name.
! This include declares and sets the variable "version".
# include "version_variable.h"
integer :: i, n, num_unused
if (present(quiet_close)) then ; if (quiet_close) then
do i = 1, CS%nfiles
! if (all_PEs_read .or. is_root_pe()) close(CS%iounit(i))
close(CS%iounit(i)) ! serial code version
call MOM_mesg("close_param_file: "// trim(CS%filename(i))// &
" has been closed successfully.", 5)
CS%iounit(i) = -1
CS%filename(i) = ''
CS%NetCDF_file(i) = .false.
deallocate (CS%param_data(i)%line)
deallocate (CS%param_data(i)%line_used)
enddo
CS%log_open = .false.
call doc_end(CS%doc)
return
endif ; endif
! Log the parameters for the parser.
docfile_default = "MOM_parameter_doc"
if (present(component)) docfile_default = trim(component)//"_parameter_doc"
all_default = (CS%log_to_stdout .eqv. log_to_stdout_default)
all_default = all_default .and. (trim(CS%doc_file) == trim(docfile_default))
if (len_trim(CS%doc_file) > 0) then
all_default = all_default .and. (CS%complete_doc .eqv. complete_doc_default)
all_default = all_default .and. (CS%minimal_doc .eqv. minimal_doc_default)
endif
mdl = "MOM_file_parser"
call log_version(CS, mdl, version, "", debugging=.true., log_to_all=.true., all_default=all_default)
call log_param(CS, mdl, "SEND_LOG_TO_STDOUT", CS%log_to_stdout, &
"If true, all log messages are also sent to stdout.", &
default=log_to_stdout_default)
call log_param(CS, mdl, "REPORT_UNUSED_PARAMS", CS%report_unused, &
"If true, report any parameter lines that are not used "//&
"in the run.", default=report_unused_default, &
debuggingParam=.true.)
call log_param(CS, mdl, "FATAL_UNUSED_PARAMS", CS%unused_params_fatal, &
"If true, kill the run if there are any unused "//&
"parameters.", default=unused_params_fatal_default, &
debuggingParam=.true.)
call log_param(CS, mdl, "DOCUMENT_FILE", CS%doc_file, &
"The basename for files where run-time parameters, their "//&
"settings, units and defaults are documented. Blank will "//&
"disable all parameter documentation.", default=docfile_default)
if (len_trim(CS%doc_file) > 0) then
call log_param(CS, mdl, "COMPLETE_DOCUMENTATION", CS%complete_doc, &
"If true, all run-time parameters are "//&
"documented in "//trim(CS%doc_file)//&
".all .", default=complete_doc_default)
call log_param(CS, mdl, "MINIMAL_DOCUMENTATION", CS%minimal_doc, &
"If true, non-default run-time parameters are "//&
"documented in "//trim(CS%doc_file)//&
".short .", default=minimal_doc_default)
endif
num_unused = 0
do i = 1, CS%nfiles
if ( (CS%report_unused .or. &
CS%unused_params_fatal)) then
! Check for unused lines.
do n=1,CS%param_data(i)%num_lines
if (.not.CS%param_data(i)%line_used(n)) then
num_unused = num_unused + 1
if (CS%report_unused) &
call MOM_error(WARNING, "Unused line in "//trim(CS%filename(i))// &
" : "//trim(CS%param_data(i)%line(n)))
endif
enddo
endif
close(CS%iounit(i))
call MOM_mesg("close_param_file: "// trim(CS%filename(i))// &
" has been closed successfully.", 5)
CS%iounit(i) = -1
CS%filename(i) = ''
CS%NetCDF_file(i) = .false.
deallocate (CS%param_data(i)%line)
deallocate (CS%param_data(i)%line_used)
enddo
if ((num_unused>0) .and. CS%unused_params_fatal) &
call MOM_error(FATAL, "Run stopped because of unused parameter lines.")
CS%log_open = .false.
call doc_end(CS%doc)
end subroutine close_param_file
!> Read the contents of a parameter input file, and store the contents in a
!! file_data_type after removing comments and simplifying white space
subroutine populate_param_data(iounit, filename, param_data)
integer, intent(in) :: iounit !< The IO unit number that is open for filename
character(len=*), intent(in) :: filename !< An input file name, optionally with the full path
type(file_data_type), intent(inout) :: param_data !< A list of the input lines that set parameters
!! after comments have been stripped out.
! Local variables
character(len=INPUT_STR_LENGTH) :: line
integer :: num_lines
logical :: inMultiLineComment
! Find the number of keyword lines in a parameter file
! Allocate the space to hold the lines in param_data%line
! Populate param_data%line with the keyword lines from parameter file
if (iounit <= 0) return
if (.true.) then
! rewind the parameter file
rewind(iounit)
! count the number of valid entries in the parameter file
num_lines = 0
inMultiLineComment = .false.
do while(.true.)
read(iounit, '(a)', end=8, err=9) line
line = replaceTabs(line)
if (inMultiLineComment) then
if (closeMultiLineComment(line)) inMultiLineComment=.false.
else
if (lastNonCommentNonBlank(line)>0) num_lines = num_lines + 1
if (openMultiLineComment(line)) inMultiLineComment=.true.
endif
enddo ! while (.true.)
8 continue ! get here when read() reaches EOF
if (inMultiLineComment ) &
call MOM_error(FATAL, 'MOM_file_parser : A C-style multi-line comment '// &
'(/* ... */) was not closed before the end of '//trim(filename))
! allocate space to hold contents of the parameter file
param_data%num_lines = num_lines
endif ! (is_root_pe())
! Broadcast the number of valid entries in parameter file
! if (.not. all_PEs_read) then
! call broadcast(param_data%num_lines, root_pe())
! endif
! Set up the space for storing the actual lines.
num_lines = param_data%num_lines
allocate (param_data%line(num_lines))
allocate (param_data%line_used(num_lines))
param_data%line(:) = ' '
param_data%line_used(:) = .false.
! Read the actual lines.
if (all_PEs_read ) then
! rewind the parameter file
rewind(iounit)
! Populate param_data%line
num_lines = 0
do while(.true.)
read(iounit, '(a)', end=18, err=9) line
line = replaceTabs(line)
if (inMultiLineComment) then
if (closeMultiLineComment(line)) inMultiLineComment=.false.
else
if (lastNonCommentNonBlank(line)>0) then
line = removeComments(line)
line = simplifyWhiteSpace(line(:len_trim(line)))
num_lines = num_lines + 1
param_data%line(num_lines) = line
endif
if (openMultiLineComment(line)) inMultiLineComment=.true.
endif
enddo ! while (.true.)
18 continue ! get here when read() reaches EOF
if (num_lines /= param_data%num_lines) &
call MOM_error(FATAL, 'MOM_file_parser : Found different number of '// &
'valid lines on second reading of '//trim(filename))
endif ! (is_root_pe())
! Broadcast the populated array param_data%line
! if (.not. all_PEs_read) then
! call broadcast(param_data%line, INPUT_STR_LENGTH, root_pe())
! endif
return
9 call MOM_error(FATAL, "MOM_file_parser : "//&
"Error while reading file "//trim(filename))
end subroutine populate_param_data
!> Return True if a /* appears on this line without a closing */
function openMultiLineComment(string)
character(len=*), intent(in) :: string !< The input string to process
logical :: openMultiLineComment
! Local variables
integer :: icom, last
openMultiLineComment = .false.
last = lastNonCommentIndex(string)+1
icom = index(string(last:), "/*")
if (icom > 0) then
openMultiLineComment=.true.
last = last+icom+1
endif
icom = index(string(last:), "*/") ; if (icom > 0) openMultiLineComment=.false.
end function openMultiLineComment
!> Return True if a */ appears on this line
function closeMultiLineComment(string)
character(len=*), intent(in) :: string !< The input string to process
logical :: closeMultiLineComment
! True if a */ appears on this line
closeMultiLineComment = .false.
if (index(string, "*/")>0) closeMultiLineComment=.true.
end function closeMultiLineComment
!> Find position of last character before any comments, As marked by "!", "//", or "/*"
!! following F90, C++, or C syntax
function lastNonCommentIndex(string)
character(len=*), intent(in) :: string !< The input string to process
integer :: lastNonCommentIndex
! Local variables
integer :: icom, last
! This subroutine is the only place where a comment needs to be defined
last = len_trim(string)
icom = index(string(:last), "!") ; if (icom > 0) last = icom-1 ! F90 style
icom = index(string(:last), "//") ; if (icom > 0) last = icom-1 ! C++ style
icom = index(string(:last), "/*") ; if (icom > 0) last = icom-1 ! C style
lastNonCommentIndex = last
end function lastNonCommentIndex
!> Find position of last non-blank character before any comments
function lastNonCommentNonBlank(string)
character(len=*), intent(in) :: string !< The input string to process
integer :: lastNonCommentNonBlank
lastNonCommentNonBlank = len_trim(string(:lastNonCommentIndex(string))) ! Ignore remaining trailing blanks
end function lastNonCommentNonBlank
!> Returns a string with tabs replaced by a blank
function replaceTabs(string)
character(len=*), intent(in) :: string !< The input string to process
character(len=len(string)) :: replaceTabs
integer :: i
do i=1, len(string)
if (string(i:i)==achar(9)) then
replaceTabs(i:i)=" "
else
replaceTabs(i:i)=string(i:i)
endif
enddo
end function replaceTabs
!> Trims comments and leading blanks from string
function removeComments(string)
character(len=*), intent(in) :: string !< The input string to process
character(len=len(string)) :: removeComments
integer :: last
removeComments=repeat(" ",len(string))
last = lastNonCommentNonBlank(string)
removeComments(:last)=adjustl(string(:last)) ! Copy only the non-comment part of string
end function removeComments
!> Constructs a string with all repeated whitespace replaced with single blanks
!! and insert white space where it helps delineate tokens (e.g. around =)
function simplifyWhiteSpace(string)
character(len=*), intent(in) :: string !< A string to modify to simpify white space
character(len=len(string)+16) :: simplifyWhiteSpace
! Local variables
integer :: i,j
logical :: nonBlank = .false., insideString = .false.
character(len=1) :: quoteChar=" "
nonBlank = .false.; insideString = .false. ! NOTE: For some reason this line is needed??
i=0
simplifyWhiteSpace=repeat(" ",len(string)+16)
do j=1,len_trim(string)
if (insideString) then ! Do not change formatting inside strings
i=i+1
simplifyWhiteSpace(i:i)=string(j:j)
if (string(j:j)==quoteChar) insideString=.false. ! End of string
else ! The following is outside of string delimiters
if (string(j:j)==" " .or. string(j:j)==achar(9)) then ! Space or tab
if (nonBlank) then ! Only copy a blank if the preceeding character was non-blank
i=i+1
simplifyWhiteSpace(i:i)=" " ! Not string(j:j) so that tabs are replace by blanks
nonBlank=.false.
endif
elseif (string(j:j)=='"' .or. string(j:j)=="'") then ! Start a sting
i=i+1
simplifyWhiteSpace(i:i)=string(j:j)
insideString=.true.
quoteChar=string(j:j) ! Keep copy of starting quote
nonBlank=.true. ! For exit from string
elseif (string(j:j)=='=') then
! Insert spaces if this character is "=" so that line contains " = "
if (nonBlank) then
i=i+1
simplifyWhiteSpace(i:i)=" "
endif
i=i+2
simplifyWhiteSpace(i-1:i)=string(j:j)//" "
nonBlank=.false.
else ! All other characters
i=i+1
simplifyWhiteSpace(i:i)=string(j:j)
nonBlank=.true.
endif
endif ! if (insideString)
enddo ! j
if (insideString) then ! A missing close quote should be flagged
call MOM_error(FATAL, &
"There is a mismatched quote in the parameter file line: "// &
trim(string))
endif
end function simplifyWhiteSpace
!> This subroutine reads the value of an integer model parameter from a parameter file.
subroutine read_param_int(CS, varname, value, fail_if_missing)
type(param_file_type), intent(in) :: CS !< The control structure for the file_parser module,
!! it is also a structure to parse for run-time parameters
character(len=*), intent(in) :: varname !< The case-sensitive name of the parameter to read
integer, intent(inout) :: value !< The value of the parameter that may be
!! read from the parameter file
logical, optional, intent(in) :: fail_if_missing !< If present and true, a fatal error occurs
!! if this variable is not found in the parameter file
! Local variables
character(len=INPUT_STR_LENGTH) :: value_string(1)
logical :: found, defined
call get_variable_line(CS, varname, found, defined, value_string)
if (found .and. defined .and. (LEN_TRIM(value_string(1)) > 0)) then
read(value_string(1),*,err = 1001) value
else
if (present(fail_if_missing)) then ; if (fail_if_missing) then
if (.not.found) then
call MOM_error(FATAL,'read_param_int: Unable to find variable '//trim(varname)// &
' in any input files.')
else
call MOM_error(FATAL,'read_param_int: Variable '//trim(varname)// &
' found but not set in input files.')
endif
endif ; endif
endif
return
1001 call MOM_error(FATAL,'read_param_int: read error for integer variable '//trim(varname)// &
' parsing "'//trim(value_string(1))//'"')
end subroutine read_param_int
!> This subroutine reads the values of an array of integer model parameters from a parameter file.
subroutine read_param_int_array(CS, varname, value, fail_if_missing)
type(param_file_type), intent(in) :: CS !< The control structure for the file_parser module,
!! it is also a structure to parse for run-time parameters
character(len=*), intent(in) :: varname !< The case-sensitive name of the parameter to read
integer, dimension(:), intent(inout) :: value !< The value of the parameter that may be
!! read from the parameter file
logical, optional, intent(in) :: fail_if_missing !< If present and true, a fatal error occurs
!! if this variable is not found in the parameter file
! Local variables
character(len=INPUT_STR_LENGTH) :: value_string(1)
logical :: found, defined
call get_variable_line(CS, varname, found, defined, value_string)
if (found .and. defined .and. (LEN_TRIM(value_string(1)) > 0)) then
read(value_string(1),*,end=991,err=1002) value
991 return
else
if (present(fail_if_missing)) then ; if (fail_if_missing) then
if (.not.found) then
call MOM_error(FATAL,'read_param_int_array: Unable to find variable '//trim(varname)// &
' in any input files.')
else
call MOM_error(FATAL,'read_param_int_array: Variable '//trim(varname)// &
' found but not set in input files.')
endif
endif ; endif
endif
return
1002 call MOM_error(FATAL,'read_param_int_array: read error for integer array '//trim(varname)// &
' parsing "'//trim(value_string(1))//'"')
end subroutine read_param_int_array
!> This subroutine reads the value of a real model parameter from a parameter file.
subroutine read_param_real(CS, varname, value, fail_if_missing, scale)
type(param_file_type), intent(in) :: CS !< The control structure for the file_parser module,
!! it is also a structure to parse for run-time parameters
character(len=*), intent(in) :: varname !< The case-sensitive name of the parameter to read
real, intent(inout) :: value !< The value of the parameter that may be
!! read from the parameter file
logical, optional, intent(in) :: fail_if_missing !< If present and true, a fatal error occurs
!! if this variable is not found in the parameter file
real, optional, intent(in) :: scale !< A scaling factor that the parameter is multiplied
!! by before it is returned.
! Local variables
character(len=INPUT_STR_LENGTH) :: value_string(1)
logical :: found, defined
call get_variable_line(CS, varname, found, defined, value_string)
if (found .and. defined .and. (LEN_TRIM(value_string(1)) > 0)) then
read(value_string(1),*,err=1003) value
if (present(scale)) value = scale*value
else
if (present(fail_if_missing)) then ; if (fail_if_missing) then
if (.not.found) then
call MOM_error(FATAL,'read_param_real: Unable to find variable '//trim(varname)// &
' in any input files.')
else
call MOM_error(FATAL,'read_param_real: Variable '//trim(varname)// &
' found but not set in input files.')
endif
endif ; endif
endif
return
1003 call MOM_error(FATAL,'read_param_real: read error for real variable '//trim(varname)// &
' parsing "'//trim(value_string(1))//'"')
end subroutine read_param_real
!> This subroutine reads the values of an array of real model parameters from a parameter file.
subroutine read_param_real_array(CS, varname, value, fail_if_missing, scale)
type(param_file_type), intent(in) :: CS !< The control structure for the file_parser module,
!! it is also a structure to parse for run-time parameters
character(len=*), intent(in) :: varname !< The case-sensitive name of the parameter to read
real, dimension(:), intent(inout) :: value !< The value of the parameter that may be
!! read from the parameter file
logical, optional, intent(in) :: fail_if_missing !< If present and true, a fatal error occurs
!! if this variable is not found in the parameter file
real, optional, intent(in) :: scale !< A scaling factor that the parameter is multiplied
!! by before it is returned.
! Local variables
character(len=INPUT_STR_LENGTH) :: value_string(1)
logical :: found, defined
call get_variable_line(CS, varname, found, defined, value_string)
if (found .and. defined .and. (LEN_TRIM(value_string(1)) > 0)) then
read(value_string(1),*,end=991,err=1004) value
991 continue
if (present(scale)) value(:) = scale*value(:)
return
else
if (present(fail_if_missing)) then ; if (fail_if_missing) then
if (.not.found) then
call MOM_error(FATAL,'read_param_real_array: Unable to find variable '//trim(varname)// &
' in any input files.')
else
call MOM_error(FATAL,'read_param_real_array: Variable '//trim(varname)// &
' found but not set in input files.')
endif
endif ; endif
endif
return
1004 call MOM_error(FATAL,'read_param_real_array: read error for real array '//trim(varname)// &
' parsing "'//trim(value_string(1))//'"')
end subroutine read_param_real_array
!> This subroutine reads the value of a character string model parameter from a parameter file.
subroutine read_param_char(CS, varname, value, fail_if_missing)
type(param_file_type), intent(in) :: CS !< The control structure for the file_parser module,
!! it is also a structure to parse for run-time parameters
character(len=*), intent(in) :: varname !< The case-sensitive name of the parameter to read
character(len=*), intent(inout) :: value !< The value of the parameter that may be
!! read from the parameter file
logical, optional, intent(in) :: fail_if_missing !< If present and true, a fatal error occurs
!! if this variable is not found in the parameter file
! Local variables
character(len=INPUT_STR_LENGTH) :: value_string(1)
logical :: found, defined
call get_variable_line(CS, varname, found, defined, value_string)
if (found) then
value = trim(strip_quotes(value_string(1)))
elseif (present(fail_if_missing)) then ; if (fail_if_missing) then
call MOM_error(FATAL,'Unable to find variable '//trim(varname)// &
' in any input files.')
endif ; endif
end subroutine read_param_char
!> This subroutine reads the values of an array of character string model parameters from a parameter file.
subroutine read_param_char_array(CS, varname, value, fail_if_missing)
type(param_file_type), intent(in) :: CS !< The control structure for the file_parser module,
!! it is also a structure to parse for run-time parameters
character(len=*), intent(in) :: varname !< The case-sensitive name of the parameter to read
character(len=*), dimension(:), intent(inout) :: value !< The value of the parameter that may be
!! read from the parameter file
logical, optional, intent(in) :: fail_if_missing !< If present and true, a fatal error occurs
!! if this variable is not found in the parameter file
! Local variables
character(len=INPUT_STR_LENGTH) :: value_string(1), loc_string
logical :: found, defined
integer :: i, i_out
call get_variable_line(CS, varname, found, defined, value_string)
if (found) then
loc_string = trim(value_string(1))
i = index(loc_string,",")
i_out = 1
do while(i>0)
value(i_out) = trim(strip_quotes(loc_string(:i-1)))
i_out = i_out+1
loc_string = trim(adjustl(loc_string(i+1:)))
i = index(loc_string,",")
enddo
if (len_trim(loc_string)>0) then
value(i_out) = trim(strip_quotes(adjustl(loc_string)))
i_out = i_out+1
endif
do i=i_out,SIZE(value) ; value(i) = " " ; enddo
elseif (present(fail_if_missing)) then ; if (fail_if_missing) then
call MOM_error(FATAL,'Unable to find variable '//trim(varname)// &
' in any input files.')
endif ; endif
end subroutine read_param_char_array
!> This subroutine reads the value of a logical model parameter from a parameter file.
subroutine read_param_logical(CS, varname, value, fail_if_missing)
type(param_file_type), intent(in) :: CS !< The control structure for the file_parser module,
!! it is also a structure to parse for run-time parameters
character(len=*), intent(in) :: varname !< The case-sensitive name of the parameter to read
logical, intent(inout) :: value !< The value of the parameter that may be
!! read from the parameter file
logical, optional, intent(in) :: fail_if_missing !< If present and true, a fatal error occurs
!! if this variable is not found in the parameter file
! Local variables
character(len=INPUT_STR_LENGTH) :: value_string(1)
logical :: found, defined
call get_variable_line(CS, varname, found, defined, value_string, paramIsLogical=.true.)
if (found) then
value = defined
elseif (present(fail_if_missing)) then ; if (fail_if_missing) then
call MOM_error(FATAL,'Unable to find variable '//trim(varname)// &
' in any input files.')
endif ; endif
end subroutine read_param_logical
!> This function removes single and double quotes from a character string
function strip_quotes(val_str)
character(len=*) :: val_str !< The character string to work on
character(len=INPUT_STR_LENGTH) :: strip_quotes
! Local variables
integer :: i
strip_quotes = val_str
i = index(strip_quotes,ACHAR(34)) ! Double quote
do while (i>0)
if (i > 1) then ; strip_quotes = strip_quotes(:i-1)//strip_quotes(i+1:)
else ; strip_quotes = strip_quotes(2:) ; endif
i = index(strip_quotes,ACHAR(34)) ! Double quote
enddo
i = index(strip_quotes,ACHAR(39)) ! Single quote
do while (i>0)
if (i > 1) then ; strip_quotes = strip_quotes(:i-1)//strip_quotes(i+1:)
else ; strip_quotes = strip_quotes(2:) ; endif
i = index(strip_quotes,ACHAR(39)) ! Single quote
enddo
end function strip_quotes
!> This subtoutine extracts the contents of lines in the param_file_type that refer to
!! a named parameter. The value_string that is returned must be interepreted in a way
!! that depends on the type of this variable.
subroutine get_variable_line(CS, varname, found, defined, value_string, paramIsLogical)
type(param_file_type), intent(in) :: CS !< The control structure for the file_parser module,
!! it is also a structure to parse for run-time parameters
character(len=*), intent(in) :: varname !< The case-sensitive name of the parameter to read
logical, intent(out) :: found !< If true, this parameter has been found in CS
logical, intent(out) :: defined !< If true, this parameter is set (or true) in the CS
character(len=*), intent(out) :: value_string(:) !< A string that encodes the new value
logical, optional, intent(in) :: paramIsLogical !< If true, this is a logical parameter
!! that can be simply defined without parsing a value_string.
! Local variables
character(len=INPUT_STR_LENGTH) :: val_str, lname, origLine
character(len=INPUT_STR_LENGTH) :: line, continuationBuffer, blockName
character(len=FILENAME_LENGTH) :: filename
integer :: is, id, isd, isu, ise, iso, verbose, ipf
integer :: last, last1, ival, oval, max_vals, count, contBufSize
character(len=52) :: set
logical :: found_override, found_equals
logical :: found_define, found_undef
logical :: force_cycle, defined_in_line, continuedLine
logical :: variableKindIsLogical, valueIsSame
logical :: inWrongBlock, fullPathParameter
logical, parameter :: requireNamedClose = .false.
set = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
continuationBuffer = repeat(" ",INPUT_STR_LENGTH)
contBufSize = 0
verbose = 1
variableKindIsLogical=.false.
if (present(paramIsLogical)) variableKindIsLogical = paramIsLogical
! Find the first instance (if any) where the named variable is found, and
! return variables indicating whether this variable is defined and the string
! that contains the value of this variable.
found = .false.
oval = 0; ival = 0
max_vals = SIZE(value_string)
do is=1,max_vals ; value_string(is) = " " ; enddo
paramfile_loop: do ipf = 1, CS%nfiles
filename = CS%filename(ipf)
continuedLine = .false.
blockName = ''
! Scan through each line of the file
do count = 1, CS%param_data(ipf)%num_lines
line = CS%param_data(ipf)%line(count)
last = len_trim(line)
last1 = max(1,last)
! Check if line ends in continuation character (either & or \)
! Note achar(92) is a backslash
if (line(last1:last1) == achar(92).or.line(last1:last1) == "&") then
continuationBuffer(contBufSize+1:contBufSize+len_trim(line))=line(:last-1)
contBufSize=contBufSize + len_trim(line)-1
continuedLine = .true.
if (count==CS%param_data(ipf)%num_lines) &
call MOM_error(FATAL, "MOM_file_parser : the last line"// &
" of the file ends in a continuation character but"// &
" there are no more lines to read. "// &
" Line: '"//trim(line(:last))//"'"//&
" in file "//trim(filename)//".")
cycle ! cycle inorder to append the next line of the file
elseif (continuedLine) then
! If we reached this point then this is the end of line continuation
continuationBuffer(contBufSize+1:contBufSize+len_trim(line))=line(:last)
line = continuationBuffer
continuationBuffer=repeat(" ",INPUT_STR_LENGTH) ! Clear for next use
contBufSize = 0
continuedLine = .false.
last = len_trim(line)
endif
origLine = trim(line) ! Keep original for error messages
! Check for '#override' at start of line
found_override = .false.; found_define = .false.; found_undef = .false.
iso = index(line(:last), "#override " )!; if (is > 0) found_override = .true.
if (iso>1) call MOM_error(FATAL, "MOM_file_parser : #override was found "// &
" but was not the first keyword."// &
" Line: '"//trim(line(:last))//"'"//&
" in file "//trim(filename)//".")
if (iso==1) then
found_override = .true.
if (index(line(:last), "#override define ")==1) found_define = .true.
if (index(line(:last), "#override undef ")==1) found_undef = .true.
line = trim(adjustl(line(iso+10:last))); last = len_trim(line)
endif
! Check for start of fortran namelist, ie. '&namelist'
if (index(line(:last),'&')==1) then
iso=index(line(:last),' ')
if (iso>0) then ! possibly simething else on this line
blockName = pushBlockLevel(blockName,line(2:iso-1))
line=trim(adjustl(line(iso:last)))
last=len_trim(line)
if (last==0) cycle ! nothing else on this line
else ! just the namelist on this line
if (len_trim(blockName)>0) then
blockName = trim(blockName) // '%' //trim(line(2:last))
else
blockName = trim(line(2:last))
endif
call flag_line_as_read(CS%param_data(ipf)%line_used,count)
cycle
endif
endif
! Newer form of parameter block, block%, %block or block%param or
iso=index(line(:last),'%')
fullPathParameter = .false.
if (iso==1) then ! % is first character means this is a close
if (len_trim(blockName)==0 ) call MOM_error(FATAL, &
'get_variable_line: An extra close block was encountered. Line="'// &
trim(line(:last))//'"' )
if (last>1 .and. trim(blockName)/=trim(line(2:last)) ) &
call MOM_error(FATAL, 'get_variable_line: A named close for a parameter'// &
' block did not match the open block. Line="'//trim(line(:last))//'"' )
if (last==1 .and. requireNamedClose) & ! line = '%' is a generic (unnamed) close
call MOM_error(FATAL, 'get_variable_line: A named close for a parameter'// &
' block is required but found "%". Block="'//trim(blockName)//'"' )
blockName = popBlockLevel(blockName)
call flag_line_as_read(CS%param_data(ipf)%line_used,count)
elseif (iso==last) then ! This is a new block if % is last character
blockName = pushBlockLevel(blockName, line(:iso-1))
call flag_line_as_read(CS%param_data(ipf)%line_used,count)
else ! This is of the form block%parameter = ... (full path parameter)
iso=index(line(:last),'%',.true.)
! Check that the parameter block names on the line matches the state set by the caller
if (iso>0 .and. trim(CS%blockName%name)==trim(line(:iso-1))) then
fullPathParameter = .true.
line = trim(line(iso+1:last)) ! Strip away the block name for subsequent processing
last = len_trim(line)
endif
endif
! We should only interpret this line if this block is the active block
inWrongBlock = .false.
if (len_trim(blockName)>0) then ! In a namelist block in file
if (trim(CS%blockName%name)/=trim(blockName)) inWrongBlock = .true. ! Not in the required block
endif
if (len_trim(CS%blockName%name)>0) then ! In a namelist block in the model
if (trim(CS%blockName%name)/=trim(blockName)) inWrongBlock = .true. ! Not in the required block
endif
! Check for termination of a fortran namelist (with a '/')
if (line(last:last)=='/') then
if (len_trim(blockName)==0) call MOM_error(FATAL, &
'get_variable_line: An extra namelist/block end was encountered. Line="'// &
trim(line(:last))//'"' )
blockName = popBlockLevel(blockName)
last = last - 1 ! Ignore the termination character from here on
endif
if (inWrongBlock .and. .not. fullPathParameter) then
if (index(" "//line(:last+1), " "//trim(varname)//" ")>0) &
call MOM_error(WARNING,"MOM_file_parser : "//trim(varname)// &
' found outside of block '//trim(CS%blockName%name)//'%. Ignoring.')
cycle
endif
! Determine whether this line mentions the named parameter or not
if (index(" "//line(:last)//" ", " "//trim(varname)//" ") == 0) cycle
! Detect keywords
found_equals = .false.
isd = index(line(:last), "define" )!; if (isd > 0) found_define = .true.
isu = index(line(:last), "undef" )!; if (isu > 0) found_undef = .true.
ise = index(line(:last), " = " ); if (ise > 1) found_equals = .true.
if (index(line(:last), "#define ")==1) found_define = .true.
if (index(line(:last), "#undef ")==1) found_undef = .true.
! Check for missing, mutually exclusive or incomplete keywords
if (.true.) then
if (.not. (found_define .or. found_undef .or. found_equals)) &
call MOM_error(FATAL, "MOM_file_parser : the parameter name '"// &
trim(varname)//"' was found without define or undef."// &
" Line: '"//trim(line(:last))//"'"//&
" in file "//trim(filename)//".")