Skip to content

Commit 1425e13

Browse files
authored
Fix attributes being dropped in certain circumstances (#54)
* Add failing test for format_lat dropping attrs * Fix dropping attrs in format_lat * Add changelog for #54: fix dropped attrs * Add Ben Mares to CITATION.cff
1 parent f2120b8 commit 1425e13

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/).
66

77
## Unreleased
88

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

1013
## 0.4.0 (2024-09-26)
1114

CITATION.cff

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ authors:
2121
- given-names: Sam
2222
family-names: Levang
2323
affiliation: Salient Predictions
24+
- given-names: Ben
25+
family-names: Mares
26+
orcid: 'https://orcid.org/0000-0002-1036-0793'
27+
affiliation: Tensorial (EI Bernard Mares)
28+
2429

2530
repository-code: 'https://github.com/EXCITED-CO2/xarray-regrid'
2631
keywords:

src/xarray_regrid/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,15 +316,15 @@ def format_lat(
316316
if dy - polar_lat >= obj.coords[lat_coord].values[0] > -polar_lat:
317317
south_pole = obj.isel({lat_coord: 0})
318318
if lon_coord is not None:
319-
south_pole = south_pole.mean(lon_coord)
319+
south_pole = south_pole.mean(lon_coord, keep_attrs=True)
320320
obj = xr.concat([south_pole, obj], dim=lat_coord) # type: ignore
321321
lat_vals = np.concatenate([[-polar_lat], lat_vals])
322322

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

tests/test_utils.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import numpy as np
2+
import xarray as xr
3+
4+
from xarray_regrid.utils import format_lat
5+
6+
7+
def test_format_lat():
8+
lat_vals = np.arange(-89.5, 89.5 + 1, 1)
9+
lon_vals = np.arange(-179.5, 179.5 + 1, 1)
10+
x_vals = np.broadcast_to(lat_vals, (len(lon_vals), len(lat_vals)))
11+
ds = xr.Dataset(
12+
data_vars={"x": (("lon", "lat"), x_vals)},
13+
coords={"lat": lat_vals, "lon": lon_vals},
14+
attrs={"foo": "bar"},
15+
)
16+
ds.lat.attrs["is"] = "coord"
17+
ds.x.attrs["is"] = "data"
18+
19+
formatted = format_lat(ds, ds, {"lat": "lat", "lon": "lon"})
20+
# Check that lat has been extended to include poles
21+
assert formatted.lat.values[0] == -90
22+
assert formatted.lat.values[-1] == 90
23+
# Check that data has been extrapolated to include poles
24+
assert (formatted.x.isel(lat=0) == -89.5).all()
25+
assert (formatted.x.isel(lat=-1) == 89.5).all()
26+
# Check that attrs have been preserved
27+
assert formatted.attrs["foo"] == "bar"
28+
assert formatted.lat.attrs["is"] == "coord"
29+
assert formatted.x.attrs["is"] == "data"

0 commit comments

Comments
 (0)