简体   繁体   中英

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.

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.

NB I'm unable to test this, because no reproducible example was provided. Nonetheless, this should work.

First open the file as a raster brick

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

Now you can extract values using coordinates like this

extract(sfcWind_output, cbind(lon,lat))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM