如何计算4个数据集的一个平均数据集[Python, xarray]



我有4个[GFS]温度数据集:它们在空间分辨率上都是相同的,唯一的区别是时间戳-它们在一天内为00 UTC, 06 UTC, 12 UTC, 18 UTC。

我需要计算平均日温度数据集。是否有一种方法来做工具性,但不像,手动弹出值从相应的节点,计算平均值,并插入到一个数据集?

import xarray as xr
dst00 = xr.open_dataset('gfs.t00z.pgrb2.1p00.f000', engine='cfgrib')
dst06 = xr.open_dataset('gfs.t06z.pgrb2.1p00.f000', engine='cfgrib')
dst12 = xr.open_dataset('gfs.t12z.pgrb2.1p00.f000', engine='cfgrib')
dst18 = xr.open_dataset('gfs.t18z.pgrb2.1p00.f000', engine='cfgrib')

示例数据集的直接下载链接:

00 utc

您可以使用xr.concat:

dst = xr.concat([dst00, dst06], dim='time').mean('time')
print(dst)
# Output
<xarray.Dataset>
Dimensions:            (latitude: 31, longitude: 101)
Coordinates:
step               timedelta64[ns] 00:00:00
heightAboveGround  float64 2.0
* latitude           (latitude) float64 60.0 61.0 62.0 63.0 ... 88.0 89.0 90.0
* longitude          (longitude) float64 0.0 1.0 2.0 3.0 ... 98.0 99.0 100.0
Data variables:
t2m                (latitude, longitude) float32 279.8 279.5 ... 243.4 243.4

检查:

import pandas as pd
out = pd.concat([d.to_dataframe()['t2m'] for d in [dst00, dst06, dst]], axis=1, 
keys=['dst00', 'dst06', 'mean'])
print(out)
# Output
dst00       dst06        mean
latitude longitude                                    
60.0     0.0        279.837494  279.800812  279.819153
1.0        279.557495  279.370789  279.464142
2.0        279.437469  279.100800  279.269135
3.0        279.227478  278.850800  279.039124
4.0        278.417480  278.190796  278.304138
...                        ...         ...         ...
90.0     96.0       243.507477  243.370804  243.439148
97.0       243.507477  243.370804  243.439148
98.0       243.507477  243.370804  243.439148
99.0       243.507477  243.370804  243.439148
100.0      243.507477  243.370804  243.439148
[3131 rows x 3 columns]