From 13ff23e38976ff2306d18f29f8136b3367222457 Mon Sep 17 00:00:00 2001
From: Lionel GUEZ <guez@lmd.ens.fr>
Date: Mon, 25 Jul 2022 13:21:36 +0200
Subject: [PATCH] Create component `unit` in `shpc_slice_handler`

Create component `unit` for `ishape_last.txt` in derived type
`shpc_slice_handler`. Open, create and close `ishape_last.txt` at the
same time we open, create and close the shapefiles. So we use
`read_opcol` instead of `read_column` when we need to read
`ishape_last.txt`.
---
 Common/derived_types.f90                 |  1 +
 Common/shpc_close.f90                    |  1 +
 Common/shpc_create.f90                   |  3 +++
 Common/shpc_open.f90                     |  9 +++++++++
 Inst_eddies/Tests/test_get_1_outerm.f90  |  4 +---
 Inst_eddies/Tests/test_nearby_extr.f90   |  5 ++---
 Inst_eddies/write_aux.f90                | 14 +++-----------
 Overlap/Tests/test_get_dispatch_snap.f90 |  5 ++---
 Overlap/Tests/test_overlap.f90           |  5 ++---
 Overlap/Tests/test_read_eddy.f90         |  5 ++---
 Overlap/Tests/test_read_snapshot.f90     |  5 ++---
 Overlap/Tests/test_send_recv.f90         |  5 ++---
 Overlap/eddy_graph.f90                   |  6 +++---
 13 files changed, 33 insertions(+), 35 deletions(-)

diff --git a/Common/derived_types.f90 b/Common/derived_types.f90
index 1a165e94..87655a45 100644
--- a/Common/derived_types.f90
+++ b/Common/derived_types.f90
@@ -72,6 +72,7 @@ module derived_types
      TYPE(shpfileobject) extremum ! shapefile extremum
      TYPE(shpfileobject) outermost ! shapefile outermost_contour
      TYPE(shpfileobject) max_speed ! shapefile max_speed_contour
+     integer unit ! ishape_last.txt
      logical cyclone
      
      ! Field identifiers in the DBF files:
diff --git a/Common/shpc_close.f90 b/Common/shpc_close.f90
index 7c43e2e8..7fb38ede 100644
--- a/Common/shpc_close.f90
+++ b/Common/shpc_close.f90
@@ -18,6 +18,7 @@ contains
     CALL shpclose(hshp%extremum)
     CALL shpclose(hshp%outermost)
     CALL shpclose(hshp%max_speed)
+    close(hshp%unit)
     deallocate(hshp%dir)
     if (allocated(hshp%ishape_last)) deallocate(hshp%ishape_last)
 
diff --git a/Common/shpc_create.f90 b/Common/shpc_create.f90
index 245c46f0..b3e7df61 100644
--- a/Common/shpc_create.f90
+++ b/Common/shpc_create.f90
@@ -61,6 +61,9 @@ contains
     call dbf_add_field_03(hshp%max_speed_eddy_index, hshp%max_speed, &
          'eddy_index', ftinteger, nwidth = 5, ndecimals = 0)
 
+    call new_unit(hshp%unit)
+    open(hshp%unit, file = hshp%dir // "/ishape_last.txt", status = "replace", &
+         action = "write")
     hshp%cyclone = cyclone
     call new_unit(unit)
     open(unit, file  = shpc_dir // "/orientation.txt", status = "replace", &
diff --git a/Common/shpc_open.f90 b/Common/shpc_open.f90
index 344d072e..654581ac 100644
--- a/Common/shpc_open.f90
+++ b/Common/shpc_open.f90
@@ -36,6 +36,15 @@ contains
     close(unit)
     hshp%cyclone = trim(adjustl(orientation)) == "cyclones"
     hshp%dir = shpc_dir
+    call new_unit(hshp%unit)
+
+    if (pszaccess == "rb") then
+       open(hshp%unit, file = hshp%dir // "/ishape_last.txt", &
+            status = "old", action = "read", position = "rewind")
+    else
+       open(hshp%unit, file = hshp%dir // "/ishape_last.txt", &
+            status = "old", action = "readwrite", position = "append")
+    end if
 
   end subroutine shpc_open
 
diff --git a/Inst_eddies/Tests/test_get_1_outerm.f90 b/Inst_eddies/Tests/test_get_1_outerm.f90
index bba5dba2..e4f7c764 100644
--- a/Inst_eddies/Tests/test_get_1_outerm.f90
+++ b/Inst_eddies/Tests/test_get_1_outerm.f90
@@ -99,9 +99,7 @@ program test_get_1_outerm
      e%valid = .true.
      call shpc_create(hshp, shpc_dir = "SHPC", cyclone = cyclone)
      call write_eddy(e, hshp, date, i = 1)
-     open(unit, file = "SHPC/ishape_last.txt", status = "replace", &
-          action = "write")
-     write(unit, fmt = *) 0
+     write(hshp%unit, fmt = *) 0
      CALL shpc_close(hshp)
      print *, 'Created "SHPC".'
   else
diff --git a/Inst_eddies/Tests/test_nearby_extr.f90 b/Inst_eddies/Tests/test_nearby_extr.f90
index ad155d3b..63d95eb2 100644
--- a/Inst_eddies/Tests/test_nearby_extr.f90
+++ b/Inst_eddies/Tests/test_nearby_extr.f90
@@ -1,7 +1,7 @@
 program test_nearby_extr
 
   ! Libraries:
-  use jumble, only: get_command_arg_dyn, new_unit, read_column
+  use jumble, only: get_command_arg_dyn, new_unit, read_opcol
   use jumble, only: deg_to_rad, rad_to_deg
   use shapelib_03, only: dbf_read_attribute_03
 
@@ -45,8 +45,7 @@ program test_nearby_extr
 
   call shpc_open(hshp, trim(shpc_dir), pszaccess = "rb")
   call dbf_read_attribute_03(hshp%d0, hshp%extremum, hshp%extr_date, ishape = 0)
-  call read_column(hshp%ishape_last, file = shpc_dir // "/ishape_last.txt", &
-       my_lbound = hshp%d0)
+  call read_opcol(hshp%ishape_last, hshp%unit, my_lbound = hshp%d0)
   call read_snapshot(s, [hshp], nlon, nlat, k = hshp%d0, &
        corner = corner_deg * deg_to_rad, step = step_deg * deg_to_rad, &
        copy = 0)
diff --git a/Inst_eddies/write_aux.f90 b/Inst_eddies/write_aux.f90
index d271e3eb..ea8ace3b 100644
--- a/Inst_eddies/write_aux.f90
+++ b/Inst_eddies/write_aux.f90
@@ -39,22 +39,14 @@ contains
     !--------------------------------------------------------------------
 
     call shp_get_info_03(hshpc%extremum, n_entities)
-    call new_unit(unit)
+    write(hshpc%unit, fmt = *) n_entities - 1
 
-    if (exist) then
-       open(unit, file = hshpc%dir // "/ishape_last.txt", status = "old", &
-            action = "write", position = "append")
-       write(unit, fmt = *) n_entities - 1
-       close(unit)
-    else
+    if (.not. exist) then
+       call new_unit(unit)
        open(unit, file = hshpc%dir // "/grid_nml.txt", status = "replace", &
             action = "write")
        write(unit, nml = grid_nml)
        close(unit)
-       open(unit, file = hshpc%dir // "/ishape_last.txt", status = "replace", &
-            action = "write")
-       write(unit, fmt = *) n_entities - 1
-       close(unit)
     end if
 
   end subroutine write_aux
diff --git a/Overlap/Tests/test_get_dispatch_snap.f90 b/Overlap/Tests/test_get_dispatch_snap.f90
index fbb12074..9a6b245f 100644
--- a/Overlap/Tests/test_get_dispatch_snap.f90
+++ b/Overlap/Tests/test_get_dispatch_snap.f90
@@ -4,7 +4,7 @@ program test_get_dispatch_snap
 
   ! Libraries:
   use ezmpi, only: ezmpi_bcast
-  use jumble, only: get_command_arg_dyn, read_column, new_unit, assert, &
+  use jumble, only: get_command_arg_dyn, read_opcol, new_unit, assert, &
        deg_to_rad
   use mpi_f08, only: mpi_init, mpi_finalize, MPI_Comm_rank, MPI_Comm_world, &
        MPI_Comm_size, mpi_abort
@@ -74,8 +74,7 @@ program test_get_dispatch_snap
 
   call shpc_open(hshp, trim(shpc_dir), pszaccess = "rb")
   call dbf_read_attribute_03(hshp%d0, hshp%extremum, hshp%extr_date, ishape = 0)
-  call read_column(hshp%ishape_last, file = shpc_dir // "/ishape_last.txt", &
-       my_lbound = hshp%d0)
+  call read_opcol(hshp%ishape_last, hshp%unit, my_lbound = hshp%d0)
 
   if (rank == 0) then
      n_dates = size(hshp%ishape_last)
diff --git a/Overlap/Tests/test_overlap.f90 b/Overlap/Tests/test_overlap.f90
index d20a3bae..fe92043a 100644
--- a/Overlap/Tests/test_overlap.f90
+++ b/Overlap/Tests/test_overlap.f90
@@ -3,7 +3,7 @@ program test_overlap
   use, intrinsic:: ISO_FORTRAN_ENV
 
   ! Libraries:
-  use jumble, only: get_command_arg_dyn, read_column, new_unit, ediff1d
+  use jumble, only: get_command_arg_dyn, read_opcol, new_unit, ediff1d
   use jumble, only: deg_to_rad, assert
   use shapelib_03, only: dbf_read_attribute_03
 
@@ -67,8 +67,7 @@ program test_overlap
   call shpc_open(hshp, trim(shpc_dir), pszaccess = "rb")
   call dbf_read_attribute_03(hshp%d0, hshp%extremum, hshp%extr_date, &
        ishape = 0)
-  call read_column(hshp%ishape_last, file = shpc_dir // "/ishape_last.txt", &
-       my_lbound = hshp%d0)
+  call read_opcol(hshp%ishape_last, hshp%unit, my_lbound = hshp%d0)
   n_dates = size(hshp%ishape_last)
   call assert(hshp%d0 <= [k_test_1, k_test_2] .and. [k_test_1, k_test_2] &
        < hshp%d0 + n_dates, "test_overlap k_test_1, k_test_2")
diff --git a/Overlap/Tests/test_read_eddy.f90 b/Overlap/Tests/test_read_eddy.f90
index 838ca5bd..a59d554d 100644
--- a/Overlap/Tests/test_read_eddy.f90
+++ b/Overlap/Tests/test_read_eddy.f90
@@ -1,7 +1,7 @@
 program test_read_eddy
 
   ! Library:
-  use jumble, only: get_command_arg_dyn, read_column
+  use jumble, only: get_command_arg_dyn, read_opcol
   use shapelib_03, only: dbf_read_attribute_03
 
   use derived_types, only: eddy, shpc_slice_handler
@@ -27,8 +27,7 @@ program test_read_eddy
   read(unit = *, nml = main_nml)
   call shpc_open(hshp, shpc_dir, pszaccess = "rb")
   call dbf_read_attribute_03(hshp%d0, hshp%extremum, hshp%extr_date, ishape = 0)
-  call read_column(hshp%ishape_last, file = shpc_dir // "/ishape_last.txt", &
-       my_lbound = hshp%d0)
+  call read_opcol(hshp%ishape_last, hshp%unit, my_lbound = hshp%d0)
   call read_eddy(e, k, eddy_i, hshp, ishape)
   CALL shpc_close(hshp)
 
diff --git a/Overlap/Tests/test_read_snapshot.f90 b/Overlap/Tests/test_read_snapshot.f90
index 82088b24..b6a9783a 100644
--- a/Overlap/Tests/test_read_snapshot.f90
+++ b/Overlap/Tests/test_read_snapshot.f90
@@ -4,7 +4,7 @@ program test_read_snapshot
 
   ! Libraries:
   use jumble, only: get_command_arg_dyn, new_unit, deg_to_rad, assert, &
-       read_column
+       read_opcol
   use shapelib_03, only: dbf_read_attribute_03
 
   use derived_types, only: snapshot, shpc_slice_handler
@@ -51,8 +51,7 @@ program test_read_snapshot
      call shpc_open(hshp(i), trim(shpc_dir), pszaccess = "rb")
      call dbf_read_attribute_03(hshp(i)%d0, hshp(i)%extremum, &
           hshp(i)%extr_date, ishape = 0)
-     call read_column(hshp(i)%ishape_last, &
-          file = shpc_dir // "/ishape_last.txt", my_lbound = hshp(i)%d0)
+     call read_opcol(hshp(i)%ishape_last, hshp(i)%unit, my_lbound = hshp(i)%d0)
   end do
 
   ! Assuming grid_nml.txt is the same in all input SHPC:
diff --git a/Overlap/Tests/test_send_recv.f90 b/Overlap/Tests/test_send_recv.f90
index b88cd1ed..a5223548 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, new_unit
+  use jumble, only: get_command_arg_dyn, read_opcol, 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
@@ -84,8 +84,7 @@ program test_send_recv
 
   call shpc_open(hshp, trim(shpc_dir), pszaccess = "rb")
   call dbf_read_attribute_03(hshp%d0, hshp%extremum, hshp%extr_date, ishape = 0)
-  call read_column(hshp%ishape_last, file = shpc_dir // "/ishape_last.txt", &
-       my_lbound = hshp%d0)
+  call read_opcol(hshp%ishape_last, hshp%unit, my_lbound = hshp%d0)
 
   if (rank == 0) then
      n_dates = size(hshp%ishape_last)
diff --git a/Overlap/eddy_graph.f90 b/Overlap/eddy_graph.f90
index 3f87fd42..e0e1a432 100644
--- a/Overlap/eddy_graph.f90
+++ b/Overlap/eddy_graph.f90
@@ -5,7 +5,7 @@ program eddy_graph
   ! Libraries:
   use ezmpi, only: ezmpi_bcast
   use jumble, only: get_command_arg_dyn, new_unit, ediff1d, assert, &
-       deg_to_rad, read_column
+       deg_to_rad, read_opcol
   use mpi_f08, only: mpi_init, mpi_finalize, MPI_Comm_rank, MPI_Comm_world, &
        MPI_Comm_size
   use shapelib_03, only: dbf_read_attribute_03
@@ -81,8 +81,8 @@ program eddy_graph
      call shpc_open(hshpc(i), trim(shpc_dir), pszaccess = "rb")
      call dbf_read_attribute_03(hshpc(i)%d0, hshpc(i)%extremum, &
           hshpc(i)%extr_date, ishape = 0)
-     call read_column(hshpc(i)%ishape_last, &
-          file = shpc_dir // "/ishape_last.txt", my_lbound = hshpc(i)%d0)
+     call read_opcol(hshpc(i)%ishape_last, hshpc(i)%unit, &
+          my_lbound = hshpc(i)%d0)
   end do
 
   if (rank == 0) then
-- 
GitLab