NCEPLIBS-g2  3.4.5
getgb2.f
Go to the documentation of this file.
1 C> @file
2 C> @brief This subroutine find and unpack a grib file.
3 C> @author Mark Iredell @date 1994-04-01
4 C>
5 
6 C> This subroutine find and unpack a grib message. It reads
7 C> a grib index file (or optionally the grib file itself) to
8 C> get the index buffer (i.e. table of contents) for the grib file.
9 C> find in the index buffer a reference to the grib field requested.
10 C> the grib field request specifies the number of fields to skip
11 C> and the unpacked identification section, grid definition template
12 C> and product defintion section parameters. (a requested parameter
13 C> of -9999 means to allow any value of this parameter to be found.)
14 C> if the requested grib field is found, then it is read from the
15 C> grib file and unpacked. Its number is returned along with
16 C> the associated unpacked parameters. the bitmap (if any), and
17 C> the data values are unpacked only if argument "unpack" is set to
18 C> true. if the grib field is not found, then the return code
19 C> will be nonzero.
20 C>
21 C> The decoded information for the selected GRIB field is returned
22 C> in a derived type variable, gfld. Gfld is of type gribfield,
23 C> which is defined in module grib_mod, so users of this routine
24 C> will need to include the line "USE GRIB_MOD" in their calling
25 C> routine. Each component of the gribfield type is described in
26 C> the OUTPUT ARGUMENT LIST section below.
27 C>
28 C> PROGRAM HISTORY LOG:
29 C> - 1994-04-01 Mark Iredell
30 C> - 1995-10-31 Mark Iredell modularized portions of code into subprograms
31 C> and allowed for unspecified index file
32 C> - 2002-01-11 Stephen Gilbert modified from getgb and getgbm to work with grib2
33 C> - 2015-11-10 Boi Vuong modified doc block for gfld\%ngrdpts and gfld\%ndpts
34 C>
35 C> @param[in] LUGB integer unit of the unblocked grib data file.
36 C> file must be opened with baopen or baopenr before calling
37 C> this routine.
38 C> @param[in] LUGI integer unit of the unblocked grib index file.
39 C> if nonzero, file must be opened with baopen baopenr before
40 C> calling this routine.
41 C> - >0 read index from index file lugi, if index doesn"t already exist.
42 C> - =0 to get index buffer from the grib file, if index
43 C> doesn"t already exist.
44 C> - <0 force reread of index from index file abs(lugi).
45 C> - =lugb force regeneration of index from grib2 file lugb.
46 C> @param[in] J integer number of fields to skip
47 C> (=0 to search from beginning)
48 C> @param[in] JDISC grib2 discipline number of requested field
49 C> (if = -1, accept any discipline see code table 0.0)
50 C> - 0 meteorological products
51 C> - 1 hydrological products
52 C> - 2 land surface products
53 C> - 3 space products
54 C> - 10 oceanographic products
55 C> @param[in] JIDS integer array of values in the identification section
56 C> (=-9999 for wildcard)
57 C> - JIDS(1) identification of originating centre
58 C> (see common code table c-1)
59 C> - JIDS(2) identification of originating sub-centre
60 C> - JIDS(3) grib master tables version number
61 C> (see code table 1.0) 0 experimental;1 initial operational version number.
62 C> - JIDS(4) grib local tables version number (see code table 1.1)
63 C> 0 local tables not used; 1-254 number of local tables version used.
64 C> - JIDS(5) significance of reference time (code table 1.2)
65 C> 0 analysis; 1 start of forecast; 2 verifying time of forecast; 3 observation time
66 C> - JIDS(6) year (4 digits)
67 C> - JIDS(7) month
68 C> - JIDS(8) day
69 C> - JIDS(9) hour
70 C> - JIDS(10) minute
71 C> - JIDS(11) second
72 C> - JIDS(12) production status of processed data (see code table 1.3)
73 C> 0 operational products; 1 operational test products;
74 C> 2 research products; 3 re-analysis products.
75 C> - JIDS(13) type of processed data (see code table 1.4)
76 C> 0 analysis products; 1 forecast products; 2 analysis and forecast
77 C> products; 3 control forecast products; 4 perturbed forecast products;
78 C> 5 control and perturbed forecast products; 6 processed satellite
79 C> observations; 7 processed radar observations.
80 C> @param[in] JPDTN integer product definition template number (n)
81 C> (if = -1, don't bother matching pdt - accept any)
82 C> @param[in] JPDT integer array of values defining the product definition
83 C> template 4.n of the field for which to search (=-9999 for wildcard)
84 C> @param[in] JGDTN integer grid definition template number (m)
85 C> (if = -1, don't bother matching gdt - accept any )
86 C> @param[in] JGDT integer array of values defining the grid definition
87 C> template 3.m of the field for which to search (=-9999 for wildcard)
88 C> @param[in] UNPACK logical value indicating whether to unpack bitmap/data
89 C> - .TRUE. unpack bitmap and data values
90 C> - .FALSE. do not unpack bitmap and data values
91 C> @param[out] K integer field number unpacked
92 C> @param[out] GFLD derived type gribfield (defined in module grib_mod)
93 C> (NOTE: See Remarks Section)
94 C> - gfld\%version GRIB edition number (currently 2)
95 C> - gfld\%discipline Message Discipline (see Code Table 0.0)
96 C> - gfld\%idsect Contains the entries in the Identification Section
97 C> (Section 1) This element is actually a pointer to an array
98 C> that holds the data.
99 C> - gfld\%idsect(1) Identification of originating Centre
100 C> (see Common Code Table C-1) 7 US National Weather Service
101 C> - gfld\%idsect(2) Identification of originating Sub-centre
102 C> - gfld\%idsect(3) GRIB Master Tables Version Number
103 C> (see Code Table 1.0) 0 Experimental; 1 Initial operational version number
104 C> - gfld\%idsect(4) GRIB Local Tables Version Number (see Code Table 1.1)
105 C> 0 Local tables not used; 1-254 Number of local tables version used
106 C> - gfld\%idsect(5) Significance of Reference Time (Code Table 1.2)
107 C> 0 Analysis; 1 Start of forecast; 2 Verifying time of forecast;
108 C> 3 Observation time.
109 C> - gfld\%idsect(6) Year (4 digits)
110 C> - gfld\%idsect(7) Month
111 C> - gfld\%idsect(8) Day
112 C> - gfld\%idsect(9) Hour
113 C> - gfld\%idsect(10) Minute
114 C> - gfld\%idsect(11) Second
115 C> - gfld\%idsect(12) Production status of processed data (see Code Table 1.3)
116 C> 0 Operational products; 1 Operational test products;
117 C> 2 Research products; 3 Re-analysis products.
118 C> - gfld\%idsect(13) Type of processed data (see Code Table 1.4)
119 C> 0 Analysis products
120 C> 1 Forecast products
121 C> 2 Analysis and forecast products
122 C> 3 Control forecast products
123 C> 4 Perturbed forecast products
124 C> 5 Control and perturbed forecast products
125 C> 6 Processed satellite observations
126 C> 7 Processed radar observations
127 C> - gfld\%idsectlen Number of elements in gfld\%idsect
128 C> - gfld\%local Pointer to character array containing contents
129 C> of Local Section 2, if included
130 C> - gfld\%locallen length of array gfld\%local
131 C> - gfld\%ifldnum field number within GRIB message
132 C> - gfld\%griddef Source of grid definition (see Code Table 3.0)
133 C> 0 Specified in Code table 3.1
134 C> 1 Predetermined grid Defined by originating centre
135 C> - gfld\%ngrdpts Number of grid points in the defined grid.
136 C> Note that the number of actual data values returned from getgb2
137 C> (in gfld\%ndpts) may be less than this value if a logical bitmap
138 C> is in use with grid points that are being masked out.
139 C> - gfld\%numoct_opt Number of octets needed for each additional grid
140 C> points definition. Used to define number of points in each row (or column)
141 C> for non-regular grids. = 0, if using regular grid.
142 C> - gfld\%interp_opt Interpretation of list for optional points definition.(Code Table 3.11)
143 C> - gfld\%igdtnum Grid Definition Template Number (Code Table 3.1)
144 C> - gfld\%igdtmpl Contains the data values for the specified Grid
145 C> Definition Template (NN=gfld\%igdtnum). Each element of this integer array
146 C> contains an entry (in the order specified) of Grid Defintion
147 C> Template 3.NN This element is actually a pointer to an array
148 C> that holds the data.
149 C> - gfld\%igdtlen Number of elements in gfld\%igdtmpl. i.e. number of
150 C> entries in Grid Defintion Template 3.NN (NN=gfld\%igdtnum).
151 C> - gfld\%list_opt (Used if gfld\%numoct_opt .ne. 0) This array contains the
152 C> number of grid points contained in each row (or column).
153 C> (part of Section 3) This element is actually a pointer to
154 C> an array that holds the data. This pointer is nullified
155 C> if gfld\%numoct_opt=0.
156 C> - gfld\%num_opt (Used if gfld\%numoct_opt .ne. 0) The number of entries in
157 C> array ideflist. i.e. number of rows (or columns) for which
158 C> optional grid points are defined. This value is set to zero,
159 C> if gfld\%numoct_opt=0.
160 C> - gfdl\%ipdtnum Product Definition Template Number (see Code Table 4.0)
161 C> - gfld\%ipdtmpl Contains the data values for the specified Product Definition
162 C> Template (N=gfdl\%ipdtnum). Each element of this integer array
163 C> contains an entry (in the order specified) of Product Defintion
164 C> Template 4.N. This element is actually a pointer to an array
165 C> that holds the data.
166 C> - gfld\%ipdtlen Number of elements in gfld\%ipdtmpl. i.e. number of
167 C> entries in Product Defintion Template 4.N (N=gfdl\%ipdtnum).
168 C> - gfld\%coord_list Real array containing floating point values
169 C> intended to document the vertical discretisation associated to
170 C> model data on hybrid coordinate vertical levels.(part of Section 4)
171 C> This element is actually a pointer to an array
172 C> that holds the data.
173 C> - gfld\%num_coord number of values in array gfld\%coord_list.
174 C> - gfld\%ndpts Number of data points unpacked and returned.
175 C> Note that this number may be different from the value of
176 C> - gfld\%ngrdpts if a logical bitmap is in use with grid points
177 C> that are being masked out.
178 C> - gfld\%idrtnum Data Representation Template Number (see Code Table 5.0)
179 C> - gfld\%idrtmpl Contains the data values for the specified Data
180 C> Representation Template (N=gfld\%idrtnum). Each element of this
181 C> integer array contains an entry (in the order specified) of
182 C> Product Defintion Template 5.N. This element is actually a
183 C> pointer to an array that holds the data.
184 C> - gfld\%idrtlen Number of elements in gfld\%idrtmpl. i.e. number
185 C> of entries in Data Representation Template 5.N (N=gfld\%idrtnum).
186 C> - gfld\%unpacked logical value indicating whether the bitmap and
187 C> data values were unpacked. If false, gfld\%bmap and gfld\%fld
188 C> pointers are nullified.
189 C> - gfld\%expanded Logical value indicating whether the data field
190 C> was expanded to the grid in the case where a bit-map is present.
191 C> If true, the data points in gfld\%fld match the grid points and
192 C> zeros were inserted at grid points where data was bit-mapped out.
193 C> If false, the data values in gfld\%fld were not expanded to the
194 C> grid and are just a consecutive array of data points corresponding
195 C> to each value of "1" in gfld\%bmap.
196 C> - gfld\%ibmap Bitmap indicator (see Code Table 6.0)
197 C> 0 bitmap applies and is included in Section 6.
198 C> 1-253 Predefined bitmap applies
199 C> 254 Previously defined bitmap applies to this field
200 C> 255 Bit map does not apply to this product.
201 C> - gfld\%bmap Logical*1 array containing decoded bitmap, if ibmap=0
202 C> or ibap=254. Otherwise nullified. This element is actually a
203 C> pointer to an array that holds the data.
204 C> - gfld\%fld Array of gfld\%ndpts unpacked data points. This element
205 C> is actually a pointer to an array that holds the data.
206 C> @param[out] IRET integer return code
207 C> - 0 all ok
208 C> - 96 error reading index
209 C> - 97 error reading grib file
210 C> - 99 request not found
211 C> - other gf_getfld grib2 unpacker return code
212 C>
213 C> @note specify an index file if feasible to increase speed.
214 C> do not engage the same logical unit from more than one processor.
215 C> Note that derived type gribfield contains pointers to many
216 C> arrays of data. The memory for these arrays is allocated
217 C> when the values in the arrays are set, to help minimize
218 C> problems with array overloading. Because of this users
219 C> are encouraged to free up this memory, when it is no longer
220 C> needed, by an explicit call to subroutine gf_free.
221 C>
222 C> @author Mark Iredell @date 1994-04-01
223 C>
224 
225  SUBROUTINE getgb2(LUGB,LUGI,J,JDISC,JIDS,JPDTN,JPDT,JGDTN,JGDT,
226  & UNPACK,K,GFLD,IRET)
228 
229  INTEGER,INTENT(IN) :: LUGB,LUGI,J,JDISC,JPDTN,JGDTN
230  INTEGER,DIMENSION(:) :: JIDS(*),JPDT(*),JGDT(*)
231  LOGICAL,INTENT(IN) :: UNPACK
232  INTEGER,INTENT(OUT) :: K,IRET
233  TYPE(gribfield),INTENT(OUT) :: GFLD
234 
235  CHARACTER(LEN=1),POINTER,DIMENSION(:) :: CBUF
236 
237 C - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
238 C DECLARE INTERFACES (REQUIRED FOR CBUF POINTER)
239  INTERFACE
240  SUBROUTINE getidx(LUGB,LUGI,CBUF,NLEN,NNUM,IRGI)
241  CHARACTER(LEN=1),POINTER,DIMENSION(:) :: CBUF
242  INTEGER,INTENT(IN) :: LUGB,LUGI
243  INTEGER,INTENT(OUT) :: NLEN,NNUM,IRGI
244  END SUBROUTINE getidx
245  END INTERFACE
246 C - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
247 C DETERMINE WHETHER INDEX BUFFER NEEDS TO BE INITIALIZED
248  irgi=0
249  CALL getidx(lugb,lugi,cbuf,nlen,nnum,irgi)
250  IF(irgi.GT.1) THEN
251  iret=96
252  RETURN
253  ENDIF
254 C - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
255 C SEARCH INDEX BUFFER
256  CALL getgb2s(cbuf,nlen,nnum,j,jdisc,jids,jpdtn,jpdt,jgdtn,jgdt,
257  & jk,gfld,lpos,irgs)
258  IF(irgs.NE.0) THEN
259  iret=99
260  CALL gf_free(gfld)
261  RETURN
262  ENDIF
263 C - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
264 C READ LOCAL USE SECTION, IF AVAILABLE
265  CALL getgb2l(lugb,cbuf(lpos),gfld,iret)
266 C - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
267 C READ AND UNPACK GRIB RECORD
268  IF (unpack) THEN
269  ! NUMFLD=GFLD%IFLDNUM
270  ! CALL GF_FREE(GFLD)
271  CALL getgb2r(lugb,cbuf(lpos),gfld,iret)
272  ENDIF
273  k=jk
274 C - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
275  RETURN
276  END
getidx
subroutine getidx(LUGB, LUGI, CINDEX, NLEN, NNUM, IRET)
This subroutine finds, reads or generates a grib2 index for the grib2 file associated with unit lugb.
Definition: getidx.f:47
getgb2s
subroutine getgb2s(CBUF, NLEN, NNUM, J, JDISC, JIDS, JPDTN, JPDT, JGDTN, JGDT, K, GFLD, LPOS, IRET)
This subroutine find in the index file for a reference to the grib field requested.
Definition: getgb2s.f:245
getgb2
subroutine getgb2(LUGB, LUGI, J, JDISC, JIDS, JPDTN, JPDT, JGDTN, JGDT, UNPACK, K, GFLD, IRET)
This subroutine find and unpack a grib message.
Definition: getgb2.f:227
grib_mod::gribfield
Definition: gribmod.f:155
grib_mod
PROGRAM HISTORY LOG:
Definition: gribmod.f:151
getgb2r
subroutine getgb2r(LUGB, CINDEX, GFLD, IRET)
This subroutine read and unpack sections 6 and 7 from a grib2 message.
Definition: getgb2r.f:161
gf_free
subroutine gf_free(gfld)
This subroutine frees up memory that was used to store array values in derived type gribfield.
Definition: gf_free.f:139
getgb2l
subroutine getgb2l(LUGB, CINDEX, GFLD, IRET)
This subroutine read and unpack a local use section from a grib2 message.
Definition: getgb2l.f:157