Accessing netCDF/OPeNDAP data with R

Get R, which includes several netCDF4 packages.

  1. Go to an OPeNDAP server (e.g. and pick a netCDF file by copying the contents of the Data URL box. Because the netcdf packages for windows are not yet opendap-enabled, download them.
  2. Define the associated url you just copied.
    url_grid <-
    "" # note: netcdf4 does not work on windows R
    url_time <-
    download.file(url_grid, "", method = "auto",
    quiet = FALSE, mode="wb", cacheOK = TRUE)
    download.file(url_time, "", method = "auto",
    quiet = FALSE, mode="wb", cacheOK = TRUE)
    A complete linux image with the R netcdf package compiled with OPeNDAP is available upon request from ""adaguc "at""".
  3. Extract the data. <- open.ncdf("")
    # look what's in there...
    # Get grid data
    G.x <- get.var.ncdf(,'x')
    G.y <- get.var.ncdf(,'y')
    # get only first timestep
    G.z <- get.var.ncdf(,'z')[,,1]
    # to get a black background, and set the scale of depth values to start from 0.
    G.z[G.z == -9999] <- 0
    # image.plot needs sorted x- and y-values;
    # as y-values are descending, the order is reversed here...
    G.y <- rev(G.y)
    G.z <- G.z[,length(G.y):1] <- open.ncdf("")
    # look what's in there...
    T.t <- get.var.ncdf(,'time')
    T.eta <-
  4. plot ...
    # R-package fields provides nice image facilities and color schemes
    par (mfrow = c(1,2))
            col = c(tim.colors(),"black"),
            xlab = "x [m]", ylab = "y [m]")
    plot(as.Date(T.t, origin="1970-01-01"), T.eta, type = "l", ylab = "spm
  5. et voila

Download the code of this R example (repos,manual download), which was provided by Karline Soetaert and Tom van Engeland.

See also: Accessing netCDF/OPeNDAP data with python, Accessing netCDF/OPeNDAP data with Matlab, PostgreSQL access with R, OPeNDAP subsetting with R