38 integer,
intent(in) :: lugb
40 integer,
intent(out) :: iret
42 character(len = 1),
allocatable,
dimension(:) :: cgrib
43 integer :: listsec0(2)
47 integer :: ierr, is, lcgrib, lengrib
50 igds = (/0, 0, 0, 0, 0/)
57 if (
associated(gfld%local) .AND. gfld%locallen .gt. 0)
then
58 lcgrib = lcgrib + gfld%locallen * 4
61 lcgrib = lcgrib + 512 + 512 + 512
63 if (gfld%ibmap .eq. 0)
then
64 lcgrib = lcgrib + gfld%ngrdpts
67 lcgrib = lcgrib + gfld%ngrdpts * 4
70 print *,
'putgb2 lugb ', lugb,
' lcgrib ', lcgrib
74 allocate(cgrib(lcgrib), stat = is)
76 print *,
'putgb2: cannot allocate memory. ', is
81 listsec0(1) = gfld%discipline
82 listsec0(2) = gfld%version
83 if (
associated(gfld%idsect))
then
84 call gribcreate(cgrib, lcgrib, listsec0, gfld%idsect, ierr)
86 write(6, *)
'putgb2: ERROR creating new GRIB2 field = ', ierr
89 print *,
'putgb2: No Section 1 info available. '
96 if (
associated(gfld%local) .AND. gfld%locallen .gt. 0)
then
97 call addlocal(cgrib, lcgrib, gfld%local, gfld%locallen, ierr)
99 write(6, *)
'putgb2: ERROR adding local info = ', ierr
104 igds(1) = gfld%griddef
105 igds(2) = gfld%ngrdpts
106 igds(3) = gfld%numoct_opt
107 igds(4) = gfld%interp_opt
108 igds(5) = gfld%igdtnum
109 if (
associated(gfld%igdtmpl))
then
110 call addgrid(cgrib, lcgrib, igds, gfld%igdtmpl, gfld%igdtlen, &
111 ilistopt, gfld%num_opt, ierr)
112 if (ierr .ne. 0)
then
113 write(6, *)
'putgb2: ERROR adding grid info = ', ierr
116 print *,
'putgb2: No GDT info available. '
123 if (
associated(gfld%ipdtmpl) .AND. &
124 associated(gfld%idrtmpl) .AND. &
125 associated(gfld%fld))
then
126 call addfield(cgrib, lcgrib, gfld%ipdtnum, gfld%ipdtmpl, &
127 gfld%ipdtlen, coordlist, gfld%num_coord, &
128 gfld%idrtnum, gfld%idrtmpl, gfld%idrtlen, &
129 gfld%fld, gfld%ngrdpts, gfld%ibmap, gfld%bmap, &
131 if (ierr .ne. 0)
then
132 write(6, *)
'putgb2: ERROR adding data field = ', ierr
135 print *,
'putgb2: Missing some field info. '
142 call gribend(cgrib, lcgrib, lengrib, ierr)
143 call wryte(lugb, lengrib, cgrib)
210 subroutine gf_getfld(cgrib, lcgrib, ifldnum, unpack, expand, gfld, ierr)
215 character(len = 1),
intent(in) :: cgrib(lcgrib)
216 integer,
intent(in) :: lcgrib, ifldnum
217 logical,
intent(in) :: unpack, expand
219 integer,
intent(out) :: ierr
221 character(len = 4),
parameter :: grib =
'GRIB', c7777 =
'7777'
222 character(len = 4) :: ctemp
223 real,
pointer,
dimension(:) :: newfld
224 integer:: listsec0(2), igds(5)
225 integer iofst, istart
226 logical*1,
pointer,
dimension(:) :: bmpsave
227 logical have3, have4, have5, have6, have7
230 integer :: numfld, j, lengrib, ipos, lensec0, lensec
231 integer :: isecnum, jerr, n, numlocal
234 subroutine gf_unpack1(cgrib, lcgrib, iofst, ids, idslen, ierr)
235 character(len = 1),
intent(in) :: cgrib(lcgrib)
236 integer,
intent(in) :: lcgrib
237 integer,
intent(inout) :: iofst
238 integer,
pointer,
dimension(:) :: ids
239 integer,
intent(out) :: ierr, idslen
241 subroutine gf_unpack2(cgrib, lcgrib, iofst, lencsec2, csec2, ierr)
242 character(len = 1),
intent(in) :: cgrib(lcgrib)
243 integer,
intent(in) :: lcgrib
244 integer,
intent(inout) :: iofst
245 integer,
intent(out) :: lencsec2
246 integer,
intent(out) :: ierr
247 character(len = 1),
pointer,
dimension(:) :: csec2
249 subroutine gf_unpack3(cgrib, lcgrib, iofst, igds, igdstmpl, &
250 mapgridlen, ideflist, idefnum, ierr)
251 character(len = 1),
intent(in) :: cgrib(lcgrib)
252 integer,
intent(in) :: lcgrib
253 integer,
intent(inout) :: iofst
254 integer,
pointer,
dimension(:) :: igdstmpl, ideflist
255 integer,
intent(out) :: igds(5)
256 integer,
intent(out) :: mapgridlen
257 integer,
intent(out) :: ierr, idefnum
259 subroutine gf_unpack4(cgrib, lcgrib, iofst, ipdsnum, ipdstmpl, &
260 mappdslen, coordlist, numcoord, ierr)
261 character(len = 1),
intent(in) :: cgrib(lcgrib)
262 integer,
intent(in) :: lcgrib
263 integer,
intent(inout) :: iofst
264 real,
pointer,
dimension(:) :: coordlist
265 integer,
pointer,
dimension(:) :: ipdstmpl
266 integer,
intent(out) :: ipdsnum
267 integer,
intent(out) :: mappdslen
268 integer,
intent(out) :: ierr, numcoord
270 subroutine gf_unpack5(cgrib, lcgrib, iofst, ndpts, idrsnum, &
271 idrstmpl, mapdrslen, ierr)
272 character(len = 1),
intent(in) :: cgrib(lcgrib)
273 integer,
intent(in) :: lcgrib
274 integer,
intent(inout) :: iofst
275 integer,
intent(out) :: ndpts, idrsnum
276 integer,
pointer,
dimension(:) :: idrstmpl
277 integer,
intent(out) :: mapdrslen
278 integer,
intent(out) :: ierr
280 subroutine gf_unpack6(cgrib, lcgrib, iofst, ngpts, ibmap, bmap, &
282 character(len = 1),
intent(in) :: cgrib(lcgrib)
283 integer,
intent(in) :: lcgrib, ngpts
284 integer,
intent(inout) :: iofst
285 integer,
intent(out) :: ibmap
286 integer,
intent(out) :: ierr
287 logical*1,
pointer,
dimension(:) :: bmap
289 subroutine gf_unpack7(cgrib, lcgrib, iofst, igdsnum, igdstmpl, &
290 idrsnum, idrstmpl, ndpts, fld, ierr)
291 character(len = 1),
intent(in) :: cgrib(lcgrib)
292 integer,
intent(in) :: lcgrib, ndpts, idrsnum, igdsnum
293 integer,
intent(inout) :: iofst
294 integer,
pointer,
dimension(:) :: idrstmpl, igdstmpl
295 integer,
intent(out) :: ierr
296 real,
pointer,
dimension(:) :: fld
308 nullify(gfld%list_opt, gfld%igdtmpl, gfld%ipdtmpl)
309 nullify(gfld%coord_list, gfld%idrtmpl, gfld%bmap, gfld%fld)
312 if (ifldnum .le. 0)
then
313 print *,
'gf_getfld: Request for field number ' &
322 ctemp = cgrib(j) // cgrib(j + 1) // cgrib(j + 2) // cgrib(j + 3)
323 if (ctemp .eq. grib)
then
328 if (istart .eq. 0)
then
329 print *,
'gf_getfld: Beginning characters GRIB not found.'
335 iofst = 8 * (istart + 5)
336 call g2_gbytec(cgrib, listsec0(1), iofst, 8)
338 call g2_gbytec(cgrib, listsec0(2), iofst, 8)
341 call g2_gbytec(cgrib, lengrib, iofst, 32)
344 ipos = istart + lensec0
347 if (listsec0(2) .ne. 2)
then
348 print *,
'gf_getfld: can only decode GRIB edition 2.'
358 ctemp = cgrib(ipos) // cgrib(ipos + 1) // cgrib(ipos + 2) // &
360 if (ctemp .eq. c7777)
then
363 if (ipos.ne.(istart + lengrib))
then
364 print *,
'gf_getfld: "7777" found, but not ' &
373 iofst = (ipos - 1) * 8
380 if ((isecnum .lt. 1) .or. (isecnum .gt. 7))
then
381 print *,
'gf_getfld: Unrecognized Section Encountered = ', &
388 if (isecnum .eq. 1)
then
390 call gf_unpack1(cgrib, lcgrib, iofst, gfld%idsect, &
391 gfld%idsectlen, jerr)
392 if (jerr .ne. 0)
then
400 if (isecnum .eq. 2)
then
402 if (
associated(gfld%local))
deallocate(gfld%local)
403 call gf_unpack2(cgrib, lcgrib, iofst, gfld%locallen, &
405 if (jerr .ne. 0)
then
415 if (isecnum .eq. 3)
then
417 if (
associated(gfld%igdtmpl))
deallocate(gfld%igdtmpl)
418 if (
associated(gfld%list_opt))
deallocate(gfld%list_opt)
419 call gf_unpack3(cgrib, lcgrib, iofst, igds, gfld%igdtmpl, &
420 gfld%igdtlen, gfld%list_opt, gfld%num_opt, jerr)
421 if (jerr .ne. 0)
then
427 gfld%griddef = igds(1)
428 gfld%ngrdpts = igds(2)
429 gfld%numoct_opt = igds(3)
430 gfld%interp_opt = igds(4)
431 gfld%igdtnum = igds(5)
436 if (isecnum .eq. 4)
then
438 if (numfld .eq. ifldnum)
then
439 gfld%discipline = listsec0(1)
440 gfld%version = listsec0(2)
441 gfld%ifldnum = ifldnum
442 gfld%unpacked = unpack
443 gfld%expanded = .false.
445 call gf_unpack4(cgrib, lcgrib, iofst, gfld%ipdtnum, &
446 gfld%ipdtmpl, gfld%ipdtlen, gfld%coord_list, &
447 gfld%num_coord, jerr)
448 if (jerr .ne. 0)
then
459 if ((isecnum .eq. 5).and.(numfld .eq. ifldnum))
then
461 call gf_unpack5(cgrib, lcgrib, iofst, gfld%ndpts, &
462 gfld%idrtnum, gfld%idrtmpl, gfld%idrtlen, jerr)
463 if (jerr .ne. 0)
then
473 if (isecnum .eq. 6)
then
477 call gf_unpack6(cgrib, lcgrib, iofst, gfld%ngrdpts, &
478 gfld%ibmap, gfld%bmap, jerr)
479 if (jerr .ne. 0)
then
485 if (gfld%ibmap .eq. 254)
then
486 if (
associated(bmpsave))
then
489 print *,
'gf_getfld: Previous bit-map ' &
490 ,
'specified, but none exists, '
496 if (
associated(bmpsave))
deallocate(bmpsave)
499 call g2_gbytec(cgrib, gfld%ibmap, iofst, 8)
506 if ((isecnum .eq. 7) .and. (numfld .eq. ifldnum) .and. unpack) &
509 call gf_unpack7(cgrib, lcgrib, iofst, gfld%igdtnum, &
510 gfld%igdtmpl, gfld%idrtnum, &
511 gfld%idrtmpl, gfld%ndpts, &
513 if (jerr .ne. 0)
then
515 print *,
'gf_getfld: return from gf_unpack7 = ', jerr
523 if (gfld%ibmap .ne. 255 .AND.
associated(gfld%bmap))
then
525 allocate(newfld(gfld%ngrdpts))
527 do j = 1, gfld%ngrdpts
528 if (gfld%bmap(j))
then
529 newfld(j) = gfld%fld(n)
535 deallocate(gfld%fld);
537 gfld%expanded = .true.
539 gfld%expanded = .false.
542 gfld%expanded = .true.
549 if (ipos .gt. (istart + lengrib))
then
550 print *,
'gf_getfld: "7777" not found at end ' &
559 if (unpack .and. have3 .and. have4 .and. have5 .and. have6 &
564 if ((.not. unpack) .and. have3 .and. have4 .and. have5 .and. &
570 print *,
'gf_getfld: GRIB message contained ', numlocal, &
572 print *,
'gf_getfld: The request was for the ', ifldnum, &
591 if (
associated(gfld%idsect))
then
592 deallocate(gfld%idsect,stat=is)
596 if (
associated(gfld%local))
then
597 deallocate(gfld%local,stat=is)
601 if (
associated(gfld%list_opt))
then
602 deallocate(gfld%list_opt,stat=is)
604 nullify(gfld%list_opt)
606 if (
associated(gfld%igdtmpl))
then
607 deallocate(gfld%igdtmpl,stat=is)
609 nullify(gfld%igdtmpl)
611 if (
associated(gfld%ipdtmpl))
then
612 deallocate(gfld%ipdtmpl,stat=is)
614 nullify(gfld%ipdtmpl)
616 if (
associated(gfld%coord_list))
then
617 deallocate(gfld%coord_list,stat=is)
619 nullify(gfld%coord_list)
621 if (
associated(gfld%idrtmpl))
then
622 deallocate(gfld%idrtmpl,stat=is)
624 nullify(gfld%idrtmpl)
626 if (
associated(gfld%bmap))
then
627 deallocate(gfld%bmap,stat=is)
631 if (
associated(gfld%fld))
then
632 deallocate(gfld%fld,stat=is)
subroutine g2_gbytec(in, iout, iskip, nbits)
Extract one arbitrary size big-endian value (up to 32 bits) from a packed bit string into one element...
subroutine addgrid(cgrib, lcgrib, igds, igdstmpl, igdstmplen, ideflist, idefnum, ierr)
Add a Grid Definition Section (Section 3) to a GRIB2 message.
subroutine addlocal(cgrib, lcgrib, csec2, lcsec2, ierr)
Add a Local Use Section (Section 2) to a GRIB2 message.
subroutine gribcreate(cgrib, lcgrib, listsec0, listsec1, ierr)
Initialize a new GRIB2 message and pack GRIB2 sections 0 (Indicator) and 1 (Identification).
subroutine addfield(cgrib, lcgrib, ipdsnum, ipdstmpl, ipdstmplen, coordlist, numcoord, idrsnum, idrstmpl, idrstmplen, fld, ngrdpts, ibmap, bmap, ierr)
Pack up Sections 4 through 7 for a field and add them to a GRIB2 message.
subroutine gribend(cgrib, lcgrib, lengrib, ierr)
Finalize a GRIB2 message after all grids and fields have been added.
subroutine gf_free(gfld)
Free memory that was used to store array values in derived type grib_mod::gribfield.
subroutine putgb2(lugb, gfld, iret)
Pack a field into a grib2 message and write that message to a file.
subroutine gf_getfld(cgrib, lcgrib, ifldnum, unpack, expand, gfld, ierr)
Return the Grid Definition, and Product Definition for a given data field.
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.
subroutine gf_unpack2(cgrib, lcgrib, iofst, lencsec2, csec2, ierr)
Unpack Section 2 (Local Use Section) of a GRIB2 message.
subroutine gf_unpack7(cgrib, lcgrib, iofst, igdsnum, igdstmpl, idrsnum, idrstmpl, ndpts, fld, ierr)
Unpack Section 7 (Data Section) of a GRIB2 message.
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...
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...
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.
subroutine gf_unpack6(cgrib, lcgrib, iofst, ngpts, ibmap, bmap, ierr)
Unpack Section 6 (Bit-Map Section) of a GRIB2 message, starting at octet 6 of that Section.
This Fortran module contains the declaration of derived type gribfield.