Skip to content

Commit

Permalink
changed isd() fxn to give back tibble instead of daata.frame in a list
Browse files Browse the repository at this point in the history
fixed tests for both isd and ghcnd to do expectations right for new outputs
bumped dev version
  • Loading branch information
sckott committed Sep 2, 2016
1 parent 8a753cb commit 201ad62
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 84 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Description: Client for many 'NOAA' data sources including the 'NCDC' climate
for 'NOAA' sea ice data, the 'NOAA' severe weather inventory, 'NOAA' Historical
Observing 'Metadata' Repository ('HOMR') data, 'NOAA' storm data via 'IBTrACS',
tornado data via the 'NOAA' storm prediction center, and more.
Version: 0.6.0.9331
Version: 0.6.0.9333
License: MIT + file LICENSE
Encoding: UTF-8
Authors@R: c(
Expand Down
2 changes: 0 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ S3method(autoplot,meteo_coverage)
S3method(ncdc_plot,default)
S3method(ncdc_plot,ncdc_data)
S3method(print,buoy)
S3method(print,isd)
S3method(print,storm_data)
S3method(print,storm_shp)
S3method(type_summ,Date)
Expand Down Expand Up @@ -93,7 +92,6 @@ export(noaa_locs_cats)
export(noaa_plot)
export(noaa_seaice)
export(noaa_stations)
export(rbind.isd)
export(readshpfile)
export(seaice)
export(seaiceeurls)
Expand Down
68 changes: 30 additions & 38 deletions R/isd.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,39 @@
#' \code{~/.rnoaa/isd}. Required.
#' @param overwrite (logical) To overwrite the path to store files in or not,
#' Default: \code{TRUE}
#' @param cleanup (logical) If \code{TRUE}, remove compressed \code{.gz} file at end of
#' function execution. Processing data takes up a lot of time, so we cache a cleaned version
#' of the data. Cleaning up will save you on disk space. Default: \code{TRUE}
#' @param cleanup (logical) If \code{TRUE}, remove compressed \code{.gz} file
#' at end of function execution. Processing data takes up a lot of time, so we
#' cache a cleaned version of the data. Cleaning up will save you on disk
#' space. Default: \code{TRUE}
#' @param ... Curl options passed on to \code{\link[httr]{GET}}
#'
#' @references ftp://ftp.ncdc.noaa.gov/pub/data/noaa/
#' @seealso \code{\link{isd_stations}}
#'
#' @details This function first looks for whether the data for your specific query has
#' already been downloaded previously in the directory given by the \code{path}
#' parameter. If not found, the data is requested form NOAA's FTP server. The first time
#' a dataset is pulled down we must a) download the data, b) process the data, and c) save
#' a compressed .rds file to disk. The next time the same data is requested, we only have
#' to read back in the .rds file, and is quite fast. The benfit of writing to .rds files
#' is that data is compressed, taking up less space on your disk, and data is read back in
#' quickly, without changing any data classes in your data, whereas we'd have to jump
#' through hoops to do that with reading in csv. The processing can take quite a long time
#' since the data is quite messy and takes a bunch of regex to split apart text strings.
#' We hope to speed this process up in the future. See examples below for different behavior.
#' @details This function saves the full set of weather data for the queried
#' site locally in the directory specified by the \code{path} argument.
#'
#' You can access the path for the cached file via \code{attr(x, "source")}
#'
#' @return A tibble (data.frame).
#'
#' @details This function first looks for whether the data for your specific
#' query has already been downloaded previously in the directory given by
#' the \code{path} parameter. If not found, the data is requested form NOAA's
#' FTP server. The first time a dataset is pulled down we must a) download the
#' data, b) process the data, and c) save a compressed .rds file to disk. The
#' next time the same data is requested, we only have to read back in the
#' .rds file, and is quite fast. The benfit of writing to .rds files is that
#' data is compressed, taking up less space on your disk, and data is read
#' back in quickly, without changing any data classes in your data, whereas
#' we'd have to jump through hoops to do that with reading in csv. The
#' processing can take quite a long time since the data is quite messy and
#' takes a bunch of regex to split apart text strings. We hope to speed
#' this process up in the future. See examples below for different behavior.
#'
#' @examples \dontrun{
#' # Get station table
#' stations <- isd_stations()
#' head(stations)
#' (stations <- isd_stations())
#'
#' ## plot stations
#' ### remove incomplete cases, those at 0,0
Expand Down Expand Up @@ -65,15 +74,14 @@
#' res3 <- isd(usaf="702700", wban="00489", year=2015)
#' res4 <- isd(usaf="109711", wban=99999, year=1970)
#' ## combine data
#' ### uses rbind.isd (all inputs of which must be of class isd)
#' res_all <- rbind(res1, res2, res3, res4)
#' library(dplyr)
#' res_all <- bind_rows(res1, res2, res3, res4)
#' # add date time
#' library("lubridate")
#' res_all$date_time <- ymd_hm(
#' sprintf("%s %s", as.character(res_all$date), res_all$time)
#' )
#' ## remove 999's
#' library("dplyr")
#' res_all <- res_all %>% filter(temperature < 900)
#' ## plot
#' library("ggplot2")
Expand All @@ -87,25 +95,9 @@ isd <- function(usaf, wban, year, path = "~/.rnoaa/isd", overwrite = TRUE, clean
isd_GET(bp = path, usaf, wban, year, overwrite, ...)
}
message(sprintf("<path>%s", rdspath), "\n")
structure(list(data = read_isd(x = rdspath, sections, cleanup)), class = "isd")
}

#' @export
#' @rdname isd
rbind.isd <- function(...) {
input <- list(...)
if (!all(sapply(input, class) == "isd")) {
stop("All inputs must be of class isd", call. = FALSE)
}
input <- lapply(input, "[[", "data")
bind_rows(input)
}

#' @export
print.isd <- function(x, ..., n = 10) {
cat("<ISD Data>", sep = "\n")
cat(sprintf("Size: %s X %s\n", NROW(x$data), NCOL(x$data)), sep = "\n")
trunc_mat_(x$data, n = n)
df <- read_isd(x = rdspath, sections, cleanup)
attr(df, "source") <- rdspath
df
}

isd_GET <- function(bp, usaf, wban, year, overwrite, ...) {
Expand Down
50 changes: 28 additions & 22 deletions man/isd.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 12 additions & 12 deletions tests/testthat/test-ghcnd.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,16 @@ test_that("get data", {
bb <- ghcnd(stationid = "AGE00135039")
cc <- ghcnd(stationid = "ASN00008264")

expect_is(aa, "ghcnd")
expect_is(bb, "ghcnd")
expect_is(cc, "ghcnd")

expect_is(aa$data, "data.frame")
expect_is(bb$data, "data.frame")
expect_is(cc$data, "data.frame")
expect_is(aa$data$element, "character")

expect_lt(NROW(cc$data), NROW(aa$data))
expect_lt(NROW(aa$data), NROW(bb$data))
expect_lt(NROW(cc$data), NROW(bb$data))
expect_is(aa, "tbl_df")
expect_is(bb, "tbl_df")
expect_is(cc, "tbl_df")

expect_is(aa, "data.frame")
expect_is(bb, "data.frame")
expect_is(cc, "data.frame")
expect_is(aa$element, "character")

expect_lt(NROW(cc), NROW(aa))
expect_lt(NROW(aa), NROW(bb))
expect_lt(NROW(cc), NROW(bb))
})
19 changes: 10 additions & 9 deletions tests/testthat/test-isd.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@ context("isd")

test_that("isd gets data", {
skip_on_cran()

data_a <- suppressMessages(isd(usaf = "011490", wban = "99999", year = 1986))
data_b <- suppressMessages(isd(usaf = "011490", wban = "99999", year = 1985))

expect_is(data_a, "isd")
expect_is(data_a$data, "data.frame")
expect_is(data_a$data$quality, "character")

expect_lt(NROW(data_a$data), NROW(data_b$data))

expect_is(data_a, "tbl_df")
# no longer a df in a list
expect_null(suppressWarnings(data_a$data))
expect_is(data_a$quality, "character")

expect_lt(NROW(data_a), NROW(data_b))
})

test_that("isd fails well", {
skip_on_cran()
expect_error(isd(usaf = "702700", wban = "489", year = 2014),

expect_error(isd(usaf = "702700", wban = "489", year = 2014),
"download failed for")
})

0 comments on commit 201ad62

Please sign in to comment.