簡體   English   中英

Scipy 插值網格數據的問題

[英]Issues with Scipy interpolate griddata

我有一個空間分辨率為 0.05º 的netcdf 文件,我想像其他 netcdf一樣將其重新設置為 0.01º 的空間分辨率。 我嘗試使用 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')

根據scipy.interpolate.griddata 文檔,我需要構建我的插值管道如下:

網格=網格數據(點,值,(grid_x_new,grid_y_new),方法='最近')

因此,就我而言,我認為它如下所示:

#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.

現在,當我運行 griddata 時:

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

我收到以下錯誤:

ValueError:形狀不匹配:無法將對象廣播到單個形狀

我認為它與緯度/經度陣列形狀有關。 我對 netcdf 領域很陌生,我真的不知道這里有什么問題。 任何幫助將不勝感激!

在您的原始代碼中,grid_x_old 和 grid_y_old 中的索引應對應於數據集中的每個唯一坐標。 為了使事情正常工作,如下所示:

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')

我建議使用 xesm 重新網格化 xarray 數據集。 下面的代碼將重新排列您的數據集:

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