gpt4 book ai didi

python - 从python中的每日netcdf文件计算每月平均值

转载 作者:行者123 更新时间:2023-11-28 21:40:15 25 4
gpt4 key购买 nike

您好,我有一个包含每日数据的 netcdf 文件。文件的形状是 (5844, 89, 89) 即 16 年的数据。我试图从每日数据中获取月平均值。我正在寻找类似于 pandas 数据框中的 resample 函数。反正有没有在 python 中做到这一点。据我所知,使用 cdo 和 nco 很容易计算,但我正在寻找 python。

我用来读取netcdf文件的示例代码是:

import netCDF4
from netCDF4 import Dataset
fh = Dataset(ncfile, mode='r')
time = fh.variables['time'][:]
lon = fh.variables['longitude'][:]
lat = fh.variables['latitude'][:]
data = fh.variables['t2m'][:]
data.shape

最佳答案

@jhamman 感谢您的建议xarray.resample .它比我想象的要简单,我的问题的答案是:

import xarray as xr
ds = xr.open_dataset(nc_file)
monthly_data = ds.resample(freq = 'm', dim = 'time', how = 'mean')

关于python - 从python中的每日netcdf文件计算每月平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45796170/

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