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

[Bug]: Attributes are not being preserved with temporal averaging APIs using xarray > 2023.3.0 #458

Closed
bonfils2 opened this issue Apr 25, 2023 · 3 comments · Fixed by #465
Assignees
Labels
type: bug Inconsistencies or issues which will cause an issue or problem for users or implementors.

Comments

@bonfils2
Copy link

What happened?

In the xcdat dependencies, xarray version is set to 2023.3.0, but it has been updated to newer version when you were installing new jupyter notebook in your env.

What did you expect to happen?

I expected the code to run, as usual.

Minimal Complete Verifiable Example

import xcdat as xc
ds = xc.open_dataset('/p/css03/esgf_publish/CMIP6/ScenarioMIP/IPSL/IPSL-CM6A-LR/ssp585/r2i1p1f1/Amon/tas/gr/v20191121/tas_Amon_IPSL-CM6A-LR_ssp585_r2i1p1f1_gr_201501-210012.nc', center_times=True)
ds = ds.temporal.group_average("tas", freq="year")
weights = ds.spatial.get_weights(axis=['Y', 'X'], lat_bounds=[-90,90],lon_bounds=[0,360])

Relevant log output

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/lee1043/git/xcdat/xcdat/spatial.py", line 274, in get_weights
    d_bounds = self._dataset.bounds.get_bounds(axis=key, var_key=data_var)
  File "/home/lee1043/git/xcdat/xcdat/bounds.py", line 243, in get_bounds
    raise KeyError(
KeyError: "No bounds data variables were found for the 'Y' axis. Make sure the dataset has bound data vars and their names match the 'bounds' attributes found on their related time coordinate variables. Alternatively, you can add bounds with `xcdat.add_missing_bounds` or `xcdat.add_bounds`."

Anything else we need to know?

The problem was fixed with : conda install xarray=2023.3.0

Environment

0.5.0
2023.4.2

@bonfils2 bonfils2 added the type: bug Inconsistencies or issues which will cause an issue or problem for users or implementors. label Apr 25, 2023
@tomvothecoder tomvothecoder added this to To do in Next Release (v0.6.0) via automation May 10, 2023
@tomvothecoder tomvothecoder moved this from To do to In progress in Next Release (v0.6.0) May 10, 2023
@tomvothecoder tomvothecoder changed the title [Bug]: ds.spatial.get_weights breaks with xarray=2023.4.2 [Bug]: Attributes are not being preserved with temporal averaging APIs May 10, 2023
@tomvothecoder tomvothecoder changed the title [Bug]: Attributes are not being preserved with temporal averaging APIs [Bug]: Attributes are not being preserved with temporal averaging APIs using xarray > 2023.3.0 May 10, 2023
@tomvothecoder
Copy link
Collaborator

This issue is related to a line in the temporal averaging API for generating weights, not spatial averaging.

weights = xr.where(np.isnan(dv), 0.0, weights)

In the latest version of xarray, xr.where() with np.isnan(dv) deletes the attributes on dv. The fix is to replace np.isnan(dv) with dv.copy().isnull(). Not exactly sure what changed in xarray.

@lee1043
Copy link
Collaborator

lee1043 commented May 11, 2023

Related discussion: #455 (leaving this for record)

Next Release (v0.6.0) automation moved this from In progress to Done May 11, 2023
@tomvothecoder tomvothecoder changed the title [Bug]: Attributes are not being preserved with temporal averaging APIs using xarray > 2023.3.0 [Bug]: Attributes are not being preserved with temporal averaging APIs using xarray >= 2023.4.2 Aug 17, 2023
@tomvothecoder tomvothecoder changed the title [Bug]: Attributes are not being preserved with temporal averaging APIs using xarray >= 2023.4.2 [Bug]: Attributes are not being preserved with temporal averaging APIs using xarray > 2023.3.0 Aug 17, 2023
@tomvothecoder
Copy link
Collaborator

Hey @bonfils2, just a heads up that I released a patch for v0.5.0 on conda-forge to fix the issue of attributes being dropped when calculating weighted temporal averages.

conda/mamba should automatically pull the latest version. If you want to update xcdat in an existing environment, you can run mamba update -c conda-forge xcdat or mamba install -c conda-forge xcdat=0.5.0=pyhd8ed1ab_1.

@tomvothecoder tomvothecoder added this to the v0.6.0 milestone Sep 27, 2023
@tomvothecoder tomvothecoder self-assigned this Sep 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug Inconsistencies or issues which will cause an issue or problem for users or implementors.
Projects
Status: Done
3 participants