Skip to content
This repository has been archived by the owner on Apr 30, 2021. It is now read-only.

esmlab.resample reverses order of dimensions in time bounds variable #163

Open
klindsay28 opened this issue Dec 17, 2019 · 0 comments
Open
Labels
bug Something isn't working

Comments

@klindsay28
Copy link

When I run the commands in the following code snippet, I see from the first two lines of output that esmlab.resample has reversed the order of dimensions in time:bounds, compared to the input xr.Dataset. This causes problems for code assuming that time is the first dimension and the length-2 dimension is second.

esmlab.resample appears to makes this assumption itself. If you attempt to call esmlab.resample on a xr.Dataset returned by esmlab.resample, you get a ValueError exception, with the Traceback below. If I transpose time:bounds before calling esmlab.resample a 2nd time, there is no fatal error.

Code Snippet

import xarray as xr
import esmlab
dir = '/glade/work/klindsay/analysis/CESM2_coup_carb_cycle_JAMES/tseries'
fname = 'SFCO2_atm_esm-piControl-pre_00_mon.nc'
path = '/'.join([dir, fname])
ds = xr.open_dataset(path)
ds_ann = esmlab.resample(ds, freq='ann')
tb_name = ds['time'].attrs['bounds']
print(f'{tb_name} dims in ds')
print(ds[tb_name].dims)
print(f'{tb_name} dims in ds_ann')
print(ds_ann[tb_name].dims)

ds_ann[tb_name] = ds_ann[tb_name].transpose()

ds_ann_ann = esmlab.resample(ds_ann, freq='ann')

Results from Code Snippet

time_bnds dims in ds
('time', 'nbnd')
time_bnds dims in ds_ann
('nbnd', 'time')
Traceback (most recent call last):
File "foo6.py", line 14, in
ds_ann_ann = esmlab.resample(ds_ann, freq='ann')
File "/glade/work/klindsay/analysis/CESM2_coup_carb_cycle_JAMES/esmlab_dev/esmlab/core.py", line 769, in resample
ds = dset.esmlab.set_time(time_coord_name=time_coord_name).compute_ann_mean(
File "/glade/work/klindsay/analysis/CESM2_coup_carb_cycle_JAMES/esmlab_dev/esmlab/core.py", line 349, in set_time
self.setup()
File "/glade/work/klindsay/analysis/CESM2_coup_carb_cycle_JAMES/esmlab_dev/esmlab/core.py", line 354, in setup
self.compute_time()
File "/glade/work/klindsay/analysis/CESM2_coup_carb_cycle_JAMES/esmlab_dev/esmlab/core.py", line 82, in compute_time
groupby_coord = self.get_time_decoded(midpoint=True)
File "/glade/work/klindsay/analysis/CESM2_coup_carb_cycle_JAMES/esmlab_dev/esmlab/core.py", line 187, in get_time_decoded
only_use_cftime_datetimes=True,
File "/glade/work/klindsay/miniconda3/envs/CESM2_coup_carb_cycle_JAMES_tst/lib/python3.7/site-packages/xarray/core/common.py", line 257, in setattr
object.setattr(self, name, value)
File "/glade/work/klindsay/miniconda3/envs/CESM2_coup_carb_cycle_JAMES_tst/lib/python3.7/site-packages/xarray/core/dataarray.py", line 563, in data
self.variable.data = value
File "/glade/work/klindsay/miniconda3/envs/CESM2_coup_carb_cycle_JAMES_tst/lib/python3.7/site-packages/xarray/core/variable.py", line 1971, in data
Variable.data.fset(self, data)
File "/glade/work/klindsay/miniconda3/envs/CESM2_coup_carb_cycle_JAMES_tst/lib/python3.7/site-packages/xarray/core/variable.py", line 345, in data
raise ValueError("replacement data must match the Variable's shape")
ValueError: replacement data must match the Variable's shape

Output of esmlab.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.7.3 | packaged by conda-forge | (default, Jul 1 2019, 21:52:21)
[GCC 7.3.0]
python-bits: 64
OS: Linux
OS-release: 3.10.0-693.21.1.el7.x86_64
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: en_US.UTF-8
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8

esmlab: 2019.4.27.post43
xarray: 0.14.0
pandas: 0.25.2
numpy: 1.17.3
scipy: 1.3.1
xesmf: 0.2.1
cftime: 1.0.3.4
dask: 2.6.0
distributed: 2.6.0
setuptools: 41.4.0
pip: 19.3.1
conda: None
pytest: None
IPython: 7.9.0
sphinx: None

@klindsay28 klindsay28 added the bug Something isn't working label Dec 17, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant