! Copyright 2022, Francois BOUCHUT, Anne MANGENEY, Marc PERUZZETTO, Antoine LUCAS,  Jacques SAINTE-MARIE
! This program is distributed under the terms of the Lesser GPL v3
! This file is part of SHALTOP.


module read_files

  use parameters
    
  implicit double precision (a-h,o-z)

contains

  subroutine read_file(filename,array)
    character (len=300),intent(in) :: filename
    double precision, dimension(0:nx+1,0:ny+1), intent(out) :: array
    integer :: len_filename
    
    len_filename=lnblnk(filename)
    
    open (9999, file = filename(:len_filename))
    write(*,*) '***********'
    write(*,*) 'Reading ',filename(:len_filename)
    do j = 1, ny
       do i = 1, nx
          read (9999,*) array(i,j)
          !array(i,j)=max(0.,array(i,j))
       enddo
    enddo
    close(9999)
    write(*,*) 'Reading ',filename(:len_filename),' : OK'
    write(*,*) '***********'
    !Extend to outer bounds
    array(1,0) = array(1,1)
    array(1,ny+1) = array(1,ny)
    array(nx,0) = array(nx,1)
    array(nx,ny+1) = array(nx,ny)
    do i=0,nx+1
       array(i,0) = array(i,1)
       array(i,ny+1) = array(i,ny)
    enddo
    do j=0,ny+1
       array(0,j) = array(1,j)
       array(nx+1,j) = array(nx,j)
    enddo

  end subroutine read_file
    
end module read_files
