From 5444de1e4ca825505a25d4c345c57d330ed8f9a6 Mon Sep 17 00:00:00 2001
From: Lionel GUEZ <guez@lmd.ens.fr>
Date: Fri, 7 Apr 2023 22:25:56 +0200
Subject: [PATCH] Check that `eddy_index` is in the right range

In function `comp_ishape`, check that `eddy_index` is in the right
range.
---
 Common/util_eddies.py | 21 ++++++++++++---------
 1 file changed, 12 insertions(+), 9 deletions(-)

diff --git a/Common/util_eddies.py b/Common/util_eddies.py
index 734e1805..03782e4f 100644
--- a/Common/util_eddies.py
+++ b/Common/util_eddies.py
@@ -185,17 +185,20 @@ class SHPC_class:
     def comp_ishape(self, date, eddy_index, i_slice, orientation):
         """Compute the location in the shapefiles."""
 
-        assert date >= self.d_init[i_slice]
+        ishape_last = self.get_ishape_last(i_slice, orientation)
 
-        if date == self.d_init[i_slice]:
-            ishape = eddy_index - 1
+        if ishape_last is None:
+            ishape = None
         else:
-            # date > self.d_init[i_slice]
-            try:
-                ishape = self.get_ishape_last(i_slice, orientation)\
-                    [date - self.d_init[i_slice] - 1] + eddy_index
-            except TypeError:
-                ishape = None
+            k = date - self.d_init[i_slice]
+            assert k >= 0
+            assert 1 <= eddy_index <= self._e_max[i_slice][orientation][k]
+
+            if k == 0:
+                ishape = eddy_index - 1
+            else:
+                # k >= 1
+                ishape = ishape_last[k - 1] + eddy_index
 
         return ishape
 
-- 
GitLab