def test_no_y_coord(self): cube = Cube(np.array([[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]), long_name="foo") x_coord = DimCoord([10, 11, 12, 13, 14], long_name="bar") cube.add_dim_coord(x_coord, 1) data_frame = iris.pandas.as_data_frame(cube) self.assertArrayEqual(data_frame, cube.data) self.assertString(str(data_frame), tests.get_result_path(("pandas", "as_dataframe", "no_y_coord.txt")))
def test_time_gregorian(self): cube = Cube(np.array([0, 1, 2, 3, 4]), long_name="ts") time_coord = DimCoord([0, 100.1, 200.2, 300.3, 400.4], long_name="time", units="days since 2000-01-01 00:00") cube.add_dim_coord(time_coord, 0) series = iris.pandas.as_series(cube) self.assertArrayEqual(series, cube.data) self.assertString(str(series), tests.get_result_path(("pandas", "as_series", "time_gregorian.txt")))
def test_simple(self): cube = Cube(np.array([0, 1, 2, 3, 4.4]), long_name="foo") dim_coord = DimCoord([5, 6, 7, 8, 9], long_name="bar") cube.add_dim_coord(dim_coord, 0) series = iris.pandas.as_series(cube) self.assertArrayEqual(series, cube.data) self.assertString(str(series), tests.get_result_path(("pandas", "as_series", "simple.txt")))
class Test_rolling_window(tests.IrisTest): def setUp(self): self.cube = Cube(np.arange(6)) val_coord = DimCoord([0, 1, 2, 3, 4, 5], long_name="val") month_coord = AuxCoord(['jan', 'feb', 'mar', 'apr', 'may', 'jun'], long_name='month') self.cube.add_dim_coord(val_coord, 0) self.cube.add_aux_coord(month_coord, 0) self.mock_agg = mock.Mock(spec=Aggregator) self.mock_agg.aggregate = mock.Mock( return_value=np.empty([4])) def test_string_coord(self): # Rolling window on a cube that contains a string coordinate. res_cube = self.cube.rolling_window('val', self.mock_agg, 3) val_coord = DimCoord(np.array([1, 2, 3, 4]), bounds=np.array([[0, 2], [1, 3], [2, 4], [3, 5]]), long_name='val') month_coord = AuxCoord( np.array(['jan|feb|mar', 'feb|mar|apr', 'mar|apr|may', 'apr|may|jun']), bounds=np.array([['jan', 'mar'], ['feb', 'apr'], ['mar', 'may'], ['apr', 'jun']]), long_name='month') self.assertEqual(res_cube.coord('val'), val_coord) self.assertEqual(res_cube.coord('month'), month_coord)
def setUp(self): nt = 10 data = np.arange(nt, dtype=np.float32) cube = Cube(data, standard_name='air_temperature', units='K') # Temporal coordinate. t_units = Unit('hours since 1970-01-01 00:00:00', calendar='gregorian') t_coord = DimCoord(points=np.arange(nt), standard_name='time', units=t_units) cube.add_dim_coord(t_coord, 0) # Increasing 1D time-series cube. self.series_inc_cube = cube self.series_inc = CubeSignature(self.series_inc_cube) # Decreasing 1D time-series cube. self.series_dec_cube = self.series_inc_cube.copy() self.series_dec_cube.remove_coord('time') t_tmp = DimCoord(points=t_coord.points[::-1], standard_name='time', units=t_units) self.series_dec_cube.add_dim_coord(t_tmp, 0) self.series_dec = CubeSignature(self.series_dec_cube) # Scalar 0D time-series cube with scalar time coordinate. cube = Cube(0, standard_name='air_temperature', units='K') cube.add_aux_coord(DimCoord(points=nt, standard_name='time', units=t_units)) self.scalar_cube = cube
class Test_concatenate_cube(tests.IrisTest): def setUp(self): self.units = Unit('days since 1970-01-01 00:00:00', calendar='gregorian') self.cube1 = Cube([1, 2, 3], 'air_temperature', units='K') self.cube1.add_dim_coord(DimCoord([0, 1, 2], 'time', units=self.units), 0) def test_pass(self): self.cube2 = Cube([1, 2, 3], 'air_temperature', units='K') self.cube2.add_dim_coord(DimCoord([3, 4, 5], 'time', units=self.units), 0) result = CubeList([self.cube1, self.cube2]).concatenate_cube() self.assertIsInstance(result, Cube) def test_fail(self): units = Unit('days since 1970-01-02 00:00:00', calendar='gregorian') cube2 = Cube([1, 2, 3], 'air_temperature', units='K') cube2.add_dim_coord(DimCoord([0, 1, 2], 'time', units=units), 0) with self.assertRaises(iris.exceptions.ConcatenateError): CubeList([self.cube1, cube2]).concatenate_cube() def test_empty(self): exc_regexp = "can't concatenate an empty CubeList" with self.assertRaisesRegexp(ValueError, exc_regexp): CubeList([]).concatenate_cube()
class TestOSGBToLatLon(tests.IrisTest): def setUp(self): path = tests.get_data_path( ('NIMROD', 'uk2km', 'WO0000000003452', '201007020900_u1096_ng_ey00_visibility0180_screen_2km')) self.src = iris.load_cube(path)[0] # Cast up to float64, to work around numpy<=1.8 bug with means of # arrays of 32bit floats. self.src.data = self.src.data.astype(np.float64) self.grid = Cube(np.empty((73, 96))) cs = GeogCS(6370000) lat = DimCoord(np.linspace(46, 65, 73), 'latitude', units='degrees', coord_system=cs) lon = DimCoord(np.linspace(-14, 8, 96), 'longitude', units='degrees', coord_system=cs) self.grid.add_dim_coord(lat, 0) self.grid.add_dim_coord(lon, 1) def _regrid(self, method): regridder = Regridder(self.src, self.grid, method, 'mask') result = regridder(self.src) return result def test_linear(self): res = self._regrid('linear') self.assertArrayShapeStats(res, (73, 96), -16100.351951, 5603.850769) def test_nearest(self): res = self._regrid('nearest') self.assertArrayShapeStats(res, (73, 96), -16095.965585, 5612.657155)
def test_fail(self): units = Unit('days since 1970-01-02 00:00:00', calendar='gregorian') cube2 = Cube([1, 2, 3], 'air_temperature', units='K') cube2.add_dim_coord(DimCoord([0, 1, 2], 'time', units=units), 0) with self.assertRaises(iris.exceptions.ConcatenateError): CubeList([self.cube1, cube2]).concatenate_cube()
def simple_2d(with_bounds=True): """ Returns an abstract, two-dimensional, optionally bounded, cube. >>> print(simple_2d()) thingness (bar: 3; foo: 4) Dimension coordinates: bar x - foo - x >>> print(repr(simple_2d().data)) [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] """ cube = Cube(np.arange(12, dtype=np.int32).reshape((3, 4))) cube.long_name = 'thingness' cube.units = '1' y_points = np.array([2.5, 7.5, 12.5]) y_bounds = np.array([[0, 5], [5, 10], [10, 15]], dtype=np.int32) y_coord = DimCoord(y_points, long_name='bar', units='1', bounds=y_bounds if with_bounds else None) x_points = np.array([ -7.5, 7.5, 22.5, 37.5]) x_bounds = np.array([[-15, 0], [0, 15], [15, 30], [30, 45]], dtype=np.int32) x_coord = DimCoord(x_points, long_name='foo', units='1', bounds=x_bounds if with_bounds else None) cube.add_dim_coord(y_coord, 0) cube.add_dim_coord(x_coord, 1) return cube
class TestOSGBToLatLon(tests.GraphicsTest): def setUp(self): path = tests.get_data_path( ('NIMROD', 'uk2km', 'WO0000000003452', '201007020900_u1096_ng_ey00_visibility0180_screen_2km')) self.src = iris.load_cube(path)[0] self.src.data = self.src.data.astype(np.float32) self.grid = Cube(np.empty((73, 96))) cs = GeogCS(6370000) lat = DimCoord(np.linspace(46, 65, 73), 'latitude', units='degrees', coord_system=cs) lon = DimCoord(np.linspace(-14, 8, 96), 'longitude', units='degrees', coord_system=cs) self.grid.add_dim_coord(lat, 0) self.grid.add_dim_coord(lon, 1) def _regrid(self, method): regridder = Regridder(self.src, self.grid, method, 'mask') result = regridder(self.src) qplt.pcolor(result, antialiased=False) qplt.plt.gca().coastlines() def test_linear(self): self._regrid('linear') self.check_graphic() def test_nearest(self): self._regrid('nearest') self.check_graphic()
def test_3d_data(self): time = DimCoord(np.arange(12) * 6, 'time', units='hours since 2013-10-29 18:00:00') cube = Cube(np.arange(12 * 5 * 6).reshape(12, 5, 6)) cube.add_dim_coord(time, 0) constraint = TimeConstraint(hour=12) sub_cube = constraint.extract(cube) self.assertArrayEqual(sub_cube.coord('time').points, [18, 42, 66])
def _create_cube(self, longitudes): # Return a Cube with circular longitude with the given values. data = np.arange(12).reshape((3, 4)) * 0.1 cube = Cube(data) lon = DimCoord(longitudes, standard_name='longitude', units='degrees', circular=True) cube.add_dim_coord(lon, 1) return cube
def make_cube(self, calendar): n_times = 10 cube = Cube(np.arange(n_times)) time_coord = DimCoord(np.arange(n_times), standard_name='time', units=Unit('days since 1980-12-25', calendar=calendar)) cube.add_dim_coord(time_coord, 0) return cube
def test_simple(self): cube = Cube(np.array([0, 1, 2, 3, 4.4]), long_name="foo") dim_coord = DimCoord([5, 6, 7, 8, 9], long_name="bar") cube.add_dim_coord(dim_coord, 0) expected_index = np.array([5, 6, 7, 8, 9]) series = iris.pandas.as_series(cube) self.assertArrayEqual(series, cube.data) self.assertArrayEqual(series.index, expected_index)
def cube(self, x, y): data = np.arange(len(x) * len(y)).reshape(len(y), len(x)) cube = Cube(data) lat = DimCoord(y, 'latitude', units='degrees') lon = DimCoord(x, 'longitude', units='degrees') cube.add_dim_coord(lat, 0) cube.add_dim_coord(lon, 1) return cube
def test_time_360(self): cube = Cube(np.array([[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]), long_name="ts") time_unit = cf_units.Unit("days since 2000-01-01 00:00", calendar=cf_units.CALENDAR_360_DAY) time_coord = DimCoord([100.1, 200.2], long_name="time", units=time_unit) cube.add_dim_coord(time_coord, 0) data_frame = iris.pandas.as_data_frame(cube) self.assertArrayEqual(data_frame, cube.data) self.assertString(str(data_frame), tests.get_result_path(("pandas", "as_dataframe", "time_360.txt")))
class Test_masked(tests.IrisTest): def setUp(self): self.cube = Cube(ma.masked_greater([1, 2, 3, 4, 5], 3)) self.cube.add_dim_coord(DimCoord([6, 7, 8, 9, 10], long_name='foo'), 0) def test_ma(self): data = MAX.aggregate(self.cube.data, axis=0) self.assertArrayEqual(data, [3])
def test_time_360(self): cube = Cube(np.array([0, 1, 2, 3, 4]), long_name="ts") time_unit = cf_units.Unit("days since 2000-01-01 00:00", calendar=cf_units.CALENDAR_360_DAY) time_coord = DimCoord([0, 100.1, 200.2, 300.3, 400.4], long_name="time", units=time_unit) cube.add_dim_coord(time_coord, 0) series = iris.pandas.as_series(cube) self.assertArrayEqual(series, cube.data) self.assertString(str(series), tests.get_result_path(("pandas", "as_series", "time_360.txt")))
def _simple_cube(self, dtype): data = np.arange(12, dtype=dtype).reshape(3, 4) points = np.arange(3, dtype=dtype) bounds = np.arange(6, dtype=dtype).reshape(3, 2) cube = Cube(data, 'air_pressure_anomaly') coord = DimCoord(points, bounds=bounds) cube.add_dim_coord(coord, 0) return cube
class Test_masked(tests.IrisTest): def setUp(self): self.cube = Cube(ma.masked_equal([1, 2, 3, 4, 5], 3)) self.cube.add_dim_coord(DimCoord([6, 7, 8, 9, 10], long_name='foo'), 0) self.func = lambda x: x >= 3 def test_ma(self): data = COUNT.aggregate(self.cube.data, axis=0, function=self.func) self.assertArrayEqual(data, [2])
def setUp(self): self.data = np.arange(6.0).reshape((2, 3)) self.lazydata = biggus.NumpyArrayAdapter(self.data) cube = Cube(self.lazydata) for i_dim, name in enumerate(('y', 'x')): npts = cube.shape[i_dim] coord = DimCoord(np.arange(npts), long_name=name) cube.add_dim_coord(coord, i_dim) self.cube = cube
def test_nonlatlon_multiple_2d(self): co_y = DimCoord([10.0, 20.0], long_name='y') co_x = DimCoord([1.0, 2.0, 3.0], long_name='x') cube = Cube(np.zeros((2, 3))) cube.add_dim_coord(co_y, 0) cube.add_dim_coord(co_x, 1) sample_points = [('x', [2.8, -350.0, 1.7]), ('y', [18.5, 8.7, 12.2])] result = nn_ndinds(cube, sample_points) self.assertEqual(result, [(1, 2), (0, 0), (0, 1)])
def test_nonlatlon_simple_2d(self): co_y = DimCoord([10.0, 20.0], long_name='y') co_x = DimCoord([1.0, 2.0, 3.0], long_name='x') cube = Cube(np.zeros((2, 3))) cube.add_dim_coord(co_y, 0) cube.add_dim_coord(co_x, 1) sample_point = [('x', 2.8), ('y', 18.5)] result = nn_ndinds(cube, sample_point) self.assertEqual(result, [(1, 2)])
def test_no_x_dim(self): # Operate in Y only, returned slice should be [iy, :]. co_x = DimCoord([1.0, 2.0, 3.0], long_name='x') co_y = DimCoord([10.0, 20.0], long_name='y') cube = Cube(np.zeros((2, 3))) cube.add_dim_coord(co_y, 0) cube.add_dim_coord(co_x, 1) sample_point = [('y', 18.5)] result = nn_ndinds(cube, sample_point) self.assertEqual(result, [(1, slice(None))])
def uk_cube(): uk = Cube(np.arange(12, dtype=np.float32).reshape(3, 4)) cs = OSGB() y_coord = DimCoord(range(3), 'projection_y_coordinate', units='m', coord_system=cs) x_coord = DimCoord(range(4), 'projection_x_coordinate', units='m', coord_system=cs) uk.add_dim_coord(y_coord, 0) uk.add_dim_coord(x_coord, 1) return uk
class Test_lazy_masked(tests.IrisTest): def setUp(self): masked_data = ma.masked_greater([1, 2, 3, 4, 5], 3) self.cube = Cube(as_lazy_data(masked_data)) self.cube.add_dim_coord(DimCoord([6, 7, 8, 9, 10], long_name='foo'), 0) def test_lazy_ma(self): lazy_data = MAX.lazy_aggregate(self.cube.lazy_data(), axis=0) self.assertTrue(is_lazy_data(lazy_data)) self.assertArrayEqual(lazy_data.compute(), [3])
def test_mismatched_src_coord_systems(self): src = Cube(np.zeros((3, 4))) cs = GeogCS(6543210) lat = DimCoord(np.arange(3), 'latitude', coord_system=cs) lon = DimCoord(np.arange(4), 'longitude') src.add_dim_coord(lat, 0) src.add_dim_coord(lon, 1) target = mock.Mock() with self.assertRaises(ValueError): AreaWeightedRegridder(src, target)
def _cube(self, ellipsoid=None): data = np.arange(12).reshape(3, 4).astype("u1") cube = Cube(data, "air_pressure_anomaly") coord = DimCoord(np.arange(3), "latitude", units="degrees", coord_system=ellipsoid) coord.guess_bounds() cube.add_dim_coord(coord, 0) coord = DimCoord(np.arange(4), "longitude", units="degrees", coord_system=ellipsoid) coord.guess_bounds() cube.add_dim_coord(coord, 1) return cube
def lat_lon_cube(): """ Returns a cube with a latitude and longitude suitable for testing saving to PP/NetCDF etc. """ cube = Cube(numpy.arange(12, dtype=numpy.int32).reshape((3, 4))) cs = LatLonCS(None, 'pm', GeoPosition(90, 0), 0) cube.add_dim_coord(iris.coords.DimCoord(points=numpy.array([-1, 0, 1], dtype=numpy.int32), standard_name='latitude', units='degrees', coord_system=cs), 0) cube.add_dim_coord(iris.coords.DimCoord(points=numpy.array([-1, 0, 1, 2], dtype=numpy.int32), standard_name='longitude', units='degrees', coord_system=cs), 1) return cube
def _cube(self, dtype): data = np.arange(12).reshape(3, 4).astype(dtype) + 20 cube = Cube(data, "air_pressure_anomaly") coord = DimCoord(np.arange(3), "latitude", units="degrees") coord.guess_bounds() cube.add_dim_coord(coord, 0) coord = DimCoord(np.arange(4), "longitude", units="degrees") coord.guess_bounds() cube.add_dim_coord(coord, 1) return cube
def _transverse_mercator_cube(self, ellipsoid=None): data = np.arange(12).reshape(3, 4) cube = Cube(data, "air_pressure_anomaly") trans_merc = TransverseMercator(49.0, -2.0, -400000.0, 100000.0, 0.9996012717, ellipsoid) coord = DimCoord( np.arange(3), "projection_y_coordinate", units="m", coord_system=trans_merc, ) cube.add_dim_coord(coord, 0) coord = DimCoord( np.arange(4), "projection_x_coordinate", units="m", coord_system=trans_merc, ) cube.add_dim_coord(coord, 1) return cube
def test_laziness(): """Test that regridding is lazy when source data is lazy.""" n_lons = 12 n_lats = 10 h = 4 lon_bounds = (-180, 180) lat_bounds = (-90, 90) grid = _grid_cube(n_lons, n_lats, lon_bounds, lat_bounds, circular=True) src_data = np.arange(n_lats * n_lons * h).reshape([n_lats, n_lons, h]) src_data = da.from_array(src_data, chunks=[3, 5, 1]) src = Cube(src_data) src.add_dim_coord(grid.coord("latitude"), 0) src.add_dim_coord(grid.coord("longitude"), 1) tgt = _grid_cube(n_lons, n_lats, lon_bounds, lat_bounds, circular=True) assert src.has_lazy_data() result = regrid_rectilinear_to_rectilinear(src, tgt) assert result.has_lazy_data() assert np.allclose(result.data, src_data)
def _stereo_cube(self, ellipsoid=None): data = np.arange(12).reshape(3, 4) cube = Cube(data, "air_pressure_anomaly") stereo = Stereographic(-10.0, 20.0, 500000.0, -200000.0, None, ellipsoid) coord = DimCoord( np.arange(3), "projection_y_coordinate", units="m", coord_system=stereo, ) cube.add_dim_coord(coord, 0) coord = DimCoord( np.arange(4), "projection_x_coordinate", units="m", coord_system=stereo, ) cube.add_dim_coord(coord, 1) return cube
def set_up_cube_lat_long( zero_point_indices=((0, 7, 7), ), num_time_points=1, num_grid_points=16): """Set up a lat-long coord cube.""" data = np.ones((num_time_points, num_grid_points, num_grid_points)) for time_index, lat_index, lon_index in zero_point_indices: data[time_index][lat_index][lon_index] = 0 cube = Cube(data, standard_name="precipitation_amount", units="kg m^-2") tunit = Unit("hours since 1970-01-01 00:00:00", "gregorian") time_points = [402192.5 + _ for _ in range(num_time_points)] cube.add_aux_coord(AuxCoord(time_points, "time", units=tunit), 0) cube.add_dim_coord( DimCoord(np.linspace(0.0, float(num_grid_points - 1), num_grid_points), 'latitude', units='degrees'), 1) cube.add_dim_coord( DimCoord(np.linspace(0.0, float(num_grid_points - 1), num_grid_points), 'longitude', units='degrees'), 2) return cube
def setUp(self): # A (3, 2, 4) cube with a masked element. cube = Cube(np.ma.arange(24, dtype=np.int32).reshape((3, 2, 4))) cs = GeogCS(6371229) coord = DimCoord(points=np.array([-1, 0, 1], dtype=np.int32), standard_name='latitude', units='degrees', coord_system=cs) cube.add_dim_coord(coord, 0) coord = DimCoord(points=np.array([-1, 0, 1, 2], dtype=np.int32), standard_name='longitude', units='degrees', coord_system=cs) cube.add_dim_coord(coord, 2) cube.coord('latitude').guess_bounds() cube.coord('longitude').guess_bounds() cube.data[1, 1, 2] = ma.masked self.src_cube = cube # Create (7, 2, 9) grid cube. self.grid_cube = _resampled_grid(cube, 2.3, 2.4)
def test_time_gregorian(self): cube = Cube(np.array([[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]), long_name="ts") day_offsets = [0, 100.1, 200.2, 300.3, 400.4] time_coord = DimCoord(day_offsets, long_name="time", units="days since 2000-01-01 00:00") cube.add_dim_coord(time_coord, 1) data_frame = iris.pandas.as_data_frame(cube) self.assertArrayEqual(data_frame, cube.data) nanoseconds_per_day = 24 * 60 * 60 * 1000000000 days_to_2000 = 365 * 30 + 7 # pandas Timestamp class cannot handle floats in pandas <v0.12 timestamps = [ pandas.Timestamp( int(nanoseconds_per_day * (days_to_2000 + day_offset))) for day_offset in day_offsets ] self.assertTrue(all(data_frame.columns == timestamps)) self.assertTrue(all(data_frame.index == [0, 1]))
def lat_lon_cube(): """ Returns a cube with a latitude and longitude suitable for testing saving to PP/NetCDF etc. """ cube = Cube(np.arange(12, dtype=np.int32).reshape((3, 4))) cs = GeogCS(6371229) coord = iris.coords.DimCoord(points=np.array([-1, 0, 1], dtype=np.int32), standard_name='latitude', units='degrees', coord_system=cs) cube.add_dim_coord(coord, 0) coord = iris.coords.DimCoord(points=np.array([-1, 0, 1, 2], dtype=np.int32), standard_name='longitude', units='degrees', coord_system=cs) cube.add_dim_coord(coord, 1) return cube
def _create_sample_cube(): cube = Cube(np.array((np.arange(1, 25), np.arange(25, 49))), var_name='co2', units='J') cube.add_dim_coord( iris.coords.DimCoord( np.arange(15., 720., 30.), standard_name='time', units=Unit('days since 1950-01-01 00:00:00', calendar='gregorian'), ), 1, ) cube.add_dim_coord( iris.coords.DimCoord( np.arange(1, 3), standard_name='latitude', ), 0, ) return cube
def _create_areacello(cfg, sample_cube, glob_attrs, out_dir): if not cfg['custom'].get('create_areacello', False): return var_info = cfg['cmor_table'].get_variable('fx', 'areacello') glob_attrs['mip'] = 'fx' lat_coord = sample_cube.coord('latitude') cube = Cube( np.full(lat_coord.shape, cfg['custom']['grid_cell_size'], np.float32), standard_name=var_info.standard_name, long_name=var_info.long_name, var_name=var_info.short_name, units='m2', ) cube.add_aux_coord(lat_coord, (0, 1)) cube.add_aux_coord(sample_cube.coord('longitude'), (0, 1)) cube.add_dim_coord(sample_cube.coord('projection_y_coordinate'), 0) cube.add_dim_coord(sample_cube.coord('projection_x_coordinate'), 1) fix_var_metadata(cube, var_info) set_global_atts(cube, glob_attrs) save_variable(cube, var_info.short_name, out_dir, glob_attrs, zlib=True)
class TestOSGBToLatLon(tests.IrisTest): def setUp(self): path = tests.get_data_path(( "NIMROD", "uk2km", "WO0000000003452", "201007020900_u1096_ng_ey00_visibility0180_screen_2km", )) self.src = iris.load_cube(path)[0] # Cast up to float64, to work around numpy<=1.8 bug with means of # arrays of 32bit floats. self.src.data = self.src.data.astype(np.float64) self.grid = Cube(np.empty((73, 96))) cs = GeogCS(6370000) lat = DimCoord( np.linspace(46, 65, 73), "latitude", units="degrees", coord_system=cs, ) lon = DimCoord( np.linspace(-14, 8, 96), "longitude", units="degrees", coord_system=cs, ) self.grid.add_dim_coord(lat, 0) self.grid.add_dim_coord(lon, 1) def _regrid(self, method): regridder = Regridder(self.src, self.grid, method, "mask") result = regridder(self.src) return result def test_linear(self): res = self._regrid("linear") self.assertArrayShapeStats(res, (73, 96), 17799.296120, 11207.701323) def test_nearest(self): res = self._regrid("nearest") self.assertArrayShapeStats(res, (73, 96), 17808.068828, 11225.314310)
def simple_2d(with_bounds=True): """ Returns an abstract, two-dimensional, optionally bounded, cube. >>> print(simple_2d()) thingness (bar: 3; foo: 4) Dimension coordinates: bar x - foo - x >>> print(repr(simple_2d().data)) [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] """ cube = Cube(np.arange(12, dtype=np.int32).reshape((3, 4))) cube.long_name = "thingness" cube.units = "1" y_points = np.array([2.5, 7.5, 12.5]) y_bounds = np.array([[0, 5], [5, 10], [10, 15]], dtype=np.int32) y_coord = DimCoord( y_points, long_name="bar", units="1", bounds=y_bounds if with_bounds else None, ) x_points = np.array([-7.5, 7.5, 22.5, 37.5]) x_bounds = np.array([[-15, 0], [0, 15], [15, 30], [30, 45]], dtype=np.int32) x_coord = DimCoord( x_points, long_name="foo", units="1", bounds=x_bounds if with_bounds else None, ) cube.add_dim_coord(y_coord, 0) cube.add_dim_coord(x_coord, 1) return cube
def create_difference_cube( cube: Cube, coord_name: str, diff_along_axis: ndarray ) -> Cube: """ Put the difference array into a cube with the appropriate metadata. Args: cube: Cube from which the differences have been calculated. coord_name: The name of the coordinate over which the difference have been calculated. diff_along_axis: Array containing the differences. Returns: Cube containing the differences calculated along the specified axis. """ points = cube.coord(coord_name).points mean_points = (points[1:] + points[:-1]) / 2 # Copy cube metadata and coordinates into a new cube. # Create a new coordinate for the coordinate along which the # difference has been calculated. metadata_dict = copy.deepcopy(cube.metadata._asdict()) diff_cube = Cube(diff_along_axis, **metadata_dict) for coord in cube.dim_coords: dims = cube.coord_dims(coord) if coord.name() in [coord_name]: coord = coord.copy(points=mean_points) diff_cube.add_dim_coord(coord.copy(), dims) for coord in cube.aux_coords: dims = cube.coord_dims(coord) diff_cube.add_aux_coord(coord.copy(), dims) for coord in cube.derived_coords: dims = cube.coord_dims(coord) diff_cube.add_aux_coord(coord.copy(), dims) return diff_cube
def _grid_cube(n_lons, n_lats, lon_outer_bounds, lat_outer_bounds, circular=False): lon_points, lon_bounds = _generate_points_and_bounds( n_lons, lon_outer_bounds) lon = DimCoord(lon_points, "longitude", units="degrees", bounds=lon_bounds, circular=circular) lat_points, lat_bounds = _generate_points_and_bounds( n_lats, lat_outer_bounds) lat = DimCoord(lat_points, "latitude", units="degrees", bounds=lat_bounds) data = np.zeros([n_lats, n_lons]) cube = Cube(data) cube.add_dim_coord(lon, 1) cube.add_dim_coord(lat, 0) return cube
def simple_1d(with_bounds=True): """ Returns an abstract, one-dimensional cube. >>> print(simple_1d()) thingness (foo: 11) Dimension coordinates: foo x >>> print(repr(simple_1d().data)) [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] """ cube = Cube(np.arange(11, dtype=np.int32)) cube.long_name = 'thingness' cube.units = '1' points = np.arange(11, dtype=np.int32) + 1 bounds = np.column_stack([np.arange(11, dtype=np.int32), np.arange(11, dtype=np.int32) + 1]) coord = iris.coords.DimCoord(points, long_name='foo', units='1', bounds=bounds) cube.add_dim_coord(coord, 0) return cube
def set_up_spot_cube(data, phenomenon_standard_name, phenomenon_units): """Create a cube containing multiple realizations.""" cube = Cube(data, standard_name=phenomenon_standard_name, units=phenomenon_units) cube.add_dim_coord(DimCoord([0, 1, 2], "realization", units="1"), 0) time_origin = "hours since 1970-01-01 00:00:00" calendar = "gregorian" tunit = Unit(time_origin, calendar) cube.add_dim_coord( DimCoord(np.array(412227, dtype=np.float64), "time", units=tunit), 1 ) cube.add_dim_coord( DimCoord(np.arange(9, dtype=np.float32), long_name="locnum", units="1"), 2 ) cube.add_aux_coord( AuxCoord( np.linspace(-45.0, 45.0, 9, dtype=np.float32), "latitude", units="degrees" ), data_dims=2, ) cube.add_aux_coord( AuxCoord( np.linspace(120, 180, 9, dtype=np.float32), "longitude", units="degrees" ), data_dims=2, ) return cube
def set_up_cube(zero_point_indices=((0, 0, 7, 7), ), num_time_points=1, num_grid_points=16, num_realization_points=1): """Set up a cube with equal intervals along the x and y axis.""" zero_point_indices = list(zero_point_indices) for index, indices in enumerate(zero_point_indices): if len(indices) == 3: indices = (0, ) + indices zero_point_indices[index] = indices zero_point_indices = tuple(zero_point_indices) data = np.ones((num_realization_points, num_time_points, num_grid_points, num_grid_points), dtype=np.float32) for indices in zero_point_indices: realization_index, time_index, lat_index, lon_index = indices data[realization_index][time_index][lat_index][lon_index] = 0 cube = Cube(data, standard_name="precipitation_amount", units="kg m^-2") cube.add_dim_coord( DimCoord(range(num_realization_points), standard_name='realization'), 0) tunit = Unit("hours since 1970-01-01 00:00:00", "gregorian") time_points = [402192.5 + _ for _ in range(num_time_points)] cube.add_dim_coord( DimCoord(time_points, standard_name="time", units=tunit), 1) step_size = 2000 y_points = np.arange(0., step_size * num_grid_points, step_size, dtype=np.float32) cube.add_dim_coord( DimCoord(y_points, 'projection_y_coordinate', units='m', coord_system=STANDARD_GRID_CCRS), 2) x_points = np.arange(-50000., (step_size * num_grid_points) - 50000, step_size, dtype=np.float32) cube.add_dim_coord( DimCoord(x_points, 'projection_x_coordinate', units='m', coord_system=STANDARD_GRID_CCRS), 3) return cube
def setUp(self): shape = (6, 14, 19) n_time, n_lat, n_lon = shape n_data = n_time * n_lat * n_lon cube = Cube(np.arange(n_data, dtype=np.int32).reshape(shape)) coord = iris.coords.DimCoord( points=np.arange(n_time), standard_name="time", units="hours since epoch", ) cube.add_dim_coord(coord, 0) cs = iris.coord_systems.GeogCS(6371229) coord = iris.coords.DimCoord( points=np.linspace(-90, 90, n_lat), standard_name="latitude", units="degrees", coord_system=cs, ) cube.add_dim_coord(coord, 1) coord = iris.coords.DimCoord( points=np.linspace(-180, 180, n_lon), standard_name="longitude", units="degrees", coord_system=cs, ) cube.add_dim_coord(coord, 2) self.cube = cube
def set_up_cube(): """Create a cube for testing Returns: cube : iris.cube.Cube dummy cube for testing """ data = np.zeros((2, 2, 2)) data[0][:][:] = 0.0 data[1][:][:] = 1.0 cube = Cube(data, standard_name="precipitation_amount", units="kg m^-2 s^-1") cube.add_dim_coord(DimCoord(np.linspace(-45.0, 45.0, 2), 'latitude', units='degrees'), 1) cube.add_dim_coord(DimCoord(np.linspace(120, 180, 2), 'longitude', units='degrees'), 2) time_origin = "hours since 1970-01-01 00:00:00" calendar = "gregorian" tunit = Unit(time_origin, calendar) time_coord = DimCoord([402192.5, 402193.5], "time", units=tunit) cube.add_dim_coord(time_coord, 0) dummy_scalar_coord = iris.coords.AuxCoord(1, long_name='scalar_coord', units='no_unit') cube.add_aux_coord(dummy_scalar_coord) return cube
def setUp(self): """Create a cube with a single non-zero point.""" data = np.zeros((2, 2, 2)) data[0][:][:] = 1.0 data[1][:][:] = 2.0 cube = Cube(data, standard_name="precipitation_amount", units="kg m^-2 s^-1") cube.add_dim_coord( DimCoord(np.linspace(-45.0, 45.0, 2), 'latitude', units='degrees'), 1) cube.add_dim_coord( DimCoord(np.linspace(120, 180, 2), 'longitude', units='degrees'), 2) time_origin = "hours since 1970-01-01 00:00:00" calendar = "gregorian" tunit = Unit(time_origin, calendar) cube.add_dim_coord(DimCoord([402192.5, 402193.5], "time", units=tunit), 0) self.cube = cube new_scalar_coord = iris.coords.AuxCoord(1, long_name='dummy_scalar_coord', units='no_unit') cube_with_scalar = cube.copy() cube_with_scalar.add_aux_coord(new_scalar_coord) self.cube_with_scalar = cube_with_scalar
def setUp(self): """Create a cube with a single non-zero point.""" latitude = DimCoord(np.linspace(-45.0, 45.0, 5), 'latitude', units='degrees') longitude = DimCoord(np.linspace(120, 180, 5), 'longitude', units='degrees') self.fuzzy_factor = 0.5 data = np.zeros((1, 5, 5)) data[0][2][2] = 0.5 # ~2 mm/hr cube = Cube(data, standard_name="precipitation_amount", units="kg m^-2 s^-1") cube.add_dim_coord(latitude, 1) cube.add_dim_coord(longitude, 2) time_origin = "hours since 1970-01-01 00:00:00" calendar = "gregorian" tunit = Unit(time_origin, calendar) cube.add_dim_coord(DimCoord([402192.5], "time", units=tunit), 0) self.cube = cube # cube to test unit conversion rate_data = np.zeros((5, 5)) rate_data[2][2] = 1.39e-6 # 5.004 mm/hr rate_cube = Cube(rate_data, 'rainfall_rate', units='m s-1', dim_coords_and_dims=[(latitude, 0), (longitude, 1)]) self.rate_cube = rate_cube
def create_cube(lon_min, lon_max, bounds=False): n_lons = max(lon_min, lon_max) - min(lon_max, lon_min) data = np.arange(4 * 3 * n_lons, dtype='f4').reshape(4, 3, n_lons) data = biggus.NumpyArrayAdapter(data) cube = Cube(data, standard_name='x_wind', units='ms-1') cube.add_dim_coord( iris.coords.DimCoord([0, 20, 40, 80], long_name='level_height', units='m'), 0) cube.add_aux_coord( iris.coords.AuxCoord([1.0, 0.9, 0.8, 0.6], long_name='sigma'), 0) cube.add_dim_coord( iris.coords.DimCoord([-45, 0, 45], 'latitude', units='degrees'), 1) step = 1 if lon_max > lon_min else -1 cube.add_dim_coord( iris.coords.DimCoord(np.arange(lon_min, lon_max, step), 'longitude', units='degrees'), 2) if bounds: cube.coord('longitude').guess_bounds() cube.add_aux_coord( iris.coords.AuxCoord(np.arange(3 * n_lons).reshape(3, n_lons) * 10, 'surface_altitude', units='m'), [1, 2]) cube.add_aux_factory( iris.aux_factory.HybridHeightFactory(cube.coord('level_height'), cube.coord('sigma'), cube.coord('surface_altitude'))) return cube
def _create_sample_full_cube(): cube = Cube(np.zeros((4, 180, 360)), var_name='co2', units='J') cube.add_dim_coord( iris.coords.DimCoord( np.array([10., 40., 70., 110.]), standard_name='time', units=Unit('days since 1950-01-01 00:00:00', calendar='gregorian'), ), 0, ) cube.add_dim_coord( iris.coords.DimCoord( np.arange(-90., 90., 1.), standard_name='latitude', units='degrees', ), 1, ) cube.add_dim_coord( iris.coords.DimCoord( np.arange(0., 360., 1.), standard_name='longitude', units='degrees', ), 2, ) cube.coord("time").guess_bounds() cube.coord("longitude").guess_bounds() cube.coord("latitude").guess_bounds() return cube
def set_up_cube(data, phenomenon_standard_name, phenomenon_units, realizations=np.array([0]), timesteps=1, y_dimension_length=3, x_dimension_length=3): """Create a cube containing multiple realizations.""" coord_placer = 0 cube = Cube(data, standard_name=phenomenon_standard_name, units=phenomenon_units) if len(realizations) > 1: realizations = DimCoord(realizations, "realization") cube.add_dim_coord(realizations, coord_placer) coord_placer = 1 else: cube.add_aux_coord(AuxCoord(realizations, 'realization', units='1')) time_origin = "hours since 1970-01-01 00:00:00" calendar = "gregorian" tunit = Unit(time_origin, calendar) cube.add_aux_coord(AuxCoord(np.linspace(402192.5, 402292.5, timesteps), "time", units=tunit)) cube.add_dim_coord(DimCoord(np.linspace(0, 10000, y_dimension_length), 'projection_y_coordinate', units='m'), coord_placer) cube.add_dim_coord(DimCoord(np.linspace(0, 10000, x_dimension_length), 'projection_x_coordinate', units='m'), coord_placer+1) return cube
def test_create_cube_4D(): """Test creation of 2D output grid.""" data = np.ones([4, 2, 3, 5]) # Create a source cube with metadata and scalar coords src_cube = Cube(np.zeros([4, 5, 5])) src_cube.units = "K" src_cube.attributes = {"a": 1} src_cube.standard_name = "air_temperature" scalar_height = AuxCoord([5], units="m", standard_name="height") scalar_time = DimCoord([10], units="s", standard_name="time") src_cube.add_aux_coord(scalar_height) src_cube.add_aux_coord(scalar_time) first_coord = DimCoord(np.arange(4), standard_name="air_pressure") src_cube.add_dim_coord(first_coord, 0) last_coord = AuxCoord(np.arange(5), long_name="last_coord") src_cube.add_aux_coord(last_coord, 2) multidim_coord = AuxCoord(np.ones([4, 5]), long_name="2d_coord") src_cube.add_aux_coord(multidim_coord, (0, 2)) ignored_coord = AuxCoord(np.arange(5), long_name="ignore") src_cube.add_aux_coord(ignored_coord, 1) mesh_dim = 1 grid_x = iris.coords.DimCoord(np.arange(3), standard_name="longitude") grid_y = iris.coords.DimCoord(np.arange(2), standard_name="latitude") cube = _create_cube(data, src_cube, (mesh_dim,), (grid_x, grid_y), 2) src_metadata = src_cube.metadata expected_cube = iris.cube.Cube(data) expected_cube.metadata = src_metadata expected_cube.add_dim_coord(grid_x, 2) expected_cube.add_dim_coord(grid_y, 1) expected_cube.add_dim_coord(first_coord, 0) expected_cube.add_aux_coord(last_coord, 3) expected_cube.add_aux_coord(multidim_coord, (0, 3)) expected_cube.add_aux_coord(scalar_height) expected_cube.add_aux_coord(scalar_time) assert expected_cube == cube
def set_up_probability_above_threshold_spot_cube(data, phenomenon_standard_name, phenomenon_units, forecast_thresholds=np.array( [8, 10, 12]), y_dimension_length=9, x_dimension_length=9): """ Create a cube containing multiple realizations, where one of the dimensions is an index used for spot forecasts. """ cube_long_name = ("probability_of_{}".format(phenomenon_standard_name)) cube = Cube(data, long_name=cube_long_name, units=phenomenon_units) coord_long_name = "threshold" cube.add_dim_coord( DimCoord(forecast_thresholds, long_name=coord_long_name, units='degreesC'), 0) time_origin = "hours since 1970-01-01 00:00:00" calendar = "gregorian" tunit = Unit(time_origin, calendar) cube.add_dim_coord(DimCoord([402192.5], "time", units=tunit), 1) cube.add_dim_coord(DimCoord(np.arange(9), long_name='locnum', units="1"), 2) cube.add_aux_coord(AuxCoord(np.linspace(-45.0, 45.0, y_dimension_length), 'latitude', units='degrees'), data_dims=2) cube.add_aux_coord(AuxCoord(np.linspace(120, 180, x_dimension_length), 'longitude', units='degrees'), data_dims=2) cube.attributes["relative_to_threshold"] = "above" return cube
def test_curvilinear(): """ Test for :func:`esmf_regrid.experimental.unstructured_scheme.MeshToGridESMFRegridder`. Tests with curvilinear source cube. """ mesh = _full_mesh() mesh_length = mesh.connectivity(contains_face=True).shape[0] h = 2 t = 3 height = DimCoord(np.arange(h), standard_name="height") time = DimCoord(np.arange(t), standard_name="time") src_data = np.empty([t, mesh_length, h]) src_data[:] = np.arange(t * h).reshape([t, h])[:, np.newaxis, :] mesh_cube = Cube(src_data) mesh_coord_x, mesh_coord_y = mesh.to_MeshCoords("face") mesh_cube.add_aux_coord(mesh_coord_x, 1) mesh_cube.add_aux_coord(mesh_coord_y, 1) mesh_cube.add_dim_coord(time, 0) mesh_cube.add_dim_coord(height, 2) n_lons = 6 n_lats = 5 lon_bounds = (-180, 180) lat_bounds = (-90, 90) tgt = _curvilinear_cube(n_lons, n_lats, lon_bounds, lat_bounds) src_cube = mesh_cube.copy() src_cube.transpose([1, 0, 2]) regridder = MeshToGridESMFRegridder(src_cube, tgt) result = regridder(mesh_cube) # Lenient check for data. expected_data = np.empty([t, n_lats, n_lons, h]) expected_data[:] = np.arange(t * h).reshape(t, h)[:, np.newaxis, np.newaxis, :] assert np.allclose(expected_data, result.data) expected_cube = Cube(expected_data) expected_cube.add_dim_coord(time, 0) expected_cube.add_aux_coord(tgt.coord("latitude"), [1, 2]) expected_cube.add_aux_coord(tgt.coord("longitude"), [1, 2]) expected_cube.add_dim_coord(height, 3) # Check metadata and scalar coords. result.data = expected_data assert expected_cube == result
def test_osgb_to_latlon(self): path = tests.get_data_path( ('NIMROD', 'uk2km', 'WO0000000003452', '201007020900_u1096_ng_ey00_visibility0180_screen_2km')) src = iris.load_cube(path)[0] src.data = src.data.astype(np.float32) grid = Cube(np.empty((73, 96))) cs = GeogCS(6370000) lat = DimCoord(np.linspace(46, 65, 73), 'latitude', units='degrees', coord_system=cs) lon = DimCoord(np.linspace(-14, 8, 96), 'longitude', units='degrees', coord_system=cs) grid.add_dim_coord(lat, 0) grid.add_dim_coord(lon, 1) result = regrid(src, grid) qplt.pcolor(result, antialiased=False) qplt.plt.gca().coastlines() self.check_graphic()
def test_time_360(self): cube = Cube(np.array([0, 1, 2, 3, 4]), long_name="ts") time_unit = cf_units.Unit("days since 2000-01-01 00:00", calendar=cf_units.CALENDAR_360_DAY) time_coord = DimCoord([0, 100.1, 200.2, 300.3, 400.4], long_name="time", units=time_unit) cube.add_dim_coord(time_coord, 0) if netCDF4.__version__ > '1.2.4': expected_index = [netcdftime.Datetime360Day(2000, 1, 1, 0, 0), netcdftime.Datetime360Day(2000, 4, 11, 2, 24), netcdftime.Datetime360Day(2000, 7, 21, 4, 48), netcdftime.Datetime360Day(2000, 11, 1, 7, 12), netcdftime.Datetime360Day(2001, 2, 11, 9, 36)] else: expected_index = [netcdftime.datetime(2000, 1, 1, 0, 0), netcdftime.datetime(2000, 4, 11, 2, 24), netcdftime.datetime(2000, 7, 21, 4, 48), netcdftime.datetime(2000, 11, 1, 7, 12), netcdftime.datetime(2001, 2, 11, 9, 36)] series = iris.pandas.as_series(cube) self.assertArrayEqual(series, cube.data) self.assertArrayEqual(series.index, expected_index)
class TestAll(unittest.TestCase): """Test fixes for all vars.""" def setUp(self): """Prepare tests.""" self.cube = Cube([[1.0, 2.0]], var_name='co2', units='J') self.cube.add_dim_coord( DimCoord([0.0, 1.0], standard_name='time', units=Unit('days since 0001-01', calendar='gregorian')), 1) self.cube.add_dim_coord( DimCoord([180], standard_name='longitude', units=Unit('degrees')), 0) self.fix = AllVars(None) def test_get(self): """Test fix get.""" self.assertListEqual( Fix.get_fixes('CMIP5', 'FGOALS-G2', 'Amon', 'tas'), [AllVars(None)]) def test_fix_metadata(self): """Test calendar fix.""" cube = self.fix.fix_metadata([self.cube])[0] time = cube.coord('time') self.assertEqual(time.units.origin, 'day since 1-01-01 00:00:00.000000') self.assertEqual(time.units.calendar, 'gregorian') def test_fix_metadata_dont_fail_if_not_longitude(self): """Test calendar fix.""" self.cube.remove_coord('longitude') self.fix.fix_metadata([self.cube]) def test_fix_metadata_dont_fail_if_not_time(self): """Test calendar fix.""" self.cube.remove_coord('time') self.fix.fix_metadata([self.cube])
class TestAllVars(unittest.TestCase): """Test fixes for all vars.""" def setUp(self): """Prepare tests.""" self.cube = Cube([1.0, 2.0], var_name='co2', units='J') reference_dates = [ datetime(300, 1, 16, 12), # e.g. piControl datetime(1850, 1, 16, 12) # e.g. historical ] esgf_time_units = { 'unit': 'days since 0001-01-01', 'calendar': 'proleptic_gregorian' } time_points = date2num(reference_dates, **esgf_time_units) self.cube.add_dim_coord( DimCoord(time_points, 'time', 'time', 'time', Unit(**esgf_time_units)), data_dim=0) self.fix = AllVars(None) def test_get(self): """Test getting of fix.""" self.assertListEqual( Fix.get_fixes('CMIP5', 'ACCESS1-3', 'Amon', 'tas'), [AllVars(None)]) def test_fix_metadata(self): """Test fix for bad calendar.""" cube = self.fix.fix_metadata([self.cube])[0] time = cube.coord('time') dates = num2date(time.points, time.units.name, time.units.calendar) self.assertEqual(time.units.calendar, 'gregorian') self.assertEqual(dates[0].strftime('%Y%m%d%H%M'), '30001161200') self.assertEqual(dates[1].strftime('%Y%m%d%H%M'), '185001161200') def test_fix_metadata_if_not_time(self): """Test calendar fix do not fail if no time coord present.""" self.cube.remove_coord('time') self.fix.fix_metadata([self.cube])