【问题标题】:calculate mean using 'year + month' combination in xarray使用 xarray 中的“年 + 月”组合计算平均值
【发布时间】:2019-10-09 15:31:34
【问题描述】:

我有一个 netcdf 文件,其中包含 5 年(2011 年至 2015 年)的每日数据。我想在 Python 中使用 XArray 计算数据的月平均值。

netcdf file:////test/Combined.nc {
  dimensions:
    latitude = 681;
    longitude = 841;
    time = 1826;
  variables:
    double latitude(latitude=681);
      :_FillValue = NaN; // double
      :name = "latitude";
      :long_name = "latitude";
      :units = "degrees_north";
      :standard_name = "latitude";

    double longitude(longitude=841);
      :_FillValue = NaN; // double
      :name = "longitude";
      :long_name = "longitude";
      :units = "degrees_east";
      :standard_name = "longitude";

    long time(time=1826);
      :name = "time";
      :long_name = "time";
      :standard_name = "time";
      :units = "days since 2011-01-01 00:00:00";
      :calendar = "proleptic_gregorian";

    float PET(time=1826, latitude=681, longitude=841);
      :_FillValue = -999.0f; // float
      :name = "PET";
      :long_name = "Potential evapotranspiration";
      :units = "mm";
      :standard_name = "PET";      

  :var_name = "PET";
}

我试图做的是使用 groupby 来计算每月平均值:

import numpy as np
import xarray as xr

ds = xr.open_dataset("c:\\test\\Combined.nc")
ds_avg = ds.PET.groupby('time.month').mean(dim='time')
ds_avg.to_netcdf("C:\\test\\Combined_avg.nc") 

但上述代码的问题是输出了一个包含每月平均(从 2011 年到 2015 年)的文件。这意味着我在结果文件中有 12 个月。那不是我想做的。我想计算 2011 年 1 月、2011 年 2 月、2011 年 3 月至 2015 年 12 月的月平均值,以便在结果文件中得到 12 * 5 个月。所以这意味着 groupby 不应该发生在“time.month”而是“time.year:time.month”。我该怎么做?

谢谢

【问题讨论】:

    标签: python netcdf python-xarray


    【解决方案1】:

    你应该用一个月的频率使用resampledoc。那么:

    ds_avg = ds.resample('1M').mean()
    

    如果您对任何其他类似(简单)操作感兴趣,请查看我们为ERA-NUTS dataset 设置的此笔记本。

    另一个使用另一个数据集的例子:

    <xarray.Dataset>
    Dimensions:    (bnds: 2, latitude: 61, longitude: 91, time: 218)
    Coordinates:
      * longitude  (longitude) float32 -22.5 -21.75 -21.0 -20.25 ... 43.5 44.25 45.0
      * latitude   (latitude) float32 72.0 71.25 70.5 69.75 ... 28.5 27.75 27.0
      * time       (time) datetime64[ns] 2000-01-16T15:00:00 ... 2018-01-01T03:00:00
    Dimensions without coordinates: bnds
    Data variables:
        time_bnds  (time, bnds) datetime64[ns] ...
        ssrdc      (time, latitude, longitude) float64 ...
        ssrd       (time, latitude, longitude) float64 ...
    

    然后应用重采样:

    In [13]: d.resample(time = '1Y').mean()                                                                                           
    Out[13]: 
    <xarray.Dataset>
    Dimensions:    (latitude: 61, longitude: 91, time: 19)
    Coordinates:
      * time       (time) datetime64[ns] 2000-12-31 2001-12-31 ... 2018-12-31
      * longitude  (longitude) float32 -22.5 -21.75 -21.0 -20.25 ... 43.5 44.25 45.0
      * latitude   (latitude) float32 72.0 71.25 70.5 69.75 ... 28.5 27.75 27.0
    Data variables:
        ssrdc      (time, latitude, longitude) float64 5.033e+05 ... 1.908e+05
        ssrd       (time, latitude, longitude) float64 4.229e+05 ... 1.909e+05
    

    【讨论】:

    • 这并没有达到预期的效果。我预计输出 PET 变量的维度为 PET(time=60, latitude=681, longitude=841),其中 60 是月数(5 年的月平均值)。但我得到的是 PET(time=60),其中缺少纬度和经度维度。
    • 这很奇怪,你是不是在做groupby之类的其他操作?你能分享你的代码吗?
    • # 打开 5 个 NetCDF 文件名 PET_2011.nc, PET_2012.nc 到 PET_2015.nc ds = xr.open_mfdataset(os.path.join("C:\\temp\\", 'PET_* .nc')) ds_avg = ds.PET.resample(time='1M').mean() ds_avg.to_netcdf("C:\\temp\\Combined_new_resample_avg.nc")
    • 你能检查一下 xarray 是否正确加载了数据变量吗?只需打印ds...
    • @Chris_007 迟到了,但你需要说 ds.resample(time='1M).mean()
    猜你喜欢
    • 2020-06-19
    • 2021-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-14
    • 2021-10-20
    相关资源
    最近更新 更多