使用 scipy.interpolate.griddata 对 xarray 中的多维数据进行插值

Use of scipy.interpolate.griddata for interpolation of data of multiple dimensions from xarray

我有一个名为 ds 的 xarray DataSet 降雨数据,具有三个维度 longitudelatitudetime:

<xarray.Dataset>
Dimensions:    (latitude: 691, longitude: 886, time: 1)
Coordinates:
  * longitude  (longitude) float64 112.0 112.0 112.1 112.1 ... 156.2 156.2 156.3
  * latitude   (latitude) float64 -9.975 -10.03 -10.08 ... -44.42 -44.47 -44.52
  * time       (time) datetime64[ns] 1980-01-03
Data variables:
    RAIN       (time, latitude, longitude) float64 0.0 0.0 0.0 ... 0.0 0.0 0.0

我想根据另一组经度和纬度插入降雨值:EXAMPLE_FFDI_LON_XR_DAEXAMPLE_FFDI_LAT_XR_DA。它们的值与 ds.

的经度和纬度完全不同

EXAMPLE_FFDI_LON_XR_DA:

<xarray.DataArray 'longitude' (longitude: 193)>
array([140.8    , 140.84792, ... ...], dtype=float32)
Coordinates:
  * longitude  (longitude) float32 140.8 140.84792 140.89584 ... 149.95209 150.0
Attributes:
    latIntersect:         0.0
    lonCentre:            145.4
    units:                degrees_east
    projectionType:       MERCATOR
    _CoordinateAxisType:  Lon

EXAMPLE_FFDI_LAT_XR_DA:

<xarray.DataArray 'latitude' (latitude: 106)>
array([-39.2     , -39.149525, ... ...], dtype=float32)
Coordinates:
  * latitude  (latitude) float32 -39.2 -39.149525 -39.09905 ... -33.950478 -33.9
Attributes:
    latIntersect:         0.0
    lonCentre:            145.4
    units:                degrees_north
    projectionType:       MERCATOR
    _CoordinateAxisType:  Lat

我想到了使用 xarray xarray.DataArray.interp 函数,但这只支持 nearest 方法。我是 scipy 的新手,但认为它更适合我使用 scipy 库 scipy.interpolate.griddata 函数进行插值的需要。我怎样才能对我的数据使用这个函数?一个工作示例会有所帮助。

我不确定是否可以 scipy 与您现在拥有的 DataArray 对象进行交互。您可以执行以下操作:

from scipy.interpolate import griddata
import xarray
import numpy as np
# construct a dummy df like you have
values = np.random.rand(10, 30, 30) * 100
df = xarray.DataArray(values, [('time', range(10)), ('latitude', range(30)), ('longitude', range(30))])
# extract the values from the array
latitude = df['latitude'].values
longitude = df['longitude'].values
length_values = len(latitude) * len(longitude)

# create grid
x_grid, y_grid = np.meshgrid(latitude, longitude)
points = np.empty((length_values, 2))
points[:, 0] = x_grid.flatten()
points[:, 1] = y_grid.flatten()

# select a single timestep in which to interpolate the data
rain_at_locations = df.where(df.time == 3, drop=True)
values = np.array(rain_at_locations.values.flatten())

#your test locations
EXAMPLE_FFDI_LAT_XR_DA = range(5, 15) # extract these from you arrays
EXAMPLE_FFDI_LON_XR_DA = range(20, 30)
grid_x, grid_y = np.meshgrid(EXAMPLE_FFDI_LAT_XR_DA, EXAMPLE_FFDI_LON_XR_DA)

interpolated_values = griddata(points, values, (grid_x, grid_y), method='cubic')

然后可以将这些值重新提供给数据数组。