gpt4 book ai didi

python - xarray : compute daily anomalies from monthly resampled average (not the climatology)

转载 作者:太空宇宙 更新时间:2023-11-04 00:19:46 24 4
gpt4 key购买 nike

xarray 的 documentation解释了如何计算月度气候学的异常。在这里,我试图做一些稍微不同的事情:我想根据每日时间序列计算本月平均值的每日异常值(而不是来自月度气候学)。

我设法使用 groupby 和手动创建的月戳(下面的代码)来做到这一点。有没有更好、更简单的方法来获得相同的结果?

import xarray as xr
import numpy as np
import pandas as pd

# Create a data array
t = pd.date_range('2001', '2003', freq='D')
da = xr.DataArray(np.arange(len(t)), coords={'time':t}, dims='time')

# Monthly time stamp for groupby
da.coords['stamp'] = ('time', [str(y) + '-' + str(m) for (y, m) in
zip(da['time.year'].values,
da['time.month'].values)])

# Anomaly
da_ano = da.groupby('stamp') - da.groupby('stamp').mean()

da_ano.plot();

plot output

最佳答案

你可以显式地 resample月时间序列意味着变成日时间序列。示例:

monthly = da.resample(time='1MS').mean()
upsampled_monthly = monthly.resample(time='1D').ffill()
anomalies = da - upsampled_monthly

关于python - xarray : compute daily anomalies from monthly resampled average (not the climatology),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49600180/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com