Learn R Programming

rNOMADS (version 1.2.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, levels, variables)

Arguments

file.name
The path and file name of the grib file to read.
levels
The levels to extract.
variables
The variables 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
#An example for the Global Forecast System 0.5 degree model

#Get the latest model url
urls.out <- CrawlModels(abbrev = "gfs0.5", depth = 1)

#Get a list of forecasts, variables and levels
model.parameters <- ParseModelPage(urls.out[1])

#Figure out which one is the 6 hour forecast
#provided by the latest model run
#(will be the forecast from 6-12 hours from the current date) 

my.pred <- model.parameters$pred[grep("06$", model.parameters$pred)]

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

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

#Get the data
file.name <- GribGrab(urls.out[1], my.pred, levels, variables)

#Extract the data
model.data <- ReadGrib(file.name, levels, variables)

#Reformat it
model.grid <- ModelGrid(model.data)

#Show an image of world temperature at ground level
image(model.grid$z[2, 1,,])

Run the code above in your browser using DataLab