首页 > 解决方案 > 如何将 RegularGridInterpolator 与不同维度(纬度/经度/时间)的数据集一起使用?

问题描述

我想插入两个 NetCDF 数据集,一个是观察,一个是预测。我想将单个预测变量(即我的示例中的 modFile 中的“hs”)插入到观察数据(obsFile)的纬度、经度和时间上。卫星观测数据集 (obsFile) 的结构是所有内容都仅按时间标注(没有纬度或经度维度,它们是变量)。观测值的 lat 和 lon 值也比预测值具有更高的分辨率,因为它们是沿着轨道值,每隔几秒从卫星写入一次。使用 xarray 打开观察 netcdf 的读数:

xarray.Dataset
Dimensions:
time: 53595
Coordinates:
time
(time)
datetime64[ns]
2021-04-01T00:00:00.689500800 .....
lat
(time)
float64
...
lon
(time)
float64
...
Data variables:
...
swh
(time)
float32
...

预测的数据集 (modFile) 使用时间和纬度和经度值进行标注。因此,每个变量都是 3D,而卫星文件变量是 1D(仅时间):

xarray.Dataset
Dimensions:
latitude: 721, longitude: 1440, time: 25
Coordinates:
longitude
(longitude)
float32
0.0 0.25 0.5 ... 359.2 359.5 359.8
latitude
(latitude)
float32
-90.0 -89.75 -89.5 ... 89.75 90.0
time
(time)
datetime64[ns]
2021-04-01 ... 2021-04-02
Data variables:
...
hs
(time, latitude, longitude)
float32
...

这是我尝试在 3D 空间(时间、纬度、经度)中进行 3D 插值的代码:

import numpy as np
from scipy.interpolate import RegularGridInterpolator as rgi

latsat = np.array(obsFile['lat'])
lonsat = np.array(obsFile['lon'])
timesat = np.array(obsFile['time'])
latww = np.array(modFile['latitude'])
lonww = np.array(modFile['longitude'])
timeww = np.array(modFile['time'])
hsww = np.array(modFile['hs'])

my_interpolating_function = rgi((timeww, latww, lonww), hsww)
Vi = my_interpolating_function(np.array([timesat, latsat, lonsat]))

我的价值错误说它超出了界限,因为这个插值创建了一个(53595 x 53595 x 53595)卫星阵列与建模(25 x 721 x 1440)阵列:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-16-db49322d1a44> in <module>
      2 from scipy.interpolate import RegularGridInterpolator as rgi
      3 my_interpolating_function = rgi((timeww, latww, lonww), hsww)
----> 4 Vi = my_interpolating_function(array([timesat, latsat, lonsat]))
      5 
      6 # fn = RegularGridInterpolator((latsat, lonsat, timesat), hsww)

~/anaconda3/envs/aoes/lib/python3.6/site-packages/scipy/interpolate/interpolate.py in __call__(self, xi, method)
   2471             raise ValueError("The requested sample points xi have dimension "
   2472                              "%d, but this RegularGridInterpolator has "
-> 2473                              "dimension %d" % (xi.shape[1], ndim))
   2474 
   2475         xi_shape = xi.shape

ValueError: The requested sample points xi have dimension 53595, but this RegularGridInterpolator has dimension 3

任何想法如何重组我的卫星数据以正确插入或不同的功能?

标签: pythonmatlabscipyinterpolationnetcdf

解决方案


推荐阅读