From 0d63255c0bd08ee2790c640af4c5c828971d06af Mon Sep 17 00:00:00 2001
From: Lionel GUEZ <guez@lmd.ens.fr>
Date: Tue, 7 May 2024 21:27:34 +0200
Subject: [PATCH] Define `self.n_dates` at initialization

Instead of defining `self._n_dates` with zeroes. So we do not need the
function `get_n_dates` any longer.
---
 Common/util_eddies.py                      | 36 ++--------------------
 Inst_eddies/Analysis/anim_eddy_contours.py |  4 +--
 2 files changed, 5 insertions(+), 35 deletions(-)

diff --git a/Common/util_eddies.py b/Common/util_eddies.py
index f070b3a8..bb575604 100644
--- a/Common/util_eddies.py
+++ b/Common/util_eddies.py
@@ -89,7 +89,6 @@ class SHPC_class:
 
             self.d_init.append(d_init_slice)
 
-        self._n_dates = np.zeros(self.n_slices, dtype=int)
         self._ishape_last = [
             {"Anticyclones": None, "Cyclones": None}
             for i in range(self.n_slices)
@@ -103,43 +102,14 @@ class SHPC_class:
 
         # We only need ishape_last for the last slice, for one
         # orientation, in order to define n_dates:
-        self.get_ishape_last(self.n_slices - 1, self.def_orient)
+        ishape_last = self.get_ishape_last(self.n_slices - 1, self.def_orient)
+
+        self.n_dates = np.ediff1d(self.d_init, to_end=ishape_last.size)
 
         self._n_shapes = [
             {"Anticyclones": 0, "Cyclones": 0} for i in range(self.n_slices)
         ]
 
-    def get_n_dates(self, i_slice):
-        """Return the number of dates in a given slice, including missing
-        dates. That is last date in the slice minus first date in the
-        slice plus one.
-
-        """
-
-        if self._n_dates[i_slice] == 0:
-            # Find out the number of dates in the slice:
-            if self._ishape_last[i_slice]["Anticyclones"] is not None:
-                self._n_dates[i_slice] = self._ishape_last[i_slice][
-                    "Anticyclones"
-                ].size
-            elif self._ishape_last[i_slice]["Cyclones"] is not None:
-                self._n_dates[i_slice] = self._ishape_last[i_slice][
-                    "Cyclones"
-                ].size
-            else:
-                try:
-                    self._n_dates[i_slice] = self.get_ishape_last(
-                        i_slice, self.def_orient
-                    ).size
-                except AttributeError:
-                    # Try the other orientation:
-                    orientation = other_orient(self.def_orient)
-                    self._n_dates[i_slice] = self.get_ishape_last(
-                        i_slice, orientation
-                    ).size
-
-        return self._n_dates[i_slice]
-
     def get_ishape_last(self, i_slice, orientation):
         """Defines and returns self._ishape_last[i_slice][orientation], which
         is a numpy array. Can return None if ishape_last.txt does not
diff --git a/Inst_eddies/Analysis/anim_eddy_contours.py b/Inst_eddies/Analysis/anim_eddy_contours.py
index a3688d03..f28b106b 100755
--- a/Inst_eddies/Analysis/anim_eddy_contours.py
+++ b/Inst_eddies/Analysis/anim_eddy_contours.py
@@ -144,12 +144,12 @@ if __name__ == "__main__":
             not SHPC.d_init[0]
             <= d_min
             < d_max
-            <= SHPC.d_init[-1] + SHPC.get_n_dates(SHPC.n_slices - 1) - 1
+                <= SHPC.d_init[-1] + SHPC.n_dates[SHPC.n_slices - 1] - 1
         ):
             sys.exit("Bad dates specified")
     else:
         d_min = SHPC.d_init[0]
-        d_max = SHPC.d_init[0] + SHPC.get_n_dates(0) - 1
+        d_max = SHPC.d_init[0] + SHPC.n_dates[0] - 1
     # --
 
     fig = plt.figure()
-- 
GitLab