Skip to content

Commit

Permalink
Merge pull request #4 from adamhsparks/devel
Browse files Browse the repository at this point in the history
Update from level
  • Loading branch information
adamhsparks authored Sep 4, 2019
2 parents 56fc01f + 42bced1 commit e7332de
Show file tree
Hide file tree
Showing 43 changed files with 1,884 additions and 806 deletions.
2 changes: 2 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@ vignettes/figure
^_pkgdown\.yml$
^\.circleci$
^GSODR.code-workspace$
^doc$
^Meta$
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ inst/doc

generate.rb
*.code-workspace
doc
Meta
10 changes: 10 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
dist: xenial
language: r
cache: packages
r_check_args: --as-cran --run-donttest
Expand All @@ -10,6 +11,15 @@ matrix:
- os: linux
r: devel

addons:
apt:
sources:
- sourceline: 'ppa:ubuntugis/ppa'
packages:
- libudunits2-dev
- libproj-dev
- libgdal-dev

after_failure:
- ./pkg-build.sh dump_logs

Expand Down
8 changes: 2 additions & 6 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ Authors@R: c(person("Adam", "Sparks", role = c("aut", "cre"),
person("Hugh", "Parsonage", role = c("cph", "ctb"),
email = "[email protected]",
comment = c(ORCID = "0000-0003-4055-0835")),
person("Bob", "Rudis", email = "[email protected]", role = c("cph", "ctb"),
comment = c(ORCID = "0000-0001-5670-2640")),
person("Gwenael", "Giboire", email = "[email protected]",
role = "ctb",
comment = "Several bug reports in early versions and testing feedback"),
Expand Down Expand Up @@ -46,6 +44,7 @@ Depends:
R (>= 3.3.0)
License: MIT + file LICENSE
Imports:
countrycode,
curl,
data.table (>= 1.11.6),
future.apply,
Expand All @@ -56,22 +55,19 @@ Imports:
Suggests:
covr,
dplyr,
DT,
future,
ggplot2,
gridExtra,
ggthemes,
knitr,
mapproj,
maps,
plotKML,
raster,
reshape2,
rgdal (>= 1.1-9),
rgeos,
rmarkdown,
roxygen2 (>= 6.1.0),
sf,
skimr,
sp,
spacetime,
testthat,
Expand Down
10 changes: 7 additions & 3 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,22 @@ data could not be downloaded any longer

## Major changes

- Corrected elevation values are no longer available from GSODR, this makes
package updates much easier
- Corrected elevation values are no longer available from GSODR

- Objects are returned as `data.table` objects

## Minor changes

- `get_inventory()` now uses https rather than FTP server

- `update_station_list()` now uses https rather than FTP server

- Implement better error handling when attempting to fetch station inventories

- Reduced package dependencies

- Improved vignettes
- Improved vignettes that are pre-compiled for faster package installation and
updated content with linting and error corrections

- Users may now specify country by FIPS code when using `get_GSOD()`

Expand Down
22 changes: 8 additions & 14 deletions R/get_GSOD.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@
#'
#' tbar
#'
#' # Download data for Australia from 2010 to 2015
#' AUS <- get_GSOD(years = 2010:2015, country = "Australia")
#' # Download data for Australia from 2010 to 2011
#' AUS <- get_GSOD(years = 2010:2011, country = "Australia")
#'
#' AUS
#' }
Expand All @@ -94,10 +94,6 @@ get_GSOD <- function(years,
country = NULL,
max_missing = NULL,
agroclimatology = FALSE) {
# Create objects for use in retrieving files ---------------------------------
original_timeout <- options("timeout")[[1]]
options(timeout = 300)
on.exit(options(timeout = original_timeout))

# Validate user inputs -------------------------------------------------------
.validate_years(years)
Expand Down Expand Up @@ -129,6 +125,7 @@ get_GSOD <- function(years,

# Load station list
load(system.file("extdata", "isd_history.rda", package = "GSODR")) # nocov
setkeyv(isd_history, "STNID")

# Validate user entered stations for existence in stations list from NCEI
invisible(lapply(
Expand All @@ -152,17 +149,14 @@ get_GSOD <- function(years,
if (!is.null(country)) {
# Load country list
# CRAN NOTE avoidance
country_list <- NULL # nocov
load(system.file("extdata", "country_list.rda", package = "GSODR")) # nocov

country <- .validate_country(country, country_list)
country <- .validate_country(country, isd_history)

file_list <-
.subset_country_list(country,
country_list,
file_list,
isd_history,
years)
.subset_country_list(country = country,
isd_history = isd_history,
file_list = file_list,
years = years)
}

# Validate stations for missing days -----------------------------------------
Expand Down
16 changes: 3 additions & 13 deletions R/get_inventory.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#' Download and return a data frame of \acronym{GSOD} weather station data inventories
#'
#' The \acronym{NCEI} maintains a document,
#' \url{ftp://ftp.ncdc.noaa.gov/pub/data/noaa/isd-inventory.txt}, which lists
#' \url{https://www1.ncdc.noaa.gov/pub/data/noaa/isd-inventory.txt}, which lists
#' the number of weather observations by station-year-month from the beginning
#' of the stations' records. This function retrieves that document and prints
#' an information header displaying the last update time with a data frame of
Expand All @@ -28,21 +28,11 @@
get_inventory <- function() {
load(system.file("extdata", "isd_history.rda", package = "GSODR")) #nocov

ftp_handle <-
curl::new_handle(
ftp_use_epsv = FALSE,
crlf = TRUE,
ssl_verifypeer = FALSE,
ftp_response_timeout = 30,
ftp_skip_pasv_ip = TRUE
)

tryCatch({
curl::curl_download(
"ftp://ftp.ncdc.noaa.gov/pub/data/noaa/isd-inventory.txt",
"https://www1.ncdc.noaa.gov/pub/data/noaa/isd-inventory.txt",
destfile = file.path(tempdir(), "inventory.txt"),
quiet = TRUE,
handle = ftp_handle
quiet = TRUE
)

"STNID" <- NULL #nocov
Expand Down
40 changes: 19 additions & 21 deletions R/internal_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@
paste0(station),
" is not a valid station ID number, please check your entry.\n",
"Valid Station IDs can be found in the isd-history.txt file\n",
"available from the US NCEI FTP server by combining the USAF and\n",
"available from the US NCEI server by combining the USAF and\n",
"WBAN columns, e.g. '007005' '99999' is '007005-99999' from this\n",
"file <ftp://ftp.ncdc.noaa.gov/pub/data/noaa/isd-history.txt>\n"
"file <https://www1.ncdc.noaa.gov/pub/data/noaa/isd-history.txt>\n"
)
}
BEGIN <-
Expand All @@ -64,43 +64,43 @@
#' Validate Country Requests
#'
#' @param country User requested country name
#' @param country_list country_list file from NCEI provided by GSODR
#' @param isd_history Data provided from NCEI on stations' locations and years
#' @keywords internal
#' @return A validated country name
#' @noRd

.validate_country <-
function(country, country_list) {
function(country, isd_history) {
if (!is.null(country)) {
country <- toupper(trimws(country[1]))
nc <- nchar(country)
if (nc == 3) {
if (country %in% country_list$ISO3C) {
c <- which(country == country_list$ISO3C)
country <- country_list[[c, 1]]
if (country %in% isd_history$ISO3C) {
c <- which(country == isd_history$ISO3C)
country <- as.character(isd_history[c, "CTRY"][1])
} else {
stop(call. = FALSE,
"\nPlease provide a valid name or 2 or 3",
"\nPlease provide a valid name or 2 or 3 ",
"letter ISO country code\n")
}
} else if (nc == 2) {
if (country %in% country_list$ISO2C) {
c <- which(country == country_list$ISO2C)
country <- country_list[[c, 1]]
} else if (country %in% country_list$FIPS) {
c <- which(country == country_list$FIPS)
country <- country_list[[c, 1]]
if (country %in% isd_history$ISO2C) {
c <- which(country == isd_history$ISO2C)
country <- as.character(isd_history[c, "CTRY"][1])
} else if (country %in% isd_history$CTRY) {
c <- which(country == isd_history$CTRY)
country <- as.character(isd_history[c, "CTRY"][1])
} else {
stop(call. = FALSE,
"\nPlease provide a valid name or 2 or 3",
"\nPlease provide a valid name or 2 or 3 ",
"\nletter ISO country code")
}
} else if (country %in% country_list$COUNTRY_NAME) {
c <- which(country == country_list$COUNTRY_NAME)
country <- country_list[[c, 1]]
} else if (country %in% isd_history$COUNTRY_NAME) {
c <- which(country == isd_history$COUNTRY_NAME)
country <- as.character(isd_history[c, "CTRY"][1])
} else {
stop(call. = FALSE,
"\nPlease provide a valid name or 2 or 3",
"\nPlease provide a valid name or 2 or 3 ",
"letter ISO country code\n")
}
}
Expand Down Expand Up @@ -263,7 +263,6 @@
#' Subset Country List
#'
#' @param country Country of interest to subset on
#' @param country_list Country list file provided by NCEI as a part of GSODR
#' @param GSOD_list List of GSOD files to be subset
#' @param isd_history isd_history.csv file from NCEI provided by GSODR
#' @param years Years being requested
Expand All @@ -273,7 +272,6 @@

.subset_country_list <-
function(country,
country_list,
file_list,
isd_history,
years) {
Expand Down
4 changes: 3 additions & 1 deletion R/process_csv.R
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@

# Add STNID col --------------------------------------------------------------
DT[, STNID := gsub('^(.{6})(.*)$', '\\1-\\2', DT$STATION)]
setkey(DT, "STNID")

# Add and convert date related columns ---------------------------------------
DT[, YEARMODA := as.Date(DATE, format = "%Y-%m-%d")]
Expand Down Expand Up @@ -159,6 +158,7 @@
DT[, FRSHTT := NULL]

# Join with internal isd-history for CTRY column -----------------------------
setkey(DT, STNID)
DT <- isd_history[DT]

# drop extra cols
Expand Down Expand Up @@ -215,5 +215,7 @@
"RH"
)
)
# drop extra cols before returning object
DT[, c("COUNTRY_NAME", "ISO2C", "ISO3C") := NULL]
return(DT)
}
1 change: 1 addition & 0 deletions R/reformat_GSOD.R
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
reformat_GSOD <- function(dsn = NULL, file_list = NULL) {
isd_history <- NULL # nocov
load(system.file("extdata", "isd_history.rda", package = "GSODR")) # nocov
setkeyv(isd_history, "STNID")

# If dsn !NULL, create a list of files to reformat
if (!is.null(dsn)) {
Expand Down
Loading

0 comments on commit e7332de

Please sign in to comment.