I am trying to get the wind exposure for infrastructures. I have a dataset with their latitude and longitude.
The NetCDF file gives daily near surface wind speed data projections for the year 2058. It can be downloaded with the following 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
I have tried the following loop to get the average wind speed for each location (their closest grid point):
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"))])
}
Where Infrast is my dataset of infrastructures, their latitude is in column 3 and longitude in column 4, an I want the output to be saved at the 12th column of my dataset.
I get the following error:
Error in sfcWind[which.min(abs(lon - (x))), which.min(abs(lat - (y))), :
incorrect number of dimensions
I used this code before to get the average of projected temperatures and it worked just fine. The NetCDF file had the same dimensions than this one (lat, lon, time). This is why I don't understand the error here.
I am quite new to R and I just started to work with NetCDF files, any help or suggestion would be appreciated.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…