Skip to content

reader_netCDF_CF_generic selects wrong vertical layers when z-coordinate is ascending (altitude data) #1799

Description

@BenSpin

When using reader_netCDF_CF_generic with a NetCDF file where the z-coordinate is ascending (altitude convention), the reader silently selects incorrect vertical layers. This causes particles to sample wind data from the wrong altitudes.

Problematic Code is reader_netCDF_CF_generic.py method get_variables

# NB: may need to flip if self.z is ascending
indices = np.searchsorted(-self.z, [-z.min(), -z.max()])

np.searchsorted requires the input array to be ascending.

  • If self.z is descending (ocean depth convention), then -self.z becomes ascending and works correctly.
  • If self.z is ascending (altitude convention), then -self.z becomes descending and returns incorrect indices.

The comment acknowledges this issue, but as far as I can tell no correction is implemented.

This results in a silent failure (no warning or error) and particles receive wind data from incorrect altitude layers. What happens is the simulations appear valid but are physically incorrect.

To get aroudn this you can store the z-coordinate in descending order in the NetCDF file:

[25800, 25500, ..., 0]

A potential correction would be to detect the intended order from the dataset within the reader

if self.z[0] > self.z[-1]:  # descending
    indices = np.searchsorted(-self.z, [-z.min(), -z.max()])
else:  # ascending 
    indices = np.searchsorted(self.z, [z.min(), z.max()])

Environment

  • OpenDrift: 1.14.9
  • Python: 3.12

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