From d6329740da31dfdd1c9c5eae03dc64557d864dd0 Mon Sep 17 00:00:00 2001
From: Lionel GUEZ <guez@lmd.ens.fr>
Date: Tue, 23 Mar 2021 22:43:08 +0100
Subject: [PATCH] Change node identifiers from Matlab

The node identifiers in Matlab use the date index in the array of
processed dates. This is not simply the difference between the current
date and the first date if there are missing dates. This means that
converting between node index in Matlab and date and eddy indices, one
needs the array of processed dates. Morever, the conversion from date
and eddy indices to node index requires a search through the array of
dates. So we do not keep the Matlab node identifier. We use k and n as
described in the documentation.

For this commit, we need to revert in part commit 68da90c. We have to
read `date_num` from the Matlab files, and we create function
`adjust_n` to convert from Matlab node identifier to the new node identifier.
---
 Convert_Matlab/overlap.m     |  3 ++-
 Convert_Matlab/overlap_v6.py | 18 +++++++++++++-----
 Convert_Matlab/tests.json    |  6 ++++--
 3 files changed, 19 insertions(+), 8 deletions(-)

diff --git a/Convert_Matlab/overlap.m b/Convert_Matlab/overlap.m
index 21508ba0..4de059cb 100644
--- a/Convert_Matlab/overlap.m
+++ b/Convert_Matlab/overlap.m
@@ -24,8 +24,9 @@ toc
 
 clear
 disp('Processing Parameters_Anticyclonic_Eddies...')
-load('Parameters_Anticyclonic_Eddies.mat', 'Nanti')
+load('Parameters_Anticyclonic_Eddies.mat', 'Nanti', 'date_num')
 save('Nanti', 'Nanti', '-v6')
+save('date_num', 'date_num', '-v6')
 
 clear
 disp('Processing Parameters_Cyclonic_Eddies...')
diff --git a/Convert_Matlab/overlap_v6.py b/Convert_Matlab/overlap_v6.py
index 56769e42..737ee0fa 100755
--- a/Convert_Matlab/overlap_v6.py
+++ b/Convert_Matlab/overlap_v6.py
@@ -10,11 +10,17 @@ import scipy.io as sio
 import csv
 import sys
 
+def adjust_n(n_Matlab):
+    k_Matlab = (n_Matlab - 1) // e_overestim
+    k = date_num[k_Matlab] - date_num[0]
+    return n_Matlab + (k - k_Matlab) * e_overestim
+
 orientation = sys.argv[1] # "cyclo" or "anti"
 id_child = sio.loadmat(f"id_child_{orientation}.mat", squeeze_me=True)\
     ["id_child"]
 n_eddies = sio.loadmat(f"N{orientation}.mat", squeeze_me = True)\
     [f"N{orientation}"].astype(int, casting = "safe", copy = False)
+date_num = sio.loadmat("date_num.mat", squeeze_me = True)["date_num"]
 n_dates = n_eddies.size
 print("n_dates =", n_dates)
 e_overestim = id_child.shape[0]
@@ -23,17 +29,19 @@ print("e_overestim =", e_overestim)
 with open(f"edgelist_{orientation}.csv", "w", newline = '') as edgelist:
     writer = csv.writer(edgelist, delimiter = ' ', lineterminator = "\n")
 
-    for k1 in range(n_dates):
-        for i1, id_child_1 in enumerate(id_child[:n_eddies[k1], k1]):
-            n1 = k1 * e_overestim + i1 + 1
+    for j1 in range(n_dates):
+        for i1, id_child_1 in enumerate(id_child[:n_eddies[j1], j1]):
+            n1 = (date_num[j1] - date_num[0]) * e_overestim + 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.
 
             try:
                 # Try processing id_child_1 as an array:
-                for n2 in id_child_1:
+                for n_Matlab in id_child_1:
+                    n2 = adjust_n(n_Matlab)
                     writer.writerow((n1, n2))
             except TypeError:
                 # id_child_1 is a scalar int.
-                writer.writerow((n1, id_child_1))
+                n2 = adjust_n(id_child_1)
+                writer.writerow((n1, n2))
diff --git a/Convert_Matlab/tests.json b/Convert_Matlab/tests.json
index 02c99001..3b9e2fa3 100644
--- a/Convert_Matlab/tests.json
+++ b/Convert_Matlab/tests.json
@@ -5,7 +5,8 @@
 	"required":
 	[
 	    "$src_dir/Convert_Matlab/Eurec4A_OA/id_child_cyclo.mat",
-	    "$src_dir/Convert_Matlab/Eurec4A_OA/Ncyclo.mat"
+	    "$src_dir/Convert_Matlab/Eurec4A_OA/Ncyclo.mat",
+	    "$src_dir/Convert_Matlab/Eurec4A_OA/date_num.mat"
 	]
     },
     {
@@ -14,7 +15,8 @@
 	"required":
 	[
 	    "$src_dir/Convert_Matlab/Eurec4A_OA/id_child_anti.mat",
-	    "$src_dir/Convert_Matlab/Eurec4A_OA/Nanti.mat"
+	    "$src_dir/Convert_Matlab/Eurec4A_OA/Nanti.mat",
+	    "$src_dir/Convert_Matlab/Eurec4A_OA/date_num.mat"
 	]
     },
     {
-- 
GitLab