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
When using
reader_netCDF_CF_genericwith 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
np.searchsortedrequires the input array to be ascending.self.zis descending (ocean depth convention), then-self.zbecomes ascending and works correctly.self.zis ascending (altitude convention), then-self.zbecomes 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
Environment