def test_fastest_with_different_dim_names(self): # Despite the dimension names ('x', and 'y') differing from the coord's # which are 'foo' and 'bar' (as permitted by the cf spec), # this should still work because the vertex dim is the fastest varying. bounds = np.arange(24).reshape(2, 3, 4) self.cf_bounds_var = mock.Mock( dimensions=('x', 'y', 'nv'), cf_name='wibble_bnds', shape=bounds.shape, dtype=bounds.dtype, __getitem__=lambda self, key: bounds[key]) 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=bounds) 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_auxiliary_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, 1]) # 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_fastest_varying_vertex_dim(self): bounds = np.arange(24).reshape(2, 3, 4) self.cf_bounds_var = mock.Mock( dimensions=('foo', 'bar', 'nv'), cf_name='wibble_bnds', shape=bounds.shape, dtype=bounds.dtype, __getitem__=lambda self, key: bounds[key]) 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=bounds) 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_auxiliary_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, 1]) # 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_fastest_varying_vertex_dim(self): bounds = np.arange(24).reshape(2, 3, 4) cf_data = self._make_cf_data(bounds) self.cf_bounds_var = mock.Mock( spec=CFVariable, dimensions=('foo', 'bar', 'nv'), cf_name='wibble_bnds', cf_data=cf_data, shape=bounds.shape, dtype=bounds.dtype, __getitem__=lambda self, key: bounds[key]) 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=bounds) 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_auxiliary_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, 1]) # 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_fastest_with_different_dim_names(self): # Despite the dimension names ('x', and 'y') differing from the coord's # which are 'foo' and 'bar' (as permitted by the cf spec), # this should still work because the vertex dim is the fastest varying. bounds = np.arange(24).reshape(2, 3, 4) self.cf_bounds_var = mock.Mock( dimensions=('x', 'y', 'nv'), cf_name='wibble_bnds', shape=bounds.shape, __getitem__=lambda self, key: bounds[key]) 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=bounds) 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_auxiliary_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, 1]) # 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_add_offset_float(self): self.cf_coord_var.add_offset = 5. with self.deferred_load_patch: build_auxiliary_coordinate(self.engine, self.cf_coord_var) coord, _ = self.engine.provides['coordinates'][0] self.assertEqual(coord.dtype.kind, 'f')
def test_scale_factor_float(self): self.cf_coord_var.scale_factor = 3. with self.deferred_load_patch: build_auxiliary_coordinate(self.engine, self.cf_coord_var) coord, _ = self.engine.provides['coordinates'][0] self.assertEqual(coord.dtype.kind, 'f')
def test_scale_factor_add_offset_int(self): self.cf_coord_var.scale_factor = 3 self.cf_coord_var.add_offset = 5 with self.deferred_load_patch: build_auxiliary_coordinate(self.engine, self.cf_coord_var) coord, _ = self.engine.cube_parts['coordinates'][0] self.assertEqual(coord.dtype.kind, 'i')
def _check_case(self, dimension_names): bounds, self.cf_bounds_var = self._make_cf_bounds_var( dimension_names=dimension_names) # Asserts must lie within context manager because of deferred loading. build_auxiliary_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( self.expected_coord, [0, 1]) # Test that engine.provides container is correctly populated. expected_list = [(self.expected_coord, self.cf_coord_var.cf_name)] self.assertEqual(self.engine.provides['coordinates'], expected_list)
def check_case_aux_coord_construction(self, climatology=False): # Test a generic auxiliary coordinate, with or without # a climatological coord. self.use_climatology_bounds = climatology 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, climatological=climatology) build_auxiliary_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])
def test_slowest_varying_vertex_dim(self): # Create the bounds cf variable. bounds = np.arange(24).reshape(4, 2, 3) cf_data = self._make_cf_data(bounds) self.cf_bounds_var = mock.Mock( spec=CFVariable, dimensions=('nv', 'foo', 'bar'), cf_name='wibble_bnds', cf_data=cf_data, shape=bounds.shape, dtype=bounds.dtype, __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 = np.rollaxis(bounds, 0, bounds.ndim) 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=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_auxiliary_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, 1]) # 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_slowest_varying_vertex_dim(self): # Create the bounds cf variable. bounds = np.arange(24).reshape(4, 2, 3) cf_data = self._make_cf_data(bounds) self.cf_bounds_var = mock.Mock( spec=CFVariable, dimensions=('nv', 'foo', 'bar'), cf_name='wibble_bnds', cf_data=cf_data, shape=bounds.shape, dtype=bounds.dtype, __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 = np.rollaxis(bounds, 0, bounds.ndim) 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=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_auxiliary_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, 1]) # 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)