From cdeb688eef244a9c0a94d6b8c3528b15b9a2228e Mon Sep 17 00:00:00 2001
From: Lionel GUEZ <guez@lmd.ens.fr>
Date: Tue, 22 May 2018 16:32:16 +0200
Subject: [PATCH] Change name of output file "graph_1.txt" to "edgelist_1.csv".
 Add title lines in "edgelist_1.csv". Add python script reading
 "edgelist_1.csv" into a NetworkX graph.

---
 Tests/read_overlap.py           | 23 +++++++++++++++++++++++
 Tests/test_successive_overlap.f | 20 ++++++++++++++------
 successive_overlap.f            |  8 ++++----
 3 files changed, 41 insertions(+), 10 deletions(-)
 create mode 100755 Tests/read_overlap.py

diff --git a/Tests/read_overlap.py b/Tests/read_overlap.py
new file mode 100755
index 00000000..9c756497
--- /dev/null
+++ b/Tests/read_overlap.py
@@ -0,0 +1,23 @@
+#!/usr/bin/env python3
+
+import networkx as nx
+import csv
+import matplotlib.pyplot as plt
+
+G = nx.DiGraph()
+
+with open("edgelist_1.csv") as f:
+    reader = csv.reader(f, delimiter = " ", skipinitialspace = True)
+
+    # Skip title lines:
+    next(reader) 
+    next(reader)
+    
+    for row in reader:
+        k1, i1, k2, i2, weight = int(row[0]), int(row[1]), int(row[2]), \
+                                 int(row[3]), float(row[4])
+        G.add_edge((k1, i1), (k2, i2), weight = weight)
+
+plt.figure()
+nx.draw(G, with_labels=True)
+plt.show()
diff --git a/Tests/test_successive_overlap.f b/Tests/test_successive_overlap.f
index e30e359f..35dbbac6 100644
--- a/Tests/test_successive_overlap.f
+++ b/Tests/test_successive_overlap.f
@@ -12,7 +12,7 @@ program test_successive_overlap
 
   implicit none
 
-  integer unit_graph, i
+  integer unit_edgelist, i
   type(snapshot) flow(2)
   TYPE(shpfileobject) hshp_extremum ! shapefile extremum_1
   TYPE(shpfileobject) hshp_outermost ! shapefile outermost_contour_1
@@ -43,11 +43,19 @@ program test_successive_overlap
   CALL shpclose(hshp_max_speed)
 
   flow(2) = flow(1)
-  call new_unit(unit_graph)
-  open(unit_graph, file = "graph_1.txt", status = "replace", action  = "write")
-  call successive_overlap(unit_graph, nlon, nlat, flow, j = 2, k = 2)
-  close(unit_graph)
-  print *, 'Created file "graph_1.txt".'
+  call new_unit(unit_edgelist)
+  open(unit_edgelist, file = "edgelist_1.csv", status = "replace", &
+       action  = "write")
+
+  ! Title lines:
+  write(unit_edgelist, fmt = "(1x, a)") '"predecessor date index" ' &
+       // '"predecessor eddy index" "successor date index" ' &
+       // '"successor eddy index"'
+  write(unit_edgelist, fmt = *) "k1 i1 k2 i2 weight"
+  
+  call successive_overlap(unit_edgelist, nlon, nlat, flow, j = 2, k = 2)
+  close(unit_edgelist)
+  print *, 'Created file "edgelist_1.csv".'
 
   do i = 1, 2
      print *, "flow(", i, ")%list_vis%delta_in:", flow(i)%list_vis%delta_in
diff --git a/successive_overlap.f b/successive_overlap.f
index 78c1f44d..5f3948ee 100644
--- a/successive_overlap.f
+++ b/successive_overlap.f
@@ -4,10 +4,10 @@ module successive_overlap_m
 
 contains
 
-  subroutine successive_overlap(unit_graph, nlon, nlat, flow, j, k)
+  subroutine successive_overlap(unit_edgelist, nlon, nlat, flow, j, k)
 
     ! Finds edges between flow(j - 1) and flow(j), corresponding to
-    ! dates k - 1 and k. Writes these edges to graph.txt. Updates
+    ! dates k - 1 and k. Writes these edges to unit_edgelist. Updates
     ! flow(j - 1)%list_vis%delta_out and flow(j)%list_vis%delta_in.
 
     use contour_531, only: polyline
@@ -17,7 +17,7 @@ contains
     use spherical_polyline_area_m, only: spherical_polyline_area
     use weight_m, only: weight
 
-    integer, intent(in):: unit_graph ! logical unit for graph file
+    integer, intent(in):: unit_edgelist ! logical unit for edgelist file
     integer, intent(in):: nlon, nlat
     type(snapshot), intent(inout):: flow(:) ! (max_delta + 1)
     integer, intent(in):: j, k
@@ -62,7 +62,7 @@ contains
                          if (spherical_polygon_area(res_pol) >= 0.25 &
                               * min(spherical_polyline_area(polyline_1), &
                               spherical_polyline_area(polyline_2))) then
-                            write(unit_graph, fmt = *) k - 1, i1, k, i2, &
+                            write(unit_edgelist, fmt = *) k - 1, i1, k, i2, &
                                  weight(flow(j - 1)%list_vis(i1), &
                                  flow(j)%list_vis(i2))
                             flow(j - 1)%list_vis(i1)%delta_out = 1
-- 
GitLab