pywatershed.utils.netcdf_utils.subset_xr

Contents

pywatershed.utils.netcdf_utils.subset_xr#

pywatershed.utils.netcdf_utils.subset_xr(ds, start_time=None, end_time=None, coord_dim_name=None, coord_dim_values_keep=None)[source]#

Subset an xarray Dataset or DataArray on to coord or dim values.

Parameters:
  • start_time (datetime64) – Optional start time if a “time” coord is present.

  • end_time (datetime64) – Optional end time if a “time” coord is present.

  • coord_dim_name (str) – Optional coord or dimension name to subset on.

  • coord_dim_values_keep (array) – Optional values on the coord or dimension to retain in teh subset.

Return type:

Union[Dataset, DataArray]

This currently works for 1-D coordinates, more dimensions not tested. To work with files rather than memory see pywatershed.utils.netcdf_utils.subset_netcdf_file(). Note: There seem to be several edge cases lurking around here with zero length dimensions and xarray’s broadcasting rules. This function is a convenience function because xarray’s functionality is not ideal for our use cases and is confusing with pitfalls. See pydata/xarray#8796 for additional discussion.