PageRenderTime 67ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/wrfv2_fire/external/io_grib2/g2lib/gbytesc.F

http://github.com/jbeezley/wrf-fire
FORTRAN Legacy | 127 lines | 72 code | 17 blank | 38 comment | 6 complexity | 3a94873535fd55737b61f91b55f9cfdb MD5 | raw file
Possible License(s): AGPL-1.0
  1. SUBROUTINE G2LIB_GBYTE(IN,IOUT,ISKIP,NBYTE)
  2. character*1 in(*)
  3. integer iout(*)
  4. CALL G2LIB_GBYTES(IN,IOUT,ISKIP,NBYTE,0,1)
  5. RETURN
  6. END
  7. SUBROUTINE G2LIB_SBYTE(OUT,IN,ISKIP,NBYTE)
  8. character*1 out(*)
  9. integer in(*)
  10. CALL G2LIB_SBYTES(OUT,IN,ISKIP,NBYTE,0,1)
  11. RETURN
  12. END
  13. SUBROUTINE G2LIB_GBYTES(IN,IOUT,ISKIP,NBYTE,NSKIP,N)
  14. C Get bytes - unpack bits: Extract arbitrary size values from a
  15. C packed bit string, right justifying each value in the unpacked
  16. C array.
  17. C IN = character*1 array input
  18. C IOUT = unpacked array output
  19. C ISKIP = initial number of bits to skip
  20. C NBYTE = number of bits to take
  21. C NSKIP = additional number of bits to skip on each iteration
  22. C N = number of iterations
  23. C v1.1
  24. C
  25. character*1 in(*)
  26. integer iout(*)
  27. integer ones(8), tbit, bitcnt
  28. save ones
  29. data ones/1,3,7,15,31,63,127,255/
  30. c nbit is the start position of the field in bits
  31. nbit = iskip
  32. do i = 1, n
  33. bitcnt = nbyte
  34. index=nbit/8+1
  35. ibit=mod(nbit,8)
  36. nbit = nbit + nbyte + nskip
  37. c first byte
  38. tbit = min(bitcnt,8-ibit)
  39. itmp = iand(mova2i(in(index)),ones(8-ibit))
  40. if (tbit.ne.8-ibit) itmp = ishft(itmp,tbit-8+ibit)
  41. index = index + 1
  42. bitcnt = bitcnt - tbit
  43. c now transfer whole bytes
  44. do while (bitcnt.ge.8)
  45. itmp = ior(ishft(itmp,8),mova2i(in(index)))
  46. bitcnt = bitcnt - 8
  47. index = index + 1
  48. enddo
  49. c get data from last byte
  50. if (bitcnt.gt.0) then
  51. itmp = ior(ishft(itmp,bitcnt),iand(ishft(mova2i(in(index)),
  52. 1 -(8-bitcnt)),ones(bitcnt)))
  53. endif
  54. iout(i) = itmp
  55. enddo
  56. RETURN
  57. END
  58. SUBROUTINE G2LIB_SBYTES(OUT,IN,ISKIP,NBYTE,NSKIP,N)
  59. C Store bytes - pack bits: Put arbitrary size values into a
  60. C packed bit string, taking the low order bits from each value
  61. C in the unpacked array.
  62. C IOUT = packed array output
  63. C IN = unpacked array input
  64. C ISKIP = initial number of bits to skip
  65. C NBYTE = number of bits to pack
  66. C NSKIP = additional number of bits to skip on each iteration
  67. C N = number of iterations
  68. C v1.1
  69. C
  70. character*1 out(*)
  71. integer in(N), bitcnt, ones(8), tbit
  72. save ones
  73. data ones/ 1, 3, 7, 15, 31, 63,127,255/
  74. c number bits from zero to ...
  75. c nbit is the last bit of the field to be filled
  76. nbit = iskip + nbyte - 1
  77. do i = 1, n
  78. itmp = in(i)
  79. bitcnt = nbyte
  80. index=nbit/8+1
  81. ibit=mod(nbit,8)
  82. nbit = nbit + nbyte + nskip
  83. c make byte aligned
  84. if (ibit.ne.7) then
  85. tbit = min(bitcnt,ibit+1)
  86. imask = ishft(ones(tbit),7-ibit)
  87. itmp2 = iand(ishft(itmp,7-ibit),imask)
  88. itmp3 = iand(mova2i(out(index)), 255-imask)
  89. out(index) = char(ior(itmp2,itmp3))
  90. bitcnt = bitcnt - tbit
  91. itmp = ishft(itmp, -tbit)
  92. index = index - 1
  93. endif
  94. c now byte aligned
  95. c do by bytes
  96. do while (bitcnt.ge.8)
  97. out(index) = char(iand(itmp,255))
  98. itmp = ishft(itmp,-8)
  99. bitcnt = bitcnt - 8
  100. index = index - 1
  101. enddo
  102. c do last byte
  103. if (bitcnt.gt.0) then
  104. itmp2 = iand(itmp,ones(bitcnt))
  105. itmp3 = iand(mova2i(out(index)), 255-ones(bitcnt))
  106. out(index) = char(ior(itmp2,itmp3))
  107. endif
  108. enddo
  109. return
  110. end