From c4db31d9e46f7faa0767369b975ce6c876391f44 Mon Sep 17 00:00:00 2001
From: Lionel GUEZ <guez@lmd.ens.fr>
Date: Mon, 25 Apr 2022 19:17:28 +0200
Subject: [PATCH] Return the average radius and Rossby number

Return from function `calculate_radii_rossby` the average radius and
Rossby number instead of the sum of average radii and the sum of
Rossby numbers.
---
 cost_function.py | 42 +++++++++++++++++++++++-------------------
 1 file changed, 23 insertions(+), 19 deletions(-)

diff --git a/cost_function.py b/cost_function.py
index 7cf7f38a..044a5951 100755
--- a/cost_function.py
+++ b/cost_function.py
@@ -31,10 +31,16 @@ import bisect
 import argparse
 
 def calculate_radii_rossby(list_eddies, e_overestim, handlers, array_d_init):
+    """Compute average on list_eddies of Rossby number and radius of
+    maximum speed contour.
+
+    """
+
     radii = 0 # in m
     rossby = 0
     days_modifier = 0
     Omega = 2 * math.pi / 86164.
+    n_eddies = len(list_eddies)
 
     for n in list_eddies:
         current_eddy = report_graph.node_to_date_eddy(n, e_overestim)
@@ -62,7 +68,14 @@ def calculate_radii_rossby(list_eddies, e_overestim, handlers, array_d_init):
 
         radii += R_Vmax # in m
 
-    return {"radii": radii, "rossby": rossby, "days_modifier": days_modifier}
+    radii /= n_eddies
+
+    if n_eddies > days_modifier:
+        rossby /= n_eddies - days_modifier
+    else:
+        rossby = None
+
+    return {"radii": radii, "rossby": rossby}
 
 def get_SHPC(array_d_ini, date_index):
     i_SHPC = bisect.bisect(array_d_init, date_index)
@@ -166,28 +179,22 @@ for n in g.vertices():
                                            e_overestim, handlers, array_d_init)
 
         # Average and assign the first radii:
-        g.vp.first_av_rad[n] = first_res['radii'] / num_of_days_to_avg
-
-        modifier = first_res['days_modifier']
+        g.vp.first_av_rad[n] = first_res['radii']
 
-        if (num_of_days_to_avg - modifier > 0):
+        if first_res['rossby'] is not None:
             # Average and assign the rossbies:
-            g.vp.first_av_ros[n] = first_res['rossby'] \
-                / (num_of_days_to_avg - modifier)
+            g.vp.first_av_ros[n] = first_res['rossby']
 
         # Last 7 days calculation:
         last_res = calculate_radii_rossby(segment[- num_of_days_to_avg:],
                                           e_overestim, handlers, array_d_init)
 
         # Average and assign the last radii
-        g.vp.last_av_rad[n] = last_res['radii'] / num_of_days_to_avg
+        g.vp.last_av_rad[n] = last_res['radii']
 
-        modifier = last_res['days_modifier']
-
-        if (num_of_days_to_avg - modifier > 0):
+        if last_res['rossby'] is not None:
             # Average and assign the rossbies:
-            g.vp.last_av_ros[n] = last_res['rossby'] \
-                / (num_of_days_to_avg - modifier)
+            g.vp.last_av_ros[n] = last_res['rossby']
     else:
         # The number of eddies in a segment is lower than the number
         # of days over which to average. The values will be the same
@@ -195,17 +202,14 @@ for n in g.vertices():
         res = calculate_radii_rossby(segment, e_overestim, handlers,
                                      array_d_init)
 
-        # grab the days modifier
-        modifier = res['days_modifier']
-
-        if (num_of_days - modifier > 0):
+        if res['rossby'] is not None:
             # Average and assign the rossbies:
-            rossby = res['rossby'] / (num_of_days - modifier)
+            rossby = res['rossby']
             g.vp.first_av_ros[n] = rossby
             g.vp.last_av_ros[n] = rossby
 
         # Average and assign the radii
-        radii = res['radii'] / num_of_days
+        radii = res['radii']
         g.vp.first_av_rad[n] = radii
         g.vp.last_av_rad[n] = radii
 
-- 
GitLab