Skip to content

Commit

Permalink
Comodo metadata (#39)
Browse files Browse the repository at this point in the history
* fixed swap_dims

* whats new update
  • Loading branch information
rabernat authored Feb 23, 2017
1 parent 191c31b commit 4a2a9b0
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
4 changes: 4 additions & 0 deletions doc/development.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ Development
Release History
---------------

v.0.3.0 (???)
~~~~~~~~~~~~~
- Fix to keep proper Comodo metadata when swapping dimensions.

v0.2.0 (2017-02-14)
~~~~~~~~~~~~~~~~~~~~

Expand Down
6 changes: 4 additions & 2 deletions xmitgcm/mds_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,7 @@ def _set_coords(ds):
def _swap_dimensions(ds, geometry, drop_old=True):
"""Replace logical coordinates with physical ones. Does not work for llc.
"""
# TODO: handle metadata correctly such that the new dimension attributes
# still conform to comodo conventions
keep_attrs = ['axis', 'c_grid_axis_shift']

# this fixes problems
ds = ds.reset_coords()
Expand All @@ -246,6 +245,9 @@ def _swap_dimensions(ds, geometry, drop_old=True):
# take the first row / column
coord_var = coord_var.isel(**{coord_dim: 0}).drop(coord_dim)
ds[new_dim] = coord_var
for key in keep_attrs:
if key in ds[orig_dim].attrs:
ds[new_dim].attrs[key] = ds[orig_dim].attrs[key]
# then swap dims
for orig_dim in ds.dims:
if 'swap_dim' in ds[orig_dim].attrs:
Expand Down
22 changes: 22 additions & 0 deletions xmitgcm/test/test_mds_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,17 @@ def test_open_mdsdataset_minimal(all_mds_datadirs):
ds_expected = xr.Dataset(coords=coords)
assert ds_expected.equals(ds)

# check for comodo metadata needed by xgcm
assert ds['i'].attrs['axis'] == 'X'
assert ds['i_g'].attrs['axis'] == 'X'
assert ds['i_g'].attrs['c_grid_axis_shift'] == -0.5
assert ds['j'].attrs['axis'] == 'Y'
assert ds['j_g'].attrs['axis'] == 'Y'
assert ds['j_g'].attrs['c_grid_axis_shift'] == -0.5
assert ds['k'].attrs['axis'] == 'Z'
assert ds['k_l'].attrs['axis'] == 'Z'
assert ds['k_l'].attrs['c_grid_axis_shift'] == -0.5

def test_read_grid(all_mds_datadirs):
"""Make sure we read all the grid variables."""
dirname, expected = all_mds_datadirs
Expand Down Expand Up @@ -508,6 +519,17 @@ def test_swap_dims(all_mds_datadirs):
grid_vars_to_coords=True)


# check for comodo metadata needed by xgcm
assert ds['XC'].attrs['axis'] == 'X'
assert ds['XG'].attrs['axis'] == 'X'
assert ds['XG'].attrs['c_grid_axis_shift'] == -0.5
assert ds['YC'].attrs['axis'] == 'Y'
assert ds['YG'].attrs['axis'] == 'Y'
assert ds['YG'].attrs['c_grid_axis_shift'] == -0.5
assert ds['Z'].attrs['axis'] == 'Z'
assert ds['Zl'].attrs['axis'] == 'Z'
assert ds['Zl'].attrs['c_grid_axis_shift'] == -0.5

# add extra layers dimensions if needed
if 'layers' in expected:
for layer_name in expected['layers']:
Expand Down
6 changes: 3 additions & 3 deletions xmitgcm/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@
k_u = dict(dims=['k_u'], attrs=dict(
standard_name="z_grid_index_at_lower_w_location",
axis="Z", long_name="z-dimension of the w grid",
c_grid_axis_shift=-0.5, swap_dim='Zu')),
c_grid_axis_shift=0.5, swap_dim='Zu')),
k_l = dict(dims=['k_l'], attrs=dict(
standard_name="z_grid_index_at_upper_w_location",
axis="Z", long_name="z-dimension of the w grid",
c_grid_axis_shift=0.5, swap_dim='Zl')),
c_grid_axis_shift=-0.5, swap_dim='Zl')),
# this is complicated because it is offset in both directions - allowed by comodo?
k_p1 = dict(dims=['k_p1'], attrs=dict(
standard_name="z_grid_index_at_w_location",
Expand Down Expand Up @@ -384,7 +384,7 @@
attrs=dict(standard_name='%s_concentration' % trname,
long_name="Concentration of %s" % trname,
units="kg m-3")
)
)


# these aliases are necessary to deal with the LLC output which was saved with
Expand Down

0 comments on commit 4a2a9b0

Please sign in to comment.