MOM6
mom_string_functions Module Reference

Functions/Subroutines

character(len=len(input_string)) function, public lowercase (input_string)
 
character(len=len(input_string)) function, public uppercase (input_string)
 
character(len=19) function, public left_int (i)
 
character(len=1320) function, public left_ints (i)
 
character(len=32) function, public left_real (val)
 
character(len=1320) function, public left_reals (r, sep)
 
logical function isformattedfloatequalto (str, val)
 
character(len=120) function, public extractword (string, n)
 Returns the string corresponding to the nth word in the argument or "" if the string is not long enough. Both spaces and commas are interpreted as separators. More...
 
character(len=120) function, public extract_word (string, separators, n)
 Returns the string corresponding to the nth word in the argument or "" if the string is not long enough. Words are delineated by the mandatory separators argument. More...
 
integer function, public extract_integer (string, separators, n, missing_value)
 Returns the integer corresponding to the nth word in the argument. More...
 
real function, public extract_real (string, separators, n, missing_value)
 Returns the real corresponding to the nth word in the argument. More...
 
character(len=120) function, public remove_spaces (string)
 Returns string with all spaces removed. More...
 
logical function, public string_functions_unit_tests (verbose)
 Returns true if a unit test of string_functions fails. More...
 
logical function localtests (verbose, str1, str2)
 True if str1 does not match str2. False otherwise. More...
 
logical function localtesti (verbose, i1, i2)
 True if i1 is not equal to i2. False otherwise. More...
 
logical function localtestr (verbose, r1, r2)
 True if r1 is not equal to r2. False otherwise. More...
 
character(len=len(dir)+2) function, public slasher (dir)
 Returns a directory name that is terminated with a "/" or "./" if the argument is an empty string. More...
 

Function/Subroutine Documentation

◆ extract_integer()

integer function, public mom_string_functions::extract_integer ( character(len=*), intent(in)  string,
character(len=*), intent(in)  separators,
integer, intent(in)  n,
integer, intent(in), optional  missing_value 
)

Returns the integer corresponding to the nth word in the argument.

Parameters
[in]stringString to scan
[in]separatorsCharacters to use for delineation
[in]nNumber of word to extract
[in]missing_valueValue to assign if word is missing

Definition at line 265 of file MOM_string_functions.F90.

References extract_word().

Referenced by mom_regridding::initialize_regridding(), and string_functions_unit_tests().

265  character(len=*), intent(in) :: string !< String to scan
266  character(len=*), intent(in) :: separators !< Characters to use for delineation
267  integer, intent(in) :: n !< Number of word to extract
268  integer, optional, intent(in) :: missing_value !< Value to assign if word is missing
269  ! Local variables
270  integer :: ns, i, b, e, nw
271  character(len=20) :: word
272 
273  word = extract_word(string, separators, n)
274 
275  if (len_trim(word)>0) then
276  read(word(1:len_trim(word)),*) extract_integer
277  else
278  if (present(missing_value)) then
279  extract_integer = missing_value
280  else
281  extract_integer = 0
282  endif
283  endif
284 
Here is the call graph for this function:
Here is the caller graph for this function:

◆ extract_real()

real function, public mom_string_functions::extract_real ( character(len=*), intent(in)  string,
character(len=*), intent(in)  separators,
integer, intent(in)  n,
real, intent(in), optional  missing_value 
)

Returns the real corresponding to the nth word in the argument.

Parameters
[in]stringString to scan
[in]separatorsCharacters to use for delineation
[in]nNumber of word to extract
[in]missing_valueValue to assign if word is missing

Definition at line 289 of file MOM_string_functions.F90.

References extract_word().

Referenced by mom_regridding::initialize_regridding(), and string_functions_unit_tests().

289  character(len=*), intent(in) :: string !< String to scan
290  character(len=*), intent(in) :: separators !< Characters to use for delineation
291  integer, intent(in) :: n !< Number of word to extract
292  real, optional, intent(in) :: missing_value !< Value to assign if word is missing
293  ! Local variables
294  integer :: ns, i, b, e, nw
295  character(len=20) :: word
296 
297  word = extract_word(string, separators, n)
298 
299  if (len_trim(word)>0) then
300  read(word(1:len_trim(word)),*) extract_real
301  else
302  if (present(missing_value)) then
303  extract_real = missing_value
304  else
305  extract_real = 0
306  endif
307  endif
308 
Here is the call graph for this function:
Here is the caller graph for this function:

◆ extract_word()

character(len=120) function, public mom_string_functions::extract_word ( character(len=*), intent(in)  string,
character(len=*), intent(in)  separators,
integer, intent(in)  n 
)

Returns the string corresponding to the nth word in the argument or "" if the string is not long enough. Words are delineated by the mandatory separators argument.

Parameters
[in]stringString to scan
[in]separatorsCharacters to use for delineation
[in]nNumber of word to extract

Definition at line 228 of file MOM_string_functions.F90.

Referenced by extract_integer(), extract_real(), extractword(), mom_open_boundary::parse_segment_data_str(), mom_open_boundary::parse_segment_str(), and string_functions_unit_tests().

228  character(len=*), intent(in) :: string !< String to scan
229  character(len=*), intent(in) :: separators !< Characters to use for delineation
230  integer, intent(in) :: n !< Number of word to extract
231  ! Local variables
232  integer :: ns, i, b, e, nw
233  logical :: lastcharisseperator
234  extract_word = ''
235  lastcharisseperator = .true.
236  ns = len_trim(string)
237  i = 0; b=0; e=0; nw=0;
238  do while (i<ns)
239  i = i+1
240  if (lastcharisseperator) then ! search for end of word
241  if (verify(string(i:i),separators)==0) then
242  continue ! Multiple separators
243  else
244  lastcharisseperator = .false. ! character is beginning of word
245  b = i
246  continue
247  endif
248  else ! continue search for end of word
249  if (verify(string(i:i),separators)==0) then
250  lastcharisseperator = .true.
251  e = i-1 ! Previous character is end of word
252  nw = nw+1
253  if (nw==n) then
254  extract_word = trim(string(b:e))
255  return
256  endif
257  endif
258  endif
259  enddo
260  if (b<=ns .and. nw==n-1) extract_word = trim(string(b:ns))
Here is the caller graph for this function:

◆ extractword()

character(len=120) function, public mom_string_functions::extractword ( character(len=*), intent(in)  string,
integer, intent(in)  n 
)

Returns the string corresponding to the nth word in the argument or "" if the string is not long enough. Both spaces and commas are interpreted as separators.

Definition at line 217 of file MOM_string_functions.F90.

References extract_word().

Referenced by mom_diag_remap::diag_remap_init(), mom_regridding::initialize_regridding(), and string_functions_unit_tests().

217  character(len=*), intent(in) :: string
218  integer, intent(in) :: n
219 
220  extractword = extract_word(string, ' ,', n)
221 
Here is the call graph for this function:
Here is the caller graph for this function:

◆ isformattedfloatequalto()

logical function mom_string_functions::isformattedfloatequalto ( character(len=*), intent(in)  str,
real, intent(in)  val 
)
private

Definition at line 199 of file MOM_string_functions.F90.

Referenced by left_real().

199 ! Returns True if the string can be read/parsed to give the exact
200 ! value of "val"
201  character(len=*), intent(in) :: str
202  real, intent(in) :: val
203  logical :: isformattedfloatequalto
204  ! Local variables
205  real :: scannedval
206 
207  isformattedfloatequalto=.false.
208  read(str(1:),*,err=987) scannedval
209  if (scannedval == val) isformattedfloatequalto=.true.
210  987 return
Here is the caller graph for this function:

◆ left_int()

character(len=19) function, public mom_string_functions::left_int ( integer, intent(in)  i)

Definition at line 83 of file MOM_string_functions.F90.

Referenced by left_ints(), left_reals(), and string_functions_unit_tests().

83 ! Returns a character string of a left-formatted integer
84 ! e.g. "123 " (assumes 19 digit maximum)
85  ! Arguments
86  character(len=19) :: left_int
87  integer, intent(in) :: i
88  ! Local variables
89  character(len=19) :: tmp
90  write(tmp(1:19),'(I19)') i
91  write(left_int(1:19),'(A)') adjustl(tmp)
Here is the caller graph for this function:

◆ left_ints()

character(len=1320) function, public mom_string_functions::left_ints ( integer, dimension(:), intent(in)  i)

Definition at line 95 of file MOM_string_functions.F90.

References left_int().

Referenced by string_functions_unit_tests().

95 ! Returns a character string of a comma-separated, compact formatted,
96 ! integers e.g. "1, 2, 3, 4"
97  ! Arguments
98  character(len=1320) :: left_ints
99  integer, intent(in) :: i(:)
100  ! Local variables
101  character(len=1320) :: tmp
102  integer :: j
103  write(left_ints(1:1320),'(A)') trim(left_int(i(1)))
104  if (size(i)>1) then
105  do j=2,size(i)
106  tmp=left_ints
107  write(left_ints(1:1320),'(A,", ",A)') trim(tmp),trim(left_int(i(j)))
108  enddo
109  endif
Here is the call graph for this function:
Here is the caller graph for this function:

◆ left_real()

character(len=32) function, public mom_string_functions::left_real ( real, intent(in)  val)

Definition at line 113 of file MOM_string_functions.F90.

References isformattedfloatequalto().

Referenced by left_reals(), mom_file_parser::log_param_real(), and string_functions_unit_tests().

113  real, intent(in) :: val
114  character(len=32) :: left_real
115 ! Returns a left-justified string with a real formatted like '(G)'
116  integer :: l, ind
117 
118  if ((abs(val) < 1.0e4) .and. (abs(val) >= 1.0e-3)) then
119  write(left_real, '(F30.11)') val
120  if (.not.isformattedfloatequalto(left_real,val)) then
121  write(left_real, '(F30.12)') val
122  if (.not.isformattedfloatequalto(left_real,val)) then
123  write(left_real, '(F30.13)') val
124  if (.not.isformattedfloatequalto(left_real,val)) then
125  write(left_real, '(F30.14)') val
126  if (.not.isformattedfloatequalto(left_real,val)) then
127  write(left_real, '(F30.15)') val
128  if (.not.isformattedfloatequalto(left_real,val)) then
129  write(left_real, '(F30.16)') val
130  endif
131  endif
132  endif
133  endif
134  endif
135  do
136  l = len_trim(left_real)
137  if ((l<2) .or. (left_real(l-1:l) == ".0") .or. &
138  (left_real(l:l) /= "0")) exit
139  left_real(l:l) = " "
140  enddo
141  elseif (val == 0.) then
142  left_real = "0.0"
143  else
144  write(left_real(1:32), '(ES23.14)') val
145  if (.not.isformattedfloatequalto(left_real,val)) then
146  write(left_real(1:32), '(ES23.15)') val
147  endif
148  do
149  ind = index(left_real,"0E")
150  if (ind == 0) exit
151  if (left_real(ind-1:ind-1) == ".") exit
152  left_real = left_real(1:ind-1)//left_real(ind+1:)
153  enddo
154  endif
155  left_real = adjustl(left_real)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ left_reals()

character(len=1320) function, public mom_string_functions::left_reals ( real, dimension(:), intent(in)  r,
character(len=*), optional  sep 
)

Definition at line 159 of file MOM_string_functions.F90.

References left_int(), and left_real().

Referenced by mom_file_parser::log_param_real_array(), and string_functions_unit_tests().

159 ! Returns a character string of a comma-separated, compact formatted, reals
160 ! e.g. "1., 2., 5*3., 5.E2"
161  ! Arguments
162  character(len=1320) :: left_reals
163  real, intent(in) :: r(:)
164  character(len=*), optional :: sep
165  ! Local variables
166  integer :: j, n, b, ns
167  logical :: dowrite
168  character(len=10) :: separator
169  n=1 ; dowrite=.true. ; left_reals='' ; b=1
170  if (present(sep)) then
171  separator=sep ; ns=len(sep)
172  else
173  separator=', ' ; ns=2
174  endif
175  do j=1,size(r)
176  dowrite=.true.
177  if (j<size(r)) then
178  if (r(j)==r(j+1)) then
179  n=n+1
180  dowrite=.false.
181  endif
182  endif
183  if (dowrite) then
184  if (b>1) then ! Write separator if a number has already been written
185  write(left_reals(b:),'(A)') separator
186  b=b+ns
187  endif
188  if (n>1) then
189  write(left_reals(b:),'(A,"*",A)') trim(left_int(n)),trim(left_real(r(j)))
190  else
191  write(left_reals(b:),'(A)') trim(left_real(r(j)))
192  endif
193  n=1 ; b=len_trim(left_reals)+1
194  endif
195  enddo
Here is the call graph for this function:
Here is the caller graph for this function:

◆ localtesti()

logical function mom_string_functions::localtesti ( logical, intent(in)  verbose,
integer, intent(in)  i1,
integer, intent(in)  i2 
)
private

True if i1 is not equal to i2. False otherwise.

Parameters
[in]verboseIf true, write results to stdout
[in]i1Integer
[in]i2Integer

Definition at line 392 of file MOM_string_functions.F90.

Referenced by string_functions_unit_tests().

392  logical, intent(in) :: verbose !< If true, write results to stdout
393  integer, intent(in) :: i1 !< Integer
394  integer, intent(in) :: i2 !< Integer
395  localtesti=.false.
396  if (i1/=i2) localtesti=.true.
397  if (localtesti .or. verbose) then
398  write(*,*) i1,i2
399  if (localtesti) write(*,*) i1,'!=',i2, '<-- FAIL'
400  endif
Here is the caller graph for this function:

◆ localtestr()

logical function mom_string_functions::localtestr ( logical, intent(in)  verbose,
real, intent(in)  r1,
real, intent(in)  r2 
)
private

True if r1 is not equal to r2. False otherwise.

Parameters
[in]verboseIf true, write results to stdout
[in]r1Float
[in]r2Float

Definition at line 405 of file MOM_string_functions.F90.

Referenced by string_functions_unit_tests().

405  logical, intent(in) :: verbose !< If true, write results to stdout
406  real, intent(in) :: r1 !< Float
407  real, intent(in) :: r2 !< Float
408  localtestr=.false.
409  if (r1/=r2) localtestr=.true.
410  if (localtestr .or. verbose) then
411  write(*,*) r1,r2
412  if (localtestr) write(*,*) r1,'!=',r2, '<-- FAIL'
413  endif
Here is the caller graph for this function:

◆ localtests()

logical function mom_string_functions::localtests ( logical, intent(in)  verbose,
character(len=*), intent(in)  str1,
character(len=*), intent(in)  str2 
)
private

True if str1 does not match str2. False otherwise.

Parameters
[in]verboseIf true, write results to stdout
[in]str1String
[in]str2String

Definition at line 379 of file MOM_string_functions.F90.

Referenced by string_functions_unit_tests().

379  logical, intent(in) :: verbose !< If true, write results to stdout
380  character(len=*), intent(in) :: str1 !< String
381  character(len=*), intent(in) :: str2 !< String
382  localtests=.false.
383  if (trim(str1)/=trim(str2)) localtests=.true.
384  if (localtests .or. verbose) then
385  write(*,*) '>'//trim(str1)//'<'
386  if (localtests) write(*,*) trim(str1),':',trim(str2), '<-- FAIL'
387  endif
Here is the caller graph for this function:

◆ lowercase()

character(len=len(input_string)) function, public mom_string_functions::lowercase ( character(len=*), intent(in)  input_string)

Definition at line 48 of file MOM_string_functions.F90.

Referenced by mom_diag_remap::diag_remap_configure_axes(), mom_state_initialization::mom_initialize_state(), mom_io::num_timelevels(), and mom_restart::restore_state().

48 ! This function returns a string in which all uppercase letters have been
49 ! replaced by their lowercase counterparts. It is loosely based on the
50 ! lowercase function in mpp_util.F90.
51  ! Arguments
52  character(len=*), intent(in) :: input_string
53  character(len=len(input_string)) :: lowercase
54  ! Local variables
55  integer, parameter :: co=iachar('a')-iachar('A') ! case offset
56  integer :: k
57 
58  lowercase = input_string
59  do k=1, len_trim(input_string)
60  if (lowercase(k:k) >= 'A' .and. lowercase(k:k) <= 'Z') &
61  lowercase(k:k) = achar(ichar(lowercase(k:k))+co)
62  end do
Here is the caller graph for this function:

◆ remove_spaces()

character(len=120) function, public mom_string_functions::remove_spaces ( character(len=*), intent(in)  string)

Returns string with all spaces removed.

Parameters
[in]stringString to scan

Definition at line 313 of file MOM_string_functions.F90.

Referenced by mom_open_boundary::open_boundary_config(), and string_functions_unit_tests().

313  character(len=*), intent(in) :: string !< String to scan
314  ! Local variables
315  integer :: ns, i, o
316  logical :: lastcharisseperator
317  lastcharisseperator = .true.
318  ns = len_trim(string)
319  i = 0; o = 0
320  do while (i<ns)
321  i = i+1
322  if (string(i:i) /= ' ') then ! Copy character to output string
323  o = o + 1
324  remove_spaces(o:o) = string(i:i)
325  endif
326  enddo
327  do i = o+1, 120
328  remove_spaces(i:i) = ' ' ! Wipe any non-empty characters
329  enddo
330  remove_spaces = trim(remove_spaces)
Here is the caller graph for this function:

◆ slasher()

character(len=len(dir)+2) function, public mom_string_functions::slasher ( character(len=*), intent(in)  dir)

Returns a directory name that is terminated with a "/" or "./" if the argument is an empty string.

Parameters
[in]dirA directory to be terminated with a "/" or changed to "./" if it is blank.

Definition at line 419 of file MOM_string_functions.F90.

Referenced by mom_domains::mom_domains_init().

419  character(len=*), intent(in) :: dir !< A directory to be terminated with a "/"
420  !! or changed to "./" if it is blank.
421  character(len=len(dir)+2) :: slasher
422 
423  if (len_trim(dir) == 0) then
424  slasher = "./"
425  elseif (dir(len_trim(dir):len_trim(dir)) == '/') then
426  slasher = trim(dir)
427  else
428  slasher = trim(dir)//"/"
429  endif
Here is the caller graph for this function:

◆ string_functions_unit_tests()

logical function, public mom_string_functions::string_functions_unit_tests ( logical, intent(in)  verbose)

Returns true if a unit test of string_functions fails.

Parameters
[in]verboseIf true, write results to stdout

Definition at line 335 of file MOM_string_functions.F90.

References extract_integer(), extract_real(), extract_word(), extractword(), left_int(), left_ints(), left_real(), left_reals(), localtesti(), localtestr(), localtests(), and remove_spaces().

Referenced by mom_unit_tests::unit_tests().

335  ! Arguments
336  logical, intent(in) :: verbose !< If true, write results to stdout
337  ! Local variables
338  integer :: i(5) = (/ -1, 1, 3, 3, 0 /)
339  real :: r(8) = (/ 0., 1., -2., 1.3, 3.e-11, 3.e-11, 3.e-11, -5.1e12 /)
340  logical :: fail, v
341  fail = .false.
342  v = verbose
343  write(*,*) '==== MOM_string_functions: string_functions_unit_tests ==='
344  fail = fail .or. localtests(v,left_int(-1),'-1')
345  fail = fail .or. localtests(v,left_ints(i(:)),'-1, 1, 3, 3, 0')
346  fail = fail .or. localtests(v,left_real(0.),'0.0')
347  fail = fail .or. localtests(v,left_reals(r(:)),'0.0, 1.0, -2.0, 1.3, 3*3.0E-11, -5.1E+12')
348  fail = fail .or. localtests(v,left_reals(r(:),sep=' '),'0.0 1.0 -2.0 1.3 3*3.0E-11 -5.1E+12')
349  fail = fail .or. localtests(v,left_reals(r(:),sep=','),'0.0,1.0,-2.0,1.3,3*3.0E-11,-5.1E+12')
350  fail = fail .or. localtests(v,extractword("One Two,Three",1),"One")
351  fail = fail .or. localtests(v,extractword("One Two,Three",2),"Two")
352  fail = fail .or. localtests(v,extractword("One Two,Three",3),"Three")
353  fail = fail .or. localtests(v,extractword("One Two, Three",3),"Three")
354  fail = fail .or. localtests(v,extractword(" One Two,Three",1),"One")
355  fail = fail .or. localtests(v,extract_word("One,Two,Three",",",3),"Three")
356  fail = fail .or. localtests(v,extract_word("One,Two,Three",",",4),"")
357  fail = fail .or. localtests(v,remove_spaces("1 2 3"),"123")
358  fail = fail .or. localtests(v,remove_spaces(" 1 2 3"),"123")
359  fail = fail .or. localtests(v,remove_spaces("1 2 3 "),"123")
360  fail = fail .or. localtests(v,remove_spaces("123"),"123")
361  fail = fail .or. localtests(v,remove_spaces(" "),"")
362  fail = fail .or. localtests(v,remove_spaces(""),"")
363  fail = fail .or. localtesti(v,extract_integer("1","",1),1)
364  fail = fail .or. localtesti(v,extract_integer("1,2,3",",",1),1)
365  fail = fail .or. localtesti(v,extract_integer("1,2",",",2),2)
366  fail = fail .or. localtesti(v,extract_integer("1,2",",",3),0)
367  fail = fail .or. localtesti(v,extract_integer("1,2",",",4,4),4)
368  fail = fail .or. localtestr(v,extract_real("1.","",1),1.)
369  fail = fail .or. localtestr(v,extract_real("1.,2.,3.",",",1),1.)
370  fail = fail .or. localtestr(v,extract_real("1.,2.",",",2),2.)
371  fail = fail .or. localtestr(v,extract_real("1.,2.",",",3),0.)
372  fail = fail .or. localtestr(v,extract_real("1.,2.",",",4,4.),4.)
373  if (.not. fail) write(*,*) 'Pass'
374  string_functions_unit_tests = fail
Here is the call graph for this function:
Here is the caller graph for this function:

◆ uppercase()

character(len=len(input_string)) function, public mom_string_functions::uppercase ( character(len=*), intent(in)  input_string)

Definition at line 66 of file MOM_string_functions.F90.

Referenced by mom_continuity::continuity_init(), regrid_consts::coordinatemode(), mom_coriolisadv::coriolisadv_init(), mom_eos::eos_init(), regrid_interp::interpolation_scheme(), mom_main(), ocean_model_mod::ocean_model_init(), mom_shared_initialization::read_face_length_list(), mom_shared_initialization::reset_face_lengths_list(), mom_remapping::setreconstructiontype(), and mom_surface_forcing::wind_forcing_from_file().

66  character(len=*), intent(in) :: input_string
67  character(len=len(input_string)) :: uppercase
68 ! This function returns a string in which all lowercase letters have been
69 ! replaced by their uppercase counterparts. It is loosely based on the
70 ! uppercase function in mpp_util.F90.
71  ! Arguments
72  integer, parameter :: co=iachar('A')-iachar('a') ! case offset
73  integer :: k
74 
75  uppercase = input_string
76  do k=1, len_trim(input_string)
77  if (uppercase(k:k) >= 'a' .and. uppercase(k:k) <= 'z') &
78  uppercase(k:k) = achar(ichar(uppercase(k:k))+co)
79  end do
Here is the caller graph for this function: