Skip to content
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

Fix attributes being dropped in certain circumstances #54

Merged
merged 4 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/).

## Unreleased

Fixed:
- Attributes are now properly preserved when updating coordinates during pre-formatting for regridding ([#54](https://github.com/xarray-contrib/xarray-regrid/pull/54)).


## 0.4.0 (2024-09-26)

Expand Down
5 changes: 5 additions & 0 deletions CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ authors:
- given-names: Sam
family-names: Levang
affiliation: Salient Predictions
- given-names: Ben
family-names: Mares
orcid: 'https://orcid.org/0000-0002-1036-0793'
affiliation: Tensorial (EI Bernard Mares)
email: [email protected]

repository-code: 'https://github.com/EXCITED-CO2/xarray-regrid'
keywords:
Expand Down
4 changes: 2 additions & 2 deletions src/xarray_regrid/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,15 +316,15 @@ def format_lat(
if dy - polar_lat >= obj.coords[lat_coord].values[0] > -polar_lat:
south_pole = obj.isel({lat_coord: 0})
if lon_coord is not None:
south_pole = south_pole.mean(lon_coord)
south_pole = south_pole.mean(lon_coord, keep_attrs=True)
obj = xr.concat([south_pole, obj], dim=lat_coord) # type: ignore
lat_vals = np.concatenate([[-polar_lat], lat_vals])

# North pole
if polar_lat - dy <= obj.coords[lat_coord].values[-1] < polar_lat:
north_pole = obj.isel({lat_coord: -1})
if lon_coord is not None:
north_pole = north_pole.mean(lon_coord)
north_pole = north_pole.mean(lon_coord, keep_attrs=True)
obj = xr.concat([obj, north_pole], dim=lat_coord) # type: ignore
lat_vals = np.concatenate([lat_vals, [polar_lat]])

Expand Down
29 changes: 29 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import numpy as np
import xarray as xr

from xarray_regrid.utils import format_lat


def test_format_lat():
lat_vals = np.arange(-89.5, 89.5 + 1, 1)
lon_vals = np.arange(-179.5, 179.5 + 1, 1)
x_vals = np.broadcast_to(lat_vals, (len(lon_vals), len(lat_vals)))
ds = xr.Dataset(
data_vars={"x": (("lon", "lat"), x_vals)},
coords={"lat": lat_vals, "lon": lon_vals},
attrs={"foo": "bar"},
)
ds.lat.attrs["is"] = "coord"
ds.x.attrs["is"] = "data"

formatted = format_lat(ds, ds, {"lat": "lat", "lon": "lon"})
# Check that lat has been extended to include poles
assert formatted.lat.values[0] == -90
assert formatted.lat.values[-1] == 90
# Check that data has been extrapolated to include poles
assert (formatted.x.isel(lat=0) == -89.5).all()
assert (formatted.x.isel(lat=-1) == 89.5).all()
# Check that attrs have been preserved
assert formatted.attrs["foo"] == "bar"
assert formatted.lat.attrs["is"] == "coord"
assert formatted.x.attrs["is"] == "data"
Loading