From 72d9cf6b57105091739a4eb421d54aa4e359f825 Mon Sep 17 00:00:00 2001
From: Lionel GUEZ <guez@lmd.ens.fr>
Date: Thu, 18 Feb 2021 16:20:01 +0100
Subject: [PATCH] Do not compute predecessor and re-decompose it

e1 is directly i1 + 1 and we just need the new function `date_index`
to process k1. Additional benefit: we process k1 out of the loop on
`id_child_1`.
---
 Convert_Matlab/overlap_v6.py | 23 +++++++++++++++--------
 1 file changed, 15 insertions(+), 8 deletions(-)

diff --git a/Convert_Matlab/overlap_v6.py b/Convert_Matlab/overlap_v6.py
index ab4481a8..c5b9daf0 100755
--- a/Convert_Matlab/overlap_v6.py
+++ b/Convert_Matlab/overlap_v6.py
@@ -10,21 +10,29 @@ import datetime
 import csv
 import sys
 
+def date_index(k):
+    """Compute the number of days since Jan 1st 1950, given the subscript
+    index in date_num.
+
+    """
+    date = datetime.date.fromordinal(date_num[k] - 366) - datetime.date(1950,
+                                                                        1, 1)
+    return date.days
+
 def convert_id(node_index):
     """Convert from node identification in graph to eddy identification in
     shapefiles.
 
     """
     k = (node_index - 1) // n_max
-    date = datetime.date.fromordinal(date_num[k] - 366) - datetime.date(1950,
-                                                                        1, 1)
-    date = date.days
     eddy_index = node_index - k * n_max
-    return date, eddy_index
+    return date_index(k), eddy_index
 
 def print_edgelist(id_child):
+    d1 = date_index(k1)
+    
     for i1, id_child_1 in enumerate(id_child):
-        predecessor = k1 * n_max + 1 + i1
+        e1 = i1 + 1
         # id_child_1 may be an int, which means there is a single
         # successor, or id_child_1 may be a numpy array, which means
         # there are several successors.
@@ -32,11 +40,10 @@ def print_edgelist(id_child):
         try:
             # Try processing id_child_1 as an array:
             for node_index in id_child_1:
-                writer.writerow(convert_id(predecessor)
-                                + convert_id(node_index))
+                writer.writerow((d1, e1) + convert_id(node_index))
         except TypeError:
             # id_child_1 is a scalar int.
-            writer.writerow(convert_id(predecessor) + convert_id(id_child_1))
+            writer.writerow((d1, e1) + convert_id(id_child_1))
 
 orientation = sys.argv[1] # "cyclo" or "anti"
 id_child = sio.loadmat(f"id_child_{orientation}.mat", squeeze_me=True)\
-- 
GitLab