      subroutine disin(icas,ifil,fil,ycl1,i,k,j,l,a,nb_ijcl)
      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
      logical(1) ycl1
!
!---------------------------------------------------------
!     ecriture des integrales au cours de leur calcul
!     pour le programme CASDI
!---------------------------------------------------------
!
      yw=.false.
      if(icas.eq.0) then
         ! Ouverture du fichier d'ecriture, mises a zero
         rewind ifil
         ii=0
         if(ycl1) then
            form='formatted'
         else
            form='unformatted'
            call incrijcl0
         endif
         open(ifil,file=fil,form=form,status='unknown')
         print*,'disin ',trim(fil)
         call titre(' Ouverture '//trim(fil)//'#',40)
         n_ijcl=fil
         return
      else if(icas.eq.2) then
         ! Generation de la fin du fichier, fermeture
         if(ycl1) then
            write(ifil,'(4i4,d20.12)') 0,0,0,0,a
            return
         endif
         ii=ii+1
         if(ii>4096) then
            ii=1
            write(ifil) iint,jnt,knt,lnt,aa
            call incrijcl(nb_ijcl,ifil)
         endif
         iint(ii)=0
         jnt(ii)=0
         knt(ii)=0
         lnt(ii)=0
         write(ifil) iint,jnt,knt,lnt,aa
!            print*,'ECRIT FIN'
!            do m=1,4096
!               print'(4i4,f10.6)',iint(m),jnt(m),knt(m),lnt(m),aa(m)
!            enddo
         return
      endif
      if(ycl1) then
         write(ifil,'(4i4,d20.12)') i,j,k,l,a
         return
      endif
      ii=ii+1
      if(ii>4096) then
         ii=1
         write(ifil) iint,jnt,knt,lnt,aa
         if(yw)print*,'nb_ijcl',nb_ijcl
         call incrijcl(nb_ijcl,ifil)
      endif
      iint(ii)=i
      jnt(ii)=j
      knt(ii)=k
      lnt(ii)=l
      aa(ii)=a
      return
      end
       subroutine incrijcl0
      use mod_disin
      ll_ijcl=1
      nrec_ijcl=1
      return
      end
       subroutine incrijcl(nb_ijcl,ifil)
      use mod_disin
      IMPLICIT REAL*8(A-H,O-X,Z),logical*1(y)
      character(2) c
      yw=.false.
      ll_ijcl=ll_ijcl+1
      if(yw)print*,ll_ijcl,nb_ijcl,trim(n_ijcl)
      if(ll_ijcl.gt.nb_ijcl) then
         ll_ijcl=1
         close(ifil)
         long=len_trim(n_ijcl)
         if(yw)print*,'LONG',long
         if(yw)print*,'file   ',n_ijcl(1:long)
         itom=0
         do k=long,1,-1
            if(n_ijcl(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_ijcl(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_ijcl(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 ijcl 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_ijcl(1:long)//'_'//trim(c)
                 open(ifil,file=trim(n_ijcl)//'_'//trim(c),
     *           form='unformatted',
     *      status='unknown')
            n_ijcl=n_ijcl(1:long)//'_'//trim(c)
         else
                 if(yw)print*,'open72  ',n_ijcl(1:k-1)//'_'//trim(c)
                 open(ifil,file=n_ijcl(1:k-1)//'_'//trim(c),
     *           form='unformatted',
     *      status='unknown')
            n_ijcl=n_ijcl(1:k-1)//'_'//trim(c)
         endif
         nrec_ijcl=nrec_ijcl+1
      endif
      return
      end
