Learn R Programming

rNOMADS (version 2.4.1)

ReadGrib: Extract data from grib files

Description

This function wraps wgrib2 and wgrib, external grib file readers provided by the National Weather Service Climate Prediction Center (see http://www.cpc.ncep.noaa.gov/products/wesley/wgrib2/ and http://www.cpc.ncep.noaa.gov/products/wesley/wgrib.html). ReadGrib extracts forecast data into R. It does this by building an argument string, executing a system call to the appropriate external grib file reader, and extracting the result. Note that wgrib2 must be installed for ReadGrib to work for current grib files, and wgrib may need to be installed when looking at archived data.

Usage

ReadGrib(file.names, levels, variables, 
    forecasts = NULL, domain = NULL, domain.type = "latlon", 
file.type = "grib2", missing.data = NULL)

Arguments

file.names

The path and file name of the grib files to read.

levels

The levels to extract.

variables

The variables to extract.

forecasts

Names of forecasts to extract. If NULL, include everything.

domain

Include model nodes in the specified region: c(LEFT LON, RIGHT LON, NORTH LAT, SOUTH LAT). If NULL, include everything. This argument works for GRIB2 only.

domain.type

Either \"latlon\" (the domain argument is a latitude/longitude quadruplet) or \"index\" (the domain argument is a model node index quadruplet; much faster but requires you to know the model setup)

file.type

Whether the file is in GRIB ("grib1") or GRIB2 ("grib2") format. Default is "grib2".

missing.data

Replace missing data in grib archive with this value. If NULL, leave the data out. Only works with wgrib2. Default NULL.

Value

model.data

A structure with a series of elements containing data extracted from the grib files.

Details

This function constructs system calls to wgrib and wgrib2. Therefore, you must have installed these programs and made it available on the system path. Unless you are interested in accessing archive data that's more than a few years old, you can install wgrib2 only. A description of wgrib2 and installation links are available at http://www.cpc.ncep.noaa.gov/products/wesley/wgrib2/ and http://www.cpc.ncep.noaa.gov/products/wesley/wgrib.html. Also, rNOMADS is focused towards GRIB2 files; I have included GRIB1 format support as a convenience.

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, ArchiveGribGrab, ModelGrid

Examples

Run this code
# NOT RUN {
#Operational Forecast Data Extraction
#NCEP output is always in GRIB2 format - this makes things easy for us
#An example for the Global Forecast System 0.5 degree model

#Get the latest model url
# }
# NOT RUN {
urls.out <- CrawlModels(abbrev = "gfs_0p50", 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
model.info <- GribGrab(urls.out[1], my.pred, levels, variables)

#Extract the data
model.data <- ReadGrib(model.info[[1]]$file.name, levels, variables)

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

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

# }

Run the code above in your browser using DataLab