将网格化数据插值到地理位置

Interpolation gridded data to geographical point location

我是 MetPy 的忠实粉丝,看过他们的插值函数 (https://unidata.github.io/MetPy/latest/api/generated/metpy.interpolate.html),但找不到我要找的东西。

我正在寻找将网格化的 2D(经度和纬度)或 3D(经度、纬度和垂直水平)气候数据字段插入特定地理位置的函数 (lat/lon)。

该函数将采用 5 个参数:2D/3D 数据变量和关联的纬度和经度变量,以及两个所需的纬度和经度坐标值。返回的是单个值(对于 2D 字段)或垂直剖面(对于 3D 字段)。

我基本上是在寻找与旧底图函数 bm.interp() 等效的函数。 Cartopy 没有等价物。 CDO(气候数据操作员)操作员 'remapbil,lon=/lat=' 做同样的事情,但直接从命令行处理 netCDF 文件,我正在寻找 Python 解决方案。

我认为这样的函数将是对 MetPy 库的有用补充,因为它允许将网格数据(例如,模型或卫星数据)与来自气象站或无线电探空仪剖面(仅作为此处为垂直剖面)。

你能给我指出正确的方向吗?

我认为您要查找的内容已存在于 scipy.interpolate 中(scipy 是 MetPy 的依赖项之一)。这里我们可以使用interpn在n维进行线性插值:

import numpy as np
from scipy.interpolate import interpn

# Array of synthetic grid to interpolate--ordered z,y,x
a = np.arange(24).reshape(2, 3, 4)

# Locations of grid points along each dimension
z = np.array([1.5, 2.5])
y = np.array([-1., 0., 1.])
x = np.array([-3.5, -1, 1, 3.5])

interpn((z, y, x), a, (2., 0.5, 2.))

这可以使用我的 nctoolkit 包轻松完成 (https://nctoolkit.readthedocs.io/en/latest/)。它使用 CDO 作为后端,默认为双线性插值。下面将把一个 .nc 文件重新网格化为单个网格点,然后将其转换为 xarray 数据集。

import nctoolkit as nc
import pandas as pd
data = nc.open_data("example.nc")
grid = pd.DataFrame({"lon":[0], "lat":[50]})
data.regrid(grid)
ds = data.to_xarray()

如果您已经在使用多维 netCDF 文件并想要 Python 解决方案,要再添加一个解决方案:查看 xarray 的 interpolation tools。它们支持多维、基于标签的插值,其用法类似于 xarray 的索引接口。这些构建在相同的 scipy.interpolate 之上,另外提到,xarray 也是 MetPy 的依赖项。