slim4.pre
Pre
Classes
- class slim4.pre.Csr
CSR structure for interpolation from structured grid to unstructured points.
Properties
row_start: NDArray[np.int32] — Row start indices (size n_rows + 1)columns: NDArray[np.int32] — Column indices of non-zero entries (size n_entries) (values between 0 and n)weights: NDArray[np.float64] — Interpolation weights for each entry (size n_entries)indices: NDArray[np.int32] — The corresponding (lon_idx, lat_idx) for entry referenced by cols (size n x 2)
- class slim4.pre.Struct
Structured 2D lon/lat or 3D lon/lat/depth field collection with interpoaltion and caching helpers.
Constructor
- __init__(lon, lat, data_abins, time=None, depth=None, npass=20)
Create a Struct instance
Parameters
lon: NDArray[np.float64] — Longitude coordinates of the structured gridlat: NDArray[np.float64] — Latitude coordinates of the structured griddata_abins: List[Tuple[str, List[Tuple[str, int]]]] — (variable name, list of (abin file paths, number of lines to skip in each file)) for the data variables on the structured grid. The abin metadata should contain a dimensions key with values like [time, lon, lat], [lat, depth, lon], etc. with optional time as first dimension and depth in any order among lon and lat.time: NDArray[np.float64] | None — time coordinate associated with time-dependent variables. If not provided, the data variables are assumed to be time-independent.depth: NDArray[np.float64] | None — depth coordinate. Must be provided if and only if the data variables contain a depth dimension.npass: int — Number of passes used when filling masked values. Higher values may lead to better filling but longer computation time.
Methods
- lon()
Returns
lon: NDArray[np.float64] — Reinterpolated longitude coordinates when available, otherwise the original grid longitudes
- lat()
Returns
lat: NDArray[np.float64] — Reinterpolated latitude coordinates when available, otherwise the original grid latitudes
- depth()
Returns
depth: NDArray[np.float64] — Reinterpolated depth coordinates when available, otherwise the original depth coordinates, or None if no depth dimension
- time()
Returns
time: NDArray[np.float64] — Time coordinates, or None if no time dimension.
- get_variable(varname, itime=-1)
Load a variable, optionally at one time index.
Parameters
varname: str — Variable name.itime: int | None — Time index for time-dependent variables.Returns
value: NDArray[np.float64] — Variable values as a 2D scalar array.
- eval_at_points(varname, lonp, latp, t=0, extrapolate=False)
Evaluate a variable at given spatial (and optional temporal) points.
Parameters
varname: str — Variable name to evaluate.lonp: NDArray[np.float64] — Longitudes of the query points.latp: NDArray[np.float64] — Latitudes of the query points.t: float | None — Evaluation time for time-dependent variables, expressed in seconds since 1970-01-01 00:00:00.extrapolate: bool | None — Whether to allow extrapolation outside the data range.Returns
value: NDArray[np.float64] — Interpolated values at the requested points as a 2D scalar array.
- set_reinterp_coordinates(lon=None, lat=None, depth=None)
Set a target grid used to reinterpolate loaded variables. Interpolation is separable: 1D weights are computed per dimension and applied as a tensor product.
Parameters
lon: NDArray[np.float64] | None — Target longitude coordinates. Defaults to the current active longitudes.lat: NDArray[np.float64] | None — Target latitude coordinates. Defaults to the current active latitudes.depth: NDArray[np.float64] | None — Target depth coordinates. Only valid when the struct has a depth dimension. Defaults to the current active depths.
- compute_csr_cg_2d(mesh, boundaries=[])
Compute the CSR representation from the mesh cg nodes to the grid, optionally restricted to a subset of boundary nodes. the computed CSR is stored internally
Parameters
mesh: slim2d.Mesh — The mesh containing the cg nodes and the boundary information.boundaries: List[str] — List of boundary tags to include in the CSR. If empty, all nodes will be included.Returns
csr: Csr — object containing the row start, columns, weights, and data indices
- compute_csr_cg_3d(mesh, z0, boundaries=[])
Compute the CSR representation from the mesh cg nodes to the grid, optionally restricted to a subset of boundary nodes. the computed CSR is stored internally
Parameters
mesh: slim3d.Mesh — The mesh containing the cg nodes and the boundary information.z0: NDArray[np.float64] — The z coordinates (dg)boundaries: List[str] — List of boundary tags to include in the CSR. If empty, all nodes will be included.Returns
csr: Csr — object containing the row start, columns, weights, and data indices
- get_lonlat_grid(csr)
Get the longitude and latitude coordinates of the points used in the csr constructed on the grid
Parameters
csr: Csr — The CSR object for which to retrieve the grid points. The CSR should have been computedReturns
points: NDArray[np.float64] — A 2D scalar array of shape (2, n) where the first row contains the longitude coordinates and the second row contains the latitude coordinates of the grid points.
- get_data(csr, t, fields=[])
Extract data formated for slim4.data.write_file_append (i.e. the values at the csr.index positions).
Parameters
csr: Csr — The CSR object defining the mapping from the structured grid to the mesht: float — Evaluation time for time-dependent variables, expressed in seconds since 1970-01-01 00:00:00.fields: List[str] — List of variable names to retrieve (all if empty).Returns
data: List[Tuple[str, NDArray[np.float64]]] — A vector of pairs, where each pair consists of a variable name and its corresponding 2D scalar array of values at the requested time.
- get_time(tmin, tmax)
Get the subset of the time axis within inclusive bounds.
Parameters
tmin: float — Lower time bound, expressed in seconds since 1970-01-01 00:00:00.tmax: float — Upper time bound, expressed in seconds since 1970-01-01 00:00:00.Returns
time_subset: NDArray[np.float64] — Time values intersecting the inclusive interval [tmin, tmax].
- get_space_time_average(fields=[])
Compute the space-time average of the specified variables.
Parameters
fields: List[str] — List of variable names to include in the average (all if empty).Returns
averages: List[Tuple[str, float]] — A vector of pairs, where each pair consists of a variable name and its corresponding space-time average value.
- static merge_structs(structs)
Merge variables from multiple compatible Struct objects.
Parameters
structs: List[Struct] — Structured datasets to merge.Returns
merged_struct: Struct — Dataset containing the union of all variables.
- static reinterp_at_finest_grid(structs, bounds=[])
Reinterpolate several datasets onto the finest available grid.
Parameters
structs: List[Struct] — Datasets to reinterpolate in place.bounds: List[float] — Spatial subset given as (minlon, maxlon, minlat, maxlat).Returns
fine_structs: List[Struct] — the modified input structsfine_time: NDArray[np.float64] — the time coordinate of the modified structs
- static from_files(filenames, varnames, npass=20)
Build a Struct by concatenating multiple files in time.
Parameters
filenames: List[str] — Paths to the NetCDF or abin files to process.varnames: List[str] — Variable names to load into the resulting object.npass: int — Number of passes used for filling masked values, by default 20Returns
struct_obj: Struct — Structured dataset whose variables are virtually stacked along time.
Functions
- slim4.pre.fill_mask_2d(array, npass)
Fill masked values in a 2D (with or without time)
Parameters
array: NDArray[np.float64] — Input array 2Dnpass: int — Number of fill passes.Returns
Array: NDArray[np.float64] — with masked values filled.
- slim4.pre.fill_mask_3d(array, npass)
Fill masked values in a 3D array (with or without time).
Parameters
array: NDArray[np.float64] — Input array.npass: int — Number of fill passes.Returns
Array: NDArray[np.float64] — with masked values filled.
- slim4.pre.clip_inclusive(dim, vmin, vmax)
Clip a monotonic array between bounds (inclusive).
Parameters
dim: NDArray[np.float64] — Monotonic coordinate array.vmin: float — Minimum value.vmax: float — Maximum value.Returns
clipped: NDArray[np.float64] — Subset of dim within [vmin, vmax].