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

时间:2018-04-23 03:35:45

标签: python python-3.x python-2.7 python-requests python-xarray

我正在使用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等标准方法做到这一点?任何帮助,将不胜感激。

3 个答案:

答案 0 :(得分:1)

停留在网格上的数据的最近邻居查找可以使用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

答案 1 :(得分:1)

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

答案 2 :(得分:0)

在应用您的代码时,收到此错误:

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.