如何对netCDF区域径流数据执行bilinear interpolation提取指定点值
问题描述
我有一个记录1850年至2014年地表径流数据的netCDF文件,已截取特定区域的径流切片,绘制结果得到四个径流值随日期变化的区域。我需要使用双线性插值(bilinear interpolation)计算经纬度点(282,43.5)处的径流值,请问该如何编写代码实现?我目前已编写的代码如下:
file='D:\\mrros_Eday_IPSL-CM6A-LR_land-hist_r1i1p1f1_gr_18500101-20141231.nc' dset = xr.open_dataset(file, decode_times=True, use_cftime=True) latbounds = (42,45) # (min, max) lonbounds = (280, 282.5) # (min, max) # 取指定日期的数据绘图 dset['mrros'].sel(time=cftime.DatetimeGregorian(1850, 7, 22, 12, 0, 0, 0),lat=slice(*latbounds),lon=slice(*lonbounds)).plot() plt.show() array=dset['mrros'].sel(time=cftime.DatetimeGregorian(1850, 7, 22, 12, 0, 0, 0),lat=slice(*latbounds),lon=slice(*lonbounds)) # 我可以看到每个区域的径流值,但是不知道怎么做双线性插值
实现方案
你不需要手动实现双线性插值逻辑,xarray内置的interp()方法已经原生支持该能力,指定插值方法参数即可直接得到结果,两种实现方式如下:
import xarray as xr import cftime import matplotlib.pyplot as plt file='D:\\mrros_Eday_IPSL-CM6A-LR_land-hist_r1i1p1f1_gr_18500101-20141231.nc' dset = xr.open_dataset(file, decode_times=True, use_cftime=True) # 定义目标插值点和时间 target_lon = 282 target_lat = 43.5 target_time = cftime.DatetimeGregorian(1850, 7, 22, 12, 0, 0, 0) # 方式1:直接对全量数据插值 runoff_result = dset['mrros'].interp( time=target_time, lon=target_lon, lat=target_lat, method='bilinear' ) print(f"插值得到的径流值为:{runoff_result.values}") # 方式2:先切区域再插值,大数据量下效率更高 latbounds = (42,45) lonbounds = (280, 282.5) data_slice = dset['mrros'].sel( time=target_time, lat=slice(*latbounds), lon=slice(*lonbounds) ) runoff_result_slice = data_slice.interp( lon=target_lon, lat=target_lat, method='bilinear' ) print(f"切片后插值得到的径流值为:{runoff_result_slice.values}") # 可视化验证插值点位置 data_slice.plot() plt.scatter(target_lon, target_lat, c='red', marker='*', s=100, label='目标插值点') plt.legend() plt.show()
说明
- 你代码注释里写的binomial interpolation是笔误,你需要的双线性插值对应参数为
method='bilinear' - 两种插值方式结果完全一致,如果需要批量插值多个经纬度点,直接给lon、lat参数传入列表即可一次性得到所有点的结果
内容的提问来源于stack exchange,提问作者aleucid
相关产品推荐
相关产品推荐

