【问题标题】:How to find climatological monthly mean from a netcdf data set using python?如何使用 python 从 netcdf 数据集中查找气候月平均值?
【发布时间】:2021-05-07 14:57:20
【问题描述】:

我有一个(每月时间x纬度x经度)的netcdf数据集。月数是 216,所以总共 18 年。第一个索引代表 1 月数据,第 2 个索引代表 2 月数据……第 12 个索引代表 12 月数据,第 13 个索引代表 1 月数据,第 14 个索引代表 2 月数据,依此类推。现在我想找到所有 1 月数据的平均值,所有 2 月数据的平均值......所有 12 月数据的平均值。因此,在执行此操作后,数组的大小将减少到 12xlatxlon(以前是 216xlatxlon)。我该怎么做?

【问题讨论】:

  • 无法上传数据集,也许看数据集后就清楚了

标签: python mean


【解决方案1】:

如果您使用的是 linux 或 macOS,您可以使用我的 nctoolkit 包 (https://nctoolkit.readthedocs.io/en/latest/) 用几行代码完成此操作:

import nctoolkit as nc

ds = nc.open_data("infile.nc")
# get the mean using month as the averaging period
ds.tmean("month")
# plot to visualize
ds.plot()

【讨论】:

    【解决方案2】:

    您可以使用xarray / pandas 中非常方便的resample() 选项。

    import xarray as xr 
    
    ds = xr.open_dataset('file.nc')
    ds_monthly_mean = ds.resample(time="1MS").mean()
    
    print(ds.time.shape)
    (5619,)
    
    print(ds_monthly_mean.time.shape)
    (12,)
    
    

    如果您不知道要使用什么重采样选项,请查看offset aliases。在定义了对数据进行重采样的维度后,只需选择您的重采样方法(均值、总和等......)

    【讨论】:

      猜你喜欢
      • 2020-10-12
      • 1970-01-01
      • 2019-07-16
      • 2022-01-07
      • 2021-12-22
      • 1970-01-01
      • 2018-01-29
      • 2017-05-05
      • 1970-01-01
      相关资源
      最近更新 更多