Using xarrayutils to build a xgcm compatible grid for observational products


In [ ]:
# Still gotta figure out how to do the download clean...store on figshare as unzipped file?

In [1]:
import xarray as xr
import xgcm
from xarrayutils.build_grids import grid_aggregate,rebuild_grid

In [2]:
path = '/Users/juliusbusecke/Work/CODE/PYTHON/xarrayutils/notebooks/MIMOC_ML_v2'
ds = xr.open_mfdataset(path+'/MIMOC_ML_v2.2_CT_SA_MLP_month*',concat_dim='month')

In [3]:
ds_new = xr.Dataset(data_vars=ds.drop(['LONGITUDE','LATITUDE']).data_vars,coords={'LONG':ds.LONGITUDE.mean('month'),'LAT':ds.LATITUDE.mean('month'),'month':ds.month})
ds_new.LAT.attrs = {'axis':'X',
                    'standard_name': 'x_grid_index',
                    'long_name': 'x-dimension of the grid'}

ds_new.LONG.attrs = {'axis':'Y',
                    'standard_name': 'y_grid_index',
                    'long_name': 'y-dimension of the grid'}

In [4]:
# grid = xgcm.Grid(ds_new)

In [5]:
bins = [('LONG',10),('LAT',10)]
grid_coarse = grid_aggregate(ds_new,bins)


---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-5-18be2f814549> in <module>()
      1 bins = [('LONG',10),('LAT',10)]
----> 2 grid_coarse = grid_aggregate(ds_new,bins)

/Users/juliusbusecke/Work/CODE/PYTHON/xarrayutils/xarrayutils/build_grids.py in grid_aggregate(grid, bins)
     73     temp = dict([])
     74     for ff in ['XC','YC']:
---> 75         temp[ff] = xr.DataArray(da.from_array(grid[ff].data,chunks=grid[ff].shape),
     76                                 coords=grid[ff].coords,dims=grid[ff].dims)
     77 

/Users/juliusbusecke/anaconda/lib/python2.7/site-packages/xarray/core/dataset.pyc in __getitem__(self, key)
    696 
    697         if hashable(key):
--> 698             return self._construct_dataarray(key)
    699         else:
    700             return self._copy_listed(np.asarray(key))

/Users/juliusbusecke/anaconda/lib/python2.7/site-packages/xarray/core/dataset.pyc in _construct_dataarray(self, name)
    640         except KeyError:
    641             _, name, variable = _get_virtual_variable(
--> 642                 self._variables, name, self._level_coords, self.dims)
    643 
    644         coords = OrderedDict()

/Users/juliusbusecke/anaconda/lib/python2.7/site-packages/xarray/core/dataset.pyc in _get_virtual_variable(variables, key, level_vars, dim_sizes)
     69         ref_var = dim_var.to_index_variable().get_level_variable(ref_name)
     70     else:
---> 71         ref_var = variables[ref_name]
     72 
     73     if var_name is None:

KeyError: 'XC'

In [ ]:
out = rebuild_grid(ds_new)

In [ ]:
ds_new

In [ ]:
grid = xgcm.Grid(ds)

In [ ]:
interp_test = grid.diff(ds.ABSOLUTE_SALINITY_MIXED_LAYER,'X')

In [ ]: