      subroutine lirin0(i,j,k,l,a)
!
!-------------------------------------------------------------
! Lecture des integrales  (itest(63)==0)
! i,j,k,l : ordre de la notation "chimiste"
!-------------------------------------------------------------
!
      IMPLICIT REAL*8(A-H,O-x,z),logical*1(y)
      parameter(maxda3=300)
      parameter(maxsym=8,maxorbcas=16)
      include 'maxorb.prm'
      character*4 hab
      common /orb/ enuc,eref,norb,noca,nocb
     *,iorb(2*maxorb),ninac,nvirt,noca0
      common/infdav/ twcas,alam(maxda3),tau,twvec,twess,sint,sint4,sh
     *,hab,Nbtots,nvec,ngao,niter,iprec,lvec(maxda3),nfrag,ipresc,irest
     *,jrest
     *,iunit,ihprec,ihvec,ifiess,NESSAI,nf8,maxda2,bidia,ywvec,ytana
     *,yhmono,ydens,ywcas
      logical*1 bidia
      common/opt/ihab,calcul
      character*3 calcul
      integer*2 iint,jnt,knt,lnt
      common/wcl/ aaint(4096),iint(4096),jnt(4096),knt(4096),lnt(4096)
      common/infodp/ydp
      integer(8) ilimi,ilimi4,nintot,nintot4,nint
      common/l0/ilimi,ilimi4,nintot,nintot4,nint,ycl
      real*4 aaints(4096)
      common/n_fil/ ijcl1,nb_ijcl,l_ijcl,nrec_ijcl
      character*80 ijcl1,ijcl2
      character*3 c
      common/test/itest(100)
      equivalence (aaint(1),aaints(1))
      yw=.false.
      if(itest(63).ge.1) then
         call lirin63(i,j,k,l,a)
         return
      endif
      c='_  '
 1    if(ycl) then
         read(26,*)i,j,k,l,a
      else
         nint=nint+1
         if(nint.gt.4096) then
            l_ijcl=l_ijcl+1
c            print*,'l_ijcl,nb_ijcl',l_ijcl,nb_ijcl
            if(l_ijcl.gt.nb_ijcl) then
               l_ijcl=1
               nrec_ijcl=nrec_ijcl+1
               if(nrec_ijcl.le.9) then
                  write(c(2:2),'(i1)') nrec_ijcl
               else
                  write(c(2:3),'(i2)') nrec_ijcl
               endif
               close(26)
               call colle(ijcl1,c,ijcl2,ibid)
               open(26,file=ijcl2,form='unformatted',status='old')
               print*,'close file ijcl'
               print*,'open new ijcl file :',ijcl2
            endif
            read(26) iint,jnt,knt,lnt,aaint
            nint=1
         endif
         i=iint(nint)
         j=jnt(nint)
         k=knt(nint)
         l=lnt(nint)
            a=aaint(nint)
      endif
      if(dabs(a).lt.sint) then
         if(k.ne.0) then
            ilimi=ilimi+1
            a=0.d0
         endif
      endif
      if(i/=j.and.i/=k.and.i/=l.and.j/=k.and.j/=l.and.k/=l) then
         if(dabs(a).lt.sint4) then
            ilimi4=ilimi4+1
            a=0.d0
         else
            nintot4=nintot4+1
         endif
      endif
      return
      end
       subroutine liri00
!
!-------------------------------------------------------------
! Initialisation de la lecture des integrales  (itest(63)==0)
!-------------------------------------------------------------
!
      IMPLICIT REAL*8(A-H,O-x,z),logical*1(y)
      parameter(maxsym=8,maxorbcas=16)
      include 'maxorb.prm'
      integer(8) ilimi,ilimi4,nintot,nintot4,nint
      common/l0/ilimi,ilimi4,nintot,nintot4,nint,ycl
      common/infodp/ydp
      common /orb/ enuc,eref,norb,noca,nocb
     *,iorb(2*maxorb),ninac,nvirt,noca0
      integer*2 iint,jnt,knt,lnt
      common/wcl/ aaint(4096),iint(4096),jnt(4096),knt(4096),lnt(4096)
      real*4 aaints(4096)
      equivalence (aaint(1),aaints(1))
      common/n_fil/ ijcl1,nb_ijcl,l_ijcl,nrec_ijcl
      character*80 ijcl1
      common/test/itest(100)
      yw=.false.
c      write(6,*)'entree liri00'
      if(itest(63).ge.1) then
         call liri00363(4)
         return
      endif
      close(26)
      if(ycl) then
         open(26,file=ijcl1,form='formatted',status='old')
      else
         open(26,file=ijcl1,form='unformatted',status='old')
      endif
 
      rewind 26
      if(ycl) return
         read(26) iint,jnt,knt,lnt,aaint
      nint=0
      l_ijcl=1
      nrec_ijcl=0
      return
      end
       subroutine lirin3063(i,j,k,a)
!
!-------------------------------------------------------------
! Lecture des integrales a 3 indices  (itest(63)==1)
!-------------------------------------------------------------
!
      IMPLICIT REAL*8(A-H,O-x,z),logical*1(y)
      parameter(maxda3=300)
      parameter(maxsym=8,maxorbcas=16)
      include 'maxorb.prm'
      character*4 hab
      common /orb/ enuc,eref,norb,noca,nocb
     *,iorb(2*maxorb),ninac,nvirt,noca0
      common/infdav/ twcas,alam(maxda3),tau,twvec,twess,sint,sint4,sh
     *,hab,Nbtots,nvec,ngao,niter,iprec,lvec(maxda3),nfrag,ipresc,irest
     *,jrest
     *,iunit,ihprec,ihvec,ifiess,NESSAI,nf8,maxda2,bidia,ywvec,ytana
     *,yhmono,ydens,ywcas
      logical*1 bidia
      common/opt/ihab,calcul
      character*3 calcul
      integer*2 iint,jnt,knt,lnt
      common/wcl/ aaint(4096),iint(4096),jnt(4096),knt(4096),lnt(4096)
      common/wcl63/ aaint63(2,4096)
      common/infodp/ydp
      integer(8) ilimi,ilimi4,nintot,nintot4,nint
      common/l0/ilimi,ilimi4,nintot,nintot4,nint,ycl
      common/n_fil/ ijcl1,nb_ijcl,l_ijcl,nrec_ijcl
      character*80 ijcl1
      dimension a(2)
      character*3 c
      yw=.false.
      c='_  '
      nint=nint+1
      if(nint.gt.4096) then
         read(98) iint,jnt,knt,aaint63
         nint=1
      endif
      i=iint(nint)
      j=jnt(nint)
      k=knt(nint)
      a(1)=aaint63(1,nint)
      a(2)=aaint63(2,nint)
      if(dabs(a(1)).lt.sint) then
         ilimi=ilimi+1
         a(1)=0.d0
      endif
      if(dabs(a(2)).lt.sint) then
         ilimi=ilimi+1
         a(2)=0.d0
      endif
      return
      end
       subroutine liri00363(n)
!
!-------------------------------------------------------------
! Initialisation de la lecture des integrales a n indices
!  (itest(63)==1)
!-------------------------------------------------------------
!
      IMPLICIT REAL*8(A-H,O-x,z),logical*1(y)
      parameter(maxsym=8,maxorbcas=16)
      include 'maxorb.prm'
      integer(8) ilimi,ilimi4,nintot,nintot4,nint
      common/l0/ilimi,ilimi4,nintot,nintot4,nint,ycl
      common/infodp/ydp
      common /orb/ enuc,eref,norb,noca,nocb
     *,iorb(2*maxorb),ninac,nvirt,noca0
      integer*2 iint,jnt,knt,lnt
      common/wcl/ aaint(4096),iint(4096),jnt(4096),knt(4096),lnt(4096)
      common/wcl63/ aaint63(2,4096)
      common/wcl63_4/ aaint63_4(3,4096),m,nb_ind,
     *nint4_1_1,nint4_1_2,nint4_2_1,nint4_2_2,nint4_3,nint4_4
      common/n_fil/ ijcl1,nb_ijcl,l_ijcl,nrec_ijcl
      common/fil63/ ijcl21,ijcl31,ijcl41
      character*80 ijcl1,ijcl21,ijcl31,ijcl41
      yw=.false.
c      write(6,*)'entree liri00'
      if(n.eq.3) then
         close(98)
         open(98,file=ijcl31,form='unformatted',status='old')
         read(98) iint,jnt,knt,aaint63
         nint=0
         nrec_ijcl=0
      else if(n.eq.4) then
         close(98)
         open(98,file=ijcl41,form='unformatted',status='old')
         read(98) maxlong_98
         if(yw)print*,'maxlong_98',maxlong_98
         close(98)
         open(98,file=ijcl41,form='unformatted',status='old')
         call filesplit('OPEN',98,maxlong_98,idum,idum2)
         call filesplit('READ',98,4,ichange,idum2)
         read(98) maxlong
         call filesplit('READ',98,131072,ichange,idum2)
         read(98) iint,jnt,knt,lnt,aaint63_4
!         do k=1,20
!            write(75,*)iint(k),jnt(k),knt(k),lnt(k),aaint63_4(:,k),'$'
!            call scril
!         enddo
         nint4_1_1=1
         nint4_1_2=0
         nint4_2_1=1
         nint4_2_2=0
         nint4_3=0
         nint4_4=0
         m=1
         nb_ind=4
      endif
      return
      end
 
       subroutine lirin63(i,j,k,l,a)
!
!-------------------------------------------------------------
! Lecture des integrales
!  (itest(63)==1)
! Toutes les integrales sont lues :
! d'abord celles a 4 indices, puis 3, puis 2, puis 1
!-------------------------------------------------------------
!
      IMPLICIT REAL*8(A-H,O-x,z),logical*1(y)
      parameter(maxda3=300)
      parameter(maxsym=8,maxorbcas=16)
      include 'maxorb.prm'
      integer(8) ilimi,ilimi4,nintot,nintot4,nint
      common/l0/ilimi,ilimi4,nintot,nintot4,nint,ycl
      integer*2 iint,jnt,knt,lnt
      common/wcl/ aaint(4096),iint(4096),jnt(4096),knt(4096),lnt(4096)
      common/wcl63/ aaint63(2,4096)
      common/wcl63_4/ aaint63_4(3,4096),m,nb_ind,
     *nint4_1_1,nint4_1_2,nint4_2_1,nint4_2_2,nint4_3,nint4_4
      dimension a_jk_ijjj(maxorb,maxorb)
      common/fil63/ ijcl21,ijcl31,ijcl41
      character*80  ijcl21,ijcl31,ijcl41
      common /orb/ enuc,eref,norb,noca,nocb
     *,iorb(2*maxorb),ninac,nvirt,noca0
      common/sym/ itsym(maxorb*2),its(maxsym,maxsym),nsym
      common/infdav/ twcas,alam(maxda3),tau,twvec,twess,sint,sint4,sh
     *,hab,Nbtots,nvec,ngao,niter,iprec,lvec(maxda3),nfrag,ipresc,irest
     *,jrest
     *,iunit,ihprec,ihvec,ifiess,NESSAI,nf8,maxda2,bidia,ywvec,ytana
     *,yhmono,ydens,ywcas
      common/test/itest(100)
      yw=.false.
 
      if(nb_ind.eq.4) then
         if(m.eq.1) then
         nint4_4=nint4_4+1
            if(nint4_4.gt.4096) then
               call filesplit('READ',98,131072,ichange,idum2)
               read(98) iint,jnt,knt,lnt,aaint63_4
               nint4_4=1
            endif
         endif
         !print*,'nint4_4',nint4_4,iint(nint4_4),jnt(nint4_4)
!     *   ,knt(nint4_4),lnt(nint4_4)
         if(m.eq.1) then
            i=iint(nint4_4)
            j=knt(nint4_4)
            k=jnt(nint4_4)
            l=lnt(nint4_4)
         else if(m.eq.2) then
            i=iint(nint4_4)
            j=lnt(nint4_4)
            k=jnt(nint4_4)
            l=knt(nint4_4)
         else if(m.eq.3) then
            i=iint(nint4_4)
            j=jnt(nint4_4)
            k=knt(nint4_4)
            l=lnt(nint4_4)
         endif
         !print*,'i,j,k,l',i,j,k,l
         if(k.eq.0) then
            nb_ind=3
            m=1
            close(98)
            open(98,file=ijcl31,form='unformatted',status='old')
            nint4_3=4096
            goto 3
         endif
         a=aaint63_4(m,nint4_4)
         if(dabs(a).lt.sint) then
            if(k.ne.0) then
               ilimi=ilimi+1
            !print*,'ilimi',ilimi
               a=0.d0
            endif
         endif
         if(dabs(a).lt.sint4) then
            ilimi4=ilimi4+1
            a=0.d0
         else
            nintot4=nintot4+1
         endif
         m=m+1
         if(m.eq.4) m=1
         return
      endif
 3    continue
      if(yw)print*,'coucou3'
      if(nb_ind.eq.3) then
         if(m.eq.1) then
            nint4_3=nint4_3+1
            if(nint4_3.gt.4096) then
               read(98) iint,jnt,knt,aaint63
               nint4_3=1
            endif
         endif
!         print'(a,4i4,2f12.8)'
!     *   ,'nint4_3',nint4_3,iint(nint4_3),jnt(nint4_3)
!     *   ,knt(nint4_3),aaint63(1:2,nint4_3)
         if(m.eq.1) then
            k=iint(nint4_3)
            l=iint(nint4_3)
            i=jnt(nint4_3)
            j=knt(nint4_3)
            if(i<j) call ech(i,j)
C            if(i<k) then
C               call ech(i,k)
C               call ech(j,l)
C            endif
         else if(m.eq.2) then
            i=iint(nint4_3)
            k=iint(nint4_3)
            j=jnt(nint4_3)
            l=knt(nint4_3)
        !print*,'dans lirin0',i,j,k,l
C            if(i<j) call ech(i,j)
C            if(k<l) call ech(k,l)
C            if(i<k) then
C               call ech(i,k)
C               call ech(j,l)
C            endif
         endif
         !print*,'i,j,k,l',i,j,k,l
         if(k.eq.0) then
            m=1
            close(98)
            open(98,file=ijcl21,form='unformatted',status='old')
            read(98) ((a_jk_ijjj(i,j),j=1,norb),i=1,norb)
            nb_ind=2
            goto 2
         endif
         a=aaint63(m,nint4_3)
         if(dabs(a).lt.sint) then
            if(k.ne.0) then
               ilimi=ilimi+1
            !print*,'2ilimi',ilimi
               a=0.d0
            endif
         endif
         m=m+1
         if(m.eq.3) m=1
         return
      endif
 2    continue
      if(yw)print*,'coucou2',norb,m
      if(nb_ind.eq.2) then
         if(m.eq.1) then
            nint4_2_2=nint4_2_2+1
C            !if(nint4_2_2.eq.nint4_2_1) nint4_2_2=nint4_2_2+1
C            if(m.eq.2.and.nint4_2_2.eq.nint4_2_1) then
C               nint4_2_2=nint4_2_2+1
C               m=1
C            endif
            if(nint4_2_2.eq.norb+1) then
               nb_ind=1
               read(98) ((a_jk_ijjj(i,j),j=1,norb),i=1,norb)
               goto 1
            endif
            if(nint4_2_2.gt.nint4_2_1) then
               nint4_2_1=nint4_2_1+1
               nint4_2_2=1
            endif
         endif
         if(m.eq.1) then
            k=min(nint4_2_1,nint4_2_2)
            l=min(nint4_2_1,nint4_2_2)
            i=max(nint4_2_1,nint4_2_2)
            j=max(nint4_2_1,nint4_2_2)
            a=a_jk_ijjj(nint4_2_1,nint4_2_2)
         else if(m.eq.2) then
            j=min(nint4_2_1,nint4_2_2)
            l=min(nint4_2_1,nint4_2_2)
            i=max(nint4_2_1,nint4_2_2)
            k=max(nint4_2_1,nint4_2_2)
            a=a_jk_ijjj(nint4_2_2,nint4_2_1)
         endif
         if(dabs(a).lt.sint) then
            if(k.ne.0) then
               ilimi=ilimi+1
            !print*,'3ilimi',ilimi
               a=0.d0
            endif
         endif
         m=m+1
         if(nint4_2_2.eq.nint4_2_1) m=1
         if(m.eq.3) m=1
         if(yw)print*,'nint4_2_1,nint4_2_2',nint4_2_1,nint4_2_2
         return
      endif
 1    continue
      if(yw)print*,'coucou1',norb
      if(nb_ind.eq.1) then
         nint4_1_2=nint4_1_2+1
         if(nint4_1_2.eq.nint4_1_1) nint4_1_2=nint4_1_2+1
         if(nint4_1_2.gt.norb) then
            nint4_1_1=nint4_1_1+1
            if(nint4_1_1.eq.norb+1) then
               i=0
               j=0
               k=0
               l=0
               return
            endif
            nint4_1_2=1
         endif
         if(itsym(nint4_1_1).ne.itsym(nint4_1_2)) goto 1
         if(m.eq.1) then
            if(nint4_1_2>nint4_1_1) then
               i=nint4_1_2
               j=nint4_1_1
               k=nint4_1_1
               l=nint4_1_1
            else
               i=nint4_1_1
               j=nint4_1_1
               k=nint4_1_1
               l=nint4_1_2
            endif
         endif
         a=a_jk_ijjj(nint4_1_1,nint4_1_2)
         if(dabs(a).lt.sint) then
            if(k.ne.0) then
               ilimi=ilimi+1
            !print*,'4ilimi',ilimi
               a=0.d0
            endif
         endif
         return
      endif
      return
      end
 
 
