Skip to content
This repository has been archived by the owner on Aug 29, 2023. It is now read-only.

Commit

Permalink
Merge pull request #809 from CCI-Tools/jg-805-coreg-id-grids
Browse files Browse the repository at this point in the history
Jg 805 coreg id grids
  • Loading branch information
JanisGailis authored Dec 19, 2018
2 parents 948a395 + 1ed9e96 commit 40f7b43
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
* Cate now distinguishes network connection problems from errors of other origin. Introduced new
error type `cate.core.ds.NetworkError` that is raised if a connection to e.g.
CCI ODP services can not be established. [#789](https://github.com/CCI-Tools/cate/issues/789)
* In coregistration make sure the replica dataset is simply returned if it is
already on master grid. [#805](https://github.com/CCI-Tools/cate/issues/805)

## Version 2.0.0.dev23

Expand Down
27 changes: 27 additions & 0 deletions cate/ops/coregistration.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ def coregister(ds_master: xr.Dataset,
' case spatial dimensions have different'
' names'.format(ds_master.dims, ds_replica.dims))

# Don't do anything if datasets already have the same spatial definition
if _grids_equal(ds_master, ds_replica):
return ds_replica

# Check if all arrays of the replica dataset have the required dimensionality
for key in ds_replica.data_vars:
if not _is_valid_array(ds_replica[key]):
Expand Down Expand Up @@ -310,13 +314,36 @@ def _resample_dataset(ds_master: xr.Dataset, ds_replica: xr.Dataset, method_us:
lat = ds_master['lat'].sel(lat=lat_slice)
ds_replica = ds_replica.sel(lon=lon_slice, lat=lat_slice)

# Don't do anything if datasets already have the same spatial definition
if _grids_equal(ds_master, ds_replica):
return ds_replica

with monitor.starting("coregister dataset", len(ds_replica.data_vars)):
kwargs = {'lon': lon, 'lat': lat, 'method_us': method_us, 'method_ds': method_ds, 'parent_monitor': monitor}
retset = ds_replica.apply(_resample_array, keep_attrs=True, **kwargs)

return adjust_spatial_attrs(retset)


def _grids_equal(master: xr.Dataset,
replica: xr.Dataset) -> bool:
"""
Check if the spatial grid of the given datasets is (almost) equal
:param master: Master dataset
:param replica: Replica dataset
"""
if len(master.lon) != len(replica.lon) or \
len(master.lat) != len(replica.lat):
return False

if not np.allclose(master.lon.values, replica.lon.values) or \
not np.allclose(master.lat.values, replica.lat.values):
return False

return True


def _find_intersection(first: np.ndarray,
second: np.ndarray,
global_bounds: Tuple[float, float]) -> Tuple[float, float]:
Expand Down
28 changes: 28 additions & 0 deletions test/ops/test_coregistration.py
Original file line number Diff line number Diff line change
Expand Up @@ -742,3 +742,31 @@ def test_int_array(self):
'time': np.array([1, 2])})

assert_almost_equal(ds_fine_resampled['first'].values, expected['first'].values)

def test_same_grid(self):
"""
Test the case when both datasets already have the same geospatial definition
"""
ds_fine = xr.Dataset({
'first': (['time', 'lat', 'lon'], np.array([np.eye(4, 8), np.eye(4, 8)])),
'second': (['time', 'lat', 'lon'], np.array([np.eye(4, 8), np.eye(4, 8)])),
'lat': np.linspace(-67.5, 67.5, 4),
'lon': np.linspace(-157.5, 157.5, 8),
'time': np.array([1, 2])}).chunk(chunks={'lat': 2, 'lon': 4})

rm = RecordingMonitor()
ds_same = coregister(ds_fine, ds_fine, monitor=rm)
# Make sure it returned the input as opposed to going through with
# coregistration
self.assertEqual([], rm.records)

assert_almost_equal(ds_same['first'].values, ds_fine['first'].values)

# Test that a subset is performed, but no coregistration done
lat_slice = slice(-70, 70)
lon_slice = slice(-40, 40)
ds_subset = ds_fine.sel(lat=lat_slice, lon=lon_slice)

ds_coreg = coregister(ds_subset, ds_fine, monitor=rm)
self.assertEqual([], rm.records)
assert_almost_equal(ds_coreg['first'].values, ds_subset['first'].values)

0 comments on commit 40f7b43

Please sign in to comment.