Esempio n. 1
0
    def test_cross_reference(self):
        # Test the creation process for a factory definition which uses
        # a cross-reference.

        param_cube = stock.realistic_4d_no_derived()
        orog_coord = param_cube.coord('surface_altitude')
        param_cube.remove_coord(orog_coord)

        orog_cube = param_cube[0, 0, :, :]
        orog_cube.data = orog_coord.points
        orog_cube.rename('surface_altitude')
        orog_cube.units = orog_coord.units
        orog_cube.attributes = orog_coord.attributes

        # We're going to test for the presence of the hybrid height
        # stuff later, so let's make sure it's not already there!
        assert len(param_cube.aux_factories) == 0
        assert not param_cube.coords('surface_altitude')

        # The fake PPFields which will be supplied to our converter.
        press_field = Mock()
        press_field.data = param_cube.data
        orog_field = Mock()
        orog_field.data = orog_cube.data
        field_generator = lambda filename: [press_field, orog_field]
        # A fake rule set returning:
        #   1) A parameter cube needing an "orography" reference
        #   2) An "orography" cube

        def converter(field):
            if field is press_field:
                src = param_cube
                factories = [Factory(HybridHeightFactory,
                                     [Reference('orography')])]
                references = []
            else:
                src = orog_cube
                factories = []
                references = [ReferenceTarget('orography', None)]
            dim_coords_and_dims = [(coord, src.coord_dims(coord)[0])
                                   for coord in src.dim_coords]
            aux_coords_and_dims = [(coord, src.coord_dims(coord))
                                   for coord in src.aux_coords]
            return ConversionMetadata(factories, references, src.standard_name,
                                      src.long_name, src.units, src.attributes,
                                      src.cell_methods, dim_coords_and_dims,
                                      aux_coords_and_dims)
        # Finish by making a fake Loader
        fake_loader = Loader(field_generator, {}, converter, None)
        cubes = load_cubes(['fake_filename'], None, fake_loader)

        # Check the result is a generator containing two Cubes.
        self.assertIsInstance(cubes, types.GeneratorType)
        cubes = list(cubes)
        self.assertEqual(len(cubes), 2)
        # Check the "cube" has an "aux_factory" added, which itself
        # must have been created with the correct arguments.
        self.assertEqual(len(cubes[1].aux_factories), 1)
        self.assertEqual(len(cubes[1].coords('surface_altitude')), 1)
Esempio n. 2
0
 def test_transposed(self):
     cube = stock.realistic_4d_no_derived()
     other = cube.copy()
     other.transpose()
     res = self.cube_func(cube, other)
     self.assertCML(res, checksum=False)
     expected_data = self.data_op(cube.data, other.data.T)
     self.assertArrayEqual(res.data, expected_data)
Esempio n. 3
0
 def test_transposed(self):
     cube = stock.realistic_4d_no_derived()
     other = cube.copy()
     other.transpose()
     res = self.cube_func(cube, other)
     self.assertCML(res, checksum=False)
     expected_data = self.data_op(cube.data, other.data.T)
     self.assertArrayEqual(res.data, expected_data)
Esempio n. 4
0
    def test_cross_reference(self):
        # Test the creation process for a factory definition which uses
        # a cross-reference.

        param_cube = stock.realistic_4d_no_derived()
        orog_coord = param_cube.coord('surface_altitude')
        param_cube.remove_coord(orog_coord)

        orog_cube = param_cube[0, 0, :, :]
        orog_cube.data = orog_coord.points
        orog_cube.rename('surface_altitude')
        orog_cube.units = orog_coord.units
        orog_cube.attributes = orog_coord.attributes

        # We're going to test for the presence of the hybrid height
        # stuff later, so let's make sure it's not already there!
        assert len(param_cube.aux_factories) == 0
        assert not param_cube.coords('surface_altitude')

        # The fake PPFields which will be supplied to our converter.
        press_field = Mock()
        press_field.data = param_cube.data
        orog_field = Mock()
        orog_field.data = orog_cube.data
        field_generator = lambda filename: [press_field, orog_field]
        # A fake rule set returning:
        #   1) A parameter cube needing an "orography" reference
        #   2) An "orography" cube
        def converter(field):
            if field is press_field:
                src = param_cube
                factories = [Factory(HybridHeightFactory,
                                     [Reference('orography')])]
                references = []
            else:
                src = orog_cube
                factories = []
                references = [ReferenceTarget('orography', None)]
            dim_coords_and_dims = [(coord, src.coord_dims(coord)[0])
                                   for coord in src.dim_coords]
            aux_coords_and_dims = [(coord, src.coord_dims(coord))
                                   for coord in src.aux_coords]
            return (factories, references, src.standard_name, src.long_name,
                    src.units, src.attributes, src.cell_methods,
                    dim_coords_and_dims, aux_coords_and_dims)
        # Finish by making a fake Loader
        fake_loader = Loader(field_generator, {}, converter, None)
        cubes = load_cubes(['fake_filename'], None, fake_loader)

        # Check the result is a generator containing two Cubes.
        self.assertIsInstance(cubes, types.GeneratorType)
        cubes = list(cubes)
        self.assertEqual(len(cubes), 2)
        # Check the "cube" has an "aux_factory" added, which itself
        # must have been created with the correct arguments.
        self.assertEqual(len(cubes[1].aux_factories), 1)
        self.assertEqual(len(cubes[1].coords('surface_altitude')), 1)
Esempio n. 5
0
 def test_collapse_middle_dim(self):
     cube = stock.realistic_4d_no_derived()
     other = cube.collapsed(["model_level_number"], MEAN)
     res = self.cube_func(cube, other)
     self.assertCML(res, checksum=False)
     # Add the collapsed dimension back in via np.newaxis to enable
     # numpy broadcasting to function.
     expected_data = self.data_op(cube.data, other.data[:, np.newaxis, ...])
     self.assertMaskedArrayEqual(res.data, expected_data)
Esempio n. 6
0
 def test_collapse_middle_dim(self):
     cube = stock.realistic_4d_no_derived()
     other = cube.collapsed(['model_level_number'], MEAN)
     res = self.cube_func(cube, other)
     self.assertCML(res, checksum=False)
     # Add the collapsed dimension back in via np.newaxis to enable
     # numpy broadcasting to function.
     expected_data = self.data_op(cube.data, other.data[:, np.newaxis, ...])
     self.assertMaskedArrayEqual(res.data, expected_data)
Esempio n. 7
0
 def test_collapse_all_dims(self):
     cube = stock.realistic_4d_no_derived()
     other = cube.collapsed(cube.coords(dim_coords=True), MEAN)
     res = self.cube_func(cube, other)
     self.assertCML(res, checksum=False)
     # No modification to other.data is needed as numpy broadcasting
     # should be sufficient.
     expected_data = self.data_op(cube.data, other.data)
     # Use assertArrayEqual rather than assertMaskedArrayEqual as
     # collapsing all dims does not result in a masked array.
     self.assertArrayEqual(res.data, expected_data)
Esempio n. 8
0
 def test_collapse_last_dims(self):
     cube = stock.realistic_4d_no_derived()
     other = cube.collapsed(["grid_latitude", "grid_longitude"], MEAN)
     res = self.cube_func(cube, other)
     self.assertCML(res, checksum=False)
     # Transpose the dimensions in self.cube that have been collapsed in
     # other to lie at the front, thereby enabling numpy broadcasting to
     # function when applying data operator. Finish by transposing back
     # again to restore order.
     expected_data = self.data_op(cube.data.transpose((2, 3, 0, 1)), other.data).transpose(2, 3, 0, 1)
     self.assertMaskedArrayEqual(res.data, expected_data)
Esempio n. 9
0
 def test_collapse_zeroth_dim(self):
     cube = stock.realistic_4d_no_derived()
     other = cube.collapsed('time', MEAN)
     res = self.cube_func(cube, other)
     self.assertCML(res, checksum=False)
     # No modification to other.data is needed as numpy broadcasting
     # should be sufficient.
     expected_data = self.data_op(cube.data, other.data)
     # Use assertMaskedArrayEqual as collapsing with MEAN results
     # in a cube with a masked data array.
     self.assertMaskedArrayEqual(res.data, expected_data)
Esempio n. 10
0
 def test_collapse_zeroth_dim(self):
     cube = stock.realistic_4d_no_derived()
     other = cube.collapsed('time', MEAN)
     res = self.cube_func(cube, other)
     self.assertCML(res, checksum=False)
     # No modification to other.data is needed as numpy broadcasting
     # should be sufficient.
     expected_data = self.data_op(cube.data, other.data)
     # Use assertMaskedArrayEqual as collapsing with MEAN results
     # in a cube with a masked data array.
     self.assertMaskedArrayEqual(res.data, expected_data)
Esempio n. 11
0
 def test_collapse_all_dims(self):
     cube = stock.realistic_4d_no_derived()
     other = cube.collapsed(cube.coords(dim_coords=True), MEAN)
     res = self.cube_func(cube, other)
     self.assertCML(res, checksum=False)
     # No modification to other.data is needed as numpy broadcasting
     # should be sufficient.
     expected_data = self.data_op(cube.data, other.data)
     # Use assertArrayEqual rather than assertMaskedArrayEqual as
     # collapsing all dims does not result in a masked array.
     self.assertArrayEqual(res.data, expected_data)
Esempio n. 12
0
 def test_collapse_last_dims(self):
     cube = stock.realistic_4d_no_derived()
     other = cube.collapsed(['grid_latitude', 'grid_longitude'], MEAN)
     res = self.cube_func(cube, other)
     self.assertCML(res, checksum=False)
     # Transpose the dimensions in self.cube that have been collapsed in
     # other to lie at the front, thereby enabling numpy broadcasting to
     # function when applying data operator. Finish by transposing back
     # again to restore order.
     expected_data = self.data_op(cube.data.transpose((2, 3, 0, 1)),
                                  other.data).transpose(2, 3, 0, 1)
     self.assertMaskedArrayEqual(res.data, expected_data)
Esempio n. 13
0
    def test_cross_reference(self):
        # Test the creation process for a factory definition which uses
        # a cross-reference.

        param_cube = stock.realistic_4d_no_derived()
        orog_coord = param_cube.coord('surface_altitude')
        param_cube.remove_coord(orog_coord)

        orog_cube = param_cube[0, 0, :, :]
        orog_cube.data = orog_coord.points
        orog_cube.rename('surface_altitude')
        orog_cube.units = orog_coord.units
        orog_cube.attributes = orog_coord.attributes

        # We're going to test for the presence of the hybrid height
        # stuff later, so let's make sure it's not already there!
        assert len(param_cube.aux_factories) == 0
        assert not param_cube.coords('surface_altitude')

        press_field = Mock()
        orog_field = Mock()
        field_generator = lambda filename: [press_field, orog_field]
        # A fake rule set returning:
        #   1) A parameter cube needing an "orography" reference
        #   2) An "orography" cube
        factory = Factory(HybridHeightFactory, [Reference('orography')])
        press_rule_result = RuleResult(param_cube, Mock(), [factory])
        orog_rule_result= RuleResult(orog_cube, Mock(), [])
        rules = Mock()
        rules.result = lambda field: \
            press_rule_result if field is press_field else orog_rule_result
        # A fake cross-reference rule set
        ref = ReferenceTarget('orography', None)
        orog_xref_rule = Mock()
        orog_xref_rule.run_actions = lambda cube, field: (ref,)
        xref_rules = Mock()
        xref_rules.matching_rules = lambda field: \
            [orog_xref_rule] if field is orog_field else []
        # Finish by making a fake Loader
        name = 'FAKE_PP'
        fake_loader = Loader(field_generator, {}, rules, xref_rules, name)
        cubes = load_cubes(['fake_filename'], None, fake_loader)
        # Check the result is a generator containing both of our cubes.
        self.assertIsInstance(cubes, types.GeneratorType)
        cubes = list(cubes)
        self.assertEqual(len(cubes), 2)
        self.assertIs(cubes[0], orog_cube)
        self.assertIs(cubes[1], param_cube)
        # Check the "cube" has an "aux_factory" added, which itself
        # must have been created with the correct arguments.
        self.assertEqual(len(param_cube.aux_factories), 1)
        self.assertEqual(len(param_cube.coords('surface_altitude')), 1)
Esempio n. 14
0
    def test_cross_reference(self):
        # Test the creation process for a factory definition which uses
        # a cross-reference.

        param_cube = stock.realistic_4d_no_derived()
        orog_coord = param_cube.coord('surface_altitude')
        param_cube.remove_coord(orog_coord)

        orog_cube = param_cube[0, 0, :, :]
        orog_cube.data = orog_coord.points
        orog_cube.rename('surface_altitude')
        orog_cube.units = orog_coord.units
        orog_cube.attributes = orog_coord.attributes

        # We're going to test for the presence of the hybrid height
        # stuff later, so let's make sure it's not already there!
        assert len(param_cube.aux_factories) == 0
        assert not param_cube.coords('surface_altitude')

        press_field = Mock()
        orog_field = Mock()
        field_generator = lambda filename: [press_field, orog_field]
        # A fake rule set returning:
        #   1) A parameter cube needing an "orography" reference
        #   2) An "orography" cube
        factory = Factory(HybridHeightFactory, [Reference('orography')])
        press_rule_result = RuleResult(param_cube, Mock(), [factory])
        orog_rule_result = RuleResult(orog_cube, Mock(), [])
        rules = Mock()
        rules.result = lambda field: \
            press_rule_result if field is press_field else orog_rule_result
        # A fake cross-reference rule set
        ref = ReferenceTarget('orography', None)
        orog_xref_rule = Mock()
        orog_xref_rule.run_actions = lambda cube, field: (ref, )
        xref_rules = Mock()
        xref_rules.matching_rules = lambda field: \
            [orog_xref_rule] if field is orog_field else []
        # Finish by making a fake Loader
        name = 'FAKE_PP'
        fake_loader = Loader(field_generator, {}, rules, xref_rules, name)
        cubes = load_cubes(['fake_filename'], None, fake_loader)
        # Check the result is a generator containing both of our cubes.
        self.assertIsInstance(cubes, types.GeneratorType)
        cubes = list(cubes)
        self.assertEqual(len(cubes), 2)
        self.assertIs(cubes[0], orog_cube)
        self.assertIs(cubes[1], param_cube)
        # Check the "cube" has an "aux_factory" added, which itself
        # must have been created with the correct arguments.
        self.assertEqual(len(param_cube.aux_factories), 1)
        self.assertEqual(len(param_cube.coords('surface_altitude')), 1)
Esempio n. 15
0
 def setUp(self):
     # Modify stock cube so it is suitable to have a
     # hybrid pressure factory added to it.
     cube = stock.realistic_4d_no_derived()
     cube.coord('surface_altitude').rename('surface_air_pressure')
     cube.coord('surface_air_pressure').units = 'Pa'
     cube.coord('level_height').rename('level_pressure')
     cube.coord('level_pressure').units = 'Pa'
     # Construct and add hybrid pressure factory.
     factory = iris.aux_factory.HybridPressureFactory(
         cube.coord('level_pressure'), cube.coord('sigma'),
         cube.coord('surface_air_pressure'))
     cube.add_aux_factory(factory)
     self.cube = cube
Esempio n. 16
0
 def test_slice(self):
     cube = stock.realistic_4d_no_derived()
     for dim in range(cube.ndim):
         keys = [slice(None)] * cube.ndim
         keys[dim] = 3
         other = cube[tuple(keys)]
         res = self.cube_func(cube, other)
         self.assertCML(res, checksum=False)
         # Add the collapsed dimension back in via np.newaxis to enable
         # numpy broadcasting to function.
         keys[dim] = np.newaxis
         expected_data = self.data_op(cube.data, other.data[tuple(keys)])
         msg = "Problem broadcasting cubes when sliced on dimension {}."
         self.assertArrayEqual(res.data, expected_data, err_msg=msg.format(dim))
Esempio n. 17
0
 def setUp(self):
     # Modify stock cube so it is suitable to have a
     # hybrid pressure factory added to it.
     cube = stock.realistic_4d_no_derived()
     cube.coord("surface_altitude").rename("surface_air_pressure")
     cube.coord("surface_air_pressure").units = "Pa"
     cube.coord("level_height").rename("level_pressure")
     cube.coord("level_pressure").units = "Pa"
     # Construct and add hybrid pressure factory.
     factory = iris.aux_factory.HybridPressureFactory(
         cube.coord("level_pressure"), cube.coord("sigma"), cube.coord("surface_air_pressure")
     )
     cube.add_aux_factory(factory)
     self.cube = cube
Esempio n. 18
0
 def test_slice(self):
     cube = stock.realistic_4d_no_derived()
     for dim in range(cube.ndim):
         keys = [slice(None)] * cube.ndim
         keys[dim] = 3
         other = cube[tuple(keys)]
         res = self.cube_func(cube, other)
         self.assertCML(res, checksum=False)
         # Add the collapsed dimension back in via np.newaxis to enable
         # numpy broadcasting to function.
         keys[dim] = np.newaxis
         expected_data = self.data_op(cube.data, other.data[tuple(keys)])
         msg = 'Problem broadcasting cubes when sliced on dimension {}.'
         self.assertArrayEqual(res.data,
                               expected_data,
                               err_msg=msg.format(dim))
Esempio n. 19
0
    def test_lbvc(self):
        cube = stock.realistic_4d_no_derived()[0, :4, ...]

        v_coord = iris.coords.DimCoord(standard_name="depth", units="m", points=[-5, -10, -15, -20])

        cube.remove_coord("level_height")
        cube.remove_coord("sigma")
        cube.remove_coord("surface_altitude")
        cube.add_aux_coord(v_coord, 0)

        expected = ([2, 1, -5.0], [2, 2, -10.0], [2, 3, -15.0], [2, 4, -20.0])

        for field, (lbvc, lblev, blev) in zip(fields_from_cube(cube), expected):
            self.assertEqual(field.lbvc, lbvc)
            self.assertEqual(field.lblev, lblev)
            self.assertEqual(field.blev, blev)
Esempio n. 20
0
 def setUp(self):
     # Modify stock cube so it is suitable to have a atmosphere sigma
     # factory added to it.
     cube = stock.realistic_4d_no_derived()
     cube.coord("surface_altitude").rename("surface_air_pressure")
     cube.coord("surface_air_pressure").units = "Pa"
     cube.coord("sigma").units = "1"
     ptop_coord = iris.coords.AuxCoord(1000.0, var_name="ptop", units="Pa")
     cube.add_aux_coord(ptop_coord, ())
     cube.remove_coord("level_height")
     # Construct and add atmosphere sigma factory.
     factory = iris.aux_factory.AtmosphereSigmaFactory(
         cube.coord("ptop"),
         cube.coord("sigma"),
         cube.coord("surface_air_pressure"),
     )
     cube.add_aux_factory(factory)
     self.cube = cube
Esempio n. 21
0
    def test_lbvc(self):
        cube = stock.realistic_4d_no_derived()[0, :4, ...]

        v_coord = iris.coords.DimCoord(standard_name='depth',
                                       units='m',
                                       points=[-5, -10, -15, -20])

        cube.remove_coord('level_height')
        cube.remove_coord('sigma')
        cube.remove_coord('surface_altitude')
        cube.add_aux_coord(v_coord, 0)

        expected = ([2, 1, -5.0], [2, 2, -10.0], [2, 3, -15.0], [2, 4, -20.0])

        for field, (lbvc, lblev, blev) in zip(fields_from_cube(cube),
                                              expected):
            self.assertEqual(field.lbvc, lbvc)
            self.assertEqual(field.lblev, lblev)
            self.assertEqual(field.blev, blev)