def test_repr(self): """Test return from __repr__ in class.""" expected = '<ExtractExtrema: period: 24, start_hour: 9>' self.assertEqual(expected, Plugin(24).__repr__()) expected = '<ExtractExtrema: period: 12, start_hour: 9>' self.assertEqual(expected, Plugin(12).__repr__()) expected = '<ExtractExtrema: period: 12, start_hour: 12>' self.assertEqual(expected, Plugin(12, start_hour=12).__repr__())
def test_time_coordinates_24_hour(self): """Time coordinate should be a series of mid points calculated from the start hour + half the period. Each should have an associated pair of bounds that show the range over which the extrema values have been calculated. The first day is the first day for which any site has valid data. The UTC time coord starts at 00 UTC on 27th April 2017, so the first day in which any data falls is 26th April 2017 (for any UTC-N sites). The first 24 hours starting at 00 therefore runs 00 26th to 00 27th. Subsequent 24 hour periods are then expected to the latest day for which any site has data; a UTC+14 site. Input data spans 48 hours, this will spread to three days with timezone adjustments.""" n_periods = 72 / 24 mid_start = mktime(dt(2017, 3, 26, 12).utctimetuple()) / 3600. lower_bound = mktime(dt(2017, 3, 26, 00).utctimetuple()) / 3600. upper_bound = mktime(dt(2017, 3, 27, 00).utctimetuple()) / 3600. result = Plugin(24, start_hour=0).process(self.cube) result = result.extract(Constraint(name='air_temperature_max')) # Expected time coordinate values. for i in range(n_periods): mid_time = mid_start + (i * 24.) low_bound = lower_bound + (i * 24.) up_bound = upper_bound + (i * 24.) self.assertEqual(result[i].coord('time').points, [mid_time]) self.assertEqual(result[i].coord('time').bounds[0, 0], [low_bound]) self.assertEqual(result[i].coord('time').bounds[0, 1], [up_bound])
def test_extrema_values_day1(self): """Test the actual values returned by the collapse method to ensure it is successfully extracting the maximum/minimum temperatures in the defined period. UTC times : 00 01 02 ... 10 11 12 13 14 UTC offset:-12 -11 -10 ... -2 -1 0 1 2 site index: 0 1 2 ... 10 11 12 13 14 Local time: 12 13 14 ... 22 23 00 01 02 site_index 12 at time_index 0 should be adjusted to fall at 00 27th April 2017 in local time, so is expected to fall outside day 1. Thus setting a high value for this site should leave the maximum unset for site 12. site_index 2 at time_index 9 (09Z 27 April 2017) will fall at 23 26th April 2017 local time, so setting this to 40 should modify the maximum for site 2 on day 1.""" self.cube.data[9, 2] = 40 self.cube.data[0, 12] = 40 # Expected data array. expected = np.arange(0, 27).astype(float) expected[2] = 40. result = Plugin(24, start_hour=0).process(self.cube) result = result.extract(Constraint(name='air_temperature_max')) self.assertTrue(result[0].data[12].mask) self.assertArrayEqual(result[0].data, expected)
def test_data_arrays_day1(self): """Test extraction of maxima and minima values from the time localised cube in the first 24 hours. The first day is the first day for which any site has valid data. The UTC time coord starts at 00 UTC on 27th April 2017, so the first day in which any data falls is 26th April 2017 (for any UTC-N sites). The first 24 hours starting at 00 therefore runs 00 26th to 00 27th. Any sites UTC+N will be have no valid data for this first day. That the correct sites return valid data is tested here.""" # Expected time coordinate values. mid_time = mktime(dt(2017, 3, 26, 12).utctimetuple()) / 3600. lower_bound = mktime(dt(2017, 3, 26, 00).utctimetuple()) / 3600. upper_bound = mktime(dt(2017, 3, 27, 00).utctimetuple()) / 3600. # Expected data array. expected = np.full(self.n_data, np.nan) expected[0:12] = range(12) expected = np.ma.masked_invalid(expected) result = Plugin(24, start_hour=0).process(self.cube) result = result.extract(Constraint(name='air_temperature_max')) self.assertArrayEqual(expected, result[0].data) self.assertEqual(result[0].coord('time').points, [mid_time]) self.assertEqual(result[0].coord('time').bounds[0, 0], [lower_bound]) self.assertEqual(result[0].coord('time').bounds[0, 1], [upper_bound])
def test_data_arrays_day2(self): """Test extraction of maxima and minima values from the time localised cube in the second 24 hours. All sites should return valid data during day 2 which runs 00 27th to 00 28th.""" # Expected time coordinate values. mid_time = mktime(dt(2017, 3, 27, 12).utctimetuple()) / 3600. lower_bound = mktime(dt(2017, 3, 27, 00).utctimetuple()) / 3600. upper_bound = mktime(dt(2017, 3, 28, 00).utctimetuple()) / 3600. # Expected data array. expected = np.arange(0, 27) result = Plugin(24, start_hour=0).process(self.cube) result = result.extract(Constraint(name='air_temperature_max')) self.assertArrayEqual(expected, result[2].data) self.assertEqual(result[1].coord('time').points, [mid_time]) self.assertEqual(result[1].coord('time').bounds[0, 0], [lower_bound]) self.assertEqual(result[1].coord('time').bounds[0, 1], [upper_bound])