more general resample utility
As discussed with @andersy005 and @bonnland, it would be nice to have a more general resample capability that enable fluid translation between data at different temporal intervals.
Here's two functions that, if generalized, might be helpful.
The first by @klindsay28 finds the overlap between any two intervals on a number line.
def interval_overlap(interval_1, interval_2):
"""Compute the overlap between two intervals."""
overlap = (np.min([np.max(interval_1), np.max(interval_2)]) -
np.max([np.min(interval_1), np.min(interval_2)]))
return max([0.0, overlap])
The second computes averaging weights for a single coord_interval, given coord_bounds.
def compute_vertical_weights(coord_interval, coord_bounds):
"""Compute averaging weights for remapping a peicewise-constant field on a
1D coordinate to a single interval.
Example:
[1]: weights = compute_vertical_weights(coord_interval=np.array([0, 20]),
coord_bounds=np.array([[0, 10],
[10, 20],
[20, 30]]))
[2]: weights
array([0.5, 0.5, 0. ])
Parameters
---------
coord_interval : numpy.array
A two-element vector with the coordinate values over which to compute weights.
For example
coord_bounds : numpy.array
A N x 2 element vector with the coordinate bounds.
"""
weights = np.zeros(coord_bounds.shape[0])
for i in range(coord_bounds.shape[0]):
weights[i] = interval_overlap(coord_interval, coord_bounds[i, :])
return weights / weights.sum()
A more general resample functionality would have coord_bounds = time_bound_in and coord_interval = time_bound_out[i, :] for every time-level in the destination time coordinate.
We need a method to generate the new time-coordinate.
@matt-long seems this is now being addressed with AxisUtilities