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

标签 python numpy netcdf

您好,我有一个包含每日数据的 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/

相关文章:

python - 使用正则表达式匹配 HTML 中的一对注释

python - 将 numpy 数组转换为 Shapely Points 的最有效方法是什么?

netcdf - 如何使用 NCO 编辑 netcdf 文件中的全局属性

Python 队列似乎正在消亡

python - 无法在我的 Windows 10 上安装 "Turicreate"

python - 如何使用python替换qml线系列中的多个点?

python - 如何在图像阵列中添加 channel ?

python - 为什么 scipy.sparse.csc_matrix.sum() 的结果将其类型更改为 numpy 矩阵?

r - 与 HDF5 或 netCDF 相比,使用 .Rdata 文件有哪些缺点?

python - 如何通过 OpenDAP 使用 xarray 打开多个 NetCDF 文件?