-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
added some logic to deal with rasterio objects in addition to filepaths #2589
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
35c5afb
35f9947
ce56d35
cd6fcb9
a113b50
de21485
dde9428
ea068fe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -199,9 +199,9 @@ def check_dtypes_roundtripped(self, expected, actual): | |
| actual_dtype = actual.variables[k].dtype | ||
| # TODO: check expected behavior for string dtypes more carefully | ||
| string_kinds = {'O', 'S', 'U'} | ||
| assert (expected_dtype == actual_dtype or | ||
| (expected_dtype.kind in string_kinds and | ||
| actual_dtype.kind in string_kinds)) | ||
| assert (expected_dtype == actual_dtype | ||
| or (expected_dtype.kind in string_kinds and | ||
| actual_dtype.kind in string_kinds)) | ||
|
|
||
| def test_roundtrip_test_data(self): | ||
| expected = create_test_data() | ||
|
|
@@ -376,17 +376,17 @@ def test_roundtrip_cftime_datetime_data(self): | |
| with self.roundtrip(expected, save_kwargs=kwds) as actual: | ||
| abs_diff = abs(actual.t.values - expected_decoded_t) | ||
| assert (abs_diff <= np.timedelta64(1, 's')).all() | ||
| assert (actual.t.encoding['units'] == | ||
| 'days since 0001-01-01 00:00:00.000000') | ||
| assert (actual.t.encoding['calendar'] == | ||
| expected_calendar) | ||
| assert (actual.t.encoding['units'] | ||
| == 'days since 0001-01-01 00:00:00.000000') | ||
| assert (actual.t.encoding['calendar'] | ||
| == expected_calendar) | ||
|
|
||
| abs_diff = abs(actual.t0.values - expected_decoded_t0) | ||
| assert (abs_diff <= np.timedelta64(1, 's')).all() | ||
| assert (actual.t0.encoding['units'] == | ||
| 'days since 0001-01-01') | ||
| assert (actual.t.encoding['calendar'] == | ||
| expected_calendar) | ||
| assert (actual.t0.encoding['units'] | ||
| == 'days since 0001-01-01') | ||
| assert (actual.t.encoding['calendar'] | ||
| == expected_calendar) | ||
|
|
||
| def test_roundtrip_timedelta_data(self): | ||
| time_deltas = pd.to_timedelta(['1h', '2h', 'NaT']) | ||
|
|
@@ -622,20 +622,20 @@ def test_unsigned_roundtrip_mask_and_scale(self): | |
| encoded = create_encoded_unsigned_masked_scaled_data() | ||
| with self.roundtrip(decoded) as actual: | ||
| for k in decoded.variables: | ||
| assert (decoded.variables[k].dtype == | ||
| actual.variables[k].dtype) | ||
| assert (decoded.variables[k].dtype | ||
| == actual.variables[k].dtype) | ||
| assert_allclose(decoded, actual, decode_bytes=False) | ||
| with self.roundtrip(decoded, | ||
| open_kwargs=dict(decode_cf=False)) as actual: | ||
| for k in encoded.variables: | ||
| assert (encoded.variables[k].dtype == | ||
| actual.variables[k].dtype) | ||
| assert (encoded.variables[k].dtype | ||
| == actual.variables[k].dtype) | ||
| assert_allclose(encoded, actual, decode_bytes=False) | ||
| with self.roundtrip(encoded, | ||
| open_kwargs=dict(decode_cf=False)) as actual: | ||
| for k in encoded.variables: | ||
| assert (encoded.variables[k].dtype == | ||
| actual.variables[k].dtype) | ||
| assert (encoded.variables[k].dtype | ||
| == actual.variables[k].dtype) | ||
| assert_allclose(encoded, actual, decode_bytes=False) | ||
| # make sure roundtrip encoding didn't change the | ||
| # original dataset. | ||
|
|
@@ -2520,8 +2520,8 @@ def myatts(**attrs): | |
| 'ULOD_FLAG': '-7777', 'ULOD_VALUE': 'N/A', | ||
| 'LLOD_FLAG': '-8888', | ||
| 'LLOD_VALUE': ('N/A, N/A, N/A, N/A, 0.025'), | ||
| 'OTHER_COMMENTS': ('www-air.larc.nasa.gov/missions/etc/' + | ||
| 'IcarttDataFormat.htm'), | ||
| 'OTHER_COMMENTS': ('www-air.larc.nasa.gov/missions/etc/' | ||
| + 'IcarttDataFormat.htm'), | ||
| 'REVISION': 'R0', | ||
| 'R0': 'No comments for this revision.', | ||
| 'TFLAG': 'Start_UTC' | ||
|
|
@@ -2610,8 +2610,8 @@ def test_uamiv_format_read(self): | |
| expected = xr.Variable(('TSTEP',), data, | ||
| dict(bounds='time_bounds', | ||
| long_name=('synthesized time coordinate ' + | ||
| 'from SDATE, STIME, STEP ' + | ||
| 'global attributes'))) | ||
| 'from SDATE, STIME, STEP ' | ||
| + 'global attributes'))) | ||
| actual = camxfile.variables['time'] | ||
| assert_allclose(expected, actual) | ||
| camxfile.close() | ||
|
|
@@ -2640,8 +2640,8 @@ def test_uamiv_format_mfread(self): | |
| data = np.concatenate([data1] * 2, axis=0) | ||
| attrs = dict(bounds='time_bounds', | ||
| long_name=('synthesized time coordinate ' + | ||
| 'from SDATE, STIME, STEP ' + | ||
| 'global attributes')) | ||
| 'from SDATE, STIME, STEP ' | ||
| + 'global attributes')) | ||
| expected = xr.Variable(('TSTEP',), data, attrs) | ||
| actual = camxfile.variables['time'] | ||
| assert_allclose(expected, actual) | ||
|
|
@@ -3057,6 +3057,73 @@ def test_http_url(self): | |
| import dask.array as da | ||
| assert isinstance(actual.data, da.Array) | ||
|
|
||
| def test_rasterio_environment(self): | ||
| import rasterio | ||
| with create_tmp_geotiff() as (tmp_file, expected): | ||
| # Should fail with error since suffix not allowed | ||
| with pytest.raises(Exception): | ||
| with rasterio.Env(GDAL_SKIP='GTiff'): | ||
| with xr.open_rasterio(tmp_file) as actual: | ||
| assert_allclose(actual, expected) | ||
|
|
||
| def test_rasterio_vrt(self): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For network tests we have a special decorator ( |
||
| import rasterio | ||
| # tmp_file default crs is UTM: CRS({'init': 'epsg:32618'} | ||
| with create_tmp_geotiff() as (tmp_file, expected): | ||
| with rasterio.open(tmp_file) as src: | ||
| with rasterio.vrt.WarpedVRT(src, crs='epsg:4326') as vrt: | ||
| expected_shape = (vrt.width, vrt.height) | ||
| expected_crs = vrt.crs | ||
| print(expected_crs) | ||
| expected_res = vrt.res | ||
| # Value of single pixel in center of image | ||
| lon, lat = vrt.xy(vrt.width // 2, vrt.height // 2) | ||
| expected_val = next(vrt.sample([(lon, lat)])) | ||
| with xr.open_rasterio(vrt) as da: | ||
| actual_shape = (da.sizes['x'], da.sizes['y']) | ||
| actual_crs = da.crs | ||
| print(actual_crs) | ||
| actual_res = da.res | ||
| actual_val = da.sel(dict(x=lon, y=lat), | ||
| method='nearest').data | ||
|
|
||
| assert actual_crs == expected_crs | ||
| assert actual_res == expected_res | ||
| assert actual_shape == expected_shape | ||
| assert expected_val.all() == actual_val.all() | ||
|
|
||
| @network | ||
| def test_rasterio_vrt_network(self): | ||
| import rasterio | ||
|
|
||
| url = 'https://storage.googleapis.com/\ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't even know this form of line-wrapping for strings was possible in Python :) |
||
| gcp-public-data-landsat/LC08/01/047/027/\ | ||
| LC08_L1TP_047027_20130421_20170310_01_T1/\ | ||
| LC08_L1TP_047027_20130421_20170310_01_T1_B4.TIF' | ||
| env = rasterio.Env(GDAL_DISABLE_READDIR_ON_OPEN='EMPTY_DIR', | ||
| CPL_VSIL_CURL_USE_HEAD=False, | ||
| CPL_VSIL_CURL_ALLOWED_EXTENSIONS='TIF') | ||
| with env: | ||
| with rasterio.open(url) as src: | ||
| with rasterio.vrt.WarpedVRT(src, crs='epsg:4326') as vrt: | ||
| expected_shape = (vrt.width, vrt.height) | ||
| expected_crs = vrt.crs | ||
| expected_res = vrt.res | ||
| # Value of single pixel in center of image | ||
| lon, lat = vrt.xy(vrt.width // 2, vrt.height // 2) | ||
| expected_val = next(vrt.sample([(lon, lat)])) | ||
| with xr.open_rasterio(vrt) as da: | ||
| actual_shape = (da.sizes['x'], da.sizes['y']) | ||
| actual_crs = da.crs | ||
| actual_res = da.res | ||
| actual_val = da.sel(dict(x=lon, y=lat), | ||
| method='nearest').data | ||
|
|
||
| assert_equal(actual_shape, expected_shape) | ||
| assert_equal(actual_crs, expected_crs) | ||
| assert_equal(actual_res, expected_res) | ||
| assert_equal(expected_val, actual_val) | ||
|
|
||
|
|
||
| class TestEncodingInvalid(object): | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than adding a subclass with a lot of duplicated logic, could you add this into the base
RasterioArrayWrapperclass?Something like:
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, just moved the changes to RasterioArrayWrapper