def test_shared(self): # Check that saving cubes with matching ancillaries maps to a shared # file variable. testcube_1 = stock.realistic_3d() ancil = iris.coords.AncillaryVariable(np.zeros(testcube_1[0].shape), long_name="latlon_refs", units="s") testcube_1.add_ancillary_variable(ancil, (1, 2)) testcube_2 = testcube_1.copy() testcube_2.units = "m" testcube_2.rename("alternate_data") with self.temp_filename(suffix=".nc") as filename: iris.save([testcube_1, testcube_2], filename) self.assertCDL(filename) # Also check that only one, shared ancillary variable was written. ds = nc.Dataset(filename) self.assertIn("air_potential_temperature", ds.variables) self.assertIn("alternate_data", ds.variables) self.assertEqual( ds.variables["air_potential_temperature"].ancillary_variables, "latlon_refs", ) self.assertEqual( ds.variables["alternate_data"].ancillary_variables, "latlon_refs", )
def setUp(self): cube = realistic_3d() axes = tests.mock.sentinel.axes self.draw = tests.mock.sentinel.draw self.patcher = self.patch('cube_browser.Plot2D.draw', return_value=self.draw) self.plot = Plot2D(cube, axes)
def setUp(self): self.cube = realistic_3d() self.axes = mock.sentinel.axes self.patch('IPython.display.display') self.patch('ipywidgets.SelectionSlider') self.patch('ipywidgets.VBox') self.patch('cube_browser.Browser.on_change')
def setUp(self): super(TestCubeLoader, self).setUp() abs_path = os.path.dirname(os.path.abspath(__file__)) self.tmp_dir_time = abs_path + '/' + 'tmp_dir_time/' self.tmp_dir = abs_path + '/' + 'tmp_dir/' if not os.path.exists(self.tmp_dir_time): os.mkdir(self.tmp_dir_time) if not os.path.exists(self.tmp_dir): os.mkdir(self.tmp_dir) base_cube = stock.realistic_3d() cube_1 = base_cube[0:2] cube_2 = base_cube[2:4] cube_3 = base_cube[4:] self.temp_1 = 'temp_1.nc' self.temp_2 = 'temp_2.nc' self.temp_3 = 'temp_3.nc' iris.save(cube_1, self.tmp_dir + self.temp_1) iris.save(cube_2, self.tmp_dir + self.temp_2) iris.save(cube_3, self.tmp_dir + self.temp_3) cube_1 = base_cube[0:2] cube_2 = base_cube[2:4] cube_3 = base_cube[4:] new_time = cf_units.Unit('hours since 1980-01-01 00:00:00', 'gregorian') cube_2.dim_coords[0].convert_units(new_time) new_time = cf_units.Unit('hours since 1990-01-01 00:00:00', 'gregorian') cube_3.dim_coords[0].convert_units(new_time) self.temp_1_time = 'temp_1_time.nc' self.temp_2_time = 'temp_2_time.nc' self.temp_3_time = 'temp_3_time.nc' iris.save(cube_1, self.tmp_dir_time + self.temp_1_time) iris.save(cube_2, self.tmp_dir_time + self.temp_2_time) iris.save(cube_3, self.tmp_dir_time + self.temp_3_time)
def test(self): cube = realistic_3d() axes = tests.mock.sentinel.axes plot = Plot2D(cube, axes) emsg = 'requires a draw method for rendering' with self.assertRaisesRegexp(NotImplementedError, emsg): plot(time=0)
def setUp(self): self.cube = realistic_3d() self.axes = mock.sentinel.axes self.patch('IPython.display.display') self.value = mock.sentinel.value mockers = [mock.Mock(value=self.value) for i in range(20)] self.patch('ipywidgets.SelectionSlider', side_effect=mockers) self.patch('ipywidgets.VBox')
def setUp(self): self.cube = realistic_3d() self.axes = mock.sentinel.axes self.patch('IPython.display.display') self.value = mock.sentinel.value mockers = [mock.Mock(value=self.value) for i in range(20)] self.patch('ipywidgets.SelectionSlider', side_effect=mockers) self.patch('ipywidgets.VBox') self.patch('matplotlib.pyplot.colorbar')
def test_is_fix_needed_fails_latitude(self): cube = realistic_3d() cube.attributes['mip_era'] = 'CMIP6' cube.attributes['institution_id'] = 'FIO-QLNM' cube.attributes['source_id'] = 'FIO-ESM-2-0' for coord_name in ['latitude', 'longitude']: coord = cube.coord('grid_' + coord_name) coord.standard_name = coord_name fix = ch.fix_known.FixCmip6FioqlnmFioesm20Latitude(cube) self.assertFalse(fix.is_fix_needed())
def setUp(self): self.cube = realistic_3d() self.axes = mock.sentinel.axes self.patch('IPython.display.display') self.value = mock.sentinel.value mockers = [mock.Mock(value=self.value) for i in range(20)] self.patch('ipywidgets.SelectionSlider', side_effect=mockers) self.patch('ipywidgets.VBox') self.patch('ipywidgets.HBox') self.patch('ipywidgets.Label') self.patch('matplotlib.pyplot.colorbar') self.patch('cube_browser.Plot2D.legend')
def test_partialdims(self): # Test saving ancillary data which maps only dims 0 and 2. testcube = stock.realistic_3d() ancil = iris.coords.AncillaryVariable( np.zeros(testcube[:, 0, :].shape), long_name="time_lon_values", units="m", ) testcube.add_ancillary_variable(ancil, (0, 2)) with self.temp_filename(suffix=".nc") as filename: iris.save(testcube, filename) self.assertCDL(filename)
def test_separate_cache(self): c1 = Contour(self.cube, self.axes) other_cube = realistic_3d() c2 = Contour(other_cube, self.axes) browser = Browser([c1, c2]) lookup = browser._cache_by_cube_id cube_id = id(self.cube) other_id = id(other_cube) expected = set([cube_id, other_id]) self.assertEqual(set(lookup.keys()), expected) self.assertNotEqual(id(c1.cache), id(c2.cache)) self.assertEqual(id(lookup[cube_id]), id(c1.cache)) self.assertEqual(id(lookup[other_id]), id(c2.cache))
def setUp(self): self.cube = realistic_3d() self.cube.attributes['institution_id'] = 'ABCD' self.expected = { 'var_name': None, 'units': 'K', 'long_name': None, 'standard_name': 'air_potential_temperature', 'time_units': 'hours since 1970-01-01 00:00:00', 'calendar': 'gregorian', 'activity_id': 'HighResMIP', 'institute': 'ABCD' }
def _get_single_time_cube(set_time_mean=False): cube = stock.realistic_3d()[0:1, :, :] cube.remove_coord('time') cube.remove_coord('forecast_period') tc = DimCoord( points=[15, ], standard_name='time', units=cf_units.Unit('days since epoch', calendar='360_day'), bounds=[[0, 30], ], ) cube.add_dim_coord(tc, 0) if set_time_mean: cube.cell_methods = (CellMethod("mean", coords='time'), ) return cube
def _get_single_time_cube(set_time_mean=False): cube = stock.realistic_3d()[0:1, :, :] cube.remove_coord("time") cube.remove_coord("forecast_period") tc = DimCoord( points=[15], standard_name="time", units=cf_units.Unit("days since epoch", calendar="360_day"), bounds=[[0, 30]], ) cube.add_dim_coord(tc, 0) if set_time_mean: cube.cell_methods = (CellMethod("mean", coords="time"), ) return cube
def setUp(self): self.cube = realistic_3d() self.metadata_1 = { 'basename': 'file.nc', 'start_date': PartialDateTime(year=2014, month=12), 'end_date': PartialDateTime(year=2014, month=12), 'frequency': 'mon' } self.metadata_2 = { 'basename': 'file.nc', 'start_date': PartialDateTime(year=2014, month=11), 'end_date': PartialDateTime(year=2014, month=12), 'frequency': 'mon' } self.metadata_3 = { 'basename': 'file.nc', 'start_date': PartialDateTime(year=2013, month=12), 'end_date': PartialDateTime(year=2014, month=12), 'frequency': 'mon' } self.metadata_4 = { 'basename': 'file.nc', 'start_date': PartialDateTime(year=2014, month=12), 'end_date': PartialDateTime(year=2015, month=9), 'frequency': 'mon' } self.metadata_5 = { 'basename': 'file-clim.nc', 'start_date': PartialDateTime(year=2014, month=12, day=20), 'end_date': PartialDateTime(year=2014, month=12, day=22), 'frequency': 'mon' } self.metadata_6 = { 'basename': 'file-clim.nc', 'start_date': PartialDateTime(year=2014, month=12, day=21), 'end_date': PartialDateTime(year=2014, month=12, day=22), 'frequency': 'mon' } self.metadata_high_freq = { 'basename': 'file.nc', 'start_date': PartialDateTime(year=2014, month=12, day=21, hour=0, minute=0), 'end_date': PartialDateTime(year=2014, month=12, day=22, hour=12, minute=0), 'frequency': '6hr' }
def test_fulldims(self): testcube = stock.realistic_3d() ancil = iris.coords.AncillaryVariable( np.zeros(testcube.shape), long_name="ancil_data", units=1, attributes={ "attr_1": 7, "attr_2": "chat" }, ) testcube.add_ancillary_variable(ancil, (0, 1, 2)) with self.temp_filename(suffix=".nc") as filename: iris.save(testcube, filename) self.assertCDL(filename)
def test_aliases(self): # Check that saving cubes with *differing* ancillaries of the same name # is correctly resolved. testcube_1 = stock.realistic_3d() testcube_2 = testcube_1.copy() testcube_2.units = "m" testcube_2.rename("alternate_data") ancil1 = iris.coords.AncillaryVariable(np.zeros(testcube_1[0].shape), long_name="latlon_refs", units="s") testcube_1.add_ancillary_variable(ancil1, (1, 2)) ancil2 = ancil1.copy() ancil2.data[0, 0] += 1.0 testcube_2.add_ancillary_variable(ancil2, (1, 2)) with self.temp_filename(suffix=".nc") as filename: iris.save([testcube_1, testcube_2], filename) self.assertCDL(filename)
def test_multiple(self): # Test saving with multiple ancillary variables. testcube = stock.realistic_3d() ancil1_time_lat_lon = iris.coords.AncillaryVariable( np.zeros(testcube.shape), long_name="data_values", units=1) testcube.add_ancillary_variable(ancil1_time_lat_lon, (0, 1, 2)) ancil2_time = iris.coords.AncillaryVariable( np.zeros(testcube[:, 0, 0].shape), long_name="time_values", units="s", ) testcube.add_ancillary_variable(ancil2_time, 0) ancil3_lon = iris.coords.AncillaryVariable( np.zeros(testcube[0, 0, :].shape), long_name="lon_values", units="m", ) testcube.add_ancillary_variable(ancil3_lon, 2) with self.temp_filename(suffix=".nc") as filename: iris.save(testcube, filename) self.assertCDL(filename)
def _make_fioesm20_cube(): """ Use an Iris test cube and modify the metadata and bounds to make it look like a CMIP6.CMIP.FIO-QLNM.FIO-ESM-2-0.historical cube, which has a non-monotonic latitude coordinate that is an auxillary coordinate rather than a dimension coordinate. Returns: An `Iris.cube.Cube` object that looks like a cube loaded from file for the CMIP6.CMIP.FIO-QLNM.FIO-ESM-2-0.historical experiment. """ cube = realistic_3d() cube.attributes['mip_era'] = 'CMIP6' cube.attributes['institution_id'] = 'FIO-QLNM' cube.attributes['source_id'] = 'FIO-ESM-2-0' for coord_name in ['latitude', 'longitude']: coord = cube.coord('grid_' + coord_name) coord.standard_name = coord_name iris.util.demote_dim_coord_to_aux_coord(cube, 'latitude') # Cannot set invalid bounds because the setter checks them and so # construct new coordinate instead and replace the existing latitude latitude = cube.coord('latitude') point_spacing = latitude.points[1] - latitude.points[0] bounds = (np.array([latitude.points, latitude.points + point_spacing]).transpose()) bounds[-1, 1] = latitude.points[-1] new_latitude = iris.coords.AuxCoord(latitude.points, standard_name=latitude.standard_name, long_name=latitude.long_name, var_name=latitude.var_name, units=latitude.units, bounds=bounds, attributes=latitude.attributes, coord_system=latitude.coord_system) cube.replace_coord(new_latitude) return cube
def _make_fgoals_cube(): """ Use an Iris test cube and modify the metadata and bounds to make it look like a CMIP6.CAS.FGOALS-f3-L cube. The FGOALS-f3-L bounds are not contiguous. Returns: An `Iris.cube.Cube` object that looks like a cube loaded from file for the CMIP6.CAS.FGOALS-f3-L model. """ cube = realistic_3d() cube.attributes['mip_era'] = 'CMIP6' cube.attributes['institution_id'] = 'CAS' cube.attributes['source_id'] = 'FGOALS-f3-L' for coord_name in ['latitude', 'longitude']: coord = cube.coord('grid_' + coord_name) coord.standard_name = coord_name point_spacing = coord.points[1] - coord.points[0] coord.bounds = (np.array([ coord.points - (0.4 * point_spacing), coord.points + (0.4 * point_spacing) ]).transpose()) return cube
def setUp(self): super(TestCubeHelp, self).setUp() abs_path = os.path.dirname(os.path.abspath(__file__)) self.tmp_dir_time = abs_path + '/' + 'tmp_dir_time/' if not os.path.exists(self.tmp_dir_time): os.mkdir(self.tmp_dir_time) self.tmp_dir_ocean = abs_path + '/' + 'tmp_dir_ocean/' if not os.path.exists(self.tmp_dir_ocean): os.mkdir(self.tmp_dir_ocean) base_cube = stock.realistic_3d() cube_1 = base_cube[0:2] cube_2 = base_cube[2:4] cube_3 = base_cube[4:] new_time = cf_units.Unit('hours since 1980-01-01 00:00:00', 'gregorian') cube_2.dim_coords[0].convert_units(new_time) new_time = cf_units.Unit('hours since 1990-01-01 00:00:00', 'gregorian') cube_3.dim_coords[0].convert_units(new_time) self.temp_1_time = 'temp_1_time.nc' self.temp_2_time = 'temp_2_time.nc' self.temp_3_time = 'temp_3_time.nc' iris.save(cube_1, self.tmp_dir_time + self.temp_1_time) iris.save(cube_2, self.tmp_dir_time + self.temp_2_time) iris.save(cube_3, self.tmp_dir_time + self.temp_3_time) base_ocean_cube = common._generate_ocean_cube() base_ocean_cube = base_ocean_cube.concatenate_cube() cube_1 = base_ocean_cube[0:10] cube_2 = base_ocean_cube[10:15] cube_3 = base_ocean_cube[15:] self.temp_1_ocean = 'temp_1_ocean.nc' self.temp_2_ocean = 'temp_2_ocean.nc' self.temp_3_ocean = 'temp_3_ocean.nc' iris.save(cube_1, self.tmp_dir_ocean + self.temp_1_ocean) iris.save(cube_2, self.tmp_dir_ocean + self.temp_2_ocean) iris.save(cube_3, self.tmp_dir_ocean + self.temp_3_ocean)
def test_fix_partial_datetime(self): test_cube = stock.realistic_3d() test_cube.coord('time').guess_bounds() test_constr_point = iris.Constraint( time=lambda cell: cell.point.day == 22) test_constr_pdt = iris.Constraint(time=iris.time.PartialDateTime( day=22)) test_constr_full_pdt = iris.Constraint( time=iris.time.PartialDateTime(2014, 12, 22)) test_constr_full_point = iris.Constraint( time=lambda cell: cell.point.year == 2014 and cell.point.month == 12 and cell.point.day == 22) fixed_constr_point = _fix_partial_datetime(test_constr_point) fixed_constr_pdt = _fix_partial_datetime(test_constr_pdt) fixed_constr_full_pdt = _fix_partial_datetime(test_constr_full_pdt) fixed_constr_full_point = _fix_partial_datetime(test_constr_full_point) self.assertNotIsInstance(fixed_constr_pdt._coord_values['time'], iris.time.PartialDateTime) self.assertNotIsInstance(fixed_constr_full_pdt._coord_values['time'], iris.time.PartialDateTime) self.assertNotIsInstance(fixed_constr_point._coord_values['time'], iris.time.PartialDateTime) cube_constr_point = test_cube.extract(fixed_constr_point).coord('time') cube_constr_pdt = test_cube.extract(fixed_constr_pdt).coord('time') cube_constr_full_pdt = test_cube.extract(fixed_constr_full_pdt).coord( 'time') cube_constr_full_point = test_cube.extract( fixed_constr_full_point).coord('time') self.assertEqual(cube_constr_point.points[0], cube_constr_pdt.points[0]) self.assertEqual(cube_constr_point.points[1], cube_constr_pdt.points[1]) self.assertEqual(cube_constr_full_point.points[0], cube_constr_full_pdt.points[0]) self.assertEqual(cube_constr_full_point.points[1], cube_constr_full_pdt.points[1])
def setUp(self): self.cube = realistic_3d() self.coords = ('grid_longitude', 'grid_latitude') self.projection = iplt.default_projection(self.cube)
def setUp(self): self.cube = stock.realistic_3d() self.pp_field = mock.MagicMock(spec=pp.PPField3) self.pp_field.HEADER_DEFN = pp.PPField3.HEADER_DEFN self.patch('iris.fileformats.pp.PPField3', return_value=self.pp_field)
def test_bounds_mismatch(self): cube = stock.realistic_3d() cube.coord('grid_longitude').guess_bounds() with self.assertRaisesRegexp(ValueError, 'bounds'): result_non_circ = _xy_range(cube)
def setUp(self): self.cube = realistic_3d() self.axes = tests.mock.sentinel.axes
def test_non_circular(self): cube = stock.realistic_3d() assert not cube.coord("grid_longitude").circular result_non_circ = _xy_range(cube) self.assertEqual(result_non_circ, ((-5.0, 5.0), (-4.0, 4.0)))
def test_normal_cube_is_unchanged(self): cube = realistic_3d() reference_cube = realistic_3d() ch.fix_known_issues(cube) self.assertEqual(cube, reference_cube)
def test_no_fix_required_message(self): cube = realistic_3d() ch.fix_known_issues(cube) self.mock_logger.info.assert_not_called()
def setUp(self): self.cube = stock.realistic_3d()
def test_missing_attribute(self): cube = realistic_3d() fix = ExampleConcreteClass(cube) self.assertFalse(fix.is_fix_needed())
def test_bounds_mismatch(self): cube = stock.realistic_3d() cube.coord("grid_longitude").guess_bounds() with self.assertRaisesRegex(ValueError, "bounds"): _ = _xy_range(cube)
def test_different_attr_value(self): cube = realistic_3d() cube.attributes['attrib_1'] = 'a' cube.attributes['attrib_2'] = 'a' fix = ExampleConcreteClass(cube) self.assertFalse(fix.is_fix_needed())
def setUp(self): self.cube = realistic_3d() self.coords = ('grid_longitude', 'grid_latitude')
def test_all_attributes_match(self): cube = realistic_3d() cube.attributes['attrib_1'] = 'a' cube.attributes['attrib_2'] = 'b' fix = ExampleConcreteClass(cube) self.assertTrue(fix.is_fix_needed())
def setUp(self): self.cube = stock.realistic_3d()[0, 0, 0] self.representer = CubeRepresentation(self.cube) self.representer.repr_html()
def test_bounds_mismatch(self): cube = stock.realistic_3d() cube.coord('grid_longitude').guess_bounds() with self.assertRaisesRegex(ValueError, 'bounds'): result_non_circ = _xy_range(cube)
def setUp(self): super(TestCubeEqualiser, self).setUp() abs_path = os.path.dirname(os.path.abspath(__file__)) self.tmp_dir = abs_path + '/' + 'tmp_dir/' self.tmp_dir_aux = abs_path + '/' + 'tmp_dir_aux/' self.tmp_dir_attr = abs_path + '/' + 'tmp_dir_attr/' self.tmp_dir_time = abs_path + '/' + 'tmp_dir_time/' if not os.path.exists(self.tmp_dir): os.mkdir(self.tmp_dir) if not os.path.exists(self.tmp_dir_attr): os.mkdir(self.tmp_dir_attr) if not os.path.exists(self.tmp_dir_aux): os.mkdir(self.tmp_dir_aux) if not os.path.exists(self.tmp_dir_time): os.mkdir(self.tmp_dir_time) base_cube = stock.realistic_3d() cube_1 = base_cube[0:2] cube_2 = base_cube[2:4] cube_3 = base_cube[4:] new_time = cf_units.Unit('hours since 1980-01-01 00:00:00', 'gregorian') cube_2.dim_coords[0].convert_units(new_time) new_time = cf_units.Unit('hours since 1990-01-01 00:00:00', 'gregorian') cube_3.dim_coords[0].convert_units(new_time) self.temp_1_time = 'temp_1_time.nc' self.temp_2_time = 'temp_2_time.nc' self.temp_3_time = 'temp_3_time.nc' iris.save(cube_1, self.tmp_dir_time + self.temp_1_time) iris.save(cube_2, self.tmp_dir_time + self.temp_2_time) iris.save(cube_3, self.tmp_dir_time + self.temp_3_time) cube_1 = base_cube[0:2] cube_2 = base_cube[2:4] cube_3 = base_cube[4:] self.temp_1 = 'temp_1.nc' self.temp_2 = 'temp_2.nc' self.temp_3 = 'temp_3.nc' iris.save(cube_1, self.tmp_dir + self.temp_1) iris.save(cube_2, self.tmp_dir + self.temp_2) iris.save(cube_3, self.tmp_dir + self.temp_3) cube_1 = base_cube[0:2] cube_2 = base_cube[2:4] cube_3 = base_cube[4:] cube_2.attributes['history'] = 'some differing attributes' cube_2.attributes['creation_date'] = 'life day' cube_3.attributes['tracking_id'] = 'askbjQODOQwd210934AS' self.temp_1_attr = 'temp_1_attr.nc' self.temp_2_attr = 'temp_2_attr.nc' self.temp_3_attr = 'temp_3_attr.nc' iris.save(cube_1, self.tmp_dir_attr + self.temp_1_attr) iris.save(cube_2, self.tmp_dir_attr + self.temp_2_attr) iris.save(cube_3, self.tmp_dir_attr + self.temp_3_attr) cube_1 = base_cube[0:2] cube_2 = base_cube[2:4] cube_3 = base_cube[4:] height_coord = iris.coords.AuxCoord(2, standard_name='height', long_name='height', var_name='height', units='m') cube_2.add_aux_coord(height_coord) self.temp_1_aux = 'temp_1_aux.nc' self.temp_2_aux = 'temp_2_aux.nc' self.temp_3_aux = 'temp_3_aux.nc' iris.save(cube_1, self.tmp_dir_aux + self.temp_1_aux) iris.save(cube_2, self.tmp_dir_aux + self.temp_2_aux) iris.save(cube_3, self.tmp_dir_aux + self.temp_3_aux)
def test_non_circular(self): cube = stock.realistic_3d() assert not cube.coord('grid_longitude').circular result_non_circ = _xy_range(cube) self.assertEqual(result_non_circ, ((-5.0, 5.0), (-4.0, 4.0)))
def setUp(self): self.cube = realistic_3d() self.axes = tests.mock.sentinel.axes self.patcher = self.patch('cube_browser.Plot2D._check_coords')