Learn R Programming

rNOMADS (version 1.0.0)

ReadGrib: Extract data from grib file

Description

This function wraps wgrib2, an external grib file reader provided by the National Weather Service Climate Prediction Center (see http://www.cpc.ncep.noaa.gov/products/wesley/wgrib2/). ReadGrib extracts forecast data for the specified variables and levels into R in CSV format. It does this by building an argument string, executing a system call to wgrib2, and extracting the result. Note that wgrib2 must be installed for ReadGrib to work.

Usage

ReadGrib(file.name, variables, levels)

Arguments

file.name
The path and file name of the grib file to read.
variables
The variables to extract.
levels
The levels to extract.

Value

  • An array containing wgrib2 output, with headers:
  • model.run.dateWhen the model was computed (GMT)
  • forecast.dateThe forecast date (GMT)
  • variableThe data type
  • levelWhat vertical level the data is located in
  • lonLongitude value of grid point
  • latThe latitude value of grid point
  • valueThe value of the given variable, at the given level, at the location defined by lon and lat

Details

This function constructs a system call to wgrib2. Therefore, you must have installed wgrib2 and made it available on the system path. A description of wgrib2 and installation links are available at http://www.cpc.ncep.noaa.gov/products/wesley/wgrib2/. Note that Windows users will likely have to use a UNIX emulator like Cygwin to install wgrib2.

References

Ebisuzaki, W, Bokhorst, R., Hyvatti, J., Jovic, D., Nilssen, K, Pfeiffer, K., Romero, P., Schwarb, M., da Silva, A., Sondell, N., and Varlamov, S. (2011). wgrib2: read and write GRIB2 files. National Weather Service Climate Prediction Center, http://www.cpc.ncep.noaa.gov/products/wesley/wgrib2/

See Also

GribGrab, ModelGrid

Examples

Run this code
#Get the latest model date
model.date <- Sys.time()

#Get the forecast 6 hours from now - addition is defined in seconds
fcst.date <- model.date + 6 * 3600

#What region of the atmosphere to get data for
#2 meters above the ground and at 800 mb pressure
levels <- c("2 m above ground", "800 mb")

#What data to return
variables <- c("TMP", "RH") #Temperature and relative humidity

#What region of the world to get data for - omit this and you get the whole planet!
model.domain <- c(-84, -74, 37, 32) #Get the area around North Carolina, USA

file.name <- "fcst.grb" #Name of file to save downloaded data to

#Get the data
GribGrab(levels, variables, model.date = model.date,
    fcst.date = fcst.date, model.domain = model.domain, file.name = file.name)

#Read the data, only capturing temperature at the 2 m above ground level
model.data <- ReadGrib(file.name, c("TMP"), c("2 m above ground"))

#Make it into an array
model.array <- ModelGrid(model.data, 0.5, 0.5)

#Make a contour plot of the temperature around North Carolina, USA:
contour(x = model.array$x, y = model.array$y, model.array$z[1,1,,] - 273.15, 
    xlab = "Latitude", ylab = "Longitude", 
   main = paste("North Carolina Temperatures for", 
   model.array$fcst.date, "GMT in Celsius"))

Run the code above in your browser using DataLab