2 !$$$ SUBPROGRAM DOCUMENTATION BLOCK
5 ! PRGMMR
: Gilbert ORG
: W
/NP11
DATE: 2004-04-27
7 ! ABSTRACT
: This Fortran Module allows access
to predefined GRIB2 Grid
8 ! Definition Templates stored in a file
. The GDTs are represented by
9 ! a predefined number or a
character abbreviation
.
11 ! At the first request
, all the grid GDT entries in the file associated
12 ! with input Fortran file unit number
, lunit
, are
read into a linked list
13 ! named gridlist
. This list is searched
for the requested entry
.
15 ! Users of this Fortran module should only
call routines getgridbynum
18 ! The
format of the file scanned by routines in this module is as follows
.
19 ! Each line contains one Grid entry containing five fields
, each separated
20 ! by a colon
, ":". The fields are
:
21 ! 1) - predefined grid number
22 ! 2) - Up
to an
8 character abbreviation
23 ! 3) - Grid Definition Template number
24 ! 4) - Number of entries in the Grid Definition Template
25 ! 5) - A list of values
for each entry in the Grid Definition Template
.
27 ! As an example
, this is the entry
for the
1x1 GFS global grid
28 ! 3:gbl_1deg
: 0:19: 0 0 0 0 0 0 0 360 181 0 0 90000000 0 48 -90000000 359000000 1000000 1000000 0
30 ! Comments can be included in the file by specifying the symbol
"#" as the
31 ! first
character on the line
. These lines are ignored
.
34 ! PROGRAM HISTORY LOG
:
40 ! LANGUAGE
: Fortran
90
45 integer,parameter :: MAXTEMP
=200
47 type
,private
:: g2grid
51 integer,dimension(MAXTEMP
) :: gridtmpl
52 character(len
=8) :: cdesc
53 type
(g2grid
),pointer
:: next
56 type
(g2grid
),pointer
,private
:: gridlist
57 integer :: num_grids
=0
62 integer function readgrids
(lunit
)
63 !$$$ SUBPROGRAM DOCUMENTATION BLOCK
65 ! SUBPROGRAM
: readgrids
66 ! PRGMMR
: Gilbert ORG
: W
/NP11
DATE: 2001-06-28
68 ! ABSTRACT
: This
function reads the list of GDT entries in the file
69 ! associated with fortran unit
, lunit
. All the entries are stored in a
70 ! linked list called gridlist
.
72 ! PROGRAM HISTORY LOG
:
75 ! USAGE
: number
=readgrids
(lunit
)
76 ! INPUT ARGUMENT LIST
:
77 ! lunit
- Fortran unit number associated the the GDT file
.
79 ! RETURNS
: The number of Grid Definition Templates
read in
.
84 ! LANGUAGE
: Fortran
90
88 integer,intent
(in
) :: lunit
90 integer,parameter :: linelen
=1280
91 character(len
=8) :: desc
92 character(len
=linelen
) :: cline
93 integer ient
,igdtn
,igdtmpl
(200),igdtlen
94 integer :: pos1
,pos2
,pos3
,pos4
96 type
(g2grid
),pointer
:: gtemp
97 type
(g2grid
),pointer
:: prev
102 ! For each line in the file
....
104 ! Read line into buffer
107 read(lunit
,end=999,fmt
='(a)') cline
110 ! Skip line
if commented out
112 if (cline
(1:1).eq
.'#') cycle
115 ! find positions of delimiters
, ":"
117 pos1
=index
(cline
,':')
119 pos2
=index
(cline
,':')
121 pos3
=index
(cline
,':')
123 pos4
=index
(cline
,':')
124 if ( pos1
.eq
.0 .or
. pos2
.eq
.0 .or
. pos3
.eq
.0 .or
.
128 ! Read each of the five fields
.
130 read(cline
(1:pos1
-1),*) ient
131 read(cline
(pos1
+1:pos2
-1),*) desc
132 read(cline
(pos2
+1:pos3
-1),*) igdtn
133 read(cline
(pos3
+1:pos4
-1),*) igdtlen
134 read(cline
(pos4
+1:linelen
),*) (igdtmpl
(j
),j
=1,igdtlen
)
137 ! Allocate new type
(g2grid
) variable
to store the GDT
139 allocate
(gtemp
,stat
=iom
)
143 gtemp
%gdt_len
=igdtlen
144 gtemp
%gridtmpl
=igdtmpl
146 nullify
(gtemp
%next
) ! defines
end of linked list
.
147 if ( count
.eq
. 1 ) then
149 else ! make sure previous entry in list
150 prev
%next
=> gtemp
! points
to the new entry
,
162 subroutine getgridbynum
(lunit
,number
,igdtn
,igdtmpl
,iret
)
163 !$$$ SUBPROGRAM DOCUMENTATION BLOCK
165 ! SUBPROGRAM
: getgridbynum
166 ! PRGMMR
: Gilbert ORG
: W
/NP11
DATE: 2004-04-26
168 ! ABSTRACT
: This
subroutine searches a file referenced by fortran unit lunit
169 ! for a Grid Definition Template assigned
to the requested number
.
170 ! The input file
format is described at the top of this module
.
172 ! PROGRAM HISTORY LOG
:
175 ! USAGE
: CALL getgridbynum
(lunit
,number
,igdtn
,igdtmpl
,iret
)
176 ! INPUT ARGUMENT LIST
:
177 ! lunit
- Unit number of file containing Grid definitions
178 ! number
- Grid number of the requested Grid definition
180 ! OUTPUT ARGUMENT LIST
:
181 ! igdtn
- NN
, indicating the number of the Grid Definition
183 ! igdtmpl
()- An array containing the values of each entry in
184 ! the Grid Definition Template
.
185 ! iret
- Error
return code
.
187 ! -1 = Undefined Grid number
.
188 ! 3 = Could not
read any grids from file
.
193 ! LANGUAGE
: Fortran
90
197 integer,intent
(in
) :: lunit
,number
198 integer,intent
(out
) :: igdtn
,igdtmpl
(*),iret
200 type
(g2grid
),pointer
:: tempgrid
207 ! If no grids in list
, try reading them from the file
.
209 if ( num_grids
.eq
. 0 ) then
210 num_grids
=readgrids
(lunit
)
213 if ( num_grids
.eq
. 0 ) then
214 iret
=3 ! problem reading file
221 ! Search through list
223 do while ( associated
(tempgrid
) )
224 if ( number
.eq
. tempgrid
%grid_num
) then
225 igdtn
=tempgrid
%gdt_num
226 igdtmpl
(1:tempgrid
%gdt_len
)=
227 & tempgrid
%gridtmpl
(1:tempgrid
%gdt_len
)
230 tempgrid
=> tempgrid
%next
240 subroutine getgridbyname
(lunit
,name
,igdtn
,igdtmpl
,iret
)
241 !$$$ SUBPROGRAM DOCUMENTATION BLOCK
243 ! SUBPROGRAM
: getgridbyname
244 ! PRGMMR
: Gilbert ORG
: W
/NP11
DATE: 2004-04-26
246 ! ABSTRACT
: This
subroutine searches a file referenced by fortran unit lunit
247 ! for a Grid Definition Template assigned
to the requested name
.
248 ! The input file
format is described at the top of this module
.
250 ! PROGRAM HISTORY LOG
:
253 ! USAGE
: CALL getgridbyname
(lunit
,name
,igdtn
,igdtmpl
,iret
)
254 ! INPUT ARGUMENT LIST
:
255 ! lunit
- Unit number of file containing Grid definitions
256 ! name
- Grid name of the requested Grid definition
258 ! OUTPUT ARGUMENT LIST
:
259 ! igdtn
- NN
, indicating the number of the Grid Definition
261 ! igdtmpl
()- An array containing the values of each entry in
262 ! the Grid Definition Template
.
263 ! iret
- Error
return code
.
265 ! -1 = Undefined Grid number
.
266 ! 3 = Could not
read any grids from file
.
271 ! LANGUAGE
: Fortran
90
275 integer,intent
(in
) :: lunit
276 character(len
=8),intent
(in
) :: name
277 integer,intent
(out
) :: igdtn
,igdtmpl
(*),iret
279 type
(g2grid
),pointer
:: tempgrid
286 ! If no grids in list
, try reading them from the file
.
288 if ( num_grids
.eq
. 0 ) then
289 num_grids
=readgrids
(lunit
)
292 if ( num_grids
.eq
. 0 ) then
293 iret
=3 ! problem reading file
300 ! Search through list
302 do while ( associated
(tempgrid
) )
303 if ( name
.eq
. tempgrid
%cdesc
) then
304 igdtn
=tempgrid
%gdt_num
305 igdtmpl
(1:tempgrid
%gdt_len
)=
306 & tempgrid
%gridtmpl
(1:tempgrid
%gdt_len
)
309 tempgrid
=> tempgrid
%next