How does xcdat's missing_bounds work? #482
-
Question criteria
Describe your questionGreetings, xarray.Dataset
Dimensions:
x: 171y: 128time: 3287bnds: 2
Coordinates:
x (x) float64 -5.344e+05 -5.281e+05 ... 5.281e+05
y (y) float64 -1.959e+06 ... -1.166e+06
Data variables:
polar_stereographic () int32 ...
z (time, y, x) float32 ...
x_bnds (x, bnds) float64 -5.375e+05 -5.312e+05 ... 5.312e+05
Indexes:
x PandasIndex
y PandasIndex Added Time-Cordinates like: t1.time.attrs["units"] = "days since 2013-01-01"
t1.time.attrs["calendar"] = "proleptic_gregorian"
t1 = xr.decode_cf(t1) Now the data looks like: xarray.Dataset
Dimensions: x: 171y: 128time: 3287bnds: 2
Coordinates:
x (x) float64 -5.344e+05 -5.281e+05 ... 5.281e+05
y (y) float64 -1.959e+06 ... -1.166e+06
time (time) datetime64[ns] 2013-01-01 ... 2021-12-31
Data variables:
polar_stereographic () int32 ...
z (time, y, x) float32 ...
x_bnds (x, bnds) float64 -5.375e+05 -5.312e+05 ... 5.312e+05
xcdat_bounds : True
Indexes:
x PandasIndex
y PandasIndex
time PandasIndex
PandasIndex(DatetimeIndex(['2013-01-01', '2013-01-02', '2013-01-03', '2013-01-04',
'2013-01-05', '2013-01-06', '2013-01-07', '2013-01-08',
'2013-01-09', '2013-01-10',
...
'2021-12-22', '2021-12-23', '2021-12-24', '2021-12-25',
'2021-12-26', '2021-12-27', '2021-12-28', '2021-12-29',
'2021-12-30', '2021-12-31'],
dtype='datetime64[ns]', name='time', length=3287, freq=None)) Here, pandas index of 'time' shows 'freq' as 'None' ds_season = xc_sic.temporal.group_average(
"z",
"season",
season_config={
"dec_mode": "DJF",}
) The error abounds BOUNDS appear (below is a sample) KeyError Traceback (most recent call last)
Cell In[88], line 1
----> 1 ds_season = xc_sic.temporal.group_average(
2 "z",
3 "season",
4 season_config={
5 "dec_mode": "DJF",}
6 )
File ~\Anaconda3\envs\xcdat_env\Lib\site-packages\xcdat\temporal.py:340, in TemporalAccessor.group_average(self, data_var, freq, weighted, keep_weights, season_config)
215 """Returns a Dataset with average of a data variable by time group.
216
217 Parameters
(...)
336 }
337 """
338 self._set_data_var_attrs(data_var)
--> 340 return self._averager(
341 data_var,
342 "group_average",
343 freq,
344 weighted=weighted,
345 keep_weights=keep_weights,
346 season_config=season_config,
347 )
File ~\Anaconda3\envs\xcdat_env\Lib\site-packages\xcdat\temporal.py:772, in TemporalAccessor._averager(self, data_var, mode, freq, weighted, keep_weights, reference_period, season_config)
770 # Get the data variable and the required time axis metadata.
771 dv = _get_data_var(ds, data_var)
--> 772 time_bounds = ds.bounds.get_bounds("T", var_key=dv.name)
774 if self._mode == "average":
775 dv = self._average(dv, time_bounds)
File ~\Anaconda3\envs\xcdat_env\Lib\site-packages\xcdat\bounds.py:218, in BoundsAccessor.get_bounds(self, axis, var_key)
215 bounds_keys = []
217 if len(bounds_keys) == 0:
--> 218 raise KeyError(
219 f"No bounds data variables were found for the '{axis}' axis. Make sure "
220 "the dataset has bound data vars and their names match the 'bounds' "
221 "attributes found on their related time coordinate variables. "
222 "Alternatively, you can add bounds with `xcdat.add_missing_bounds` "
223 "or `xcdat.add_bounds`."
224 )
226 bounds: Union[xr.Dataset, xr.DataArray] = self._dataset[
227 bounds_keys if len(bounds_keys) > 1 else bounds_keys[0]
228 ].copy()
230 return bounds
KeyError: "No bounds data variables were found for the 'T' 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`." Now, I'm pretty sure the data doesn't have frequency - I read abound adding missing bounds Are there are any possible answers you came across?Relevant discussions: Here @tomvothecoder talks about adding missing bounds through But, although I don't get an error while adding, it just doesn't work. From this discussion: #411 (reply in thread) Many Thanks in advance!! Minimal Complete Verifiable Example (MVCE)No response Relevant log outputNo response EnvironmentNSTALLED VERSIONScommit: None xarray: 2023.4.2 Anything else we need to know?No response |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
It looks like you are dealing with daily dataset. Can you try below and see if that work? xc_sic = xc_sic.bounds.add_time_bounds(method="freq", freq="day") |
Beta Was this translation helpful? Give feedback.
-
@gkb999 – it is super helpful/important that you post a "complete minimal example." From what you posted above, it seems like you have modified the dataset you are working on (I see pandas indexes in the dataset, which I'm not used to seeing – I think this means you may have taken some intermediate steps). It's also unclear how you opened the dataset, which is relevant to your issue. Could you post code that shows the following:
Also – it seems like you open the dataset and then manually decode the time axis. I suspect you may not have opened the dataset with
|
Beta Was this translation helpful? Give feedback.
@gkb999 – it is super helpful/important that you post a "complete minimal example." From what you posted above, it seems like you have modified the dataset you are working on (I see pandas indexes in the dataset, which I'm not used to seeing – I think this means you may have taken some intermediate steps). It's also unclear how you opened the dataset, which is relevant to your issue. Could you post code that shows the following:
import xcdat
)ds = xc.open_dataset("...")
ds_season = ds.temp…