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

Commit

Permalink
Normalize inverted latitude
Browse files Browse the repository at this point in the history
Handling inverted latitude in coregistration became troublesome. As it
would then also be done in multiple places, it makes sense to include
inverted latitude in normalization.

Such datasets are of course very normal, but it still makes sense to
eliminate such inconsistencies between datasets ingested by cate.

Closes #733
  • Loading branch information
Jānis Gailis committed Sep 28, 2018
1 parent 1e9075c commit 25ef90e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
16 changes: 16 additions & 0 deletions cate/core/opimpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,27 @@ def normalize_impl(ds: xr.Dataset) -> xr.Dataset:
ds = _normalize_lat_lon_2d(ds)
ds = _normalize_dim_order(ds)
ds = _normalize_lon_360(ds)
ds = _normalize_inverted_lat(ds)
ds = normalize_missing_time(ds)
ds = _normalize_jd2datetime(ds)
return ds


def _normalize_inverted_lat(ds: xr.Dataset) -> xr.Dataset:
"""
In case the latitude decreases, invert it
:param ds: some xarray dataset
:return: a normalized xarray dataset
"""
try:
if _lat_inverted(ds.lat):
ds = ds.sel(lat=slice(None, None, -1))
except AttributeError:
# The dataset doesn't have 'lat', probably not geospatial
pass
return ds


def _normalize_lat_lon(ds: xr.Dataset) -> xr.Dataset:
"""
Rename variables named 'longitude' or 'long' to 'lon', and 'latitude' to 'lon'.
Expand Down
29 changes: 26 additions & 3 deletions test/ops/test_normalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ def test_normalize_lon_lat_2d(self):
a_data = np.random.random_sample((t_size, y_size, x_size))
b_data = np.random.random_sample((t_size, y_size, x_size))
time_data = [1, 2]
lat_data = [[30., 30., 30., 30.],
lat_data = [[10., 10., 10., 10.],
[20., 20., 20., 20.],
[10., 10., 10., 10.]]
[30., 30., 30., 30.]]
lon_data = [[-10., 0., 10., 20.],
[-10., 0., 10., 20.],
[-10., 0., 10., 20.]]
Expand All @@ -65,7 +65,7 @@ def test_normalize_lon_lat_2d(self):
expected = xr.Dataset({'a': (new_dims, a_data, attribs),
'b': (new_dims, b_data, attribs)},
{'time': (('time',), time_data),
'lat': (('lat',), [30., 20., 10.]),
'lat': (('lat',), [10., 20., 30.]),
'lon': (('lon',), [-10., 0., 10., 20.]),
},
{'geospatial_lon_min': -15.,
Expand Down Expand Up @@ -110,6 +110,29 @@ def test_normalize_lon_lat(self):
actual = normalize(dataset)
assertDatasetEqual(actual, expected)

def test_normalize_inverted_lat(self):
first = np.zeros([3, 45, 90])
first[0, :, :] = np.eye(45, 90)
ds = xr.Dataset({
'first': (['time', 'lat', 'lon'], first),
'second': (['time', 'lat', 'lon'], np.zeros([3, 45, 90])),
'lat': np.linspace(88, -88, 45),
'lon': np.linspace(-178, 178, 90),
'time': [datetime(2000, x, 1) for x in range(1, 4)]}).chunk(chunks={'time':1})

first = np.zeros([3, 45, 90])
first[0, :, :] = np.flip(np.eye(45, 90), axis=0)
expected = xr.Dataset({
'first': (['time', 'lat', 'lon'], first),
'second': (['time', 'lat', 'lon'], np.zeros([3, 45, 90])),
'lat': np.linspace(-88, 88, 45),
'lon': np.linspace(-178, 178, 90),
'time': [datetime(2000, x, 1) for x in range(1, 4)]}).chunk(chunks={'time':1})

actual = normalize(ds)
xr.testing.assert_equal(actual, expected)


def test_normalize_with_missing_time_dim(self):
ds = xr.Dataset({'first': (['lat', 'lon'], np.zeros([90, 180])),
'second': (['lat', 'lon'], np.zeros([90, 180]))},
Expand Down

0 comments on commit 25ef90e

Please sign in to comment.