Skip to content

Commit

Permalink
Added unit test for negative temperatures during heatwaves
Browse files Browse the repository at this point in the history
  • Loading branch information
tammasloughran committed Mar 3, 2024
1 parent 392db63 commit 815ea64
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions tests/run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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."""
Expand Down

0 comments on commit 815ea64

Please sign in to comment.