简体   繁体   English

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

[英]Interpolation gridded data to geographical point location

I am a big fan of MetPy and had a look at their interpolation functions ( https://unidata.github.io/MetPy/latest/api/generated/metpy.interpolate.html ) but could not find what I was looking for.我是 MetPy 的忠实粉丝,并查看了它们的插值函数( https://unidata.github.io/MetPy/latest/api/generated/metpy.interpolate.html ),但找不到我要找的东西。

I am looking for a function to interpolate a gridded 2D (lon and lat) or 3D (lon, lat and vertical levels) climate data field to a specific geographic location (lat/lon).我正在寻找一个函数来将网格化的 2D(经度和纬度)或 3D(经度、纬度和垂直水平)气候数据字段插入到特定的地理位置(纬度/经度)。

The function would take 5 arguments: a 2D/3D data variable and associated latitude and longitude variables, as well as the two desired latitude and longitude coordinate values.该函数将采用 5 个参数:一个 2D/3D 数据变量和相关的纬度和经度变量,以及两个所需的纬度和经度坐标值。 Returned is either a single value (for 2D field) or a vertical profile (for 3D field).返回的是单个值(对于 2D 字段)或垂直剖面(对于 3D 字段)。

I am basically looking for an equivalent to the old Basemap function bm.interp().我基本上是在寻找与旧底图函数 bm.interp() 等效的函数。 Cartopy does not have an equivalent. Cartopy 没有等价物。 The CDO (Climate Data Operators) operator 'remapbil,lon=/lat=' does the same thing but works directly on netCDF files from the command line, I'm looking for a Python solution. CDO(气候数据运算符)运算符 'remapbil,lon=/lat=' 做同样的事情,但直接从命令行处理 netCDF 文件,我正在寻找 Python 解决方案。

I think such a function would be a useful addition to the MetPy library as it allows for comparing gridded data (eg, model or satellite data) with point observations such as from weather stations or radiosonde profiles (treated as just a vertical profile here).我认为这样的函数将是 MetPy 库的一个有用补充,因为它允许将网格数据(例如,模型或卫星数据)与来自气象站或无线电探空仪剖面(此处仅视为垂直剖面)的点观测进行比较。

Can you point me in the right direction?你能为我指出正确的方向吗?

I think what you're looking for already exists in scipy.interpolate (scipy is one of MetPy's dependencies).我认为你正在寻找的东西已经存在于scipy.interpolate (scipy 是 MetPy 的依赖项之一)。 Here we can use interpn to interpolate linearly in n dimensions:这里我们可以使用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.))

This can be done easily with my nctoolkit package ( https://nctoolkit.readthedocs.io/en/latest/ ).这可以通过我的 nctoolkit 包( https://nctoolkit.readthedocs.io/en/latest/ )轻松完成。 It uses CDO as a backend, and defaults to bilinear interpolation.它使用 CDO 作为后端,默认为双线性插值。 The following would regrid a .nc file to a single grid point and then convert it to an xarray dataset.以下内容将 .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()

To add one more solution, if you're already using multidimensional netCDF files and want a Python solution: check out xarray's interpolation tools .要添加更多解决方案,如果您已经在使用多维 netCDF 文件并想要一个 Python 解决方案:请查看 xarray 的插值工具 They support multidimensional, label-based interpolation with usage similar to xarray's indexing interface.它们支持多维、基于标签的插值,其用法类似于 xarray 的索引接口。 These are built on top of the same scipy.interpolate otherwise mentioned, and xarray is also a MetPy dependency.它们建立在相同的scipy.interpolate另外提到,并且 xarray 也是 MetPy 依赖项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM