Skip to content

Commit

Permalink
version 1.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
dannycbowman authored and cran-robot committed Mar 17, 2014
1 parent 19d5aa9 commit 8521950
Show file tree
Hide file tree
Showing 25 changed files with 1,254 additions and 279 deletions.
16 changes: 8 additions & 8 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ Package: rNOMADS
Type: Package
Title: An interface to the NOAA Operational Model Archive and
Distribution System
Version: 1.2.0
Date: 2013-10-28
Version: 1.3.0
Date: 2014-03-17
Authors@R: c(person(given="Daniel C.",family="Bowman", role=c("aut", "cre"),
email="daniel.bowman@unc.edu"))
Depends: R (>= 3.0.2), scrapeR (>= 0.1.6), stringr (>= 0.6.2), fields
(>= 6.7.6)
Suggests: GEOmap (>= 2.1)
Description: An interface to the National Oceanic and Atmospheric Administration's Operational Model Archive and Distribution System (NOMADS) that allows R users to quickly and efficiently download global and regional weather model data for processing. rNOMADS currently supports 54 models, ranging from global weather data to an altitude of 40 km, to high resolution regional weather models, to wave and sea ice models. rNOMADS uses an external series of routines called wgrib2 to read the model data into R; get wgrib2 at http://www.cpc.ncep.noaa.gov/products/wesley/wgrib2/. Future versions of rNOMADS will have a native data reader.
Depends: R (>= 3.0.2)
Imports: scrapeR (>= 0.1.6), stringr (>= 0.6.2), fields (>= 6.7.6), XML
(>= 3.98.1.1), GEOmap (>= 2.1), MBA
Description: An interface to the National Oceanic and Atmospheric Administration's Operational Model Archive and Distribution System (NOMADS) that allows R users to quickly and efficiently download global and regional weather model data for processing. rNOMADS currently supports 54 models, ranging from global weather data to an altitude of 40 km, to high resolution regional weather models, to wave and sea ice models. It can also retrieve archived NOMADS models. rNOMADS uses an external series of routines called wgrib2 to read real time model data into R; this must be downloaded and installed separately. rNOMADS also offers limited support to reading archived data in GRIB1 format via the wgrib utility, which also must be downloaded and installed separately. Future versions of rNOMADS will have a native data reader.
License: GPL (>= 3)
URL: http://code.google.com/p/bovine-aerospace/ (subversion repository)
http://www.unc.edu/~haksaeng/rNOMADS/rNOMADS_examples.pdf (some
examples)
Packaged: 2013-10-28 23:38:13 UTC; dantayaga
Packaged: 2014-03-17 18:34:03 UTC; bowman
Author: Daniel C. Bowman [aut, cre]
Maintainer: Daniel C. Bowman <daniel.bowman@unc.edu>
NeedsCompilation: no
Repository: CRAN
Date/Publication: 2013-10-29 07:58:07
Date/Publication: 2014-03-17 20:36:05
37 changes: 22 additions & 15 deletions MD5
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
46e5c9cbcf08752ff6889c82fd436870 *DESCRIPTION
8c4ca1b8a830e7b29c151810092fa1dd *DESCRIPTION
8b54e5a89fbda3af5e077053d40bec76 *NAMESPACE
aea696128a88fab764070b5b96000ec2 *NEWS
1d5d5c7b5cf99238004990dadaa92fc1 *R/GetGrib.R
e2844f1cc97f8b247164e9e984f28b07 *R/ReadGrib.R
9a1d5acc515f113ba7fc071fcb1a8373 *data/GFS.RData
ecc05a650bf8a9bb66d6e0b304fa84fb *man/CrawlModels.Rd
511c5cf354d8a6783a673091a901512c *man/GribGrab.Rd
53e83b1d9db682e4c9a8ae109b0dfc51 *man/ModelGrid.Rd
72a485c026302c53600129123e88f3d4 *man/NOMADSList.Rd
31934edbe61a9df8adce91ef50efa283 *man/ParseModelPage.Rd
e015ffdf22e6871643da5f05fc6a66be *man/ReadGrib.Rd
20a8f0d15931f4c9d24b9a657b434928 *man/SanitizeURL.Rd
259a9a6904923c821401ca8f75c0ed3c *man/WebCrawler.Rd
0dd3d7a5e714839bbd56fc366c3fbc66 *man/gfs.model.data.Rd
0eb6d62b59fa45a9c4d74e049d45725d *man/rNOMADS-package.Rd
b50009ef2cc56c7d1bad14831729bed0 *NEWS
ac93b34428aac067fdc392c12d220f37 *R/GetArchiveGrib.R
dcd0720b3c62464ca2bc9447889cd768 *R/GetRealTimeGrib.R
ba7932f404dcae6431101e73585016dc *R/RNomadsTools.R
0deae0d6acab221d7826e814acc60a43 *R/ReadGrib.R
70c6eb675cc7409c07bf90e101abeb22 *man/ArchiveGribGrab.Rd
2164ae225fd14611d99250048e54cb86 *man/BuildProfile.Rd
c3db37c6ee117bd69480cd15c7ce8396 *man/CheckNOMADSArchive.Rd
44d744f08fe9734affcc51396855f238 *man/CrawlModels.Rd
8e20e8969efe86b8a0a1634f31ba6144 *man/GetClosestGFSForecasts.Rd
57c03683469f2d353a74114092c83aa4 *man/GribGrab.Rd
43004dfb2d645efdba52442e8e94bea1 *man/MagnitudeAzimuth.Rd
a6864f75ec5d7bd091e2de80e85b1eaa *man/ModelGrid.Rd
0e31dc41201c09c3cc466b093af65c21 *man/NOMADSArchiveList.Rd
ed761c567d03cbb8d62b20035466a099 *man/NOMADSRealTimeList.Rd
279fb81a8200494513fce358a350bbbf *man/ParseModelPage.Rd
cc1c63a4baeb4844db1244532442e186 *man/RTModelProfile.Rd
379b58c58374e25e8026a31ae5ef486b *man/ReadGrib.Rd
6a2249cbb7db7acd1dcfad27a924b3cd *man/SanitizeURL.Rd
d0f6d0412e8764aa61fa837812c61d28 *man/WebCrawler.Rd
f866da8ebf4f0038cf36b7297c265479 *man/rNOMADS-package.Rd
4 changes: 4 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@ This has necessitated major changes to some functions.
See the supplied vignette link on the rNOMADS page on CRAN for examples of how the new rNOMADS code works.
The lack of a good GRIB file format reader in R is still vexing, and I plan to write a small package to take care of this.
A future version of rNOMADS will call this package to remove the necessity of installing third party software.

1.3-0
Added support for archived model data, and the ability to read in GRIB1 files using the external "wgrib" series of codes.
Also added some nice tools to quickly generate atmospheric profiles and so forth.
201 changes: 201 additions & 0 deletions R/GetArchiveGrib.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
ArchiveGribGrab <- function(abbrev, model.date, model.run, pred, local.dir = ".", file.name = "fcst.grb",
tidy = FALSE, verbose = TRUE, download.method = NULL, file.type = "grib2") {
#Get archived grib file
#INPUTS
# ABBREV - Model abbreviation returned by NOMADSArchiveList
# MODEL.DATE - The year. month, and day of the model run, in YYYYMMDD format
# MODEL.RUN - Which hour the model was run (i.e. 00, 06, 12, 18 for GFS)
# PRED - Which prediction to get (analysis is 00)
# LOCAL.DIR is the directory to save the files in
# FILE.NAME is the directory path and file name to save the grib file on disk, defaults to "fcst.grb" in current directory
# TIDY asks whether to delete all grib files in the directory specified in FILE.NAME, default FALSE.
# This is useful to clear out previous model runs.
# It looks for all files named '.grb' and removes them.
# VERBOSE gives a blow by blow account of the download. Default TRUE.
# DOWNLOAD.METHOD allows the user to set the download method used by download.file
# FILE.TYPE specifies which file type to try and get - there may be only one available.
# grib1 for GRIB1, grib2 for GRIB2
#OUTPUTS
# GRIB.INFO contains information about the downloaded file
# $LOCAL.DIR is the directory where the grib file is saved
# $FILE.NAME is the local file name where the data is stored
# $URL is the URL from which the file was downloaded

if(tidy) {
unlink(list.files(local.dir, pattern = "*\\.grb$"))
}

model.date <- as.numeric(strsplit(as.character(model.date), split = "")[[1]])

if(length(model.date) != 8) {
stop("MODEL.DATE should be in YYYYMMDD format!")
}

#Get specified file format
if(file.type == "grib1") {
suffix <- ".grb"
} else if (file.type == "grib2") {
suffix <- ".grb2"
} else {
stop(paste0("Did not recognize file type \"", file.type, "\""))
}

#Get model info and set up URL to archive
model.url <- NOMADSArchiveList(abbrev)$url
download.url <- paste0(model.url, paste(model.date[1:6], collapse = ""), "/", paste(model.date, collapse = ""), "/")
file.part <- paste0(paste(model.date, collapse = ""), "_", sprintf("%02.f", model.run), "00_", sprintf("%03.f", pred), suffix)

#Find out which grib files are in the archive
doc <- XML::htmlParse(download.url)
link.list <- unique(XML::xpathSApply(doc, "//a/@href"))
XML::free(doc)


#Check if the requested file is where we think it is
if(sum(grepl(paste0(".*", file.part, "$"), link.list)) < 1) {
stop(paste("The requested data file ending in", file.part, "does not appear to be in the archive.
Try opening", download.url, "in a web browser to verify that it's missing."))
}

#Set up URL to file
grb.urls <- paste0(download.url, link.list[grepl(paste0(".*", file.part, "$"), link.list)])

#Download the file
if(length(grb.urls) > 1) {
warning("Two files were found: ", paste(link.list[grepl(paste0(".*", file.part, "$"), link.list)], collapse = " "), ". Both will be downloaded.")
c <- 1
} else {
c <- ""
}

use.curl <- FALSE #May use this as an option in the future
file.names <- NULL
for(grb.url in grb.urls) {
file.name.tmp <- paste0(file.name, c)
if(is.null(download.method) & !use.curl) {#Let R decide how to download the file
download.file(grb.url, paste(local.dir,file.name.tmp, sep = "/"), mode = "wb", quiet = !verbose)
}
if(!is.null(download.method) & !use.curl) { #Download using specific method
download.file(grb.url, paste(local.dir,file.name.tmp, sep = "/"), download.method, mode = "wb", quiet = !verbose)
}
file.names <- append(file.names, file.name.tmp)
if(c != "") {
c <- c + 1
}
}
grib.info <- list(local.dir = normalizePath(local.dir), file.name = file.names, url = grb.urls)
return(grib.info)
}

CheckNOMADSArchive <- function(abbrev, model.date = NULL) {
#Determine what data are available in the archive for a given model.
#If MODEL.DATE is NULL, find out what dates have directories (and probably data).
#IF MODEL.DATE is a valid date, find out what files are available for that specific directory.
#INPUTS
# ABBREV is the model abbreviation that rNOMADS uses to figure out which model you want.
# MODEL.DATE is a specific date in YYYYMMMDD format - if this is not NULL, figure out what model files are available for that date
#OUTPUTS
# AVAILABLE.MODELS
# $DATE - What date the file is for, in YYYYMMDD format
# $MODEL.RUN - At which hour the model was run (GMT)
# $PRED - What predictions are available
# $FILE.NAME - The grib file with model data for the date, model run, and prediction

model.url <- NOMADSArchiveList(abbrev)$url
model.list <- c()
if(is.null(model.date)) { #Check all available dates
#Find out which months are available
doc <- XML::htmlParse(model.url)
month.list <- grep("\\d{6}/", XML::xpathSApply(doc, "//a/@href"), value = TRUE)
XML::free(doc)
for(month in month.list) {
doc <- XML::htmlParse(paste0(model.url, month))
date.list <- grep("\\d{8}/", XML::xpathSApply(doc, "//a/@href"), value = TRUE)
XML::free(doc)
for(day in date.list) {
doc <- XML::htmlParse(paste0(model.url, month, day))
model.list <- append(model.list, grep("grb\\d?$", XML::xpathSApply(doc, "//a/@href"), value = TRUE))
XML::free(doc)
}
}
} else {
model.date <- as.numeric(strsplit(as.character(model.date), split = "")[[1]])
doc <- XML::htmlParse(paste0(model.url, paste(model.date[1:6], collapse = ""), "/", paste(model.date, collapse = ""), "/"))
model.list <- append(model.list, grep("grb\\d?$", XML::xpathSApply(doc, "//a/@href"), value = TRUE))
XML::free(doc)
}

model.list <- as.vector(model.list)

available.models <- list(
date = stringr::str_extract(model.list, "\\d{8}"),
model.run = stringr::str_replace_all(stringr::str_extract(model.list, "_\\d{4}_"), "_", ""),
pred = stringr::str_replace(stringr::str_replace(stringr::str_extract(model.list, "_\\d{3}\\."), "\\.", ""), "_", ""),
file.name = model.list)
invisible(available.models)

}
NOMADSArchiveList <- function(abbrev = NULL) {
#Returns a list of model abbreviations for archived models, a short description, and URL for each model offered by the NOMADS server
#If a specific model abbreviation is requested, the abbreviation is checked against the model list.
#If a match is found, information is returned about that model; otherwise an error occurs
#INPUTS
# ABBREV is the model abbreviation that rNOMADS uses to figure out which model you want.
# If NULL, returns information on all models
#OUTPUTS
# MODEL.LIST - a list of model metadata with elements
# $ABBREV - the abbrevation used to call the model in rNOMADS
# $NAME - the name of the model
# $URL - the location of the model on the NOMADS website

abbrevs <- c(
"ruc20",
"ruc13",
"nam-nrt",
"gfs-nrt1.0",
"gfs-nrt0.5",
"rap-nrt20",
"rap-nrt13",
"gfs-anl",
"ruc-anl",
"nam-anl"
)

names <- c(
"Rapid Update Cycle 20 km grid",
"Rapid Update Cycle 13 km grid",
"North American Mesoscale, Near Real Time",
"Global Forecast System, Near Real Time, 1 degree grid",
"Global Forecast System, Near Real Time, 0.5 degree grid",
"Rapid Refresh Weather Prediction System - Near Real Time, 20 km grid",
"Rapid Refresh Weather Prediction System - Near Real Time, 13 km grid",
"Global Forecast System, Analysis",
"Rapid Update Cycle, Analysis",
"North American Mesoscale, Analysis"
)

urls <- c(
"http://nomads.ncdc.noaa.gov/data/ruc/",
"http://nomads.ncdc.noaa.gov/data/ruc13",
"http://nomads.ncdc.noaa.gov/data/meso-eta-hi/",
"http://nomads.ncdc.noaa.gov/data/gfs-avn-hi/",
"http://nomads.ncdc.noaa.gov/data/gfs4/",
"http://nomads.ncdc.noaa.gov/data/rap252/",
"http://nomads.ncdc.noaa.gov/data/rap130/",
"http://nomads.ncdc.noaa.gov/data/gfsanl/",
"http://nomads.ncdc.noaa.gov/data/rucanl/",
"http://nomads.ncdc.noaa.gov/data/namanl/"
)

if(!is.null(abbrev)) {
i <- which(abbrevs == abbrev)
if(length(i) == 0) {
stop(paste("The model you searched for:\"", abbrev, "\"is not included in rNOMADS. Sorry!"))
} else {
return(list(abbrev = abbrev, name = names[i], url = urls[i]))
}
}

return(list(abbrevs = abbrevs, names = names, urls = urls))
}

0 comments on commit 8521950

Please sign in to comment.