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

Fixes preserving ds/da attributes in the regrid2 module #468

Merged
merged 2 commits into from
May 22, 2023
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
35 changes: 32 additions & 3 deletions tests/test_regrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@ def setup(self):
)
time = time[:-1].to_pydatetime() + datetime.timedelta(hours=12)

self.ds_attrs = {
"Conventions": "CF-1.7 CMIP-6.2",
"activity_id": "CMIP",
"experiment": "historical",
}

self.da_attrs = {
"standard_name": "surface_temperature",
"long_name": "Surface Temperature",
"units": "K",
}

self.coarse_2d_ds = xr.Dataset(
{
"ts": xr.DataArray(
Expand All @@ -67,10 +79,12 @@ def setup(self):
"lat": self.coarse_lat_bnds["lat"],
"lon": self.coarse_lon_bnds["lon"],
},
attrs=self.da_attrs,
),
"lat_bnds": self.coarse_lat_bnds,
"lon_bnds": self.coarse_lon_bnds,
}
},
attrs=self.ds_attrs,
)

self.coarse_3d_ds = xr.Dataset(
Expand All @@ -89,11 +103,13 @@ def setup(self):
"lat": self.coarse_lat_bnds["lat"],
"lon": self.coarse_lon_bnds["lon"],
},
attrs=self.da_attrs,
),
"time_bnds": (["time", "bnds"], self.time_bnds),
"lat_bnds": self.coarse_lat_bnds,
"lon_bnds": self.coarse_lon_bnds,
}
},
attrs=self.ds_attrs,
)

self.height_bnds = gen_uniform_axis(0.0, 40000.1, 20000.0, "height", "Z")
Expand All @@ -116,12 +132,14 @@ def setup(self):
"lat": self.coarse_lat_bnds["lat"],
"lon": self.coarse_lon_bnds["lon"],
},
attrs=self.da_attrs,
),
"time_bnds": (["time", "bnds"], self.time_bnds),
"height_bnds": self.height_bnds,
"lat_bnds": self.coarse_lat_bnds,
"lon_bnds": self.coarse_lon_bnds,
}
},
attrs=self.ds_attrs,
)

self.fine_2d_ds = xr.Dataset(
Expand Down Expand Up @@ -279,6 +297,17 @@ def test_regrid_output_mask(self):

assert np.all(output_data.ts.values == expected_output)

def test_preserve_attrs(self):
regridder = regrid2.Regrid2Regridder(self.coarse_2d_ds, self.fine_2d_ds)

output_data = regridder.horizontal("ts", self.coarse_2d_ds)

assert output_data.attrs == self.ds_attrs
assert output_data["ts"].attrs == self.da_attrs

for x in output_data.coords:
assert output_data[x].attrs == self.coarse_2d_ds[x].attrs

def test_regrid_2d(self):
regridder = regrid2.Regrid2Regridder(self.coarse_2d_ds, self.fine_2d_ds)

Expand Down
1 change: 1 addition & 0 deletions xcdat/regridder/regrid2.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ def _create_output_dataset(
output_data,
dims=[axis_variable_name_map[x] for x in ordered_axis_names],
coords=coords,
attrs=input_ds[data_var].attrs.copy(),
)

data_vars = {data_var: output_da}
Expand Down