ocean-eddy-cpt / gcm-filters

Diffusion-based Spatial Filtering of Gridded Data
https://gcm-filters.readthedocs.io/
Other
37 stars 24 forks source link

Helper method for applying a filter to an xarray Dataset #89

Closed asross closed 3 years ago

asross commented 3 years ago

I've found myself writing a wrapper method around Filter#apply that works for both DataArrays and Datasets (applying to all variables that have the given dims):

def apply(filter, ds, dims=['y','x']): 
    if isinstance(ds, xr.Dataset):
        filtered = ds.copy()
        for k, v in ds.variables.items():
            if all(d in v.dims for d in dims):
                filtered[k] = filter.apply(ds[k], dims=dims)
        return filtered
    else:
        return filter.apply(ds, dims=dims)

Would it make sense to make a PR that wraps the current definition of Filter#apply with a helper of this kind?

rabernat commented 3 years ago

Yes, I think this would be a very useful contribution. Thanks a lot for your offer!

Please make sure to include tests for any new features you propose in your PR.

asross commented 3 years ago

Awesome! I created a PR :)