NCEPLIBS-g2  3.4.5
g2grids.f
Go to the documentation of this file.
1 
6 
29 
30  module g2grids
31 
32  integer,parameter :: maxtemp=200
33 
34  type,private :: g2grid
35  integer :: grid_num
36  integer :: gdt_num
37  integer :: gdt_len
38  integer,dimension(MAXTEMP) :: gridtmpl
39  character(len=8) :: cdesc
40  type(g2grid),pointer :: next
41  end type g2grid
42 
43  type(g2grid),pointer,private :: gridlist
44  integer :: num_grids=0
45 
46  contains
47 
56 
57  integer function readgrids(lunit)
58  integer,intent(in) :: lunit
59 
60  integer,parameter :: linelen=1280
61  character(len=8) :: desc
62  character(len=linelen) :: cline
63  integer ient,igdtn,igdtmpl(200),igdtlen
64  integer :: pos1,pos2,pos3,pos4
65 
66  type(g2grid),pointer :: gtemp
67  type(g2grid),pointer :: prev
68  integer count
69 
70  count=0
71 
72  ! For each line in the file....
73  DO
74  ! Read line into buffer
75  !
76  cline(1:linelen)=' '
77  read(lunit,end=999,fmt='(a)') cline
78 
79  !
80  ! Skip line if commented out
81  !
82  if (cline(1:1).eq.'#') cycle
83 
84  !
85  ! find positions of delimiters, ":"
86  !
87  pos1=index(cline,':')
88  cline(pos1:pos1)=';'
89  pos2=index(cline,':')
90  cline(pos2:pos2)=';'
91  pos3=index(cline,':')
92  cline(pos3:pos3)=';'
93  pos4=index(cline,':')
94  if ( pos1.eq.0 .or. pos2.eq.0 .or. pos3.eq.0 .or.
95  & pos4.eq.0) cycle
96 
97  !
98  ! Read each of the five fields.
99  !
100  read(cline(1:pos1-1),*) ient
101  read(cline(pos1+1:pos2-1),*) desc
102  read(cline(pos2+1:pos3-1),*) igdtn
103  read(cline(pos3+1:pos4-1),*) igdtlen
104  read(cline(pos4+1:linelen),*) (igdtmpl(j),j=1,igdtlen)
105 
106  !
107  ! Allocate new type(g2grid) variable to store the GDT
108  !
109  allocate(gtemp,stat=iom)
110  count=count+1
111  gtemp%grid_num=ient
112  gtemp%gdt_num=igdtn
113  gtemp%gdt_len=igdtlen
114  gtemp%gridtmpl=igdtmpl
115  gtemp%cdesc=desc
116  nullify(gtemp%next) ! defines end of linked list.
117  if ( count .eq. 1 ) then
118  gridlist => gtemp
119  else ! make sure previous entry in list
120  prev%next => gtemp ! points to the new entry,
121  endif
122  prev => gtemp
123 
124  enddo
125 
126  999 readgrids=count
127  return
128 
129  end function
130 
148 
149  subroutine getgridbynum(lunit,number,igdtn,igdtmpl,iret)
150 
151  integer,intent(in) :: lunit,number
152  integer,intent(out) :: igdtn,igdtmpl(*),iret
153 
154  type(g2grid),pointer :: tempgrid
155 
156  iret=0
157  igdtn=-1
158  !igdtmpl=0
159 
160  !
161  ! If no grids in list, try reading them from the file.
162  !
163  if ( num_grids .eq. 0 ) then
164  num_grids=readgrids(lunit)
165  endif
166 
167  if ( num_grids .eq. 0 ) then
168  iret=3 ! problem reading file
169  return
170  endif
171 
172  tempgrid => gridlist
173 
174  !
175  ! Search through list
176  !
177  do while ( associated(tempgrid) )
178  if ( number .eq. tempgrid%grid_num ) then
179  igdtn=tempgrid%gdt_num
180  igdtmpl(1:tempgrid%gdt_len)=
181  & tempgrid%gridtmpl(1:tempgrid%gdt_len)
182  return
183  else
184  tempgrid => tempgrid%next
185  endif
186  enddo
187 
188  iret=-1
189  return
190 
191  end subroutine
192 
210 
211  subroutine getgridbyname(lunit,name,igdtn,igdtmpl,iret)
212 
213  integer,intent(in) :: lunit
214  character(len=8),intent(in) :: name
215  integer,intent(out) :: igdtn,igdtmpl(*),iret
216 
217  type(g2grid),pointer :: tempgrid
218 
219  iret=0
220  igdtn=-1
221  !igdtmpl=0
222 
223  !
224  ! If no grids in list, try reading them from the file.
225  !
226  if ( num_grids .eq. 0 ) then
227  num_grids=readgrids(lunit)
228  endif
229 
230  if ( num_grids .eq. 0 ) then
231  iret=3 ! problem reading file
232  return
233  endif
234 
235  tempgrid => gridlist
236 
237  !
238  ! Search through list
239  !
240  do while ( associated(tempgrid) )
241  if ( name .eq. tempgrid%cdesc ) then
242  igdtn=tempgrid%gdt_num
243  igdtmpl(1:tempgrid%gdt_len)=
244  & tempgrid%gridtmpl(1:tempgrid%gdt_len)
245  return
246  else
247  tempgrid => tempgrid%next
248  endif
249  enddo
250 
251  iret=-1
252  return
253 
254  end subroutine
255 
256 
257  end
258 
g2grids::readgrids
integer function readgrids(lunit)
This function reads the list of GDT entries in the file associated with fortran unit,...
Definition: g2grids.f:58
g2grids::maxtemp
integer, parameter maxtemp
maximum template number for grid definition.
Definition: g2grids.f:32
g2grids
This Fortran Module allows access to predefined GRIB2 Grid Definition Templates stored in a file.
Definition: g2grids.f:30
g2grids::getgridbynum
subroutine getgridbynum(lunit, number, igdtn, igdtmpl, iret)
This subroutine searches a file referenced by fortran unit lunit for a Grid Definition Template assig...
Definition: g2grids.f:150
g2grids::getgridbyname
subroutine getgridbyname(lunit, name, igdtn, igdtmpl, iret)
This subroutine searches a file referenced by fortran unit lunit for a Grid Definition Template assig...
Definition: g2grids.f:212
g2grids::num_grids
integer num_grids
the number of grids.
Definition: g2grids.f:44