NCEPLIBS-g2  3.5.0
g2bytes.F90
Go to the documentation of this file.
1 
5 
20 subroutine g2_gbytec(in, iout, iskip, nbits)
21  implicit none
22 
23  character*1, intent(in) :: in(*)
24  integer, intent(inout) :: iout(*)
25  integer, intent(in) :: iskip, nbits
26 
27  interface
28  subroutine g2_gbytesc(in, iout, iskip, nbits, nskip, n)
29  character*1, intent(in) :: in(*)
30  integer, intent(out) :: iout(*)
31  integer, intent(in) :: iskip, nbits, nskip, n
32  end subroutine g2_gbytesc
33  end interface
34 
35  call g2_gbytesc(in, iout, iskip, nbits, 0, 1)
36 end subroutine g2_gbytec
37 
51 subroutine g2_gbytec1(in, siout, iskip, nbits)
52  implicit none
53 
54  character*1, intent(in) :: in(*)
55  integer, intent(inout) :: siout
56  integer, intent(in) :: iskip, nbits
57 
58  integer (kind = 4) :: iout(1)
59 
60  interface
61  subroutine g2_gbytesc(in, iout, iskip, nbits, nskip, n)
62  character*1, intent(in) :: in(*)
63  integer, intent(out) :: iout(*)
64  integer, intent(in) :: iskip, nbits, nskip, n
65  end subroutine g2_gbytesc
66  end interface
67 
68  call g2_gbytesc(in, iout, iskip, nbits, 0, 1)
69  siout = iout(1)
70 end subroutine g2_gbytec1
71 
81 !iteration.
85 subroutine g2_gbytescr(in, rout, iskip, nbits, nskip, n)
86  implicit none
87  character*1, intent(in) :: in(*)
88  real (kind = 4), intent(out) :: rout(*)
89  integer, intent(in) :: iskip, nbits, nskip, n
90  integer (kind = 4) :: iout(n)
91 
92  interface
93  subroutine g2_gbytesc(in, iout, iskip, nbits, nskip, n)
94  character*1, intent(in) :: in(*)
95  integer, intent(out) :: iout(*)
96  integer, intent(in) :: iskip, nbits, nskip, n
97  end subroutine g2_gbytesc
98  end interface
99 
100  ! Unpack into integer array.
101  call g2_gbytesc(in, iout, iskip, nbits, nskip, n)
102 
103  ! Transfer to real array.
104  rout(1:n) = transfer(iout, rout(1:n), n)
105 end subroutine g2_gbytescr
106 
119 subroutine g2_gbytesc(in, iout, iskip, nbits, nskip, n)
120  implicit none
121 
122  character*1, intent(in) :: in(*)
123  integer, intent(out) :: iout(*)
124  integer, intent(in) :: iskip, nbits, nskip, n
125  integer :: tbit, bitcnt
126  integer, parameter :: ones(8) = (/ 1, 3, 7, 15, 31, 63, 127, 255 /)
127 
128  integer :: nbit, i, index, ibit, itmp
129  integer, external :: g2_mova2i
130 
131  ! nbit is the start position of the field in bits
132  nbit = iskip
133  do i = 1, n
134  bitcnt = nbits
135  index = nbit / 8 + 1
136  ibit = mod(nbit, 8)
137  nbit = nbit + nbits + nskip
138 
139  ! first byte
140  tbit = min(bitcnt, 8 - ibit)
141  itmp = iand(g2_mova2i(in(index)), ones(8 - ibit))
142  if (tbit .ne. 8 - ibit) itmp = ishft(itmp, tbit - 8 + ibit)
143  index = index + 1
144  bitcnt = bitcnt - tbit
145 
146  ! now transfer whole bytes
147  do while (bitcnt .ge. 8)
148  itmp = ior(ishft(itmp,8), g2_mova2i(in(index)))
149  bitcnt = bitcnt - 8
150  index = index + 1
151  enddo
152 
153  ! get data from last byte
154  if (bitcnt .gt. 0) then
155  itmp = ior(ishft(itmp, bitcnt), iand(ishft(g2_mova2i(in(index)), &
156  - (8 - bitcnt)), ones(bitcnt)))
157  endif
158 
159  iout(i) = itmp
160  enddo
161 
162 end subroutine g2_gbytesc
163 
177 subroutine g2_gbytec8(in, iout, iskip, nbits)
178  implicit none
179 
180  character*1, intent(in) :: in(*)
181  integer (kind = 8), intent(inout) :: iout(*)
182  integer, intent(in) :: iskip, nbits
183 
184  interface
185  subroutine g2_gbytesc8(in, iout, iskip, nbits, nskip, n)
186  character*1, intent(in) :: in(*)
187  integer (kind = 8), intent(out) :: iout(*)
188  integer, intent(in) :: iskip, nbits, nskip, n
189  end subroutine g2_gbytesc8
190  end interface
191 
192  call g2_gbytesc8(in, iout, iskip, nbits, 0, 1)
193 end subroutine g2_gbytec8
194 
208 subroutine g2_gbytec81(in, siout, iskip, nbits)
209  implicit none
210 
211  character*1, intent(in) :: in(*)
212  integer (kind = 8), intent(inout) :: siout
213  integer, intent(in) :: iskip, nbits
214  integer (kind = 8) :: iout(1)
215 
216  interface
217  subroutine g2_gbytesc8(in, iout, iskip, nbits, nskip, n)
218  character*1, intent(in) :: in(*)
219  integer (kind = 8), intent(out) :: iout(*)
220  integer, intent(in) :: iskip, nbits, nskip, n
221  end subroutine g2_gbytesc8
222  end interface
223 
224  call g2_gbytesc8(in, iout, iskip, nbits, 0, 1)
225  siout = iout(1)
226 end subroutine g2_gbytec81
227 
240 subroutine g2_gbytesc8(in, iout, iskip, nbits, nskip, n)
241  implicit none
242 
243  character*1, intent(in) :: in(*)
244  integer (kind = 8), intent(out) :: iout(*)
245  integer, intent(in) :: iskip, nbits, nskip, n
246 
247  integer :: tbit, bitcnt
248  integer, parameter :: ones(8) = (/ 1, 3, 7, 15, 31, 63, 127, 255 /)
249 
250  integer :: nbit, i, index, ibit, itmp
251  integer (kind = 8) :: itmp8, itmp8_2, itmp8_3
252  integer, external :: g2_mova2i
253  integer (kind = 8), external :: g2_mova2i8
254 
255  ! Nbit is the start position of the field in bits.
256  nbit = iskip
257  do i = 1, n
258  bitcnt = nbits
259  index = nbit / 8 + 1
260  ibit = mod(nbit, 8)
261  nbit = nbit + nbits + nskip
262 
263  ! first byte
264  tbit = min(bitcnt, 8 - ibit)
265  itmp8 = iand(g2_mova2i8(in(index)), int(ones(8 - ibit), kind = 8))
266  itmp = iand(g2_mova2i(in(index)), ones(8 - ibit))
267  if (tbit .ne. 8 - ibit) itmp = ishft(itmp, tbit - 8 + ibit)
268  if (tbit .ne. 8 - ibit) itmp8 = ishft(itmp8, tbit - 8 + ibit)
269  index = index + 1
270  bitcnt = bitcnt - tbit
271 
272  ! now transfer whole bytes
273  do while (bitcnt .ge. 8)
274  itmp = ior(ishft(itmp,8), g2_mova2i(in(index)))
275  itmp8 = ior(ishft(itmp8,8), g2_mova2i8(in(index)))
276  bitcnt = bitcnt - 8
277  index = index + 1
278  enddo
279 
280  ! get data from last byte
281  if (bitcnt .gt. 0) then
282  itmp = ior(ishft(itmp, bitcnt), iand(ishft(g2_mova2i(in(index)), - (8 - bitcnt)), ones(bitcnt)))
283  itmp8_2 = ishft(g2_mova2i8(in(index)), int(-(8 - bitcnt), kind(8)))
284  itmp8_3 = int(ones(bitcnt), kind(8))
285  itmp8 = ior(ishft(itmp8, bitcnt), iand(itmp8_2, itmp8_3))
286  endif
287 
288  iout(i) = itmp8
289  enddo
290 end subroutine g2_gbytesc8
291 
305 subroutine g2_sbytec(out, in, iskip, nbits)
306  implicit none
307 
308  character*1, intent(inout) :: out(*)
309  integer, intent(in) :: in(*)
310  integer, intent(in) :: iskip, nbits
311 
312  interface
313  subroutine g2_sbytesc(out, in, iskip, nbits, nskip, n)
314  character*1, intent(out) :: out(*)
315  integer, intent(in) :: in(n)
316  integer, intent(in) :: iskip, nbits, nskip, n
317  end subroutine g2_sbytesc
318  end interface
319 
320  call g2_sbytesc(out, in, iskip, nbits, 0, 1)
321 end subroutine g2_sbytec
322 
336 subroutine g2_sbytec1(out, in, iskip, nbits)
337  implicit none
338 
339  character*1, intent(inout) :: out(*)
340  integer, intent(in) :: in
341  integer, intent(in) :: iskip, nbits
342  integer :: ain(1)
343 
344  interface
345  subroutine g2_sbytesc(out, in, iskip, nbits, nskip, n)
346  character*1, intent(out) :: out(*)
347  integer, intent(in) :: in(n)
348  integer, intent(in) :: iskip, nbits, nskip, n
349  end subroutine g2_sbytesc
350  end interface
351 
352  ain(1) = in
353  call g2_sbytesc(out, ain, iskip, nbits, 0, 1)
354 end subroutine g2_sbytec1
355 
367 subroutine g2_sbytescr(out, rin, iskip, nbits, nskip, n)
368  implicit none
369  character*1, intent(out) :: out(*)
370  real, intent(in) :: rin(n)
371  integer, intent(in) :: iskip, nbits, nskip, n
372  integer :: in(n)
373 
374  interface
375  subroutine g2_sbytesc(out, in, iskip, nbits, nskip, n)
376  character*1, intent(out) :: out(*)
377  integer, intent(in) :: in(n)
378  integer, intent(in) :: iskip, nbits, nskip, n
379  end subroutine g2_sbytesc
380  end interface
381 
382  ! Transfer real array to integer array.
383  in(1:n) = transfer(rin, in(1:n), n)
384 
385  ! Pack into character array.
386  call g2_sbytesc(out, in, iskip, nbits, nskip, n)
387 end subroutine g2_sbytescr
388 
401 subroutine g2_sbytesc(out, in, iskip, nbits, nskip, n)
402  implicit none
403 
404  character*1, intent(out) :: out(*)
405  integer, intent(in) :: in(n)
406  integer, intent(in) :: iskip, nbits, nskip, n
407 
408  integer :: bitcnt, tbit
409  integer, parameter :: ones(8)=(/ 1, 3, 7, 15, 31, 63, 127, 255/)
410  integer :: nbit, i, itmp, index, ibit, imask, itmp2, itmp3
411  integer, external :: g2_mova2i
412 
413  ! number bits from zero to ...
414  ! nbit is the last bit of the field to be filled
415  nbit = iskip + nbits - 1
416  !print *, 'nbit', nbit, 'nbits ', nbits, 'nskip', nskip, 'n', n
417  do i = 1, n
418  itmp = in(i)
419  bitcnt = nbits
420  index = nbit / 8 + 1
421  ibit = mod(nbit, 8)
422  nbit = nbit + nbits + nskip
423  !print *, 'i', i, 'itmp', itmp, 'bitcnt', bitcnt, 'index', index, 'ibit', ibit, 'nbit', nbit
424 
425  ! make byte aligned
426  if (ibit .ne. 7) then
427  tbit = min(bitcnt, ibit + 1)
428  imask = ishft(ones(tbit), 7 - ibit)
429  itmp2 = iand(ishft(itmp, 7 - ibit),imask)
430  itmp3 = iand(g2_mova2i(out(index)), 255 - imask)
431  out(index) = char(ior(itmp2, itmp3))
432  bitcnt = bitcnt - tbit
433  itmp = ishft(itmp, -tbit)
434  index = index - 1
435  endif
436 
437  ! now byte aligned
438 
439  ! do by bytes
440  do while (bitcnt .ge. 8)
441  out(index) = char(iand(itmp, 255))
442  !print '(z2.2, x, z2.2, x, z2.2)', out(index), itmp, iand(itmp, 255)
443  itmp = ishft(itmp, -8)
444  bitcnt = bitcnt - 8
445  index = index - 1
446  enddo
447 
448  ! Do left over bits.
449  if (bitcnt .gt. 0) then
450  itmp2 = iand(itmp, ones(bitcnt))
451  !print '(z2.2, x, z2.2)', ones(bitcnt), itmp2
452  itmp3 = iand(g2_mova2i(out(index)), 255 - ones(bitcnt))
453  out(index) = char(ior(itmp2, itmp3))
454  endif
455  enddo
456 end subroutine g2_sbytesc
457 
472 subroutine g2_sbytec8(out, in, iskip, nbits)
473  implicit none
474 
475  character*1, intent(inout) :: out(*)
476  integer (kind = 8), intent(in) :: in(*)
477  integer, intent(in) :: iskip, nbits
478 
479  interface
480  subroutine g2_sbytesc8(out, in, iskip, nbits, nskip, n)
481  character*1, intent(out) :: out(*)
482  integer (kind = 8), intent(in) :: in(n)
483  integer, intent(in) :: iskip, nbits, nskip, n
484  end subroutine g2_sbytesc8
485  end interface
486 
487  call g2_sbytesc8(out, in, iskip, nbits, 0, 1)
488 end subroutine g2_sbytec8
489 
504 subroutine g2_sbytec81(out, sin, iskip, nbits)
505  implicit none
506 
507  character*1, intent(inout) :: out(*)
508  integer (kind = 8), intent(in) :: sin
509  integer, intent(in) :: iskip, nbits
510 
511  integer (kind = 8) :: in(1)
512 
513  interface
514  subroutine g2_sbytesc8(out, in, iskip, nbits, nskip, n)
515  character*1, intent(out) :: out(*)
516  integer (kind = 8), intent(in) :: in(n)
517  integer, intent(in) :: iskip, nbits, nskip, n
518  end subroutine g2_sbytesc8
519  end interface
520 
521  in(1) = sin
522  call g2_sbytesc8(out, in, iskip, nbits, 0, 1)
523 end subroutine g2_sbytec81
524 
538 subroutine g2_sbytesc8(out, in, iskip, nbits, nskip, n)
539  implicit none
540 
541  character*1, intent(out) :: out(*)
542  integer (kind = 8), intent(in) :: in(n)
543  integer, intent(in) :: iskip, nbits, nskip, n
544 
545  integer :: bitcnt, tbit
546  integer, parameter :: ones(8)=(/ 1, 3, 7, 15, 31, 63, 127, 255/)
547  integer :: nbit, i, index, ibit, imask, itmp1, itmp2, itmp3
548  integer (kind = 8) :: itmp8, itmp8_2
549  integer, external :: g2_mova2i
550 
551  ! number bits from zero to ...
552  ! nbit is the last bit of the field to be filled
553  nbit = iskip + nbits - 1
554  !print *, 'nbit', nbit, 'nbits ', nbits, 'nskip', nskip, 'n', n
555  do i = 1, n
556  itmp8 = in(i)
557  bitcnt = nbits
558  index = nbit / 8 + 1
559  ibit = mod(nbit, 8)
560  nbit = nbit + nbits + nskip
561  !print *, 'i', i, 'itmp8', itmp8, 'bitcnt', bitcnt, 'index', index, 'ibit', ibit, 'nbit', nbit
562 
563  ! make byte aligned
564  if (ibit .ne. 7) then
565  tbit = min(bitcnt, ibit + 1)
566  imask = ishft(ones(tbit), 7 - ibit)
567  itmp1 = int(ishft(itmp8, int(7 - ibit, kind(8))), kind(4))
568  itmp2 = iand(itmp1, imask)
569  itmp3 = iand(g2_mova2i(out(index)), 255 - imask)
570  out(index) = char(ior(itmp2, itmp3))
571  bitcnt = bitcnt - tbit
572  itmp8 = ishft(itmp8, -tbit)
573  index = index - 1
574  endif
575 
576  ! now byte aligned
577 
578  ! Process a byte at a time.
579  do while (bitcnt .ge. 8)
580  !print *, bitcnt, iand(itmp8, 255_8)
581  out(index) = char(iand(itmp8, 255_8))
582  itmp8 = ishft(itmp8, -8)
583  bitcnt = bitcnt - 8
584  index = index - 1
585  enddo
586 
587  ! Take care of left over bits.
588  if (bitcnt .gt. 0) then
589  itmp8_2 = int(ones(bitcnt), kind(8))
590  itmp2 = int(iand(itmp8, itmp8_2), kind(4))
591  itmp3 = iand(g2_mova2i(out(index)), 255 - ones(bitcnt))
592  out(index) = char(ior(itmp2, itmp3))
593  endif
594  enddo
595 end subroutine g2_sbytesc8
596 
606 subroutine rdieeec(cieee, a, num)
607  implicit none
608 
609  character(len = 1), intent(in) :: cieee(*)
610  real, intent(out) :: a(num)
611  integer, intent(in) :: num
612  real (kind = 4) :: rieee(num)
613 
614  interface
615  subroutine rdieee(rieee, a, num)
616  real(4), intent(in) :: rieee(num)
617  real, intent(out) :: a(num)
618  integer, intent(in) :: num
619  end subroutine rdieee
620  end interface
621 
622  rieee(1:num) = transfer(cieee(1:num * 4), rieee, num)
623  call rdieee(rieee, a, num)
624 end subroutine rdieeec
625 
636 subroutine rdieee(rieee, a, num)
637  implicit none
638 
639  real(4), intent(in) :: rieee(num)
640  real, intent(out) :: a(num)
641  integer, intent(in) :: num
642 
643  integer(4) :: ieee
644  real, parameter :: two23 = scale(1.0, -23)
645  real, parameter :: two126 = scale(1.0, -126)
646  integer :: iexp, imant, isign, j
647  real :: sign, temp
648 
649  do j = 1, num
650  ! Transfer IEEE bit string to integer variable.
651  ieee = transfer(rieee(j), ieee)
652 
653  ! Extract sign bit, exponent, and mantissa.
654  isign = ibits(ieee, 31, 1)
655  iexp = ibits(ieee, 23, 8)
656  imant = ibits(ieee, 0, 23)
657  sign = 1.0
658  if (isign .eq. 1) sign = -1.0
659 
660  if (iexp .gt. 0 .and. iexp .lt. 255) then
661  temp = 2.0**(iexp - 127)
662  a(j) = sign * temp * (1.0 + (two23 * real(imant)))
663  elseif (iexp .eq. 0) then
664  if (imant .ne. 0) then
665  a(j) = sign * two126 * two23 * real(imant)
666  else
667  a(j) = sign * 0.0
668  endif
669  elseif (iexp .eq. 255) then
670  a(j) = sign * huge(a(j))
671  endif
672  enddo
673 end subroutine rdieee
674 
684 subroutine mkieee(a, rieee, num)
685  implicit none
686 
687  real(4), intent(in) :: a(num)
688  real(4), intent(out) :: rieee(num)
689  integer, intent(in) :: num
690 
691  integer(4) :: ieee
692  real, parameter :: two23 = scale(1.0,23)
693  real, parameter :: two126 = scale(1.0,126)
694  real :: alog2, atemp
695  integer :: iexp, imant, j, n
696 
697  alog2 = alog(2.0)
698 
699  do j = 1, num
700  ieee = 0
701  if (a(j) .eq. 0.) then
702  ieee = 0
703  rieee(j) = transfer(ieee, rieee(j))
704  cycle
705  endif
706 
707  ! Set Sign bit (bit 31 - leftmost bit).
708  if (a(j) .lt. 0.0) then
709  ieee = ibset(ieee, 31)
710  atemp = abs(a(j))
711  else
712  ieee = ibclr(ieee, 31)
713  atemp = a(j)
714  endif
715 
716  ! Determine exponent n with base 2.
717  if (atemp .ge. 1.0) then
718  n = 0
719  do while (2.0**(n+1) .le. atemp)
720  n = n + 1
721  enddo
722  else
723  n = -1
724  do while (2.0**n .gt. atemp )
725  n = n - 1
726  enddo
727  endif
728  iexp = n + 127
729  if (n .gt. 127) iexp = 255 ! overflow
730  if (n .lt. -127) iexp = 0
731  call mvbits(iexp, 0, 8, ieee, 23)
732 
733  ! Determine Mantissa.
734  if (iexp .ne. 255) then
735  if (iexp .ne. 0) then
736  atemp = (atemp / (2.0**n)) - 1.0
737  else
738  atemp = atemp * two126
739  endif
740  imant = nint(atemp * two23)
741  else
742  imant = 0
743  endif
744  ! set mantissa bits (bits 22-0).
745  call mvbits(imant, 0, 23, ieee, 0)
746 
747  ! Transfer IEEE bit string to real variable.
748  rieee(j) = transfer(ieee, rieee(j))
749  enddo
750 end subroutine mkieee
751 
subroutine g2_gbytesc(in, iout, iskip, nbits, nskip, n)
Extract arbitrary size big-endian integer values (up to 32 bits each) from a packed bit string.
Definition: g2bytes.F90:120
subroutine rdieee(rieee, a, num)
Copy array of 32-bit IEEE floating point values to local floating point representation.
Definition: g2bytes.F90:637
subroutine g2_sbytesc8(out, in, iskip, nbits, nskip, n)
Put arbitrary sized (up to 64 bits each) values into a packed bit string, taking the low order bits f...
Definition: g2bytes.F90:539
subroutine g2_gbytec(in, iout, iskip, nbits)
Extract one arbitrary size big-endian value (up to 32 bits) from a packed bit string into one element...
Definition: g2bytes.F90:21
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_sbytescr(out, rin, iskip, nbits, nskip, n)
Put real values into a packed bit string in big-endian order.
Definition: g2bytes.F90:368
subroutine g2_gbytec8(in, iout, iskip, nbits)
Extract one arbitrary sized (up to 64-bits) values from a packed bit string, right justifying each va...
Definition: g2bytes.F90:178
subroutine g2_sbytec81(out, sin, iskip, nbits)
Put one arbitrary sized (up to 64 bits) scalar into a packed bit string, taking the low order bits fr...
Definition: g2bytes.F90:505
subroutine g2_sbytec(out, in, iskip, nbits)
Put one arbitrary sized (up to 32 bits) value from an integer array, into a packed bit string,...
Definition: g2bytes.F90:306
subroutine g2_sbytec1(out, in, iskip, nbits)
Put one arbitrary sized (up to 32 bits) values from an integer scalar into a packed bit string,...
Definition: g2bytes.F90:337
subroutine rdieeec(cieee, a, num)
Copy array of 32-bit IEEE floating point values stored in char array to local floating point represen...
Definition: g2bytes.F90:607
subroutine g2_gbytec81(in, siout, iskip, nbits)
Extract one arbitrary size big-endian integer value (up to 64 bits) from a packed bit string into a s...
Definition: g2bytes.F90:209
subroutine g2_gbytesc8(in, iout, iskip, nbits, nskip, n)
Extract arbitrary sized (up to 64-bits) values from a packed bit string, right justifying each value ...
Definition: g2bytes.F90:241
subroutine g2_gbytescr(in, rout, iskip, nbits, nskip, n)
Extract big-endian floating-point values (32 bits each) from a packed bit string.
Definition: g2bytes.F90:86
subroutine g2_sbytec8(out, in, iskip, nbits)
Put one arbitrary sized (up to 64 bits) values into a packed bit string, taking the low order bits fr...
Definition: g2bytes.F90:473
subroutine g2_gbytec1(in, siout, iskip, nbits)
Extract one arbitrary size big-endian integer value (up to 32 bits) from a packed bit string into a s...
Definition: g2bytes.F90:52
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