def test_unhashable_types_array(self):
        """Test that the utility returns differences when unhashable attributes
        are present, e.g. a numpy array."""
        self.cube.attributes["test_attribute"] = np.array([0, 1, 2])
        cubelist = iris.cube.CubeList([self.cube, self.cube_ukv])
        result = compare_attributes(cubelist)

        expected = [
            {
                "mosg__model_configuration": "uk_ens",
                "mosg__grid_version": "1.2.0",
                "test_attribute": np.array([0, 1, 2]),
            },
            {
                "mosg__model_configuration": "uk_det",
                "mosg__grid_version": "1.1.0"
            },
        ]

        # The numpy array prevents us comparing the whole list of dictionaries
        # in a single step, so we break it up to compare the elements.
        self.assertDictEqual(result[1], expected[1])
        self.assertEqual(
            result[0]["mosg__model_configuration"],
            expected[0]["mosg__model_configuration"],
        )
        self.assertEqual(result[0]["mosg__grid_version"],
                         expected[0]["mosg__grid_version"])
        self.assertArrayEqual(result[0]["test_attribute"],
                              expected[0]["test_attribute"])
def check_grid_match(grid_metadata_identifier, cubes):
    """
    Uses the provided grid_metadata_identifier to extract and compare
    attributes on the input cubes. The expectation is that all the metadata
    identified should match for the cubes to be deemed compatible.

    Args:
        grid_metadata_identifier (str or None):
            A partial or complete attribute name. Attributes matching this are
            compared between the two cubes. If set to None, no such check is
            made and the cubes are assumed to be suitable for use with one
            another.
        cubes (list of iris.cube.Cube items):
            List of cubes for which the attributes should be tested.
    Raises:
        ValueError: Raised if the metadata extracted is not identical on
                    all cubes.
    """
    # Allow user to bypass cube comparison by setting identifier to None.
    if grid_metadata_identifier is None:
        return

    comparison_result = compare_attributes(
        cubes, attribute_filter=grid_metadata_identifier)

    # Check that all dictionaries returned are empty, indicating matches.
    if not all(not item for item in comparison_result):
        raise ValueError('Cubes do not share the metadata identified '
                         'by the grid_metadata_identifier ({})'.format(
                             grid_metadata_identifier))
    def test_filtered_difference_three_cubes(self):
        """Test that the utility returns a list of filtered differences when
        there are more than two input cubes for comparison. In this case we
        expect the history attribute that is different to be ignored."""

        cube1 = self.cube.copy()
        cube2 = self.cube.copy()
        cube3 = self.cube.copy()
        cube1.attributes["history"] = "2017-01-18T08:59:53: StaGE Decoupler"
        cube2.attributes["mosg__model_configuration"] = "test"
        cube3.attributes["mosg__grid_version"] = "10"

        cubelist = iris.cube.CubeList([cube1, cube2, cube3])
        result = compare_attributes(cubelist, attribute_filter="mosg")
        expected = [
            {
                "mosg__model_configuration": "uk_ens",
                "mosg__grid_version": "1.2.0"
            },
            {
                "mosg__model_configuration": "test",
                "mosg__grid_version": "1.2.0"
            },
            {
                "mosg__model_configuration": "uk_ens",
                "mosg__grid_version": "10"
            },
        ]
        self.assertArrayEqual(result, expected)
Example #4
0
    def test_unhashable_types_array(self):
        """Test that the utility returns differences when unhashable attributes
        are present, e.g. a numpy array."""
        self.cube.attributes['source_realizations'] = np.array([0, 1, 2])
        cubelist = iris.cube.CubeList([self.cube, self.cube_ukv])
        result = compare_attributes(cubelist)

        expected = [{
            'mosg__model_configuration': 'uk_ens',
            'mosg__grid_version': '1.2.0',
            'source_realizations': np.array([0, 1, 2])
        }, {
            'mosg__model_configuration': 'uk_det',
            'mosg__grid_version': '1.1.0'
        }]

        # The numpy array prevents us comparing the whole list of dictionaries
        # in a single step, so we break it up to compare the elements.
        self.assertDictEqual(result[1], expected[1])
        self.assertEqual(result[0]['mosg__model_configuration'],
                         expected[0]['mosg__model_configuration'])
        self.assertEqual(result[0]['mosg__grid_version'],
                         expected[0]['mosg__grid_version'])
        self.assertArrayEqual(result[0]['source_realizations'],
                              expected[0]['source_realizations'])
 def test_warning(self, warning_list=None):
     """Test that the utility returns warning if only one cube supplied."""
     result = compare_attributes(iris.cube.CubeList([self.cube]))
     self.assertTrue(any(item.category == UserWarning for item in warning_list))
     warning_msg = "Only a single cube so no differences will be found "
     self.assertTrue(any(warning_msg in str(item) for item in warning_list))
     self.assertArrayEqual(result, [])
 def test_basic(self):
     """Test that the utility returns a list and have no differences."""
     cube1 = self.cube.copy()
     cube2 = self.cube.copy()
     cubelist = iris.cube.CubeList([cube1, cube2])
     result = compare_attributes(cubelist)
     self.assertIsInstance(result, list)
     self.assertArrayEqual(result, [{}, {}])
Example #7
0
 def test_filtered_differences(self):
     """Test that the utility returns differences only between attributes
     that match the attribute filter."""
     cubelist = iris.cube.CubeList([self.cube, self.cube_ukv])
     result = compare_attributes(cubelist, attribute_filter='mosg__grid')
     self.assertArrayEqual(result,
                           [{'mosg__grid_version': '1.2.0'},
                            {'mosg__grid_version': '1.1.0'}])
Example #8
0
 def test_multiple_differences(self):
     """Test that the utility returns multiple differences"""
     cubelist = iris.cube.CubeList([self.cube, self.cube_ukv])
     result = compare_attributes(cubelist)
     self.assertArrayEqual(result,
                           [{'mosg__model_configuration': 'uk_ens',
                             'mosg__grid_version': '1.2.0'},
                            {'mosg__model_configuration': 'uk_det',
                             'mosg__grid_version': '1.1.0'}])
Example #9
0
 def test_history_attribute(self):
     """Test that the utility returns diff when history do not match"""
     cube1 = self.cube.copy()
     cube2 = self.cube.copy()
     cube1.attributes["history"] = "2017-01-18T08:59:53: StaGE Decoupler"
     cube2.attributes["history"] = "2017-01-19T08:59:53: StaGE Decoupler"
     cubelist = iris.cube.CubeList([cube1, cube2])
     result = compare_attributes(cubelist)
     self.assertArrayEqual(result,
                           [{'history':
                             '2017-01-18T08:59:53: StaGE Decoupler'},
                            {'history':
                             '2017-01-19T08:59:53: StaGE Decoupler'}])
 def test_multiple_differences(self):
     """Test that the utility returns multiple differences"""
     cube_no_attributes = set_up_temperature_cube()
     cubelist = iris.cube.CubeList(
         [cube_no_attributes, self.cube, self.cube_ukv])
     result = compare_attributes(cubelist)
     self.assertAlmostEqual(
         result, [{}, {
             'grid_id': 'enukx_standard_v1',
             'title': 'Operational Mogreps UK Model Forecast'
         }, {
             'grid_id': 'ukvx_standard_v1',
             'title': 'Operational UKV Model Forecast'
         }])
Example #11
0
    def test_unhashable_types_list(self):
        """Test that the utility returns differences when unhashable attributes
        are present, e.g. a list."""
        self.cube.attributes['test_attribute'] = [0, 1, 2]
        cubelist = iris.cube.CubeList([self.cube, self.cube_ukv])
        result = compare_attributes(cubelist)

        expected = [
            {'mosg__model_configuration': 'uk_ens',
             'mosg__grid_version': '1.2.0',
             'test_attribute': [0, 1, 2]},
            {'mosg__model_configuration': 'uk_det',
             'mosg__grid_version': '1.1.0'}]

        self.assertArrayEqual(result, expected)
    def test_unhashable_types_list(self):
        """Test that the utility returns differences when unhashable attributes
        are present, e.g. a list."""
        self.cube.attributes["test_attribute"] = [0, 1, 2]
        cubelist = iris.cube.CubeList([self.cube, self.cube_ukv])
        result = compare_attributes(cubelist)

        expected = [
            {
                "mosg__model_configuration": "uk_ens",
                "mosg__grid_version": "1.2.0",
                "test_attribute": [0, 1, 2],
            },
            {"mosg__model_configuration": "uk_det", "mosg__grid_version": "1.1.0"},
        ]

        self.assertArrayEqual(result, expected)
Example #13
0
    def test_three_cubes(self):
        """Test that the utility returns a list of differences when there are
        more than two input cubes for comparison."""
        cube1 = self.cube.copy()
        cube2 = self.cube.copy()
        cube3 = self.cube.copy()
        cube1.attributes["history"] = "2017-01-18T08:59:53: StaGE Decoupler"
        cube2.attributes["mosg__model_configuration"] = "test"
        cube3.attributes["mosg__grid_version"] = "10"

        cubelist = iris.cube.CubeList([cube1, cube2, cube3])
        result = compare_attributes(cubelist)
        expected = [
            {'mosg__model_configuration': 'uk_ens',
             'mosg__grid_version': '1.2.0',
             'history': '2017-01-18T08:59:53: StaGE Decoupler'},
            {'mosg__model_configuration': 'test',
             'mosg__grid_version': '1.2.0'},
            {'mosg__model_configuration': 'uk_ens',
             'mosg__grid_version': '10'}]
        self.assertArrayEqual(result, expected)