1 subroutine comunpack
(cpack
,len
,lensec
,idrsnum
,idrstmpl
,ndpts
,
3 !$$$ SUBPROGRAM DOCUMENTATION BLOCK
5 ! SUBPROGRAM
: comunpack
6 ! PRGMMR
: Gilbert ORG
: W
/NP11
DATE: 2000-06-21
8 ! ABSTRACT
: This
subroutine unpacks a data field that was packed using a
9 ! complex packing algorithm as defined in the GRIB2 documention
,
10 ! using info from the GRIB2 Data Representation Template
5.2 or
5.3.
11 ! Supports GRIB2
complex packing templates with or without
12 ! spatial differences
(i
.e
. DRTs
5.2 and
5.3).
14 ! PROGRAM HISTORY LOG
:
16 ! 2004-12-29 Gilbert
- Added test
( provided by Arthur Taylor
/MDL
)
17 ! to verify that group widths and lengths are
18 ! consistent with section length
.
20 ! USAGE
: CALL comunpack
(cpack
,len
,lensec
,idrsnum
,idrstmpl
,ndpts
,fld
,ier
)
21 ! INPUT ARGUMENT LIST
:
22 ! cpack
- The packed data field
(character*1 array
)
23 ! len
- length of packed field cpack
().
24 ! lensec
- length of section
7 (used
for error checking
).
25 ! idrsnum
- Data Representation Template number
5.N
27 ! idrstmpl
- Contains the array of values
for Data Representation
29 ! ndpts
- The number of data values
to unpack
31 ! OUTPUT ARGUMENT LIST
:
32 ! fld
() - Contains the unpacked data values
35 ! 1 = Problem
- inconsistent group lengths of widths
.
40 ! LANGUAGE
: XL Fortran
90
45 character(len
=1),intent
(in
) :: cpack
(len
)
46 integer,intent
(in
) :: ndpts
,len
47 integer,intent
(in
) :: idrstmpl
(*)
48 real,intent
(out
) :: fld
(ndpts
)
50 integer,allocatable
:: ifld
(:),ifldmiss
(:)
52 integer,allocatable
:: gref
(:),gwidth
(:),glen
(:)
53 real :: ref
,bscale
,dscale
,rmiss1
,rmiss2
55 integer :: totBit
, totLen
58 !print
*,'IDRSTMPL: ',(idrstmpl
(j
),j
=1,16)
60 call rdieee
(ieee
,ref
,1)
61 bscale
= 2.0**real(idrstmpl
(2))
62 dscale
= 10.0**real(-idrstmpl
(3))
63 nbitsgref
= idrstmpl
(4)
65 ngroups
= idrstmpl
(10)
66 nbitsgwidth
= idrstmpl
(12)
67 nbitsglen
= idrstmpl
(16)
68 if (idrsnum
.eq
.3) then
74 if (ngroups
.eq
.0) then
82 allocate
(ifld
(ndpts
),stat
=is
)
83 !print
*,'ALLOC ifld: ',is
,ndpts
84 allocate
(gref
(ngroups
),stat
=is
)
85 !print
*,'ALLOC gref: ',is
86 allocate
(gwidth
(ngroups
),stat
=is
)
87 !print
*,'ALLOC gwidth: ',is
89 ! Get missing values
, if supplied
91 if ( idrstmpl
(7).eq
.1 ) then
93 call rdieee
(idrstmpl
(8),rmiss1
,1)
95 rmiss1
=real(idrstmpl
(8))
97 elseif
( idrstmpl
(7).eq
.2 ) then
99 call rdieee
(idrstmpl
(8),rmiss1
,1)
100 call rdieee
(idrstmpl
(9),rmiss2
,1)
102 rmiss1
=real(idrstmpl
(8))
103 rmiss2
=real(idrstmpl
(9))
106 !print
*,'RMISSs: ',rmiss1
,rmiss2
,ref
108 ! Extract Spatial differencing values
, if using DRS Template
5.3
110 if (idrsnum
.eq
.3) then
111 if (nbitsd
.ne
.0) then
112 call gbyte
(cpack
,isign
,iofst
,1)
114 call gbyte
(cpack
,ival1
,iofst
,nbitsd
-1)
116 if (isign
.eq
.1) ival1
=-ival1
117 if (idrstmpl
(17).eq
.2) then
118 call gbyte
(cpack
,isign
,iofst
,1)
120 call gbyte
(cpack
,ival2
,iofst
,nbitsd
-1)
122 if (isign
.eq
.1) ival2
=-ival2
124 call gbyte
(cpack
,isign
,iofst
,1)
126 call gbyte
(cpack
,minsd
,iofst
,nbitsd
-1)
128 if (isign
.eq
.1) minsd
=-minsd
134 !print
*,'SDu ',ival1
,ival2
,minsd
,nbitsd
137 ! Extract Each Group
's reference value
139 !print *,'SAG1
: ',nbitsgref,ngroups,iofst
140 if (nbitsgref.ne.0) then
141 call gbytes(cpack,gref,iofst,nbitsgref,0,ngroups)
142 itemp=nbitsgref*ngroups
144 if (mod(itemp,8).ne.0) iofst=iofst+(8-mod(itemp,8))
148 !write(78,*)'GREFs
: ',(gref(j),j=1,ngroups)
150 ! Extract Each Group's bit width
152 !print
*,'SAG2: ',nbitsgwidth
,ngroups
,iofst
,idrstmpl
(11)
153 if (nbitsgwidth
.ne
.0) then
154 call gbytes
(cpack
,gwidth
,iofst
,nbitsgwidth
,0,ngroups
)
155 itemp
=nbitsgwidth*ngroups
157 if (mod
(itemp
,8).ne
.0) iofst
=iofst
+(8-mod
(itemp
,8))
162 gwidth
(j
)=gwidth
(j
)+idrstmpl
(11)
164 !write(78,*)'GWIDTHs: ',(gwidth
(j
),j
=1,ngroups
)
166 ! Extract Each Group
's length (number of values in each group)
168 allocate(glen(ngroups),stat=is)
169 !print *,'ALLOC glen
: ',is
170 !print *,'SAG3
: ',nbitsglen,ngroups,iofst,idrstmpl(14),idrstmpl(13)
171 if (nbitsglen.ne.0) then
172 call gbytes(cpack,glen,iofst,nbitsglen,0,ngroups)
173 itemp=nbitsglen*ngroups
175 if (mod(itemp,8).ne.0) iofst=iofst+(8-mod(itemp,8))
180 glen(j)=(glen(j)*idrstmpl(14))+idrstmpl(13)
182 glen(ngroups)=idrstmpl(15)
183 !write(78,*)'GLENs
: ',(glen(j),j=1,ngroups)
184 !print *,'GLENsum
: ',sum(glen)
186 ! Test to see if the group widths and lengths are consistent with number of
187 ! values, and length of section 7.
192 totBit = totBit + (gwidth(j)*glen(j));
193 totLen = totLen + glen(j);
195 if (totLen .NE. ndpts) then
199 if ( (totBit/8) .GT. lensec) then
204 ! For each group, unpack data values
206 if ( idrstmpl(7).eq.0 ) then ! no missing values
209 !write(78,*)'NGP
',j,gwidth(j),glen(j),gref(j)
210 if (gwidth(j).ne.0) then
211 call gbytes(cpack,ifld(n),iofst,gwidth(j),0,glen(j))
213 ifld(n)=ifld(n)+gref(j)
217 ifld(n:n+glen(j)-1)=gref(j)
220 iofst=iofst+(gwidth(j)*glen(j))
222 elseif ( idrstmpl(7).eq.1.OR.idrstmpl(7).eq.2 ) then
223 ! missing values included
224 allocate(ifldmiss(ndpts))
229 !print *,'SAGNGP
',j,gwidth(j),glen(j),gref(j)
230 if (gwidth(j).ne.0) then
231 msng1=(2**gwidth(j))-1
233 call gbytes(cpack,ifld(n),iofst,gwidth(j),0,glen(j))
234 iofst=iofst+(gwidth(j)*glen(j))
236 if (ifld(n).eq.msng1) then
238 elseif (idrstmpl(7).eq.2.AND.ifld(n).eq.msng2) then
242 ifld(non)=ifld(n)+gref(j)
248 msng1=(2**nbitsgref)-1
250 if (gref(j).eq.msng1) then
251 ifldmiss(n:n+glen(j)-1)=1
252 !ifld(n:n+glen(j)-1)=0
253 elseif (idrstmpl(7).eq.2.AND.gref(j).eq.msng2) then
254 ifldmiss(n:n+glen(j)-1)=2
255 !ifld(n:n+glen(j)-1)=0
257 ifldmiss(n:n+glen(j)-1)=0
258 ifld(non:non+glen(j)-1)=gref(j)
265 !write(78,*)'IFLDs
: ',(ifld(j),j=1,ndpts)
267 if ( allocated(gref) ) deallocate(gref)
268 if ( allocated(gwidth) ) deallocate(gwidth)
269 if ( allocated(glen) ) deallocate(glen)
271 ! If using spatial differences, add overall min value, and
274 if (idrsnum.eq.3) then ! spatial differencing
275 if (idrstmpl(17).eq.1) then ! first order
277 if ( idrstmpl(7).eq.0 ) then ! no missing values
283 ifld(n)=ifld(n)+minsd
284 ifld(n)=ifld(n)+ifld(n-1)
286 elseif (idrstmpl(17).eq.2) then ! second order
289 if ( idrstmpl(7).eq.0 ) then ! no missing values
295 ifld(n)=ifld(n)+minsd
296 ifld(n)=ifld(n)+(2*ifld(n-1))-ifld(n-2)
299 !write(78,*)'IFLDs
: ',(ifld(j),j=1,ndpts)
302 ! Scale data back to original form
304 !print *,'SAGT
: ',ref,bscale,dscale
305 if ( idrstmpl(7).eq.0 ) then ! no missing values
307 fld(n)=((real(ifld(n))*bscale)+ref)*dscale
308 !write(78,*)'SAG
',n,fld(n),ifld(n),bscale,ref,1./dscale
310 elseif ( idrstmpl(7).eq.1.OR.idrstmpl(7).eq.2 ) then
311 ! missing values included
314 if ( ifldmiss(n).eq.0 ) then
315 fld(n)=((real(ifld(non))*bscale)+ref)*dscale
316 !print *,'SAG
',n,fld(n),ifld(non),bscale,ref,dscale
318 elseif ( ifldmiss(n).eq.1 ) then
320 elseif ( ifldmiss(n).eq.2 ) then
324 if ( allocated(ifldmiss) ) deallocate(ifldmiss)
327 if ( allocated(ifld) ) deallocate(ifld)
329 !open(10,form='unformatted
',recl=24180,access='direct
')
330 !read(10,rec=1) (fldo(k),k=1,6045)
332 ! print *,i,fldo(i),fld(i),fldo(i)-fld(i)