【发布时间】: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