From 815ea64ad89c2611f09cec8753d934a0595f9b52 Mon Sep 17 00:00:00 2001 From: Tammas Loughran Date: Sun, 3 Mar 2024 13:19:14 +1100 Subject: [PATCH] Added unit test for negative temperatures during heatwaves --- tests/run_tests.py | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/tests/run_tests.py b/tests/run_tests.py index e51575f..9f56a0c 100755 --- a/tests/run_tests.py +++ b/tests/run_tests.py @@ -182,13 +182,24 @@ class TestIdentifyHW(unittest.TestCase): """Tests for the identify_hw function.""" # Some example EHF index data - ehfdata = np.ma.array([-1.3, -0.3, 3.4, 8.5, -0.4, # Not a heatwave - 5.6, 10.2, 20.4, -1.4, # a three day heatwave starting on day 6 - 7.8, 15.5, 16.9, 17.9, 30.2, -3.3]) # a 5 day heatwave starting on day 10 + ehfdata = np.ma.array([ + -1.3, -0.3, 3.4, 8.5, -0.4, # Not a heatwave + 5.6, 10.2, 20.4, -1.4, # a three day heatwave starting on day 6 + 7.8, 15.5, 16.9, 17.9, 30.2, -3.3, # a 5 day heatwave starting on day 10 + ]) ehfdata[ehfdata<0] = 0 known_events = np.ma.array([0,0,0,0,0,1,1,1,0,1,1,1,1,1,0]) known_ends = np.ma.array([0,0,0,0,0,3,0,0,0,5,0,0,0,0,0]) + # Example data for a heatwave with negative temperatures. + tmindata = np.ma.array([ + np.nan,np.nan,np.nan,np.nan, # does not exceed threshold so is masked out. + -2.5,-1.9,-0.2,0.5,2.1, # Heatwaves with negative values. + np.nan,np.nan, # does not exceed threshold. + ]) + tmin_known_events = np.ma.array([0,0,0,0,1,1,1,1,1,0,0]) + tmin_known_ends = np.ma.array([0,0,0,0,5,0,0,0,0,0,0]) + def testReturnTupple(self): """Should return a tupple containging the event indicator and the durations (numpy.ndarray).""" result = ehfheatwaves.identify_hw(self.ehfdata) @@ -209,6 +220,12 @@ def testShape(self): self.assertEqual(events.shape, input_shape) self.assertEqual(ends.shape, input_shape) + def testNegativeTemperatures(self): + """Tmin or tmax exceedances with negative values should be identified correctly.""" + events, ends = ehfheatwaves.identify_hw(self.tmindata) + self.assertTrue((events==self.tmin_known_events).all()) + self.assertTrue((ends==self.tmin_known_ends).all()) + class TestIdentifySemiHW(unittest.TestCase): """Tests for the identify_semi_hw function."""