FMS  2025.04
Flexible Modeling System
parse.inc
1 !***********************************************************************
2 !* Apache License 2.0
3 !*
4 !* This file is part of the GFDL Flexible Modeling System (FMS).
5 !*
6 !* Licensed under the Apache License, Version 2.0 (the "License");
7 !* you may not use this file except in compliance with the License.
8 !* You may obtain a copy of the License at
9 !*
10 !* http://www.apache.org/licenses/LICENSE-2.0
11 !*
12 !* FMS is distributed in the hope that it will be useful, but WITHOUT
13 !* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied;
14 !* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
15 !* PARTICULAR PURPOSE. See the License for the specific language
16 !* governing permissions and limitations under the License.
17 !***********************************************************************
18 
19 
20  character(*), parameter :: SPACE = ' '
21  character(*), parameter :: DELIM = space//','
22  integer :: parse
23  integer :: is, ie, id, k
24  integer :: ts, last, i
25 
26  parse = 0; ts = 1; last=len_trim(text)
27  do
28  i=scan(text(ts:last),'=') ! location of the next equal sign in the input test
29  if (i == 0) return
30  ! find the last non-space character before the equal sign
31  do ie = ts+i-2,ts,-1
32  if (scan(text(ie:ie),space)==0) exit
33  enddo
34  ! find the last delimeter preceding spaces and equal sign
35  do is = ie,ts,-1
36  if (scan(text(is:is),delim)>0) exit
37  enddo
38  if (trim(label)==text(is+1:ie)) exit ! from outer loop: found the label
39  ! for the next iteration of the loop
40  ts = ts+i+1 ! shift the beginning of the line
41  enddo
42 
43  is = ts+i
44  do k = 1, size(values(:))
45 
46  ! position of ending pointer
47  id = scan( text(is:last), ',' )
48  if (id == 0) then
49  ie = last ! no trailing comma, use end of text string
50  else
51  ie = is+id-2
52  endif
53 
54  ! decode value
55  ! print *, 'k,is,ie,id,last=',k,is,ie,id,last
56  ! print *, 'DECODE: ', text(is:ie)
57  read ( text(is:ie), *, err=99, end=99 ) values(k)
58  parse = parse+1 ! parse is the number of values decoded
59 
60  if (ie == last) exit ! end of text string ... DONE
61  is = is+id ! move starting pointer after ","
62  if (is > last) exit ! end of text string ... DONE
63  enddo
64  return
65 
66 99 continue
67  call mpp_error (fatal,'in parse, error decoding "'//trim(label)//'" in text "'//text//'"')