osmose.forcing.grid

Grid geometry helpers for CMEMS->OSMOSE forcing conversion.

Pure: numpy/xarray + osmose.maps.builder.GridSpec only. No CMEMS/MCP deps, so this module (and its tests) run in the clean CI venv.

regrid/resample use O(nlat*nlon) Python loops (verbatim from the MCP source), intended for OSMOSE-scale config grids (~1e3-1e4 cells, coarse by construction). A much finer grid would want a vectorized scipy.spatial.cKDTree / np.searchsorted.

Functions

apply_land_mask(groups, ocean_mask)

Set land cells to NaN in every (time, lat, lon) array, in place.

cell_volume_m3(grid, depth_m)

Approximate cell volume (m^3) using the grid's mid-latitude cos factor.

get_coords(ds)

Extract lat/lon arrays, tolerating 'latitude'/'lat' and 'longitude'/'lon'.

get_var(ds, name)

Variable as 3D (time, lat, lon), NaN->0; None if absent.

load_ocean_mask(grid_file)

Load a bool (nlat, nlon) ocean mask (True=ocean) from a grid NetCDF, or None.

regrid(data_3d, src_lat, src_lon, grid)

Nearest-neighbor regrid (time, src_lat, src_lon) -> (time, nlat, nlon).

resample_to_24(data)

Linear-interpolate (time, lat, lon) to 24 biweekly steps; identity if already 24.

target_coords(grid)

Cell-center (lat[nlat], lon[nlon]); latitude descending (north->south).