Skip to content

Commit

Permalink
QA tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesVarndell committed Sep 24, 2024
1 parent e02a9ab commit 2b59d74
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions src/earthkit/plots/geo/grids.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,21 @@ def is_structured(lat, lon, tol=1e-5):

# If the product of unique lat/lon values doesn't match total points, it's unstructured
return False


def interpolate_unstructured(x, y, z, resolution=1000, method="linear"):
"""
Interpolates unstructured data to a structured grid, handling NaNs in z-values
and preventing interpolation across large gaps.
Parameters:
- x: 1D array of x-coordinates.
- y: 1D array of y-coordinates.
- z: 1D array of z values.
- resolution: The number of points along each axis for the structured grid.
- method: Interpolation method ('linear', 'nearest', 'cubic').
- gap_threshold: The distance threshold beyond which interpolation is not performed (set to NaN).
Returns:
- grid_x: 2D grid of x-coordinates.
- grid_y: 2D grid of y-coordinates.
Expand All @@ -75,11 +75,18 @@ def interpolate_unstructured(x, y, z, resolution=1000, method="linear"):
x_filtered = x[mask]
y_filtered = y[mask]
z_filtered = z[mask]

# Create a structured grid
grid_x, grid_y = np.mgrid[x.min():x.max():resolution*1j, y.min():y.max():resolution*1j]

grid_x, grid_y = np.mgrid[
x.min() : x.max() : resolution * 1j, y.min() : y.max() : resolution * 1j
]

# Interpolate the filtered data onto the structured grid
grid_z = griddata(np.column_stack((x_filtered, y_filtered)), z_filtered, (grid_x, grid_y), method=method)

grid_z = griddata(
np.column_stack((x_filtered, y_filtered)),
z_filtered,
(grid_x, grid_y),
method=method,
)

return grid_x, grid_y, grid_z

0 comments on commit 2b59d74

Please sign in to comment.