      module venond
      implicit none
      integer,parameter :: lungo=100
      integer,private :: mdestod=0
      integer,private :: ndestod=0
      integer,private :: mdestod1=0
      integer,private :: ndestod1=0
      integer,private :: mdestod2561=0
      integer,private :: mdestod256=0
      integer,private :: npie2=0
      integer,private :: mpie2=0
      integer,private :: mpie256=0 
      integer,private :: iii=1
      integer,private :: filedet
      integer,private :: ms2,norb,noac
      character(1),allocatable,dimension(:) ::  ctps,cex,c256
      contains
       subroutine venond_init(norb_in,ms2_in,noac_in,filedet_in)
      implicit none
      integer ms2_in,norb_in,noac_in,filedet_in
      intent(in) ms2_in,norb_in,noac_in,filedet_in
      integer maxne
      filedet=filedet_in
      rewind filedet
      ms2=ms2_in
      norb=norb_in
      noac=noac_in
      mdestod=0
      ndestod=0
      mdestod1=0
      ndestod1=0
      mdestod2561=0
      mdestod256=0
      npie2=0
      mpie2=0
      mpie256=0 
      iii=1
      maxne=noac+2
      if(.not.allocated(ctps)) allocate(ctps((maxne)*lungo*2),cex(lungo)
     *,c256((maxne/4+1)*lungo))
      end subroutine

       subroutine venond2(ne,ntv,npv,ierr)
      implicit none
      integer,dimension(noac+2) :: ntv,npv
      integer np1024(4)
      logical(1) yw
      integer ierr,nems,mst,msp,ne
      integer it,k,kk,kkk,i
      yw=.false.
      ierr=0
      ndestod1=ndestod1+1
      if(ndestod1.gt.ndestod) then
         read(filedet,end=1) mdestod,ndestod,(ctps(i),i=1,mdestod),
     *   (cex(i),i=1,ndestod)
     *   ,mdestod256,(c256(i),i=1,mdestod256)
C         if(yw)print*,'venond2 lect ndestod,mdestod',ndestod,mdestod
c        print*,'venond2 ctps', (ichar(ctps(i)),i=1,mdestod)
c         print*,'c256',(ichar(c256(k)),k=1,mdestod256)
         ndestod1=1
         mdestod1=0
         mdestod2561=0
C         if(yw)print*,'venond2 ndestod',ndestod
      endif
C      if(yw)print*,'venond2 ndestod1',ndestod1
      nems=ichar(cex(ndestod1))
      call venond4(nems,ne,mst)
C      if(yw)print*,'nems',nems
      msp=mst-(-ms2+1)/2
C      if(yw)print'(a,20i4)','nems,ne,mst,msp,ms2',nems,ne,mst,msp,ms2
C      if(yw)print'(a,20i4)','char',(ichar(ctps(k)),k=1,ne+ne)
      if(ne.eq.0) return
      do k=1,mst-1
         mdestod1=mdestod1+1
         it=ichar(ctps(mdestod1))
C         if(yw)print*,'it',it
         if(it.le.0) it=it+256
         ntv(k)=it
      enddo
      do k=mst,ne
         mdestod1=mdestod1+1
         it=ichar(ctps(mdestod1))+norb
C         if(yw)print*,'it',it,norb
         if(it.le.norb) it=it+256
         ntv(k)=it
      enddo
      do k=1,msp-1
         mdestod1=mdestod1+1
         it=ichar(ctps(mdestod1))
         if(it.le.0) it=it+256
C         if(yw)print*,'pit',it
         npv(k)=it
      enddo
      do k=msp,ne
         mdestod1=mdestod1+1
         it=ichar(ctps(mdestod1))+norb
         if(it.le.norb) it=it+256
C         if(yw)print*,'4it',it
         npv(k)=it
      enddo
      if(norb.gt.256) then
         do k=1,ne,4
            mdestod2561=mdestod2561+1
            iii=ichar(c256(mdestod2561))
            np1024(4)=iii/64
            np1024(3)=(iii-np1024(4)*64)/16
            np1024(1)=mod(iii,4)
            np1024(2)=mod((iii-np1024(1))/4,4)
c            print*,'mdestod2561,iii',mdestod2561,iii
c            print*,'np1024',np1024
            kkk=0
            do kk=k,min(k+3,ne)
               kkk=kkk+1
               npv(kk)=npv(kk)+np1024(kkk)*256
            enddo
         enddo
      endif
C      if(yw)print*,'venond2 itv,ipv',(ntv(k),k=1,ne),(npv(k),k=1,ne)
      return
 1    ierr=1
C      if(yw)print*,'venond2 lect fin fich',ndestod,ndestod1
      return
      end subroutine

       subroutine venond4(nems,ne,mst)
      implicit none
      logical(1) yw
      integer ne,mst,nems
      yw=.false.
c--------------------------------------------------
c     on a la formule nems=ne*(ne+1)/2+mst
c     on doit trouver ne et mst a partir de nems
c     on peut demontrer que :
c--------------------------------------------------
      ne=(  int(dsqrt(8.d0*nems))   -1)/2
C      if(yw)print*,'ne',ne
c     alors...
      mst=nems-ne*(ne+1)/2
C      if(yw)print*,'nems,ne,mst',nems,ne,mst
      return
      end subroutine
      end module
