NCEPLIBS-g2  3.4.5
getgb2r.f
Go to the documentation of this file.
1 C> @file
2 C> @brief This subroutine read and unpack sections 6 and 7 from ah
3 C> grib2 message.
4 C> @author Stephen Gilbert @date 2002-01-11
5 C>
6 
7 C> This subroutine read and unpack sections 6 and 7 from a grib2
8 C> message. It assumes that the "metadata" for this field already
9 C> exists in derived type gribfield. Specifically, it requires
10 C> gfld\%ibmap, gfld\%ngrdpts, gfld\%idrtnum, gfld\%idrtmpl, and
11 C> gfld\%ndpts. It decoded information for the selected grib field and
12 C> returned it in a derived type variable, gfld. gfld is of type
13 C> gribfield, which is defined in module grib_mod, so users of this
14 C> routine will need to include the line "use grib_mod" in their
15 C> calling routine. Each component of the gribfield type is described
16 C> in the output argument list section below.
17 C>
18 C> @param[in] LUGB integer unit of the unblocked grib data file.
19 C> @param[in] CINDEX index record of the grib field (see docblock of
20 C> subroutine ixgb2() for description of an index record.)
21 C> @param[out] GFLD derived type gribfield (defined in module grib_mod)
22 C> (NOTE: See Remarks Section)
23 C> - gfld\%version GRIB edition number (currently 2)
24 C> - gfld\%discipline Message Discipline (see Code Table 0.0)
25 C> - gfld\%idsect Contains the entries in the Identification Section
26 C> (Section 1) This element is actually a pointer to an array
27 C> that holds the data.
28 C> - gfld\%idsect(1) Identification of originating Centre
29 C> (see Common Code Table C-1) 7 US National Weather Service
30 C> - gfld\%idsect(2) Identification of originating Sub-centre
31 C> - gfld\%idsect(3) GRIB Master Tables Version Number
32 C> (see Code Table 1.0) 0 Experimental; 1 Initial operational version number
33 C> - gfld\%idsect(4) GRIB Local Tables Version Number (see Code Table 1.1)
34 C> - 0 Local tables not used
35 C> - 0 1-254 Number of local tables version used
36 C> - gfld\%idsect(5) Significance of Reference Time (Code Table 1.2)
37 C> - 0 Analysis
38 C> - 1 Start of forecast
39 C> - 2 Verifying time of forecast
40 C> - 3 Observation time.
41 C> - gfld\%idsect(6) Year (4 digits)
42 C> - gfld\%idsect(7) Month
43 C> - gfld\%idsect(8) Day
44 C> - gfld\%idsect(9) Hour
45 C> - gfld\%idsect(10) Minute
46 C> - gfld\%idsect(11) Second
47 C> - gfld\%idsect(12) Production status of processed data (see Code
48 C> Table 1.3)
49 C> - 0 Operational products
50 C> - 1 Operational test products
51 C> - 2 Research products
52 C> - 3 Re-analysis products
53 C> - gfld\%idsect(13) Type of processed data (see Code Table 1.4)
54 C> - 0 Analysis products
55 C> - 1 Forecast products
56 C> - 2 Analysis and forecast products
57 C> - 3 Control forecast products
58 C> - 4 Perturbed forecast products
59 C> - 5 Control and perturbed forecast products
60 C> - 6 Processed satellite observations
61 C> - 7 Processed radar observations
62 C> - gfld\%idsectlen Number of elements in gfld\%idsect
63 C> - gfld\%local Pointer to character array containing contents
64 C> of Local Section 2, if included
65 C> - gfld\%locallen length of array gfld\%local
66 C> - gfld\%ifldnum field number within GRIB message
67 C> - gfld\%griddef Source of grid definition (see Code Table 3.0)
68 C> - 0 Specified in Code table 3.1
69 C> - 1 Predetermined grid Defined by originating centre
70 C> - gfld\%ngrdpts Number of grid points in the defined grid.
71 C> Note that the number of actual data values returned from getgb2
72 C> (in gfld\%ndpts) may be less than this value if a logical bitmap
73 C> is in use with grid points that are being masked out.
74 C> - gfld\%numoct_opt Number of octets needed for each additional grid
75 C> points definition. Used to define number of points in each row (or
76 C> column) for non-regular grids. = 0, if using regular grid.
77 C> - gfld\%interp_opt Interpretation of list for optional points
78 C> definition.(Code Table 3.11)
79 C> - gfld\%igdtnum Grid Definition Template Number (Code Table 3.1)
80 C> - gfld\%igdtmpl Contains the data values for the specified Grid
81 C> Definition Template (NN=gfld\%igdtnum). Each element of this
82 C> integer array contains an entry (in the order specified) of Grid
83 C> Defintion Template 3.NN This element is actually a pointer to an
84 C> array that holds the data.
85 C> - gfld\%igdtlen Number of elements in gfld\%igdtmpl. i.e. number
86 C> of entries in Grid Defintion Template 3.NN (NN=gfld\%igdtnum).
87 C> - gfld\%list_opt (Used if gfld\%numoct_opt .ne. 0) This array
88 C> contains the number of grid points contained in each row (or
89 C> column). (part of Section 3) This element is actually a pointer
90 C> to an array that holds the data. This pointer is nullified
91 C> if gfld\%numoct_opt=0.
92 C> - gfld\%num_opt (Used if gfld\%numoct_opt .ne. 0) The number of
93 C> entries in array ideflist. i.e. number of rows (or columns) for which
94 C> optional grid points are defined. This value is set to zero,
95 C> if gfld\%numoct_opt=0.
96 C> - gfdl\%ipdtnum Product Definition Template Number (Code Table 4.0)
97 C> - gfld\%ipdtmpl Contains the data values for the specified Product
98 C> Definition Template (N=gfdl\%ipdtnum). Each element of this integer
99 C> array contains an entry (in the order specified) of Product Defintion
100 C> Template 4.N. This element is actually a pointer to an array
101 C> that holds the data.
102 C> - gfld\%ipdtlen Number of elements in gfld\%ipdtmpl. i.e. number of
103 C> entries in Product Defintion Template 4.N (N=gfdl\%ipdtnum).
104 C> - gfld\%coord_list Real array containing floating point values
105 C> intended to document the vertical discretisation associated to
106 C> model data on hybrid coordinate vertical levels.(part of Section 4)
107 C> This element is actually a pointer to an array
108 C> that holds the data.
109 C> - gfld\%num_coord number of values in array gfld\%coord_list.
110 C> - gfld\%ndpts Number of data points unpacked and returned.
111 C> Note that this number may be different from the value of
112 C> - gfld\%ngrdpts if a logical bitmap is in use with grid points
113 C> that are being masked out.
114 C> - gfld\%idrtnum Data Representation Template Number (Code Table 5.0)
115 C> - gfld\%idrtmpl Contains the data values for the specified Data
116 C> Representation Template (N=gfld\%idrtnum). Each element of this
117 C> integer array contains an entry (in the order specified) of
118 C> Product Defintion Template 5.N. This element is actually a
119 C> pointer to an array that holds the data.
120 C> - gfld\%idrtlen Number of elements in gfld\%idrtmpl. i.e. number
121 C> of entries in Data Representation Template 5.N (N=gfld\%idrtnum).
122 C> - gfld\%unpacked logical value indicating whether the bitmap and
123 C> data values were unpacked. If false, gfld\%bmap and gfld\%fld
124 C> pointers are nullified.
125 C> - gfld\%expanded Logical value indicating whether the data field
126 C> was expanded to the grid in the case where a bit-map is present.
127 C> If true, the data points in gfld\%fld match the grid points and
128 C> zeros were inserted at grid points where data was bit-mapped out.
129 C> If false, the data values in gfld\%fld were not expanded to the
130 C> grid and are just a consecutive array of data points corresponding
131 C> to each value of "1" in gfld\%bmap.
132 C> - gfld\%ibmap Bitmap indicator (see Code Table 6.0)
133 C> - 0 bitmap applies and is included in Section 6.
134 C> - 1-253 Predefined bitmap applies
135 C> - 254 Previously defined bitmap applies to this field
136 C> - 255 Bit map does not apply to this product.
137 C> - gfld\%bmap Logical*1 array containing decoded bitmap, if ibmap=0
138 C> or ibap=254. Otherwise nullified. This element is actually a
139 C> pointer to an array that holds the data.
140 C> - gfld\%fld Array of gfld\%ndpts unpacked data points. This element
141 C> is actually a pointer to an array that holds the data.
142 C> @param[out] IRET integer return code
143 C> - 0 all ok
144 C> - 97 error reading grib file
145 C> - other gf_getfld grib2 unpacker return code
146 C>
147 C> @note do not engage the same logical unit from more than one
148 C> processor. This subprogram is intended for private use by getgb2
149 C> routines only. Note that derived type gribfield contains pointers
150 C> to many arrays of data. The memory for these arrays is allocated
151 C> when the values in the arrays are set, to help minimize problems
152 C> with array overloading. Because of this users are encouraged to
153 C> free up this memory, when it is no longer needed, by an explicit
154 C> call to subroutine gf_free().
155 C>
156 C> @author Stephen Gilbert @date 2002-01-11
157 C>
158 
159 C-----------------------------------------------------------------------
160  SUBROUTINE getgb2r(LUGB,CINDEX,GFLD,IRET)
161 
162  USE grib_mod
163 
164  INTEGER,INTENT(IN) :: LUGB
165  CHARACTER(LEN=1),INTENT(IN) :: CINDEX(*)
166  INTEGER,INTENT(OUT) :: IRET
167  TYPE(gribfield) :: GFLD
168 
169  INTEGER :: LSKIP,SKIP6,SKIP7
170  CHARACTER(LEN=1):: CSIZE(4)
171  CHARACTER(LEN=1),ALLOCATABLE :: CTEMP(:)
172  real,pointer,dimension(:) :: newfld
173 
174  interface
175  subroutine gf_unpack6(cgrib,lcgrib,iofst,ngpts,ibmap,
176  & bmap,ierr)
177  character(len=1),intent(in) :: cgrib(lcgrib)
178  integer,intent(in) :: lcgrib,ngpts
179  integer,intent(inout) :: iofst
180  integer,intent(out) :: ibmap
181  integer,intent(out) :: ierr
182  logical*1,pointer,dimension(:) :: bmap
183  end subroutine gf_unpack6
184  subroutine gf_unpack7(cgrib,lcgrib,iofst,igdsnum,igdstmpl,
185  & idrsnum,idrstmpl,ndpts,fld,ierr)
186  character(len=1),intent(in) :: cgrib(lcgrib)
187  integer,intent(in) :: lcgrib,ndpts,idrsnum,igdsnum
188  integer,intent(inout) :: iofst
189  integer,pointer,dimension(:) :: idrstmpl,igdstmpl
190  integer,intent(out) :: ierr
191  real,pointer,dimension(:) :: fld
192  end subroutine gf_unpack7
193  end interface
194 C - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
195 C GET INFO
196  NULLIFY(gfld%bmap,gfld%fld)
197  iret=0
198  CALL g2_gbytec(cindex,lskip,4*8,4*8)
199  CALL g2_gbytec(cindex,skip6,24*8,4*8)
200  CALL g2_gbytec(cindex,skip7,28*8,4*8)
201 
202 C - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
203 C READ AND UNPACK BIT_MAP, IF PRESENT
204  IF ( gfld%ibmap.eq.0.OR.gfld%ibmap.eq.254 ) THEN
205  iskip=lskip+skip6
206  CALL baread(lugb,iskip,4,lread,csize) ! GET LENGTH OF SECTION
207  CALL g2_gbytec(csize,ilen,0,32)
208  ALLOCATE(ctemp(ilen))
209  CALL baread(lugb,iskip,ilen,lread,ctemp) ! READ IN SECTION
210  IF (ilen.NE.lread) THEN
211  iret=97
212  DEALLOCATE(ctemp)
213  RETURN
214  ENDIF
215  iofst=0
216  CALL gf_unpack6(ctemp,ilen,iofst,gfld%ngrdpts,idum,
217  & gfld%bmap,ierr)
218  IF (ierr.NE.0) THEN
219  iret=98
220  DEALLOCATE(ctemp)
221  RETURN
222  ENDIF
223  DEALLOCATE(ctemp)
224  ENDIF
225 C - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
226 C READ AND UNPACK DATA FIELD
227  iskip=lskip+skip7
228  CALL baread(lugb,iskip,4,lread,csize) ! GET LENGTH OF SECTION
229  CALL g2_gbytec(csize,ilen,0,32)
230  if (ilen.lt.6) ilen=6
231  ALLOCATE(ctemp(ilen))
232  CALL baread(lugb,iskip,ilen,lread,ctemp) ! READ IN SECTION
233  IF (ilen.NE.lread) THEN
234  iret=97
235  DEALLOCATE(ctemp)
236  RETURN
237  ENDIF
238  iofst=0
239  CALL gf_unpack7(ctemp,ilen,iofst,gfld%igdtnum,gfld%igdtmpl,
240  & gfld%idrtnum,gfld%idrtmpl,gfld%ndpts,
241  & gfld%fld,ierr)
242  IF (ierr.NE.0) THEN
243  iret=98
244  DEALLOCATE(ctemp)
245  RETURN
246  ENDIF
247  DEALLOCATE(ctemp)
248 C - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
249  ! If bitmap is used with this field, expand data field
250  ! to grid, if possible.
251  if ( gfld%ibmap .ne. 255 .AND. associated(gfld%bmap) ) then
252  allocate(newfld(gfld%ngrdpts))
253  !newfld=0.0
254  !newfld=unpack(lgfld%fld,lgfld%bmap,newfld)
255  n=1
256  do j=1,gfld%ngrdpts
257  if ( gfld%bmap(j) ) then
258  newfld(j)=gfld%fld(n)
259  n=n+1
260  else
261  newfld(j)=0.0
262  endif
263  enddo
264  deallocate(gfld%fld);
265  gfld%fld=>newfld;
266  gfld%expanded=.true.
267  else
268  gfld%expanded=.true.
269  endif
270 C - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
271  RETURN
272  END
gf_unpack7
subroutine gf_unpack7(cgrib, lcgrib, iofst, igdsnum, igdstmpl, idrsnum, idrstmpl, ndpts, fld, ierr)
This subroutine unpacks Section 7 (Data Section).
Definition: gf_unpack7.f:44
gf_unpack6
subroutine gf_unpack6(cgrib, lcgrib, iofst, ngpts, ibmap, bmap, ierr)
This subroutine unpacks Section 6 (Bit-Map Section) starting at octet 6 of that Section.
Definition: gf_unpack6.f:37
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
g2_gbytec
subroutine g2_gbytec(IN, IOUT, ISKIP, NBYTE)
This subrountine is to extract arbitrary size values from a packed bit string, right justifying each ...
Definition: g2_gbytesc.f:20