subroutine write_esri_ascii_file(filename_ascii_sub, ncols_sub, nrows_sub, cellsize_sub, val_array, x_array,y_array)
character(*) :: filename_ascii_sub
integer :: ncols_sub, nrows_sub
real :: cellsize_sub
real :: val_array(ncols_sub,nrows_sub)
real :: x_array(ncols_sub,nrows_sub)
real :: y_array(ncols_sub,nrows_sub)
! Local variables
integer :: ii,jj
real :: xllcorner
real :: yllcorner
real :: NODATA_value = -999.0
integer :: unit_in = 20
character(len=:), allocatable :: fmt
xllcorner = x_array(1,1) - cellsize_sub/2.0
yllcorner = y_array(1,1) - cellsize_sub/2.0
open(unit_in, file=filename_ascii_sub, access='sequential', form='formatted', status='unknown')
write(unit_in,*) 'ncols', ncols_sub
write(unit_in,*) 'nrows', nrows_sub
write(unit_in,*) 'xllcorner', xllcorner
write(unit_in,*) 'yllcorner', yllcorner
write(unit_in,*) 'cellsize', cellsize_sub
write(unit_in,*) 'NODATA_value', NODATA_value
do jj = nrows_sub, 1, -1
write(fmt, '(A,I0,A)') '(', ncols_sub, 'es12.3)'
write(unit_in,fmt) (val_array(ii,jj), ii = 1, ncols_sub)
end do
close(unit_in)
end subroutine write_esri_ascii_file