Pass arguments along with preprocess=...
on open_mfdataset
#6820
-
I'm looking to pass function arguments (args or kwargs) into the E.g., # Opening arbitrary set of small NetCDFs for example
ds = xr.open_mfdataset(
"file_*.nc",
concat_dim="time",
preprocess=_slice_models
)
# Arbitrary slicing func. Could use a `NamedTuple` or do any
# other simple, but variable operation.
def _slice_models(ds, x0, x1, y0, y1):
ds = ds.sel(lon=slice(x0, x1), lat=slice(y0, y1))
return ds This would be ideal in the case of reusable code that is loading in large, global high-res datasets and slicing over various sub-domains. Ideally this isn't done with global variables, since the |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
I think I've done this using |
Beta Was this translation helpful? Give feedback.
I think I've done this using
functools.partial
before. Using that you can bind any args or kwargs into the function before passing it in toopen_mfdataset
, but still have it wait upon theds
argument. This pattern might be a good addition to a "recipes" page or similar (or just an example in theopen_mfdataset
docstring).