如何从NetCDF文件中提取R中特定位置的地表风速数据



我正在努力获得基础设施的风力暴露。我有一个数据集,里面有他们的纬度和经度。

NetCDF文件提供了2058年的每日近地表风速数据预测。可以使用以下URL下载:http://esg-dn2.nsc.liu.se/thredds/fileServer/esg_dataroot1/cmip6data/CMIP6/ScenarioMIP/EC-Earth-Consortium/EC-Earth3/ssp585/r1i1p1f1/day/sfcWind/gr/v20200310/sfcWind_day_EC-Earth3_ssp585_r1i1p1f1_gr_20580101-20581231.nc

我尝试了以下循环来获得每个位置(最近的网格点(的平均风速:

sfcWind_filepath<-paste0("sfcWind_day_EC-Earth3_ssp585_r1i1p1f1_gr_20580101-20581231.nc")
sfcWind_output<-nc_open(sfcWind_filepath)
lon<-ncvar_get(sfcWind_output,varid = "lon")
lat<-ncvar_get(sfcWind_output,varid = "lat")
sfcWind_time<-nc.get.time.series(sfcWind_output,v = "sfcWind",time.dim.name = "time")
sfcWind<-ncvar_get(sfcWind_output, "sfcWind")
for(i in 1:nrow(Infrast))
{sfcWind<-rep(i,nrow(Infrast))
x<-Infrast[i,4]
y<-Infrast[i,3]
Infrast[i,12]<-mean(sfcWind[which.min(abs(lon - (x))),
which.min(abs(lat - (y))),
c(which(format(sfcWind_time, "%Y-%m-%d") == "2058-01-01"):which(format(sfcWind_time, "%Y-%m-%d") == "2058-12-31"))])
}

Infrast是我的基础设施数据集,它们的纬度在第3列,经度在第4列,我希望输出保存在数据集的第12列。

我得到以下错误:

Error in sfcWind[which.min(abs(lon - (x))), which.min(abs(lat - (y))),  : 
incorrect number of dimensions

我以前用过这个代码来获得预计温度的平均值,它运行得很好。NetCDF文件具有与此文件相同的维度(lat、lon、time(。这就是为什么我不理解这里的错误。

我是R的新手,我刚刚开始使用NetCDF文件,如果有任何帮助或建议,我们将不胜感激。

我对R也相对陌生,但一种可能的方法是使用cmsaf包。在这里,您可以使用selpoint或selpoint.multi函数提取特定位置或多个位置的变量的时间序列。您所需要的只是所需位置的纬度/经度坐标列表。然后,它将为输出生成一个新的netcdf或csv文件。然后,您可以根据提取的点数据计算平均值。也许有一种更好、更有效的方法,但希望这会有所帮助。

NB我无法对此进行测试,因为没有提供可复制的示例。尽管如此,这应该奏效。

首先将文件作为光栅砖打开

library(raster)
sfcWind_output <- brick(sfcWind_filepath, varname="sfcWind")

现在,您可以使用类似的坐标提取值

extract(sfcWind_output, cbind(lon,lat))

最新更新