def test_fastest_varying_vertex_dim(self):
        bounds = np.arange(12).reshape(6, 2)
        self.cf_bounds_var = mock.Mock(
            dimensions=('foo', 'nv'),
            cf_name='wibble_bnds',
            shape=bounds.shape,
            __getitem__=lambda self, key: bounds[key])

        expected_coord = DimCoord(
            self.cf_coord_var[:],
            long_name=self.cf_coord_var.long_name,
            var_name=self.cf_coord_var.cf_name,
            units=self.cf_coord_var.units,
            bounds=bounds)

        # Asserts must lie within context manager because of deferred loading.
        with self.deferred_load_patch, self.get_cf_bounds_var_patch:
            build_dimension_coordinate(self.engine, self.cf_coord_var)

            # Test that expected coord is built and added to cube.
            self.engine.cube.add_dim_coord.assert_called_with(
                expected_coord, [0])

            # Test that engine.provides container is correctly populated.
            expected_list = [(expected_coord, self.cf_coord_var.cf_name)]
            self.assertEqual(self.engine.provides['coordinates'],
                             expected_list)
 def _assert_circular(self, value):
     with self.deferred_load_patch, self.get_cf_bounds_var_patch:
         build_dimension_coordinate(self.engine, self.cf_coord_var,
                                    coord_name='longitude')
         self.assertEqual(self.engine.cube.add_aux_coord.call_count, 1)
         coord, dims = self.engine.cube.add_aux_coord.call_args[0]
     self.assertEqual(coord.circular, value)
    def test_fastest_with_different_dim_names(self):
        # Despite the dimension names 'x' differing from the coord's
        # which is 'foo' (as permitted by the cf spec),
        # this should still work because the vertex dim is the fastest varying.
        bounds = np.arange(12).reshape(6, 2)
        self.cf_bounds_var = mock.Mock(
            dimensions=('x', 'nv'),
            cf_name='wibble_bnds',
            shape=bounds.shape,
            __getitem__=lambda self, key: bounds[key])

        expected_coord = DimCoord(
            self.cf_coord_var[:],
            long_name=self.cf_coord_var.long_name,
            var_name=self.cf_coord_var.cf_name,
            units=self.cf_coord_var.units,
            bounds=bounds)

        # Asserts must lie within context manager because of deferred loading.
        with self.deferred_load_patch, self.get_cf_bounds_var_patch:
            build_dimension_coordinate(self.engine, self.cf_coord_var)

            # Test that expected coord is built and added to cube.
            self.engine.cube.add_dim_coord.assert_called_with(
                expected_coord, [0])

            # Test that engine.provides container is correctly populated.
            expected_list = [(expected_coord, self.cf_coord_var.cf_name)]
            self.assertEqual(self.engine.provides['coordinates'],
                             expected_list)
Ejemplo n.º 4
0
    def test_dim_coord_construction_masked_array(self):
        self._set_cf_coord_var(
            np.ma.array(
                np.arange(6),
                mask=[True, False, False, False, False, False],
                fill_value=-999,
            ))

        expected_coord = DimCoord(np.array([-999, 1, 2, 3, 4, 5]),
                                  long_name=self.cf_coord_var.long_name,
                                  var_name=self.cf_coord_var.cf_name,
                                  units=self.cf_coord_var.units,
                                  bounds=self.bounds)

        with warnings.catch_warnings(record=True) as w:
            # Asserts must lie within context manager because of deferred
            # loading.
            with self.deferred_load_patch, self.get_cf_bounds_var_patch:
                build_dimension_coordinate(self.engine, self.cf_coord_var)

                # Test that expected coord is built and added to cube.
                self.engine.cube.add_dim_coord.assert_called_with(
                    expected_coord, [0])

            # Assert warning is raised
            assert len(w) == 1
            assert 'Gracefully filling' in w[0].message.args[0]
    def test_slowest_varying_vertex_dim(self):
        # Create the bounds cf variable.
        bounds = np.arange(12).reshape(2, 6)
        self.cf_bounds_var = mock.Mock(
            dimensions=('nv', 'foo'),
            cf_name='wibble_bnds',
            shape=bounds.shape,
            __getitem__=lambda self, key: bounds[key])

        # Expected bounds on the resulting coordinate should be rolled so that
        # the vertex dimension is at the end.
        expected_bounds = bounds.transpose()
        expected_coord = DimCoord(
            self.cf_coord_var[:],
            long_name=self.cf_coord_var.long_name,
            var_name=self.cf_coord_var.cf_name,
            units=self.cf_coord_var.units,
            bounds=expected_bounds)

        # Asserts must lie within context manager because of deferred loading.
        with self.deferred_load_patch, self.get_cf_bounds_var_patch:
            build_dimension_coordinate(self.engine, self.cf_coord_var)

            # Test that expected coord is built and added to cube.
            self.engine.cube.add_dim_coord.assert_called_with(
                expected_coord, [0])

            # Test that engine.provides container is correctly populated.
            expected_list = [(expected_coord, self.cf_coord_var.cf_name)]
            self.assertEqual(self.engine.provides['coordinates'],
                             expected_list)
    def test_dim_coord_construction_masked_array_mask_does_nothing(self):
        self._set_cf_coord_var(np.ma.array(
            np.arange(6),
            mask=False,
        ))

        expected_coord = DimCoord(
            self.cf_coord_var[:],
            long_name=self.cf_coord_var.long_name,
            var_name=self.cf_coord_var.cf_name,
            units=self.cf_coord_var.units,
            bounds=self.bounds)

        with warnings.catch_warnings(record=True) as w:
            # Asserts must lie within context manager because of deferred
            # loading.
            with self.deferred_load_patch, self.get_cf_bounds_var_patch:
                build_dimension_coordinate(self.engine, self.cf_coord_var)

                # Test that expected coord is built and added to cube.
                self.engine.cube.add_dim_coord.assert_called_with(
                    expected_coord, [0])

            # Assert no warning is raised
            assert len(w) == 0
 def _check_circular(self, circular, *args, **kwargs):
     if 'coord_name' in kwargs:
         coord_name = kwargs.pop('coord_name')
     else:
         coord_name = 'longitude'
     self._make_vars(*args, **kwargs)
     with self.deferred_load_patch, self.get_cf_bounds_var_patch:
         build_dimension_coordinate(self.engine, self.cf_coord_var,
                                    coord_name=coord_name)
         self.assertEqual(self.engine.cube.add_dim_coord.call_count, 1)
         coord, dims = self.engine.cube.add_dim_coord.call_args[0]
     self.assertEqual(coord.circular, circular)
Ejemplo n.º 8
0
    def test_dim_coord_construction(self):
        self._set_cf_coord_var(np.arange(6))

        expected_coord = DimCoord(self.cf_coord_var[:],
                                  long_name=self.cf_coord_var.long_name,
                                  var_name=self.cf_coord_var.cf_name,
                                  units=self.cf_coord_var.units,
                                  bounds=self.bounds)

        # Asserts must lie within context manager because of deferred loading.
        with self.deferred_load_patch, self.get_cf_bounds_var_patch:
            build_dimension_coordinate(self.engine, self.cf_coord_var)

            # Test that expected coord is built and added to cube.
            self.engine.cube.add_dim_coord.assert_called_with(
                expected_coord, [0])
    def test_dim_coord_construction(self):
        self._set_cf_coord_var(np.arange(6))

        expected_coord = DimCoord(
            self.cf_coord_var[:],
            long_name=self.cf_coord_var.long_name,
            var_name=self.cf_coord_var.cf_name,
            units=self.cf_coord_var.units,
            bounds=self.bounds)

        # Asserts must lie within context manager because of deferred loading.
        with self.deferred_load_patch, self.get_cf_bounds_var_patch:
            build_dimension_coordinate(self.engine, self.cf_coord_var)

            # Test that expected coord is built and added to cube.
            self.engine.cube.add_dim_coord.assert_called_with(
                expected_coord, [0])
Ejemplo n.º 10
0
    def check_case_dim_coord_construction(self, climatology=False):
        # Test a generic dimension coordinate, with or without
        # a climatological coord.
        self.use_climatology_bounds = climatology
        self._set_cf_coord_var(np.arange(6))

        expected_coord = DimCoord(self.cf_coord_var[:],
                                  long_name=self.cf_coord_var.long_name,
                                  var_name=self.cf_coord_var.cf_name,
                                  units=self.cf_coord_var.units,
                                  bounds=self.bounds,
                                  climatological=climatology)

        # Asserts must lie within context manager because of deferred loading.
        with self.deferred_load_patch, self.get_cf_bounds_var_patch:
            build_dimension_coordinate(self.engine, self.cf_coord_var)

            # Test that expected coord is built and added to cube.
            self.engine.cube.add_dim_coord.assert_called_with(
                expected_coord, [0])
    def test_slowest_varying_vertex_dim(self):
        # Create the bounds cf variable.
        bounds = np.arange(12).reshape(2, 6)
        self.cf_bounds_var = mock.Mock(
            dimensions=('nv', 'foo'),
            cf_name='wibble_bnds',
            shape=bounds.shape,
            __getitem__=lambda self, key: bounds[key])

        # Expected bounds on the resulting coordinate should be rolled so that
        # the vertex dimension is at the end.
        expected_bounds = bounds.transpose()
        expected_coord = DimCoord(
            self.cf_coord_var[:],
            long_name=self.cf_coord_var.long_name,
            var_name=self.cf_coord_var.cf_name,
            units=self.cf_coord_var.units,
            bounds=expected_bounds)

        # Patch the helper function that retrieves the bounds cf variable.
        # This avoids the need for setting up further mocking of cf objects.
        get_cf_bounds_var_patch = mock.patch(
            'iris.fileformats._pyke_rules.compiled_krb.'
            'fc_rules_cf_fc.get_cf_bounds_var',
            return_value=self.cf_bounds_var)

        # Asserts must lie within context manager because of deferred loading.
        with self.deferred_load_patch, get_cf_bounds_var_patch:
            build_dimension_coordinate(self.engine, self.cf_coord_var)

            # Test that expected coord is built and added to cube.
            self.engine.cube.add_dim_coord.assert_called_with(
                expected_coord, [0])

            # Test that engine.provides container is correctly populated.
            expected_list = [(expected_coord, self.cf_coord_var.cf_name)]
            self.assertEqual(self.engine.provides['coordinates'],
                             expected_list)
Ejemplo n.º 12
0
    def test_aux_coord_construction(self):
        # Use non monotonically increasing coordinates to force aux coord
        # construction.
        self._set_cf_coord_var(np.array([1, 3, 2, 4, 6, 5]))

        expected_coord = AuxCoord(self.cf_coord_var[:],
                                  long_name=self.cf_coord_var.long_name,
                                  var_name=self.cf_coord_var.cf_name,
                                  units=self.cf_coord_var.units,
                                  bounds=self.bounds)

        warning_patch = mock.patch('warnings.warn')

        # Asserts must lie within context manager because of deferred loading.
        with warning_patch, self.deferred_load_patch, \
                self.get_cf_bounds_var_patch:
            build_dimension_coordinate(self.engine, self.cf_coord_var)

            # Test that expected coord is built and added to cube.
            self.engine.cube.add_aux_coord.assert_called_with(
                expected_coord, [0])
            self.assertIn("creating 'wibble' auxiliary coordinate instead",
                          warnings.warn.call_args[0][0])
    def test_slowest_varying_vertex_dim(self):
        # Create the bounds cf variable.
        bounds = np.arange(12).reshape(2, 6)
        self.cf_bounds_var = mock.Mock(
            dimensions=('nv', 'foo'),
            cf_name='wibble_bnds',
            shape=bounds.shape,
            __getitem__=lambda self, key: bounds[key])

        # Expected bounds on the resulting coordinate should be rolled so that
        # the vertex dimension is at the end.
        expected_bounds = bounds.transpose()
        expected_coord = DimCoord(self.cf_coord_var[:],
                                  long_name=self.cf_coord_var.long_name,
                                  var_name=self.cf_coord_var.cf_name,
                                  units=self.cf_coord_var.units,
                                  bounds=expected_bounds)

        # Patch the helper function that retrieves the bounds cf variable.
        # This avoids the need for setting up further mocking of cf objects.
        get_cf_bounds_var_patch = mock.patch(
            'iris.fileformats._pyke_rules.compiled_krb.'
            'fc_rules_cf_fc.get_cf_bounds_var',
            return_value=self.cf_bounds_var)

        # Asserts must lie within context manager because of deferred loading.
        with self.deferred_load_patch, get_cf_bounds_var_patch:
            build_dimension_coordinate(self.engine, self.cf_coord_var)

            # Test that expected coord is built and added to cube.
            self.engine.cube.add_dim_coord.assert_called_with(
                expected_coord, [0])

            # Test that engine.provides container is correctly populated.
            expected_list = [(expected_coord, self.cf_coord_var.cf_name)]
            self.assertEqual(self.engine.provides['coordinates'],
                             expected_list)
    def test_aux_coord_construction(self):
        # Use non monotonically increasing coordinates to force aux coord
        # construction.
        self._set_cf_coord_var(np.array([1, 3, 2, 4, 6, 5]))

        expected_coord = AuxCoord(
            self.cf_coord_var[:],
            long_name=self.cf_coord_var.long_name,
            var_name=self.cf_coord_var.cf_name,
            units=self.cf_coord_var.units,
            bounds=self.bounds)

        warning_patch = mock.patch('warnings.warn')

        # Asserts must lie within context manager because of deferred loading.
        with warning_patch, self.deferred_load_patch, \
                self.get_cf_bounds_var_patch:
            build_dimension_coordinate(self.engine, self.cf_coord_var)

            # Test that expected coord is built and added to cube.
            self.engine.cube.add_aux_coord.assert_called_with(
                expected_coord, [0])
            self.assertIn("creating 'wibble' auxiliary coordinate instead",
                          warnings.warn.call_args[0][0])