Skip to content

S-coordinates - issues with top and bottom cells #620

@nvogtvincent

Description

@nvogtvincent

Firstly, thank you so much for producing OceanParcels (and all of the documentation) - fabulous work!

I'm trying to use fields from Croco (a version of ROMS) which uses an s-coordinate system. Following this discussion and using the s-coordinate functions described on this page (currently using the old ROMS function for legacy reasons), I've produced a sigma-depth file using the code at the bottom of this post (adapted from the code Philippe wrote in the discussion above).

The problem is that whilst this is running, I'm getting "OutOfBoundsError" when I initialise shallow (<5m) particles. I've been sloppy and for testing purposes I'm currently using a snapshot as my zeta field rather than a time-mean or time-evolving zeta, but this doesn't explain why particles initialised at -4m are 'out of bounds', and it isn't related to the vertical velocity because I have the same issue with 2D advection.

I'm pretty certain that the reason why this isn't working is because I'm currently using the sigma/stretching function values at the rho points rather than the zeta points, so the shallowest values in my depth-sigma field is as deep as -3.5m rather than zeta which is what I assume it's supposed to be. But I'm not sure what to do about this because Croco returns u/v fields at the rho depth level, and if I change the coordinates I'm inputting into the dimensions list to use the n+1 w depths rather than the n u/v/rho depths, I get a "index X is out of bounds..." error.

I'm a bit perplexed at the moment, assistance would be greatly appreciated!

from parcels import FieldSet, ParticleSet, Field, JITParticle, AdvectionRK4_3D, AdvectionRK4, CurvilinearSGrid, RectilinearSGrid, plotTrajectoriesFile
from netCDF4 import Dataset
from datetime import timedelta
from glob import glob 
import numpy as np
from scipy.interpolate import interp2d, RectBivariateSpline
from datetime import timedelta as delta
from os import path

# Import files
data_path = 'XXX'
his = data_path + 'history_file.nc'
grd = data_path + 'grid_file.nc' # Not used

# Load in coordinates
fh = Dataset(his, mode='r+')
lon_rho = fh.variables['lon_rho'][:]
lat_rho = fh.variables['lat_rho'][:]
lon_u = fh.variables['lon_u'][:]
lat_u = fh.variables['lat_u'][:]
lon_v = fh.variables['lon_v'][:]
lat_v = fh.variables['lat_v'][:]
s_rho = fh.dimensions['s_rho'] # Sigma rho
s_rho_d = fh.variables['s_rho'] # Sigma rho (variable-ified)
Cs_r = fh.variables['Cs_r'][:] # Stretching function (of sigma) rho
t = fh.variables['time'][:]
h = fh.variables['h'][:]
zeta = fh.variables['zeta'][:]
h_c = fh.variables['hc'] # Vertical stretching parameter
xi_rho = fh.dimensions['xi_rho']
eta_rho = fh.dimensions['eta_rho']
zdim = s_rho.size

fh.close

# Construct psi grid
[lon_psi_, lat_psi_] = np.meshgrid(lon_u[0,:], lat_v[:,0])
depth_psi = np.zeros((Cs_r.size, lon_psi_.shape[0], lon_psi_.shape[1]))

for k in range(zdim):
    #depth_rho = zeta[0, :, :]*(1+s_w_d[k]) + h_c*s_w_d[k] + (h - h_c)*Cs_w[k] # Convert sigma to depth - check
    S_ = h_c*s_rho_d[k] + (h - h_c)*Cs_r[k]
    depth_rho = S_ + zeta[0,:,:]*(1+(S_/h))
    depth_interp = RectBivariateSpline(lat_rho[:,0], lon_rho[0,:], depth_rho)
    depth_psi[k,:,:] = depth_interp(lat_v[:,0], lon_u[0, :],)
    # depth_psi[k,:,:] = depth_interp(lon_psi1, lat_psi1)

# Question still remains of how to apply this to u/v
if 'sigma_psi' in fh.variables:
    pass
else:
    sigma_psi = fh.createVariable('sigma_psi', 'f', dimensions=('s_rho', 'eta_v', 'xi_u'))
    lat_psi = fh.createVariable('lat_psi', 'f', dimensions=('eta_v', 'xi_u'))
    lon_psi = fh.createVariable('lon_psi', 'f', dimensions=('eta_v', 'xi_u'))
    
    lat_psi[:] = lat_psi_ # Write depth to file
    lon_psi[:] = lon_psi_ # Write depth to file
    sigma_psi[:] = depth_psi # Write depth to file

filenames = {'U': {'lon': grd, 'lat': grd, 'depth': his, 'data': his},
             'V': {'lon': grd, 'lat': grd, 'depth': his, 'data': his},
             'W': {'lon': grd, 'lat': grd, 'depth': his, 'data': his}}

variables = {'U': 'u',
             'V': 'v',
             'W': 'w'}

dimensions = {'U': {'lon': 'lon_psi', 'lat': 'lat_psi', 'depth': 'sigma_psi', 'time': 'time'},
              'V': {'lon': 'lon_psi', 'lat': 'lat_psi', 'depth': 'sigma_psi', 'time': 'time'},
              'W': {'lon': 'lon_psi', 'lat': 'lat_psi', 'depth': 'sigma_psi', 'time': 'time'}}

fieldset = FieldSet.from_c_grid_dataset(filenames, variables, dimensions)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions