sup3r.utilities.regridder.WindRegridder
- class WindRegridder(source_meta, target_meta, cache_pattern=None, leaf_size=4, k_neighbors=4, n_chunks=100, max_distance=None, max_workers=None)[source]
Bases:
Regridder
Class to regrid windspeed and winddirection. Includes methods for converting windspeed and winddirection to U and V and inverting after interpolation
Get weights and indices used to map from source grid to target grid
- Parameters:
source_meta (pd.DataFrame) – Set of coordinates for source grid
target_meta (pd.DataFrame) – Set of coordinates for target grid
cache_pattern (str | None) – Pattern for cached indices and distances for ball tree. Will load these if provided. Should be of the form ‘./{array_name}.pkl’ where array_name will be replaced with either ‘indices’ or ‘distances’.
leaf_size (int, optional) – leaf size for BallTree
k_neighbors (int, optional) – number of nearest neighbors to use for interpolation
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.
max_distance (float | None) – Max distance to new grid points from original points before filling with nans.
max_workers (int | None) – Max number of workers to use for running all tree queries needed to building full set of indices and distances for each target_meta coordinate.
Methods
Cache indices and distances from ball tree query
get_all_queries
([max_workers])Query ball tree for all coordinates in the target_meta and store results
get_source_uv
(index_chunk, height, source_files)Get u/v wind components from windspeed and winddirection
get_source_values
(index_chunk, feature, ...)Get values to use for interpolation from h5 source files
get_spatial_chunk
(s_slice)Get list of coordinates in target_meta specified by the given spatial slice
Initialize arrays for tree queries and either load query cache or perform all queries
interpolate
(distance_chunk, values)Interpolate to new coordinates based on distances from those coordinates and the values of the points at those distances
invert_uv
(u, v)Get u/v wind components from windspeed and winddirection
Load cached indices and distances from ball tree query
query_tree
(s_slice)Get indices and distances for points specified by the given spatial slice
regrid_coordinates
(index_chunk, ...)Regrid wind fields at given height for the requested coordinate index
run
(source_meta, target_meta[, ...])Query tree for every point in target_meta to get full set of indices and distances for the neighboring points in the source_meta.
save_query
(s_slice)Save tree query for coordinates specified by given spatial slice
Attributes
MAX_DISTANCE
MIN_DISTANCE
Check if cache exists before building tree.
Mask for points too far from original grid
Get name of cache distances file
Get distances for all tree queries.
Get name of cache indices file
Get indices for all tree queries.
Build ball tree from source_meta
Get weights used for regridding
- classmethod get_source_values(index_chunk, feature, source_files)[source]
Get values to use for interpolation from h5 source files
- Parameters:
index_chunk (ndarray) – Chunk of the full array of indices where indices[i] gives the list of coordinate indices in the source data to be used for interpolation for the i-th coordinate in the target data. (temporal, n_points, k_neighbors)
feature (str) – Name of feature to interpolate
source_files (list) – List of paths to source files
- Returns:
ndarray – Array of values to use for interpolation with shape (temporal, n_points, k_neighbors)
- classmethod get_source_uv(index_chunk, height, source_files)[source]
Get u/v wind components from windspeed and winddirection
- Parameters:
index_chunk (ndarray) – Chunk of the full array of indices where indices[i] gives the list of coordinate indices in the source data to be used for interpolation for the i-th coordinate in the target data. (temporal, n_points, k_neighbors)
height (int) – Wind height level
source_files (list) – List of paths to h5 source files
- Returns:
u (ndarray) – Array of zonal wind values to use for interpolation with shape (temporal, n_points, k_neighbors)
v (ndarray) – Array of meridional wind values to use for interpolation with shape (temporal, n_points, k_neighbors)
- classmethod invert_uv(u, v)[source]
Get u/v wind components from windspeed and winddirection
- Parameters:
u (ndarray) – Array of interpolated zonal wind values with shape (temporal, n_points)
v (ndarray) – Array of interpolated meridional wind values with shape (temporal, n_points)
- Returns:
ws (ndarray) – Array of interpolated windspeed values with shape (temporal, n_points)
wd (ndarray) – Array of winddirection values with shape (temporal, n_points)
- classmethod regrid_coordinates(index_chunk, distance_chunk, height, source_files)[source]
Regrid wind fields at given height for the requested coordinate index
- Parameters:
index_chunk (ndarray) – Chunk of the full array of indices where indices[i] gives the list of coordinate indices in the source data to be used for interpolation for the i-th coordinate in the target data. (temporal, n_points, k_neighbors)
distance_chunk (ndarray) – Chunk of the full array of distances where distances[i] gives the list of distances to the source coordinates to be used for interpolation for the i-th coordinate in the target data. (temporal, n_points, k_neighbors)
height (int) – Wind height level
source_files (list) – List of paths to h5 source files
- Returns:
ws (ndarray) – Array of interpolated windspeed values with shape (temporal, n_points)
wd (ndarray) – Array of winddirection values with shape (temporal, n_points)
- __call__(data)
Regrid given spatiotemporal data over entire grid
- Parameters:
data (ndarray) – 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 (ndarray) – Flattened regridded spatiotemporal data (spatial, temporal)
- cache_all_queries()
Cache indices and distances from ball tree query
- property cache_exists
Check if cache exists before building tree.
- property dist_mask
Mask for points too far from original grid
- Returns:
mask (ndarray) – Bool array for points outside original grid extent
- property distance_file
Get name of cache distances file
- property distances
Get distances for all tree queries.
- get_all_queries(max_workers=None)
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
- 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.
- property index_file
Get name of cache indices file
- property indices
Get indices for all tree queries.
- init_queries()
Initialize arrays for tree queries and either load query cache or perform all queries
- classmethod interpolate(distance_chunk, values)
Interpolate to new coordinates based on distances from those coordinates and the values of the points at those distances
- Parameters:
distance_chunk (ndarray) – Chunk of the full array of distances where distances[i] gives the list of k_neighbors distances to the source coordinates to be used for interpolation for the i-th coordinate in the target data. (n_points, k_neighbors)
values (ndarray) – Array of values corresponding to the point distances with shape (temporal, n_points, k_neighbors)
- Returns:
ndarray – Time series of values at interpolated points with shape (temporal, n_points)
- load_cache()
Load cached indices and distances from ball tree query
- query_tree(s_slice)
Get indices and distances for points 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:
distances (ndarray) – Array of distances for neighboring points for each point selected by s_slice. (n_ponts, k_neighbors)
indices (ndarray) – Array of indices for neighboring points for each point selected by s_slice. (n_ponts, k_neighbors)
- classmethod run(source_meta, target_meta, cache_pattern=None, leaf_size=4, k_neighbors=4, n_chunks=100, max_workers=None)
Query tree for every point in target_meta to get full set of indices and distances for the neighboring points in the source_meta.
- Parameters:
source_meta (pd.DataFrame) – Set of coordinates for source grid
target_meta (pd.DataFrame) – Set of coordinates for target grid
cache_pattern (str | None) – Pattern for cached indices and distances for ball tree. Will load these if provided. Should be of the form ‘./{array_name}.pkl’ where array_name will be replaced with either ‘indices’ or ‘distances’.
leaf_size (int, optional) – leaf size for BallTree
k_neighbors (int, optional) – number of nearest neighbors to use for interpolation
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.
max_workers (int | None) – Max number of workers to use for running all tree queries needed to building full set of indices and distances for each target_meta coordinate.
- save_query(s_slice)
Save tree query for coordinates specified by given spatial slice
- property tree
Build ball tree from source_meta
- property weights
Get weights used for regridding