sea_level_response.selrem.selrem_helpers
SELREM helper module.
Functions
|
Bin loads into coarse lat/lon cells and return one point load per occupied cell (at the cell centre). |
|
Build a regular global grid: lon = -180 to 180 (periodic), lat = 90 to -90. |
Compute the mean mass balance and, if present, its error over a time range, collapsing the time dimension. |
|
|
Reproject an ocean mask onto a new longitude/latitude grid using interpolation. |
|
Linearly interpolate a (lon, lat) field to the target grid. |
Load the ocean mask from a NetCDF file. |
|
Load SELREM parameters from a JSON file. |
|
Preprocess annual SLR data for a specific location, returning plot-ready time series. |
|
|
Load, slice, and prepare the VMB dataset for SELREM computation. |
|
Wrap any longitude (e.g. 0-360) to [-180, 180). |
- bin_loads_to_points(lat_deg: ndarray, lon_deg: ndarray, mass_gt: ndarray, masserr_gt: ndarray, bin_res_deg: float = 1.0) tuple[ndarray, ndarray, ndarray, ndarray]
Bin loads into coarse lat/lon cells and return one point load per occupied cell (at the cell centre).
Masses are summed. Errors are combined as sqrt(sum(err^2)), i.e. assuming independent errors.
- Parameters:
lat_deg (np.ndarray) – Latitude of the loads in degrees (any shape; they are flattened).
lon_deg (np.ndarray) – Longitude of the loads in degrees (any shape; they are flattened).
mass_gt (np.ndarray) – Mass in Gt, same shape as lat/lon.
masserr_gt (np.ndarray) – Mass error in Gt, same shape as lat/lon.
bin_res_deg (float) – Resolution at which mass-balance loads are binned to point loads.
- Returns:
lat_centres, lon_centres (degrees), mass (Gt), mass error (Gt) of the occupied bins.
- Return type:
tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]
- build_lonlat(res_deg: float) tuple[ndarray, ndarray, ndarray, ndarray]
Build a regular global grid: lon = -180 to 180 (periodic), lat = 90 to -90.
- Parameters:
res_deg (float) – Grid resolution in decimal degrees
- Returns:
(Longitude, Latitude, 2D Longitude, 2D Latitude)
- Return type:
tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]
- compute_mean_mass_balance_over_time_window(ds: Dataset, start_time: datetime | None = None, end_time: datetime | None = None) Dataset
Compute the mean mass balance and, if present, its error over a time range, collapsing the time dimension.
- Parameters:
ds (xr.Dataset) – Input mass balance dataset with a time dimension. The mass-balance error variable is optional.
start_time (datetime | None) – Start time for slicing, by default None. If both start_time and end_time are provided, the dataset is sliced accordingly before computing the mean. Otherwise, the entire time range is used.
end_time (datetime | None) – End time for slicing, by default None. If both start_time and end_time are provided, the dataset is sliced accordingly before computing the mean. Otherwise, the entire time range is used.
- Returns:
Dataset with the same spatial dimensions (‘point’, ‘x’, ‘y’), where the ‘time’ dimension has been removed and each variable represents the mean value across the specified years. The error variable is only included if it was present in the input.
- Return type:
xr.Dataset
- interp_ocean_mask_to(target_lons: ndarray, target_lats: ndarray, ocean_mask: ndarray) ndarray
Reproject an ocean mask onto a new longitude/latitude grid using interpolation.
- Parameters:
target_lons (np.ndarray) – Target longitudes for the new grid
target_lats (np.ndarray) – Target latitudes for the new grid
ocean_mask (np.ndarray) – Spatial grid masking ocean cells
- Returns:
The reprojected ocean mask on the target grid.
- Return type:
np.ndarray
- interp_to_grid(field: ndarray, lons_src: ndarray, lats_src: ndarray, lon2d_t: ndarray, lat2d_t: ndarray) ndarray
Linearly interpolate a (lon, lat) field to the target grid.
Points outside the source grid become NaN.
- Parameters:
field (np.ndarray) – 2D field on the source grid, shape (nlon, nlat)
lons_src (np.ndarray) – Longitudes of the source grid in decimal degrees
lats_src (np.ndarray) – Latitudes of the source grid in decimal degrees
lon2d_t (np.ndarray) – 2D longitudes of the target grid in decimal degrees
lat2d_t (np.ndarray) – 2D latitudes of the target grid in decimal degrees
- Returns:
The field interpolated onto the target grid, same shape as
lon2d_t.- Return type:
np.ndarray
- load_ocean_mask() tuple[ndarray, ndarray, ndarray]
Load the ocean mask from a NetCDF file.
- Returns:
A tuple containing the ocean mask array, longitude array, and latitude array.
- Return type:
tuple[np.ndarray, np.ndarray, np.ndarray]
- load_selrem_parameters() tuple[ndarray, ndarray, ndarray, ndarray]
Load SELREM parameters from a JSON file.
- Returns:
gdot_fit, udot_fit, gdot_unc, udot_unc arrays.
- Return type:
tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]
- preprocess_location_timeseries(annual_slr_ds: Dataset, lat0: float, lon0: float) dict
Preprocess annual SLR data for a specific location, returning plot-ready time series.
This function extracts time series data at the nearest grid point to the specified coordinates, computes cumulative sums, calculates linear trends, and returns data ready for plotting.
- Parameters:
annual_slr_ds (xr.Dataset) – Annual SLR dataset containing ‘ndot’, ‘udot’, ‘sdot’ and their uncertainties.
lat0 (float) – Target latitude in decimal degrees (-90 to 90).
lon0 (float) – Target longitude in decimal degrees (-180 to 180).
- Returns:
Dictionary containing: - ‘years’: List of year labels (integers) - ‘sdot_cumulative’: Cumulative relative SLR (mm) - ‘ndot_cumulative’: Cumulative absolute SLR (mm) - ‘udot_cumulative’: Cumulative vertical deformation (mm) - ‘sig_sdot_cumulative’: Cumulative uncertainty for sdot (mm) - ‘sig_ndot_cumulative’: Cumulative uncertainty for ndot (mm) - ‘sig_udot_cumulative’: Cumulative uncertainty for udot (mm) - ‘slope_sdot’: Linear trend for relative SLR (mm/yr) - ‘slope_ndot’: Linear trend for absolute SLR (mm/yr) - ‘slope_udot’: Linear trend for vertical deformation (mm/yr) - ‘actual_lat’: Actual latitude of nearest grid point - ‘actual_lon’: Actual longitude of nearest grid point
- Return type:
dict
- slice_and_prepare_dataset(vmb_dataset_path: Path | str, start_time: datetime, end_time: datetime) Dataset
Load, slice, and prepare the VMB dataset for SELREM computation.
- Parameters:
vmb_dataset_path (Path | str) – Path to the VMB GeoZarr dataset.
start_time (datetime) – Start time for slicing.
end_time (datetime) – End time for slicing.
- Returns:
Sliced and prepared VMB dataset.
- Return type:
xr.Dataset
- Raises:
ValueError – If the dataset does not contain a ‘time’ dimension or if the time range is invalid
- wrap180(lon_deg: ndarray | float) ndarray | float
Wrap any longitude (e.g. 0-360) to [-180, 180).
- Parameters:
lon_deg (np.ndarray | float) – Longitude in decimal degrees
- Returns:
Longitude in decimal degrees wrapped to [-180, 180)
- Return type:
np.ndarray | float