@@ -16,13 +16,14 @@ module MOM_file_parser
16
16
17
17
implicit none ; private
18
18
19
+ ! These are hard-coded limits that are used in the following code. They should be set
20
+ ! generously enough not to impose any significant limitations.
19
21
integer , parameter , public :: MAX_PARAM_FILES = 5 ! < Maximum number of parameter files.
20
- integer , parameter :: INPUT_STR_LENGTH = 320 ! < Maximum line length in parameter file.
21
- integer , parameter :: FILENAME_LENGTH = 200 ! < Maximum number of characters in file names.
22
+ integer , parameter :: INPUT_STR_LENGTH = 1024 ! < Maximum line length in parameter file. Lines that
23
+ ! ! are combined by ending in '\' or '&' can exceed
24
+ ! ! this limit after merging.
25
+ integer , parameter :: FILENAME_LENGTH = 200 ! < Maximum number of characters in file names.
22
26
23
- ! The all_PEs_read option should be eliminated with post-riga shared code.
24
- logical :: all_PEs_read = .false. ! < If true, all PEs read the input files
25
- ! ! TODO: Eliminate this parameter
26
27
27
28
! >@{ Default values for parameters
28
29
logical , parameter :: report_unused_default = .true.
@@ -138,7 +139,6 @@ subroutine open_param_file(filename, CS, checkable, component, doc_file_dir)
138
139
logical :: file_exists, unit_in_use, Netcdf_file, may_check, reopened_file
139
140
integer :: ios, iounit, strlen, i
140
141
character (len= 240 ) :: doc_path
141
- character (len= 16 ) :: sub_string
142
142
type (parameter_block), pointer :: block = > NULL ()
143
143
144
144
may_check = .true. ; if (present (checkable)) may_check = checkable
@@ -183,11 +183,10 @@ subroutine open_param_file(filename, CS, checkable, component, doc_file_dir)
183
183
if (Netcdf_file) &
184
184
call MOM_error(FATAL," open_param_file: NetCDF files are not yet supported." )
185
185
186
- if (all_PEs_read .or. is_root_pe()) then
186
+ if (is_root_pe()) then
187
187
open (newunit= iounit, file= trim (filename), access= ' SEQUENTIAL' , &
188
188
form= ' FORMATTED' , action= ' READ' , position= ' REWIND' , iostat= ios)
189
- if (ios /= 0 ) call MOM_error(FATAL, " open_param_file: Error opening '" // &
190
- trim (filename)// " '." )
189
+ if (ios /= 0 ) call MOM_error(FATAL, " open_param_file: Error opening '" // trim (filename)// " '." )
191
190
else
192
191
iounit = 1
193
192
endif
@@ -202,15 +201,11 @@ subroutine open_param_file(filename, CS, checkable, component, doc_file_dir)
202
201
if (associated (CS% blockName)) deallocate (CS% blockName)
203
202
allocate (block) ; block% name = ' ' ; CS% blockName = > block
204
203
205
- call MOM_mesg(" open_param_file: " // trim (filename)// &
206
- " has been opened successfully." , 5 )
204
+ call MOM_mesg(" open_param_file: " // trim (filename)// " has been opened successfully." , 5 )
207
205
208
206
call populate_param_data(iounit, filename, CS% param_data(i))
209
207
! Increment the maximum line length, but always report values in blocks of 4 characters.
210
208
CS% max_line_len = max (CS% max_line_len, 4 + 4 * (max_input_line_length(CS, i) - 1 ) / 4 )
211
- write (sub_string, ' (i4.4)' ) CS% max_line_len
212
- call MOM_mesg(" open_param_file: Maximum input line length determined to be " // &
213
- trim (adjustl (sub_string))// " characters." , 5 )
214
209
215
210
call read_param(CS," SEND_LOG_TO_STDOUT" ,CS% log_to_stdout)
216
211
call read_param(CS," REPORT_UNUSED_PARAMS" ,CS% report_unused)
@@ -264,7 +259,7 @@ subroutine close_param_file(CS, quiet_close, component)
264
259
265
260
if (present (quiet_close)) then ; if (quiet_close) then
266
261
do i = 1 , CS% nfiles
267
- if (all_PEs_read .or. is_root_pe()) close (CS% iounit(i))
262
+ if (is_root_pe()) close (CS% iounit(i))
268
263
call MOM_mesg(" close_param_file: " // trim (CS% filename(i))// &
269
264
" has been closed successfully." , 5 )
270
265
CS% iounit(i) = - 1
@@ -334,9 +329,8 @@ subroutine close_param_file(CS, quiet_close, component)
334
329
enddo
335
330
endif
336
331
337
- if (all_PEs_read .or. is_root_pe()) close (CS% iounit(i))
338
- call MOM_mesg(" close_param_file: " // trim (CS% filename(i))// &
339
- " has been closed successfully." , 5 )
332
+ if (is_root_pe()) close (CS% iounit(i))
333
+ call MOM_mesg(" close_param_file: " // trim (CS% filename(i))// " has been closed successfully." , 5 )
340
334
CS% iounit(i) = - 1
341
335
CS% filename(i) = ' '
342
336
CS% NetCDF_file(i) = .false.
@@ -369,11 +363,8 @@ subroutine populate_param_data(iounit, filename, param_data)
369
363
integer :: n, num_lines, total_chars, ch, rsc, llen, int_buf(2 )
370
364
logical :: inMultiLineComment
371
365
372
- character (len= 80 ) :: frag1, frag2
373
-
374
-
375
366
! Find the number of keyword lines in a parameter file
376
- if (all_PEs_read .or. is_root_pe()) then
367
+ if (is_root_pe()) then
377
368
! rewind the parameter file
378
369
rewind(iounit)
379
370
@@ -408,19 +399,17 @@ subroutine populate_param_data(iounit, filename, param_data)
408
399
endif ! (is_root_pe())
409
400
410
401
! Broadcast the number of valid entries in parameter file
411
- if (.not. all_PEs_read) then
412
- call broadcast(int_buf, 2 , root_pe())
413
- num_lines = int_buf(1 )
414
- total_chars = int_buf(2 )
415
- endif
402
+ call broadcast(int_buf, 2 , root_pe())
403
+ num_lines = int_buf(1 )
404
+ total_chars = int_buf(2 )
416
405
417
406
! Set up the space for storing the actual lines.
418
407
param_data% num_lines = num_lines
419
408
allocate (line_len(num_lines), source= 0 )
420
409
allocate (char_buf(total_chars), source= " " )
421
410
422
411
! Read the actual lines.
423
- if (all_PEs_read .or. is_root_pe()) then
412
+ if (is_root_pe()) then
424
413
! rewind the parameter file
425
414
rewind(iounit)
426
415
@@ -435,6 +424,10 @@ subroutine populate_param_data(iounit, filename, param_data)
435
424
else
436
425
if (lastNonCommentNonBlank(line)>0 ) then
437
426
line = removeComments(line)
427
+ if ((len_trim (line) > 1000 ) .and. is_root_PE()) then
428
+ call MOM_error(WARNING, " MOM_file_parser: Consider using continuation to split up " // &
429
+ " the excessivley long parameter input line " // trim (line))
430
+ endif
438
431
line = simplifyWhiteSpace(line(:len_trim (line)))
439
432
num_lines = num_lines + 1
440
433
llen = len_trim (line)
@@ -453,10 +446,8 @@ subroutine populate_param_data(iounit, filename, param_data)
453
446
endif ! (is_root_pe())
454
447
455
448
! Broadcast the populated arrays line_len and char_buf
456
- if (.not. all_PEs_read) then
457
- call broadcast(line_len, num_lines, root_pe())
458
- call broadcast(char_buf(1 :total_chars), 1 , root_pe())
459
- endif
449
+ call broadcast(line_len, num_lines, root_pe())
450
+ call broadcast(char_buf(1 :total_chars), 1 , root_pe())
460
451
461
452
! Allocate space to hold contents of the parameter file, including the lines in param_data%fln
462
453
allocate (param_data% fln(num_lines))
@@ -767,8 +758,7 @@ subroutine read_param_char(CS, varname, value, fail_if_missing)
767
758
if (found) then
768
759
value = trim (strip_quotes(value_string(1 )))
769
760
elseif (present (fail_if_missing)) then ; if (fail_if_missing) then
770
- call MOM_error(FATAL,' Unable to find variable ' // trim (varname)// &
771
- ' in any input files.' )
761
+ call MOM_error(FATAL, ' Unable to find variable ' // trim (varname)// ' in any input files.' )
772
762
endif ; endif
773
763
774
764
end subroutine read_param_char
@@ -805,8 +795,7 @@ subroutine read_param_char_array(CS, varname, value, fail_if_missing)
805
795
endif
806
796
do i= i_out,SIZE (value) ; value(i) = " " ; enddo
807
797
elseif (present (fail_if_missing)) then ; if (fail_if_missing) then
808
- call MOM_error(FATAL,' Unable to find variable ' // trim (varname)// &
809
- ' in any input files.' )
798
+ call MOM_error(FATAL, ' Unable to find variable ' // trim (varname)// ' in any input files.' )
810
799
endif ; endif
811
800
812
801
end subroutine read_param_char_array
@@ -829,8 +818,7 @@ subroutine read_param_logical(CS, varname, value, fail_if_missing)
829
818
if (found) then
830
819
value = defined
831
820
elseif (present (fail_if_missing)) then ; if (fail_if_missing) then
832
- call MOM_error(FATAL,' Unable to find variable ' // trim (varname)// &
833
- ' in any input files.' )
821
+ call MOM_error(FATAL, ' Unable to find variable ' // trim (varname)// ' in any input files.' )
834
822
endif ; endif
835
823
end subroutine read_param_logical
836
824
@@ -891,11 +879,9 @@ subroutine read_param_time(CS, varname, value, timeunit, fail_if_missing, date_f
891
879
else
892
880
if (present (fail_if_missing)) then ; if (fail_if_missing) then
893
881
if (.not. found) then
894
- call MOM_error(FATAL,' Unable to find variable ' // trim (varname)// &
895
- ' in any input files.' )
882
+ call MOM_error(FATAL, ' Unable to find variable ' // trim (varname)// ' in any input files.' )
896
883
else
897
- call MOM_error(FATAL,' Variable ' // trim (varname)// &
898
- ' found but not set in input files.' )
884
+ call MOM_error(FATAL, ' Variable ' // trim (varname)// ' found but not set in input files.' )
899
885
endif
900
886
endif ; endif
901
887
endif
@@ -907,7 +893,7 @@ end subroutine read_param_time
907
893
908
894
! > This function removes single and double quotes from a character string
909
895
function strip_quotes (val_str )
910
- character (len=* ) :: val_str ! < The character string to work on
896
+ character (len=* ), intent ( in ) :: val_str ! < The character string to work on
911
897
character (len= len (val_str)) :: strip_quotes
912
898
! Local variables
913
899
integer :: i
@@ -939,7 +925,6 @@ function max_input_line_length(CS, pf_num) result(max_len)
939
925
940
926
! Local variables
941
927
character (len= FILENAME_LENGTH) :: filename
942
- ! character(len=:), allocatable :: line
943
928
character :: last_char
944
929
integer :: ipf, ipf_s, ipf_e
945
930
integer :: last, last1, line_len, count, contBufSize
@@ -1003,7 +988,8 @@ subroutine get_variable_line(CS, varname, found, defined, value_string, paramIsL
1003
988
1004
989
! Local variables
1005
990
character (len= CS% max_line_len) :: val_str, lname, origLine
1006
- character (len= CS% max_line_len) :: line, continuationBuffer, blockName
991
+ character (len= CS% max_line_len) :: line, continuationBuffer
992
+ character (len= 240 ) :: blockName
1007
993
character (len= FILENAME_LENGTH) :: filename
1008
994
integer :: is, id, isd, isu, ise, iso, ipf
1009
995
integer :: last, last1, ival, oval, max_vals, count, contBufSize
@@ -1407,7 +1393,7 @@ subroutine log_param_int_array(CS, modulename, varname, value, desc, &
1407
1393
logical , optional , intent (in ) :: like_default ! < If present and true, log this parameter as
1408
1394
! ! though it has the default value, even if there is no default.
1409
1395
1410
- character (len= 1320 ) :: mesg
1396
+ character (len= CS % max_line_len +120 ) :: mesg
1411
1397
character (len= 240 ) :: myunits
1412
1398
1413
1399
write (mesg, ' (" ",a," ",a,": ",A)' ) trim (modulename), trim (varname), trim (left_ints(value))
@@ -1549,16 +1535,16 @@ subroutine log_param_char(CS, modulename, varname, value, desc, units, &
1549
1535
logical , optional , intent (in ) :: like_default ! < If present and true, log this parameter as
1550
1536
! ! though it has the default value, even if there is no default.
1551
1537
1552
- character (len= 1024 ) :: mesg, myunits
1538
+ character (len= :), allocatable :: mesg
1539
+ character (len= 240 ) :: myunits
1553
1540
1554
- write (mesg, ' (" ",a," ",a,": ",a)' ) &
1555
- trim (modulename), trim (varname), trim (value)
1541
+ mesg = " " // trim (modulename) // " " // trim (varname) // " : " // trim (value)
1556
1542
if (is_root_pe()) then
1557
1543
if (CS% log_open) write (CS% stdlog,' (a)' ) trim (mesg)
1558
1544
if (CS% log_to_stdout) write (CS% stdout,' (a)' ) trim (mesg)
1559
1545
endif
1560
1546
1561
- myunits= " " ; if (present (units)) write (myunits(1 :1024 ),' (A)' ) trim (units)
1547
+ myunits= " " ; if (present (units)) write (myunits(1 :240 ),' (A)' ) trim (units)
1562
1548
if (present (desc)) &
1563
1549
call doc_param(CS% doc, varname, desc, myunits, value, default, &
1564
1550
layoutParam= layoutParam, debuggingParam= debuggingParam, like_default= like_default)
@@ -1936,7 +1922,7 @@ subroutine get_param_char_array(CS, modulename, varname, value, desc, units, &
1936
1922
! Local variables
1937
1923
logical :: do_read, do_log
1938
1924
integer :: i, len_tot, len_val
1939
- character (len= 1024 ) :: cat_val
1925
+ character (len= :), allocatable :: cat_val
1940
1926
1941
1927
do_read = .true. ; if (present (do_not_read)) do_read = .not. do_not_read
1942
1928
do_log = .true. ; if (present (do_not_log)) do_log = .not. do_not_log
@@ -1947,7 +1933,7 @@ subroutine get_param_char_array(CS, modulename, varname, value, desc, units, &
1947
1933
endif
1948
1934
1949
1935
if (do_log) then
1950
- cat_val = trim (value(1 )); len_tot = len_trim (value(1 ))
1936
+ cat_val = trim (value(1 )) ; len_tot = len_trim (value(1 ))
1951
1937
do i= 2 ,size (value)
1952
1938
len_val = len_trim (value(i))
1953
1939
if ((len_val > 0 ) .and. (len_tot + len_val + 2 < 240 )) then
0 commit comments