Stream: python-questions

Topic: GeoCAT climatology error


view this post on Zulip Brian Medeiros (Sep 07 2023 at 16:50):

Hi all,
I'm getting an error while using the GeoCAT function climate_anomaly. The error is AttributeError: 'DataArray' object has no attribute '_data'. The data is from the CESM2 large ensemble. Here's a complete example that I was just trying to run on casper:

from pathlib import Path
import xarray as xr
import numpy as np
import geocat.comp as gc

#
# LOAD SST FOR ONE MEMBER OF CESM2 LARGE ENSEMBLE
#
varname = "SST"
loc = Path(f"/glade/campaign/cgd/cesm/CESM2-LE/timeseries/atm/proc/tseries/month_1/{varname}")
case = "b.e21.BHISTcmip6.f09_g17.LE2-1281.002"
srch = f".cam.h0.{varname}.*.nc"
fils = sorted(loc.glob(case+srch))
print(f"Found {len(fils)} files.")
#
# open dataset and correct the time coordinate to month mid-point
#
ds = xr.open_mfdataset(fils, decode_times=False)
t = ds['time_bnds'].mean(dim='nbnd')
t.attrs = ds.time.attrs
ds = ds.assign_coords({"time":t})
ds = xr.decode_cf(ds)
#
# calculate the SST anomaly using GeoCAT
#
ds_p_anom = gc.climate_anomaly(ds[varname], 'month', time_dim='time')

Anyone have any input for this?

Note that replacing the last line with these two lines seems to work (and the whole thing runs in about 13s):

climo = ds[varname].groupby('time.month').mean(dim='time').compute()
anom = (ds[varname].groupby('time.month') - climo).compute()

view this post on Zulip Cora Schneck (Sep 07 2023 at 16:53):

There is an issue with how geocat is currently working with XArray (https://github.com/NCAR/geocat-comp/issues/381#issuecomment-1627832266)

view this post on Zulip Brian Medeiros (Sep 07 2023 at 16:57):

Thanks @Cora Schneck That is definitely the same issue I'm hitting.

view this post on Zulip Cora Schneck (Sep 07 2023 at 16:59):

There is the local fix (by changing contains_cftime_datetimes), but I'm not sure about a more general fix

view this post on Zulip Cora Schneck (Sep 07 2023 at 18:00):

Here is a more general solution

So I believe it should be:
ds = xr.decode_cf(ds)
To
ds = xr.decode_cf(ds.variable)

view this post on Zulip Anissa Zacharias (Sep 07 2023 at 18:06):

@Brian Medeiros This is also an open issue on geocat-comp. Pinning xarray<=2023.02.0 should get you around the error until we address it in a release.


Last updated: May 16 2025 at 17:14 UTC