Skip to content

Commit

Permalink
Merge branch 'devel'
Browse files Browse the repository at this point in the history
  • Loading branch information
adamhsparks committed Aug 22, 2023
2 parents c23e96a + 9729c2e commit 816dd70
Show file tree
Hide file tree
Showing 36 changed files with 696 additions and 602 deletions.
5 changes: 1 addition & 4 deletions .github/workflows/tic.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
config:
# use a different tic template type if you do not want to build on all listed platforms
- { os: windows-latest, r: "release" }
- { os: macOS-latest, r: "release", pkgdown: "true", latex: "true" }
- { os: macOS-latest, r: "release", pkgdown: "false", latex: "true" }
- { os: ubuntu-latest, r: "devel" }
- { os: ubuntu-latest, r: "release" }

Expand All @@ -39,9 +39,6 @@ jobs:
TIC_DEPLOY_KEY: ${{ secrets.TIC_DEPLOY_KEY }}
# prevent rgl issues because no X11 display is available
RGL_USE_NULL: true
# if you use bookdown or blogdown, replace "PKGDOWN" by the respective
# capitalized term. This also might need to be done in tic.R
BUILD_PKGDOWN: ${{ matrix.config.pkgdown }}
# use GITHUB_TOKEN from GitHub to workaround rate limits in {remotes}
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}

Expand Down
54 changes: 26 additions & 28 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,40 +1,41 @@
Package: GSODR
Type: Package
Title: Global Surface Summary of the Day ('GSOD') Weather Data Client
Version: 3.1.8.9000
Authors@R: c(person("Adam H.", "Sparks", role = c("aut", "cre"),
email = "[email protected]",
comment = c(ORCID = "0000-0002-0061-8359")),
Version: 3.1.9
Authors@R: c(
person("Adam H.", "Sparks", role = c("aut", "cre"),
email = "[email protected]",
comment = c(ORCID = "0000-0002-0061-8359")),
person("Tomislav", "Hengl", role = "aut",
email = "[email protected]",
comment = c(ORCID = "0000-0002-9921-5129")),
email = "[email protected]",
comment = c(ORCID = "0000-0002-9921-5129")),
person("Andrew", "Nelson", role = "aut",
email = "[email protected]",
comment = c(ORCID = "0000-0002-7249-3778")),
email = "[email protected]",
comment = c(ORCID = "0000-0002-7249-3778")),
person("Hugh", "Parsonage", role = c("cph", "ctb"),
email = "[email protected]",
comment = c(ORCID = "0000-0003-4055-0835")),
email = "[email protected]",
comment = c(ORCID = "0000-0003-4055-0835")),
person("Taras", "Kaduk", role = "ctb",
email = "[email protected]",
comment = "Suggestion for handling bulk station downloads more
efficiently"),
email = "[email protected]",
comment = "Suggestion for handling bulk station downloads more
efficiently"),
person("Gwenael", "Giboire", role = "ctb",
email = "[email protected]",
comment = "Several bug reports in early versions and testing feedback"),
email = "[email protected]",
comment = "Several bug reports in early versions and testing feedback"),
person("Łukasz", "Pawlik", role = "ctb", email = "[email protected]",
comment = "Reported bug in windspeed conversion calculation"),
comment = "Reported bug in windspeed conversion calculation"),
person("Ross","Darnell", role = "ctb",
email = "[email protected]", comment = c(
"Reported bug in 'Windows OS' versions causing 'GSOD' data untarring to
fail",
ORCID = "0000-0002-7973-6322")),
email = "[email protected]", comment = c(
"Reported bug in 'Windows OS' versions causing 'GSOD' data untarring to
fail",
ORCID = "0000-0002-7973-6322")),
person("Tyler", "Widdison", role = "ctb",
email = "[email protected]", comment = "Reported bug where
`nearest_stations()` did not return stations in order of nearest to
farthest"),
email = "[email protected]", comment = "Reported bug where
`nearest_stations()` did not return stations in order of nearest to
farthest"),
person(given = "Western Australia Agriculture Authority (WAAA)",
role = "cph", comment = c("Supported the development of 'GSODR' through
Adam H. Sparks's time.")))
role = "cph", comment = c("Supported the development of 'GSODR' through
Adam H. Sparks's time.")))
URL: https://docs.ropensci.org/GSODR/
BugReports: https://github.com/ropensci/GSODR/issues
Description: Provides automated downloading, parsing, cleaning, unit conversion
Expand All @@ -61,7 +62,6 @@ Imports:
countrycode,
curl,
data.table,
httr,
R.utils,
stats,
utils
Expand All @@ -73,9 +73,7 @@ Suggests:
knitr,
mapproj,
maps,
remotes,
rmarkdown,
roxygen2,
testthat,
tidyr
RoxygenNote: 7.2.3
Expand Down
14 changes: 13 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
# GSODR (development version)
# GSODR 3.1.9

## Bug fixes

- Fix duplicated elevation columns.

## Minor changes

- Remove {httr} as an Import. Use base functionality to check existence first and then {curl} will properly error if a download fails, we will use that functionality.

- Fix codecov badge to point at proper branch.

- Ensure function titles are all proper title case.

- Enhanced handling of data requests for incompatible station-year combinations. A `warning()` is issued for each bad combo, but if a `vector()` of stations is requested, any valid stations will still be downloaded with the bad combos omitted after the warning is emitted.

# GSODR 3.1.8

- Update CITATION file to follow CRAN's ~ever-changing whims~ guidelines.
Expand Down
70 changes: 43 additions & 27 deletions R/get_GSOD.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

#' Download and return a data.table object of GSOD weather data
#' Download and Return a data.table Object of GSOD Weather Data
#'
#' @description
#' This function automates downloading, cleaning, reformatting of data from
Expand Down Expand Up @@ -99,46 +98,59 @@ get_GSOD <- function(years,
country = NULL,
max_missing = NULL,
agroclimatology = FALSE) {

# Validate user inputs -------------------------------------------------------
.validate_years(years)
# Validate stations for missing days -----------------------------------------
if (!is.null(max_missing)) {
if (is.na(max_missing) | max_missing < 1) {
stop(call. = FALSE,
"\nThe `max_missing` parameter must be a positive",
"value larger than 1\n")
stop(
call. = FALSE,
"\nThe `max_missing` parameter must be a positive",
"value larger than 1\n"
)
}
}

if (!is.null(max_missing)) {
if (format(Sys.Date(), "%Y") %in% years) {
stop(call. = FALSE,
"You cannot use `max_missing` with the current, incomplete year.")
stop(
call. = FALSE,
"You cannot use `max_missing` with the current, incomplete year."
)
}
}

if (isTRUE(agroclimatology) & !is.null(station)) {
stop(call. = FALSE,
"You cannot specify a single station along with agroclimatology.")
stop(
call. = FALSE,
"You cannot specify a single station along with agroclimatology."
)
}

# CRAN NOTE avoidance
isd_history <- NULL # nocov

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

# Validate user entered stations for existence in stations list from NCEI
invisible(lapply(
X = station,
FUN = .validate_station,
isd_history = isd_history,
years = years
))
if (!is.null(station)) {
# Validate user entered stations for existence in stations list from NCEI
invisible(lapply(
X = station,
FUN = .validate_station_id,
isd_history = isd_history
))

# Validate station data against years available. If years are requested w/ no
# data, an Warning and an `NA` is returned and removed here before passing the
# modified vector to `.download_files()`
station <- lapply(
X = station,
FUN = .validate_station_data_years,
isd_history = isd_history,
years = years
)
station <- station[!is.na(station)]
}

# Download files from server -------------------------------------------------
# remove "-" from station to construct proper URL
file_list <- .download_files(station, years)

# Subset file_list for agroclimatology only stations -----------------------
Expand All @@ -155,19 +167,23 @@ get_GSOD <- function(years,
country <- .validate_country(country, isd_history)

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

# Validate stations for missing days -----------------------------------------
if (!is.null(max_missing)) {
file_list <-
.validate_missing_days(max_missing, file_list)
if (length(file_list) == 0) {
stop(call. = FALSE,
"There were no stations that had a max of ", max_missing, " days.")
stop(
call. = FALSE,
"There were no stations that had a max of ", max_missing, " days."
)
}
}

Expand Down
124 changes: 63 additions & 61 deletions R/get_inventory.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

#' Download and return a data.table object GSOD weather station data inventories
#' Download and Return a data.table Object of GSOD Weather Station Data Inventories
#'
#' The \acronym{NCEI} maintains a document,
#' \url{https://www1.ncdc.noaa.gov/pub/data/noaa/isd-inventory.txt}, which lists
Expand Down Expand Up @@ -28,78 +27,81 @@
#' @export get_inventory

get_inventory <- function() {
"STNID" <- isd_history <- NULL #nocov
load(system.file("extdata", "isd_history.rda", package = "GSODR")) #nocov
load(system.file("extdata", "isd_history.rda", package = "GSODR")) # nocov
setkeyv(isd_history, "STNID")

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

main_body <-
fread(
file.path(tempdir(), "inventory.txt"),
skip = 8,
col.names = c(
"USAF",
"WBAN",
"YEAR",
"JAN",
"FEB",
"MAR",
"APR",
"MAY",
"JUN",
"JUL",
"AUG",
"SEP",
"OCT",
"NOV",
"DEC"
main_body <-
fread(
file.path(tempdir(), "inventory.txt"),
skip = 8,
col.names = c(
"USAF",
"WBAN",
"YEAR",
"JAN",
"FEB",
"MAR",
"APR",
"MAY",
"JUN",
"JUL",
"AUG",
"SEP",
"OCT",
"NOV",
"DEC"
)
)
)

main_body[, STNID := paste(main_body$USAF, main_body$WBAN, sep = "-")]
setkeyv(main_body, "STNID")
main_body[, STNID := paste(main_body$USAF, main_body$WBAN, sep = "-")]
setkeyv(main_body, "STNID")

main_body[, c("USAF", "WBAN") := NULL]
main_body[, c("USAF", "WBAN") := NULL]

setcolorder(main_body, "STNID")
setcolorder(main_body, "STNID")

header <-
readLines(file.path(tempdir(), "inventory.txt"), n = 6)
header <-
readLines(file.path(tempdir(), "inventory.txt"), n = 6)

# sift out the year and month
year_month <- grep("[0-9]{4}", header)
# sift out the year and month
year_month <- grep("[0-9]{4}", header)

year_month <-
tools::toTitleCase(tolower(gsub("^([^\\D]*\\d+).*", "\\1",
header[[year_month]])))
year_month <- gsub("Through ", "", year_month)
year_month <- gsub("\\..*", "", year_month)
year_month <-
tools::toTitleCase(tolower(gsub(
"^([^\\D]*\\d+).*", "\\1",
header[[year_month]]
)))
year_month <- gsub("Through ", "", year_month)
year_month <- gsub("\\..*", "", year_month)

main_body <- isd_history[main_body, on = "STNID"]
main_body <- isd_history[main_body, on = "STNID"]

class(main_body) <- c("GSODR.Info", class(main_body))
class(main_body) <- c("GSODR.Info", class(main_body))

# add attributes for printing df
attr(main_body, "GSODR.Inventory") <- c(
" *** FEDERAL CLIMATE COMPLEX INTEGRATED SURFACE DATA INVENTORY *** \n",
" This inventory provides the number of weather observations by \n",
" STATION-YEAR-MONTH for beginning of record through", year_month, " \n"
)
},
error = function(cond) {
stop(
"There was a problem retrieving the inventory file. Perhaps \n",
"the server is not responding currently or there is no \n",
"Internet connection. Please try again later.",
call. = FALSE
)
})
# add attributes for printing df
attr(main_body, "GSODR.Inventory") <- c(
" *** FEDERAL CLIMATE COMPLEX INTEGRATED SURFACE DATA INVENTORY *** \n",
" This inventory provides the number of weather observations by \n",
" STATION-YEAR-MONTH for beginning of record through", year_month, " \n"
)
},
error = function(cond) {
stop(
"There was a problem retrieving the inventory file. Perhaps \n",
"the server is not responding currently or there is no \n",
"Internet connection. Please try again later.",
call. = FALSE
)
}
)

unlink(file.path(tempdir(), "inventory.txt"))
return(main_body)
Expand Down
Loading

0 comments on commit 816dd70

Please sign in to comment.