Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
166 views
in Technique[技术] by (71.8m points)

How to extract surface wind speed data from a NetCDF file for a specific location in R?

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I'm also relative new to R too, but one possible way is to use the cmsaf package. Here, you can use the selpoint or selpoint.multi function to extract the time series of a variable at a specific location or for multiple locations. All you would need is the list of lat/lon coordinates for your desired locations. It will then make a new netcdf or csv file for the output. Then you could calculate the average from the extracted point data. There probably is a better and more efficient way but hopefully that might help.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...