module mod_lirint
!
!=============================================================
!Read or write integral files (also simultaneously)
!
! 1. open files. ("old" if read, "unknown" if write)
!    (ifil1 to read, ifil2 to write)
! 2. call lirin4_init(ifil1,maxlong_modif) (read maxlong_modif)
!    call ecrin4_init(ifil2,maxlong_modif) (write maxlong_modif)
!
! 3. do
!       call lirin4(i,j,k,l,aa,ifil1)
!       call ecri4(i,j,k,l,aa,ifil2)
!    enddo
!
! 4. call ecri4_end(ifil2)
! 5. close files
!=============================================================
!
integer,save,private :: index_read_4,index_read_3
integer,save,private :: index_write_4,index_write_3
double precision,save,dimension(3,4096) :: aaloc_read_4
double precision,save,dimension(3,4096) :: aaloc_write_4
double precision,save,dimension(2,4096) :: aaloc_read_3
double precision,save,dimension(2,4096) :: aaloc_write_3
integer(2),dimension(4096),save,private :: iint,jnt,knt,lnt
contains
!**************************************************************
 subroutine lirin4_init(ifil,maxlong_modif)
IMPLICIT REAL(8)(A-H,O-X,Z),logical(1)(y)
index_read_4=4096
rewind ifil
read(ifil) maxlong_modif
end subroutine
!**************************************************************
 subroutine lirin4(i,j,k,l,aa,ifil)
IMPLICIT REAL(8)(A-H,O-X,Z),logical(1)(y)
dimension aa(3)
integer(2),dimension(4096),save :: iint,jnt,knt,lnt
character(80) file
!
! lecture d'une integrale a 4 indices differents
!
index_read_4=index_read_4+1
if(index_read_4>4096) then
   index_read_4=1
   read(ifil,end=1) iint,jnt,knt,lnt,aaloc_read_4
   goto 2
 1       continue
   inquire(ifil,name=file)
   call incr_name2(file,ifil)
   read(ifil) iint,jnt,knt,lnt,aaloc_read_4
endif
 2    continue
i=iint(index_read_4)
j=jnt(index_read_4)
k=knt(index_read_4)
l=lnt(index_read_4)
aa=aaloc_read_4(:,index_read_4)
return
end subroutine
!**************************************************************
!**************************************************************
 subroutine ecrin4_init(ifil,maxlong_modif)
IMPLICIT REAL(8)(A-H,O-X,Z),logical(1)(y)
index_write_4=0
rewind ifil
write(ifil) maxlong_modif
end subroutine
!**************************************************************
 subroutine ecrin4(i,j,k,l,aa,ifil)
IMPLICIT REAL(8)(A-H,O-X,Z),logical(1)(y)
dimension aa(3)
!
index_write_4=index_write_4+1
if(index_write_4>4096) then
   index_write_4=1
   write(ifil) iint,jnt,knt,lnt,aaloc_write_4
endif
iint(index_write_4)=i
jnt(index_write_4)=j
knt(index_write_4)=k
lnt(index_write_4)=l
aaloc_write_4(:,index_write_4)=aa
end subroutine
!**************************************************************
 subroutine ecrin4_end(ifil)
IMPLICIT REAL(8)(A-H,O-X,Z),logical(1)(y)
index_write_4=index_write_4+1
if(index_write_4>4096) then
   index_write_4=1
   write(ifil) iint,jnt,knt,lnt,aaloc_write_4
endif
iint(index_write_4)=0
write(ifil) iint,jnt,knt,lnt,aaloc_write_4
end subroutine
!**************************************************************
!**************************************************************
!**************************************************************
 subroutine lirin3_init(ifil)
index_read_3=4096
rewind ifil
end subroutine
!**************************************************************
 subroutine lirin3(i,j,k,aa,ifil)
IMPLICIT REAL(8)(A-H,O-X,Z),logical(1)(y)
dimension aa(2)
character(80) file
!
index_read_3=index_read_3+1
if(index_read_3>4096) then
   index_read_3=1
   read(ifil) iint,jnt,knt,aaloc_read_3
   goto 2
 1 continue
   inquire(ifil,name=file)
   call incr_name2(file,ifil)
   read(ifil) iint,jnt,knt,aaloc_read_3
endif
 2       continue
i=iint(index_read_3)
j=jnt(index_read_3)
k=knt(index_read_3)
aa=aaloc_read_3(:,index_read_3)
return
end subroutine
!**************************************************************
!**************************************************************
 subroutine ecrin3_init(ifil)
IMPLICIT REAL(8)(A-H,O-X,Z),logical(1)(y)
index_write_3=4096
rewind ifil
end subroutine
!**************************************************************
 subroutine ecrin3(i,j,k,aa,ifil)
IMPLICIT REAL(8)(A-H,O-X,Z),logical(1)(y)
dimension aa(2)
!
index_write_3=index_write_3+1
if(index_write_3>4096) then
   index_write_3=1
   write(ifil) iint,jnt,knt,aaloc_write_3
endif
iint(index_write_3)=i
jnt(index_write_3)=j
knt(index_write_3)=k
aaloc_write_3(:,index_write_3)=aa
end subroutine
!**************************************************************
 subroutine ecrin3_end(ifil)
IMPLICIT REAL(8)(A-H,O-X,Z),logical(1)(y)
index_write_3=index_write_3+1
if(index_write_3>4096) then
   index_write_3=1
   write(ifil) iint,jnt,knt,aaloc_write_3
endif
iint(index_write_3)=0
write(ifil) iint,jnt,knt,aaloc_write_3
end subroutine
!**************************************************************
end module mod_lirint
      
      
