section_extract#
- section_extract(lat_array, lon_array, depth_array, lat, lon, method='idw', power=2, max_iter=20, tol=1e-10, eps=1e-10)[source]#
Build a vertical section along given (lat, lon) points on a curvilinear grid, and return both the interpolated depth_array section and a callable to apply the same section to any 3-D scalar field on the same grid.
Algorithm: The data will be imported horizontally, layer by layer, and then all layers will be concatenated to form a section. First, the section will be divided into small parts. For example: lat = 10, lon = [10, 10.1, 10.2, …, 11]. In this example, we will have about 10 points. For each point, we find the 4 surrounding grid corners and construct a distance-weighted function. Using this weighted function, we interpolate the value at that point. Repeat this process for all points in layer 1. Repeat for all layers. Since the locations of the points do not change across layers, the weighted function only needs to be calculated once. For each point, we must also apply the same procedure to interpolate the depth_array (not just the data). Because the depth_array varies across points and layers, we interpolate the depth_array grid to obtain the depth_array at each point along the section. Finally, we obtain both the data section and the depth_array section, which can then be plotted as standard 2D data.
- Parameters:
lat_array ((ny, nx) array_like) – Grid node latitudes.
lon_array ((ny, nx) array_like) – Grid node longitudes.
depth_array ((nz, ny, nx) array_like) – depth_array values for each model layer on the grid.
lat ((M,) array_like) – Latitudes of section points in order along the section.
lon ((M,) array_like) – Longitudes of section points in order along the section.
method ({"idw", "bilinear"}, optional) –
Interpolation method for the horizontal step:
”idw”: inverse-distance weighting using the 4 surrounding corners.
”bilinear”: true bilinear interpolation inside the local cell.
Default is “idw”.
power (float, optional) – Power for IDW distances. Ignored for “bilinear”. Default 2.
max_iter (int, optional) – Maximum Newton iterations for solving bilinear coordinates (s, t).
tol (float, optional) – Convergence tolerance on position residual for bilinear solve.
eps (float, optional) – Small number to avoid division by zero.
- Returns:
depth_array_section ((nz, M) ndarray) – Interpolated depth_array along the section.
apply_to_data (callable) – Function apply_to_data(data3d) that returns a data section with shape (nz, M) for any scalar field data3d of shape (nz, ny, nx).
Notes
Precomputes the surrounding cell and interpolation weights once.
For “bilinear”, it solves for local cell coordinates (s, t) so that P(s, t) matches the query point inside that cell. Falls back to IDW if the solve fails for a point.
depth_array is interpolated with the same precomputed weights.
Currently, we only support scalar fields (e.g., temperature, salinity), not vector fields.