Recipe: R program as OPeNDAP client
Back to G5NR Data Access Guide.
Problem
By accessing the collection inst01hr_3d_T_Cv
via the OPeNDAP server
http://opendap.nccs.nasa.gov/dods/OSSE/G5NR/Ganymed/7km
we want to read the surface temperature data inside the box bound by latitudes 25oN, 50oN and longitudes -130oW, -65oW for 2006/Sep/18, 9z, compute its min/max and plot the data at the surface.
Solution
The following code has been tested with R version 3.1.2.
The metadata for the collection inst01hr_3d_T_Cv
is available at
http://opendap.nccs.nasa.gov/dods/OSSE/G5NR/Ganymed/7km/0.5000_deg/inst/inst01hr_3d_T_Cv.info
Code
This code accesses the collection inst01hr_3d_T_Cv
from the OPeNDAP server and reads a subset of the temperature data (all levels inside the bounding box specified above) and computes its max/min. It then plots the data at the surface (level=72).
NOTE:
- Instead of reading a downloaded NetCDF-4 file, we read an OPeNDAP URL.
- While in the downloaded file, the temperature variable appears in the uppercase (T), on the OPeNDAP server, this variable is in lowercase.
- Via the OPeNDAP URL, we now have access to all times for which data exists. The hourly
inst
files are available starting at 2005/15/15, 2200z. Our desired time, 2006/09/18, 0900z is then the 11772th step.
# Load Libraries
library(ncdf4)
library(rworldmap)
# Url
url <- "http://opendap.nccs.nasa.gov:80/dods/OSSE/G5NR/Ganymed/7km/0.5000_deg/inst/inst01hr_3d_T_Cv"
nc <- nc_open(url)
# Bounding Box
imin <- round((-130.+180.)/0.5)-1
imax <- round(( -64.+180.)/0.5)-1
jmin <- round(( 25 + 90.)/0.5) - 1
jmax <- round(( 50 + 90.)/0.5) - 1
im <- imax-imin+1
jm <- jmax-jmin+1
lm <- 72
# Start and Count vectors
offset <- c(imin, jmin, 1, 11771)
count <- c(im, jm, lm, 1)
t <- ncvar_get(nc,"t",start=offset,count=count)
# Statistics
print(summary(t))
# Plot Data
mapGriddedData(t[1:im,1:jm,71],xlim=c(-130.,-65.),ylim=c(25.,50.))
Output
Running this R script > source("g5nr.R")
we get the text output
Min. 1st Qu. Median Mean 3rd Qu. Max. 190.7 221.2 248.1 248.7 275.3 305.8
and the plot