def test_promoted_auxiliary_ignore(self): self.wibble = netcdf_variable('wibble', 'lat wibble', np.float) self.variables['wibble'] = self.wibble self.orography.coordinates = 'wibble' with mock.patch('netCDF4.Dataset', return_value=self.dataset), \ mock.patch('warnings.warn') as warn: cf_group = CFReader('dummy').cf_group.promoted promoted = ['wibble', 'orography'] self.assertEqual(set(cf_group.keys()), set(promoted)) for name in promoted: self.assertIs(cf_group[name].cf_data, getattr(self, name)) self.assertEqual(warn.call_count, 2)
def test_promoted_auxiliary_ignore(self): self.wibble = netcdf_variable("wibble", "lat wibble", np.float64) self.variables["wibble"] = self.wibble self.orography.coordinates = "wibble" with mock.patch("netCDF4.Dataset", return_value=self.dataset), mock.patch( "warnings.warn") as warn: cf_group = CFReader("dummy").cf_group.promoted promoted = ["wibble", "orography"] self.assertEqual(set(cf_group.keys()), set(promoted)) for name in promoted: self.assertIs(cf_group[name].cf_data, getattr(self, name)) self.assertEqual(warn.call_count, 2)
def test_create_formula_terms(self): with mock.patch("netCDF4.Dataset", return_value=self.dataset): cf_group = CFReader("dummy").cf_group self.assertEqual(len(cf_group), len(self.variables)) # Check there is a singular data variable. group = cf_group.data_variables self.assertEqual(len(group), 1) self.assertEqual(list(group.keys()), ["temp"]) self.assertIs(group["temp"].cf_data, self.temp) # Check there are three coordinates. group = cf_group.coordinates self.assertEqual(len(group), 3) coordinates = ["height", "lat", "lon"] self.assertEqual(set(group.keys()), set(coordinates)) for name in coordinates: self.assertIs(group[name].cf_data, getattr(self, name)) # Check there are three auxiliary coordinates. group = cf_group.auxiliary_coordinates self.assertEqual(len(group), 3) aux_coordinates = ["delta", "sigma", "orography"] self.assertEqual(set(group.keys()), set(aux_coordinates)) for name in aux_coordinates: self.assertIs(group[name].cf_data, getattr(self, name)) # Check all the auxiliary coordinates are formula terms. formula_terms = cf_group.formula_terms self.assertEqual(set(group.items()), set(formula_terms.items())) # Check there are three bounds. group = cf_group.bounds self.assertEqual(len(group), 3) bounds = ["height_bnds", "delta_bnds", "sigma_bnds"] self.assertEqual(set(group.keys()), set(bounds)) for name in bounds: self.assertEqual(group[name].cf_data, getattr(self, name))
def test_create_formula_terms(self): with mock.patch('netCDF4.Dataset', return_value=self.dataset): cf_group = CFReader('dummy').cf_group self.assertEqual(len(cf_group), len(self.variables)) # Check there is a singular data variable. group = cf_group.data_variables self.assertEqual(len(group), 1) self.assertEqual(group.keys(), ['temp']) self.assertIs(group['temp'].cf_data, self.temp) # Check there are three coordinates. group = cf_group.coordinates self.assertEqual(len(group), 3) self.assertEqual(group.viewkeys(), set(['height', 'lat', 'lon'])) self.assertIs(group['height'].cf_data, self.height) self.assertIs(group['lat'].cf_data, self.lat) self.assertIs(group['lon'].cf_data, self.lon) # Check there are three auxiliary coordinates. group = cf_group.auxiliary_coordinates self.assertEqual(len(group), 3) self.assertEqual(group.viewkeys(), set(['delta', 'sigma', 'orography'])) self.assertIs(group['delta'].cf_data, self.delta) self.assertIs(group['sigma'].cf_data, self.sigma) self.assertIs(group['orography'].cf_data, self.orography) # Check all the auxiliary coordinates are formula terms. formula_terms = cf_group.formula_terms self.assertEqual(group.viewitems(), formula_terms.viewitems())
def test_associate_formula_terms_with_data_variable(self): with mock.patch('netCDF4.Dataset', return_value=self.dataset): cf_group = CFReader('dummy').cf_group self.assertEqual(len(cf_group), len(self.variables)) # Check the cf-group associated with the data variable. temp_cf_group = cf_group['temp'].cf_group # Check the data variable is associated with six variables. self.assertEqual(len(temp_cf_group), 6) # Check there are three coordinates. group = temp_cf_group.coordinates self.assertEqual(len(group), 3) self.assertEqual(group.viewkeys(), set(['height', 'lat', 'lon'])) self.assertIs(group['height'].cf_data, self.height) self.assertIs(group['lat'].cf_data, self.lat) self.assertIs(group['lon'].cf_data, self.lon) # Check there are three auxiliary coordinates. group = temp_cf_group.auxiliary_coordinates self.assertEqual(len(group), 3) self.assertEqual(group.viewkeys(), set(['delta', 'sigma', 'orography'])) self.assertIs(group['delta'].cf_data, self.delta) self.assertIs(group['sigma'].cf_data, self.sigma) self.assertIs(group['orography'].cf_data, self.orography) # Check all the auxiliary coordinates are formula terms. formula_terms = cf_group.formula_terms self.assertEqual(group.viewitems(), formula_terms.viewitems()) # Check the terms by root. self.assertEqual(formula_terms['delta'].cf_terms_by_root, dict(height='a')) self.assertEqual(formula_terms['sigma'].cf_terms_by_root, dict(height='b')) self.assertEqual(formula_terms['orography'].cf_terms_by_root, dict(height='orog'))
def test_create_formula_terms(self): with mock.patch('netCDF4.Dataset', return_value=self.dataset): cf_group = CFReader('dummy').cf_group self.assertEqual(len(cf_group), len(self.variables)) # Check there is a singular data variable. group = cf_group.data_variables self.assertEqual(len(group), 1) self.assertEqual(list(group.keys()), ['temp']) self.assertIs(group['temp'].cf_data, self.temp) # Check there are three coordinates. group = cf_group.coordinates self.assertEqual(len(group), 3) coordinates = ['height', 'lat', 'lon'] self.assertEqual(set(group.keys()), set(coordinates)) for name in coordinates: self.assertIs(group[name].cf_data, getattr(self, name)) # Check there are three auxiliary coordinates. group = cf_group.auxiliary_coordinates self.assertEqual(len(group), 3) aux_coordinates = ['delta', 'sigma', 'orography'] self.assertEqual(set(group.keys()), set(aux_coordinates)) for name in aux_coordinates: self.assertIs(group[name].cf_data, getattr(self, name)) # Check all the auxiliary coordinates are formula terms. formula_terms = cf_group.formula_terms self.assertEqual(set(group.items()), set(formula_terms.items())) # Check there are three bounds. group = cf_group.bounds self.assertEqual(len(group), 3) bounds = ['height_bnds', 'delta_bnds', 'sigma_bnds'] self.assertEqual(set(group.keys()), set(bounds)) for name in bounds: self.assertEqual(group[name].cf_data, getattr(self, name))
def test_formula_terms_ignore(self): self.orography.dimensions = ['lat', 'wibble'] with mock.patch('netCDF4.Dataset', return_value=self.dataset), \ mock.patch('warnings.warn') as warn: cf_group = CFReader('dummy').cf_group group = cf_group.promoted self.assertEqual(list(group.keys()), ['orography']) self.assertIs(group['orography'].cf_data, self.orography) self.assertEqual(warn.call_count, 1)
def test_formula_terms_ignore(self): self.orography.dimensions = ["lat", "wibble"] with mock.patch("netCDF4.Dataset", return_value=self.dataset), mock.patch( "warnings.warn") as warn: cf_group = CFReader("dummy").cf_group group = cf_group.promoted self.assertEqual(list(group.keys()), ["orography"]) self.assertIs(group["orography"].cf_data, self.orography) self.assertEqual(warn.call_count, 1)
def test_auxiliary_ignore(self): self.x.dimensions = ['lat', 'wibble'] with mock.patch('netCDF4.Dataset', return_value=self.dataset), \ mock.patch('warnings.warn') as warn: cf_group = CFReader('dummy').cf_group promoted = ['x', 'orography'] group = cf_group.promoted self.assertEqual(set(group.keys()), set(promoted)) for name in promoted: self.assertIs(group[name].cf_data, getattr(self, name)) self.assertEqual(warn.call_count, 1)
def test_auxiliary_ignore(self): self.x.dimensions = ["lat", "wibble"] with mock.patch("netCDF4.Dataset", return_value=self.dataset), mock.patch( "warnings.warn") as warn: cf_group = CFReader("dummy").cf_group promoted = ["x", "orography"] group = cf_group.promoted self.assertEqual(set(group.keys()), set(promoted)) for name in promoted: self.assertIs(group[name].cf_data, getattr(self, name)) self.assertEqual(warn.call_count, 1)
def test_formula_terms_ignore(self): self.orography.dimensions = ['lat', 'wibble'] for state in [False, True]: with mock.patch('netCDF4.Dataset', return_value=self.dataset), \ iris.FUTURE.context(netcdf_promote=state), \ mock.patch('warnings.warn') as warn: cf_group = CFReader('dummy').cf_group if state: group = cf_group.promoted self.assertEqual(list(group.keys()), ['orography']) self.assertIs(group['orography'].cf_data, self.orography) else: self.assertEqual(len(cf_group.promoted), 0) self.assertEqual(warn.call_count, 1)
def test_auxiliary_ignore(self): self.x.dimensions = ['lat', 'wibble'] for state in [False, True]: with mock.patch('netCDF4.Dataset', return_value=self.dataset), \ iris.FUTURE.context(netcdf_promote=state), \ mock.patch('warnings.warn') as warn: cf_group = CFReader('dummy').cf_group if state: promoted = ['x', 'orography'] group = cf_group.promoted self.assertEqual(group.viewkeys(), set(promoted)) for name in promoted: self.assertIs(group[name].cf_data, getattr(self, name)) else: self.assertEqual(len(cf_group.promoted), 0) self.assertEqual(warn.call_count, 1)
def test_promote_reference(self): with mock.patch('netCDF4.Dataset', return_value=self.dataset): cf_group = CFReader('dummy').cf_group self.assertEqual(len(cf_group), len(self.variables)) # Check the number of data variables. self.assertEqual(len(cf_group.data_variables), 1) self.assertEqual(list(cf_group.data_variables.keys()), ['temp']) # Check the number of promoted variables. self.assertEqual(len(cf_group.promoted), 1) self.assertEqual(list(cf_group.promoted.keys()), ['orography']) # Check the promoted variable dependencies. group = cf_group.promoted['orography'].cf_group.coordinates self.assertEqual(len(group), 2) coordinates = ('lat', 'lon') self.assertEqual(set(group.keys()), set(coordinates)) for name in coordinates: self.assertIs(group[name].cf_data, getattr(self, name))
def test_associate_formula_terms_with_data_variable(self): with mock.patch("netCDF4.Dataset", return_value=self.dataset): cf_group = CFReader("dummy").cf_group self.assertEqual(len(cf_group), len(self.variables)) # Check the cf-group associated with the data variable. temp_cf_group = cf_group["temp"].cf_group # Check the data variable is associated with eight variables. self.assertEqual(len(temp_cf_group), 8) # Check there are three coordinates. group = temp_cf_group.coordinates self.assertEqual(len(group), 3) coordinates = ["height", "lat", "lon"] self.assertEqual(set(group.keys()), set(coordinates)) for name in coordinates: self.assertIs(group[name].cf_data, getattr(self, name)) # Check the height coordinate is bounded. group = group["height"].cf_group self.assertEqual(len(group.bounds), 1) self.assertIn("height_bnds", group.bounds) self.assertIs(group["height_bnds"].cf_data, self.height_bnds) # Check there are five auxiliary coordinates. group = temp_cf_group.auxiliary_coordinates self.assertEqual(len(group), 5) aux_coordinates = ["delta", "sigma", "orography", "x", "y"] self.assertEqual(set(group.keys()), set(aux_coordinates)) for name in aux_coordinates: self.assertIs(group[name].cf_data, getattr(self, name)) # Check all the auxiliary coordinates are formula terms. formula_terms = cf_group.formula_terms self.assertTrue( set(formula_terms.items()).issubset(list(group.items()))) # Check the terms by root. for name, term in zip(aux_coordinates, ["a", "b", "orog"]): self.assertEqual(formula_terms[name].cf_terms_by_root, dict(height=term)) # Check the bounded auxiliary coordinates. for name, name_bnds in zip(["delta", "sigma"], ["delta_bnds", "sigma_bnds"]): aux_coord_group = group[name].cf_group self.assertEqual(len(aux_coord_group.bounds), 1) self.assertIn(name_bnds, aux_coord_group.bounds) self.assertIs( aux_coord_group[name_bnds].cf_data, getattr(self, name_bnds), )
def load_cube_from_cdl(self, cdl_string, cdl_path, nc_path): """ Load the 'phenom' data variable in a CDL testcase, as a cube. Using ncgen, CFReader and the _load_cube call. """ # Write the CDL to a file. with open(cdl_path, "w") as f_out: f_out.write(cdl_string) # Create a netCDF file from the CDL file. command = "ncgen -o {} {}".format(nc_path, cdl_path) subprocess.check_call(command, shell=True) # Simulate the inner part of the file reading process. cf = CFReader(nc_path) # Grab a data variable : FOR NOW always grab the 'phenom' variable. cf_var = cf.cf_group.data_variables["phenom"] engine = iris.fileformats.netcdf._actions_engine() # If debug enabled, switch on the activation summary debug output. # Use 'patch' so it is restored after the test. self.patch("iris.fileformats.netcdf.DEBUG", self.debug) # Call the main translation function to load a single cube. # _load_cube establishes per-cube facts, activates rules and # produces an actual cube. cube = _load_cube(engine, cf, cf_var, nc_path) # Also Record, on the cubes, which hybrid coord elements were identified # by the rules operation. # Unlike the other translations, _load_cube does *not* convert this # information into actual cube elements. That is instead done by # `iris.fileformats.netcdf._load_aux_factory`. # For rules testing, it is anyway more convenient to deal with the raw # data, as each factory type has different validity requirements to # build it, and none of that is relevant to the rules operation. cube._formula_type_name = engine.requires.get("formula_type") cube._formula_terms_byname = engine.requires.get("formula_terms") # Always returns a single cube. return cube
def test_associate_formula_terms_with_data_variable(self): with mock.patch('netCDF4.Dataset', return_value=self.dataset): cf_group = CFReader('dummy').cf_group self.assertEqual(len(cf_group), len(self.variables)) # Check the cf-group associated with the data variable. temp_cf_group = cf_group['temp'].cf_group # Check the data variable is associated with eight variables. self.assertEqual(len(temp_cf_group), 8) # Check there are three coordinates. group = temp_cf_group.coordinates self.assertEqual(len(group), 3) coordinates = ['height', 'lat', 'lon'] self.assertEqual(set(group.keys()), set(coordinates)) for name in coordinates: self.assertIs(group[name].cf_data, getattr(self, name)) # Check the height coordinate is bounded. group = group['height'].cf_group self.assertEqual(len(group.bounds), 1) self.assertIn('height_bnds', group.bounds) self.assertIs(group['height_bnds'].cf_data, self.height_bnds) # Check there are five auxiliary coordinates. group = temp_cf_group.auxiliary_coordinates self.assertEqual(len(group), 5) aux_coordinates = ['delta', 'sigma', 'orography', 'x', 'y'] self.assertEqual(set(group.keys()), set(aux_coordinates)) for name in aux_coordinates: self.assertIs(group[name].cf_data, getattr(self, name)) # Check all the auxiliary coordinates are formula terms. formula_terms = cf_group.formula_terms self.assertTrue( set(formula_terms.items()).issubset(list(group.items()))) # Check the terms by root. for name, term in zip(aux_coordinates, ['a', 'b', 'orog']): self.assertEqual(formula_terms[name].cf_terms_by_root, dict(height=term)) # Check the bounded auxiliary coordinates. for name, name_bnds in zip(['delta', 'sigma'], ['delta_bnds', 'sigma_bnds']): aux_coord_group = group[name].cf_group self.assertEqual(len(aux_coord_group.bounds), 1) self.assertIn(name_bnds, aux_coord_group.bounds) self.assertIs(aux_coord_group[name_bnds].cf_data, getattr(self, name_bnds))
def test_create_global_attributes(self): with mock.patch('netCDF4.Dataset', return_value=self.dataset): global_attrs = CFReader('dummy').cf_group.global_attributes self.assertEqual(global_attrs['dimensions'], 'something something_else')
def test_create_global_attributes(self): with mock.patch("netCDF4.Dataset", return_value=self.dataset): global_attrs = CFReader("dummy").cf_group.global_attributes self.assertEqual(global_attrs["dimensions"], "something something_else")