Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

In this these example we will show how to benefit from OPeNDAP subsetting.

world DEM (lat,lon)

We will request subsets from worldwide digital elevevation maps (DEMs) that are too big to request as a whole: resulting in approx. 500 MB (over 10,000 x 20,000 points) or 2 GB (over 20,000 x 40,000) for respectively a 1 minute or 30 second grids. The code below was made by Maarten Plieger from KNMI for an NMDC sprint session.

...

Download the code of this R example (repos,manual download)

Jarkus (lat,lon)

Example for transect data, thanx to student Afra Asjes (VU).

Code Block

##Constructing dataframe from netcdf file in R

#Open ncdf4(only works on 32-bit)
library(ncdf4)

#Download netcdf file
url_grid<-"http://opendap.deltares.nl/thredds/fileServer/opendap/rijkswaterstaat/jarkus/grids/jarkusKB134_1110.nc"
download.file(url_grid, "jarkusKB134_1110.nc", method = "auto",quiet = FALSE, mode="wb", cacheOK = TRUE)

#Open file and check variables
ncin<-nc_open("jarkusKB134_1110.nc")
print(ncin)

#Get variables from netcdf file
lon <- ncvar_get(ncin, "lon")
nlon <- dim(lon)
head(lon)
lon.a<-as.vector(lon)

lat <- ncvar_get(ncin, "lat", verbose = F)
nlat <- dim(lat)
head(lat)
lat.a<-as.vector(lat)

print(c(nlon,nlat))

t<-ncvar_get(ncin,"time")
time<-as.vector(t)

altitude<-ncvar_get(ncin,"z")
dim(altitude)

#Select part of the variable of interest, here altitude
m <- 1
altitude.slice <- altitude[, , m]
altitude.vec<-as.vector(altitude.slice)

#Combine into data frame
altitude.data<-data.frame(cbind(time,lon.a,lat.a,altitude))
names(altitude.data)<-c("time","lon","lat","altitude")
head(altitude.data)

#Altitude data contains missing values; delete those rows
altitude.data<-altitude.data[!is.na(altitude.data$altitude),]
head(altitude.data)

#Save as CSV
write.csv(altitude.data,file="jarkusKB134_1110.csv",row.names=FALSE)

See also: OPeNDAP access with R, OPeNDAP subsetting with Matlab,OPeNDAP subsetting with python, PostgreSQL access with R

...