rex.utilities.regridder.Regridder
- class Regridder(source_meta: DataFrame, target_meta: DataFrame, k_neighbors: int | None = 4, n_chunks: int | None = 100, max_workers: int | None = None, min_distance: float | None = 1e-12, leaf_size: int | None = 4)[source]
Bases:
_InterpolationMixin
Interpolate from one grid to another using inverse weighted distances.
This class builds ball tree and runs all queries to create full arrays of indices and distances for neighbor points. It computes an array of weights used to interpolate from the old grid to the new grid.
- Parameters:
source_meta (
pandas.DataFrame
) – Set of coordinates for source grid. Must contain “latitude” and “longitude” columns representing the coordinates (in degrees).target_meta (
pandas.DataFrame
) – Set of coordinates for target grid. Must contain “latitude” and “longitude” columns representing the coordinates (in degrees).k_neighbors (int, optional) – Number of nearest neighbors to use for interpolation. By default,
4
.n_chunks (int) – Number of spatial chunks to use for tree queries. The total number of points in the target_meta will be split into n_chunks, and the points in each chunk will be queried at the same time. By default,
100
.max_workers (int, optional) – Max number of workers to use for running all tree queries needed to build the full set of indices and distances for each target_meta coordinate. By default,
None
, which uses all available CPU cores.min_distance (float, optional) – Minimum distance to use for inverse-weighted distances calculation to avoid diving by 0. By default,
1e-12
.leaf_size (int, optional) – Leaf size for
BallTree
instance. By default,4
.
Methods
get_all_queries
([max_workers])Query ball tree for all coordinates in the target_meta and store results
get_spatial_chunk
(s_slice)Get list of coordinates in target_meta specified by the given spatial slice
Initialize arrays for tree queries and perform all queries
run
(source_meta, target_meta, source_data[, ...])Regrid data using inverse distance weighting.
save_query
(s_slice)Save tree query for coordinates specified by given spatial slice
Attributes
Get distances for all tree queries.
Get indices for all tree queries.
k_neighbors
leaf_size
max_workers
min_distance
n_chunks
Build ball tree from source_meta
Weights used for regridding.
source_meta
target_meta
- property distances
Get distances for all tree queries.
- property indices
Get indices for all tree queries.
- property tree
Build ball tree from source_meta
- get_all_queries(max_workers=None)[source]
Query ball tree for all coordinates in the target_meta and store results
- get_spatial_chunk(s_slice)[source]
Get list of coordinates in target_meta specified by the given spatial slice
- Parameters:
s_slice (slice) – Slice specifying which spatial indices in the target grid should be selected. This selects n_points from the target grid.
- Returns:
ndarray – Array of n_points in target_meta selected by s_slice.
- classmethod run(source_meta, target_meta, source_data, k_neighbors=4, n_chunks=100, max_workers=None, min_distance=1e-12, leaf_size=4)[source]
Regrid data using inverse distance weighting.
- Parameters:
source_meta (
pandas.DataFrame
) – Set of coordinates for source grid. Must contain “latitude” and “longitude” columns representing the coordinates (in degrees).target_meta (
pandas.DataFrame
) – Set of coordinates for target grid. Must contain “latitude” and “longitude” columns representing the coordinates (in degrees).source_data (ndarray) – Spatiotemporal data to regrid to target_meta coordinate grid. Data can be flattened in the spatial dimension to match the target_meta or be in a 2D spatial grid, e.g.: (spatial, temporal) or (spatial_1, spatial_2, temporal)
leaf_size (int, optional) – Leaf size for
BallTree
instance. By default,4
.k_neighbors (int, optional) – Number of nearest neighbors to use for interpolation. By default,
4
.n_chunks (int) – Number of spatial chunks to use for tree queries. The total number of points in the target_meta will be split into n_chunks, and the points in each chunk will be queried at the same time. By default,
100
.max_workers (int, optional) – Max number of workers to use for running all tree queries needed to build the full set of indices and distances for each target_meta coordinate. By default,
None
, which uses all available CPU cores.min_distance (float, optional) – Minimum distance to use for inverse-weighted distances calculation to avoid diving by 0. By default,
1e-12
.
- __call__(data)
Regrid given spatiotemporal data over entire grid.
- Parameters:
data (
numpy.ndarray
|dask.core.array.Array
) – Spatiotemporal data to regrid to target_meta. Data can be flattened in the spatial dimension to match the target_meta or be in a 2D spatial grid, e.g.: (spatial, temporal) or (spatial_1, spatial_2, temporal)- Returns:
out (
numpy.ndarray
|dask.core.array.Array
) – Flattened regridded spatiotemporal data (spatial, temporal)
- property weights
Weights used for regridding.
- Type:
ndarray