Some other utility functions (s2stools.utils)
Functions
- s2stools.utils.add_years(dt64, years)
Add year to date.
- Parameters:
dt64 ([datetime64], datetime64) – date to change
years ([int], int) – how many years to add. If scalar, add same year to each date, if list then years must have same shape as dt64.
- Returns:
new date(s) with same shape as dt64
- s2stools.utils.groupby_quantiles(dataarray, groupby, q, q_dim, labels=None)
xr.core.dataset.Dataset.groupby_bins(…, bins=N) groups the data into N groups which span same interval sizes. This method groups the data into len(q) bins, which have equal number of subdatasets in the respective groups.
- Parameters:
xr.DataArray) (dataarray (xr.Dataset |)
str) (groupby (xr.Dataset | xr.DataArray |)
(list) (q)
(str) (q_dim)
([str]) (labels)
- Returns:
groupby_object
- Return type:
xarray.core.groupby.DatasetGroupBy | xarray.core.groupby.DataArrayGroupBy
- s2stools.utils.lat_weighted_spatial_mean(dataarray, lon_name='longitude', lat_name='latitude')
Spatial average over longitude, latitude. Gridpoints are weighted by cos(lat) to account for different areas on Gaussian grid on a sphere.
- Parameters:
(xr.DataArray) (dataarray)
(str) (lat_name)
(str)
- Return type:
weighted_dataarray (xr.DataArray)
- s2stools.utils.month_int_to_abbr(months)
convert e.g. [12, 1, 2] to [“Dec”, “Jan”, “Feb”] :param months: one month or list of months as integers
- Returns:
list of str
- s2stools.utils.to_timedelta64(a, assume='D')
Convert to numpy timedelta64
- Parameters:
a (int or np.timedelta64) – timedelta
() (assume) – timedelta64 format that is assumed if a is of type int.
- Returns:
np.timedelta64
- s2stools.utils.unwrap_time(xr_data)
Transform data back from (“season”, “timestepofseason”) to a linear time axis.
- Parameters:
xr.DataArray) (xr_data (xr.Dataset |)
- Returns:
xr_data
- Return type:
xr.Dataset | xr.DataArray
See also
- s2stools.utils.wrap_time(xr_data, season_start_month, change_dims=True)
Transform data from a linear time axis to a reshaped pair of
("season", "timestepofseason"). Can for example be used to go from time to DJF and day since Dec 1.- Parameters:
xr.DataArray) (xr_data (xr.Dataset |)
(int) (season_start_month)
(bool) (change_dims) – If False, the time dimension, and
seasonandtimestepofseasonare just added as new coordinates.
- Returns:
xr_data
- Return type:
xr.Dataset | xr.DataArray
Examples
>>> times = pd.date_range("2000-11-01", "2001-12-01", freq="D") >>> nt = len(times) >>> ds = xr.DataArray( >>> np.random.randint(low=0, high=100, size=(nt, 10)), >>> coords=dict(time=times, x=np.arange(10)), >>> dims=["time", "x"], >>> ) >>> print(ds) <xarray.DataArray (time: 396, x: 10)> array([[27, 34, 84, ..., 68, 16, 61], [12, 98, 94, ..., 25, 38, 87], [ 4, 27, 91, ..., 78, 48, 55], ..., [ 1, 75, 64, ..., 21, 21, 7], [18, 96, 9, ..., 60, 85, 2], [31, 28, 36, ..., 64, 48, 97]]) Coordinates: * time (time) datetime64[ns] 2000-11-01 2000-11-02 ... 2001-12-01 * x (x) int64 0 1 2 3 4 5 6 7 8 9 >>> ds_transformed = wrap_time(ds, season_start_month=11, change_dims=True) >>> print(ds_transformed) <xarray.DataArray (season: 2, x: 10, timestepofseason: 365)> array([[[46., 3., 62., ..., 63., 15., 69.], [22., 23., 89., ..., 40., 47., 63.], .... [91., 7., 78., ..., nan, nan, nan], [87., 30., 88., ..., nan, nan, nan]]]) Coordinates: * x (x) int64 0 1 2 3 4 5 6 7 8 9 * timestepofseason (timestepofseason) int64 1 2 3 4 5 ... 362 363 364 365 time (season, timestepofseason) datetime64[ns] 2000-11-01 ... * season (season) int64 2000 2001 season_start_month int64 11