def test_flag_pass_thru_multi(self): names = ["masks", "meanings", "values"] attrs = [ [("flag_masks", self.flag_masks), ("wibble", "wibble")], [ ("flag_meanings", self.flag_meanings), ("add_offset", "add_offset"), ], [("flag_values", self.flag_values)], [("valid_range", self.valid_range)], [("valid_min", self.valid_min)], [("valid_max", self.valid_max)], ] cf, cf_var = self._make(names, attrs) cube = _load_cube(self.engine, cf, cf_var, self.filename) self.assertEqual(len(cube.coords()), 3) self.assertEqual(set([c.name() for c in cube.coords()]), set(names)) expected = [ attrs[0], [attrs[1][0]], attrs[2], attrs[3], attrs[4], attrs[5], ] for name, expect in zip(names, expected): attributes = cube.coord(name).attributes self.assertEqual(set(attributes.items()), set(expect))
def test_flag_pass_thru(self): attrs = [('flag_masks', self.flag_masks), ('flag_meanings', self.flag_meanings), ('flag_values', self.flag_values)] for key, value in attrs: cf_var = self._make([(key, value)]) cube = _load_cube(self.engine, self.cf, cf_var, self.filename) self.assertEqual(len(cube.attributes), 1) self.assertEqual(list(cube.attributes.keys()), [key]) self.assertEqual(list(cube.attributes.values()), [value])
def test_flag_pass_thru_multi(self): attrs = [('flag_masks', self.flag_masks), ('wibble', 'wobble'), ('flag_meanings', self.flag_meanings), ('add_offset', 'add_offset'), ('flag_values', self.flag_values), ('standard_name', 'air_temperature')] expected = set([attrs[0], attrs[1], attrs[2], attrs[4]]) cf_var = self._make(attrs) cube = _load_cube(self.engine, self.cf, cf_var, self.filename) self.assertEqual(len(cube.attributes), len(expected)) self.assertEqual(set(cube.attributes.items()), expected)
def test_flag_pass_thru(self): items = [('masks', 'flag_masks', self.flag_masks), ('meanings', 'flag_meanings', self.flag_meanings), ('values', 'flag_values', self.flag_values)] for name, attr, value in items: names = [name] attrs = [[(attr, value)]] cf, cf_var = self._make(names, attrs) cube = _load_cube(self.engine, cf, cf_var, self.filename) self.assertEqual(len(cube.coords(name)), 1) coord = cube.coord(name) self.assertEqual(len(coord.attributes), 1) self.assertEqual(list(coord.attributes.keys()), [attr]) self.assertEqual(list(coord.attributes.values()), [value])
def test_flag_pass_thru_multi(self): names = ['masks', 'meanings', 'values'] attrs = [[('flag_masks', self.flag_masks), ('wibble', 'wibble')], [('flag_meanings', self.flag_meanings), ('add_offset', 'add_offset')], [('flag_values', self.flag_values)]] cf, cf_var = self._make(names, attrs) cube = _load_cube(self.engine, cf, cf_var, self.filename) self.assertEqual(len(cube.coords()), 3) self.assertEqual(set([c.name() for c in cube.coords()]), set(names)) expected = [attrs[0], [attrs[1][0]], attrs[2]] for name, expect in zip(names, expected): attributes = cube.coord(name).attributes self.assertEqual(set(attributes.items()), set(expect))
def test_flag_pass_thru_multi(self): attrs = [('flag_masks', self.flag_masks), ('wibble', 'wobble'), ('flag_meanings', self.flag_meanings), ('add_offset', 'add_offset'), ('flag_values', self.flag_values), ('standard_name', 'air_temperature'), ('valid_range', self.valid_range), ('valid_min', self.valid_min), ('valid_max', self.valid_max)] # Expect everything from above to be returned except those # corresponding to exclude_ind. expected = set([attrs[ind] for ind in [0, 1, 2, 4, 6, 7, 8]]) cf_var = self._make(attrs) cube = _load_cube(self.engine, self.cf, cf_var, self.filename) self.assertEqual(len(cube.attributes), len(expected)) self.assertEqual(set(cube.attributes.items()), expected)
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(self, cf_var, expected_fill_value): cube = _load_cube(self.engine, self.cf, cf_var, self.filename) self.assertEqual(cube._my_data.fill_value, expected_fill_value)
def _test(self, cf_var, expected_fill_value): cube = _load_cube(self.engine, self.cf, cf_var, self.filename) self.assertEqual(cube.fill_value, expected_fill_value)