def test_complex_condition(self): """Test with a condition that uses an operator""" query = { "rain_or_snow": self.dummy_queries["significant_precipitation"], } query["rain_or_snow"]["diagnostic_fields"] = [ [ "probability_of_lwe_sleetfall_rate_above_threshold", "-", "probability_of_rainfall_rate_above_threshold", ], [ "probability_of_lwe_sleetfall_rate_above_threshold", "-", "probability_of_lwe_snowfall_rate_above_threshold", ], ] query["rain_or_snow"]["diagnostic_thresholds"] = [ [AuxCoord(0.1, units="mm hr-1"), AuxCoord(0.1, units="mm hr-1")], [AuxCoord(0.1, units="mm hr-1"), AuxCoord(0.1, units="mm hr-1")], ] query["rain_or_snow"]["diagnostic_conditions"] = [ ["above", "above"], ["above", "above"], ] plugin = WeatherSymbols() test_condition = query["rain_or_snow"] result = plugin.create_condition_chain(test_condition) expected = ( "(( cubes.extract(iris.Constraint(name='probability_of_" "lwe_sleetfall_rate_above_threshold', lwe_sleetfall_rate=" "lambda cell: 0.1 * {t_min} < cell < 0.1 * {t_max})" ")[0].data - cubes.extract(iris.Constraint(" "name='probability_of_rainfall_rate_above_threshold', " "rainfall_rate=lambda cell: 0.1 * {t_min} < cell < " "0.1 * {t_max}))[0].data) >= 0.5) | " "(( cubes.extract(iris.Constraint(name=" "'probability_of_lwe_sleetfall_rate_above_threshold', " "lwe_sleetfall_rate=lambda cell: 0.1 * {t_min} < cell " "< 0.1 * {t_max}))[0].data - " "cubes.extract(iris.Constraint(name=" "'probability_of_lwe_snowfall_rate_above_threshold', " "lwe_snowfall_rate=lambda cell: 0.1 * {t_min} < cell " "< 0.1 * {t_max}))[0].data) >= 0.5)".format( t_min=(1.0 - WeatherSymbols().float_tolerance), t_max=(1.0 + WeatherSymbols().float_tolerance), ) ) self.assertIsInstance(result, list) self.assertTrue(all([isinstance(s, str) for s in result])) self.assertEqual(len(result), 1) self.assertEqual(result[0], expected)
def test_basic(self): """Test create_condition_chain returns a list of strings.""" plugin = WeatherSymbols() test_condition = self.dummy_queries['significant_precipitation'] result = plugin.create_condition_chain(test_condition) expected = ("(cubes.extract(iris.Constraint(name='probability_of_" "rainfall_rate', threshold=lambda cell: 0.03 * {t_min} < " "cell < 0.03 * {t_max}))[0].data >= 0.5) | (cubes.extract" "(iris.Constraint(name='probability_of_lwe_snowfall_rate'," " threshold=lambda cell: 0.03 * {t_min} < cell < 0.03 * " "{t_max}))[0].data >= 0.5)".format( t_min=(1. - WeatherSymbols().float_tolerance), t_max=(1. + WeatherSymbols().float_tolerance))) self.assertIsInstance(result, list) self.assertIsInstance(result[0], str) self.assertEqual(result[0], expected)
def test_old_naming_convention(self): """Test create_condition_chain can return conditions using old threshold coordinate name""" plugin = WeatherSymbols() plugin.coord_named_threshold = True test_condition = self.dummy_queries['significant_precipitation'] result = plugin.create_condition_chain(test_condition) expected = ("(cubes.extract(iris.Constraint(name='probability_of_" "rainfall_rate_above_threshold', threshold=lambda " "cell: 0.03 * {t_min} < " "cell < 0.03 * {t_max}))[0].data >= 0.5) | (cubes.extract" "(iris.Constraint(" "name='probability_of_lwe_snowfall_rate_above_threshold'," " threshold=lambda cell: 0.03 * {t_min} < cell < " "0.03 * {t_max}))[0].data >= 0.5)".format( t_min=(1. - WeatherSymbols().float_tolerance), t_max=(1. + WeatherSymbols().float_tolerance))) self.assertIsInstance(result, list) self.assertIsInstance(result[0], str) self.assertEqual(result[0], expected)
def test_basic(self): """Test create_condition_chain returns a list of strings.""" plugin = WeatherSymbols() test_condition = self.dummy_queries["significant_precipitation"] result = plugin.create_condition_chain(test_condition) expected = ("(cubes.extract(iris.Constraint(name='probability_of_" "rainfall_rate_above_threshold', rainfall_rate=lambda " "cell: 0.03 * {t_min} < " "cell < 0.03 * {t_max}))[0].data >= 0.5) | (cubes.extract" "(iris.Constraint(" "name='probability_of_lwe_snowfall_rate_above_threshold'," " lwe_snowfall_rate=lambda cell: 0.03 * {t_min} < cell < " "0.03 * {t_max}))[0].data >= 0.5)".format( t_min=(1.0 - WeatherSymbols().float_tolerance), t_max=(1.0 + WeatherSymbols().float_tolerance), )) self.assertIsInstance(result, list) self.assertTrue(all([isinstance(s, str) for s in result])) self.assertEqual(len(result), 1) self.assertEqual(result[0], expected)