NCEPLIBS-g2  3.4.7
getgb2s.F90
Go to the documentation of this file.
1 
5 
78 SUBROUTINE getgb2s(CBUF, NLEN, NNUM, J, JDISC, JIDS, JPDTN, JPDT, JGDTN, &
79  JGDT, K, GFLD, LPOS, IRET)
80  USE grib_mod
81  implicit none
82 
83  CHARACTER(LEN = 1), INTENT(IN) :: CBUF(NLEN)
84  INTEGER, INTENT(IN) :: NLEN, NNUM, J, JDISC, JPDTN, JGDTN
85  INTEGER, DIMENSION(:) :: JIDS(*), JPDT(*), JGDT(*)
86  INTEGER, INTENT(OUT) :: K, LPOS, IRET
87  TYPE(gribfield), INTENT(OUT) :: GFLD
88 
89  INTEGER :: KGDS(5)
90  LOGICAL :: MATCH1, MATCH3, MATCH4
91  integer :: i, icnd, inlen, iof, ipos, jpos, lsec1, lsec3, lsec4, lsec5, numgdt, numpdt
92 
93  interface
94  subroutine gf_unpack1(cgrib, lcgrib, iofst, ids, idslen, ierr)
95  character(len = 1), intent(in) :: cgrib(lcgrib)
96  integer, intent(in) :: lcgrib
97  integer, intent(inout) :: iofst
98  integer, pointer, dimension(:) :: ids
99  integer, intent(out) :: ierr, idslen
100  end subroutine gf_unpack1
101  subroutine gf_unpack3(cgrib, lcgrib, iofst, igds, igdstmpl, &
102  mapgridlen, ideflist, idefnum, ierr)
103  character(len = 1), intent(in) :: cgrib(lcgrib)
104  integer, intent(in) :: lcgrib
105  integer, intent(inout) :: iofst
106  integer, pointer, dimension(:) :: igdstmpl, ideflist
107  integer, intent(out) :: igds(5)
108  integer, intent(out) :: ierr, idefnum
109  end subroutine gf_unpack3
110  subroutine gf_unpack4(cgrib, lcgrib, iofst, ipdsnum, ipdstmpl, &
111  mappdslen, coordlist, numcoord, ierr)
112  character(len = 1), intent(in) :: cgrib(lcgrib)
113  integer, intent(in) :: lcgrib
114  integer, intent(inout) :: iofst
115  real, pointer, dimension(:) :: coordlist
116  integer, pointer, dimension(:) :: ipdstmpl
117  integer, intent(out) :: ipdsnum
118  integer, intent(out) :: ierr, numcoord
119  end subroutine gf_unpack4
120  subroutine gf_unpack5(cgrib, lcgrib, iofst, ndpts, idrsnum, &
121  idrstmpl, mapdrslen, ierr)
122  character(len = 1), intent(in) :: cgrib(lcgrib)
123  integer, intent(in) :: lcgrib
124  integer, intent(inout) :: iofst
125  integer, intent(out) :: ndpts, idrsnum
126  integer, pointer, dimension(:) :: idrstmpl
127  integer, intent(out) :: ierr
128  end subroutine gf_unpack5
129  end interface
130 
131  ! INITIALIZE
132  k = 0
133  lpos = 0
134  iret = 1
135  ipos = 0
136  nullify(gfld%idsect, gfld%local)
137  nullify(gfld%list_opt, gfld%igdtmpl, gfld%ipdtmpl)
138  nullify(gfld%coord_list, gfld%idrtmpl, gfld%bmap, gfld%fld)
139 
140  ! SEARCH FOR REQUEST
141  DO WHILE(iret.NE.0 .and. k.LT.nnum)
142  k = k + 1
143  CALL g2_gbytec(cbuf, inlen, ipos * 8, 4 * 8) ! GET LENGTH OF CURRENT
144  ! INDEX RECORD
145  IF (k.LE.j) THEN ! SKIP THIS INDEX
146  ipos = ipos + inlen
147  cycle
148  ENDIF
149 
150  ! CHECK IF GRIB2 DISCIPLINE IS A MATCH
151  CALL g2_gbytec(cbuf, gfld%DISCIPLINE, (ipos + 41)*8, 1*8)
152  IF ((jdisc.NE.-1) .and. (jdisc.NE.gfld%DISCIPLINE)) THEN
153  ipos = ipos + inlen
154  cycle
155  ENDIF
156 
157  ! CHECK IF IDENTIFICATION SECTION IS A MATCH
158  match1 = .false.
159  CALL g2_gbytec(cbuf, lsec1, (ipos + 44) * 8, 4 * 8) ! GET LENGTH OF IDS
160  iof = 0
161  CALL gf_unpack1(cbuf(ipos + 45), lsec1, iof, gfld%IDSECT, gfld%IDSECTLEN, icnd)
162  IF (icnd .eq. 0) THEN
163  match1 = .true.
164  DO i = 1, gfld%IDSECTLEN
165  IF ((jids(i).NE.-9999) .and. (jids(i).NE.gfld%IDSECT(i))) THEN
166  match1 = .false.
167  EXIT
168  ENDIF
169  ENDDO
170  ENDIF
171  IF (.NOT. match1) THEN
172  DEALLOCATE(gfld%IDSECT)
173  ipos = ipos + inlen
174  cycle
175  ENDIF
176 
177  ! CHECK IF GRID DEFINITION TEMPLATE IS A MATCH
178  jpos = ipos + 44 + lsec1
179  match3 = .false.
180  CALL g2_gbytec(cbuf, lsec3, jpos * 8, 4 * 8) ! GET LENGTH OF GDS
181  IF (jgdtn .eq. -1) THEN
182  match3 = .true.
183  ELSE
184  CALL g2_gbytec(cbuf, numgdt, (jpos + 12) * 8, 2 * 8) ! GET GDT TEMPLATE NO.
185  IF (jgdtn .eq. numgdt) THEN
186  iof = 0
187  CALL gf_unpack3(cbuf(jpos + 1), lsec3, iof, kgds, gfld%IGDTMPL, &
188  gfld%IGDTLEN, gfld%LIST_OPT, gfld%NUM_OPT, icnd)
189  IF (icnd .eq. 0) THEN
190  match3 = .true.
191  DO i = 1, gfld%IGDTLEN
192  IF ((jgdt(i).NE.-9999) .and. (jgdt(i).NE.gfld%IGDTMPL(i))) THEN
193  match3 = .false.
194  EXIT
195  ENDIF
196  ENDDO
197  ENDIF
198  ENDIF
199  ENDIF
200  IF (.NOT. match3) THEN
201  IF (ASSOCIATED(gfld%IDSECT)) DEALLOCATE(gfld%IDSECT)
202  IF (ASSOCIATED(gfld%IGDTMPL)) DEALLOCATE(gfld%IGDTMPL)
203  IF (ASSOCIATED(gfld%LIST_OPT)) DEALLOCATE(gfld%LIST_OPT)
204  ipos = ipos + inlen
205  cycle
206  ELSE
207  gfld%GRIDDEF = kgds(1)
208  gfld%NGRDPTS = kgds(2)
209  gfld%NUMOCT_OPT = kgds(3)
210  gfld%INTERP_OPT = kgds(4)
211  gfld%IGDTNUM = kgds(5)
212  ENDIF
213 
214  ! CHECK IF PRODUCT DEFINITION TEMPLATE IS A MATCH
215  jpos = jpos + lsec3
216  match4 = .false.
217  CALL g2_gbytec(cbuf, lsec4, jpos * 8, 4 * 8) ! GET LENGTH OF PDS
218  IF (jpdtn .eq. -1) THEN
219  match4 = .true.
220  ELSE
221  CALL g2_gbytec(cbuf, numpdt, (jpos + 7) * 8, 2 * 8) ! GET PDT TEMPLATE NO.
222  IF (jpdtn .eq. numpdt) THEN
223  iof = 0
224  CALL gf_unpack4(cbuf(jpos + 1), lsec4, iof, gfld%IPDTNUM, &
225  gfld%IPDTMPL, gfld%IPDTLEN, gfld%COORD_LIST, gfld%NUM_COORD, icnd)
226  IF (icnd .eq. 0) THEN
227  match4 = .true.
228  DO i = 1, gfld%IPDTLEN
229  IF ((jpdt(i).NE.-9999) .and. (jpdt(i).NE.gfld%IPDTMPL(i))) THEN
230  match4 = .false.
231  EXIT
232  ENDIF
233  ENDDO
234  ENDIF
235  ENDIF
236  ENDIF
237  IF (.NOT. match4) THEN
238  IF (ASSOCIATED(gfld%IDSECT)) DEALLOCATE(gfld%IDSECT)
239  IF (ASSOCIATED(gfld%IPDTMPL)) DEALLOCATE(gfld%IPDTMPL)
240  IF (ASSOCIATED(gfld%COORD_LIST)) DEALLOCATE(gfld%COORD_LIST)
241  ENDIF
242 
243  ! IF REQUEST IS FOUND
244  ! SET VALUES FOR DERIVED TYPE GFLD AND RETURN
245  IF(match1 .and. match3 .and. match4) THEN
246  lpos = ipos + 1
247  CALL g2_gbytec(cbuf, gfld%VERSION, (ipos + 40) * 8, 1 * 8)
248  CALL g2_gbytec(cbuf, gfld%IFLDNUM, (ipos + 42) * 8, 2 * 8)
249  gfld%UNPACKED = .false.
250  jpos = ipos + 44 + lsec1
251  IF (jgdtn.EQ.-1) THEN ! UNPACK GDS, IF NOT DONE BEFORE
252  iof = 0
253  CALL gf_unpack3(cbuf(jpos + 1), lsec3, iof, kgds, gfld%IGDTMPL, &
254  gfld%IGDTLEN, gfld%LIST_OPT, gfld%NUM_OPT, icnd)
255  gfld%GRIDDEF = kgds(1)
256  gfld%NGRDPTS = kgds(2)
257  gfld%NUMOCT_OPT = kgds(3)
258  gfld%INTERP_OPT = kgds(4)
259  gfld%IGDTNUM = kgds(5)
260  ENDIF
261  jpos = jpos + lsec3
262  IF (jpdtn.EQ.-1 ) THEN ! UNPACK PDS, IF NOT DONE BEFORE
263  iof = 0
264  CALL gf_unpack4(cbuf(jpos + 1), lsec4, iof, gfld%IPDTNUM, &
265  gfld%IPDTMPL, gfld%IPDTLEN, gfld%COORD_LIST, gfld%NUM_COORD, icnd)
266  ENDIF
267  jpos = jpos + lsec4
268  CALL g2_gbytec(cbuf, lsec5, jpos * 8, 4 * 8) ! GET LENGTH OF DRS
269  iof = 0
270  CALL gf_unpack5(cbuf(jpos + 1), lsec5, iof, gfld%NDPTS, &
271  gfld%IDRTNUM, gfld%IDRTMPL, gfld%IDRTLEN, icnd)
272  jpos = jpos + lsec5
273  CALL g2_gbytec(cbuf, gfld%IBMAP, (jpos + 5)*8, 1 * 8) ! GET IBMAP
274  iret = 0
275  ELSE ! PDT DID NOT MATCH
276  ipos = ipos+inlen
277  ENDIF
278  ENDDO
279 END SUBROUTINE getgb2s
subroutine g2_gbytec(in, iout, iskip, nbits)
Extract arbitrary size values from a packed bit string, right justifying each value in the unpacked a...
Definition: g2_gbytesc.F90:20
subroutine getgb2s(CBUF, NLEN, NNUM, J, JDISC, JIDS, JPDTN, JPDT, JGDTN, JGDT, K, GFLD, LPOS, IRET)
Find information about a GRIB field from the index and fill a grib_mod::gribfield.
Definition: getgb2s.F90:80
subroutine gf_unpack1(cgrib, lcgrib, iofst, ids, idslen, ierr)
Unpack Section 1 (Identification Section) of a GRIB2 message, starting at octet 6 of that Section.
Definition: gf_unpack1.F90:43
subroutine gf_unpack3(cgrib, lcgrib, iofst, igds, igdstmpl, mapgridlen, ideflist, idefnum, ierr)
Unpack Section 3 (Grid Definition Section) of a GRIB2 message, starting at octet 6 of that Section.
Definition: gf_unpack3.F90:52
subroutine gf_unpack4(cgrib, lcgrib, iofst, ipdsnum, ipdstmpl, mappdslen, coordlist, numcoord, ierr)
Unpack Section 4 (Product Definition Section) of a GRIB2 message, starting at octet 6 of that Section...
Definition: gf_unpack4.F90:37
subroutine gf_unpack5(cgrib, lcgrib, iofst, ndpts, idrsnum, idrstmpl, mapdrslen, ierr)
Unpack Section 5 (Data Representation Section) of a GRIB2 message, starting at octet 6 of that Sectio...
Definition: gf_unpack5.F90:35
This Fortran module contains the declaration of derived type gribfield.
Definition: gribmod.F90:10