在 python 中重新网格 Netcdf 文件

lsr*_*729 2 python interpolation netcdf python-xarray netcdf4

我正在尝试将 NetCDF 文件从 0.125 度重新网格化到 0.083 度空间尺度。netcdf 包含 224 个纬度和 464 个经度,并且包含一年的每日数据。

我尝试了 xarray 但它产生了这个内存错误: MemoryError: Unable to allocate 103. GiB for an array with shape (13858233841,) and data type float64

如何使用 python 重新网格化文件?

小智 5

另一个选择是尝试cf-python,它(通常)可以在球极坐标和笛卡尔坐标中重新网格大于内存的数据集。它使用ESMF重新网格化引擎来完成此操作,因此可以使用线性、一阶和二阶保守、最近邻等重新网格化方法。

以下是您需要的重新网格化的示例:

import cf
import numpy

f = cf.example_field(2) # Use cf.read to read your own data

print('Source field:')
print(f)

# Define the output grid
lat = cf.DimensionCoordinate(
           data=cf.Data(numpy.arange(-90, 90.01, 0.083), 'degreesN'))
lon = cf.DimensionCoordinate(
          data=cf.Data(numpy.arange(0, 360, 0.083), 'degreesE'))

# Regrid the field
g = f.regrids({'latitude': lat, 'longitude': lon}, method='linear')

print('\nRegridded field:')
print(g)
Run Code Online (Sandbox Code Playgroud)

其产生:

Source field:
Field: air_potential_temperature (ncvar%air_potential_temperature)
------------------------------------------------------------------
Data            : air_potential_temperature(time(36), latitude(5), longitude(8)) K
Cell methods    : area: mean
Dimension coords: time(36) = [1959-12-16 12:00:00, ..., 1962-11-16 00:00:00]
                : latitude(5) = [-75.0, ..., 75.0] degrees_north
                : longitude(8) = [22.5, ..., 337.5] degrees_east
                : air_pressure(1) = [850.0] hPa

Regridded field:
Field: air_potential_temperature (ncvar%air_potential_temperature)
------------------------------------------------------------------
Data            : air_potential_temperature(time(36), latitude(2169), longitude(4338)) K
Cell methods    : area: mean
Dimension coords: time(36) = [1959-12-16 12:00:00, ..., 1962-11-16 00:00:00]
                : latitude(2169) = [-90.0, ..., 89.94399999999655] degreesN
                : longitude(4338) = [0.0, ..., 359.971] degreesE
                : air_pressure(1) = [850.0] hPa

Run Code Online (Sandbox Code Playgroud)

有很多选项可以从其他字段获取目标网格,以及显式定义它。更多详细信息可以在文档中找到

cf-python 将从附加到数据集的 CF 元数据推断哪些轴是 X 和 Y 等,但如果缺少,那么总有办法手动设置它或解决它。


Rob*_*son 5

使用 CDO 作为后端的 Python 选项是我的包 nctoolkit: https: //nctoolkit.readthedocs.io/en/latest/,可通过 pip 安装(https://pypi.org/project/nctoolkit/

它有一个名为 to_latlon 的内置方法,它将重新网格化到指定的 latlon 网格

对于您的情况,您需要执行以下操作:

import nctoolkit as nc
data = nc.open_data(infile)
data.to_latlon(lon = [lon_min,lon_max],lat=[lat_min,lat_max], res =[0.083, 0.083])
Run Code Online (Sandbox Code Playgroud)