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

问题描述 投票:0回答:1

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

我需要计算平均日温度数据集。有没有一种方法可以 instrumentally,但不能像从相应节点手动弹出值,计算平均值并插入到数据集中?

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')

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

00UTC 06UTC

python arrays dataset mean python-xarray
1个回答
0
投票

您可以使用

xr.concat

# With Pandas Index
# idx = pd.Index(['latitude', 'longitude'], name='lat_lon')
# dst = xr.concat([dst00, dst06], dim=idx).mean('lat_lon')
dst = xr.concat([dst00, dst06], dim=['latitude', 'longitude']).mean('concat_dim')
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]
© www.soinside.com 2019 - 2024. All rights reserved.