extract climate time series from E-OBS from multiple closest points to the point of interest

106 Views Asked by At

I would like to update the following codes to extract daily climate time series of E-OBS database to multiple (user defined) closest points:

I use ncdf4 package to read E-OBS data:

library(ncdf4)
obsdata <- nc_open("tg_ens_mean_0.1deg_reg_v19.0e.nc")

Create a dataframe with locations and WGS84 coordinates:

df <- data.frame(locality=c(1,2,3), lat=c(48,48,48), lon=c(17,18,19))

Now, run these codes to extract data from the closest point and save in separate csv files by ID of the location:

for (i in 1:nrow(df)) {
  lat <- df[i,2] # longitude of location
  lon <- df[i,3] # latitude  of location
  
  
  # get dates
  obsdatadates <- as.Date(obsdata$dim$time$vals, origin = '1950-01-01')
  
  
  # get values at location lonlat
  obsoutput <- ncvar_get(obsdata, varid = "tg",
                         start= c(which.min(abs(obsdata$dim$longitude$vals - lon)), # look for closest long
                                  which.min(abs(obsdata$dim$latitude$vals - lat)),  # look for closest lat
                                  1),
                         count = c(1,1,-1)) #count '-1' means 'all values along that dimension'that dimension'
  # create dataframe
  datafinal <- data.frame(date= obsdatadates, value = obsoutput, latitude=lat, longitude=lon, variable="Tday")
  fn1<-paste0(df[i,1],"_","Tday",".csv")
  write.table(datafinal,fn1,sep=",", row.names=FALSE)
}

Any help is appreciated! Thanks!

0

There are 0 best solutions below