NCEPLIBS-g2  3.5.0
g2grids.F90
Go to the documentation of this file.
1 
5 
33 module g2grids
34  implicit none
35  integer, parameter :: maxtemp = 200
36 
37  type,private :: g2grid
38  integer :: grid_num
39  integer :: gdt_num
40  integer :: gdt_len
41  integer, dimension(MAXTEMP) :: gridtmpl
42  character(len = 8) :: cdesc
43  type(g2grid), pointer :: next
44  end type g2grid
45 
46  type(g2grid), pointer, private :: gridlist
47  integer :: num_grids = 0
48 
49 contains
50 
58  integer function readgrids(lunit)
59  implicit none
60 
61  integer, intent(in) :: lunit
62 
63  integer, parameter :: linelen = 1280
64  character(len = 8) :: desc
65  character(len = linelen) :: cline
66  integer :: ient, igdtn, igdtmpl(200), igdtlen
67  integer :: pos1, pos2, pos3, pos4
68  type(g2grid), pointer :: gtemp
69  type(g2grid), pointer :: prev => null()
70  integer :: count
71 
72  integer :: j
73 
74  count = 0
75 
76  ! For each line in the file....
77  do
78  ! Read line into buffer.
79  cline(1 : linelen) = ' '
80  read(lunit, end = 999, fmt = '(a)') cline
81 
82  ! Skip line if commented out.
83  if (cline(1 : 1) .eq. '#') cycle
84 
85  ! Find positions of delimiters, ":".
86  pos1 = index(cline, ':')
87  cline(pos1 : pos1) = ';'
88  pos2 = index(cline, ':')
89  cline(pos2 : pos2) = ';'
90  pos3 = index(cline, ':')
91  cline(pos3 : pos3) = ';'
92  pos4 = index(cline, ':')
93  if ( pos1 .eq. 0 .or. pos2 .eq. 0 .or. pos3 .eq. 0 .or. &
94  pos4 .eq. 0) cycle
95 
96  ! Read each of the five fields.
97  read(cline(1 : pos1 - 1), *) ient
98  read(cline(pos1 + 1 : pos2 - 1), *) desc
99  read(cline(pos2 + 1 : pos3 - 1), *) igdtn
100  read(cline(pos3 + 1 : pos4 - 1), *) igdtlen
101  read(cline(pos4 + 1 : linelen), *) (igdtmpl(j), j = 1, igdtlen)
102 
103  ! Allocate new type(g2grid) variable to store the GDT.
104  allocate(gtemp)
105  count = count + 1
106  gtemp%grid_num = ient
107  gtemp%gdt_num = igdtn
108  gtemp%gdt_len = igdtlen
109  gtemp%gridtmpl = igdtmpl
110  gtemp%cdesc = desc
111  nullify(gtemp%next) ! defines end of linked list.
112  if ( count .eq. 1 ) then
113  gridlist => gtemp
114  else ! make sure previous entry in list
115  prev%next => gtemp ! points to the new entry,
116  endif
117  prev => gtemp
118  enddo
119 999 backspace(lunit)
120  readgrids = count
121  return
122 
123  end function readgrids
124 
144  subroutine getgridbynum(lunit, number, igdtn, igdtmpl, iret)
145  implicit none
146  integer, intent(in) :: lunit, number
147  integer, intent(out) :: igdtn, igdtmpl(*), iret
148  type(g2grid), pointer :: tempgrid
149 
150  iret = 0
151  igdtn = -1
152 
153  ! If no grids in list, try reading them from the file.
154  if (num_grids .eq. 0) then
155  num_grids = readgrids(lunit)
156  endif
157 
158  if (num_grids .eq. 0) then
159  iret = 3 ! problem reading file
160  return
161  endif
162 
163  tempgrid => gridlist
164 
165  ! Search through list.
166  do while (associated(tempgrid))
167  if (number .eq. tempgrid%grid_num) then
168  igdtn = tempgrid%gdt_num
169  igdtmpl(1 : tempgrid%gdt_len) = tempgrid%gridtmpl(1 : tempgrid%gdt_len)
170  return
171  else
172  tempgrid => tempgrid%next
173  endif
174  enddo
175 
176  iret = -1
177  return
178 
179  end subroutine getgridbynum
180 
200  subroutine getgridbyname(lunit, name, igdtn, igdtmpl, iret)
201  implicit none
202  integer, intent(in) :: lunit
203  character(len = 8), intent(in) :: name
204  integer, intent(out) :: igdtn, igdtmpl(*), iret
205  type(g2grid), pointer :: tempgrid
206 
207  iret = 0
208  igdtn = -1
209 
210  ! If no grids in list, try reading them from the file.
211  if (num_grids .eq. 0) then
212  num_grids = readgrids(lunit)
213  endif
214 
215  if (num_grids .eq. 0) then
216  iret = 3 ! problem reading file
217  return
218  endif
219 
220  tempgrid => gridlist
221 
222  ! Search through list.
223  do while (associated(tempgrid))
224  if (name .eq. tempgrid%cdesc) then
225  igdtn = tempgrid%gdt_num
226  igdtmpl(1:tempgrid%gdt_len) = tempgrid%gridtmpl(1:tempgrid%gdt_len)
227  return
228  else
229  tempgrid => tempgrid%next
230  endif
231  enddo
232 
233  iret = -1
234  return
235  end subroutine getgridbyname
236 
241  subroutine freegridlist()
242  implicit none
243  integer :: i
244  type(g2grid), pointer :: gridnext
245 
246  do i = 1, num_grids
247  gridnext => gridlist%next
248  deallocate(gridlist)
249  gridlist => gridnext
250  end do
251  end subroutine freegridlist
252 end module g2grids
Allow access to predefined GRIB2 Grid Definition Templates (GDT) stored in a file.
Definition: g2grids.F90:33
subroutine freegridlist()
This subroutine frees the memory allocated for the linked list of grid templates stored in module var...
Definition: g2grids.F90:242
integer, parameter maxtemp
maximum template number for grid definition.
Definition: g2grids.F90:35
integer function readgrids(lunit)
This function reads the list of GDT entries in the file associated with fortran unit,...
Definition: g2grids.F90:59
integer num_grids
the number of grids.
Definition: g2grids.F90:47
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.F90:145
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.F90:201