NCEPLIBS-g2  3.4.5
putgb2.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 packs a single field into a grib2 message and
8 C> writes out that message to the file associated with unit lugb.
9 C> note that file/unit lugb should be opened woth a call to
10 C> subroutine baopenw before this routine is called.
11 C> The information to be packed into the grib field is stored in a
12 C> derived type variable, gfld. gfld is of type gribfield, which is
13 C> defined in module grib_mod, so users of this routine will need to
14 C> include the line "use grib_mod" in their calling routine. Each
15 C> component of the gribfield type is described in the input argument
16 C> list section below.
17 C>
18 C> PROGRAM HISTORY LOG:
19 C> - 2002-04-22 Stephen Gilbert
20 C> - 2005-02-28 Stephen Gilbert Changed dimension of array cgrib to be
21 C> a multiple of gfld%ngrdpts instead of gfld%ndpts.
22 C> - 2009-03-10Boi Vuong Initialize variable coordlist.
23 C> - 2011-06-09Boi Vuong Initialize variable gfld%list_opt.
24 C> - 2012-02-28Boi Vuong Initialize variable ilistopt.
25 C>
26 C> @param[in] LUGB integer unit of the unblocked grib data file.
27 C> file must be opened with baopen or baopenw before calling this
28 C> routine.
29 C> @param[in] GFLD derived type gribfield (defined in module grib_mod)
30 C> (NOTE: See Remarks Section)
31 C> - gfld\%version GRIB edition number (currently 2)
32 C> - gfld\%discipline Message Discipline (see Code Table 0.0)
33 C> - gfld\%idsect Contains the entries in the Identification Section
34 C> (Section 1) This element is actually a pointer to an array
35 C> that holds the data.
36 C> - gfld\%idsect(1) Identification of originating Centre
37 C> (see Common Code Table C-1) 7 US National Weather Service
38 C> - gfld\%idsect(2) Identification of originating Sub-centre
39 C> - gfld\%idsect(3) GRIB Master Tables Version Number
40 C> (see Code Table 1.0) 0 Experimental; 1 Initial operational version number
41 C> - gfld\%idsect(4) GRIB Local Tables Version Number (see Code Table 1.1)
42 C> - 0 Local tables not used
43 C> - 0 1-254 Number of local tables version used
44 C> - gfld\%idsect(5) Significance of Reference Time (Code Table 1.2)
45 C> - 0 Analysis
46 C> - 1 Start of forecast
47 C> - 2 Verifying time of forecast
48 C> - 3 Observation time.
49 C> - gfld\%idsect(6) Year (4 digits)
50 C> - gfld\%idsect(7) Month
51 C> - gfld\%idsect(8) Day
52 C> - gfld\%idsect(9) Hour
53 C> - gfld\%idsect(10) Minute
54 C> - gfld\%idsect(11) Second
55 C> - gfld\%idsect(12) Production status of processed data (see Code
56 C> Table 1.3)
57 C> - 0 Operational products
58 C> - 1 Operational test products
59 C> - 2 Research products
60 C> - 3 Re-analysis products
61 C> - gfld\%idsect(13) Type of processed data (see Code Table 1.4)
62 C> - 0 Analysis products
63 C> - 1 Forecast products
64 C> - 2 Analysis and forecast products
65 C> - 3 Control forecast products
66 C> - 4 Perturbed forecast products
67 C> - 5 Control and perturbed forecast products
68 C> - 6 Processed satellite observations
69 C> - 7 Processed radar observations
70 C> - gfld\%idsectlen Number of elements in gfld\%idsect
71 C> - gfld\%local Pointer to character array containing contents
72 C> of Local Section 2, if included
73 C> - gfld\%locallen length of array gfld\%local
74 C> - gfld\%ifldnum field number within GRIB message
75 C> - gfld\%griddef Source of grid definition (see Code Table 3.0)
76 C> - 0 Specified in Code table 3.1
77 C> - 1 Predetermined grid Defined by originating centre
78 C> - gfld\%ngrdpts Number of grid points in the defined grid.
79 C> Note that the number of actual data values returned from getgb2
80 C> (in gfld\%ndpts) may be less than this value if a logical bitmap
81 C> is in use with grid points that are being masked out.
82 C> - gfld\%numoct_opt Number of octets needed for each additional grid
83 C> points definition. Used to define number of points in each row (or
84 C> column) for non-regular grids. = 0, if using regular grid.
85 C> - gfld\%interp_opt Interpretation of list for optional points
86 C> definition.(Code Table 3.11)
87 C> - gfld\%igdtnum Grid Definition Template Number (Code Table 3.1)
88 C> - gfld\%igdtmpl Contains the data values for the specified Grid
89 C> Definition Template (NN=gfld\%igdtnum). Each element of this
90 C> integer array contains an entry (in the order specified) of Grid
91 C> Defintion Template 3.NN This element is actually a pointer to an
92 C> array that holds the data.
93 C> - gfld\%igdtlen Number of elements in gfld\%igdtmpl. i.e. number
94 C> of entries in Grid Defintion Template 3.NN (NN=gfld\%igdtnum).
95 C> - gfld\%list_opt (Used if gfld\%numoct_opt .ne. 0) This array
96 C> contains the number of grid points contained in each row (or
97 C> column). (part of Section 3) This element is actually a pointer
98 C> to an array that holds the data. This pointer is nullified
99 C> if gfld\%numoct_opt=0.
100 C> - gfld\%num_opt (Used if gfld\%numoct_opt .ne. 0) The number of
101 C> entries in array ideflist. i.e. number of rows (or columns) for which
102 C> optional grid points are defined. This value is set to zero,
103 C> if gfld\%numoct_opt=0.
104 C> - gfdl\%ipdtnum Product Definition Template Number (Code Table 4.0)
105 C> - gfld\%ipdtmpl Contains the data values for the specified Product
106 C> Definition Template (N=gfdl\%ipdtnum). Each element of this integer
107 C> array contains an entry (in the order specified) of Product Defintion
108 C> Template 4.N. This element is actually a pointer to an array
109 C> that holds the data.
110 C> - gfld\%ipdtlen Number of elements in gfld\%ipdtmpl. i.e. number of
111 C> entries in Product Defintion Template 4.N (N=gfdl\%ipdtnum).
112 C> - gfld\%coord_list Real array containing floating point values
113 C> intended to document the vertical discretisation associated to
114 C> model data on hybrid coordinate vertical levels.(part of Section 4)
115 C> This element is actually a pointer to an array
116 C> that holds the data.
117 C> - gfld\%num_coord number of values in array gfld\%coord_list.
118 C> - gfld\%ndpts Number of data points unpacked and returned.
119 C> Note that this number may be different from the value of
120 C> - gfld\%ngrdpts if a logical bitmap is in use with grid points
121 C> that are being masked out.
122 C> - gfld\%idrtnum Data Representation Template Number (Code Table 5.0)
123 C> - gfld\%idrtmpl Contains the data values for the specified Data
124 C> Representation Template (N=gfld\%idrtnum). Each element of this
125 C> integer array contains an entry (in the order specified) of
126 C> Product Defintion Template 5.N. This element is actually a
127 C> pointer to an array that holds the data.
128 C> - gfld\%idrtlen Number of elements in gfld\%idrtmpl. i.e. number
129 C> of entries in Data Representation Template 5.N (N=gfld\%idrtnum).
130 C> - gfld\%unpacked logical value indicating whether the bitmap and
131 C> data values were unpacked. If false, gfld\%bmap and gfld\%fld
132 C> pointers are nullified.
133 C> - gfld\%expanded Logical value indicating whether the data field
134 C> was expanded to the grid in the case where a bit-map is present.
135 C> If true, the data points in gfld\%fld match the grid points and
136 C> zeros were inserted at grid points where data was bit-mapped out.
137 C> If false, the data values in gfld\%fld were not expanded to the
138 C> grid and are just a consecutive array of data points corresponding
139 C> to each value of "1" in gfld\%bmap.
140 C> - gfld\%ibmap Bitmap indicator (see Code Table 6.0)
141 C> - 0 bitmap applies and is included in Section 6.
142 C> - 1-253 Predefined bitmap applies
143 C> - 254 Previously defined bitmap applies to this field
144 C> - 255 Bit map does not apply to this product.
145 C> - gfld\%bmap Logical*1 array containing decoded bitmap, if ibmap=0
146 C> or ibap=254. Otherwise nullified. This element is actually a
147 C> pointer to an array that holds the data.
148 C> - gfld\%fld Array of gfld\%ndpts unpacked data points. This element
149 C> is actually a pointer to an array that holds the data.
150 C> @param[out] IRET integer return code
151 C> - 0 all ok.
152 C> - 2 Memory allocation error.
153 C> - 10 No Section 1 info available.
154 C> - 11 No Grid Definition Template info available.
155 C> - 12 Missing some required data field info.
156 C>
157 C> @note That derived type gribfield contains pointers to
158 C> many arrays of data. The memory for these arrays is allocated
159 C> when the values in the arrays are set, to help minimize problems
160 C> with array overloading. Because of this users are encouraged to
161 C> free up this memory, when it is no longer needed, by an explicit
162 C> call to subroutine gf_free().
163 C>
164 C> @author Stephen Gilbert @date 2002-04-22
165 C>
166 
167 C-----------------------------------------------------------------------
168  SUBROUTINE putgb2(LUGB,GFLD,IRET)
169 
170  USE grib_mod
171 
172  INTEGER,INTENT(IN) :: LUGB
173  TYPE(gribfield),INTENT(IN) :: GFLD
174  INTEGER,INTENT(OUT) :: IRET
175 
176  CHARACTER(LEN=1),ALLOCATABLE,DIMENSION(:) :: CGRIB
177  integer :: listsec0(2)
178  integer :: igds(5)
179  real :: coordlist
180  integer :: ilistopt
181 
182  listsec0=(/0,2/)
183  igds=(/0,0,0,0,0/)
184  coordlist=0.0
185  ilistopt=0
186 C - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
187 C ALLOCATE ARRAY FOR GRIB2 FIELD
188  lcgrib=gfld%ngrdpts*4
189  allocate(cgrib(lcgrib),stat=is)
190  if ( is.ne.0 ) then
191  print *,'putgb2: cannot allocate memory. ',is
192  iret=2
193  endif
194 C - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
195 C CREATE NEW MESSAGE
196  listsec0(1)=gfld%discipline
197  listsec0(2)=gfld%version
198  if ( associated(gfld%idsect) ) then
199  call gribcreate(cgrib,lcgrib,listsec0,gfld%idsect,ierr)
200  if (ierr.ne.0) then
201  write(6,*) 'putgb2: ERROR creating new GRIB2 field = ',ierr
202  endif
203  else
204  print *,'putgb2: No Section 1 info available. '
205  iret=10
206  deallocate(cgrib)
207  return
208  endif
209 C - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
210 C ADD LOCAL USE SECTION TO GRIB2 MESSAGE
211  if ( associated(gfld%local).AND.gfld%locallen.gt.0 ) then
212  call addlocal(cgrib,lcgrib,gfld%local,gfld%locallen,ierr)
213  if (ierr.ne.0) then
214  write(6,*) 'putgb2: ERROR adding local info = ',ierr
215  endif
216  endif
217 C - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
218 C ADD GRID TO GRIB2 MESSAGE
219  igds(1)=gfld%griddef
220  igds(2)=gfld%ngrdpts
221  igds(3)=gfld%numoct_opt
222  igds(4)=gfld%interp_opt
223  igds(5)=gfld%igdtnum
224  if ( associated(gfld%igdtmpl) ) then
225  call addgrid(cgrib,lcgrib,igds,gfld%igdtmpl,gfld%igdtlen,
226  & ilistopt,gfld%num_opt,ierr)
227  if (ierr.ne.0) then
228  write(6,*) 'putgb2: ERROR adding grid info = ',ierr
229  endif
230  else
231  print *,'putgb2: No GDT info available. '
232  iret=11
233  deallocate(cgrib)
234  return
235  endif
236 C - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
237 C ADD DATA FIELD TO GRIB2 MESSAGE
238  if ( associated(gfld%ipdtmpl).AND.
239  & associated(gfld%idrtmpl).AND.
240  & associated(gfld%fld) ) then
241  call addfield(cgrib,lcgrib,gfld%ipdtnum,gfld%ipdtmpl,
242  & gfld%ipdtlen,coordlist,gfld%num_coord,
243  & gfld%idrtnum,gfld%idrtmpl,gfld%idrtlen,
244  & gfld%fld,gfld%ngrdpts,gfld%ibmap,gfld%bmap,
245  & ierr)
246  if (ierr.ne.0) then
247  write(6,*) 'putgb2: ERROR adding data field = ',ierr
248  endif
249  else
250  print *,'putgb2: Missing some field info. '
251  iret=12
252  deallocate(cgrib)
253  return
254  endif
255 C - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
256 C CLOSE GRIB2 MESSAGE AND WRITE TO FILE
257  call gribend(cgrib,lcgrib,lengrib,ierr)
258  call wryte(lugb,lengrib,cgrib)
259 C - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
260  deallocate(cgrib)
261  RETURN
262  END
addfield
subroutine addfield(cgrib, lcgrib, ipdsnum, ipdstmpl, ipdstmplen, coordlist, numcoord, idrsnum, idrstmpl, idrstmplen, fld, ngrdpts, ibmap, bmap, ierr)
This subroutine packs up Sections 4 through 7 for a given field and adds them to a GRIB2 message.
Definition: addfield.f:91
grib_mod::gribfield
Definition: gribmod.f:155
grib_mod
PROGRAM HISTORY LOG:
Definition: gribmod.f:151
addlocal
subroutine addlocal(cgrib, lcgrib, csec2, lcsec2, ierr)
This subroutine adds a Local Use Section (Section 2) to a GRIB2 message.
Definition: addlocal.f:32
gribcreate
subroutine gribcreate(cgrib, lcgrib, listsec0, listsec1, ierr)
This subroutine initializes a new GRIB2 message and packs GRIB2 sections 0 (Indicator Section) and 1 ...
Definition: gribcreate.f:48
putgb2
subroutine putgb2(LUGB, GFLD, IRET)
This subroutine packs a single field into a grib2 message and writes out that message to the file ass...
Definition: putgb2.f:169
gribend
subroutine gribend(cgrib, lcgrib, lengrib, ierr)
This subroutine finalizes a GRIB message after all grids and fields have been added.
Definition: gribend.f:32
addgrid
subroutine addgrid(cgrib, lcgrib, igds, igdstmpl, igdstmplen, ideflist, idefnum, ierr)
This routine is used with routines gribcreate(), addlocal(), addfield(), and gribend() to create a co...
Definition: addgrid.f:48