Ejemplo n.º 1
0
def test_2d_coordinates_in_extra_dimensions():

    dummy = MockPrognosticWithExtraDimensionsIn2d()

    with pytest.raises(NotImplementedError) as excinfo:
        get_default_state([dummy])
    assert 'not yet supported' in str(excinfo.value)
Ejemplo n.º 2
0
def test_with_malformed_extra_quantity():

    dummy = MockPrognosticWithMalformedExtraQuantities()

    with pytest.raises(ValueError) as excinfo:
        get_default_state([dummy])
    assert 'Malformed' in str(excinfo.value)
Ejemplo n.º 3
0
def test_only_int_levels_specified():

    dummy = MockPrognostic()
    with pytest.raises(ValueError):
        get_default_state([dummy],
                          interface_levels=dict(label='vert_coord',
                                                values=np.linspace(0, 10, 10),
                                                units='kilometer'))
Ejemplo n.º 4
0
def test_different_dimension_units():

    dummy = MockPrognostic()
    with pytest.raises(ValueError) as excinfo:
        get_default_state([dummy],
                          y=dict(label='along_shore',
                                 values=np.ones((2, 2)),
                                 units='degrees_north'))
    assert 'must have the same shape' in str(excinfo.value)
Ejemplo n.º 5
0
def test_mid_level_2d():

    dummy = MockPrognostic()
    with pytest.raises(ValueError):
        get_default_state([dummy],
                          mid_levels=dict(label='vert_coord',
                                          values=np.random.randn(4, 4),
                                          units='kilometer'),
                          interface_levels=dict(label='vert_coord',
                                                values=np.linspace(0, 10, 10),
                                                units='kilometer'))
Ejemplo n.º 6
0
def test_mid_int_levels_not_consistent():

    dummy = MockPrognostic()
    with pytest.raises(ValueError):
        get_default_state([dummy],
                          interface_levels=dict(label='vert_coord',
                                                values=np.linspace(0, 10, 10),
                                                units='kilometer'),
                          mid_levels=dict(label='vert_coord',
                                          values=np.linspace(0, 10, 11),
                                          units='kilometer'))
Ejemplo n.º 7
0
def test_2d_coordinates_wrong_shape():

    dummy = MockPrognosticWithExtraQuantities()

    with pytest.raises(ValueError) as excinfo:
        get_default_state([dummy],
                          x=dict(label='shore',
                                 values=np.random.randn(3, 4),
                                 units='km'),
                          y=dict(label='latitude',
                                 values=np.random.randn(3, 3),
                                 units='degrees east'))
    assert '2d coordinates, they' in str(excinfo.value)
Ejemplo n.º 8
0
def test_dcmip_options():

    dcmip = DcmipInitialConditions()

    state = climt.get_default_state([dcmip],
                                    y=dict(label='latitude',
                                           values=np.linspace(0, 60, 20),
                                           units='degrees_north'))

    dry_state = dcmip(state)
    moist_state = dcmip(state, moist_simulation=True)
    not_perturbed_state = dcmip(state, add_perturbation=False)

    tropical_cyclone_state = dcmip(state,
                                   type_of_output='tropical_cyclone',
                                   moist_simulation=True)

    assert not np.all(
        np.isclose(dry_state['specific_humidity'].values,
                   moist_state['specific_humidity'].values))

    assert not np.all(
        np.isclose(dry_state['eastward_wind'].values,
                   not_perturbed_state['eastward_wind'].values))

    assert np.all(
        np.isclose(
            tropical_cyclone_state['surface_air_pressure'].values - 1.015e5,
            np.zeros(
                not_perturbed_state['surface_air_pressure'].values.shape)))
Ejemplo n.º 9
0
def test_enthalpy_and_water_conservation():

    conv_adj = climt.DryConvectiveAdjustment()

    state = climt.get_default_state([conv_adj],
                                    grid_state=climt.get_grid(nz=35))

    dp = (state['air_pressure_on_interface_levels'][:-1] -
          state['air_pressure_on_interface_levels'][1:]) / (
              state['air_pressure_on_interface_levels'][0] -
              state['air_pressure_on_interface_levels'][-1])
    state['air_temperature'][:1] += 10
    state['specific_humidity'][0] = 0.5

    initial_enthalpy = np.sum(
        heat_capacity(state['specific_humidity']) * state['air_temperature'] *
        dp)
    initial_water = np.sum(state['specific_humidity'].values * dp.values)

    diag, output = conv_adj(state, timedelta(hours=1))

    final_water = np.sum(output['specific_humidity'].values * dp.values)
    final_enthalpy = np.sum(
        heat_capacity(output['specific_humidity']) *
        output['air_temperature'] * dp)

    assert np.isclose(initial_water, final_water)
    assert np.isclose(initial_enthalpy, final_enthalpy)
Ejemplo n.º 10
0
def test_dcmip_options():

    state = climt.get_default_state([DcmipInitialConditions()],
                                    grid_state=get_grid(nx=64, ny=64, nz=10))

    dry_state = DcmipInitialConditions(moist=False)(state)
    moist_state = DcmipInitialConditions(moist=True)(state)
    not_perturbed_state = DcmipInitialConditions(moist=False,
                                                 add_perturbation=False)(state)
    tropical_cyclone_state = DcmipInitialConditions(
        moist=True, condition_type='tropical_cyclone')(state)

    assert not np.all(
        np.isclose(dry_state['specific_humidity'].values,
                   moist_state['specific_humidity'].values))

    assert not np.all(
        np.isclose(dry_state['eastward_wind'].values,
                   not_perturbed_state['eastward_wind'].values))

    assert not np.all(
        np.isclose(
            tropical_cyclone_state['surface_air_pressure'].values - 1.015e5,
            np.zeros(
                not_perturbed_state['surface_air_pressure'].values.shape)))
Ejemplo n.º 11
0
 def get_1d_input_state(self, component=None):
     if component is None:
         component = self.get_component_instance()
     return climt.get_default_state([component],
                                    grid_state=get_grid(nx=None,
                                                        ny=None,
                                                        nz=30))
Ejemplo n.º 12
0
def test_basic_2d_coordinates():

    dummy = MockPrognosticWithExtraQuantities()
    random_x_values = np.random.randn(3, 4)
    random_y_values = np.random.randn(3, 4)
    state = get_default_state([dummy],
                              x=dict(label='shore',
                                     values=random_x_values,
                                     units='km'),
                              y=dict(label='latitude',
                                     values=random_y_values,
                                     units='degrees east'))

    assert state['x'].values.shape[0] == 3
    assert state['x'].values.shape[1] == 4

    assert state['y'].values.shape[0] == 3
    assert state['y'].values.shape[1] == 4

    assert np.all(state['x'].values == random_x_values)
    assert np.all(state['y'].values == random_y_values)

    for quantity in dummy._climt_inputs.keys():
        assert 'shore' in state[quantity].coords
        assert 'latitude' in state[quantity].coords
        assert state[quantity].coords['shore'].units is 'km'
        assert state[quantity].coords['latitude'].units is 'degrees east'
Ejemplo n.º 13
0
def test_get_diagnostics_with_real_component():
    dummy = RRTMGLongwave()
    state = get_default_state([dummy])

    diag = dummy.create_state_dict_for('_climt_diagnostics', state)

    for quantity in dummy._climt_diagnostics:
        assert quantity in diag
Ejemplo n.º 14
0
def test_unknown_attribute():

    dummy = MockPrognosticWithExtraQuantities()
    state = get_default_state([dummy])

    with pytest.raises(IndexError) as excinfo:
        dummy.get_numpy_arrays_from_state('random', state)
    assert 'no attribute called' in str(excinfo.value)
Ejemplo n.º 15
0
def test_get_class_defined_quantity():

    dummy = MockPrognosticWithExtraQuantities()
    state = get_default_state([dummy])
    input_arrays = dummy.get_numpy_arrays_from_state('_climt_inputs', state)

    assert np.all(state['sigma_on_interface_levels'].values ==
                  input_arrays['sigma_on_interface_levels'])
Ejemplo n.º 16
0
 def get_3d_input_state(self, component=None):
     if component is None:
         component = self.get_component_instance()
     state = climt.get_default_state([component],
                                     grid_state=climt.get_grid(nx=10, ny=5))
     state['cloud_area_fraction_in_atmosphere_layer'][16:19] = 0.5
     state['mass_content_of_cloud_ice_in_atmosphere_layer'][16:19] = 0.3
     return state
Ejemplo n.º 17
0
def test_dycore_inputs_are_valid():

    dummy = MockDycoreWithAllAttributes()
    state = get_default_state([dummy])

    for input in dummy.inputs:
        if input not in ['x', 'y', 'mid_levels', 'interface_levels']:
            assert input in state
            assert input in dummy.input_properties
Ejemplo n.º 18
0
 def get_3d_input_state(self):
     state = climt.get_default_state([self.get_component_instance()],
                                     grid_state=get_grid(nx=32,
                                                         ny=32,
                                                         nz=28))
     # state = super(TestGFSDycoreWithDcmipInitialConditions, self).get_3d_input_state()
     state.update(
         climt.DcmipInitialConditions(add_perturbation=True)(state))
     return state
Ejemplo n.º 19
0
def try1():
    radiation = climt.GrayLongwaveRadiation()
    surface = climt.SlabSurface()
    state = climt.get_default_state([radiation, surface])
    tendencies, diagnostics = radiation(state)
    
    print tendencies.keys()
    print
    print tendencies['air_temperature']
Ejemplo n.º 20
0
def test_get_inputs_with_real_component():
    dummy = RRTMGLongwave()
    state = get_default_state([dummy])

    diag = dummy.create_state_dict_for('_climt_inputs', state)

    for quantity in dummy._climt_inputs:
        assert quantity in diag
        assert diag[quantity].dims == state[quantity].dims
Ejemplo n.º 21
0
def test_get_tendencies():

    dummy = MockPrognosticWithAllAttributes()
    state = get_default_state([dummy])

    diag = dummy.create_state_dict_for('_climt_tendencies', state)

    for quantity in dummy.tendencies:
        assert quantity in diag
Ejemplo n.º 22
0
    def get_3d_input_state(self):

        component = self.get_component_instance()
        state = climt.get_default_state([component],
                                        y=dict(label='latitude',
                                               values=np.linspace(0, 2, 4),
                                               units='degrees_north'))

        return state
Ejemplo n.º 23
0
def test_get_undefined_array():

    dummy = MockPrognosticWithExtraQuantities()
    state = get_default_state([dummy])
    dummy = MockPrognosticWithExtraQuantitiesNotDefined()

    with pytest.raises(IndexError) as excinfo:
        dummy.get_numpy_arrays_from_state('_climt_inputs', state)
    assert 'not described' in str(excinfo.value)
Ejemplo n.º 24
0
def test_inputs_is_not_dict():

    dummy = MockPrognostic()
    state = get_default_state([dummy])
    dummy._climt_inputs = ('air_temperature', 'oxygen_mixing_ratio')

    with pytest.raises(NotImplementedError) as excinfo:
        dummy.get_numpy_arrays_from_state('_climt_inputs', state)
    assert 'with a dict-like' in str(excinfo.value)
Ejemplo n.º 25
0
 def test_component_pairs(self):
     random.seed(0)
     for _ in range(self.pair_tests):
         i, j = random.sample(range(len(self.component_classes)), 2)
         component1 = self.component_classes[i]()
         component2 = self.component_classes[j]()
         print(component1.__class__.__name__, component2.__class__.__name__)
         state = get_default_state([component1, component2])
         call_component(component1, state)
         call_component(component2, state)
Ejemplo n.º 26
0
def test_wrong_memory_layout():

    dummy = MockPrognostic()
    state = get_default_state([dummy])

    with pytest.raises(ValueError) as excinfo:
        dummy.get_numpy_arrays_from_state('_climt_inputs',
                                          state,
                                          memory_layout='abcd')
    assert 'memory_layout' in str(excinfo.value)
Ejemplo n.º 27
0
def test_extracting_arrays_from_real_component():
    dummy = RRTMGLongwave()
    state = get_default_state([dummy])

    arrays = dummy.get_numpy_arrays_from_state('_climt_inputs', state)

    for quantity in dummy._climt_inputs.keys():
        units = dummy._climt_inputs[quantity]
        state_values = state[quantity].to_units(units).values
        assert np.all(arrays[quantity] == state_values)
Ejemplo n.º 28
0
def test_get_numpy_version_with_numpy_array_in_state():

    component = RRTMGShortwave()
    state = get_default_state([component])

    state['test_values'] = np.arange(100)

    raw_array = numpy_version_of(state)

    assert np.all(raw_array['test_values'] == np.arange(100))
Ejemplo n.º 29
0
def test_scaling_diagnostic_output():

    dcmip = climt.DcmipInitialConditions()
    dcmip = dcmip.scaled_version(diagnostic_scale_factors=dict(
        eastward_wind=0))

    state = climt.get_default_state([dcmip])

    diagnostics = dcmip(state)

    assert np.all(diagnostics['eastward_wind'].values == 0)
Ejemplo n.º 30
0
def test_scaling_prognostic_tendency():

    radiation = climt.RRTMGLongwave()
    radiation = radiation.scaled_version(tendency_scale_factors=dict(
        air_temperature=0))

    state = climt.get_default_state([radiation])

    tendency, diagnostics = radiation(state)

    assert np.all(tendency['air_temperature'].values == 0)