You can run this notebook in a live session or view it on Github.

# ROMS Ocean Model Example

The Regional Ocean Modeling System (ROMS) is an open source hydrodynamic model that is used for simulating currents and water properties in coastal and estuarine regions. ROMS is one of a few standard ocean models, and it has an active user community.

ROMS uses a regular C-Grid in the horizontal, similar to other structured grid ocean and atmospheric models, and a stretched vertical coordinate (see the ROMS documentation for more details). Both of these require special treatment when using `xarray` to analyze ROMS ocean model output. This example notebook shows how to create a lazily evaluated vertical coordinate, and make some basic plots. The `xgcm` package is required to do analysis that is aware of the horizontal C-Grid.

    [1]:

    import numpy as np
    import cartopy.crs as ccrs
    import cartopy.feature as cfeature
    import matplotlib.pyplot as plt

    %matplotlib inline

    import xarray as xr

Load a sample ROMS file. This is a subset of a full model available at

    http://barataria.tamu.edu/thredds/catalog.html?dataset=txla_hindcast_agg

The subsetting was done using the following command on one of the output files:

    #open dataset
    ds = xr.open_dataset('/d2/shared/TXLA_ROMS/output_20yr_obc/2001/ocean_his_0015.nc')

    # Turn on chunking to activate dask and parallelize read/write.
    ds = ds.chunk({'ocean_time': 1})

    # Pick out some of the variables that will be included as coordinates
    ds = ds.set_coords(['Cs_r', 'Cs_w', 'hc', 'h', 'Vtransform'])

    # Select a a subset of variables. Salt will be visualized, zeta is used to
    # calculate the vertical coordinate
    variables = ['salt', 'zeta']
    ds[variables].isel(ocean_time=slice(47, None, 7*24),
                       xi_rho=slice(300, None)).to_netcdf('ROMS_example.nc', mode='w')

So, the `ROMS_example.nc` file contains a subset of the grid, one 3D variable, and two time steps.

## Load in ROMS dataset as an xarray object

    [2]:

    # load in the file
    ds = xr.tutorial.open_dataset("ROMS_example.nc", chunks={"ocean_time": 1})

    # This is a way to turn on chunking and lazy evaluation. Opening with mfdataset, or
    # setting the chunking in the open_dataset would also achieve this.
    ds

    ---------------------------------------------------------------------------
    gaierror                                  Traceback (most recent call last)
    File /usr/lib/python3/dist-packages/urllib3/connection.py:198, in HTTPConnection._new_conn(self)
        197 try:
    --> 198     sock = connection.create_connection(
        199         (self._dns_host, self.port),
        200         self.timeout,
        201         source_address=self.source_address,
        202         socket_options=self.socket_options,
        203     )
        204 except socket.gaierror as e:

    File /usr/lib/python3/dist-packages/urllib3/util/connection.py:60, in create_connection(address, timeout, source_address, socket_options)
         58     raise LocationParseError(f"'{host}', label empty or too long") from None
    ---> 60 for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
         61     af, socktype, proto, canonname, sa = res

    File /usr/lib/python3.13/socket.py:977, in getaddrinfo(host, port, family, type, proto, flags)
        976 addrlist = []
    --> 977 for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
        978     af, socktype, proto, canonname, sa = res

    gaierror: [Errno -2] Name or service not known

    The above exception was the direct cause of the following exception:

    NameResolutionError                       Traceback (most recent call last)
    File /usr/lib/python3/dist-packages/urllib3/connectionpool.py:787, in HTTPConnectionPool.urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, preload_content, decode_content, **response_kw)
        786 # Make the request on the HTTPConnection object
    --> 787 response = self._make_request(
        788     conn,
        789     method,
        790     url,
        791     timeout=timeout_obj,
        792     body=body,
        793     headers=headers,
        794     chunked=chunked,
        795     retries=retries,
        796     response_conn=response_conn,
        797     preload_content=preload_content,
        798     decode_content=decode_content,
        799     **response_kw,
        800 )
        802 # Everything went great!

    File /usr/lib/python3/dist-packages/urllib3/connectionpool.py:488, in HTTPConnectionPool._make_request(self, conn, method, url, body, headers, retries, timeout, chunked, response_conn, preload_content, decode_content, enforce_content_length)
        487         new_e = _wrap_proxy_error(new_e, conn.proxy.scheme)
    --> 488     raise new_e
        490 # conn.request() calls http.client.*.request, not the method in
        491 # urllib3.request. It also calls makefile (recv) on the socket.

    File /usr/lib/python3/dist-packages/urllib3/connectionpool.py:464, in HTTPConnectionPool._make_request(self, conn, method, url, body, headers, retries, timeout, chunked, response_conn, preload_content, decode_content, enforce_content_length)
        463 try:
    --> 464     self._validate_conn(conn)
        465 except (SocketTimeout, BaseSSLError) as e:

    File /usr/lib/python3/dist-packages/urllib3/connectionpool.py:1093, in HTTPSConnectionPool._validate_conn(self, conn)
       1092 if conn.is_closed:
    -> 1093     conn.connect()
       1095 # TODO revise this, see https://github.com/urllib3/urllib3/issues/2791

    File /usr/lib/python3/dist-packages/urllib3/connection.py:704, in HTTPSConnection.connect(self)
        703 sock: socket.socket | ssl.SSLSocket
    --> 704 self.sock = sock = self._new_conn()
        705 server_hostname: str = self.host

    File /usr/lib/python3/dist-packages/urllib3/connection.py:205, in HTTPConnection._new_conn(self)
        204 except socket.gaierror as e:
    --> 205     raise NameResolutionError(self.host, self, e) from e
        206 except SocketTimeout as e:

    NameResolutionError: <urllib3.connection.HTTPSConnection object at 0x7d7fc6f93620>: Failed to resolve 'github.com' ([Errno -2] Name or service not known)

    The above exception was the direct cause of the following exception:

    MaxRetryError                             Traceback (most recent call last)
    File /usr/lib/python3/dist-packages/requests/adapters.py:667, in HTTPAdapter.send(self, request, stream, timeout, verify, cert, proxies)
        666 try:
    --> 667     resp = conn.urlopen(
        668         method=request.method,
        669         url=url,
        670         body=request.body,
        671         headers=request.headers,
        672         redirect=False,
        673         assert_same_host=False,
        674         preload_content=False,
        675         decode_content=False,
        676         retries=self.max_retries,
        677         timeout=timeout,
        678         chunked=chunked,
        679     )
        681 except (ProtocolError, OSError) as err:

    File /usr/lib/python3/dist-packages/urllib3/connectionpool.py:841, in HTTPConnectionPool.urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, preload_content, decode_content, **response_kw)
        839     new_e = ProtocolError("Connection aborted.", new_e)
    --> 841 retries = retries.increment(
        842     method, url, error=new_e, _pool=self, _stacktrace=sys.exc_info()[2]
        843 )
        844 retries.sleep()

    File /usr/lib/python3/dist-packages/urllib3/util/retry.py:519, in Retry.increment(self, method, url, response, error, _pool, _stacktrace)
        518     reason = error or ResponseError(cause)
    --> 519     raise MaxRetryError(_pool, url, reason) from reason  # type: ignore[arg-type]
        521 log.debug("Incremented Retry for (url='%s'): %r", url, new_retry)

    MaxRetryError: HTTPSConnectionPool(host='github.com', port=443): Max retries exceeded with url: /pydata/xarray-data/raw/master/ROMS_example.nc (Caused by NameResolutionError("<urllib3.connection.HTTPSConnection object at 0x7d7fc6f93620>: Failed to resolve 'github.com' ([Errno -2] Name or service not known)"))

    During handling of the above exception, another exception occurred:

    ConnectionError                           Traceback (most recent call last)
    Cell In[2], line 2
          1 # load in the file
    ----> 2 ds = xr.tutorial.open_dataset("ROMS_example.nc", chunks={"ocean_time": 1})
          4 # This is a way to turn on chunking and lazy evaluation. Opening with mfdataset, or
          5 # setting the chunking in the open_dataset would also achieve this.
          6 ds

    File /usr/lib/python3/dist-packages/xarray/tutorial.py:167, in open_dataset(name, cache, cache_dir, engine, **kws)
        164 downloader = pooch.HTTPDownloader(headers=headers)
        166 # retrieve the file
    --> 167 filepath = pooch.retrieve(
        168     url=url, known_hash=None, path=cache_dir, downloader=downloader
        169 )
        170 ds = _open_dataset(filepath, engine=engine, **kws)
        171 if not cache:

    File /usr/lib/python3/dist-packages/pooch/core.py:239, in retrieve(url, known_hash, fname, path, processor, downloader, progressbar)
        236 if downloader is None:
        237     downloader = choose_downloader(url, progressbar=progressbar)
    --> 239 stream_download(url, full_path, known_hash, downloader, pooch=None)
        241 if known_hash is None:
        242     get_logger().info(
        243         "SHA256 hash of downloaded file: %s\n"
        244         "Use this value as the 'known_hash' argument of 'pooch.retrieve'"
       (...)
        247         file_hash(str(full_path)),
        248     )

    File /usr/lib/python3/dist-packages/pooch/core.py:807, in stream_download(url, fname, known_hash, downloader, pooch, retry_if_failed)
        803 try:
        804     # Stream the file to a temporary so that we can safely check its
        805     # hash before overwriting the original.
        806     with temporary_file(path=str(fname.parent)) as tmp:
    --> 807         downloader(url, tmp, pooch)
        808         hash_matches(tmp, known_hash, strict=True, source=str(fname.name))
        809         shutil.move(tmp, str(fname))

    File /usr/lib/python3/dist-packages/pooch/downloaders.py:220, in HTTPDownloader.__call__(self, url, output_file, pooch, check_only)
        218     # pylint: enable=consider-using-with
        219 try:
    --> 220     response = requests.get(url, timeout=timeout, **kwargs)
        221     response.raise_for_status()
        222     content = response.iter_content(chunk_size=self.chunk_size)

    File /usr/lib/python3/dist-packages/requests/api.py:73, in get(url, params, **kwargs)
         62 def get(url, params=None, **kwargs):
         63     r"""Sends a GET request.
         64
         65     :param url: URL for the new :class:`Request` object.
       (...)
         70     :rtype: requests.Response
         71     """
    ---> 73     return request("get", url, params=params, **kwargs)

    File /usr/lib/python3/dist-packages/requests/api.py:59, in request(method, url, **kwargs)
         55 # By using the 'with' statement we are sure the session is closed, thus we
         56 # avoid leaving sockets open which can trigger a ResourceWarning in some
         57 # cases, and look like a memory leak in others.
         58 with sessions.Session() as session:
    ---> 59     return session.request(method=method, url=url, **kwargs)

    File /usr/lib/python3/dist-packages/requests/sessions.py:589, in Session.request(self, method, url, params, data, headers, cookies, files, auth, timeout, allow_redirects, proxies, hooks, stream, verify, cert, json)
        584 send_kwargs = {
        585     "timeout": timeout,
        586     "allow_redirects": allow_redirects,
        587 }
        588 send_kwargs.update(settings)
    --> 589 resp = self.send(prep, **send_kwargs)
        591 return resp

    File /usr/lib/python3/dist-packages/requests/sessions.py:703, in Session.send(self, request, **kwargs)
        700 start = preferred_clock()
        702 # Send the request
    --> 703 r = adapter.send(request, **kwargs)
        705 # Total elapsed time of the request (approximately)
        706 elapsed = preferred_clock() - start

    File /usr/lib/python3/dist-packages/requests/adapters.py:700, in HTTPAdapter.send(self, request, stream, timeout, verify, cert, proxies)
        696     if isinstance(e.reason, _SSLError):
        697         # This branch is for urllib3 v1.22 and later.
        698         raise SSLError(e, request=request)
    --> 700     raise ConnectionError(e, request=request)
        702 except ClosedPoolError as e:
        703     raise ConnectionError(e, request=request)

    ConnectionError: HTTPSConnectionPool(host='github.com', port=443): Max retries exceeded with url: /pydata/xarray-data/raw/master/ROMS_example.nc (Caused by NameResolutionError("<urllib3.connection.HTTPSConnection object at 0x7d7fc6f93620>: Failed to resolve 'github.com' ([Errno -2] Name or service not known)"))

## Add a lazilly calculated vertical coordinates

Write equations to calculate the vertical coordinate. These will be only evaluated when data is requested. Information about the ROMS vertical coordinate can be found (here)[https://www.myroms.org/wiki/Vertical_S-coordinate]

In short, for `Vtransform==2` as used in this example,

\(Z_0 = (h_c \, S + h \,C) / (h_c + h)\)

\(z = Z_0 (\zeta + h) + \zeta\)

where the variables are defined as in the link above.

    [3]:

    if ds.Vtransform == 1:
        Zo_rho = ds.hc * (ds.s_rho - ds.Cs_r) + ds.Cs_r * ds.h
        z_rho = Zo_rho + ds.zeta * (1 + Zo_rho / ds.h)
    elif ds.Vtransform == 2:
        Zo_rho = (ds.hc * ds.s_rho + ds.Cs_r * ds.h) / (ds.hc + ds.h)
        z_rho = ds.zeta + (ds.zeta + ds.h) * Zo_rho

    ds.coords["z_rho"] = z_rho.transpose()  # needing transpose seems to be an xarray bug
    ds.salt

    ---------------------------------------------------------------------------
    NameError                                 Traceback (most recent call last)
    Cell In[3], line 1
    ----> 1 if ds.Vtransform == 1:
          2     Zo_rho = ds.hc * (ds.s_rho - ds.Cs_r) + ds.Cs_r * ds.h
          3     z_rho = Zo_rho + ds.zeta * (1 + Zo_rho / ds.h)

    NameError: name 'ds' is not defined

## A naive vertical slice

Creating a slice using the s-coordinate as the vertical dimension is typically not very informative.

    [4]:

    ds.salt.isel(xi_rho=50, ocean_time=0).plot()

    ---------------------------------------------------------------------------
    NameError                                 Traceback (most recent call last)
    Cell In[4], line 1
    ----> 1 ds.salt.isel(xi_rho=50, ocean_time=0).plot()

    NameError: name 'ds' is not defined

We can feed coordinate information to the plot method to give a more informative cross-section that uses the depths. Note that we did not need to slice the depth or longitude information separately, this was done automatically as the variable was sliced.

    [5]:

    section = ds.salt.isel(xi_rho=50, eta_rho=slice(0, 167), ocean_time=0)
    section.plot(x="lon_rho", y="z_rho", figsize=(15, 6), clim=(25, 35))
    plt.ylim([-100, 1]);

    ---------------------------------------------------------------------------
    NameError                                 Traceback (most recent call last)
    Cell In[5], line 1
    ----> 1 section = ds.salt.isel(xi_rho=50, eta_rho=slice(0, 167), ocean_time=0)
          2 section.plot(x="lon_rho", y="z_rho", figsize=(15, 6), clim=(25, 35))
          3 plt.ylim([-100, 1]);

    NameError: name 'ds' is not defined

## A plan view

Now make a naive plan view, without any projection information, just using lon/lat as x/y. This looks OK, but will appear compressed because lon and lat do not have an aspect constrained by the projection.

    [6]:

    ds.salt.isel(s_rho=-1, ocean_time=0).plot(x="lon_rho", y="lat_rho")

    ---------------------------------------------------------------------------
    NameError                                 Traceback (most recent call last)
    Cell In[6], line 1
    ----> 1 ds.salt.isel(s_rho=-1, ocean_time=0).plot(x="lon_rho", y="lat_rho")

    NameError: name 'ds' is not defined

And let’s use a projection to make it nicer, and add a coast.

    [7]:

    proj = ccrs.LambertConformal(central_longitude=-92, central_latitude=29)
    fig = plt.figure(figsize=(15, 5))
    ax = plt.axes(projection=proj)
    ds.salt.isel(s_rho=-1, ocean_time=0).plot(
        x="lon_rho", y="lat_rho", transform=ccrs.PlateCarree()
    )

    coast_10m = cfeature.NaturalEarthFeature(
        "physical", "land", "10m", edgecolor="k", facecolor="0.8"
    )
    ax.add_feature(coast_10m)

    ---------------------------------------------------------------------------
    NameError                                 Traceback (most recent call last)
    Cell In[7], line 4
          2 fig = plt.figure(figsize=(15, 5))
          3 ax = plt.axes(projection=proj)
    ----> 4 ds.salt.isel(s_rho=-1, ocean_time=0).plot(
          5     x="lon_rho", y="lat_rho", transform=ccrs.PlateCarree()
          6 )
          8 coast_10m = cfeature.NaturalEarthFeature(
          9     "physical", "land", "10m", edgecolor="k", facecolor="0.8"
         10 )
         11 ax.add_feature(coast_10m)

    NameError: name 'ds' is not defined

    [ ]: