Beispiel #1
0
def test_bad_name(cube_name):
    """Test we get a useful error if one of the input cubes is incorrectly named."""
    grid = input_cubes()
    (cube,) = grid.extract(cube_name)
    cube.rename("kittens")
    with assert_raises_regex(ValueError, f"Cannot find a cube named '{cube_name}' in "):
        ConvectionRatioFromComponents()(grid)
Beispiel #2
0
def process(*cubes: cli.inputcube, model_id_attr: str = None):
    """ Calculate the convection ratio from convective and dynamic (stratiform)
    precipitation rate components.

    Calculates the convective ratio as:

        ratio = convective_rate / (convective_rate + dynamic_rate)

    Args:
        cubes (iris.cube.CubeList):
            Cubes of "lwe_convective_precipitation_rate" and "lwe_stratiform_precipitation_rate"
            in units that can be converted to "m s-1"
        model_id_attr (str):
            Name of the attribute used to identify the source model for
            blending.

    Returns:
        iris.cube.Cube:
            A cube of convection_ratio of the same dimensions as the input cubes.

    """
    from improver.precipitation_type.convection import ConvectionRatioFromComponents

    if len(cubes) != 2:
        raise IOError(f"Expected 2 input cubes, received {len(cubes)}")
    return ConvectionRatioFromComponents()(cubes, model_id_attr=model_id_attr)
def test_bad_units():
    """Test we get a useful error if the input cubes have units that are not rates."""
    grid = input_cubes()
    for cube in grid:
        cube.units = "m"
    with assert_raises_regex(
            ValueError,
            "Input lwe_convective_precipitation_rate cube cannot be converted to 'm s-1' from ",
    ):
        ConvectionRatioFromComponents()(grid)
def test_basic():
    """Ensure Plugin returns object of correct type and meta-data and that
    no precip => masked array"""
    grid = input_cubes()
    expected_array = np.ma.masked_all_like(grid[0].data)
    result = ConvectionRatioFromComponents()(grid.copy())
    assert isinstance(result, iris.cube.Cube)
    assert_allclose(result.data, expected_array)
    assert result.attributes == grid[0].attributes
    assert result.long_name == "convective_ratio"
    assert result.units == "1"
def test_data(data_con_dyn_out, units):
    """Test that the data are calculated as expected for a selection of values on both
    grids with SI and non-SI units including either side of the minimum precipitation
    rate tolerance 1e-9 m s-1. For each parametrized test, ONE grid point is modified
    (point: [0, 0]). Other points remain as zero inputs which gives a masked output.
    The special expected value of np.inf is used to indicate that the output is expected
    to be a masked value."""
    grid = input_cubes()
    for i in range(2):
        grid[i].data[0, 0] = data_con_dyn_out[i]
    for cube in grid:
        cube.convert_units(units)
    expected_array = np.ma.masked_all_like(grid[0].data)
    if np.isfinite(data_con_dyn_out[2]):
        expected_array[0, 0] = data_con_dyn_out[2]
    result = ConvectionRatioFromComponents()(grid)
    assert_allclose(result.data, expected_array)
    # assert_allclose doesn't check masks appropriately, so check separately
    assert_equal(result.data.mask, expected_array.mask)