def test_cross_section(self): # Slice to get a cross section. # Constant latitude src = self.realistic_cube[0, :, 10, :] lon = _resampled_coord(src.coord("grid_longitude"), 0.6) shape = list(src.shape) shape[1] = len(lon.points) data = np.zeros(shape) dest = iris.cube.Cube(data) dest.add_dim_coord(lon, 1) dest.add_aux_coord(src.coord("grid_latitude").copy(), None) res = regrid_area_weighted(src, dest) self.assertCMLApproxData(res, RESULT_DIR + ("const_lat_cross_section.cml",)) # Constant longitude src = self.realistic_cube[0, :, :, 10] lat = _resampled_coord(src.coord("grid_latitude"), 0.6) shape = list(src.shape) shape[1] = len(lat.points) data = np.zeros(shape) dest = iris.cube.Cube(data) dest.add_dim_coord(lat, 1) dest.add_aux_coord(src.coord("grid_longitude").copy(), None) res = regrid_area_weighted(src, dest) self.assertCMLApproxData(res, RESULT_DIR + ("const_lon_cross_section.cml",))
def test_cross_section(self): # Slice to get a cross section. # Constant latitude src = self.realistic_cube[0, :, 10, :] lon = _resampled_coord(src.coord('grid_longitude'), 0.6) shape = list(src.shape) shape[1] = len(lon.points) data = np.zeros(shape) dest = iris.cube.Cube(data) dest.add_dim_coord(lon, 1) dest.add_aux_coord(src.coord('grid_latitude').copy(), None) res = regrid_area_weighted(src, dest) self.assertCMLApproxData(res, RESULT_DIR + ('const_lat_cross_section.cml',)) # Plot a single slice. qplt.plot(res[0]) qplt.plot(src[0], 'r') self.check_graphic() # Constant longitude src = self.realistic_cube[0, :, :, 10] lat = _resampled_coord(src.coord('grid_latitude'), 0.6) shape = list(src.shape) shape[1] = len(lat.points) data = np.zeros(shape) dest = iris.cube.Cube(data) dest.add_dim_coord(lat, 1) dest.add_aux_coord(src.coord('grid_longitude').copy(), None) res = regrid_area_weighted(src, dest) self.assertCMLApproxData(res, RESULT_DIR + ('const_lon_cross_section.cml',)) # Plot a single slice. qplt.plot(res[0]) qplt.plot(src[0], 'r') self.check_graphic()
def test_cross_section(self): # Slice to get a cross section. # Constant latitude src = self.realistic_cube[0, :, 10, :] lon = _resampled_coord(src.coord('grid_longitude'), 0.6) shape = list(src.shape) shape[1] = len(lon.points) data = np.zeros(shape) dest = iris.cube.Cube(data) dest.add_dim_coord(lon, 1) dest.add_aux_coord(src.coord('grid_latitude').copy(), None) res = regrid_area_weighted(src, dest) self.assertCMLApproxData( res, RESULT_DIR + ('const_lat_cross_section.cml', )) # Plot a single slice. qplt.plot(res[0]) qplt.plot(src[0], 'r') self.check_graphic() # Constant longitude src = self.realistic_cube[0, :, :, 10] lat = _resampled_coord(src.coord('grid_latitude'), 0.6) shape = list(src.shape) shape[1] = len(lat.points) data = np.zeros(shape) dest = iris.cube.Cube(data) dest.add_dim_coord(lat, 1) dest.add_aux_coord(src.coord('grid_longitude').copy(), None) res = regrid_area_weighted(src, dest) self.assertCMLApproxData( res, RESULT_DIR + ('const_lon_cross_section.cml', )) # Plot a single slice. qplt.plot(res[0]) qplt.plot(src[0], 'r') self.check_graphic()
def test_regrid_reorder_axis(self): src = self.realistic_cube[0, :4, :3, :2] z = src.coord("model_level_number") lat = src.coord("grid_latitude") lon = src.coord("grid_longitude") dest = _resampled_grid(self.realistic_cube[0, 0, :3, :2], 3, 3) res = regrid_area_weighted(src, dest) self.assertArrayShapeStats(src, (4, 3, 2), 288.08868, 0.008262919) self.assertArrayShapeStats(res, (4, 9, 6), 288.08865, 0.00826281) # Reshape src so that the coords are ordered [x, z, y], # the mean and std statistics should be the same data = np.moveaxis(src.data.copy(), 2, 0) src = iris.cube.Cube(data) src.add_dim_coord(lat, 2) src.add_dim_coord(z, 1) src.add_dim_coord(lon, 0) res = regrid_area_weighted(src, dest) self.assertArrayShapeStats(src, (2, 4, 3), 288.08868, 0.008262919) self.assertArrayShapeStats(res, (6, 4, 9), 288.08865, 0.00826281) # Reshape src so that the coords are ordered [y, x, z], # the mean and std statistics should be the same data = np.moveaxis(src.data.copy(), 2, 0) src = iris.cube.Cube(data) src.add_dim_coord(z, 2) src.add_dim_coord(lon, 1) src.add_dim_coord(lat, 0) dest = _resampled_grid(self.realistic_cube[0, 0, :3, :2], 3, 3) res = regrid_area_weighted(src, dest) self.assertArrayShapeStats(src, (3, 2, 4), 288.08868, 0.008262919) self.assertArrayShapeStats(res, (9, 6, 4), 288.08865, 0.00826281)
def test_cross_section(self): # Slice to get a cross section. # Constant latitude src = self.realistic_cube[0, :, 10, :] lon = _resampled_coord(src.coord("grid_longitude"), 0.6) shape = list(src.shape) shape[1] = len(lon.points) data = np.zeros(shape) dest = iris.cube.Cube(data) dest.add_dim_coord(lon, 1) dest.add_aux_coord(src.coord("grid_latitude").copy(), None) res = regrid_area_weighted(src, dest) self.assertCMLApproxData( res, RESULT_DIR + ("const_lat_cross_section.cml", )) # Constant longitude src = self.realistic_cube[0, :, :, 10] lat = _resampled_coord(src.coord("grid_latitude"), 0.6) shape = list(src.shape) shape[1] = len(lat.points) data = np.zeros(shape) dest = iris.cube.Cube(data) dest.add_dim_coord(lat, 1) dest.add_aux_coord(src.coord("grid_longitude").copy(), None) res = regrid_area_weighted(src, dest) self.assertCMLApproxData( res, RESULT_DIR + ("const_lon_cross_section.cml", ))
def test_scalar_source_cube(self): src = self.simple_cube[1, 2] # Extend dest beyond src grid dest = src.copy() dest.coord("latitude").bounds = np.array([[-0.5, 1.5]]) res = regrid_area_weighted(src, dest) self.assertTrue(res.data.mask.all()) # Shrink dest to 1/4 of src dest = src.copy() dest.coord("latitude").bounds = np.array([[0.25, 0.75]]) dest.coord("longitude").bounds = np.array([[1.25, 1.75]]) res = regrid_area_weighted(src, dest) self.assertEqual(res.data, src.data)
def test_different_cs(self): src = self.simple_cube.copy() src_cs = copy.copy(src.coord("latitude").coord_system) src_cs.semi_major_axis = 7000000 src.coord("longitude").coord_system = src_cs src.coord("latitude").coord_system = src_cs dest = self.simple_cube.copy() dest_cs = copy.copy(src_cs) dest_cs.semi_major_axis = 7000001 dest.coord("longitude").coord_system = dest_cs dest.coord("latitude").coord_system = dest_cs with self.assertRaises(ValueError): regrid_area_weighted(src, dest)
def test_different_cs(self): src = self.simple_cube.copy() src_cs = copy.copy(src.coord('latitude').coord_system) src_cs.semi_major_axis = 7000000 src.coord('longitude').coord_system = src_cs src.coord('latitude').coord_system = src_cs dest = self.simple_cube.copy() dest_cs = copy.copy(src_cs) dest_cs.semi_major_axis = 7000001 dest.coord('longitude').coord_system = dest_cs dest.coord('latitude').coord_system = dest_cs with self.assertRaises(ValueError): regrid_area_weighted(src, dest)
def test_regrid_transposed(self): src = self.simple_cube.copy() dest = _subsampled_grid(src, 2, 3) # Transpose src so that the coords are not y, x ordered. src.transpose() res = regrid_area_weighted(src, dest) self.assertCMLApproxData(res, RESULT_DIR + ('trasposed.cml',)) # Using original and transposing the result should give the # same answer. src = self.simple_cube.copy() res = regrid_area_weighted(src, dest) res.transpose() self.assertCMLApproxData(res, RESULT_DIR + ('trasposed.cml',))
def test_regrid_transposed(self): src = self.simple_cube.copy() dest = _subsampled_grid(src, 2, 3) # Transpose src so that the coords are not y, x ordered. src.transpose() res = regrid_area_weighted(src, dest) self.assertCMLApproxData(res, RESULT_DIR + ('trasposed.cml', )) # Using original and transposing the result should give the # same answer. src = self.simple_cube.copy() res = regrid_area_weighted(src, dest) res.transpose() self.assertCMLApproxData(res, RESULT_DIR + ('trasposed.cml', ))
def test_scalar_source_cube(self): src = self.simple_cube[1, 2] # Extend dest beyond src grid dest = src.copy() dest.coord('latitude').bounds = np.array([[-0.5, 1.5]]) res = regrid_area_weighted(src, dest) self.assertTrue(res.data.mask.all()) # Shrink dest to 1/4 of src dest = src.copy() dest.coord('latitude').bounds = np.array([[0.25, 0.75]]) dest.coord('longitude').bounds = np.array([[1.25, 1.75]]) res = regrid_area_weighted(src, dest) self.assertEqual(res.data, src.data)
def test_cross_section(self): # Slice to get a cross section. # Constant latitude src = self.realistic_cube[0, :, 10, :] lon = _resampled_coord(src.coord("grid_longitude"), 0.6) shape = list(src.shape) shape[1] = len(lon.points) data = np.zeros(shape) dest = iris.cube.Cube(data) dest.add_dim_coord(lon, 1) dest.add_aux_coord(src.coord("grid_latitude").copy(), None) res = regrid_area_weighted(src, dest) self.assertCMLApproxData( res, RESULT_DIR + ("const_lat_cross_section.cml",) ) # Constant latitude, data order [x, z] # Using original and transposing the result should give the # same answer. src.transpose() dest.transpose() res = regrid_area_weighted(src, dest) res.transpose() self.assertCMLApproxData( res, RESULT_DIR + ("const_lat_cross_section.cml",) ) # Constant longitude src = self.realistic_cube[0, :, :, 10] lat = _resampled_coord(src.coord("grid_latitude"), 0.6) shape = list(src.shape) shape[1] = len(lat.points) data = np.zeros(shape) dest = iris.cube.Cube(data) dest.add_dim_coord(lat, 1) dest.add_aux_coord(src.coord("grid_longitude").copy(), None) res = regrid_area_weighted(src, dest) self.assertCMLApproxData( res, RESULT_DIR + ("const_lon_cross_section.cml",) ) # Constant longitude, data order [y, z] # Using original and transposing the result should give the # same answer. src.transpose() dest.transpose() res = regrid_area_weighted(src, dest) res.transpose() self.assertCMLApproxData( res, RESULT_DIR + ("const_lon_cross_section.cml",) )
def test_global_data_increase_res(self): src = iris.tests.stock.global_pp() src.coord('latitude').guess_bounds() src.coord('longitude').guess_bounds() dest = _resampled_grid(src, 1.5, 1.5) res = regrid_area_weighted(src, dest) self.assertArrayShapeStats(res, (109, 144), 280.349625, 16.073397)
def test_global_data_same_res(self): src = iris.tests.stock.global_pp() src.coord('latitude').guess_bounds() src.coord('longitude').guess_bounds() res = regrid_area_weighted(src, src) qplt.pcolormesh(res) self.check_graphic()
def test_lazy_nop(self): src = self.realistic_cube[:2, :3, :10, :10] src.data = da.asarray(src.data, chunks=((1, 1), (2, 1), (10, ), (10, ))) res = regrid_area_weighted(src, src) self.assertTrue(res.has_lazy_data()) self.assertEqual(res, src)
def test_global_data_increase_res(self): src = iris.tests.stock.global_pp() src.coord('latitude').guess_bounds() src.coord('longitude').guess_bounds() dest = _resampled_grid(src, 1.5, 1.5) res = regrid_area_weighted(src, dest) self.assertArrayShapeStats(res, (109, 144), 279.955722, 16.313940)
def test_non_contiguous_bounds(self): src = self.simple_cube.copy() bounds = src.coord("latitude").bounds.copy() bounds[1, 1] -= 0.1 src.coord("latitude").bounds = bounds dest = self.simple_cube.copy() with self.assertRaises(ValueError): regrid_area_weighted(src, dest) src = self.simple_cube.copy() dest = self.simple_cube.copy() bounds = dest.coord("longitude").bounds.copy() bounds[1, 1] -= 0.1 dest.coord("longitude").bounds = bounds with self.assertRaises(ValueError): regrid_area_weighted(src, dest)
def test_no_y_overlap(self): src = self.simple_cube dest = _scaled_and_offset_grid(src, 1.0, 1.0, 0.0, (np.max(src.coord('latitude').bounds) - np.min(src.coord('latitude').bounds))) res = regrid_area_weighted(src, dest) self.assertTrue(res.data.mask.all())
def test_global_data_increase_res(self): src = iris.tests.stock.global_pp() src.coord("latitude").guess_bounds() src.coord("longitude").guess_bounds() dest = _resampled_grid(src, 1.5, 1.5) res = regrid_area_weighted(src, dest) self.assertArrayShapeStats(res, (109, 144), 280.349625, 16.073397)
def test_unqeual_area_numbers(self): # Remove coords system and units so it is no longer spherical. self.simple_cube.coord("latitude").coord_system = None self.simple_cube.coord("latitude").units = None self.simple_cube.coord("longitude").coord_system = None self.simple_cube.coord("longitude").units = None # Reduce src to two cells along x, one three times the size # of the other. src = self.simple_cube.copy() src = _subsampled_grid(src, 2, 2) lon = src.coord("longitude") points = lon.points.copy() bounds = [[-1, 0], [0, 3]] lon = lon.copy(points=points, bounds=bounds) src.replace_coord(lon) # Reduce src to two cells along y, one 2 times the size # of the other. lat = src.coord("latitude") points = lat.points.copy() bounds = [[-1, 0], [0, 2]] lat = lat.copy(points=points, bounds=bounds) src.replace_coord(lat) # Populate with data src.data = np.arange(src.data.size).reshape(src.shape) + 1.23 # dest is a single cell over the whole area. dest = _subsampled_grid(self.simple_cube, 4, 3) res = regrid_area_weighted(src, dest) expected_val = (1.0 / 12.0 * src.data[0, 0] + 2.0 / 12.0 * np.mean(src.data[1:, 0]) + 3.0 / 12.0 * np.mean(src.data[0, 1:]) + 6.0 / 12.0 * np.mean(src.data[1:, 1:])) self.assertAlmostEqual(expected_val, res.data)
def test_global_data_increase_res(self): src = iris.tests.stock.global_pp() src.coord("latitude").guess_bounds() src.coord("longitude").guess_bounds() dest = _resampled_grid(src, 1.5, 1.5) res = regrid_area_weighted(src, dest) self.assertArrayShapeStats(res, (109, 144), 279.955722, 16.313940)
def test_non_contiguous_bounds(self): src = self.simple_cube.copy() bounds = src.coord('latitude').bounds.copy() bounds[1, 1] -= 0.1 src.coord('latitude').bounds = bounds dest = self.simple_cube.copy() with self.assertRaises(ValueError): regrid_area_weighted(src, dest) src = self.simple_cube.copy() dest = self.simple_cube.copy() bounds = dest.coord('longitude').bounds.copy() bounds[1, 1] -= 0.1 dest.coord('longitude').bounds = bounds with self.assertRaises(ValueError): regrid_area_weighted(src, dest)
def test_regrid_to_non_int_frac(self): # Create dest such that bounds do not line up # with src: src.shape = (3, 4), dest.shape = (2, 3) src = self.simple_cube dest = _resampled_grid(src, 0.75, 0.67) res = regrid_area_weighted(src, dest) self.assertCMLApproxData(res, RESULT_DIR + ("lower.cml", ))
def test_no_y_overlap(self): src = self.simple_cube dest = _scaled_and_offset_grid( src, 1.0, 1.0, 0.0, (np.max(src.coord("latitude").bounds) - np.min(src.coord("latitude").bounds)) ) res = regrid_area_weighted(src, dest) self.assertTrue(res.data.mask.all())
def test_global_data_reduce_res(self): src = iris.tests.stock.global_pp() src.coord("latitude").guess_bounds() src.coord("longitude").guess_bounds() dest = _resampled_grid(src, 0.4, 0.3) res = regrid_area_weighted(src, dest) self.assertArrayShapeStats(res, (21, 38), 280.484932, 15.831545)
def test_scalar(self): src = self.realistic_cube i = 2 j = 3 dest = src[0, 0, i, j] res = regrid_area_weighted(src, dest) self.assertEqual(res, src[:, :, i, j])
def test_unqeual_area_numbers(self): # Remove coords system and units so it is no longer spherical. self.simple_cube.coord('latitude').coord_system = None self.simple_cube.coord('latitude').units = None self.simple_cube.coord('longitude').coord_system = None self.simple_cube.coord('longitude').units = None # Reduce src to two cells along x, one three times the size # of the other. src = self.simple_cube.copy() src = _subsampled_grid(src, 2, 2) lon = src.coord('longitude') points = lon.points.copy() bounds = [[-1, 0], [0, 3]] lon = lon.copy(points=points, bounds=bounds) src.replace_coord(lon) # Reduce src to two cells along y, one 2 times the size # of the other. lat = src.coord('latitude') points = lat.points.copy() bounds = [[-1, 0], [0, 2]] lat = lat.copy(points=points, bounds=bounds) src.replace_coord(lat) # Populate with data src.data = np.arange(src.data.size).reshape(src.shape) + 1.23 # dest is a single cell over the whole area. dest = _subsampled_grid(self.simple_cube, 4, 3) res = regrid_area_weighted(src, dest) expected_val = (1. / 12. * src.data[0, 0] + 2. / 12. * np.mean(src.data[1:, 0]) + 3. / 12. * np.mean(src.data[0, 1:]) + 6. / 12. * np.mean(src.data[1:, 1:])) self.assertAlmostEqual(expected_val, res.data)
def test_masked_data_all_false(self): src = self.simple_cube.copy() src.data = ma.masked_array(src.data, mask=False, fill_value=999) dest = _resampled_grid(self.simple_cube, 2.3, 2.4) res = regrid_area_weighted(src, dest) self.assertArrayEqual(res.data.mask, False) self.assertArrayEqual(res.data.fill_value, 999)
def test_regrid_to_non_int_frac(self): # Create dest such that bounds do not line up # with src: src.shape = (3, 4), dest.shape = (2, 3) src = self.simple_cube dest = _resampled_grid(src, 0.75, 0.67) res = regrid_area_weighted(src, dest) self.assertCMLApproxData(res, RESULT_DIR + ('lower.cml',))
def test_global_data_reduce_res(self): src = iris.tests.stock.global_pp() src.coord("latitude").guess_bounds() src.coord("longitude").guess_bounds() dest = _resampled_grid(src, 0.4, 0.3) res = regrid_area_weighted(src, dest) self.assertArrayShapeStats(res, (21, 38), 280.137698, 15.998966)
def test_global_data_increase_res(self): src = iris.tests.stock.global_pp() src.coord('latitude').guess_bounds() src.coord('longitude').guess_bounds() dest = _resampled_grid(src, 1.5, 1.5) res = regrid_area_weighted(src, dest) qplt.pcolormesh(res) self.check_graphic()
def test_missing_data(self): src = self.simple_cube.copy() src.data = ma.masked_array(src.data) src.data[1, 2] = ma.masked dest = _resampled_grid(self.simple_cube, 2.3, 2.4) res = regrid_area_weighted(src, dest) mask = np.zeros((7, 9), bool) mask[slice(2, 5), slice(4, 7)] = True self.assertArrayEqual(res.data.mask, mask)
def test_one_point(self): src = self.simple_cube.copy() for n in range(10): i = random.randint(0, src.shape[0] - 1) j = random.randint(0, src.shape[1] - 1) indices = tuple([slice(i, i + 1), slice(j, j + 1)]) dest = src[indices] res = regrid_area_weighted(src, dest) self.assertTrue(res, src[indices])
def test_ten_by_ten_subset(self): src = _resampled_grid(self.simple_cube, 20, 20) for n in range(10): i = random.randint(0, src.shape[0] - 10) j = random.randint(0, src.shape[1] - 10) indices = tuple([slice(i, i + 10), slice(j, j + 10)]) dest = src[indices] res = regrid_area_weighted(src, dest) self.assertTrue(res, src[indices])
def test_equal_area_numbers(self): # Remove coords system and units so it is no longer spherical. self.simple_cube.coord('latitude').coord_system = None self.simple_cube.coord('latitude').units = None self.simple_cube.coord('longitude').coord_system = None self.simple_cube.coord('longitude').units = None # Reduce to a single cell src = self.simple_cube.copy() dest = _subsampled_grid(src, 4, 3) res = regrid_area_weighted(src, dest) expected_val = np.mean(src.data) self.assertAlmostEqual(expected_val, res.data) # Reduce to two cells along x src = self.simple_cube.copy() dest = _subsampled_grid(src, 2, 3) res = regrid_area_weighted(src, dest) expected_val_left = np.mean(src.data[:, 0:2]) self.assertEqual(expected_val_left, res.data[0]) expected_val_right = np.mean(src.data[:, 2:4]) self.assertAlmostEqual(expected_val_right, res.data[1]) # Reduce to two cells along x, one three times the size # of the other. src = self.simple_cube.copy() dest = _subsampled_grid(src, 2, 3) lon = dest.coord('longitude') points = lon.points.copy() bounds = [[-1, 0], [0, 3]] lon = lon.copy(points=points, bounds=bounds) dest.replace_coord(lon) res = regrid_area_weighted(src, dest) expected_val_left = np.mean(src.data[:, 0:1]) self.assertEqual(expected_val_left, res.data[0]) expected_val_right = np.mean(src.data[:, 1:4]) self.assertAlmostEqual(expected_val_right, res.data[1])
def test_equal_area_numbers(self): # Remove coords system and units so it is no longer spherical. self.simple_cube.coord("latitude").coord_system = None self.simple_cube.coord("latitude").units = None self.simple_cube.coord("longitude").coord_system = None self.simple_cube.coord("longitude").units = None # Reduce to a single cell src = self.simple_cube.copy() dest = _subsampled_grid(src, 4, 3) res = regrid_area_weighted(src, dest) expected_val = np.mean(src.data) self.assertAlmostEqual(expected_val, res.data) # Reduce to two cells along x src = self.simple_cube.copy() dest = _subsampled_grid(src, 2, 3) res = regrid_area_weighted(src, dest) expected_val_left = np.mean(src.data[:, 0:2]) self.assertEqual(expected_val_left, res.data[0]) expected_val_right = np.mean(src.data[:, 2:4]) self.assertAlmostEqual(expected_val_right, res.data[1]) # Reduce to two cells along x, one three times the size # of the other. src = self.simple_cube.copy() dest = _subsampled_grid(src, 2, 3) lon = dest.coord("longitude") points = lon.points.copy() bounds = [[-1, 0], [0, 3]] lon = lon.copy(points=points, bounds=bounds) dest.replace_coord(lon) res = regrid_area_weighted(src, dest) expected_val_left = np.mean(src.data[:, 0:1]) self.assertEqual(expected_val_left, res.data[0]) expected_val_right = np.mean(src.data[:, 1:4]) self.assertAlmostEqual(expected_val_right, res.data[1])
def test_global_data_subset(self): src = iris.tests.stock.global_pp() src.coord("latitude").guess_bounds() src.coord("longitude").guess_bounds() dest_lat = src.coord("latitude")[0:40] dest_lon = iris.coords.DimCoord( np.linspace(-160, -70, 30), standard_name="longitude", units="degrees", coord_system=dest_lat.coord_system ) # Note target grid (in -180 to 180) src in 0 to 360 dest_lon.guess_bounds() data = np.zeros((dest_lat.shape[0], dest_lon.shape[0])) dest = iris.cube.Cube(data) dest.add_dim_coord(dest_lat, 0) dest.add_dim_coord(dest_lon, 1) res = regrid_area_weighted(src, dest) self.assertArrayShapeStats(res, (40, 30), 280.979336, 16.640421)
def test_circular_subset(self): src = iris.tests.stock.global_pp() src.coord('latitude').guess_bounds() src.coord('longitude').guess_bounds() dest_lat = src.coord('latitude')[0:40] dest_lon = iris.coords.DimCoord([-15., -10., -5., 0., 5., 10., 15.], standard_name='longitude', units='degrees', coord_system=dest_lat.coord_system) # Note target grid (in -180 to 180) src in 0 to 360 dest_lon.guess_bounds() data = np.zeros((dest_lat.shape[0], dest_lon.shape[0])) dest = iris.cube.Cube(data) dest.add_dim_coord(dest_lat, 0) dest.add_dim_coord(dest_lon, 1) res = regrid_area_weighted(src, dest) self.assertArrayShapeStats(res, (40, 7), 285.653967, 15.212710)
def test_global_data_subset(self): src = iris.tests.stock.global_pp() src.coord('latitude').guess_bounds() src.coord('longitude').guess_bounds() dest_lat = src.coord('latitude')[0:40] dest_lon = iris.coords.DimCoord(np.linspace(-160, -70, 30), standard_name='longitude', units='degrees', coord_system=dest_lat.coord_system) # Note target grid (in -180 to 180) src in 0 to 360 dest_lon.guess_bounds() data = np.zeros((dest_lat.shape[0], dest_lon.shape[0])) dest = iris.cube.Cube(data) dest.add_dim_coord(dest_lat, 0) dest.add_dim_coord(dest_lon, 1) res = regrid_area_weighted(src, dest) self.assertArrayShapeStats(res, (40, 30), 280.979336, 16.640421)
def test_hybrid_height(self): src = self.realistic_cube dest = _resampled_grid(src, 0.7, 0.8) res = regrid_area_weighted(src, dest) self.assertCMLApproxData(res, RESULT_DIR + ('hybridheight.cml',)) # Consider a single slice to allow visual tests of altitudes. src = src[1, 2] res = res[1, 2] qplt.pcolormesh(res) self.check_graphic() plt.contourf(res.coord('grid_longitude').points, res.coord('grid_latitude').points, res.coord('altitude').points) self.check_graphic() plt.contourf(res.coord('grid_longitude').points, res.coord('grid_latitude').points, res.coord('surface_altitude').points) self.check_graphic()
def test_no_bounds(self): src = self.simple_cube.copy() src.coord("latitude").bounds = None dest = self.simple_cube.copy() with self.assertRaises(ValueError): regrid_area_weighted(src, dest) src = self.simple_cube.copy() src.coord("longitude").bounds = None with self.assertRaises(ValueError): regrid_area_weighted(src, dest) src = self.simple_cube.copy() dest = self.simple_cube.copy() dest.coord("latitude").bounds = None with self.assertRaises(ValueError): regrid_area_weighted(src, dest) dest = self.simple_cube.copy() dest.coord("longitude").bounds = None with self.assertRaises(ValueError): regrid_area_weighted(src, dest)
def test_no_bounds(self): src = self.simple_cube.copy() src.coord('latitude').bounds = None dest = self.simple_cube.copy() with self.assertRaises(ValueError): regrid_area_weighted(src, dest) src = self.simple_cube.copy() src.coord('longitude').bounds = None with self.assertRaises(ValueError): regrid_area_weighted(src, dest) src = self.simple_cube.copy() dest = self.simple_cube.copy() dest.coord('latitude').bounds = None with self.assertRaises(ValueError): regrid_area_weighted(src, dest) dest = self.simple_cube.copy() dest.coord('longitude').bounds = None with self.assertRaises(ValueError): regrid_area_weighted(src, dest)
def test_circular_subset(self): src = iris.tests.stock.global_pp() src.coord('latitude').guess_bounds() src.coord('longitude').guess_bounds() dest_lat = src.coord('latitude')[0:40] dest_lon = iris.coords.DimCoord([-15., -10., -5., 0., 5., 10., 15.], standard_name='longitude', units='degrees', coord_system=dest_lat.coord_system) # Note target grid (in -180 to 180) src in 0 to 360 dest_lon.guess_bounds() data = np.zeros((dest_lat.shape[0], dest_lon.shape[0])) dest = iris.cube.Cube(data) dest.add_dim_coord(dest_lat, 0) dest.add_dim_coord(dest_lon, 1) res = regrid_area_weighted(src, dest) qplt.pcolormesh(res) plt.gca().coastlines() self.check_graphic()
def test_hybrid_height(self): src = self.realistic_cube dest = _resampled_grid(src, 0.7, 0.8) res = regrid_area_weighted(src, dest) self.assertCMLApproxData(res, RESULT_DIR + ('hybridheight.cml', )) # Consider a single slice to allow visual tests of altitudes. src = src[1, 2] res = res[1, 2] qplt.pcolormesh(res) self.check_graphic() plt.contourf( res.coord('grid_longitude').points, res.coord('grid_latitude').points, res.coord('altitude').points) self.check_graphic() plt.contourf( res.coord('grid_longitude').points, res.coord('grid_latitude').points, res.coord('surface_altitude').points) self.check_graphic()
def test_non_circular_subset(self): src = iris.tests.stock.global_pp() src.coord("latitude").guess_bounds() src.coord("longitude").guess_bounds() src.coord("longitude").circular = False dest_lat = src.coord("latitude")[0:40] dest_lon = iris.coords.DimCoord( [-15.0, -10.0, -5.0, 0.0, 5.0, 10.0, 15.0], standard_name="longitude", units="degrees", coord_system=dest_lat.coord_system, ) # Note target grid (in -180 to 180) src in 0 to 360 dest_lon.guess_bounds() data = np.zeros((dest_lat.shape[0], dest_lon.shape[0])) dest = iris.cube.Cube(data) dest.add_dim_coord(dest_lat, 0) dest.add_dim_coord(dest_lon, 1) res = regrid_area_weighted(src, dest) self.assertArrayShapeStats(res, (40, 7), 285.550800, 15.190245)
def test_non_circular_subset(self): src = iris.tests.stock.global_pp() src.coord("latitude").guess_bounds() src.coord("longitude").guess_bounds() src.coord("longitude").circular = False dest_lat = src.coord("latitude")[0:40] dest_lon = iris.coords.DimCoord( [-15.0, -10.0, -5.0, 0.0, 5.0, 10.0, 15.0], standard_name="longitude", units="degrees", coord_system=dest_lat.coord_system, ) # Note target grid (in -180 to 180) src in 0 to 360 dest_lon.guess_bounds() data = np.zeros((dest_lat.shape[0], dest_lon.shape[0])) dest = iris.cube.Cube(data) dest.add_dim_coord(dest_lat, 0) dest.add_dim_coord(dest_lon, 1) res = regrid_area_weighted(src, dest) self.assertArrayShapeStats(res, (40, 7), 285.550814, 15.190245)
def test_missing_coords(self): dest = self.simple_cube.copy() # Missing src_x. src = self.simple_cube.copy() src.remove_coord("longitude") with self.assertRaises(ValueError): regrid_area_weighted(src, dest) # Missing src_y. src = self.simple_cube.copy() src.remove_coord("latitude") with self.assertRaises(ValueError): regrid_area_weighted(src, dest) # Missing dest_x. src = self.simple_cube.copy() dest = self.simple_cube.copy() dest.remove_coord("longitude") with self.assertRaises(ValueError): regrid_area_weighted(src, dest) # Missing dest_y. src = self.simple_cube.copy() dest = self.simple_cube.copy() dest.remove_coord("latitude") with self.assertRaises(ValueError): regrid_area_weighted(src, dest)
def test_missing_coords(self): dest = self.simple_cube.copy() # Missing src_x. src = self.simple_cube.copy() src.remove_coord('longitude') with self.assertRaises(ValueError): regrid_area_weighted(src, dest) # Missing src_y. src = self.simple_cube.copy() src.remove_coord('latitude') with self.assertRaises(ValueError): regrid_area_weighted(src, dest) # Missing dest_x. src = self.simple_cube.copy() dest = self.simple_cube.copy() dest.remove_coord('longitude') with self.assertRaises(ValueError): regrid_area_weighted(src, dest) # Missing dest_y. src = self.simple_cube.copy() dest = self.simple_cube.copy() dest.remove_coord('latitude') with self.assertRaises(ValueError): regrid_area_weighted(src, dest)
def test_regrid_to_same_grid(self): src = self.simple_cube res = regrid_area_weighted(src, src) self.assertEqual(res, src) self.assertCMLApproxData(res, RESULT_DIR + ('simple.cml',))
def test_regrid_latlon_reduced_res(self): src = self.simple_cube # Reduce from (3, 4) to (2, 2). dest = _subsampled_grid(src, 2, 2) res = regrid_area_weighted(src, dest) self.assertCMLApproxData(res, RESULT_DIR + ('latlonreduced.cml',))
def test_regrid_lon_to_half_res(self): src = self.simple_cube dest = _resampled_grid(src, 0.5, 1.0) res = regrid_area_weighted(src, dest) self.assertCMLApproxData(res, RESULT_DIR + ('lonhalved.cml',))
def test_regrid_to_higher_res(self): src = self.simple_cube frac = 3.5 dest = _resampled_grid(src, frac, frac) res = regrid_area_weighted(src, dest) self.assertCMLApproxData(res, RESULT_DIR + ('higher.cml',))