      subroutine disinl(ifil,icas,fil,jint,i,j,k,l,a)
      use module
      use mod_disin
      IMPLICIT REAL(8)(A-H,O-X,Z),logical(1)(y)
      save ii
      integer(2),dimension(4096),save :: iint,jnt,knt,lnt
      double precision a
      character(*) fil
      character(11) form
!
!---------------------------------------------------------
!     ecriture des integrales au cours de leur calcul
!---------------------------------------------------------
!
      yw=.false.
      if(icas.eq.0) then
         ! Ouverture du fichier d'ecriture, mises a zero
         rewind ifil
         ii=0
         form='unformatted'
         call incrijcloc0
         open(ifil,file=trim(fil),form=form,status='unknown')
         call titre(' Ouverture '//trim(fil)//'#',40)
         n_ijcloc=fil
         return
      else if(icas.eq.2) then
         ! Generation de la fin du fichier, fermeture
         ii=ii+1
         if(ii>4096) then
            ii=1
            if(ifil==31) then
               write(ifil) iint,jnt,knt,lnt,aaloc
            else
               write(ifil) iint,jnt,knt,aaloc
            endif
            call incrijcloc(nb_ijcloc,ifil)
         endif
         iint(ii)=0
         jnt(ii)=0
         knt(ii)=0
         if(ifil==31) then
            lnt(ii)=0
            write(ifil) iint,jnt,knt,lnt,aaloc
!            do k=1,4096
!            print'(''int'',4i4,3f12.8)',iint(k),jnt(k),knt(k),lnt(k)
!     *      ,aaloc(:,k)
!            enddo
         else
            write(ifil) iint,jnt,knt,aaloc
         endif
!            print*,'ECRIT FIN'
!            do m=1,4096
!               print'(4i4,f10.6)',iint(m),jnt(m),knt(m),lnt(m),aa(m)
!            enddo
         return
      endif
      ! icas=1 stockage des integrales calculees
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! cas a 4 indices (ifil==30)
! on ecrit 4 indices i>j>k>l et 3 valeurs d'integrales aaloc(1:3)
! EN NOTATION PHYSICIENNE :
! aaloc(1) : ijkl      aaloc(2) : ijlk        aaloc(3) : ikjl
!
! cas a 3 indices (ifil==31)
! on ecrit 3 indices i,j>k et 2 valeurs d'integrales aaloc(1:3)
! EN NOTATION CHIMIQUE :
! aaloc(1) : iikl      aaloc(2) : ijil
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
      if(jint==1) ii=ii+1
      if(ii>4096) then
         ii=1
         if(ifil==31) then
            write(ifil) iint,jnt,knt,lnt,aaloc
!            do k=1,4096
!            print'(''int'',4i4,3f12.8)',iint(k),jnt(k),knt(k),lnt(k)
!     *      ,aaloc(:,k)
!            enddo
         else
            write(ifil) iint,jnt,knt,aaloc
         endif
         if(yw)print*,'nb_ijcloc',nb_ijcloc
         call incrijcloc(nb_ijcloc,ifil)
      endif
      if(jint==1) then
         iint(ii)=i
         jnt(ii)=j
         knt(ii)=k
         if(ifil==31)lnt(ii)=l
      endif
      aaloc(jint,ii)=a
      return
      end
       subroutine incrijcloc0
      use mod_disin
      ll_ijcloc=1
      nrec_ijcloc=1
      return
      end
       subroutine incrijcloc(nb_ijcloc,ifil)
      use mod_disin
      IMPLICIT REAL*8(A-H,O-X,Z),logical*1(y)
      character(2) c
      yw=.false.
      ll_ijcloc=ll_ijcloc+1
      if(yw)print*,ll_ijcloc,nb_ijcloc,trim(n_ijcloc)
      if(ll_ijcloc.gt.nb_ijcloc) then
         print*,'ouverture fermeture ijclloc4',ll_ijcloc,nb_ijcloc
         call flush(6)
         ll_ijcloc=1
         close(ifil)
         long=len_trim(n_ijcloc)
         if(yw)print*,'LONG',long
         if(yw)print*,'file   ',n_ijcloc(1:long)
         itom=0
         do k=long,1,-1
            if(n_ijcloc(k:k).eq.'_') then
                    if(yw)print*,'long,k',long,k,long-2
               if(k.eq.long-1) then
                       if(yw)print*,'coucou1'
                  read(n_ijcloc(k+1:long),'(i1)',err=1) itom
                  if(yw)print*,'Itom',itom
               else if(k.eq.long-2) then
                       if(yw)print*,'coucou2'
                  read(n_ijcloc(k+1:long),'(i2)',err=1) itom
               else
                       if(yw)print*,'coucou3'
                  goto 1
               endif
 1             continue
               exit
            endif
         enddo
         itom=itom+1
         if(yw)print*,'itom',itom
         if(itom.eq.100) stop 'too much ijcloc files'
         c='  '
         if(itom.lt.10) then
            write(c(1:1),'(i1)') itom
         else
            write(c(1:2),'(i2)') itom
         endif
         if(itom.eq.1) then
            if(yw)print*,'longc',len_trim(c)
            if(yw)print*,'open72  ',n_ijcloc(1:long)//'_'//trim(c)
            open(ifil,file=trim(n_ijcloc)//'_'//trim(c),
     *      form='unformatted',
     *      status='unknown')
            n_ijcloc=n_ijcloc(1:long)//'_'//trim(c)
         else
            if(yw)print*,'open72  ',n_ijcloc(1:k-1)//'_'//trim(c)
            open(ifil,file=n_ijcloc(1:k-1)//'_'//trim(c),
     *      form='unformatted',
     *      status='unknown')
            n_ijcloc=n_ijcloc(1:k-1)//'_'//trim(c)
         endif
         nrec_ijcloc=nrec_ijcloc+1
      endif
      return
      end
