esmlab icon indicating copy to clipboard operation
esmlab copied to clipboard

esmlab.climatology does not propagate all coordinate variables

Open klindsay28 opened this issue 4 years ago • 0 comments

The following code snippet demonstrates the problem that I'm running into

import xarray as xr
import esmlab

ds = xr.open_dataset('/glade/work/klindsay/analysis/CESM2_coup_carb_cycle_JAMES/tseries/FG_CO2_ocn_piControl_00.nc')
print(ds)
print('****************************************')
ds_mon_clim = esmlab.climatology(ds, freq='mon')
print(ds_mon_clim)
print('****************************************')
ds_mon_clim.sel(region='Global')

I get the output

<xarray.Dataset>
Dimensions:     (d2: 2, region: 4, time: 14400)
Coordinates:
  * time        (time) object 0001-01-16 12:59:59 ... 1200-12-16 12:00:00
  * region      (region) object 'Global' 'SH_mid_lat' 'low_lat' 'NH_mid_lat'
Dimensions without coordinates: d2
Data variables:
    FG_CO2      (time, region) float32 ...
    time_bound  (time, d2) object ...
Attributes:
    title:             b.e21.B1850.f09_g17.CMIP6-piControl.001
    history:           created by /glade/work/klindsay/analysis/CESM2_coup_ca...
    Conventions:       CF-1.0; http://www.cgd.ucar.edu/cms/eaton/netcdf/CF-cu...
    time_period_freq:  month_1
    model_doi_url:     https://doi.org/10.5065/D67H1H0V
    contents:          Diagnostic and Prognostic Variables
    source:            CCSM POP2, the CCSM Ocean Component
    revision:          $Id: tavg.F90 89644 2018-08-04 14:26:01Z klindsay $
    calendar:          All years have exactly  365 days.
    start_time:        This dataset was created on 2018-08-09 at 18:18:26.3
    cell_methods:      cell_methods = time: mean ==> the variable values are ...
    input_file_list:   /glade/collections/cdg/timeseries-cmip6/b.e21.B1850.f0...
****************************************
<xarray.Dataset>
Dimensions:  (month: 12, region: 4)
Coordinates:
  * month    (month) int64 1 2 3 4 5 6 7 8 9 10 11 12
Dimensions without coordinates: region
Data variables:
    FG_CO2   (month, region) float32 -0.08761434 0.029329425 ... 0.8051468
Attributes:
    title:             b.e21.B1850.f09_g17.CMIP6-piControl.001
    history:           \n2019-10-30 16:16:20.158031 esmlab.climatology(<DATAS...
    Conventions:       CF-1.0; http://www.cgd.ucar.edu/cms/eaton/netcdf/CF-cu...
    time_period_freq:  month_1
    model_doi_url:     https://doi.org/10.5065/D67H1H0V
    contents:          Diagnostic and Prognostic Variables
    source:            CCSM POP2, the CCSM Ocean Component
    revision:          $Id: tavg.F90 89644 2018-08-04 14:26:01Z klindsay $
    calendar:          All years have exactly  365 days.
    start_time:        This dataset was created on 2018-08-09 at 18:18:26.3
    cell_methods:      cell_methods = time: mean ==> the variable values are ...
    input_file_list:   /glade/collections/cdg/timeseries-cmip6/b.e21.B1850.f0...
****************************************
Traceback (most recent call last):
  File "foo2.py", line 10, in <module>
    ds_mon_clim.sel(region='Global')
  File "/glade/work/klindsay/miniconda3/envs/CESM2_coup_carb_cycle_JAMES_tst/lib/python3.7/site-packages/xarray/core/dataset.py", line 2002, in sel
    result = self.isel(indexers=pos_indexers, drop=drop)
  File "/glade/work/klindsay/miniconda3/envs/CESM2_coup_carb_cycle_JAMES_tst/lib/python3.7/site-packages/xarray/core/dataset.py", line 1896, in isel
    indexers_list = list(self._validate_indexers(indexers))
  File "/glade/work/klindsay/miniconda3/envs/CESM2_coup_carb_cycle_JAMES_tst/lib/python3.7/site-packages/xarray/core/dataset.py", line 1774, in _validate_indexers
    index = self.indexes[k]
  File "/glade/work/klindsay/miniconda3/envs/CESM2_coup_carb_cycle_JAMES_tst/lib/python3.7/site-packages/xarray/core/indexes.py", line 36, in __getitem__
    return self._indexes[key]
KeyError: 'region'

Note that ds has a coordinate variable region and ds_mon_clim does not, despite FG_CO2 having region as a dimension.

Because of this, the evaluation of ds_mon_clim.sel(region='Global') generates an error.

print(ds.sel(region='Global')) works fine (not shown here).

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 avatar Oct 30 '19 22:10 klindsay28