31 subroutine compack(fld,ndpts,idrsnum,idrstmpl,cpack,lcpack)
35 integer,
intent(in) :: ndpts,idrsnum
36 real,
intent(in) :: fld(ndpts)
37 character(len=1),
intent(out) :: cpack(*)
38 integer,
intent(inout) :: idrstmpl(*)
39 integer,
intent(out) :: lcpack
42 integer :: j,iofst,imin,ival1,ival2,minsd,nbitsd,n
43 integer :: igmax,nbitsgref,left,iwmax,i,ilmax,kk,ij
44 integer :: ngwidthref,nbitsgwidth,nglenref,nglenlast
45 integer :: maxorig,nbitorig,isd,ngroups,itemp,minpk
46 integer :: kfildo,inc,maxgrps,missopt,miss1,miss2,lg
47 integer :: ibit,jbit,kbit,novref,lbitref,ier,ng,imax
53 integer,
allocatable :: ifld(:)
54 integer,
allocatable :: jmin(:),jmax(:),lbit(:)
55 integer,
parameter :: zero=0
56 integer,
allocatable :: gref(:),gwidth(:),glen(:)
57 integer :: glength,grpwidth
62 bscale=2.0**real(-idrstmpl(2))
63 dscale=10.0**real(idrstmpl(3))
75 if (fld(j).gt.rmax) rmax=fld(j)
76 if (fld(j).lt.rmin) rmin=fld(j)
84 multival:
if (rmin.ne.rmax)
then
88 allocate(gwidth(ndpts))
93 if (idrstmpl(2).eq.0)
then
94 imin=nint(rmin*dscale)
98 ifld(j)=max(0,nint(fld(j)*dscale)-imin)
104 ifld(j)=max(0,nint(((fld(j)*dscale)-rmin)*bscale))
110 alg3:
if (idrsnum.eq.3)
then
111 if (idrstmpl(17).ne.1.and.idrstmpl(17).ne.2) idrstmpl(17)=2
112 if (idrstmpl(17).eq.1)
then
115 print *,
'G2: negative ival1',ival1
119 ifld(j)=ifld(j)-ifld(j-1)
122 elseif (idrstmpl(17).eq.2)
then
126 print *,
'G2: negative ival1',ival1
130 print *,
'G2: negative ival2',ival2
134 ifld(j)=ifld(j)-(2*ifld(j-1))+ifld(j-2)
143 minsd=minval(ifld(isd:ndpts))
145 ifld(j)=ifld(j)-minsd
151 nbitsd=
i1log2(abs(minsd))+1
157 if (idrstmpl(17).eq.2.and.ival2.gt.ival1) maxorig=ival2
158 nbitorig=
i1log2(maxorig)+1
159 if (nbitorig.gt.nbitsd) nbitsd=nbitorig
161 if (mod(nbitsd,8).ne.0) nbitsd=nbitsd+(8-mod(nbitsd,8))
166 if (nbitsd.ne.0)
then
174 call g2_sbytec(cpack,iabs(ival1),iofst,nbitsd-1)
177 if (idrstmpl(17).eq.2)
then
185 call g2_sbytec(cpack,iabs(ival2),iofst,nbitsd-1)
196 call g2_sbytec(cpack,iabs(minsd),iofst,nbitsd-1)
205 simplealg:
if ( simple_alg )
then
208 print *,
'G2: use simple algorithm'
222 maxgrps=((ndpts+minpk-1)/minpk)
223 allocate(jmin(maxgrps))
224 allocate(jmax(maxgrps))
225 allocate(lbit(maxgrps))
227 call pack_gp(kfildo,ifld,ndpts,missopt,minpk,inc,miss1,miss2,
228 & jmin,jmax,lbit,glen,maxgrps,ngroups,ibit,jbit,
229 & kbit,novref,lbitref,ier)
232 1099
format(
'G2: fall back to simple algorithm (glahn ier=',i0,
242 elseif(ngroups<1)
then
244 print *,
'Glahn algorithm failed; use simple packing'
255 glen(ng)=glen(ng)+novref
273 if (ifld(j).lt.gref(ng)) gref(ng)=ifld(j)
274 if (ifld(j).gt.imax) imax=ifld(j)
278 if ( gref(ng).ne.imax )
then
279 gwidth(ng)=
i1log2(imax-gref(ng))
286 ifld(j)=ifld(j)-gref(ng)
298 igmax=maxval(gref(1:ngroups))
301 call g2_sbytesc(cpack,gref,iofst,nbitsgref,0,ngroups)
302 itemp=nbitsgref*ngroups
304 if (mod(itemp,8).ne.0)
then
318 iwmax=maxval(gwidth(1:ngroups))
319 ngwidthref=minval(gwidth(1:ngroups))
320 if (iwmax.ne.ngwidthref)
then
321 nbitsgwidth=
i1log2(iwmax-ngwidthref)
323 gwidth(i)=gwidth(i)-ngwidthref
325 write(0,*)
'i,gw,ngw=',i,gwidth(i),ngwidthref
329 call g2_sbytesc(cpack,gwidth,iofst,nbitsgwidth,0,ngroups)
330 itemp=nbitsgwidth*ngroups
333 if (mod(itemp,8).ne.0)
then
348 ilmax=maxval(glen(1:ngroups-1))
349 nglenref=minval(glen(1:ngroups-1))
350 nglenlast=glen(ngroups)
351 if (ilmax.ne.nglenref)
then
352 nbitsglen=
i1log2(ilmax-nglenref)
354 glen(i)=glen(i)-nglenref
356 write(0,*)
'i,glen(i) = ',i,glen(i)
360 call g2_sbytesc(cpack,glen,iofst,nbitsglen,0,ngroups)
361 itemp=nbitsglen*ngroups
364 if (mod(itemp,8).ne.0)
then
380 glength=glen(ng)+nglenref
381 if (ng.eq.ngroups ) glength=nglenlast
382 grpwidth=gwidth(ng)+ngwidthref
384 if ( grpwidth.ne.0 )
then
385 call g2_sbytesc(cpack,ifld(n),iofst,grpwidth,0,glength)
386 iofst=iofst+(grpwidth*glength)
395 if (mod(iofst,8).ne.0)
then
402 if (
allocated(ifld) )
deallocate(ifld)
403 if (
allocated(gref) )
deallocate(gref)
404 if (
allocated(gwidth) )
deallocate(gwidth)
405 if (
allocated(glen) )
deallocate(glen)
424 iref=transfer(ref,iref)
426 idrstmpl(4)=nbitsgref
433 idrstmpl(11)=ngwidthref
434 idrstmpl(12)=nbitsgwidth
435 idrstmpl(13)=nglenref
437 idrstmpl(15)=nglenlast
438 idrstmpl(16)=nbitsglen
439 if (idrsnum.eq.3)
then
440 idrstmpl(18)=nbitsd/8