question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

fitting a ROMS curvilinear grid

See original GitHub issue

Hi, I’m trying to use xgcm for analyzing ROMS model output on a curvilinear grid, but I can’t seem to fit the grid

Here is how my dataset looks like:

import xarray as xr
from xgcm import Grid

model_path = 'http://barataria.tamu.edu:8080/thredds/dodsC/NcML/txla_hindcast_agg'
model = xr.open_dataset(model_path)

model
Out[2]:
<xarray.Dataset>
Dimensions:         (eta_psi: 190, eta_rho: 191, eta_u: 191, eta_v: 190, ocean_time: 210384, s_rho: 30, s_w: 31, tracer: 6, xi_psi: 670, xi_rho: 671, xi_u: 670, xi_v: 671)
Coordinates:
  * s_rho           (s_rho) float64 -0.9833 -0.95 -0.9167 -0.8833 -0.85 ...
  * s_w             (s_w) float64 -1.0 -0.9667 -0.9333 -0.9 -0.8667 -0.8333 ...
    lon_rho         (eta_rho, xi_rho) float64 ...
    lat_rho         (eta_rho, xi_rho) float64 ...
    lon_u           (eta_u, xi_u) float64 ...
    lat_u           (eta_u, xi_u) float64 ...
    lon_v           (eta_v, xi_v) float64 ...
    lat_v           (eta_v, xi_v) float64 ...
    lon_psi         (eta_psi, xi_psi) float64 ...
    lat_psi         (eta_psi, xi_psi) float64 ...
  * ocean_time      (ocean_time) datetime64[ns] 1993-01-01T01:00:00 ...
Dimensions without coordinates: eta_psi, eta_rho, eta_u, eta_v, tracer, xi_psi, xi_rho, xi_u, xi_v
Data variables:
    ntimes          int32 ...
...
    h               (eta_rho, xi_rho) float64 ...
    f               (eta_rho, xi_rho) float64 ...
    pm              (eta_rho, xi_rho) float64 ...
    pn              (eta_rho, xi_rho) float64 ...
    angle           (eta_rho, xi_rho) float64 ...
    mask_rho        (eta_rho, xi_rho) float64 ...
    mask_u          (eta_u, xi_u) float64 ...
    mask_v          (eta_v, xi_v) float64 ...
    mask_psi        (eta_psi, xi_psi) float64 ...
    zeta            (ocean_time, eta_rho, xi_rho) float32 ...
    u               (ocean_time, s_rho, eta_u, xi_u) float32 ...
    v               (ocean_time, s_rho, eta_v, xi_v) float32 ...
    w               (ocean_time, s_w, eta_rho, xi_rho) float32 ...
    temp            (ocean_time, s_rho, eta_rho, xi_rho) float32 ...
    salt            (ocean_time, s_rho, eta_rho, xi_rho) float32 ...
    dye_01          (ocean_time, s_rho, eta_rho, xi_rho) float32 ...
    dye_02          (ocean_time, s_rho, eta_rho, xi_rho) float32 ...
    dye_03          (ocean_time, s_rho, eta_rho, xi_rho) float32 ...
    dye_04          (ocean_time, s_rho, eta_rho, xi_rho) float32 ...
    AKs             (ocean_time, s_w, eta_rho, xi_rho) float32 ...
    tke             (ocean_time, s_w, eta_rho, xi_rho) float32 ...
    gls             (ocean_time, s_w, eta_rho, xi_rho) float32 ...
    Uwind           (ocean_time, eta_rho, xi_rho) float32 ...
    Vwind           (ocean_time, eta_rho, xi_rho) float32 ...
    shflux          (ocean_time, eta_rho, xi_rho) float32 ...
    ssflux          (ocean_time, eta_rho, xi_rho) float32 ...
    sustr           (ocean_time, eta_u, xi_u) float32 ...
    svstr           (ocean_time, eta_v, xi_v) float32 ...
Attributes:
    file:                      /scratch/user/d.kobashi/outputs_20yr_obc/1993/...
    format:                    netCDF-4/HDF5 file
    Conventions:               CF-1.4
...

As it’s a curvilinear grid the metadata seems to not be in a currently readable. So following the convention on the documentation site, I’ve tried to create my coord dictionary:


In [85]:

coords_roms={'xi':{'center':'xi_rho', 'inner':'xi_u', 'outer':'xi_psi'}, 
        'eta':{'center':'eta_rho', 'inner':'eta_v', 'outer':'eta_psi' }, 
        's':{'center':'s_rho', 'outer':'s_w'}}
grid = Grid(model, coords=coords_roms)
grid
Out[85]:
<xgcm.Grid>
xi Axis (periodic):
  * center   xi_rho (671) --> outer
  * inner    xi_u (670) --> center
  * outer    xi_psi (670) --> center
eta Axis (periodic):
  * center   eta_rho (191) --> outer
  * inner    eta_v (190) --> center
  * outer    eta_psi (190) --> center
s Axis (periodic):
  * center   s_rho (30) --> outer
  * outer    s_w (31) --> center

this seems to work but I’m losing xi_v in the xi axis (staggered on eta), and eta_u on the eta axis (staggered on xi)

so I expected something like this to work:

coords_simp={'xi':{'center':['xi_rho', 'xi_v'], 'inner':'xi_u', 'outer':'xi_psi'}, 
        'eta':{'center':['eta_rho', 'eta_u'], 'inner':'eta_v', 'outer':'eta_psi'}, 
        's':{'center':'s_rho', 'outer':'s_w'}}

but it doesn’t

grid = Grid(model, coords=coords_roms)
grid
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
~/miniconda3/envs/owl/lib/python3.6/site-packages/IPython/core/formatters.py in __call__(self, obj)
    700                 type_pprinters=self.type_printers,
    701                 deferred_pprinters=self.deferred_printers)
--> 702             printer.pretty(obj)
    703             printer.flush()
    704             return stream.getvalue()

~/miniconda3/envs/owl/lib/python3.6/site-packages/IPython/lib/pretty.py in pretty(self, obj)
    398                         if cls is not object \
    399                                 and callable(cls.__dict__.get('__repr__')):
--> 400                             return _repr_pprint(obj, self, cycle)
    401 
    402             return _default_pprint(obj, self, cycle)

~/miniconda3/envs/owl/lib/python3.6/site-packages/IPython/lib/pretty.py in _repr_pprint(obj, p, cycle)
    693     """A pprint that just redirects to the normal repr function."""
    694     # Find newlines and replace them with p.break_()
--> 695     output = repr(obj)
    696     for idx,output_line in enumerate(output.splitlines()):
    697         if idx:

~/miniconda3/envs/owl/lib/python3.6/site-packages/xgcm-0.1.0-py3.6.egg/xgcm/grid.py in __repr__(self)
    783             is_periodic = 'periodic' if axis._periodic else 'not periodic'
    784             summary.append('%s Axis (%s):' % (name, is_periodic))
--> 785             summary += axis._coord_desc()
    786         return '\n'.join(summary)
    787 

~/miniconda3/envs/owl/lib/python3.6/site-packages/xgcm-0.1.0-py3.6.egg/xgcm/grid.py in _coord_desc(self)
    166         summary = []
    167         for name, coord in iteritems(self.coords):
--> 168             coord_info = ('  * %-8s %s (%g)' % (name, coord.name, len(coord)))
    169             if name in self._default_shifts:
    170                 coord_info += ' --> %s' % self._default_shifts[name]

~/miniconda3/envs/owl/lib/python3.6/site-packages/xarray/core/common.py in __getattr__(self, name)
    174                     return source[name]
    175         raise AttributeError("%r object has no attribute %r" %
--> 176                              (type(self).__name__, name))
    177 
    178     def __setattr__(self, name, value):

AttributeError: 'Dataset' object has no attribute 'name'

trying to create an “axis” attribute in my dataset doesn’t​ seem to work either:

In [92]:

model.xi_rho.attrs['axis']=model.xi_u.attrs['axis']=model.xi_v.attrs['axis']=model.lon_psi.attrs['axis']='xi'
model.xi_u.attrs['c_grid_axis_shift']= 0.5
model.xi_psi.attrs['c_grid_axis_shift']= -0.5
​
grid = Grid(model, periodic=['xi'])
grid
​
Out[92]:
<xgcm.Grid>

Any idea on how to solve this issue?

please, please, and thank you

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:40 (23 by maintainers)

github_iconTop GitHub Comments

1reaction
dcheriancommented, Sep 14, 2020

Regarding the z-coordinate, because ROMS output doesn’t generally have that as an output, we have to recreate it depending on the function used in the run.

should add this to cf-xarray: https://github.com/xarray-contrib/cf-xarray/issues/34

1reaction
rabernatcommented, Sep 14, 2020

xgcm doesn’t currently recognize the typical ROMS output grid, which follows the SGRID convention,

We would still like to solve this! See #109…just has not been implemented yet.

I don’t know then if there is a need to do something in the xgcm code, as we are happy with the current solution that we use in xroms.

As a long-term roadmap, we would love to improve things in xgcm such that fewer workarounds are needed downstream, e.g. in xroms. For the short-term, I’m glad things are in a useable state as is.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Curvilinear grid for ROMS - Ocean Modeling Discussion
I am trying to make a curvilinear grid for ROMS, this grid is at equal distance at polar regions so it is curvilinear...
Read more >
Subsetting a curvilinear netCDF file (ROMS model output ...
This is ROMS model output in NetCDF form, but the grid is curvilinear (lon and lat arrays are 2D). I want to get...
Read more >
ROMS - Forward tracking — PyLag 0.6.1 documentation
We do this by first moving the u and v velocity components to rho-points, then rotating the velocity field onto a geographic lat-lon...
Read more >
Grid generation for numerical ocean models - ResearchGate
1. Spherical, Curvilinear or Non-curvilinear? ... Note that [ROMS'] general formulation of curvilinear coordinates includes Cartesian coordinates ...
Read more >
Technical Manual for a Coupled Sea-Ice/Ocean Circulation ...
13 A tiled grid with some ROMS tile variables. ... Appendix C contains the curvilinear version of several common vector quantities.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found