【问题标题】:How I can extract a subset of months from all NetCDF files in one directory如何从一个目录中的所有 NetCDF 文件中提取月份的子集
【发布时间】:2018-05-01 15:51:06
【问题描述】:

我需要根据 1961-1990 年的温度数据计算第 90 个百分位数。我有 30 个 NETCDF 文件,每个文件都包含一年的每日数据。我需要计算特殊纬度和经度的百分位数(第 90 位),同时考虑所有 30 年每日数据中的夏季。我还需要考虑二月有 29 天的年份。当我运行我的代码时,它只考虑了第一个夏天(1961 年夏天),不能考虑所有的夏天。

data = xr.open_mfdataset('/Tmax-2m/Control/*.nc')

time = data.variables['time']
lon = data.variables['lon'][:]
lat = data.variables['lat'][:]
tmax = data.variables['tmax'][:]

df = data.sel(lat=39.18,lon=-95.57, method='nearest')
time2=df.variables['time'][151:243]
dg=df.sel (time=time2, method = 'nearest')
print np.percentile (dg.tmax, 90)

我尝试过这种方式,但它会计算每年夏天的百分位数:

splits=[151,516,881,1247,1612,1977,2342,2708,3073,3438,3803,4169,4534,4899,5264,5630,5995,6360,6725,7091,7456,7821,8186,8552,8917,9282,9647,10013,10378,10743]
t0=92
result=[]
for i in splits:
    time3=df.variables['time'][i:(i+t0)]
    dg=df.sel(time=time3, method ='nearest')  
    result.append(np.percentile (dg.tmax, 90))

np.savetxt("percentile1.csv", result, fmt="%s")

【问题讨论】:

  • 你不能只取结果的平均值吗?
  • 否,因为它的极端值并且不能被平均。

标签: python time extract netcdf percentile


【解决方案1】:

您是否考虑过使用 CDO 来完成这项任务? (如果你在linux下这很容易,如果你在windows上,你可能需要在cygwin下安装它)

您可以像这样将 30 个文件合并为一个时间序列:

cdo mergetime file_y*.nc timeseries.nc

这里的 * 是文件名中年份(1961、1962 等)的通配符,我假设文件名是 file_y1961.nc file_y1962.nc 等...酌情采用。 timeseries.nc 是输出文件。

然后像这样计算季节性百分位数:

cdo yseaspctl,90 timeseries.nc -yseasmin timeseries.nc -yseasmax timeseries.nc percen.nc

percen.nc 将包含季节性百分位数,您可以提取夏季百分位数。

更多详情:https://code.mpimet.mpg.de/projects/cdo/

【讨论】:

  • 谢谢,但我不知道如何安装 CDO!
  • 你在使用 linux (ubuntu, mint?) 那么你可以执行 sudo apt-get install cdo 。但如果你在 Windows 机器上,你可能必须先安装 cygwin(在 cdo 网站上有关于如何在该平台上安装 cdo 的提示)。
猜你喜欢
  • 2022-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-01
  • 1970-01-01
  • 2019-08-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多