简体   繁体   English

Scipy 插值网格数据的问题

[英]Issues with Scipy interpolate griddata

I have a netcdf file with a spatial resolution of 0.05º and I want to regrid it to a spatial resolution of 0.01º like this other netcdf .我有一个空间分辨率为 0.05º 的netcdf 文件,我想像其他 netcdf一样将其重新设置为 0.01º 的空间分辨率。 I tried using scipy.interpolate.griddata, but I am not really getting there, I think there is something that I am missing.我尝试使用 scipy.interpolate.griddata,但我并没有真正到达那里,我认为我缺少一些东西。

original_dataset = xr.open_dataset('to_regrid.nc')
target_dataset= xr.open_dataset('SSTA_L4_MED_0_1dg_2022-01-18.nc')

According to scipy.interpolate.griddata documentation , I need to construct my interpolation pipeline as following:根据scipy.interpolate.griddata 文档,我需要构建我的插值管道如下:

grid = griddata(points, values, (grid_x_new, grid_y_new), method='nearest')网格=网格数据(点,值,(grid_x_new,grid_y_new),方法='最近')

So in my case, I assume it would be as following:因此,就我而言,我认为它如下所示:

#Saving in variables the old and new grids
grid_x_new = target_dataset['lon']
grid_y_new = target_dataset['lat']
grid_x_old = original_dataset ['lon']
grid_y_old = original_dataset ['lat']

points = (grid_x_old,grid_y_old)
values = original_dataset['analysed_sst'] #My variable in the netcdf is the sea surface temp.

Now, when I run griddata:现在,当我运行 griddata 时:

from scipy.interpolate import griddata
grid = griddata(points, values, (grid_x_new, grid_y_new),method='nearest')

I am getting the following error:我收到以下错误:

ValueError: shape mismatch: objects cannot be broadcast to a single shape ValueError:形状不匹配:无法将对象广播到单个形状

I assume it has something to do with the lat/lon array shapes.我认为它与纬度/经度阵列形状有关。 I am quite new to netcdf field and don't really know what can be the issue here.我对 netcdf 领域很陌生,我真的不知道这里有什么问题。 Any help would be very appreciated!任何帮助将不胜感激!

In your original code the indices in grid_x_old and grid_y_old should correspond to each unique coordinate in the dataset.在您的原始代码中,grid_x_old 和 grid_y_old 中的索引应对应于数据集中的每个唯一坐标。 To get things working correctly something like the following will work:为了使事情正常工作,如下所示:

import xarray as xr
from scipy.interpolate import griddata
original_dataset = xr.open_dataset('to_regrid.nc')
target_dataset= xr.open_dataset('SSTA_L4_MED_0_1dg_2022-01-18.nc')
#Saving in variables the old and new grids
grid_x_old = original_dataset.to_dataframe().reset_index().loc[:,["lat", "lon"]].lon
grid_y_old = original_dataset.to_dataframe().reset_index().loc[:,["lat", "lon"]].lat

grid_x_new = target_dataset.to_dataframe().reset_index().loc[:,["lat", "lon"]].lon
grid_y_new = target_dataset.to_dataframe().reset_index().loc[:,["lat", "lon"]].lat
values = original_dataset.to_dataframe().reset_index().loc[:,["lat", "lon", "analysed_sst"]].analysed_sst
points = (grid_x_old,grid_y_old)
grid = griddata(points, values, (grid_x_new, grid_y_new),method='nearest')

I recommend using xesm for regridding xarray datasets.我建议使用 xesm 重新网格化 xarray 数据集。 The code below will regrid your dataset:下面的代码将重新排列您的数据集:

import xarray as xr
import xesmf as xe
original_dataset = xr.open_dataset('to_regrid.nc')
target_dataset= xr.open_dataset('SSTA_L4_MED_0_1dg_2022-01-18.nc')
regridder = xe.Regridder(original_dataset, target_dataset, "bilinear")
dr_out = regridder(original_dataset)

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

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