Skip to content

Commit

Permalink
fix for 2d diagnostics llc bug (#146)
Browse files Browse the repository at this point in the history
* fix for 2d diags issue

* remove comment

* remove old numpy env from ci
  • Loading branch information
rabernat authored May 20, 2019
1 parent 310f3c8 commit abb0072
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 15 deletions.
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ matrix:
env: CONDA_ENV=py27-xarray0.10.9
- python: 2.7
env: CONDA_ENV=py27-xarray0.11.3
- python: 2.7
env: CONDA_ENV=py27-numpy1.11.1
- python: 3.5
env: CONDA_ENV=py35
- python: 3.6
Expand Down
11 changes: 0 additions & 11 deletions ci/environment-py27-numpy1.11.1.yml

This file was deleted.

7 changes: 5 additions & 2 deletions xmitgcm/mds_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -660,11 +660,14 @@ def load_from_prefix(self, prefix, iternum=None, extra_metadata=None):
# How should we handle this? Can either eliminate one of the dims
# or add an extra axis to the data. Let's try the former, on the
# grounds that it is simpler for the user.
if len(dims) == 3 and data.ndim == 2:
if ((len(dims) == 3 and data.ndim == 2) or
(self.llc and (len(dims) == 3 and data.ndim == 3))):
# Deleting the first dimension (z) assumes that 2D data always
# corresponds to x,y horizontal data. Is this really true?
# The answer appears to be yes: 2D (x|y,z) data retains the
# missing dimension as an axis of length 1.
# Also handles https://github.com/xgcm/xmitgcm/issues/140
# (special case for 2d llc diags)
dims = dims[1:]
elif len(dims) == 1 and data.ndim > 1:
# this is for certain profile data like RC, PHrefC, etc.
Expand Down Expand Up @@ -928,5 +931,5 @@ def _reshape_for_llc(dims, data):
# add face dimension to dims
jdim = dims.index(dim)
dims.insert(jdim, LLC_FACE_DIMNAME)
assert data.ndim==len(dims)
assert data.ndim==len(dims), '%r %r' % (data.shape, dims)
return dims, data
34 changes: 34 additions & 0 deletions xmitgcm/test/test_mds_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import xarray as xr
from glob import glob
from shutil import copyfile
import py

_TESTDATA_FILENAME = 'testdata.tar.gz'
_TESTDATA_ITERS = [39600, ]
Expand Down Expand Up @@ -194,6 +195,39 @@ def test_open_dataset_no_meta(all_mds_datadirs):
ds = xmitgcm.open_mdsdataset(dirname, prefix=['T', 'Eta'],
nz=nz, **kwargs)

def test_open_dataset_2D_diags(all_mds_datadirs):
# convert 3D fields with only 2D diagnostic output
# https://github.com/xgcm/xmitgcm/issues/140
dirname, expected = all_mds_datadirs

shape = expected['shape']

nz = shape[0]
ny, nx = shape[-2:]
shape_2d = shape[1:]
dims_2d = ('j', 'i')
if expected['geometry']=='llc':
dims_2d = ('face',) + dims_2d
ny = nx*shape[-3]
dims_3d = dims_2d if nz==1 else ('k',) + dims_2d
dims_2d = ('time',) + dims_2d
dims_3d = ('time',) + dims_3d

it = expected['test_iternum']
kwargs = dict(iters=it, geometry=expected['geometry'], read_grid=False,
swap_dims=False)

to_hide = ['T.%010d.meta' % it, 'T.%010d.data' % it]
with hide_file(dirname, *to_hide):

ldir = py.path.local(dirname)
old_prefix = 'Eta.%010d' % it
new_prefix = 'T.%010d' % it
for suffix in ['.data', '.meta']:
lp = ldir.join(old_prefix + suffix)
lp.copy(ldir.join(new_prefix + suffix))

ds = xmitgcm.open_mdsdataset(dirname, prefix=['T'], **kwargs)

def test_swap_dims(all_mds_datadirs):
"""See if we can swap dimensions."""
Expand Down

0 comments on commit abb0072

Please sign in to comment.