【问题标题】:using CDO to extract dataset only for a specific region使用 CDO 仅提取特定区域的数据集
【发布时间】:2019-10-26 18:32:22
【问题描述】:

我想使用 'cdo' 从使用南美洲另一个 NetCDF 的降水 NetCDF 数据集中提取数据。 我尝试了多个程序,但总是遇到一些错误(例如网格大小不同、不支持的通用坐标等)。

我试过的代码:

cdo mul chirps_2000-2015_annual_SA.nc Extract feature.nc output.nc
# Got grid error

cdo -f nc4 setctomiss,0 -gtc,0 -remapcon,r1440x720 Chirps_2000-2015_annual_SA.nc CHIRPS_era5_pev_2000-2015_annual_SA_masked.nc
# Got unsupported generic error


【问题讨论】:

  • 如果网格丢失,那么您可能会发现 NCO 中的数据切片更有帮助,文件的链接现在已失效,因此我删除了最后一句提到死链接。

标签: dataset extract cdo-climate


【解决方案1】:

我相信您可以制作/找到更优雅的解决方案,但我只是将 Python 和 shell 可执行文件 cdo 组合在一起来完成任务(有时/某处调用子进程可能被视为一种坏习惯)。

#!/usr/bin/env ipython
import numpy as np
from netCDF4 import Dataset
import subprocess
# -------------------------------------------------
def nc_varget(filein,varname):
    ncin=Dataset(filein);
    vardata=ncin.variables[varname][:];
    ncin.close()
    return vardata
# -------------------------------------------------
gridfile='extract_feature.nc'
inputfile='precipitation_2000-2015_annual_SA.nc'
outputfile='selected_region.nc'
# -------------------------------------------------
# Detect the start/end based on gridfile:
poutlon=nc_varget(gridfile,'lon')
poutlat=nc_varget(gridfile,'lat')

pinlon=nc_varget(inputfile,'lon')
pinlat=nc_varget(inputfile,'lat')

kkx=np.where((pinlon>=np.min(poutlon)) & (pinlon<=np.max(poutlon)))
kky=np.where((pinlat>=np.min(poutlat)) & (pinlat<=np.max(poutlat)))
# -------------------------------------------------
# -------------------------------------------------
commandstr='cdo selindexbox,'+str(np.min(kkx))+','+str(np.max(kkx))+','+str(np.min(kky))+','+str(np.max(kky))+' '+inputfile+' '+outputfile
subprocess.call(commandstr,shell=True)

您的数据中的问题是文件“precipitation_2000-2015_annual_SA.nc”目前没有指定网格 - 变量 lon、lat 是通用的,因此网格是通用的。否则,您可以使用其他运算符代替selindexbox。文件extract_feature.nc 更接近标准,因为变量 lon、lat 也有 name 和 unit 属性。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-15
    • 2012-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-04
    • 1970-01-01
    相关资源
    最近更新 更多