1 subroutine misspack(fld,ndpts,idrsnum,idrstmpl,cpack,lcpack)
2 !$$$ SUBPROGRAM DOCUMENTATION BLOCK
5 ! PRGMMR: Gilbert ORG: W/NP11 DATE: 2000-06-21
7 ! ABSTRACT: This subroutine packs up a data field using a complex
8 ! packing algorithm as defined in the GRIB2 documention. It
9 ! supports GRIB2 complex packing templates with or without
10 ! spatial differences (i.e. DRTs 5.2 and 5.3).
11 ! It also fills in GRIB2 Data Representation Template 5.2 or 5.3
12 ! with the appropriate values.
13 ! This version assumes that Missing Value Management is being used and that
14 ! 1 or 2 missing values appear in the data.
16 ! PROGRAM HISTORY LOG:
18 ! 2004-12-29 Gilbert - Corrected bug when encoding secondary missing values.
20 ! USAGE: CALL misspack(fld,ndpts,idrsnum,idrstmpl,cpack,lcpack)
21 ! INPUT ARGUMENT LIST:
22 ! fld() - Contains the data values to pack
23 ! ndpts - The number of data values in array fld()
24 ! idrsnum - Data Representation Template number 5.N
26 ! idrstmpl - Contains the array of values for Data Representation
28 ! (1) = Reference value - ignored on input
29 ! (2) = Binary Scale Factor
30 ! (3) = Decimal Scale Factor
33 ! (7) = Missing value management
34 ! (8) = Primary missing value
35 ! (9) = Secondary missing value
38 ! (17) = Order of Spatial Differencing ( 1 or 2 )
42 ! OUTPUT ARGUMENT LIST:
43 ! idrstmpl - Contains the array of values for Data Representation
45 ! (1) = Reference value - set by misspack routine.
46 ! (2) = Binary Scale Factor - unchanged from input
47 ! (3) = Decimal Scale Factor - unchanged from input
50 ! cpack - The packed data field (character*1 array)
51 ! lcpack - length of packed field cpack().
56 ! LANGUAGE: XL Fortran 90
61 integer,intent(in) :: ndpts,idrsnum
62 real,intent(in) :: fld(ndpts)
63 character(len=1),intent(out) :: cpack(*)
64 integer,intent(inout) :: idrstmpl(*)
65 integer,intent(out) :: lcpack
69 integer,allocatable :: ifld(:),ifldmiss(:),jfld(:)
70 integer,allocatable :: jmin(:),jmax(:),lbit(:)
71 integer,parameter :: zero=0
72 integer,allocatable :: gref(:),gwidth(:),glen(:)
73 integer :: glength,grpwidth
74 logical :: simple_alg = .false.
77 bscale=2.0**real(-idrstmpl(2))
78 dscale=10.0**real(idrstmpl(3))
80 if ( missopt.ne.1 .AND. missopt.ne.2 ) then
81 print *,'misspack: Unrecognized option.'
84 else ! Get missing values
85 call rdieee(idrstmpl(8),rmissp,1)
86 if (missopt.eq.2) call rdieee(idrstmpl(9),rmisss,1)
89 ! Find min value of non-missing values in the data,
90 ! AND set up missing value mapping of the field.
92 allocate(ifldmiss(ndpts))
94 if ( missopt .eq. 1 ) then ! Primary missing value only
96 if (fld(j).eq.rmissp) then
100 if (fld(j).lt.rmin) rmin=fld(j)
104 if ( missopt .eq. 2 ) then ! Primary and secondary missing values
106 if (fld(j).eq.rmissp) then
108 elseif (fld(j).eq.rmisss) then
112 if (fld(j).lt.rmin) rmin=fld(j)
117 ! Allocate work arrays:
118 ! Note: -ifldmiss(j),j=1,ndpts is a map of original field indicating
119 ! which of the original data values
120 ! are primary missing (1), sencondary missing (2) or non-missing (0).
121 ! -jfld(j),j=1,nonmiss is a subarray of just the non-missing values from
122 ! the original field.
124 !if (rmin.ne.rmax) then
126 allocate(ifld(ndpts))
127 allocate(jfld(ndpts))
128 allocate(gref(ndpts))
129 allocate(gwidth(ndpts))
130 allocate(glen(ndpts))
132 ! Scale original data
135 if (idrstmpl(2).eq.0) then ! No binary scaling
136 imin=nint(rmin*dscale)
137 !imax=nint(rmax*dscale)
140 if (ifldmiss(j).eq.0) then
142 jfld(nonmiss)=nint(fld(j)*dscale)-imin
145 else ! Use binary scaling factor
149 if (ifldmiss(j).eq.0) then
151 jfld(nonmiss)=nint(((fld(j)*dscale)-rmin)*bscale)
156 ! Calculate Spatial differences, if using DRS Template 5.3
158 if (idrsnum.eq.3) then ! spatial differences
159 if (idrstmpl(17).ne.1.and.idrstmpl(17).ne.2) idrstmpl(17)=2
160 if (idrstmpl(17).eq.1) then ! first order
163 jfld(j)=jfld(j)-jfld(j-1)
166 elseif (idrstmpl(17).eq.2) then ! second order
170 jfld(j)=jfld(j)-(2*jfld(j-1))+jfld(j-2)
176 ! subtract min value from spatial diff field
179 minsd=minval(jfld(isd:nonmiss))
181 jfld(j)=jfld(j)-minsd
184 ! find num of bits need to store minsd and add 1 extra bit
187 temp=alog(real(abs(minsd)+1))/alog2
188 nbitsd=ceiling(temp)+1
190 ! find num of bits need to store ifld(1) ( and ifld(2)
191 ! if using 2nd order differencing )
194 if (idrstmpl(17).eq.2.and.ival2.gt.ival1) maxorig=ival2
195 temp=alog(real(maxorig+1))/alog2
196 nbitorig=ceiling(temp)+1
197 if (nbitorig.gt.nbitsd) nbitsd=nbitorig
198 ! increase number of bits to even multiple of 8 ( octet )
199 if (mod(nbitsd,8).ne.0) nbitsd=nbitsd+(8-mod(nbitsd,8))
201 ! Store extra spatial differencing info into the packed
204 if (nbitsd.ne.0) then
205 ! pack first original value
207 call g2lib_sbyte(cpack,ival1,iofst,nbitsd)
210 call g2lib_sbyte(cpack,1,iofst,1)
212 call g2lib_sbyte(cpack,iabs(ival1),iofst,nbitsd-1)
215 if (idrstmpl(17).eq.2) then
216 ! pack second original value
218 call g2lib_sbyte(cpack,ival2,iofst,nbitsd)
221 call g2lib_sbyte(cpack,1,iofst,1)
223 call g2lib_sbyte(cpack,iabs(ival2),iofst,nbitsd-1)
227 ! pack overall min of spatial differences
229 call g2lib_sbyte(cpack,minsd,iofst,nbitsd)
232 call g2lib_sbyte(cpack,1,iofst,1)
234 call g2lib_sbyte(cpack,iabs(minsd),iofst,nbitsd-1)
238 !print *,'SDp ',ival1,ival2,minsd,nbitsd
239 endif ! end of spatial diff section
241 ! Expand non-missing data values to original grid.
243 miss1=minval(jfld(1:nonmiss))-1
247 if ( ifldmiss(j).eq.0 ) then
250 elseif ( ifldmiss(j).eq.1 ) then
252 elseif ( ifldmiss(j).eq.2 ) then
257 ! Determine Groups to be used.
259 if ( simple_alg ) then
260 ! set group length to 10 : calculate number of groups
261 ! and length of last group
270 ! Use Dr. Glahn's algorithm for determining grouping.
275 maxgrps=(ndpts/minpk)+1
276 allocate(jmin(maxgrps))
277 allocate(jmax(maxgrps))
278 allocate(lbit(maxgrps))
279 call pack_gp(kfildo,ifld,ndpts,missopt,minpk,inc,miss1,miss2,
280 & jmin,jmax,lbit,glen,maxgrps,ngroups,ibit,jbit,
281 & kbit,novref,lbitref,ier)
282 !print *,'SAGier = ',ier,ibit,jbit,kbit,novref,lbitref
284 glen(ng)=glen(ng)+novref
291 ! For each group, find the group's reference value (min)
292 ! and the number of bits needed to hold the remaining values
296 ! how many of each type?
297 num0=count(ifldmiss(n:n+glen(ng)-1) .EQ. 0)
298 num1=count(ifldmiss(n:n+glen(ng)-1) .EQ. 1)
299 num2=count(ifldmiss(n:n+glen(ng)-1) .EQ. 2)
300 if ( num0.eq.0 ) then ! all missing values
301 if ( num1.eq.0 ) then ! all secondary missing
304 elseif ( num2.eq.0 ) then ! all primary missing
307 else ! both primary and secondary
311 else ! contains some non-missing data
312 ! find max and min values of group
317 if ( ifldmiss(j).eq.0 ) then
318 if (ifld(j).lt.gref(ng)) gref(ng)=ifld(j)
319 if (ifld(j).gt.imax) imax=ifld(j)
323 if (missopt.eq.1) imax=imax+1
324 if (missopt.eq.2) imax=imax+2
325 ! calc num of bits needed to hold data
326 if ( gref(ng).ne.imax ) then
327 temp=alog(real(imax-gref(ng)+1))/alog2
328 gwidth(ng)=ceiling(temp)
333 ! Subtract min from data
337 if (ifldmiss(j).eq.0) then ! non-missing
338 ifld(j)=ifld(j)-gref(ng)
339 elseif (ifldmiss(j).eq.1) then ! primary missing
341 elseif (ifldmiss(j).eq.2) then ! secondary missing
346 ! increment fld array counter
350 ! Find max of the group references and calc num of bits needed
351 ! to pack each groups reference value, then
352 ! pack up group reference values
354 !write(77,*)'GREFS: ',(gref(j),j=1,ngroups)
355 igmax=maxval(gref(1:ngroups))
356 if (missopt.eq.1) igmax=igmax+1
357 if (missopt.eq.2) igmax=igmax+2
359 temp=alog(real(igmax+1))/alog2
360 nbitsgref=ceiling(temp)
361 ! restet the ref values of any "missing only" groups.
364 if (gref(j).eq.-1) gref(j)=mtemp-1
365 if (gref(j).eq.-2) gref(j)=mtemp-2
367 call g2lib_sbytes(cpack,gref,iofst,nbitsgref,0,ngroups)
368 itemp=nbitsgref*ngroups
370 ! Pad last octet with Zeros, if necessary,
371 if (mod(itemp,8).ne.0) then
373 call g2lib_sbyte(cpack,zero,iofst,left)
380 ! Find max/min of the group widths and calc num of bits needed
381 ! to pack each groups width value, then
382 ! pack up group width values
384 !write(77,*)'GWIDTHS: ',(gwidth(j),j=1,ngroups)
385 iwmax=maxval(gwidth(1:ngroups))
386 ngwidthref=minval(gwidth(1:ngroups))
387 if (iwmax.ne.ngwidthref) then
388 temp=alog(real(iwmax-ngwidthref+1))/alog2
389 nbitsgwidth=ceiling(temp)
391 gwidth(i)=gwidth(i)-ngwidthref
393 call g2lib_sbytes(cpack,gwidth,iofst,nbitsgwidth,0,ngroups)
394 itemp=nbitsgwidth*ngroups
396 ! Pad last octet with Zeros, if necessary,
397 if (mod(itemp,8).ne.0) then
399 call g2lib_sbyte(cpack,zero,iofst,left)
407 ! Find max/min of the group lengths and calc num of bits needed
408 ! to pack each groups length value, then
409 ! pack up group length values
411 !write(77,*)'GLENS: ',(glen(j),j=1,ngroups)
412 ilmax=maxval(glen(1:ngroups-1))
413 nglenref=minval(glen(1:ngroups-1))
414 nglenlast=glen(ngroups)
415 if (ilmax.ne.nglenref) then
416 temp=alog(real(ilmax-nglenref+1))/alog2
417 nbitsglen=ceiling(temp)
419 glen(i)=glen(i)-nglenref
421 call g2lib_sbytes(cpack,glen,iofst,nbitsglen,0,ngroups)
422 itemp=nbitsglen*ngroups
424 ! Pad last octet with Zeros, if necessary,
425 if (mod(itemp,8).ne.0) then
427 call g2lib_sbyte(cpack,zero,iofst,left)
435 ! For each group, pack data values
437 !write(77,*)'IFLDS: ',(ifld(j),j=1,ndpts)
441 glength=glen(ng)+nglenref
442 if (ng.eq.ngroups ) glength=nglenlast
443 grpwidth=gwidth(ng)+ngwidthref
444 !write(77,*)'NGP ',ng,grpwidth,glength,gref(ng)
445 if ( grpwidth.ne.0 ) then
446 call g2lib_sbytes(cpack,ifld(n),iofst,grpwidth,0,glength)
447 iofst=iofst+(grpwidth*glength)
451 !write(77,*)'SAG ',ij,fld(ij),ifld(ij),gref(ng),bscale,rmin,dscale
455 ! Pad last octet with Zeros, if necessary,
456 if (mod(iofst,8).ne.0) then
458 call g2lib_sbyte(cpack,zero,iofst,left)
463 if ( allocated(ifld) ) deallocate(ifld)
464 if ( allocated(jfld) ) deallocate(jfld)
465 if ( allocated(ifldmiss) ) deallocate(ifldmiss)
466 if ( allocated(gref) ) deallocate(gref)
467 if ( allocated(gwidth) ) deallocate(gwidth)
468 if ( allocated(glen) ) deallocate(glen)
469 !else ! Constant field ( max = min )
477 ! Fill in ref value and number of bits in Template 5.2
479 call mkieee(rmin,ref,1) ! ensure reference value is IEEE format
480 ! call g2lib_gbyte(ref,idrstmpl(1),0,32)
481 iref=transfer(ref,iref)
483 idrstmpl(4)=nbitsgref
484 idrstmpl(5)=0 ! original data were reals
485 idrstmpl(6)=1 ! general group splitting
486 idrstmpl(10)=ngroups ! Number of groups
487 idrstmpl(11)=ngwidthref ! reference for group widths
488 idrstmpl(12)=nbitsgwidth ! num bits used for group widths
489 idrstmpl(13)=nglenref ! Reference for group lengths
490 idrstmpl(14)=1 ! length increment for group lengths
491 idrstmpl(15)=nglenlast ! True length of last group
492 idrstmpl(16)=nbitsglen ! num bits used for group lengths
493 if (idrsnum.eq.3) then
494 idrstmpl(18)=nbitsd/8 ! num bits used for extra spatial
495 ! differencing values