Beispiel #1
0
def test_check_units_fail(mock_cubes):
    """Test units after derivation if derivation scripts returns None."""
    mock_all_derived_variables(Unit('kg'))
    with pytest.raises(ValueError) as err:
        derive(mock_cubes, SHORT_NAME, mock.sentinel.long_name, 'm',
               standard_name=mock.sentinel.standard_name)
    assert str(err.value) == (
        "Units 'kg' after executing derivation script of 'short_name' cannot "
        "be converted to target units 'm'"
    )
    _derive.logger.warning.assert_not_called()
Beispiel #2
0
def test_derive_noop():
    """Test derivation when it is not necessary."""
    alb = Cube([1.])
    alb.var_name = 'alb'
    alb.long_name = 'albedo at the surface'
    alb.units = 1

    cube = derive([alb], alb.var_name, alb.long_name, alb.units)

    assert cube is alb
Beispiel #3
0
def test_derive_noop():

    alb = Cube([1.])
    alb.var_name = 'alb'
    alb.long_name = 'albedo at the surface'
    alb.units = 1

    cube = derive([alb], alb.var_name, alb.long_name, alb.units)

    print(cube)
    assert cube is alb
Beispiel #4
0
def test_check_units_convertible(mock_cubes):
    """Test units after derivation if derivation scripts returns None."""
    mock_all_derived_variables(Unit('kg s-1'))
    cube = derive(mock_cubes, SHORT_NAME, mock.sentinel.long_name, 'g yr-1',
                  standard_name=mock.sentinel.standard_name)
    assert_derived_var_calc_called_once_with(mock_cubes)
    cube.convert_units.assert_called_once_with('g yr-1')
    assert cube.var_name == SHORT_NAME
    assert cube.long_name == mock.sentinel.long_name
    assert cube.standard_name == mock.sentinel.standard_name
    _derive.logger.warning.assert_not_called()
Beispiel #5
0
def test_derive_mixed_case_with_fx(tmp_path, monkeypatch):

    short_name = 'ohc'
    long_name = 'Heat content in grid cell'
    units = 'J'

    ohc_cube = Cube([])

    def mock_calculate(self, cubes):
        assert len(cubes) == 1
        assert cubes[0] == ohc_cube
        return Cube([])

    monkeypatch.setattr(DerivedVariable, 'calculate', mock_calculate)

    derive(
        [ohc_cube],
        short_name,
        long_name,
        units,
    )
Beispiel #6
0
def run_derive(
    cubes,
    variable: typing.Dict,
    **kwargs,
):
    short_name = variable["short_name"]
    long_name = variable["long_name"]
    units = variable["units"]
    results = derive(cubes,
                     short_name=short_name,
                     long_name=long_name,
                     units=units)

    # TODO: see preprocess() in preprocessor/__init__.py
    return results
Beispiel #7
0
def test_check_units_unknown(mock_cubes):
    """Test units after derivation if derivation scripts returns None."""
    mock_all_derived_variables(Unit('unknown'))
    cube = derive(mock_cubes, SHORT_NAME, mock.sentinel.long_name, 'J',
                  standard_name=mock.sentinel.standard_name)
    assert_derived_var_calc_called_once_with(mock_cubes)
    assert cube.units == Unit('J')
    assert cube.var_name == SHORT_NAME
    assert cube.long_name == mock.sentinel.long_name
    assert cube.standard_name == mock.sentinel.standard_name
    _derive.logger.warning.assert_called_once_with(
        "Units of cube after executing derivation script of '%s' are '%s', "
        "automatically setting them to '%s'. This might lead to incorrect "
        "data", SHORT_NAME, Unit('unknown'), 'J')
    cube.convert_units.assert_not_called()
Beispiel #8
0
def test_derive_nonstandard_nofx():

    short_name = 'alb'
    long_name = 'albedo at the surface'
    units = 1
    standard_name = ''

    rsds = Cube([2.])
    rsds.standard_name = 'surface_downwelling_shortwave_flux_in_air'

    rsus = Cube([1.])
    rsus.standard_name = 'surface_upwelling_shortwave_flux_in_air'

    cubes = CubeList([rsds, rsus])

    alb = derive(cubes, short_name, long_name, units, standard_name)

    print(alb)
    assert alb.var_name == short_name
    assert alb.long_name == long_name
    assert alb.units == units
    assert alb.data == [0.5]
Beispiel #9
0
def test_derive_nonstandard_nofx():
    """Test a specific derivation."""
    short_name = 'alb'
    long_name = 'albedo at the surface'
    units = 1
    standard_name = ''

    rsdscs = Cube([2.])
    rsdscs.short_name = 'rsdscs'
    rsdscs.var_name = rsdscs.short_name

    rsuscs = Cube([1.])
    rsuscs.short_name = 'rsuscs'
    rsuscs.var_name = rsuscs.short_name

    cubes = CubeList([rsdscs, rsuscs])

    alb = derive(cubes, short_name, long_name, units, standard_name)

    assert alb.var_name == short_name
    assert alb.long_name == long_name
    assert alb.units == units
    assert alb.data == [0.5]