本文整理汇总了Python中xarray.Dataset.expand_dims方法的典型用法代码示例。如果您正苦于以下问题:Python Dataset.expand_dims方法的具体用法?Python Dataset.expand_dims怎么用?Python Dataset.expand_dims使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类xarray.Dataset
的用法示例。
在下文中一共展示了Dataset.expand_dims方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: normalize_missing_time
# 需要导入模块: from xarray import Dataset [as 别名]
# 或者: from xarray.Dataset import expand_dims [as 别名]
def normalize_missing_time(ds: xr.Dataset) -> xr.Dataset:
"""
Add a time coordinate variable and their associated bounds coordinate variables
if temporal CF attributes ``time_coverage_start`` and ``time_coverage_end``
are given but the time dimension is missing.
The new time coordinate variable will be named ``time`` with dimension ['time'] and shape [1].
The time bounds coordinates variable will be named ``time_bnds`` with dimensions ['time', 'bnds'] and shape [1,2].
Both are of data type ``datetime64``.
:param ds: Dataset to adjust
:return: Adjusted dataset
"""
time_coverage_start = ds.attrs.get('time_coverage_start')
if time_coverage_start is not None:
# noinspection PyBroadException
try:
time_coverage_start = pd.to_datetime(time_coverage_start)
except BaseException:
pass
time_coverage_end = ds.attrs.get('time_coverage_end')
if time_coverage_end is not None:
# noinspection PyBroadException
try:
time_coverage_end = pd.to_datetime(time_coverage_end)
except BaseException:
pass
if not time_coverage_start and not time_coverage_end:
# Can't do anything
return ds
if 'time' in ds:
time = ds.time
if not time.dims:
ds = ds.drop('time')
elif len(time.dims) == 1:
time_dim_name = time.dims[0]
is_time_used_as_dim = any([(time_dim_name in ds[var_name].dims) for var_name in ds.data_vars])
if is_time_used_as_dim:
# It seems we already have valid time coordinates
return ds
time_bnds_var_name = time.attrs.get('bounds')
if time_bnds_var_name in ds:
ds = ds.drop(time_bnds_var_name)
ds = ds.drop('time')
ds = ds.drop([var_name for var_name in ds.coords if time_dim_name in ds.coords[var_name].dims])
if time_coverage_start or time_coverage_end:
# noinspection PyBroadException
try:
ds = ds.expand_dims('time')
except BaseException as e:
warnings.warn(f'failed to add time dimension: {e}')
if time_coverage_start and time_coverage_end:
time_value = time_coverage_start + 0.5 * (time_coverage_end - time_coverage_start)
else:
time_value = time_coverage_start or time_coverage_end
new_coord_vars = dict(time=xr.DataArray([time_value], dims=['time']))
if time_coverage_start and time_coverage_end:
has_time_bnds = 'time_bnds' in ds.coords or 'time_bnds' in ds
if not has_time_bnds:
new_coord_vars.update(time_bnds=xr.DataArray([[time_coverage_start, time_coverage_end]],
dims=['time', 'bnds']))
ds = ds.assign_coords(**new_coord_vars)
ds.coords['time'].attrs['long_name'] = 'time'
ds.coords['time'].attrs['standard_name'] = 'time'
ds.coords['time'].encoding['units'] = 'days since 1970-01-01'
if 'time_bnds' in ds.coords:
ds.coords['time'].attrs['bounds'] = 'time_bnds'
ds.coords['time_bnds'].attrs['long_name'] = 'time'
ds.coords['time_bnds'].attrs['standard_name'] = 'time'
ds.coords['time_bnds'].encoding['units'] = 'days since 1970-01-01'
return ds