      subroutine hec_slow_pc_epio
c
c unpack slow control PC data
c
      implicit none
c
      include'hec_epio.inc'        ! epio common
      include'hec_hvpar.inc'      ! # of HV modules and channels
      include'hec_slow_pc.inc'     ! pc slow control common
c
c local variable declarations
c   
      integer maxbloks
      parameter ( maxbloks = 3 )
      integer i,j,nbloks,itype,imod,ival,nch 
      integer ipdata(maxbloks),ipbank(maxbloks),len(maxbloks)
      logical getmore
c
c dump bank content
c 
c      print*,(ibankbuf(i),i=1,ibankbuf(1)+1)
c
c find pointers to, and lenght of, HV, pressure
c and temperature subblocks
c
      getmore = .true.
      nbloks  = 1
      ipbank(nbloks) = 3
      len(nbloks) = ibankbuf(ipbank(nbloks)+1)+1 
c      
      do while (getmore .and. nbloks.lt.maxbloks)
          nbloks = nbloks + 1
          ipbank(nbloks) = ipbank(nbloks-1) +
     +        len(nbloks-1)+1
          len(nbloks) = ibankbuf(ipbank(nbloks)+1)+1 
          if(ipbank(nbloks) .ge. ibankbuf(1))then
             getmore =.false.
c decrease nbloks
             nbloks = nbloks-1
          endif
      enddo
c
c warning if less subblocks than expected
c
      if(nbloks.lt.maxbloks)then
         print *,'  '
         print *,' *** WARNING: only ',nbloks,' subblock in PC '//
     +        'slow control record: '
         do i = 1, nbloks
            if (ibankbuf(ipbank(i)) .eq. 10001)then
               print *, ' *** High Voltage block'
            else if (ibankbuf(ipbank(i)) .eq. 10002)then
               print *, ' *** Pressure block'
            else if (ibankbuf(ipbank(i)) .eq. 10003)then
               print *, ' *** Temperature block'
            endif
         enddo
      endif
c
c find pointers to beginning of data in each
c subblock and decrease subblock lenght
c (skip time information)
c
      do i = 1,nbloks
         len(i) = len(i) - ibankbuf(ipbank(i)+2) - 2 
         ipdata(i)=ipbank(i)+
     +        ibankbuf(ipbank(i)+2) + 3
      enddo
c
c fill common
c
      do i = 1,nbloks
c
c -- High Voltage
c
         if( ibankbuf(ipbank(i)) .eq. 10001 )then
            getmore =.true.
            do while ( getmore )
               itype = ibankbuf(ipdata(i))
               imod  = ibankbuf(ipdata(i)+1)
               ival  = ibankbuf(ipdata(i)+2)
               nch   = ibankbuf(ipdata(i)+3)
               if(itype .eq. 0)then
                  if(imod.gt.nmodhv_cal_max)then
                     print*,'cal module # out of range in'//
     +                    ' hec_slow_pc_epio.f'
                     getmore = .false.
                  endif
                  if(ival.eq.0)then
                     if(nch.gt.nchhv_cal_max)then
                        nch = nchhv_cal_max
                        print*, 'too many current values in'//
     +                      ' hec_slow_pc_epio.f'
                     endif
                     do j=1,nch
                        hv_cal_i(imod,j)=ibankbuf(ipdata(i)+3+j)/100.
                     enddo
                  else if(ival.eq.1)then
                     if(nch.gt.nchhv_cal_max)then
                        nch = nchhv_cal_max
                        print*, 'too many hv values in'//
     +                      ' hec_slow_pc_epio.f'
                     endif
                     do j=1,nch
                        hv_cal_hv(imod,j)=ibankbuf(ipdata(i)+3+j)
                     enddo
                  else
                     print *,' *** Unknown data type in PC '//
     +                    'slow control bank'
                  endif
               else if(itype .eq. 1)then
                  if(imod.gt.nmodhv_pm_max)then
                     print*,'pm module # out of range in'//
     +                    ' hec_slow_pc_epio.f'
                     getmore = .false.
                  endif
                  if(ival.eq.0)then
                     if(nch.gt.nchhv_pm_max)then
                        nch = nchhv_pm_max
                        print*, 'too many PM current values in'//
     +                      ' hec_slow_pc_epio.f'
                     endif
                     do j=1,nch
                        hv_pm_i(imod,j)=ibankbuf(ipdata(i)+3+j)
                     enddo
                  else if(ival.eq.1)then
                     if(nch.gt.nchhv_pm_max)then
                        nch = nchhv_pm_max
                        print*, 'too many MP hv values in'//
     +                      ' hec_slow_pc_epio.f'
                     endif
                     do j=1,nch
                        hv_pm_hv(imod,j)=ibankbuf(ipdata(i)+3+j)
                     enddo
                  else
                     print *,' *** Unknown data type in PC '//
     +                    'slow control bank'
                  endif
               else if(itype. eq. 2)then
                  if(imod.gt.nmodhv_mwpc_max)then
                     print*,'mwpc module # out of range in'//
     +                    ' hec_slow_pc_epio.f'
                     getmore = .false.
                  endif
                  if(ival.eq.0)then
                     if(nch.gt.nchhv_mwpc_max)then
                        nch = nchhv_mwpc_max
                        print*, 'too many MWPC current values in'//
     +                      ' hec_slow_pc_epio.f'
                     endif
                     do j=1,nch
                        hv_mwpc_i(imod,j)=ibankbuf(ipdata(i)+3+j)
                     enddo
                  else if(ival.eq.1)then
                     if(nch.gt.nchhv_mwpc_max)then
                        nch = nchhv_mwpc_max
                        print*, 'too many MWPC current values in'//
     +                      ' hec_slow_pc_epio.f'
                     endif
                     do j=1,nch
                        hv_mwpc_hv(imod,j)=ibankbuf(ipdata(i)+3+j)
                     enddo
                  else
                     print *,' *** Unknown data type in PC '//
     +                    'slow control bank'
                  endif
               else
                  print *,' *** Unknown module type in PC '//
     +                 'slow control bank', itype
               endif
c
               len(i) = len(i) - 3 - nch - 1
               ipdata(i) = ipdata(i) + 3 + nch + 1
               if( len(i) . le . 0) getmore = .false.

            enddo
c
c -- Pressure
c
         else if( ibankbuf(ipbank(i)) .eq. 10002 )then     

            cryo_press=ibankbuf(ipdata(i)+1)
c
c -- Temperatue
c
         else if( ibankbuf(ipbank(i)) .eq. 10003 )then     

            nch = ibankbuf(ipdata(i))
            if(nch .gt. nval_temp_max)then
               nch = nval_temp_max
               print*, 'too many temperature values in'//
     +              ' hec_slow_pc_epio.f, read first ',
     +              nval_temp_max
            endif
            do j=1,nch
               cryo_temp(j)=ibankbuf(ipdata(i)+j)/100.
            enddo
c
c -- Unknown data type
c
         else
            print *,' *** Unknown type of data in PC'//
     +           ' slow control bank',ibankbuf(ipbank(i)) 
         endif
      enddo
c     
c      print*, ' '
c      print*, 'found',(nch_cal_hv(i),i=0,nmod_cal_hv-1),
c     +' hv values in',nmod_cal_hv,' calorimeter modules'
c      print*, 'found',(nch_cal_i(i),i=0,nmod_cal_i-1),
c     +' current values in',nmod_cal_i,' calorimeter modules' 
c      print*, 'found',(nch_pm_hv(i),i=0,nmod_pm_hv-1),
c     +' hv values in',nmod_pm_hv,' PM modules'
c      print*, 'found',(nch_pm_i(i),i=0,nmod_pm_i-1),
c     +' current values',nmod_pm_i,' in PM modules'
c      print*, 'found',(nch_mwpc_hv(i),i=0,nmod_mwpc_hv-1),
c     +' hv values in',nmod_mwpc_hv,' MWPC modules'
c      print*, 'found',(nch_mwpc_i(i),i=0,nmod_mwpc_i-1),
c     +' current values in',nmod_mwpc_i,' MWPC modules'
c
      end












