如何使用 IDW 将 xarray 从高分辨率重新网格化到低分辨率

How to re-gridding xarray from higher to lower resolution using IDW

我正在使用 winpython 3.6。我有一个给定区域的 xarray 数据,如下所示:

sea_clt=clt.sel(lat=slice(-13, 31), lon=slice(89,152))
clt_sea_array=sea_clt[:,:,:]

Out[20]: 
<xarray.DataArray 'clt' (time: 20075, lat: 23, lon: 25)>
[11543125 values with dtype=float32]
Coordinates:
  * lat      (lat) float64 -13.0 -11.0 -9.0 -7.0 -5.0 -3.0 -1.0 1.0 3.0 5.0 ...
  * lon      (lon) float64 91.25 93.75 96.25 98.75 101.2 103.8 106.2 108.8 ...
  * time     (time) datetime64[ns] 1950-01-01T12:00:00 1950-01-02T12:00:00 ...

网格间距为200km*200km(2.0度*2.0度尺度),日时间序列变量。现在我想在每个时间步长(50km*50km 或 0.5degree*0.5degree 网格比例)重新网格化这些数据。我尝试了重塑选项但没有成功。我无法得到任何解决方案。如何使用最近邻或 IDW 等标准方法做到这一点?任何帮助,将不胜感激。

可以使用 reindex

完成网格上数据的最近邻查找
sea_clt.reindex(lat=lat_new, lon=lot_new, method='nearest')

其他插值,例如线性插值,尚未在 xarray 中实现。

对于线性插值,我们现在能做的最好的可能是

from scipy.interpolation import interp1d

def interp(y_src, x_src, x_dest, **kwargs):
    return interp1d(x_src, y_src, **kwargs)(x_dest)

new_da = sea_clt
new_da = xr.apply_ufunc(interp, new_da, input_core_dims=[['lat']], 
                        output_core_dims=[['lat_new']], 
                        kwargs={'x_src': new_da['lat'], 'x_dest': lat_new})
new_da.coords['lat_new'] = lat_new

new_da = xr.apply_ufunc(interp, new_da, input_core_dims=[['lon']], 
                        output_core_dims=[['lon_new']], 
                        kwargs={'x_src': new_da['lon'], 'x_dest': lon_new})
new_da.coords['lon_new'] = lon_new

在应用您的代码时,出现此错误:

from scipy.interpolate import interp1d
lat_new = np.linspace(0.5, -13, 31)
lon_new = np.linspace(0.5, 89,152)


def interp(y_src, x_src, x_dest, **kwargs):
    return interp1d(x_src, y_src, **kwargs)(x_dest)


new_da = sea_clt
new_da = xarray.apply_ufunc(interp, new_da, input_core_dims=[['lat']], 
                        output_core_dims=[['lat_new']], 
                        kwargs={'x_src': new_da['lat'], 'x_dest': lat_new})
new_da.coords['lat_new'] = lat_new

new_da = xarray.apply_ufunc(interp, new_da, input_core_dims=[['lon']], 
                        output_core_dims=[['lon_new']], 
                        kwargs={'x_src': new_da['lon'], 'x_dest': lon_new})
new_da.coords['lon_new'] = lon_new

没明白我哪里做错了。错误在这里:

  File "C:\python3\WinPython\python-3.6.5.amd64\lib\site-packages\scipy\interpolate\interpolate.py", line 663, in _check_bounds
    raise ValueError("A value in x_new is below the interpolation "

ValueError: A value in x_new is below the interpolation range.

你也可以试试xEMSF package,它似乎提供了强大的方法来重新网格化xarray数据。

我想你想在空间尺度上从低分辨率重新网格化到高分辨率。一种方法是使用 xarray 的 interp_like 方法。以下是示例代码。

import xarray as xr
import matplotlib.pyplot as plt
import numpy as np
ds1=xr.open_dataset("etopo40.cdf")
ds2=xr.open_dataset("etopo20.cdf")
ds1=ds1.rename({'ETOPO40Y': 'lat', 'ETOPO40X':'lon'})
ds2=ds2.rename({'ETOPO20Y': 'lat', 'ETOPO20X1_1081':'lon'})
# print(ds1)
# print(ds2)
rose40=ds1.ROSE
rose20=ds2.ROSE
rose_interp=rose40.interp_like(rose20)