def ok_bad(self, coord_names): # Deletes the named coords from `bad`. ok = lat_lon_cube() bad = lat_lon_cube() for name in coord_names: bad.remove_coord(name) return ok, bad
def ok_bad(self, lat_cs, lon_cs): # Updates `bad` to use the given coordinate systems. ok = lat_lon_cube() bad = lat_lon_cube() bad.coord('latitude').coord_system = lat_cs bad.coord('longitude').coord_system = lon_cs return ok, bad
def test_pp_save_rules(self): # Test single process flags for _, process_desc in iris.fileformats.pp.LBPROC_PAIRS[1:]: # Get basic cube and set process flag manually ll_cube = stock.lat_lon_cube() ll_cube.attributes["ukmo__process_flags"] = (process_desc,) # Save cube to pp temp_filename = iris.util.create_temp_filename(".pp") iris.save(ll_cube, temp_filename) # Check the lbproc is what we expect self.assertEqual(self.lbproc_from_pp(temp_filename), iris.fileformats.pp.lbproc_map[process_desc]) os.remove(temp_filename) # Test mutiple process flags multiple_bit_values = ((128, 64), (4096, 1024), (8192, 1024)) # Maps lbproc value to the process flags that should be created multiple_map = {sum(bits) : [iris.fileformats.pp.lbproc_map[bit] for bit in bits] for bits in multiple_bit_values} for lbproc, descriptions in six.iteritems(multiple_map): ll_cube = stock.lat_lon_cube() ll_cube.attributes["ukmo__process_flags"] = descriptions # Save cube to pp temp_filename = iris.util.create_temp_filename(".pp") iris.save(ll_cube, temp_filename) # Check the lbproc is what we expect self.assertEqual(self.lbproc_from_pp(temp_filename), lbproc) os.remove(temp_filename)
def ok_bad(self): # Make lat/lon share a single dimension on `bad`. ok = lat_lon_cube() bad = lat_lon_cube() lat = bad.coord('latitude') bad = bad[0, :lat.shape[0]] bad.remove_coord('latitude') bad.add_aux_coord(lat, 0) return ok, bad
def ok_bad(self, coord_name): # Demotes the named DimCoord on `bad` to an AuxCoord. ok = lat_lon_cube() bad = lat_lon_cube() coord = bad.coord(coord_name) dims = bad.coord_dims(coord) bad.remove_coord(coord_name) aux_coord = AuxCoord.from_coord(coord) bad.add_aux_coord(aux_coord, dims) return ok, bad
def test_process_flags(self): # Test single process flags for _, process_desc in iris.fileformats.pp.LBPROC_PAIRS[1:]: # Get basic cube and set process flag manually ll_cube = stock.lat_lon_cube() ll_cube.attributes["ukmo__process_flags"] = (process_desc,) # Save cube to netCDF temp_filename = iris.util.create_temp_filename(".nc") iris.save(ll_cube, temp_filename) # Reload cube cube = iris.load_cube(temp_filename) # Check correct number and type of flags self.assertTrue(len(cube.attributes["ukmo__process_flags"]) == 1, "Mismatch in number of process flags.") process_flag = cube.attributes["ukmo__process_flags"][0] self.assertEquals(process_flag, process_desc) os.remove(temp_filename) # Test mutiple process flags multiple_bit_values = ((128, 64), (4096, 1024), (8192, 1024)) # Maps lbproc value to the process flags that should be created multiple_map = {bits: [iris.fileformats.pp.lbproc_map[bit] for bit in bits] for bits in multiple_bit_values} for bits, descriptions in multiple_map.iteritems(): ll_cube = stock.lat_lon_cube() ll_cube.attributes["ukmo__process_flags"] = descriptions # Save cube to netCDF temp_filename = iris.util.create_temp_filename(".nc") iris.save(ll_cube, temp_filename) # Reload cube cube = iris.load_cube(temp_filename) # Check correct number and type of flags process_flags = cube.attributes["ukmo__process_flags"] self.assertTrue(len(process_flags) == len(bits), 'Mismatch in ' 'number of process flags.') self.assertEquals(set(process_flags), set(descriptions)) os.remove(temp_filename)
def test_unlimited_dim_latitude(self): cube = lat_lon_cube() unlim_dim_name = 'latitude' with self.temp_filename('foo.nc') as nc_out: save(cube, nc_out, unlimited_dimensions=[unlim_dim_name]) ds = nc.Dataset(nc_out) self.assertTrue(ds.dimensions[unlim_dim_name].isunlimited())
def setUp(self): self.cube = stock.lat_lon_cube() # Rename cube to avoid warning about unknown discipline/parameter. self.cube.rename('air_temperature') coord = DimCoord(23, 'time', bounds=[0, 100], units=Unit('days since epoch', calendar='standard')) self.cube.add_aux_coord(coord)
def test_no_unlimited_default(self): cube = lat_lon_cube() with iris.FUTURE.context(netcdf_no_unlimited=False): with self.temp_filename("foo.nc") as nc_out: save(cube, nc_out) ds = nc.Dataset(nc_out) self.assertTrue(ds.dimensions["latitude"].isunlimited())
def test_no_unlimited_future_default(self): cube = lat_lon_cube() with iris.FUTURE.context(netcdf_no_unlimited=True): with self.temp_filename('foo.nc') as nc_out: save(cube, nc_out) ds = nc.Dataset(nc_out) self.assertFalse(ds.dimensions['latitude'].isunlimited())
def setUp(self): self.cube = stock.lat_lon_cube() # Rename cube to avoid warning about unknown discipline/parameter. self.cube.rename('y_wind') time_coord = DimCoord( 20, 'time', bounds=[0, 40], units=Unit('days since epoch', calendar='julian')) self.cube.add_aux_coord(time_coord)
def setUp(self): self.cube = stock.lat_lon_cube() self.names_map = ['latitude', 'longitude'] masked_array = np.ma.masked_array([0, 1, 2], mask=[True, False, True]) self.cm = iris.coords.CellMeasure(masked_array, measure='area', var_name='cell_area') self.cube.add_cell_measure(self.cm, data_dims=0) self.exp_emsg = 'Cell measures with missing data are not supported.'
def test_coord_metadata_mismatch(self): # Check for failure when coordinate definitions differ. uk = uk_cube() self.remove_coord_systems(uk) lat_lon = lat_lon_cube() self.remove_coord_systems(lat_lon) with self.assertRaises(ValueError): regrid(uk, lat_lon)
def cube_with_cs(self, coord_system, names=['grid_longitude', 'grid_latitude']): cube = stock.lat_lon_cube() x, y = cube.coord('longitude'), cube.coord('latitude') x.coord_system = y.coord_system = coord_system for coord, name in zip([x, y], names): coord.rename(name) return cube
def test_longitude_no_units(self): cube = stock.lat_lon_cube() cube.coord('latitude').guess_bounds() cube.coord('longitude').guess_bounds() cube.coord('longitude').units = None with self.assertRaisesRegexp(ValueError, 'Units of degrees or ' 'radians required'): iris.analysis.cartography.area_weights(cube)
def setUp(self): self.cube = stock.lat_lon_cube() # Rename cube to avoid warning about unknown discipline/parameter. self.cube.rename('atmosphere_mole_content_of_ozone') coord = DimCoord(24, 'time', units=Unit('days since epoch', calendar='standard')) self.cube.add_aux_coord(coord) self.cube.attributes['WMO_constituent_type'] = 0
def test_no_forecast_time(self): cube = stock.lat_lon_cube() coord = iris.coords.DimCoord(np.array([24], dtype=np.int64), standard_name="time", units="hours since epoch") cube.add_aux_coord(coord) self.assertCML(cube, ["cube_to_pp", "no_forecast_time.cml"]) reference_txt_path = tests.get_result_path(("cube_to_pp", "no_forecast_time.txt")) with self.cube_save_test(reference_txt_path, reference_cubes=cube) as temp_pp_path: iris.save(cube, temp_pp_path)
def setUp(self): self.cube = stock.lat_lon_cube() # Add scalar time coord so that product_definition_template_common # doesn't get upset. t_coord = DimCoord([424854.], standard_name='time', units=Unit('hours since 1970-01-01 00:00:00', calendar='gregorian')) self.cube.add_aux_coord(t_coord) # Rename cube to avoid warning about unknown discipline/parameter. self.cube.rename('air_temperature')
def test_fully_wrapped_not_circular(self): cube = stock.lat_lon_cube() new_long = cube.coord('longitude').copy( cube.coord('longitude').points + 710) cube.remove_coord('longitude') cube.add_dim_coord(new_long, 1) interpolator = LinearInterpolator(cube, ['longitude']) res = interpolator([-10]) self.assertArrayEqual(res.data, cube[:, 1].data)
def setUp(self): self.cube = stock.lat_lon_cube() # Rename cube to avoid warning about unknown discipline/parameter. self.cube.rename('air_temperature') coord = DimCoord(23, 'time', bounds=[0, 100], units=Unit('days since epoch', calendar='standard')) self.cube.add_aux_coord(coord) coord = DimCoord(4, 'realization', units='1') self.cube.add_aux_coord(coord)
def test_polygon_larger_than_cube(self): cube = stock.lat_lon_cube() cube.dim_coords[0].guess_bounds() cube.dim_coords[1].guess_bounds() geometry = shapely.geometry.box(-0.6, -0.6, 0.6, 0.6) actual = _extract_relevant_cube_slice(cube, geometry) target = (cube[:, :3], cube[:, :3].coords(axis='x')[0], cube[:, :3].coords(axis='y')[0], (0, 0, 2, 2)) self.assertEqual(target, actual)
def test_no_forecast_time(self): cube = stock.lat_lon_cube() coord = iris.coords.DimCoord(np.array([24], dtype=np.int64), standard_name='time', units='hours since epoch') cube.add_aux_coord(coord) self.assertCML(cube, ['cube_to_pp', 'no_forecast_time.cml']) reference_txt_path = tests.get_result_path(('cube_to_pp', 'no_forecast_time.txt')) with self.cube_save_test(reference_txt_path, reference_cubes=cube) as temp_pp_path: iris.save(cube, temp_pp_path)
def test_polygon_on_cube_boundary(self): cube = stock.lat_lon_cube() cube.dim_coords[0].guess_bounds() cube.dim_coords[1].guess_bounds() geometry = shapely.geometry.box(-0.5, -0.5, 0.5, 0.5) actual = _extract_relevant_cube_slice(cube, geometry) target = (cube[1, 1], cube[1, 1].coords(axis='x')[0], cube[1, 1].coords(axis='y')[0], (1, 1, 1, 1)) self.assertEqual(target, actual)
def test_fully_wrapped_not_circular(self): cube = stock.lat_lon_cube() new_long = cube.coord('longitude').copy( cube.coord('longitude').points + 710) cube.remove_coord('longitude') cube.add_dim_coord(new_long, 1) interpolator = RectilinearInterpolator(cube, ['longitude'], LINEAR, EXTRAPOLATE) res = interpolator([-10]) self.assertArrayEqual(res.data, cube[:, 1].data)
def test_no_forecast_time(self): cube = stock.lat_lon_cube() coord = iris.coords.DimCoord(24, standard_name='time', units='hours since epoch') cube.add_aux_coord(coord) self.assertCML(cube, ['cube_to_pp', 'no_forecast_time.cml']) reference_txt_path = tests.get_result_path(('cube_to_pp', 'no_forecast_time.txt')) with self.cube_save_test(reference_txt_path, reference_cubes=cube) as temp_pp_path: iris.save(cube, temp_pp_path)
def test_pp_save_rules(self): # Test single process flags for _, process_desc in iris.fileformats.pp.LBPROC_PAIRS[1:]: # Get basic cube and set process flag manually ll_cube = stock.lat_lon_cube() ll_cube.attributes["ukmo__process_flags"] = (process_desc, ) # Save cube to pp temp_filename = iris.util.create_temp_filename(".pp") iris.save(ll_cube, temp_filename) # Check the lbproc is what we expect self.assertEqual( self.lbproc_from_pp(temp_filename), iris.fileformats.pp.lbproc_map[process_desc], ) os.remove(temp_filename) # Test mutiple process flags multiple_bit_values = ((128, 64), (4096, 1024), (8192, 1024)) # Maps lbproc value to the process flags that should be created multiple_map = { sum(bits): [iris.fileformats.pp.lbproc_map[bit] for bit in bits] for bits in multiple_bit_values } for lbproc, descriptions in multiple_map.items(): ll_cube = stock.lat_lon_cube() ll_cube.attributes["ukmo__process_flags"] = descriptions # Save cube to pp temp_filename = iris.util.create_temp_filename(".pp") iris.save(ll_cube, temp_filename) # Check the lbproc is what we expect self.assertEqual(self.lbproc_from_pp(temp_filename), lbproc) os.remove(temp_filename)
def test_polygon_on_cube_boundary(self): cube = stock.lat_lon_cube() cube.dim_coords[0].guess_bounds() cube.dim_coords[1].guess_bounds() geometry = shapely.geometry.box(-0.5, -0.5, 0.5, 0.5) actual = _extract_relevant_cube_slice(cube, geometry) target = ( cube[1, 1], cube[1, 1].coords(axis="x")[0], cube[1, 1].coords(axis="y")[0], (1, 1, 1, 1), ) self.assertEqual(target, actual)
def test_polygon_larger_than_cube(self): cube = stock.lat_lon_cube() cube.dim_coords[0].guess_bounds() cube.dim_coords[1].guess_bounds() geometry = shapely.geometry.box(-0.6, -0.6, 0.6, 0.6) actual = _extract_relevant_cube_slice(cube, geometry) target = ( cube[:, :3], cube[:, :3].coords(axis="x")[0], cube[:, :3].coords(axis="y")[0], (0, 0, 2, 2), ) self.assertEqual(target, actual)
def test_auto_guess_bounds(self): # Check that the `get_data()` method automatically adds bounds # when they're missing. cube = lat_lon_cube() element = Image(cube.copy()) plot = GeoImagePlot(element) ranges = {} style = {'interpolation': None} plot_args, style, axis_kwargs = plot.get_data(element, ranges, style) for name in ('latitude', 'longitude'): cube.coord(name).guess_bounds() self.assertEqual(plot_args, (cube, )) self.assertEqual(style, {'vmax': 11, 'vmin': 0}) self.assertEqual(axis_kwargs, {})
def test_auto_guess_bounds(self): # Check that the `get_data()` method automatically adds bounds # when they're missing. cube = lat_lon_cube() element = Image(cube.copy()) plot = GeoImagePlot(element) ranges = {} style = {'interpolation': None} plot_args, style, axis_kwargs = plot.get_data(element, ranges, style) for name in ('latitude', 'longitude'): cube.coord(name).guess_bounds() self.assertEqual(plot_args, (cube,)) self.assertEqual(style, {'vmax': 11, 'vmin': 0}) self.assertEqual(axis_kwargs, {})
def test_save_load(self): cube = stock.lat_lon_cube() cube.rename('atmosphere_mole_content_of_ozone') cube.units = Unit('Dobson') tcoord = DimCoord(23, 'time', units=Unit('days since epoch', calendar='standard')) fpcoord = DimCoord(24, 'forecast_period', units=Unit('hours')) cube.add_aux_coord(tcoord) cube.add_aux_coord(fpcoord) cube.attributes['WMO_constituent_type'] = 0 with self.temp_filename('test_grib_pdt40.grib2') as temp_file_path: save(cube, temp_file_path) loaded = load_cube(temp_file_path) self.assertEqual(loaded.attributes, cube.attributes)
def test_other_cell_methods(self, mock_set): cube = stock.lat_lon_cube() # Rename cube to avoid warning about unknown discipline/parameter. cube.rename('air_temperature') coord = DimCoord(23, 'time', bounds=[0, 24], units=Unit('hours since epoch')) cube.add_aux_coord(coord) # Add one time cell method and another unrelated one. cell_method = CellMethod(method='mean', coords=['elephants']) cube.add_cell_method(cell_method) cell_method = CellMethod(method='sum', coords=['time']) cube.add_cell_method(cell_method) _product_definition_template_8_and_11(cube, mock.sentinel.grib) mock_set.assert_any_call(mock.sentinel.grib, 'numberOfTimeRange', 1)
def test_auto_guess_bounds_not_needed(self): # Check that the `get_data()` method *doesn't* try to add or # modify the bounds when they're already present. cube = lat_lon_cube() for name in ('latitude', 'longitude'): coord = cube.coord(name) coord.guess_bounds() coord.bounds = coord.bounds + 0.1 element = Image(cube.copy()) plot = GeoImagePlot(element) ranges = {} style = {'interpolation': None} plot_args, style, axis_kwargs = plot.get_data(element, ranges, style) self.assertEqual(plot_args, (cube, )) self.assertEqual(style, {'vmax': 11, 'vmin': 0}) self.assertEqual(axis_kwargs, {})
def test_save_load(self): cube = stock.lat_lon_cube() cube.rename("atmosphere_mole_content_of_ozone") cube.units = Unit("Dobson") tcoord = DimCoord(23, "time", units=Unit("days since epoch", calendar="standard")) fpcoord = DimCoord(24, "forecast_period", units=Unit("hours")) cube.add_aux_coord(tcoord) cube.add_aux_coord(fpcoord) cube.attributes["WMO_constituent_type"] = 0 with self.temp_filename("test_grib_pdt40.grib2") as temp_file_path: save(cube, temp_file_path) loaded = load_cube(temp_file_path) self.assertEqual(loaded.attributes, cube.attributes)
def test_auto_guess_bounds_not_needed(self): # Check that the `get_data()` method *doesn't* try to add or # modify the bounds when they're already present. cube = lat_lon_cube() for name in ('latitude', 'longitude'): coord = cube.coord(name) coord.guess_bounds() coord.bounds = coord.bounds + 0.1 element = Image(cube.copy()) plot = GeoImagePlot(element) ranges = {} style = {'interpolation': None} plot_args, style, axis_kwargs = plot.get_data(element, ranges, style) self.assertEqual(plot_args, (cube,)) self.assertEqual(style, {'vmax': 11, 'vmin': 0}) self.assertEqual(axis_kwargs, {})
def test_no_forecast_period(self): cube = stock.lat_lon_cube() # Add a bounded scalar time coord and a forecast_reference_time. time_coord = iris.coords.DimCoord( 10.958333, standard_name="time", units="days since 2013-05-10 12:00", bounds=[10.916667, 11.0] ) cube.add_aux_coord(time_coord) forecast_reference_time = iris.coords.DimCoord( 2.0, standard_name="forecast_reference_time", units="weeks since 2013-05-07" ) cube.add_aux_coord(forecast_reference_time) self.assertCML(cube, ["cube_to_pp", "no_forecast_period.cml"]) reference_txt_path = tests.get_result_path(("cube_to_pp", "no_forecast_period.txt")) with self.cube_save_test(reference_txt_path, reference_cubes=cube) as temp_pp_path: iris.save(cube, temp_pp_path)
def test_no_forecast_period(self): cube = stock.lat_lon_cube() # Add a bounded scalar time coord and a forecast_reference_time. time_coord = iris.coords.DimCoord( 10.958333, standard_name='time', units='days since 2013-05-10 12:00', bounds=[10.916667, 11.0]) cube.add_aux_coord(time_coord) forecast_reference_time = iris.coords.DimCoord( 2.0, standard_name='forecast_reference_time', units='weeks since 2013-05-07') cube.add_aux_coord(forecast_reference_time) self.assertCML(cube, ['cube_to_pp', 'no_forecast_period.cml']) reference_txt_path = tests.get_result_path(('cube_to_pp', 'no_forecast_period.txt')) with self.cube_save_test(reference_txt_path, reference_cubes=cube) as \ temp_pp_path: iris.save(cube, temp_pp_path)
def test_no_forecast_period(self): cube = stock.lat_lon_cube() # Add a bounded scalar time coord and a forecast_reference_time. time_coord = iris.coords.DimCoord( 10.958333, standard_name="time", units="days since 2013-05-10 12:00", bounds=[10.916667, 11.0], ) cube.add_aux_coord(time_coord) forecast_reference_time = iris.coords.DimCoord( 2.0, standard_name="forecast_reference_time", units="weeks since 2013-05-07", ) cube.add_aux_coord(forecast_reference_time) self.assertCML(cube, ["cube_to_pp", "no_forecast_period.cml"]) reference_txt_path = tests.get_result_path( ("cube_to_pp", "no_forecast_period.txt")) with self.cube_save_test(reference_txt_path, reference_cubes=cube) as temp_pp_path: iris.save(cube, temp_pp_path)
def setUp(self): self.cube = lat_lon_cube() self.epsilon = 0.01
def setUp(self): self.cube = stock.lat_lon_cube()
def setUp(self): self.src_grid = lat_lon_cube() self.bad = np.ones((3, 4)) self.weights = np.ones(self.src_grid.shape, self.src_grid.dtype)
def test_fail_non2d_coords(self): # Check error with bad args. cube = lat_lon_cube() with self.assertRaisesRegex(ValueError, "inputs must have 2-dimensional shape"): gridcell_angles(cube)
def setUp(self): super().setUp() self.cube = simple_2d(with_bounds=True) self.cube.add_aux_coord(AuxCoord(list('abcd'), long_name='str_coord'), 1) self.lat_lon_cube = lat_lon_cube()
def setUp(self): self.cube = stock.lat_lon_cube() # Rename cube to avoid warning about unknown discipline/parameter. self.cube.rename('air_temperature')
def test_no_unlimited_dims(self): cube = lat_lon_cube() with self.temp_filename('foo.nc') as nc_out: save(cube, nc_out) ds = nc.Dataset(nc_out) self.assertFalse(ds.dimensions['latitude'].isunlimited())
def _cube_with_cs(self, coord_system): """Return a simple 2D cube that uses the given coordinate system.""" cube = stock.lat_lon_cube() x, y = cube.coord('longitude'), cube.coord('latitude') x.coord_system = y.coord_system = coord_system return cube
def ok_bad(self): # Changes the longitude coord to radians on `bad`. ok = lat_lon_cube() bad = lat_lon_cube() bad.coord('longitude').units = 'radians' return ok, bad
def setUp(self): self.cube = stock.lat_lon_cube() # Rename cube to avoid warning about unknown discipline/parameter. self.cube.rename('air_temperature') cell_method = CellMethod(method='sum', coords=['time']) self.cube.add_cell_method(cell_method)
def setUp(self): self.cube = lat_lon_cube() # Regridder method and extrapolation-mode. self.args = ('linear', 'mask') self.regridder = Regridder(self.cube, self.cube, *self.args)
def setUp(self): self.ok = lat_lon_cube() self.bad = np.ones((3, 4)) self.weights = np.ones(self.ok.shape, self.ok.dtype)
def ok_bad(self): # Defines `bad` with an x coordinate in km. ok = lat_lon_cube() bad = uk_cube() bad.coord(axis='x').units = 'km' return ok, bad
def setUp(self): self.cubes = CubeList([stock.simple_3d(), stock.lat_lon_cube()]) self.cubes[0].rename('name & <html>') self.representer = CubeListRepresentation(self.cubes) self.content = self.representer.make_content()
def setUp(self): self.cubes = CubeList([stock.simple_3d(), stock.lat_lon_cube()]) self.representer = CubeListRepresentation(self.cubes)
def init_data(self): self.cube = lat_lon_cube() self.epsilon = 0.01
def setUp(self): self.cube = stock.lat_lon_cube() # Check we're not tripped up by names containing spaces. self.cube.rename('Electron density') self.cube.units = '1e11 e/m^3' self.representer = CubeRepresentation(self.cube)