NCEPLIBS-g2  3.4.5
jpcpack.f
Go to the documentation of this file.
1 
6 
37 
38  subroutine jpcpack(fld,width,height,idrstmpl,cpack,lcpack)
39 
40  integer,intent(in) :: width,height
41  real,intent(in) :: fld(width*height)
42  character(len=1),intent(out) :: cpack(*)
43  integer,intent(inout) :: idrstmpl(*)
44  integer,intent(inout) :: lcpack
45 
46  real(4) :: ref,rmin4
47  real(8) :: rmin,rmax
48  integer(4) :: iref
49  integer :: ifld(width*height),retry
50  integer,parameter :: zero=0
51  integer :: enc_jpeg2000
52  character(len=1),allocatable :: ctemp(:)
53 
54  ndpts=width*height
55  bscale=2.0**real(-idrstmpl(2))
56  dscale=10.0**real(idrstmpl(3))
57 !
58 ! Find max and min values in the data
59 !
60  if(ndpts>0) then
61  rmax=fld(1)
62  rmin=fld(1)
63  else
64  rmax=1.0
65  rmin=1.0
66  endif
67  do j=2,ndpts
68  if (fld(j).gt.rmax) rmax=fld(j)
69  if (fld(j).lt.rmin) rmin=fld(j)
70  enddo
71  if (idrstmpl(2).eq.0) then
72  maxdif=nint(rmax*dscale)-nint(rmin*dscale)
73  else
74  maxdif=nint((rmax-rmin)*dscale*bscale)
75  endif
76 !
77 ! If max and min values are not equal, pack up field.
78 ! If they are equal, we have a constant field, and the reference
79 ! value (rmin) is the value for each point in the field and
80 ! set nbits to 0.
81 !
82  if (rmin.ne.rmax .AND. maxdif.ne.0) then
83  !
84  ! Determine which algorithm to use based on user-supplied
85  ! binary scale factor and number of bits.
86  !
87  if (idrstmpl(2).eq.0) then
88  !
89  ! No binary scaling and calculate minimum number of
90  ! bits in which the data will fit.
91  !
92  imin=nint(rmin*dscale)
93  imax=nint(rmax*dscale)
94  maxdif=imax-imin
95  temp=alog(real(maxdif+1))/alog(2.0)
96  nbits=ceiling(temp)
97  rmin=real(imin)
98  ! scale data
99  do j=1,ndpts
100  ifld(j)=nint(fld(j)*dscale)-imin
101  enddo
102  else
103  !
104  ! Use binary scaling factor and calculate minimum number of
105  ! bits in which the data will fit.
106  !
107  rmin=rmin*dscale
108  rmax=rmax*dscale
109  maxdif=nint((rmax-rmin)*bscale)
110  temp=alog(real(maxdif+1))/alog(2.0)
111  nbits=ceiling(temp)
112  ! scale data
113  do j=1,ndpts
114  ifld(j)=max(0,nint(((fld(j)*dscale)-rmin)*bscale))
115  enddo
116  endif
117  !
118  ! Pack data into full octets, then do JPEG2000 encode.
119  ! and calculate the length of the packed data in bytes
120  !
121  retry=0
122  nbytes=(nbits+7)/8
123  nsize=lcpack ! needed for input to enc_jpeg2000
124  allocate(ctemp(nbytes*ndpts))
125  call g2_sbytesc(ctemp,ifld,0,nbytes*8,0,ndpts)
126  lcpack=enc_jpeg2000(ctemp,width,height,nbits,idrstmpl(6),
127  & idrstmpl(7),retry,cpack,nsize)
128  if (lcpack.le.0) then
129  print *,'jpcpack: ERROR Packing JPC=',lcpack
130  if (lcpack.eq.-3) then
131  retry=1
132  print *,'jpcpack: Retrying....'
133  lcpack=enc_jpeg2000(ctemp,width,height,nbits,idrstmpl(6),
134  & idrstmpl(7),retry,cpack,nsize)
135  if (lcpack.le.0) then
136  print *,'jpcpack: Retry Failed.'
137  else
138  print *,'jpcpack: Retry Successful.'
139  endif
140  endif
141  endif
142  deallocate(ctemp)
143 
144  else
145  nbits=0
146  lcpack=0
147  endif
148 
149 !
150 ! Fill in ref value and number of bits in Template 5.0
151 !
152  rmin4 = rmin
153  call mkieee(rmin4,ref,1) ! ensure reference value is IEEE format
154 ! call g2_gbytec(ref,idrstmpl(1),0,32)
155  iref=transfer(ref,iref)
156  idrstmpl(1)=iref
157  idrstmpl(4)=nbits
158  idrstmpl(5)=0 ! original data were reals
159  if (idrstmpl(6).eq.0) idrstmpl(7)=255 ! lossy not used
160 
161  return
162  end
g2_sbytesc
subroutine g2_sbytesc(OUT, IN, ISKIP, NBYTE, NSKIP, N)
This subrountine is to put arbitrary size values into a packed bit string, taking the low order bits ...
Definition: g2_gbytesc.f:115
mkieee
subroutine mkieee(a, rieee, num)
This subroutine stores a list of real values in 32-bit IEEE floating point format.
Definition: mkieee.f:17
jpcpack
subroutine jpcpack(fld, width, height, idrstmpl, cpack, lcpack)
This subroutine packs up a data field into a JPEG2000 code stream.
Definition: jpcpack.f:39