def generate_test_dataarray( dims=OrderedDict([('y', 20), ('x', 20), ('time', 10)]), name='variable', mean=0, sigma=1, extent=(-10.0, 50.0, 0.0, 60.0), random_seed=42, crs='epsg:4326'): np.random.seed(random_seed) coords = OrderedDict() if 'y' in dims: coords['y'] = np.linspace(extent[3], extent[1], dims['y']) if 'x' in dims: coords['x'] = np.linspace(extent[0], extent[2], dims['x']) if 'time' in dims: coords['time'] = pd.date_range('2017-01-01', '2018-01-01', periods=dims['time']) meta = {'attr1': 1, 'attr2': 2, 'attr3': 3} if 'x' in dims and 'y' in dims: transform = rasterio.transform.from_bounds( *extent, width=dims['x']-1, height=dims['y']-1) meta['crs'] = _parse_crs(crs).to_string() meta['transform'] = transform[:6] data = np.random.normal(mean, sigma, tuple(dims.values())) da = xr.DataArray(data, coords=coords, dims=list(dims.keys()), name=name, attrs=meta) return da
def test_reproject_with_extra_dims(dims): crs1 = warp._parse_crs('epsg:4326') crs2 = warp._parse_crs('epsg:3395') ds = generate_test_dataset( dims=dims, crs=crs1 ) proj = warp.Reprojection(crs=crs2) reprojected = proj.apply(ds) # Check that a reprojected slice of the dataset is the same as # the slice of the reprojection of the entire dataset. slices = [ {'band': 3}, {'time': slice(1, 3)} ] for s in slices: xr_assert_equal( proj.apply(ds.isel(**s)), reprojected.isel(**s) )
def generate_test_dataset( dims=OrderedDict([('y', 20), ('x', 20), ('time', 10)]), var=['C11', 'C12__im', 'C12__re', 'C22'], mean=0, sigma=1, extent=(-10.0, 50.0, 0.0, 60.0), random_seed=42, crs='epsg:4326'): np.random.seed(random_seed) coords = OrderedDict() for name, size in dims.items(): if name == 'y': coords[name] = np.linspace(extent[3], extent[1], size) elif name == 'x': coords[name] = np.linspace(extent[0], extent[2], size) elif name == 'time': coords[name] = pd.date_range('2017-01-01', '2018-01-01', periods=size) else: coords[name] = np.arange(size) meta = {'attr1': 1, 'attr2': 2, 'attr3': 3} ds = xr.Dataset(coords=coords, attrs=meta) if 'x' in dims and 'y' in dims: transform = rasterio.transform.from_bounds( *extent, width=dims['x']-1, height=dims['y']-1) ds.attrs['crs'] = _parse_crs(crs).to_string() ds.attrs['transform'] = transform[:6] ds.attrs['res'] = (abs(transform.a), abs(transform.e)) ds.attrs['bounds'] = extent if isinstance(mean, (int, float)): mean = [mean] * len(var) for v, m in zip(var, mean): ds[v] = (tuple(dims.keys()), np.random.normal(m, sigma, tuple(dims.values()))) return ds
def test_reprojection(name, kwargs): ds = generate_test_dataset(**kwargs) crs = warp._parse_crs('epsg:4326') proj = warp.Reprojection(crs=crs) reprojected = proj.apply(ds) assert_equal_crs(crs, warp.get_crs(reprojected))
def test_parse_crs_fails(invalidcrs): with assert_raises(CRSError): warp._parse_crs(invalidcrs)
def test_parse_crs(crs): assert_equal_crs(crs, warp._parse_crs(crs)) assert_equal_crs(crs, warp._parse_crs(crs.to_string())) assert_equal_crs(crs, warp._parse_crs(crs.to_dict())) assert_equal_crs(crs, warp._parse_crs(crs.wkt)) assert_equal_crs(crs, warp._parse_crs(crs.to_epsg()))