NCEPLIBS-g2  3.4.7
gf_unpack4.F90
Go to the documentation of this file.
1 
7 
35 subroutine gf_unpack4(cgrib, lcgrib, iofst, ipdsnum, ipdstmpl, &
36  mappdslen, coordlist, numcoord, ierr)
37  use pdstemplates
38  use re_alloc ! needed for subroutine realloc
39  implicit none
40 
41  character(len = 1), intent(in) :: cgrib(lcgrib)
42  integer, intent(in) :: lcgrib
43  integer, intent(inout) :: iofst
44  real, pointer, dimension(:) :: coordlist
45  integer, pointer, dimension(:) :: ipdstmpl
46  integer, intent(out) :: ipdsnum
47  integer, intent(out) :: ierr, numcoord
48 
49  real(4), allocatable :: coordieee(:)
50  integer, allocatable :: mappds(:)
51  integer :: mappdslen
52  logical needext
53  integer :: lensec, nbits, newmappdslen
54  integer :: istat1, istat, isign, iret, i
55 
56  ierr = 0
57  nullify(ipdstmpl, coordlist)
58 
59  ! Get Length of Section.
60  call g2_gbytec(cgrib, lensec, iofst, 32)
61  iofst = iofst + 32
62  iofst = iofst + 8 ! skip section number
63  allocate(mappds(lensec))
64 
65  ! Get num of coordinate values.
66  call g2_gbytec(cgrib, numcoord, iofst, 16)
67  iofst = iofst + 16
68  ! Get Prod. Def Template num.
69  call g2_gbytec(cgrib, ipdsnum, iofst, 16)
70  iofst = iofst + 16
71  ! Get Product Definition Template.
72  call getpdstemplate(ipdsnum, mappdslen, mappds, needext, iret)
73  if (iret.ne.0) then
74  ierr = 5
75  if (allocated(mappds)) deallocate(mappds)
76  return
77  endif
78 
79  ! Unpack each value into array ipdstmpl from the the appropriate
80  ! number of octets, which are specified in corresponding entries in
81  ! array mappds.
82  istat = 0
83  if (mappdslen.gt.0) allocate(ipdstmpl(mappdslen), stat = istat)
84  if (istat.ne.0) then
85  ierr = 6
86  nullify(ipdstmpl)
87  if (allocated(mappds)) deallocate(mappds)
88  return
89  endif
90  do i = 1, mappdslen
91  nbits = iabs(mappds(i))*8
92  if (mappds(i).ge.0) then
93  call g2_gbytec(cgrib, ipdstmpl(i), iofst, nbits)
94  else
95  call g2_gbytec(cgrib, isign, iofst, 1)
96  call g2_gbytec(cgrib, ipdstmpl(i), iofst + 1, nbits-1)
97  if (isign.eq.1) ipdstmpl(i) = -ipdstmpl(i)
98  endif
99  iofst = iofst + nbits
100  enddo
101 
102  ! Check to see if the Product Definition Template needs to be
103  ! extended. The number of values in a specific template may vary
104  ! depending on data specified in the "static" part of the template.
105  if (needext) then
106  call extpdstemplate(ipdsnum, ipdstmpl, newmappdslen, mappds)
107  call realloc(ipdstmpl, mappdslen, newmappdslen, istat)
108  ! Unpack the rest of the Product Definition Template.
109  do i = mappdslen + 1, newmappdslen
110  nbits = iabs(mappds(i))*8
111  if (mappds(i).ge.0) then
112  call g2_gbytec(cgrib, ipdstmpl(i), iofst, nbits)
113  else
114  call g2_gbytec(cgrib, isign, iofst, 1)
115  call g2_gbytec(cgrib, ipdstmpl(i), iofst + 1, nbits-1)
116  if (isign.eq.1) ipdstmpl(i) = -ipdstmpl(i)
117  endif
118  iofst = iofst + nbits
119  enddo
120  mappdslen = newmappdslen
121  endif
122  if (allocated(mappds)) deallocate(mappds)
123 
124  ! Get Optional list of vertical coordinate values
125  ! after the Product Definition Template, if necessary.
126  nullify(coordlist)
127  if (numcoord .ne. 0) then
128  allocate (coordieee(numcoord), stat = istat1)
129  allocate(coordlist(numcoord), stat = istat)
130  if ((istat1 + istat).ne.0) then
131  ierr = 6
132  nullify(coordlist)
133  if (allocated(coordieee)) deallocate(coordieee)
134  return
135  endif
136  call g2_gbytesc(cgrib, coordieee, iofst, 32, 0, numcoord)
137  call rdieee(coordieee, coordlist, numcoord)
138  deallocate (coordieee)
139  iofst = iofst + (32 * numcoord)
140  endif
141 end subroutine gf_unpack4
subroutine g2_gbytesc(in, iout, iskip, nbits, nskip, n)
Extract arbitrary size values from a packed bit string, right justifying each value in the unpacked a...
Definition: g2_gbytesc.F90:63
subroutine g2_gbytec(in, iout, iskip, nbits)
Extract arbitrary size values from a packed bit string, right justifying each value in the unpacked a...
Definition: g2_gbytesc.F90:20
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...
Definition: gf_unpack4.F90:37
Information on all GRIB2 Product Definition Templates used in Section 4 - the Product Definition Sect...
subroutine extpdstemplate(number, list, nummap, map)
This subroutine generates the remaining octet map for a given Product Definition Template,...
subroutine getpdstemplate(number, nummap, map, needext, iret)
This subroutine returns PDS template information for a specified Product Definition Template.
Reallocate memory, preserving contents.
Definition: realloc.f:12
subroutine rdieee(rieee, a, num)
Copy array of 32-bit IEEE floating point values to local floating point representation.
Definition: rdieee.F90:16