From 86cf7b848c476403ce48b463b173da33ec402d2c Mon Sep 17 00:00:00 2001
From: Lionel GUEZ <guez@lmd.ens.fr>
Date: Tue, 11 Apr 2023 21:02:09 +0200
Subject: [PATCH] Return None if eddy index is greater than max

In function `comp_ishape`, return None instead of crashing if eddy
index is greater than max. This allows the script
`write_date_range.py` to work with an input SHPC containing a date
without eddies.
---
 Common/util_eddies.py | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)

diff --git a/Common/util_eddies.py b/Common/util_eddies.py
index a89e5a70..1ebff246 100644
--- a/Common/util_eddies.py
+++ b/Common/util_eddies.py
@@ -185,7 +185,8 @@ class SHPC_class:
     def comp_ishape(self, date, eddy_index, i_slice, orientation):
         """Compute the location in the shapefiles. Returns None if ishape_last
         was not found and could not be created. Crashes if date is not
-        in i_slice. Crashes if eddy_index is out of range.
+        in i_slice. Returns None if eddy_index is greater than the
+        maximum value.
 
         """
 
@@ -196,13 +197,16 @@ class SHPC_class:
         else:
             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
+            assert 1 <= eddy_index
+
+            if 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
             else:
-                # k >= 1
-                ishape = ishape_last[k - 1] + eddy_index
+                ishape = None
 
         return ishape
 
-- 
GitLab