NCEPLIBS-g2  3.5.0
g2jpc.F90
Go to the documentation of this file.
1 
6 
34 subroutine jpcpack(fld,width,height,idrstmpl,cpack,lcpack)
35  implicit none
36 
37  integer,intent(in) :: width,height
38  real,intent(in) :: fld(width*height)
39  character(len=1),intent(out) :: cpack(*)
40  integer,intent(inout) :: idrstmpl(*)
41  integer,intent(inout) :: lcpack
42  integer :: ndpts, nbits, maxdif, imin, imax, j, nbytes
43  real :: dscale, bscale, temp
44 
45  interface
46  function enc_jpeg2000(cin, width, height, nbits, ltype, ratio, retry, outjpc, jpclen) &
47  bind(c, name="g2c_enc_jpeg2000")
48  use iso_c_binding
49  character(kind = c_char), intent(in) :: cin(*)
50  integer(c_int), value, intent(in) :: width, height, nbits, ltype, ratio, retry
51  character(kind = c_char), intent(inout) :: outjpc(*)
52  integer(c_size_t), value, intent(in) :: jpclen
53  integer(c_int) :: enc_jpeg2000
54  end function enc_jpeg2000
55  end interface
56 
57  real(4) :: ref, rmin4
58  real(8) :: rmin, rmax
59  integer(4) :: iref
60  integer(8) :: nsize
61  integer :: ifld(width * height), retry
62  integer, parameter :: zero = 0
63  character(len = 1), allocatable :: ctemp(:)
64 
65  ndpts = width * height
66  bscale = 2.0**real(-idrstmpl(2))
67  dscale = 10.0**real(idrstmpl(3))
68 
69  ! Find max and min values in the data.
70  if (ndpts > 0) then
71  rmax = fld(1)
72  rmin = fld(1)
73  else
74  rmax = 1.0
75  rmin = 1.0
76  endif
77  do j = 2, ndpts
78  if (fld(j) .gt. rmax) rmax = fld(j)
79  if (fld(j) .lt. rmin) rmin = fld(j)
80  enddo
81  if (idrstmpl(2) .eq. 0) then
82  maxdif = nint(rmax * dscale) - nint(rmin * dscale)
83  else
84  maxdif = nint((rmax - rmin) * dscale * bscale)
85  endif
86 
87  ! If max and min values are not equal, pack up field. If they are
88  ! equal, we have a constant field, and the reference value (rmin) is
89  ! the value for each point in the field and set nbits to 0.
90  if (rmin .ne. rmax .AND. maxdif .ne. 0) then
91  ! Determine which algorithm to use based on user-supplied binary
92  ! scale factor and number of bits.
93  if (idrstmpl(2) .eq. 0) then
94  ! No binary scaling and calculate minimum number of bits in
95  ! which the data will fit.
96  imin = nint(rmin * dscale)
97  imax = nint(rmax * dscale)
98  maxdif = imax - imin
99  temp = alog(real(maxdif + 1)) / alog(2.0)
100  nbits = ceiling(temp)
101  rmin = real(imin)
102  ! Scale data.
103  do j = 1, ndpts
104  ifld(j) = nint(fld(j) * dscale) - imin
105  enddo
106  else
107  ! Use binary scaling factor and calculate minimum number of
108  ! bits in which the data will fit.
109  rmin = rmin * dscale
110  rmax = rmax * dscale
111  maxdif = nint((rmax - rmin) * bscale)
112  temp = alog(real(maxdif + 1)) / alog(2.0)
113  nbits = ceiling(temp)
114  ! scale data
115  do j = 1, ndpts
116  ifld(j) = max(0, nint(((fld(j) * dscale) - rmin) * bscale))
117  enddo
118  endif
119 
120  ! Pack data into full octets, then do JPEG2000 encode. and
121  ! calculate the length of the packed data in bytes
122  retry = 0
123  nbytes = (nbits + 7) / 8
124  nsize = lcpack ! needed for input to enc_jpeg2000
125  allocate(ctemp(nbytes * ndpts))
126  call g2_sbytesc(ctemp, ifld, 0, nbytes * 8, 0, ndpts)
127  lcpack = enc_jpeg2000(ctemp, width, height, nbits, idrstmpl(6), &
128  idrstmpl(7), retry, cpack, nsize)
129  if (lcpack .le. 0) then
130  print *,'jpcpack: ERROR Packing JPC = ',lcpack
131  if (lcpack .eq. -3) then
132  retry = 1
133  print *,'jpcpack: Retrying....'
134  lcpack = enc_jpeg2000(ctemp, width, height, nbits, idrstmpl(6), &
135  idrstmpl(7), retry, cpack, nsize)
136  if (lcpack .le. 0) then
137  print *,'jpcpack: Retry Failed.'
138  else
139  print *,'jpcpack: Retry Successful.'
140  endif
141  endif
142  endif
143  deallocate(ctemp)
144  else
145  nbits = 0
146  lcpack = 0
147  endif
148 
149  ! Fill in ref value and number of bits in Template 5.0.
150  rmin4 = real(rmin, 4)
151  call mkieee(rmin4, ref, 1) ! ensure reference value is IEEE format.
152  iref = transfer(ref, iref)
153  idrstmpl(1) = iref
154  idrstmpl(4) = nbits
155  idrstmpl(5) = 0 ! original data were reals.
156  if (idrstmpl(6) .eq. 0) idrstmpl(7) = 255 ! lossy not used
157 
158 end subroutine
159 
176 subroutine jpcunpack(cpack,len,idrstmpl,ndpts,fld)
177  implicit none
178 
179  character(len=1),intent(in) :: cpack(len)
180  integer,intent(in) :: ndpts,len
181  integer,intent(in) :: idrstmpl(*)
182  real,intent(out) :: fld(ndpts)
183 
184  integer :: ifld(ndpts)
185  integer(4) :: ieee
186  integer(8) :: len8
187  real :: ref,bscale,dscale
188  integer :: nbits, j, iret
189 
190  interface
191  function dec_jpeg2000(cin, len, ifld) &
192  bind(c, name="g2c_dec_jpeg2000")
193  use iso_c_binding
194  character(kind = c_char), intent(in) :: cin(*)
195  integer(c_size_t), value, intent(in) :: len
196  integer(c_int), intent(inout) :: ifld(*)
197  integer(c_int) :: dec_jpeg2000
198  end function dec_jpeg2000
199  end interface
200 
201  ieee = idrstmpl(1)
202  call rdieee(ieee,ref,1)
203  bscale = 2.0**real(idrstmpl(2))
204  dscale = 10.0**real(-idrstmpl(3))
205  nbits = idrstmpl(4)
206 
207  ! if nbits equals 0, we have a constant field where the reference value
208  ! is the data value at each gridpoint
209  if (nbits.ne.0) then
210  ! call g2_gbytesc(cpack,ifld,0,nbits,0,ndpts)
211  len8 = len
212  iret=dec_jpeg2000(cpack,len8,ifld)
213  do j=1,ndpts
214  fld(j)=((real(ifld(j))*bscale)+ref)*dscale
215  enddo
216  else
217  do j=1,ndpts
218  fld(j)=ref
219  enddo
220  endif
221 
222 end subroutine jpcunpack
subroutine rdieee(rieee, a, num)
Copy array of 32-bit IEEE floating point values to local floating point representation.
Definition: g2bytes.F90:637
subroutine mkieee(a, rieee, num)
Copy an array of real to an array of 32-bit IEEE floating points.
Definition: g2bytes.F90:685
subroutine g2_sbytesc(out, in, iskip, nbits, nskip, n)
Put arbitrary size (up to 32 bits each) integer values into a packed bit string in big-endian order.
Definition: g2bytes.F90:402
int dec_jpeg2000(char *injpc, g2int bufsize, g2int *outfld)
This Function decodes a JPEG2000 code stream specified in the JPEG2000 Part-1 standard (i....
Definition: g2cjpeg2000.c:367
int enc_jpeg2000(unsigned char *cin, g2int width, g2int height, g2int nbits, g2int ltype, g2int ratio, g2int retry, char *outjpc, g2int jpclen)
This Function encodes a grayscale image into a JPEG2000 code stream specified in the JPEG2000 Part-1 ...
Definition: g2cjpeg2000.c:44
subroutine jpcunpack(cpack, len, idrstmpl, ndpts, fld)
Unpack a data field from a JPEG2000 code stream as defined in Data Representation Template 5....
Definition: g2jpc.F90:177
subroutine jpcpack(fld, width, height, idrstmpl, cpack, lcpack)
Pack a data field into a JPEG2000 code stream as defined in Data Representation Template 5....
Definition: g2jpc.F90:35