Skip to content
This repository has been archived by the owner on Apr 30, 2021. It is now read-only.

more general resample utility #142

Open
matt-long opened this issue Jun 21, 2019 · 1 comment
Open

more general resample utility #142

matt-long opened this issue Jun 21, 2019 · 1 comment
Labels
2 - normal Normal priority enhancement New feature or request

Comments

@matt-long
Copy link
Contributor

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.

@maboualidev
Copy link

@matt-long seems this is now being addressed with AxisUtilities

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
2 - normal Normal priority enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants