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