Beispiel #1
0
def test_prognostic_composite_two_components_incompatible_input_dims():
    prognostic1 = MockTendencyComponent(
        input_properties={
            'input1': {
                'dims': ['dims1', 'dims2'],
                'units': 'degK',
            },
        },
        diagnostic_properties={},
        tendency_properties={},
        diagnostic_output={},
        tendency_output={},
    )
    prognostic2 = MockTendencyComponent(
        input_properties={
            'input1': {
                'dims': ['dims1', 'dims3'],
                'units': 'degK',
            },
        },
        diagnostic_properties={},
        tendency_properties={},
        diagnostic_output={},
        tendency_output={},
    )
    try:
        TendencyComponentComposite(prognostic1, prognostic2)
    except InvalidPropertyDictError:
        pass
    else:
        raise AssertionError('Should have raised InvalidPropertyDictError')
Beispiel #2
0
def test_prognostic_composite_two_components_overlapping_diagnostic():
    prognostic1 = MockTendencyComponent(
        input_properties={},
        diagnostic_properties={'diag1': {
            'dims': ['dim1'],
            'units': 'm',
        }},
        tendency_properties={},
        diagnostic_output={},
        tendency_output={},
    )
    prognostic2 = MockTendencyComponent(
        input_properties={},
        diagnostic_properties={'diag1': {
            'dims': ['dim1'],
            'units': 'm',
        }},
        tendency_properties={},
        diagnostic_output={},
        tendency_output={},
    )
    try:
        TendencyComponentComposite(prognostic1, prognostic2)
    except SharedKeyError:
        pass
    else:
        raise AssertionError('Should have raised SharedKeyError')
Beispiel #3
0
def test_prognostic_composite_two_components_swapped_input_dims():
    prognostic1 = MockTendencyComponent(
        input_properties={
            'input1': {
                'dims': ['dims1', 'dims2'],
                'units': 'degK',
            },
        },
        diagnostic_properties={},
        tendency_properties={},
        diagnostic_output={},
        tendency_output={},
    )
    prognostic2 = MockTendencyComponent(
        input_properties={
            'input1': {
                'dims': ['dims2', 'dims1'],
                'units': 'degK',
            },
        },
        diagnostic_properties={},
        tendency_properties={},
        diagnostic_output={},
        tendency_output={},
    )
    composite = TendencyComponentComposite(prognostic1, prognostic2)
    diagnostic_properties = {}
    tendency_properties = {}
    assert (composite.input_properties == prognostic1.input_properties
            or composite.input_properties == prognostic2.input_properties)
    assert composite.diagnostic_properties == diagnostic_properties
    assert composite.tendency_properties == tendency_properties
Beispiel #4
0
def test_prognostic_composite_two_components_tendency_compatible_units():
    prognostic1 = MockTendencyComponent(
        input_properties={},
        diagnostic_properties={},
        tendency_properties={'tend1': {
            'dims': ['dim1'],
            'units': 'km/s',
        }},
        diagnostic_output={},
        tendency_output={},
    )
    prognostic2 = MockTendencyComponent(
        input_properties={},
        diagnostic_properties={},
        tendency_properties={
            'tend1': {
                'dims': ['dim1'],
                'units': 'm/day'
            },
        },
        diagnostic_output={},
        tendency_output={},
    )
    composite = TendencyComponentComposite(prognostic1, prognostic2)
    assert 'tend1' in composite.tendency_properties.keys()
    assert composite.tendency_properties['tend1']['dims'] == ['dim1']
    assert units_are_compatible(
        composite.tendency_properties['tend1']['units'], 'm/s')
Beispiel #5
0
def test_prognostic_composite_two_components_compatible_input_units():
    prognostic1 = MockTendencyComponent(
        input_properties={
            'input1': {
                'dims': ['dims1', 'dims2'],
                'units': 'km',
            },
        },
        diagnostic_properties={},
        tendency_properties={},
        diagnostic_output={},
        tendency_output={},
    )
    prognostic2 = MockTendencyComponent(
        input_properties={
            'input1': {
                'dims': ['dims1', 'dims2'],
                'units': 'cm',
            },
        },
        diagnostic_properties={},
        tendency_properties={},
        diagnostic_output={},
        tendency_output={},
    )
    composite = TendencyComponentComposite(prognostic1, prognostic2)
    assert 'input1' in composite.input_properties.keys()
    assert composite.input_properties['input1']['dims'] == ['dims1', 'dims2']
    assert units_are_compatible(composite.input_properties['input1']['units'],
                                'm')
Beispiel #6
0
def test_empty_prognostic_composite():
    prognostic_composite = TendencyComponentComposite()
    state = {'air_temperature': 273.15}
    tendencies, diagnostics = prognostic_composite(state)
    assert len(tendencies) == 0
    assert len(diagnostics) == 0
    assert isinstance(tendencies, dict)
    assert isinstance(diagnostics, dict)
Beispiel #7
0
def test_prognostic_composite_cannot_use_diagnostic():
    try:
        TendencyComponentComposite(MockEmptyDiagnosticComponent())
    except TypeError:
        pass
    except Exception as err:
        raise err
    else:
        raise AssertionError('TypeError should have been raised')
Beispiel #8
0
def test_prognostic_composite_calls_one_prognostic(mock_call):
    mock_call.return_value = ({'air_temperature': 0.5}, {'foo': 50.})
    prognostic_composite = TendencyComponentComposite(
        MockEmptyTendencyComponent())
    state = {'air_temperature': 273.15}
    tendencies, diagnostics = prognostic_composite(state)
    assert mock_call.called
    assert tendencies == {'air_temperature': 0.5}
    assert diagnostics == {'foo': 50.}
Beispiel #9
0
def test_prognostic_composite_calls_two_prognostics(mock_call):
    mock_call.return_value = ({'air_temperature': 0.5}, {})
    prognostic_composite = TendencyComponentComposite(
        MockEmptyTendencyComponent(), MockEmptyTendencyComponent())
    state = {'air_temperature': 273.15}
    tendencies, diagnostics = prognostic_composite(state)
    assert mock_call.called
    assert mock_call.call_count == 2
    assert tendencies == {'air_temperature': 1.}
    assert diagnostics == {}
Beispiel #10
0
def test_prognostic_composite_two_components_input():
    prognostic1 = MockTendencyComponent(
        input_properties={
            'input1': {
                'dims': ['dims1', 'dims2'],
                'units': 'degK',
            },
            'input2': {
                'dims': ['dims1'],
                'units': 'm',
            },
        },
        diagnostic_properties={},
        tendency_properties={},
        diagnostic_output={},
        tendency_output={},
    )
    prognostic2 = MockTendencyComponent(
        input_properties={
            'input1': {
                'dims': ['dims1', 'dims2'],
                'units': 'degK',
            },
            'input3': {
                'dims': ['dims2'],
                'units': '',
            },
        },
        diagnostic_properties={},
        tendency_properties={},
        diagnostic_output={},
        tendency_output={},
    )
    composite = TendencyComponentComposite(prognostic1, prognostic2)
    input_properties = {
        'input1': {
            'dims': ['dims1', 'dims2'],
            'units': 'degK',
        },
        'input2': {
            'dims': ['dims1'],
            'units': 'm',
        },
        'input3': {
            'dims': ['dims2'],
            'units': '',
        },
    }
    diagnostic_properties = {}
    tendency_properties = {}
    assert composite.input_properties == input_properties
    assert composite.diagnostic_properties == diagnostic_properties
    assert composite.tendency_properties == tendency_properties
Beispiel #11
0
def test_prognostic_composite_single_diagnostic():
    prognostic = MockTendencyComponent(
        input_properties={},
        diagnostic_properties={'diag1': {
            'dims': ['dims2'],
            'units': '',
        }},
        tendency_properties={},
        diagnostic_output={},
        tendency_output={},
    )
    composite = TendencyComponentComposite(prognostic)
    assert composite.input_properties == prognostic.input_properties
    assert composite.diagnostic_properties == prognostic.diagnostic_properties
    assert composite.tendency_properties == prognostic.tendency_properties
Beispiel #12
0
def test_prognostic_component_handles_units_when_combining(
        mock_call, mock2_call):
    mock_call.return_value = ({
        'eastward_wind':
        DataArray(1., attrs={'units': 'm/s'})
    }, {})
    mock2_call.return_value = ({
        'eastward_wind':
        DataArray(50., attrs={'units': 'cm/s'})
    }, {})
    prognostic1 = MockEmptyTendencyComponent()
    prognostic2 = MockEmptyTendencyComponent2()
    composite = TendencyComponentComposite(prognostic1, prognostic2)
    tendencies, diagnostics = composite({})
    assert tendencies['eastward_wind'].to_units('m/s').values.item() == 1.5
Beispiel #13
0
def test_two_prognostic_composite_compatible_dims():
    prognostic = MockTendencyComponent(
        input_properties={
            'input1': {
                'dims': ['dims1', 'dims2'],
                'units': 'degK',
            },
            'input2': {
                'dims': ['dims1', 'dims2'],
                'units': 'degK'
            }
        },
        diagnostic_properties={},
        tendency_properties={
            'tend1': {
                'dims': ['dims1', 'dims2'],
                'units': 'degK / day',
            }
        },
        diagnostic_output={},
        tendency_output={},
    )
    prognostic2 = MockTendencyComponent(
        input_properties={
            'input1': {
                'dims': ['dims1', 'dims2'],
                'units': 'degK',
            },
            'input2': {
                'dims': ['dims1', 'dims2'],
                'units': 'degK'
            }
        },
        diagnostic_properties={},
        tendency_properties={
            'tend1': {
                'dims': ['dims1', 'dims2'],
                'units': 'degK / day',
            }
        },
        diagnostic_output={},
        tendency_output={},
    )
    composite = TendencyComponentComposite(prognostic, prognostic2)
    assert composite.input_properties == prognostic.input_properties
    assert composite.diagnostic_properties == prognostic.diagnostic_properties
    assert composite.tendency_properties == prognostic.tendency_properties
Beispiel #14
0
def test_prognostic_composite_two_components_tendency():
    prognostic1 = MockTendencyComponent(
        input_properties={},
        diagnostic_properties={},
        tendency_properties={'tend1': {
            'dims': ['dim1'],
            'units': 'm/s',
        }},
        diagnostic_output={},
        tendency_output={},
    )
    prognostic2 = MockTendencyComponent(
        input_properties={},
        diagnostic_properties={},
        tendency_properties={
            'tend1': {
                'dims': ['dim1'],
                'units': 'm/s'
            },
            'tend2': {
                'dims': ['dim1'],
                'units': 'degK/s'
            }
        },
        diagnostic_output={},
        tendency_output={},
    )
    composite = TendencyComponentComposite(prognostic1, prognostic2)
    input_properties = {}
    diagnostic_properties = {}
    tendency_properties = {
        'tend1': {
            'dims': ['dim1'],
            'units': 'm/s'
        },
        'tend2': {
            'dims': ['dim1'],
            'units': 'degK/s'
        }
    }
    assert composite.input_properties == input_properties
    assert composite.diagnostic_properties == diagnostic_properties
    assert composite.tendency_properties == tendency_properties
Beispiel #15
0
def test_prognostic_composite_explicit_dims_not_in_input():
    prognostic = MockTendencyComponent(
        input_properties={
            'input1': {
                'dims': ['dims1', 'dims2'],
                'units': 'degK',
            }
        },
        diagnostic_properties={},
        tendency_properties={
            'tend1': {
                'dims': ['dims1', 'dims2'],
                'units': 'degK / day',
            }
        },
        diagnostic_output={},
        tendency_output={},
    )
    composite = TendencyComponentComposite(prognostic)
    assert composite.input_properties == prognostic.input_properties
    assert composite.diagnostic_properties == prognostic.diagnostic_properties
    assert composite.tendency_properties == prognostic.tendency_properties
Beispiel #16
0
def test_prognostic_composite_two_components_diagnostic():
    prognostic1 = MockTendencyComponent(
        input_properties={},
        diagnostic_properties={'diag1': {
            'dims': ['dim1'],
            'units': 'm',
        }},
        tendency_properties={},
        diagnostic_output={},
        tendency_output={},
    )
    prognostic2 = MockTendencyComponent(
        input_properties={},
        diagnostic_properties={'diag2': {
            'dims': ['dim2'],
            'units': 'm',
        }},
        tendency_properties={},
        diagnostic_output={},
        tendency_output={},
    )
    composite = TendencyComponentComposite(prognostic1, prognostic2)
    input_properties = {}
    diagnostic_properties = {
        'diag1': {
            'dims': ['dim1'],
            'units': 'm',
        },
        'diag2': {
            'dims': ['dim2'],
            'units': 'm',
        },
    }
    tendency_properties = {}
    assert composite.input_properties == input_properties
    assert composite.diagnostic_properties == diagnostic_properties
    assert composite.tendency_properties == tendency_properties