      subroutine hec_adc_unpk
c
c unpack adc values
c
      implicit none
c
      include'hec_par.inc'      !shared parameters
      include'hec_geo.inc'      !geometry correspondence tables 
      include'hec_adc.inc'      !adc values
c
c local variables
c
      integer ic, i_t
      integer ipat, ipat2
      integer i_adc_even, i_adc_odd
      integer chnum
      integer*4 target
c
c unpack bits and fill common. 
c
      do ic = 2, i_adc_used, 2
        do i_t = 1, i_tim_used
          ipat = i_adc_pk(ic - 1, i_t) 
          ipat2 = i_adc_pk(ic, i_t)
c
c cernlib function from Kernlib M421. Copies "ipat2(ipat)" starting at
c bit 1(1) into "target" byte starting at bit 1 (17). 
c Bytes are here defined as 16 bits. 
c
          call cbyt(ipat,1,target,17,16)
          call cbyt(ipat2,1,target,1,16)
c
c make binary into actual channel numbers and adc values
c first 8 bits are channel, then 12 bits for even numbered 
c channel followed by 12 bits for odd numbered channel.
c ibits is from cernlib Kernlib M441. It translates a certain
c bit range in a word into an integer (counts right to left).
c
c
          chnum = ibits(target,0,8)
c
c verify channel number
c
c
c          if (chnum .ne. ic) then
c            print *,' *** hec_adc_unpk, channel number from data '
c     +             ,chnum, ' is different from expected ',ic
c            stop
c          endif
c
          i_adc_odd = ibits(target,8,12)
          i_adc_even = ibits(target,20,12)
c
c fill adc array
c
          i_adc_ic_t(ic - 1, i_t) = i_adc_odd
          i_adc_ic_t(ic, i_t) = i_adc_even
c
c print debug
c
ccc          print *, 'ipat,ipat2 -->',ipat,ipat2
ccc          print *, 'chnum, even, odd-->',chnum,i_adc_even,i_adc_odd
c
        enddo
      enddo
c
c     print *,' *** adc data unpacked'
c
      end









