NCEPLIBS-g2  3.4.8
gf_unpack3.F90
Go to the documentation of this file.
1 
6 
50 subroutine gf_unpack3(cgrib, lcgrib, iofst, igds, igdstmpl, &
51  mapgridlen, ideflist, idefnum, ierr)
52 
53  use gridtemplates
54  use re_alloc ! needed for subroutine realloc
55  implicit none
56 
57  character(len = 1), intent(in) :: cgrib(lcgrib)
58  integer, intent(in) :: lcgrib
59  integer, intent(inout) :: iofst
60  integer, pointer, dimension(:) :: igdstmpl, ideflist
61  integer, intent(out) :: igds(5)
62  integer, intent(out) :: ierr, idefnum
63 
64  integer, allocatable :: mapgrid(:)
65  integer, intent(out) :: mapgridlen
66  integer :: ibyttem
67  logical needext
68  integer :: lensec, istat, i, nbits, isign, newmapgridlen, iret
69 
70  ierr = 0
71  nullify(igdstmpl, ideflist)
72 
73  call g2_gbytec(cgrib, lensec, iofst, 32) ! Get Length of Section
74  iofst = iofst + 32
75  iofst = iofst + 8 ! skip section number
76 
77  call g2_gbytec(cgrib, igds(1), iofst, 8) ! Get source of Grid def.
78  iofst = iofst + 8
79  call g2_gbytec(cgrib, igds(2), iofst, 32) ! Get number of grid pts.
80  iofst = iofst + 32
81  call g2_gbytec(cgrib, igds(3), iofst, 8) ! Get num octets for opt. list
82  iofst = iofst + 8
83  call g2_gbytec(cgrib, igds(4), iofst, 8) ! Get interpret. for opt. list
84  iofst = iofst + 8
85  call g2_gbytec(cgrib, igds(5), iofst, 16) ! Get Grid Def Template num.
86  iofst = iofst + 16
87 
88  if (igds(1) .eq. 0 .OR. igds(1) .eq. 255) then ! FOR ECMWF TEST ONLY
89  allocate(mapgrid(lensec))
90 
91  ! Get Grid Definition Template
92  call getgridtemplate(igds(5), mapgridlen, mapgrid, needext, iret)
93  if (iret .ne. 0) then
94  ierr = 5
95  if (allocated(mapgrid)) deallocate(mapgrid)
96  return
97  endif
98  else
99  mapgridlen = 0
100  needext = .false.
101  endif
102 
103  ! Unpack each value into array igdstmpl from the the appropriate
104  ! number of octets, which are specified in corresponding entries in
105  ! array mapgrid.
106  istat = 0
107  if (mapgridlen .gt. 0) allocate(igdstmpl(mapgridlen), stat = istat)
108  if (istat .ne. 0) then
109  ierr = 6
110  nullify(igdstmpl)
111  if (allocated(mapgrid)) deallocate(mapgrid)
112  return
113  endif
114  ibyttem = 0
115  do i = 1, mapgridlen
116  nbits = iabs(mapgrid(i)) * 8
117  if (mapgrid(i) .ge. 0) then
118  call g2_gbytec(cgrib, igdstmpl(i), iofst, nbits)
119  else
120  call g2_gbytec(cgrib, isign, iofst, 1)
121  call g2_gbytec(cgrib, igdstmpl(i), iofst + 1, nbits - 1)
122  if (isign .eq. 1) igdstmpl(i) = -igdstmpl(i)
123  endif
124  iofst = iofst + nbits
125  ibyttem = ibyttem + iabs(mapgrid(i))
126  enddo
127 
128  ! Check to see if the Grid Definition Template needs to be extended.
129  ! The number of values in a specific template may vary depending on
130  ! data specified in the "static" part of the template.
131  if (needext) then
132  call extgridtemplate(igds(5), igdstmpl, newmapgridlen, &
133  mapgrid)
134 
135  ! Unpack the rest of the Grid Definition Template.
136  call realloc(igdstmpl, mapgridlen, newmapgridlen, istat)
137  do i = mapgridlen + 1, newmapgridlen
138  nbits = iabs(mapgrid(i)) * 8
139  if (mapgrid(i) .ge. 0) then
140  call g2_gbytec(cgrib, igdstmpl(i), iofst, nbits)
141  else
142  call g2_gbytec(cgrib, isign, iofst, 1)
143  call g2_gbytec(cgrib, igdstmpl(i), iofst + 1, nbits - 1)
144  if (isign.eq.1) igdstmpl(i) = -igdstmpl(i)
145  endif
146  iofst = iofst + nbits
147  ibyttem = ibyttem + iabs(mapgrid(i))
148  enddo
149  mapgridlen = newmapgridlen
150  endif
151  if (allocated(mapgrid)) deallocate(mapgrid)
152 
153  ! Unpack optional list of numbers defining number of points in each
154  ! row or column, if included. This is used for non regular grids.
155  if (igds(3) .ne. 0) then
156  nbits = igds(3) * 8
157  idefnum = (lensec - 14 - ibyttem) / igds(3)
158  istat = 0
159  if (idefnum .gt. 0) allocate(ideflist(idefnum), stat = istat)
160  if (istat .ne. 0) then
161  ierr = 6
162  nullify(ideflist)
163  return
164  endif
165  call g2_gbytesc(cgrib, ideflist, iofst, nbits, 0, idefnum)
166  iofst = iofst + (nbits * idefnum)
167  else
168  idefnum = 0
169  nullify(ideflist)
170  endif
171 end subroutine gf_unpack3
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_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.
Definition: gf_unpack3.F90:52
This Fortran module contains info on all the available GRIB2 Grid Definition Templates used in [Secti...
subroutine getgridtemplate(number, nummap, map, needext, iret)
Get the grid template information for a specified Grid Definition Template.
subroutine extgridtemplate(number, list, nummap, map)
Generate the remaining octet map for a given Grid Definition Template, if required.
Reallocate memory, preserving contents.
Definition: realloc.f:12