def test_NaN_values(self): """Test that NaN values are preserved when they are contained within the input when applying a minimum precipitation rate.""" expected = np.array( [ [ [0.0, 1.0, 2.0], [np.NaN, np.NaN, np.NaN], [0.0, 1.0, MIN_PRECIP_RATE_MMH], ] ] ) precip_cube = self.precip_cube.copy() subtracted_cube = self.subtracted_cube.copy() subtracted_cube.convert_units("mm/hr") subtracted_cube.data = np.array( [[[0.0, 1.0, 2.0], [np.NaN, np.NaN, np.NaN], [0.0, 1.0, 0.0]]] ) subtracted_cube.convert_units("m/s") plugin = ApplyOrographicEnhancement("subtract") result = plugin._apply_minimum_precip_rate(precip_cube, subtracted_cube) self.assertIsInstance(result, iris.cube.Cube) self.assertEqual(result.units, Unit("m/s")) self.assertEqual(result.metadata, subtracted_cube.metadata) result.convert_units("mm/hr") self.assertArrayAlmostEqual(result.data, expected)
def test_differing_units(self): """Test that the minimum precipitation rate is applied correctly, when the units of the input precipitation cube and the cube computed using the input precipitation cube and the orographic enhancement cube are handled correctly, even if the units differ.""" expected = np.array( [ [ [0.0, 1.0, 2.0], [1.0, 2.0, MIN_PRECIP_RATE_MMH], [0.0, 1.0, MIN_PRECIP_RATE_MMH], ] ] ) precip_cube = self.precip_cube.copy() subtracted_cube = self.subtracted_cube.copy() precip_cube.convert_units("km/hr") subtracted_cube.convert_units("ft/s") plugin = ApplyOrographicEnhancement("subtract") result = plugin._apply_minimum_precip_rate(precip_cube, subtracted_cube) self.assertIsInstance(result, iris.cube.Cube) self.assertEqual(result.units, Unit("ft/s")) self.assertEqual(result.metadata, subtracted_cube.metadata) result.convert_units("mm/hr") self.assertArrayAlmostEqual(result.data, expected)
def test_basic_add(self): """Test a minimum precipitation rate is applied, when the orographic enhancement causes the precipitation rate to become negative""" expected = np.array([[[0., 1., 2.], [1., 2., 7.], [0., 3., 4.]]]) precip_cube = self.precip_cube.copy() added_cube = self.added_cube.copy() plugin = ApplyOrographicEnhancement("add") result = plugin._apply_minimum_precip_rate(precip_cube, added_cube) self.assertIsInstance(result, iris.cube.Cube) self.assertEqual(result.units, Unit("m/s")) self.assertEqual(result.metadata, added_cube.metadata) result.convert_units("mm/hr") self.assertArrayAlmostEqual(result.data, expected)
def test_no_unit_conversion(self): """Test that the minimum precipitation rate is applied correctly, when the units of the input cube do not require conversion to mm/hr.""" expected = np.array([[[0., 1., 2.], [1., 2., MIN_PRECIP_RATE_MMH], [0., 1., MIN_PRECIP_RATE_MMH]]]) precip_cube = self.precip_cube.copy() subtracted_cube = self.subtracted_cube.copy() precip_cube.convert_units("mm/hr") subtracted_cube.convert_units("mm/hr") plugin = ApplyOrographicEnhancement("subtract") result = (plugin._apply_minimum_precip_rate(precip_cube, subtracted_cube)) self.assertIsInstance(result, iris.cube.Cube) self.assertEqual(result.units, Unit("mm/hr")) self.assertEqual(result.metadata, subtracted_cube.metadata) self.assertArrayAlmostEqual(result.data, expected)
def test_no_min_precip_rate_applied_no_input_precip(self): """Test no minimum precipitation rate is applied, when the input precipitation rate to always below the orographic enhancement.""" expected = np.array([[[0., 0., 0.], [0., 0., 1.], [0., 1., 1.]]]) precip_cube = self.precip_cube.copy() precip_cube.convert_units("mm/hr") precip_cube.data = np.array([[[0., 0., 0.], [0., 0., 5.], [0., 2., 3.]]]) precip_cube.convert_units("m/s") subtracted_cube = precip_cube - self.oe_cube plugin = ApplyOrographicEnhancement("subtract") result = (plugin._apply_minimum_precip_rate(precip_cube, subtracted_cube)) self.assertIsInstance(result, iris.cube.Cube) self.assertEqual(result.units, Unit("m/s")) self.assertEqual(result.metadata, subtracted_cube.metadata) result.convert_units("mm/hr") self.assertArrayAlmostEqual(result.data, expected)
def test_basic_subtract(self): """Test a minimum precipitation rate is applied, when the orographic enhancement causes the precipitation rate to become negative""" expected = np.array([[ [0.0, 1.0, 2.0], [1.0, 2.0, MIN_PRECIP_RATE_MMH], [0.0, 1.0, MIN_PRECIP_RATE_MMH], ]]) precip_cube = self.precip_cube.copy() subtracted_cube = self.subtracted_cube.copy() plugin = ApplyOrographicEnhancement("subtract") result = plugin._apply_minimum_precip_rate(precip_cube, subtracted_cube) self.assertIsInstance(result, iris.cube.Cube) self.assertEqual(result.units, Unit("m/s")) self.assertEqual(result.metadata, subtracted_cube.metadata) result.convert_units("mm/hr") self.assertArrayAlmostEqual(result.data, expected)
def test_no_min_precip_rate_applied_no_negative_rates(self): """Test no minimum precipitation rate is applied, when the cube calculated by subtracting the orographic enhancement from the input precipitation is always positive, so there are no negative values that require the minimum precipitation rate to be applied.""" expected = np.array([[[1., 1., 1.], [1., 2., 3.], [3., 3., 4.]]]) precip_cube = self.precip_cube.copy() subtracted_cube = self.subtracted_cube.copy() subtracted_cube.convert_units("mm/hr") subtracted_cube.data = np.array([[[1., 1., 1.], [1., 2., 3.], [3., 3., 4.]]]) subtracted_cube.convert_units("m/s") plugin = ApplyOrographicEnhancement("subtract") result = (plugin._apply_minimum_precip_rate(precip_cube, subtracted_cube)) self.assertIsInstance(result, iris.cube.Cube) self.assertEqual(result.units, Unit("m/s")) self.assertEqual(result.metadata, subtracted_cube.metadata) result.convert_units("mm/hr") self.assertArrayAlmostEqual(result.data, expected)