本文整理汇总了Python中xarray.Dataset.squeeze方法的典型用法代码示例。如果您正苦于以下问题:Python Dataset.squeeze方法的具体用法?Python Dataset.squeeze怎么用?Python Dataset.squeeze使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类xarray.Dataset
的用法示例。
在下文中一共展示了Dataset.squeeze方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: diff
# 需要导入模块: from xarray import Dataset [as 别名]
# 或者: from xarray.Dataset import squeeze [as 别名]
def diff(ds: xr.Dataset,
ds2: xr.Dataset,
monitor: Monitor = Monitor.NONE) -> xr.Dataset:
"""
Calculate the difference of two datasets (ds - ds2). This is done by
matching variable names in the two datasets against each other and taking
the difference of matching variables.
If lat/lon/time extents differ between the datasets, the default behavior
is to take the intersection of the datasets and run subtraction on that.
However, broadcasting is possible. E.g. ds(lat/lon/time) - ds(lat/lon) is
valid. In this case the subtrahend will be stretched to the size of
ds(lat/lon/time) so that it can be subtracted. This also works if the
subtrahend is a single time slice of arbitrary temporal position. In this
case, the time dimension will be squeezed out leaving a lat/lon dataset.
:param ds: The minuend dataset
:param ds2: The subtrahend dataset
:param monitor: a progress monitor.
:return: The difference dataset
"""
try:
# Times do not intersect
if 0 == len(ds.time - ds2.time) and \
len(ds.time) == len(ds2.time): # Times are the same length
# If the datasets don't intersect in time dimension, a naive difference
# would return empty data variables. Hence, the time coordinate has to
# be dropped beforehand
ds = ds.drop('time')
ds2 = ds2.drop('time')
return ds - ds2
except AttributeError:
# It is likely that the one operand is a lat/lon array that can be
# broadcast against the other operand
pass
try:
if 1 == len(ds2.time):
# The subtrahend is a single time-slice -> squeeze 'time' dimension to
# be able to broadcast is along minuend
ds2 = ds2.squeeze('time', drop=True)
except AttributeError:
# Doesn't have a time dimension already
pass
except TypeError as e:
if 'unsized object' in str(e):
# The 'time' variable is a scalar
pass
else:
raise TypeError(str(e))
with monitor.observing("Subtract datasets"):
diff = ds - ds2
return diff