【问题标题】:Python netcdf - Monthly median over the all time period of daily dataPython netcdf - 每日数据所有时间段的月中位数
【发布时间】:2019-02-22 17:04:36
【问题描述】:

我有一个 NetCDF 文件 input.nc。该文件代表了近 18 年每 4 天采样一次的数据。从这个文件中,我想计算整个时间段的每月中值。所以输出文件应该只包含 12 个时间步。

我正在使用以下代码:

import xarray as xr
os.chdir(inbasedir)
data = xr.open_dataset('input.nc')
monthly_data = data.resample(freq='m', dim ='time', how = 'median')
monthly_data.to_netcdf("test.nc")

不幸的是,当我查看输出文件时,我的代码已经完成了所有时间序列每个月的中值,我最终得到了 200 多个值。如何更改我的代码,以便计算整个时间段内 12 个月的中位数?

【问题讨论】:

    标签: python netcdf python-xarray


    【解决方案1】:

    您想使用groupby 方法:

    monthly_data = data.groupby('time.month').median()
    

    这里有一些很好的例子来说明如何将 xarray 与时间序列数据一起使用:http://xarray.pydata.org/en/stable/time-series.html

    【讨论】:

      猜你喜欢
      • 2015-04-14
      • 2015-07-06
      • 2021-11-13
      • 1970-01-01
      • 2018-01-29
      • 2021-10-05
      • 2021-10-02
      • 2019-04-25
      • 1970-01-01
      相关资源
      最近更新 更多