program test
! Demonstration of namelist I/O.    Written by Roland Stull, 9 Jan 2013.

  implicit none                     !enforce strong typing
  integer :: err, ero               !error flags for file reading and opening
  character(len=30) :: line         !one line of the input (a dummy variable)
  real :: dx, dy, dz, dt            !grid increments (m) and timestep (s)
  namelist / modelsettings / dx, dy, dz, dt 

  write(*,*)
  write (*,*) "Test of namelists"
  
! Set default  values 
  dz = 2.0           !layer depth (m)
  dt = 0.5           !time step increment (s)
  dy = 200.0         !north-south grid spacing (m)
  dx = 300.0         !east-west grid spacing (m)

! Display the default parameters in the namelist
  write(*,*)
  write(*,*) "Default namelist variables are:"
  write(*,nml=modelsettings)    ! display the default namelist

!======= Example of reading all namelist variables, but in a different order =======
! Get new values from an input file
  write(*,*)
  write(*,*) "Read the first file: inputfile.txt"
  open(1,file="inputfile.txt",status="old", action="read", iostat=ero)
  
! Show each line in the file, before reading it as a namelist
  write(*,*)
  write(*,*) "========== read all variables ======="
  write(*,*) "This is what the input file looks like:"
  do
    line = ""                       !initialize to an empty line
    read(1,"(a)",iostat=err) line
    if(err .ne. 0) exit
    write(*,*) trim(line)
  enddo  
  rewind(1)                         !reset the file back to the beginning

! Now, read the file as a namelist  
  read(1,nml=modelsettings, iostat=err)
  if (ero .ne. 0) then
    write(*,*) "   Sorry.  Cannot open the file.  Will use default values instead."
  else 
   if (err .ne. 0) write(*,*) "   Sorry, cannot read the  namelist.  Will use default values instead."
  endif
  
  write(*,*)
  write(*,*) "New namelist variables are:"
  write(*,nml=modelsettings)    ! display the new values namelist

   close(1)

!======= Example of reading only a subset of the namelist variables =======
! Get new values from an input file
  open(1,file="inputfile2.txt",status="old", action="read", iostat=ero)
  
  write(*,*)
  write(*,*) "=========== Read the second input file ========="
  write(*,*) "Read the second file: inputfile2.txt   ..."
  
! Show each line in the file, before reading it as a namelist
  write(*,*)
  write(*,*) "... which has only a subset of namelist variables"
  write(*,*) "This is what the input file looks like:"
  do
    line = ""                       !initialize to an empty line
    read(1,"(a)",iostat=err) line
    if(err .ne. 0) exit
    write(*,*) trim(line)
  enddo  
  rewind(1)                         !reset the file back to the beginning

! Now, read the file as a namelist  
  read(1,nml=modelsettings, iostat=err)
  if (ero .ne. 0) then
    write(*,*) "   Sorry.  Cannot open the file.  Will use default values instead."
  else 
   if (err .ne. 0) write(*,*) "   Sorry, cannot read the  namelist.  Will use default values instead."
  endif
  
  write(*,*)
  write(*,*) "New namelist variables are:"
  write(*,nml=modelsettings)    ! display the new values namelist
  
! Regardless of whether you read variables as a namelist, they are still normal variables.
  write(*,*)
  write(*,*) "====== Output of the variables, but NOT as a namelist ======="
  write(*,*) "dx, dy, dz (m) =", dx, dy, dz, "   and dt (s) =", dt
  
  close(1)
  
  write(*,*)
  write(*,*) "Bye."
  write(*,*)  

end program test
  