From 268d991597b039bb9b09473007e823abc0e37e63 Mon Sep 17 00:00:00 2001
From: Lionel GUEZ <guez@lmd.ens.fr>
Date: Fri, 5 Feb 2021 16:05:16 +0100
Subject: [PATCH] Group `new_unit` and open

Group `new_unit` and open in the same procedure, that we rename from
`set_unit_edge` to `open_edge_file`. This removes the need for a
special version of `new_unit` and it is closer to the use of the
`new_unit` keyword in Fortran 2008.
---
 Tests/test_overlap.f90       |  9 ++-------
 Tests/test_write_overlap.f90 |  8 ++------
 eddy_graph.f90               | 10 ++--------
 unit_edge_m.f90              | 20 ++++++++++++++++----
 4 files changed, 22 insertions(+), 25 deletions(-)

diff --git a/Tests/test_overlap.f90 b/Tests/test_overlap.f90
index 8f2c35cd..e8b67db3 100644
--- a/Tests/test_overlap.f90
+++ b/Tests/test_overlap.f90
@@ -15,7 +15,7 @@ program test_overlap
   use shp_tr_create_m, only: shp_tr_create
   use shp_tr_open_m, only: shp_tr_open
   use overlap_m, only: overlap
-  use unit_edge_m, only: set_unit_edge, unit_edge_cyclo, unit_edge_anti
+  use unit_edge_m, only: open_edge_file, unit_edge_cyclo, unit_edge_anti
   use write_overlap_m, only: init_interpolated_eddy
 
   implicit none
@@ -96,12 +96,7 @@ program test_overlap
   print *, "flow(max_delta + 1)%list_vis%delta_in:", &
        flow(max_delta + 1)%list_vis%delta_in
   flow(2:max_delta)%number_eddies = 20000
-
-  call set_unit_edge
-  open(unit_edge_cyclo, file = "edgelist_cyclo.csv", status = "replace", &
-       action  = "write")
-  open(unit_edge_anti, file = "edgelist_anti.csv", status = "replace", &
-       action  = "write")
+  call open_edge_file(rank = 0)
 
   ! Title lines:
   write(unit_edge_cyclo, fmt = "(1x, a)") '"predecessor date subscript" ' &
diff --git a/Tests/test_write_overlap.f90 b/Tests/test_write_overlap.f90
index 1b7e99f0..e8eea129 100644
--- a/Tests/test_write_overlap.f90
+++ b/Tests/test_write_overlap.f90
@@ -5,7 +5,7 @@ program test_write_overlap
   use derived_types, only: shp_tr
   use shp_tr_close_m, only: shp_tr_close
   use shp_tr_create_m, only: shp_tr_create
-  use unit_edge_m, only: set_unit_edge, unit_edge_cyclo, unit_edge_anti
+  use unit_edge_m, only: open_edge_file, unit_edge_cyclo, unit_edge_anti
   use write_overlap_m, only: write_overlap, init_interpolated_eddy
 
   implicit none
@@ -25,11 +25,7 @@ program test_write_overlap
   read(unit = *, nml = main_nml)
   write(unit = *, nml = main_nml)
   
-  call set_unit_edge
-  open(unit_edge_cyclo, file = "edgelist_cyclo.csv", status = "replace", &
-       action  = "write")
-  open(unit_edge_anti, file = "edgelist_anti.csv", status = "replace", &
-       action  = "write")
+  call open_edge_file(rank = 0)
 
   ! Title lines:
   write(unit_edge_cyclo, fmt = "(1x, a)") '"predecessor date subscript" ' &
diff --git a/eddy_graph.f90 b/eddy_graph.f90
index 54f80ffb..49c60c5a 100644
--- a/eddy_graph.f90
+++ b/eddy_graph.f90
@@ -14,7 +14,7 @@ program eddy_graph
   use dispatch_snapshot_m, only: dispatch_snapshot
   use get_snapshot_m, only: get_snapshot
   use overlap_m, only: overlap
-  use unit_edge_m, only: set_unit_edge, unit_edge_cyclo, unit_edge_anti
+  use unit_edge_m, only: open_edge_file, unit_edge_cyclo, unit_edge_anti
   use shp_tr_open_m, only: shp_tr_open
   use shp_tr_close_m, only: shp_tr_close
   use shp_tr_create_m, only: shp_tr_create
@@ -130,13 +130,7 @@ program eddy_graph
   open(unit_number_eddies, file = trim(file), status = "replace", &
        action = "write")
 
-  call set_unit_edge
-  write(unit = file, fmt = '("edgelist_cyclo_", i0, ".csv")') rank
-  open(unit_edge_cyclo, file = trim(file), status = "replace", &
-       action  = "write")
-  write(unit = file, fmt = '("edgelist_anti_", i0, ".csv")') rank
-  open(unit_edge_anti, file = trim(file), status = "replace", &
-       action  = "write")
+  call open_edge_file(rank)
 
   write(unit = file, fmt = '("SHP_triplet_", i0)') rank
   call shp_tr_create(hshp_interp, shp_tr_dir = trim(file))
diff --git a/unit_edge_m.f90 b/unit_edge_m.f90
index 33aa5975..3517b6c9 100644
--- a/unit_edge_m.f90
+++ b/unit_edge_m.f90
@@ -7,15 +7,27 @@ module unit_edge_m
 
 contains
 
-  subroutine set_unit_edge
+  subroutine open_edge_file(rank)
 
     use jumble, only: new_unit
 
+    integer, intent(in):: rank
+
+    ! Local:
+    character(len = 30) file
+
     !--------------------------------------------------------------
 
     call new_unit(unit_edge_cyclo)
-    call new_unit(unit_edge_anti, yield = .true.)
-    
-  end subroutine set_unit_edge
+    write(unit = file, fmt = '("edgelist_cyclo_", i0, ".csv")') rank
+    open(unit_edge_cyclo, file = trim(file), status = "replace", &
+         action  = "write")
+
+    call new_unit(unit_edge_anti)
+    write(unit = file, fmt = '("edgelist_anti_", i0, ".csv")') rank
+    open(unit_edge_anti, file = trim(file), status = "replace", &
+         action  = "write")
+
+  end subroutine open_edge_file
   
 end module unit_edge_m
-- 
GitLab