** TAG CREATION **
[WPS-merge.git] / ungrib / src / ngl / g2 / putgb2.f
blob6935b9d34386e5ed07fa7f9bfa55b3cb7b39c9bb
1 C-----------------------------------------------------------------------
2 SUBROUTINE PUTGB2(LUGB,GFLD,IRET)
3 C$$$ SUBPROGRAM DOCUMENTATION BLOCK
5 C SUBPROGRAM: PUTGB2 PACKS AND WRITES A GRIB2 MESSAGE
6 C PRGMMR: GILBERT ORG: W/NP11 DATE: 2002-04-22
8 C ABSTRACT: PACKS A SINGLE FIELD INTO A GRIB2 MESSAGE
9 C AND WRITES OUT THAT MESSAGE TO THE FILE ASSOCIATED WITH UNIT LUGB.
10 C NOTE THAT FILE/UNIT LUGB SHOULD BE OPENED WOTH A CALL TO
11 C SUBROUTINE BAOPENW BEFORE THIS ROUTINE IS CALLED.
13 C The information to be packed into the GRIB field
14 C is stored in a derived type variable, gfld.
15 C Gfld is of type gribfield, which is defined
16 C in module grib_mod, so users of this routine will need to include
17 C the line "USE GRIB_MOD" in their calling routine. Each component of the
18 C gribfield type is described in the INPUT ARGUMENT LIST section below.
20 C PROGRAM HISTORY LOG:
21 C 2002-04-22 GILBERT
22 C 2005-02-28 GILBERT - Changed dimension of array cgrib to be a multiple
23 C of gfld%ngrdpts instead of gfld%ndpts.
24 C 2009-03-10 VUONG - Initialize variable coordlist
25 C 2011-06-09 VUONG - Initialize variable gfld%list_opt
27 C USAGE: CALL PUTGB2(LUGB,GFLD,IRET)
28 C INPUT ARGUMENTS:
29 C LUGB INTEGER UNIT OF THE UNBLOCKED GRIB DATA FILE.
30 C FILE MUST BE OPENED WITH BAOPEN OR BAOPENW BEFORE CALLING
31 C THIS ROUTINE.
32 C gfld - derived type gribfield ( defined in module grib_mod )
33 C ( NOTE: See Remarks Section )
34 C gfld%version = GRIB edition number ( currently 2 )
35 C gfld%discipline = Message Discipline ( see Code Table 0.0 )
36 C gfld%idsect() = Contains the entries in the Identification
37 C Section ( Section 1 )
38 C This element is actually a pointer to an array
39 C that holds the data.
40 C gfld%idsect(1) = Identification of originating Centre
41 C ( see Common Code Table C-1 )
42 C 7 - US National Weather Service
43 C gfld%idsect(2) = Identification of originating Sub-centre
44 C gfld%idsect(3) = GRIB Master Tables Version Number
45 C ( see Code Table 1.0 )
46 C 0 - Experimental
47 C 1 - Initial operational version number
48 C gfld%idsect(4) = GRIB Local Tables Version Number
49 C ( see Code Table 1.1 )
50 C 0 - Local tables not used
51 C 1-254 - Number of local tables version used
52 C gfld%idsect(5) = Significance of Reference Time (Code Table 1.2)
53 C 0 - Analysis
54 C 1 - Start of forecast
55 C 2 - Verifying time of forecast
56 C 3 - Observation time
57 C gfld%idsect(6) = Year ( 4 digits )
58 C gfld%idsect(7) = Month
59 C gfld%idsect(8) = Day
60 C gfld%idsect(9) = Hour
61 C gfld%idsect(10) = Minute
62 C gfld%idsect(11) = Second
63 C gfld%idsect(12) = Production status of processed data
64 C ( see Code Table 1.3 )
65 C 0 - Operational products
66 C 1 - Operational test products
67 C 2 - Research products
68 C 3 - Re-analysis products
69 C gfld%idsect(13) = Type of processed data ( see Code Table 1.4 )
70 C 0 - Analysis products
71 C 1 - Forecast products
72 C 2 - Analysis and forecast products
73 C 3 - Control forecast products
74 C 4 - Perturbed forecast products
75 C 5 - Control and perturbed forecast products
76 C 6 - Processed satellite observations
77 C 7 - Processed radar observations
78 C gfld%idsectlen = Number of elements in gfld%idsect().
79 C gfld%local() = Pointer to character array containing contents
80 C of Local Section 2, if included
81 C gfld%locallen = length of array gfld%local()
82 C gfld%ifldnum = field number within GRIB message
83 C gfld%griddef = Source of grid definition (see Code Table 3.0)
84 C 0 - Specified in Code table 3.1
85 C 1 - Predetermined grid Defined by originating centre
86 C gfld%ngrdpts = Number of grid points in the defined grid.
87 C gfld%numoct_opt = Number of octets needed for each
88 C additional grid points definition.
89 C Used to define number of
90 C points in each row ( or column ) for
91 C non-regular grids.
92 C = 0, if using regular grid.
93 C gfld%interp_opt = Interpretation of list for optional points
94 C definition. (Code Table 3.11)
95 C gfld%igdtnum = Grid Definition Template Number (Code Table 3.1)
96 C gfld%igdtmpl() = Contains the data values for the specified Grid
97 C Definition Template ( NN=gfld%igdtnum ). Each
98 C element of this integer array contains an entry (in
99 C the order specified) of Grid Defintion Template 3.NN
100 C This element is actually a pointer to an array
101 C that holds the data.
102 C gfld%igdtlen = Number of elements in gfld%igdtmpl(). i.e. number of
103 C entries in Grid Defintion Template 3.NN
104 C ( NN=gfld%igdtnum ).
105 C gfld%list_opt() = (Used if gfld%numoct_opt .ne. 0) This array
106 C contains the number of grid points contained in
107 C each row ( or column ). (part of Section 3)
108 C This element is actually a pointer to an array
109 C that holds the data. This pointer is nullified
110 C if gfld%numoct_opt=0.
111 C gfld%num_opt = (Used if gfld%numoct_opt .ne. 0) The number of entries
112 C in array ideflist. i.e. number of rows ( or columns )
113 C for which optional grid points are defined. This value
114 C is set to zero, if gfld%numoct_opt=0.
115 C gfdl%ipdtnum = Product Definition Template Number (see Code Table 4.0)
116 C gfld%ipdtmpl() = Contains the data values for the specified Product
117 C Definition Template ( N=gfdl%ipdtnum ). Each element
118 C of this integer array contains an entry (in the
119 C order specified) of Product Defintion Template 4.N.
120 C This element is actually a pointer to an array
121 C that holds the data.
122 C gfld%ipdtlen = Number of elements in gfld%ipdtmpl(). i.e. number of
123 C entries in Product Defintion Template 4.N
124 C ( N=gfdl%ipdtnum ).
125 C gfld%coord_list() = Real array containing floating point values
126 C intended to document the vertical discretisation
127 C associated to model data on hybrid coordinate
128 C vertical levels. (part of Section 4)
129 C This element is actually a pointer to an array
130 C that holds the data.
131 C gfld%num_coord = number of values in array gfld%coord_list().
132 C gfld%ndpts = Number of data points unpacked and returned.
133 C gfld%idrtnum = Data Representation Template Number
134 C ( see Code Table 5.0)
135 C gfld%idrtmpl() = Contains the data values for the specified Data
136 C Representation Template ( N=gfld%idrtnum ). Each
137 C element of this integer array contains an entry
138 C (in the order specified) of Product Defintion
139 C Template 5.N.
140 C This element is actually a pointer to an array
141 C that holds the data.
142 C gfld%idrtlen = Number of elements in gfld%idrtmpl(). i.e. number
143 C of entries in Data Representation Template 5.N
144 C ( N=gfld%idrtnum ).
145 C gfld%unpacked = logical value indicating whether the bitmap and
146 C data values were unpacked. If false,
147 C gfld%bmap and gfld%fld pointers are nullified.
148 C gfld%ibmap = Bitmap indicator ( see Code Table 6.0 )
149 C 0 = bitmap applies and is included in Section 6.
150 C 1-253 = Predefined bitmap applies
151 C 254 = Previously defined bitmap applies to this field
152 C 255 = Bit map does not apply to this product.
153 C gfld%bmap() = Logical*1 array containing decoded bitmap,
154 C if ibmap=0 or ibap=254. Otherwise nullified.
155 C This element is actually a pointer to an array
156 C that holds the data.
157 C gfld%fld() = Array of gfld%ndpts unpacked data points.
158 C This element is actually a pointer to an array
159 C that holds the data.
161 C OUTPUT ARGUMENTS:
162 C IRET INTEGER RETURN CODE
163 C 0 ALL OK
164 C 2 MEMORY ALLOCATION ERROR
165 C 10 No Section 1 info available
166 C 11 No Grid Definition Template info available
167 C 12 Missing some required data field info
169 C SUBPROGRAMS CALLED:
170 C gribcreate Start a new grib2 message
171 C addlocal Add local section to a GRIB2 message
172 C addgrid Add grid info to a GRIB2 message
173 C addfield Add data field to a GRIB2 message
174 C gribend End GRIB2 message
176 C REMARKS:
178 C Note that derived type gribfield contains pointers to many
179 C arrays of data. The memory for these arrays is allocated
180 C when the values in the arrays are set, to help minimize
181 C problems with array overloading. Because of this users
182 C are encouraged to free up this memory, when it is no longer
183 C needed, by an explicit call to subroutine gf_free.
184 C ( i.e. CALL GF_FREE(GFLD) )
186 C ATTRIBUTES:
187 C LANGUAGE: FORTRAN 90
189 C$$$
190 USE GRIB_MOD
192 INTEGER,INTENT(IN) :: LUGB
193 TYPE(GRIBFIELD),INTENT(IN) :: GFLD
194 INTEGER,INTENT(OUT) :: IRET
196 CHARACTER(LEN=1),ALLOCATABLE,DIMENSION(:) :: CGRIB
197 integer :: listsec0(2)=(/0,2/)
198 integer :: igds(5)=(/0,0,0,0,0/)
199 real :: coordlist=0.0
201 C - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
202 C ALLOCATE ARRAY FOR GRIB2 FIELD
203 lcgrib=gfld%ngrdpts*4
204 allocate(cgrib(lcgrib),stat=is)
205 if ( is.ne.0 ) then
206 print *,'putgb2: cannot allocate memory. ',is
207 iret=2
208 endif
209 C - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
210 C CREATE NEW MESSAGE
211 listsec0(1)=gfld%discipline
212 listsec0(2)=gfld%version
213 if ( associated(gfld%idsect) ) then
214 call gribcreate(cgrib,lcgrib,listsec0,gfld%idsect,ierr)
215 if (ierr.ne.0) then
216 write(6,*) 'putgb2: ERROR creating new GRIB2 field = ',ierr
217 endif
218 else
219 print *,'putgb2: No Section 1 info available. '
220 iret=10
221 deallocate(cgrib)
222 return
223 endif
224 C - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
225 C ADD LOCAL USE SECTION TO GRIB2 MESSAGE
226 if ( associated(gfld%local).AND.gfld%locallen.gt.0 ) then
227 call addlocal(cgrib,lcgrib,gfld%local,gfld%locallen,ierr)
228 if (ierr.ne.0) then
229 write(6,*) 'putgb2: ERROR adding local info = ',ierr
230 endif
231 endif
232 C - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
233 C ADD GRID TO GRIB2 MESSAGE
234 igds(1)=gfld%griddef
235 igds(2)=gfld%ngrdpts
236 igds(3)=gfld%numoct_opt
237 igds(4)=gfld%interp_opt
238 igds(5)=gfld%igdtnum
239 if ( associated(gfld%igdtmpl) ) then
240 call addgrid(cgrib,lcgrib,igds,gfld%igdtmpl,gfld%igdtlen,
241 & gfld%list_opt,gfld%num_opt,ierr)
242 if (ierr.ne.0) then
243 write(6,*) 'putgb2: ERROR adding grid info = ',ierr
244 endif
245 else
246 print *,'putgb2: No GDT info available. '
247 iret=11
248 deallocate(cgrib)
249 return
250 endif
251 C - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
252 C ADD DATA FIELD TO GRIB2 MESSAGE
253 if ( associated(gfld%ipdtmpl).AND.
254 & associated(gfld%idrtmpl).AND.
255 & associated(gfld%fld) ) then
256 call addfield(cgrib,lcgrib,gfld%ipdtnum,gfld%ipdtmpl,
257 & gfld%ipdtlen,coordlist,gfld%num_coord,
258 & gfld%idrtnum,gfld%idrtmpl,gfld%idrtlen,
259 & gfld%fld,gfld%ngrdpts,gfld%ibmap,gfld%bmap,
260 & ierr)
261 if (ierr.ne.0) then
262 write(6,*) 'putgb2: ERROR adding data field = ',ierr
263 endif
264 else
265 print *,'putgb2: Missing some field info. '
266 iret=12
267 deallocate(cgrib)
268 return
269 endif
270 C - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
271 C CLOSE GRIB2 MESSAGE AND WRITE TO FILE
272 call gribend(cgrib,lcgrib,lengrib,ierr)
273 call wryte(lugb,lengrib,cgrib)
274 C - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
275 deallocate(cgrib)
276 RETURN