From 67eeff1afffa3b6707c7df5a7a76c4bca8eac55a Mon Sep 17 00:00:00 2001
From: Lionel GUEZ <guez@lmd.ens.fr>
Date: Mon, 22 Nov 2021 16:37:19 +0100
Subject: [PATCH] Bug fix: check we find a max-speed contour

Check we find a max-speed contour when `e%radius4 >= 2` and
`e%speed_cont%ssh /= missing_ssh`. See test `Set_max_speed_degenerate`
for a case when we do not find one.
---
 Inst_eddies/set_max_speed.f90 | 37 +++++++++++++++++++++++------------
 1 file changed, 24 insertions(+), 13 deletions(-)

diff --git a/Inst_eddies/set_max_speed.f90 b/Inst_eddies/set_max_speed.f90
index 750ee87a..16d0776f 100644
--- a/Inst_eddies/set_max_speed.f90
+++ b/Inst_eddies/set_max_speed.f90
@@ -91,23 +91,34 @@ contains
        else          
           e%speed_cont%polyline = good_contour(corner, step, ssh, &
                e%speed_cont%ssh, e%coord_extr, outside_points)
-          e%max_speed = mean_speed(u, v, e%speed_cont%polyline, e%coord_extr, &
-               corner, step)
 
-          if (IEEE_IS_NAN(speed_outerm)) then
-             ! This may happen when the outermost contour is near land.
-             ! Stick to the contour coming from max_speed_contour_ssh.
-             e%speed_cont%area = spher_polyline_area(e%speed_cont%polyline)
+          if (e%speed_cont%n_points == 0) then
+             ! This may happen if there is the exact same value of ssh
+             ! next to the extremum and this value is
+             ! e%speed_cont%ssh.
+             e%speed_cont%ssh = missing_ssh
+             e%speed_cont%area = - 1e6
+             e%max_speed = speed_outerm
           else
-             ! Note the following test should raise an invalid exception
-             ! if e%max_speed is NaN.
-             if (abs(speed_outerm) > abs(e%max_speed)) then
-                ! Abandon the contour coming from max_speed_contour_ssh:
-                e%speed_cont = null_ssh_contour()
-                e%max_speed = speed_outerm
-             else
+             e%max_speed = mean_speed(u, v, e%speed_cont%polyline, &
+                  e%coord_extr, corner, step)
+
+             if (IEEE_IS_NAN(speed_outerm)) then
+                ! This may happen when the outermost contour is near land.
                 ! Stick to the contour coming from max_speed_contour_ssh.
                 e%speed_cont%area = spher_polyline_area(e%speed_cont%polyline)
+             else
+                ! Note the following test should raise an invalid exception
+                ! if e%max_speed is NaN.
+                if (abs(speed_outerm) > abs(e%max_speed)) then
+                   ! Abandon the contour coming from max_speed_contour_ssh:
+                   e%speed_cont = null_ssh_contour()
+                   e%max_speed = speed_outerm
+                else
+                   ! Stick to the contour coming from max_speed_contour_ssh.
+                   e%speed_cont%area &
+                        = spher_polyline_area(e%speed_cont%polyline)
+                end if
              end if
           end if
        end if
-- 
GitLab