Extract data from grib file

Share:

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

1
2
ReadGrib(file.name, levels, variables, 
    domain = NULL, file.type = "grib2", missing.data = NULL)

Arguments

file.name

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

levels

The levels to extract.

variables

The variables to extract.

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.

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.

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.

Value

model.data

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

Author(s)

Daniel C. Bowman daniel.bowman@unc.edu

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#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$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,,])


#Archived Data Extraction
#This is sometimes in GRIB1 format
#This example is in GRIB1

abbrev <- "gfsanl"
model.date <- 20040302 #March 2, 2004
model.run <- 18 #1800 GMT model run
pred <- 0 #Analysis

grib.info <- ArchiveGribGrab(abbrev, model.date, model.run,
    pred, file.type = "grib1")

model.data <- ReadGrib(grib.info$file.name, c("1000 mb"), c("TMP"), 
    file.type = "grib1")

## End(Not run)