def test_diff_units(self): """Tests that a ValueError is raised if the input uv files have different units. """ msg = "The input uv files do not have the same units." with self.assertRaisesRegex(ValueError, msg): calculate_uv_index(self.cube_uv_up, self.cube_diff_units, scale_factor=1.0)
def test_nan_input(self): """Tests that a ValueError is raised if the input contains values that are not a number. """ self.cube_uv_down.data.fill(np.nan) msg = ("The radiation flux in UV downward contains data " "that is negative or NaN. Data should be >= 0.") with self.assertRaisesRegex(ValueError, msg): calculate_uv_index(self.cube_uv_down)
def test_negative_input(self): """Tests that a ValueError is raised if the input contains negative values. """ negative_data_down = np.full_like(self.cube_uv_down.data, dtype=np.float32, fill_value=-0.1) negative_uv_down = self.cube_uv_down.copy(data=negative_data_down) msg = ("The radiation flux in UV downward contains data " "that is negative or NaN. Data should be >= 0.") with self.assertRaisesRegex(ValueError, msg): calculate_uv_index(negative_uv_down)
def process(uv_flux_up: cli.inputcube, uv_flux_down: cli.inputcube, *, model_id_attr: str = None): """Calculate the UV index using the data in the input cubes. Calculate the uv index using the radiation flux in UV downward at surface and the radiation flux UV upwards at surface. Args: uv_flux_up (iris.cube.Cube): Cube of radiation flux in UV upwards at surface. uv_flux_down (iris.cube.Cube): Cube of radiation flux in UV downwards at surface. model_id_attr (str): Name of the attribute used to identify the source model for blending. Returns: iris.cube.Cube: Processed Cube. """ from improver.uv_index import calculate_uv_index result = calculate_uv_index(uv_flux_up, uv_flux_down, model_id_attr=model_id_attr) return result
def test_metadata(self): """ Tests that the uv index output has the correct metadata (no units, and name = ultraviolet index).""" result = calculate_uv_index(self.cube_uv_up, self.cube_uv_down) self.assertEqual(str(result.standard_name), "ultraviolet_index") self.assertIsNone(result.var_name) self.assertIsNone(result.long_name) self.assertEqual((result.units), Unit("1"))
def test_basic(self): """ Test that the a basic uv calculation works, using the default scaling factor. Make sure the output is a cube with the expected data.""" scale_factor = 1.0 expected = np.array([[0.3, 0.3, 0.3], [0.3, 0.3, 0.3]], dtype=np.float32) result = calculate_uv_index(self.cube_uv_up, self.cube_uv_down, scale_factor) self.assertArrayEqual(result.data, expected)
def test_basic(self): """ Test that the a basic uv calculation works, using the default scaling factor. Make sure the output is a cube with the expected data.""" scale_factor = 1.0 expected = self.cube_uv_down.data.copy() result = calculate_uv_index(self.cube_uv_down, scale_factor) self.assertArrayEqual(result.data, expected)
def test_scale_factor(self): """ Test the uv calculation works when changing the scale factor. Make sure the output is a cube with the expected data.""" expected = np.array([[3.0, 3.0, 3.0], [3.0, 3.0, 3.0]], dtype=np.float32) result = calculate_uv_index(self.cube_uv_up, self.cube_uv_down, scale_factor=10) self.assertArrayEqual(result.data, expected)
def test_unit_conversion(self): """Test that the units are successfully converted to W m-2.""" self.cube_uv_down.convert_units("kW m-2") scale_factor = 1.0 expected = np.full_like( self.cube_uv_down.data, dtype=np.float32, fill_value=0.1 ) result = calculate_uv_index(self.cube_uv_down, scale_factor) self.assertArrayEqual(result.data, expected)
def process(rad_uv_up, rad_uv_down): """Calculate the UV index using the data in the input cubes. Calculate the uv index using radiation flux in UV downward at surface, radiation flux UV upwards at surface and a scaling factor. The scaling factor is configured by the user. Args: rad_uv_up (iris.cube.Cube): Cube of radiation flux in UV upwards at surface. rad_uv_down (iris.cube.Cube): Cube of radiation flux in UV downwards at surface. Returns: result (iris.cube.Cube): Processed Cube. """ result = calculate_uv_index(rad_uv_up, rad_uv_down) return result
def process(uv_flux_up: cli.inputcube, uv_flux_down: cli.inputcube): """Calculate the UV index using the data in the input cubes. Calculate the uv index using the radiation flux in UV downward at surface and the radiation flux UV upwards at surface. Args: uv_flux_up (iris.cube.Cube): Cube of radiation flux in UV upwards at surface. uv_flux_down (iris.cube.Cube): Cube of radiation flux in UV downwards at surface. Returns: iris.cube.Cube: Processed Cube. """ from improver.uv_index import calculate_uv_index result = calculate_uv_index(uv_flux_up, uv_flux_down) return result
def main(argv=None): """ Calculate the UV index using the data in the input cubes.""" parser = ArgParser(description="Calculates the UV index.") parser.add_argument("radiation_flux_upward", metavar="RADIATION_FLUX_UPWARD", help="Path to a NetCDF file of radiation flux " "in uv upward at surface.") parser.add_argument("radiation_flux_downward", metavar="RADIATION_FLUX_DOWNWARD", help="Path to a NetCDF file of radiation flux " "in uv downward at surface.") parser.add_argument("output_filepath", metavar="OUTPUT_FILE", help="The output path for the processed NetCDF") args = parser.parse_args(args=argv) rad_uv_up = load_cube(args.radiation_flux_upward) rad_uv_down = load_cube(args.radiation_flux_downward) result = calculate_uv_index(rad_uv_up, rad_uv_down) save_netcdf(result, args.output_filepath)
def test_badname_up(self): """Tests that a ValueError is raised if the input uv up file has the wrong name. """ msg = "The radiation flux in UV upward" with self.assertRaisesRegex(ValueError, msg): calculate_uv_index(self.cube_up_badname, self.cube_uv_down)
def test_badname_down(self): """Tests that a ValueError is raised if the input uv down file has the wrong name. """ msg = 'The radiation flux in UV downward' with self.assertRaisesRegex(ValueError, msg): calculate_uv_index(self.cube_uv_up, self.cube_down_badname)