From bf7a3ba18f8b5b0ee9b9afcbc4594e0abffa5b82 Mon Sep 17 00:00:00 2001
From: Lionel GUEZ <guez@lmd.ens.fr>
Date: Wed, 16 Jun 2021 18:17:04 +0200
Subject: [PATCH] Read grid data from `grid_nml.txt`

Instead of asking the user to define it. So ne no longer need the file
`read_snapshot_nml.txt` for programs `test_read_snapshot` and
`test_send_recv`: we can just supply the remaining data in the `input`
key of tests `Read_snapshot` and `Send_recv`.
---
 .../Input/Region_4/read_snapshot_nml.txt      |  5 ---
 Overlap/Tests/short_tests.json                |  4 +--
 Overlap/Tests/test_read_snapshot.f90          | 29 +++++++++-------
 Overlap/Tests/test_send_recv.f90              | 33 +++++++++++--------
 4 files changed, 40 insertions(+), 31 deletions(-)
 delete mode 100644 Overlap/Tests/Input/Region_4/read_snapshot_nml.txt

diff --git a/Overlap/Tests/Input/Region_4/read_snapshot_nml.txt b/Overlap/Tests/Input/Region_4/read_snapshot_nml.txt
deleted file mode 100644
index f4f7c4fe..00000000
--- a/Overlap/Tests/Input/Region_4/read_snapshot_nml.txt
+++ /dev/null
@@ -1,5 +0,0 @@
- &MAIN_NML
- CORNER  = 16.12500       , -38.87500      ,
- NLON    = 20,
- NLAT    = 20
- /
diff --git a/Overlap/Tests/short_tests.json b/Overlap/Tests/short_tests.json
index d7835a6a..3efdcdaa 100644
--- a/Overlap/Tests/short_tests.json
+++ b/Overlap/Tests/short_tests.json
@@ -41,7 +41,7 @@
         ]
     },
     {
-        "stdin_filename": "$src_dir/Overlap/Tests/Input/Region_4/read_snapshot_nml.txt",
+        "input": "&main_nml /\n",
         "title": "Read_snapshot",
         "stdout": "test_read_snapshot_stdout.txt",
         "commands": [
@@ -351,7 +351,7 @@
         ]
     },
     {
-        "stdin_filename": "$src_dir/Overlap/Tests/Input/Region_4/read_snapshot_nml.txt",
+        "input": "&main_nml /\n",
         "description": "Send and receive an eddy.",
         "title": "Send_recv",
         "stdout": "test_send_recv_stdout.txt",
diff --git a/Overlap/Tests/test_read_snapshot.f90 b/Overlap/Tests/test_read_snapshot.f90
index 05b6e7b1..7a42e69c 100644
--- a/Overlap/Tests/test_read_snapshot.f90
+++ b/Overlap/Tests/test_read_snapshot.f90
@@ -3,7 +3,7 @@ program test_read_snapshot
   use, intrinsic:: ISO_FORTRAN_ENV
 
   ! Libraries:
-  use jumble, only: get_command_arg_dyn
+  use jumble, only: get_command_arg_dyn, new_unit
   use mpi_f08, only: mpi_init, mpi_finalize
   use nr_util, only: deg_to_rad, assert
   use shapelib_03, only: dbf_read_attribute_03
@@ -19,13 +19,13 @@ program test_read_snapshot
   character(len = :), allocatable:: shp_tr_dir
   type(snapshot) s
   TYPE(shp_tr) hshp
-  integer k1, copy
+  integer k1, copy, unit
 
-  real:: corner(2) = [0.125, - 59.875]
+  real:: corner_deg(2) = [0.125, - 59.875]
   ! longitude and latitude of the corner of the whole grid in input
   ! NetCDF, in degrees
 
-  real:: step(2) = [0.25, 0.25] ! longitude and latitude steps, in degrees
+  real:: step_deg(2) = [0.25, 0.25] ! longitude and latitude steps, in degrees
   logical periodic ! grid is periodic in longitude
 
   integer:: nlon = 120, nlat = 120
@@ -36,7 +36,8 @@ program test_read_snapshot
   ! We look for an overlapping eddy at dist_lim (in grid points) of
   ! the first extremum.
 
-  namelist /main_nml/ corner, step, nlon, nlat, dist_lim
+  namelist /main_nml/ dist_lim
+  namelist /grid_nml/ corner_deg, step_deg, nlon, nlat
 
   !-------------------------------------------------------------------------
 
@@ -49,21 +50,27 @@ program test_read_snapshot
   read(unit = *, nml = main_nml)
   write(unit = *, nml = main_nml)
 
+  call new_unit(unit)
+  open(unit, file = shp_tr_dir // "/grid_nml.txt", status = "old", &
+       action = "read", position  = "rewind")
+  read(unit, nml = grid_nml)
+  close(unit)
+
   ! As we are requiring the grid spacing to be uniform, the value of
-  ! "periodic" may be deduced from the values of step(1) and nlon:
-  periodic = nint(360. / step(1)) == nlon
+  ! "periodic" may be deduced from the values of step_deg(1) and nlon:
+  periodic = nint(360. / step_deg(1)) == nlon
   print *, "periodic = ", periodic
-  if (periodic) call assert(2 * dist_lim * step(1) < 180., &
+  if (periodic) call assert(2 * dist_lim * step_deg(1) < 180., &
        "test_read_snapshot dist_lim")
   copy = merge(dist_lim, 0, periodic)
 
   call shp_tr_open(hshp, trim(shp_tr_dir), rank = 0)
   call dbf_read_attribute_03(k1, hshp%extremum, hshp%extr_date, ishape = 0)
-  call read_snapshot(s, k1, hshp, corner * deg_to_rad, step * deg_to_rad, &
-       nlon, nlat, copy, k1, ishape_last = [3])
+  call read_snapshot(s, k1, hshp, corner_deg * deg_to_rad, &
+       step_deg * deg_to_rad, nlon, nlat, copy, k1, ishape_last = [3])
   CALL shp_tr_close(hshp)
 
-  call write_snapshot(s, corner, step, nlon, nlat, copy, k1)
+  call write_snapshot(s, corner_deg, step_deg, nlon, nlat, copy, k1)
   call mpi_finalize
 
 end program test_read_snapshot
diff --git a/Overlap/Tests/test_send_recv.f90 b/Overlap/Tests/test_send_recv.f90
index 1bf4b1aa..cc5b3195 100644
--- a/Overlap/Tests/test_send_recv.f90
+++ b/Overlap/Tests/test_send_recv.f90
@@ -4,7 +4,7 @@ program test_send_recv
 
   ! Libraries:
   use ezmpi, only: ezmpi_bcast
-  use jumble, only: get_command_arg_dyn, read_column
+  use jumble, only: get_command_arg_dyn, read_column, new_unit
   use mpi_f08, only: mpi_init, mpi_finalize, MPI_Comm_rank, MPI_Comm_world, &
        MPI_Comm_size, mpi_abort, MPI_TAG_UB, MPI_Comm_get_attr, &
        MPI_ADDRESS_KIND, MPI_io, MPI_ANY_SOURCE
@@ -24,21 +24,22 @@ program test_send_recv
 
   character(len = :), allocatable:: shp_tr_dir
   type(snapshot) s
-  integer rank, n_proc, k1, copy, n_dates
+  integer rank, n_proc, k1, copy, n_dates, unit
   logical flag
   INTEGER(KIND=MPI_ADDRESS_KIND) attribute_val
 
-  real:: corner(2) = [0.125, - 59.875]
+  real:: corner_deg(2) = [0.125, - 59.875]
   ! longitude and latitude of the corner of the whole grid, in degrees
 
-  real:: step(2) = [0.25, 0.25] ! longitude and latitude steps, in degrees
+  real:: step_deg(2) = [0.25, 0.25] ! longitude and latitude steps, in degrees
   integer:: nlon = 120, nlat = 120
 
   integer:: dist_lim = 12
   ! We look for an overlapping eddy at dist_lim (in grid points) of
   ! the first extremum.
 
-  namelist /main_nml/ corner, step, nlon, nlat, dist_lim
+  namelist /main_nml/ dist_lim
+  namelist /grid_nml/ corner_deg, step_deg, nlon, nlat
   logical periodic ! grid is periodic in longitude
   TYPE(shp_tr) hshp
 
@@ -71,11 +72,17 @@ program test_send_recv
      read(unit = *, nml = main_nml)
      write(unit = *, nml = main_nml)
 
+     call new_unit(unit)
+     open(unit, file = shp_tr_dir // "/grid_nml.txt", status = "old", &
+          action = "read", position  = "rewind")
+     read(unit, nml = grid_nml)
+     close(unit)
+
      ! As we are requiring the grid spacing to be uniform, the value of
-     ! "periodic" may be deduced from the values of step(1) and nlon:
-     periodic = nint(360. / step(1)) == nlon
+     ! "periodic" may be deduced from the values of step_deg(1) and nlon:
+     periodic = nint(360. / step_deg(1)) == nlon
      print *, "periodic = ", periodic
-     if (periodic) call assert(2 * dist_lim * step(1) < 180., &
+     if (periodic) call assert(2 * dist_lim * step_deg(1) < 180., &
           "test_send_recv dist_lim")
      copy = merge(dist_lim, 0, periodic)
      call read_column(ishape_last, &
@@ -83,8 +90,8 @@ program test_send_recv
      n_dates = size(ishape_last)
   end if
 
-  call ezmpi_bcast(corner, root = 0)
-  call ezmpi_bcast(step, root = 0)
+  call ezmpi_bcast(corner_deg, root = 0)
+  call ezmpi_bcast(step_deg, root = 0)
   call ezmpi_bcast(nlon, root = 0)
   call ezmpi_bcast(nlat, root = 0)
   call ezmpi_bcast(copy, root = 0)
@@ -96,8 +103,8 @@ program test_send_recv
   if (rank == 0) call dbf_read_attribute_03(k1, hshp%extremum, hshp%extr_date, &
        ishape = 0)
   call ezmpi_bcast(k1, root = 0)
-  if (rank == 1) call read_snapshot(s, k1, hshp, corner * deg_to_rad, &
-       step * deg_to_rad, nlon, nlat, copy, k1, ishape_last)
+  if (rank == 1) call read_snapshot(s, k1, hshp, corner_deg * deg_to_rad, &
+       step_deg * deg_to_rad, nlon, nlat, copy, k1, ishape_last)
   CALL shp_tr_close(hshp)
   
   if (rank == 1) then
@@ -108,7 +115,7 @@ program test_send_recv
      call assert(flag, "test_send_recv MPI_Comm_get_attr MPI_TAG_UB")
      print *, "MPI_TAG_UB = ", attribute_val
      call recv_snapshot(s, nlon, nlat, copy, source = 1, tag = k1)
-     call write_snapshot(s, corner, step, nlon, nlat, copy, k1)
+     call write_snapshot(s, corner_deg, step_deg, nlon, nlat, copy, k1)
   end if
 
   call mpi_finalize
-- 
GitLab