【问题标题】:Problems with NetCDF regridding, pythonNetCDF重新网格化,python的问题
【发布时间】:2021-12-20 16:20:23
【问题描述】:

大家好,这是我的第一个问题^^

我需要对一些 NetCDF 文件进行重新网格化,因此我尝试使用 cdo 和 cf 模块来执行此操作,但一切都失败了。也许有人为此目的知道其他一些模块,或者可以帮助我摆脱我的错误。 首先,我试过cf:

df = cf.read('my_file.nc')[0]

#define new grid
lat1 = cf.DimensionCoordinate(data=cf.Data(np.arange(-90, 90.01, 0.1), 'degreesN'))
lon1 = cf.DimensionCoordinate(data=cf.Data(np.arange(-180, 180.01, 0.1), 'degreesE'))

#regrid
g = df.regrids({'lat': lat1, 'lon': lon1}, method='linear')

但它给了我:“RuntimeError:除非安装了 ESMF 库,否则重新网格化方法将不起作用”。而且我不知道如何将它安装到我正在处理的远程服务器上。

然后我尝试了 cdo:

#define input and output files
infile='my_file.nc'
outfile='newgrid.nc' 

#file with needed grid parameters 
gridfile='gridfile.txt'

#bilinear regrid
cdo.remapbil('gridfile.txt', input=infile, output=outfile)

它给了我: “…… STDERR:错误(cdf_load_bounds):134369280000 字节的分配失败。 [第2048行文件stream_cdf_i.c] 系统错误信息:无法分配内存”

我的文件大约 2gb,这个操作怎么需要 134gb?

我也试过 xarray:

da_input = xarray.open_dataarray('my_file.nc') # the file the data will be loaded from
regrid_axis = np.arange(-90, 90, 0.1) # new coordinates
da_output = da_input.interp(latitude=regrid_axis) # specify calculation
da_output.to_netcdf('output.nc') # save direct to file

它有效,但我想它只在笛卡尔平面上进行插值,我需要它在球面上。

提前感谢大家

更新: 感谢您的所有回答。原来问题出在我的文件上(分辨率太高 300x300m)。所以我将它拆分为一些较小的文件(对于这个操作 cdo.sellonlatbox 也需要 134 gb,但是 xarray.sel 做到了),然后用 cdo 重新网格化并合并结果。

【问题讨论】:

  • 嗨,同意 Rob 的 cmets,很可能是您的目标网格规范导致了问题,您可以在您的问题中发布您的 gridfile.txt 文件吗?另外,请阅读标签元数据,cdo 用于 eclipse 框架,您需要 cdo-climate ;-)
  • CDO 必须至少在 RAM 中有一个水平切片。所以被错误指定,例如分辨率太高,可能会导致大多数机器崩溃

标签: python netcdf remap cdo-climate


【解决方案1】:

您可以尝试使用 nctoolkit 解决这个问题,它使用 CDO 作为后端:

import nctoolkit as nc
ds = nc.open_data("my_file.nc")
ds.to_latlon(lon = [-180, 180], lat = [-90, 90], res = 0.1)
ds.to_nc("output.nc")

如果您的文件为 2 GB,那么您可能在 CDO 示例中使用了网格文件错误。它不应该尝试分配那么多 RAM。 nctoolkit 将为您生成适当的 CDO 网格文件和命令。

【讨论】:

    猜你喜欢
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 2019-01-07
    • 2017-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-06
    相关资源
    最近更新 更多