Skip to content

Commit

Permalink
formatting to reduce lines to 80 characters where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
rabutler-usbr committed Oct 27, 2023
1 parent eca601e commit 8b54373
Show file tree
Hide file tree
Showing 13 changed files with 145 additions and 68 deletions.
4 changes: 2 additions & 2 deletions R/get_prism_annual.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#' "tmax", "tdmean", "vpdmin", or "vpdmax". Note that `tmean ==
#' mean(tmin, tmax)`.
#'
#' @param years a valid numeric year, or vector of years, to download data for.
#' @param years a valid numeric year, or vector of years, to download data for.
#'
#' @param keepZip if `TRUE`, leave the downloaded zip files in your
#' 'prism.path', if `FALSE`, they will be deleted.
Expand Down Expand Up @@ -41,7 +41,7 @@
#' Annual and monthly prism data are available from 1891 to present. For
#' 1891-1980 data, monthly and annual data are grouped together in one download
#' file; `keep_pre81_months` determines if the other months/yearly data are kept
#' after the download. Data will be downloaded for all specified months (`mon`)
#' after the download. Data will be downloaded for all specified months (`mon`)
#' in all the `years` in the supplied vectors.
#'
#' @return Nothing is returned - an error will occcur if download is not
Expand Down
2 changes: 1 addition & 1 deletion R/get_prism_normals.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#' 30-year normals are currently computed using 1991-2020 and are available at
#' 4km and 800m resolution. See
#' [https://prism.nacse.org/normals/](https://prism.nacse.org/normals/).
#' If `mon` is specified and `annual` is `TRUE`, then monthly and annual normal
#' If `mon` is specified and `annual` is `TRUE`, then monthly and annual normal
#' data will be downloaded.
#'
#' @examples \dontrun{
Expand Down
30 changes: 18 additions & 12 deletions R/pd_get.R
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ prism_md <- function(f, returnDate = FALSE) {
#'
#' @param p a prism file directory or bil file
#'
#' @param returnDate TRUE or FALSE. If TRUE, an ISO date is returned. By default
#' @param returnDate TRUE or FALSE. If TRUE, an ISO date is returned. By default
#' years will come back with YYYY-01-01 and months as YYYY-MM-01
#'
#' @return a properly parsed string of human readable names
Expand All @@ -105,27 +105,28 @@ pr_parse <- function(p,returnDate = FALSE){
normals <- FALSE

if(grepl("normal",paste(p,collapse=""))){
if(grepl("annual",paste(p,collapse=""))){mon <- "Annual"
} else {
mon <- month.abb[as.numeric(p[length(p)-1])]
}
if(grepl("annual",paste(p,collapse=""))) {
mon <- "Annual"
} else {
mon <- month.abb[as.numeric(p[length(p)-1])]
}
ds <- paste(mon,"30-year normals",sep=" ")
normals <- TRUE
} else {
} else {


d <- p[length(p)-1]
yr <- substr(d,1,4)
mon <- substr(d,5,6)
day <- substr(d,7,8)

## Get resolution
### Create date string
ifelse(
ds <- ifelse(
!is.na(month.abb[as.numeric(mon)]),
ds <- paste(month.abb[as.numeric(mon)],day,yr,sep=" "),
ds <- paste(yr,sep=" ")
paste(month.abb[as.numeric(mon)],day,yr,sep=" "),
paste(yr,sep=" ")
)
}
}

ures <- ifelse(
grepl("4km",paste(p,collapse="")),
Expand All @@ -142,7 +143,12 @@ pr_parse <- function(p,returnDate = FALSE){
if (normals) {
out <- ""
} else {
out <- paste(yr,ifelse(nchar(mon) > 0,mon,"01"),ifelse(nchar(day) > 0,day,"01"),sep="-")
out <- paste(
yr,
ifelse(nchar(mon) > 0, mon, "01"),
ifelse(nchar(day) > 0, day, "01"),
sep = "-"
)
}
}

Expand Down
2 changes: 1 addition & 1 deletion R/pd_get_station_md.R
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ read_md_csv <- function(x) {
v3 <- "Station,Name,Longitude,Latitude,Elevation(m),Network"
v4 <- "Station,Name,Longitude,Latitude,Elevation(m),Network,station_id"

if (!any(var_names == v1, var_names == v2, var_names == v3, var_names == v4)) {
if (!any(var_names == v1, var_names == v2, var_names == v3, var_names == v4)){
stop(
"Metadata file does not appear to be formatted as expected.\n",
" Please check that the .stn.csv file exists and is from prism.\n",
Expand Down
2 changes: 1 addition & 1 deletion R/prism_archive_ls.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#' `prism_archive_ls()` lists all available prism data (all variables and all
#' temporal periods) that are available in the local archive, i.e., they
#' have already been downloaded and are available in [prism_get_dl_dir()].
#' [prism_archive_subset()] can be used to subset the archive based on specified
#' [prism_archive_subset()] can be used to subset the archive based on specified
#' variables and temporal periods.
#'
#' @details
Expand Down
9 changes: 7 additions & 2 deletions R/prism_archive_subset.R
Original file line number Diff line number Diff line change
Expand Up @@ -242,10 +242,15 @@ check_subset_folders_args <- function(type, temp_period, years, mon, minDate,
}

if (!(temp_period %in% both_norm) & !is.null(resolution))
stop("`resolution` should only be specified when `temp_period` is 'normals'")
stop(
"`resolution` should only be specified when `temp_period` is 'normals'"
)

# day specifications only for daily
if (temp_period != "daily" & any(!is.null(minDate), !is.null(maxDate), !is.null(dates)))
if (
temp_period != "daily" &
any(!is.null(minDate), !is.null(maxDate), !is.null(dates))
)
stop("`minDate`, `maxDate`, and/or `dates` should only be specified when `temp_period` is 'daily'")

# if annual normals, then no years or months should be specified
Expand Down
2 changes: 1 addition & 1 deletion R/prism_archive_verify.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#' [prism_archive_subset()]. Any files that are not readable can automatically
#' be re-downloaded.
#'
#' Under the hood, it uses `raster::stack()` and then `raster::rasterToPoints()`
#' Under the hood, it uses `raster::stack()` and then `raster::rasterToPoints()`
#' to determine if the bil files are readable. If both those files are able
#' to successfully read the files, they are assumed to be valid/readable.
#'
Expand Down
10 changes: 7 additions & 3 deletions R/utility_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -227,12 +227,16 @@ gen_dates <- function(minDate, maxDate, dates){
if(all(is.null(dates), is.null(minDate), is.null(maxDate)))
stop("You must specify either a date range (minDate and maxDate) or a vector of dates")

if((!is.null(dates) && !is.null(maxDate)) | (!is.null(dates) && !is.null(minDate))){
if((!is.null(dates) && !is.null(maxDate)) |
(!is.null(dates) && !is.null(minDate))) {
stop("You can enter a date range or a vector of dates, but not both")
}

if((!is.null(maxDate) & is.null(minDate)) | (!is.null(minDate) & is.null(maxDate))){
stop("Both minDate and maxDate must be specified if specifying a date range")
if((!is.null(maxDate) & is.null(minDate)) |
(!is.null(minDate) & is.null(maxDate))) {
stop(
"Both minDate and maxDate must be specified if specifying a date range"
)
}

if(!is.null(dates)){
Expand Down
4 changes: 3 additions & 1 deletion data-raw/prism_example.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ Then, create a raster stack of the data, using only October 2017 - September 202

```{r}
ond2017 <- prism_archive_subset("tmean", "monthly", years = 2017, mon = 10:12)
full_yrs <- prism_archive_subset("tmean", "monthly", years = 2018:2019, mon = 1:12)
full_yrs <- prism_archive_subset(
"tmean", "monthly", years = 2018:2019, mon = 1:12
)
js2019 <- prism_archive_subset("tmean", "monthly", years = 2020, mon = 1:9)
ps <- pd_stack(c(ond2017, full_yrs, js2019))
Expand Down
87 changes: 61 additions & 26 deletions tests/testthat/test-gen_dates.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,28 @@ context("Test gen_dates")
d1 <- "2016-01-01"
test_that("gen_dates returns expected data", {
expect_equal(
class(gen_dates(minDate = "2016-01-01", maxDate = "2016-01-01", dates = NULL)),
class(
gen_dates(minDate = "2016-01-01", maxDate = "2016-01-01", dates = NULL)
),
"Date"
)
expect_length(gen_dates(minDate = NULL, maxDate = NULL, dates = d1), 1)
expect_length(
gen_dates(minDate = NULL, maxDate = NULL, dates = c(d1,"2016-03-03", "2015-04-25")),
gen_dates(
minDate = NULL,
maxDate = NULL,
dates = c(d1,"2016-03-03", "2015-04-25")
),
3
)
expect_length(
gen_dates(minDate = "2015-01-01", maxDate = "2015-12-31", dates = NULL),
365
)
expect_length(gen_dates(minDate = d1, maxDate = "2016-12-31", dates = NULL), 366)
expect_length(
gen_dates(minDate = d1, maxDate = "2016-12-31", dates = NULL),
366
)
expect_equal(
gen_dates(minDate = d1, maxDate = d1, dates = NULL),
gen_dates(minDate = NULL, maxDate = NULL, dates = d1)
Expand All @@ -27,30 +36,56 @@ test_that("gen_dates returns expected data", {
})

test_that("gen_dates errors properly", {
expect_error(gen_dates(minDate = "2016-01-01", maxDate = NULL, dates = "2016-01-02"),
"You can enter a date range or a vector of dates, but not both")
expect_error(gen_dates(minDate = NULL, maxDate = "2016-01-01", dates = "2016-01-02"),
"You can enter a date range or a vector of dates, but not both")
expect_error(gen_dates(minDate = "2016-01-01", maxDate = NULL, dates = NULL),
"Both minDate and maxDate must be specified if specifying a date range")
expect_error(gen_dates(minDate = NULL, maxDate = "2016-01-01", dates = NULL),
"Both minDate and maxDate must be specified if specifying a date range")
expect_error(gen_dates(minDate = "01-30-2016", maxDate = "2016-01-02", dates = NULL),
"character string is not in a standard unambiguous format")
expect_error(gen_dates(minDate = "2016-01-01", maxDate = "Jan 12, 2016", dates = NULL),
"character string is not in a standard unambiguous format")
expect_error(gen_dates(minDate = NULL, maxDate = NULL, dates = "12/15/2016"),
"character string is not in a standard unambiguous format")
expect_error(gen_dates(minDate = NULL, maxDate = NULL, dates = NULL),
"You must specify either a date range (minDate and maxDate) or a vector of dates",
fixed = TRUE)
expect_error(gen_dates(minDate = "2016-01-10", maxDate = "2016-01-02", dates = NULL),
"Your minimum date must be less than your maximum date")
expect_error(
gen_dates(minDate = "2016-01-01", maxDate = NULL, dates = "2016-01-02"),
"You can enter a date range or a vector of dates, but not both"
)
expect_error(
gen_dates(minDate = NULL, maxDate = "2016-01-01", dates = "2016-01-02"),
"You can enter a date range or a vector of dates, but not both"
)
expect_error(
gen_dates(minDate = "2016-01-01", maxDate = NULL, dates = NULL),
"Both minDate and maxDate must be specified if specifying a date range"
)
expect_error(
gen_dates(minDate = NULL, maxDate = "2016-01-01", dates = NULL),
"Both minDate and maxDate must be specified if specifying a date range"
)
expect_error(
gen_dates(minDate = "01-30-2016", maxDate = "2016-01-02", dates = NULL),
"character string is not in a standard unambiguous format"
)
expect_error(
gen_dates(minDate = "2016-01-01", maxDate = "Jan 12, 2016", dates = NULL),
"character string is not in a standard unambiguous format"
)
expect_error(
gen_dates(minDate = NULL, maxDate = NULL, dates = "12/15/2016"),
"character string is not in a standard unambiguous format"
)
expect_error(
gen_dates(minDate = NULL, maxDate = NULL, dates = NULL),
"You must specify either a date range (minDate and maxDate) or a vector of dates",
fixed = TRUE
)
expect_error(
gen_dates(minDate = "2016-01-10", maxDate = "2016-01-02", dates = NULL),
"Your minimum date must be less than your maximum date"
)
# check that dates that are specified wrong, but can convert to Date return an
# error because they fall outside the range of data available
expect_error(gen_dates(minDate = "1/1/2016", maxDate = "2016/1/2", dates = NULL),
"Please ensure minDate and maxData are within the available Prism data record")
expect_error(gen_dates(minDate = NULL, maxDate = NULL, dates = c("1/1/2016", "1/2/2016", "2016/1/3")),
"Please ensure all dates fall within the valid Prism data record")
expect_error(
gen_dates(minDate = "1/1/2016", maxDate = "2016/1/2", dates = NULL),
"Please ensure minDate and maxData are within the available Prism data record"
)
expect_error(
gen_dates(
minDate = NULL,
maxDate = NULL,
dates = c("1/1/2016", "1/2/2016", "2016/1/3")
),
"Please ensure all dates fall within the valid Prism data record"
)
})

6 changes: 4 additions & 2 deletions tests/testthat/test-pd_get_md.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ test_that("pd_get_md() works", {
expect_setequal(colnames(x), exp_cols)

expect_s3_class(
x <- pd_get_md(prism_archive_subset("tdmean", "monthly", year = 2005, mon = 11:12)),
x <- pd_get_md(
prism_archive_subset("tdmean", "monthly", year = 2005, mon = 11:12)
),
"data.frame"
)
expect_identical(dim(x), c(2L, 8L))
Expand All @@ -34,7 +36,7 @@ test_that("pd_get_md() works", {
expect_setequal(colnames(x), exp_cols)

expect_s3_class(
y <- pd_get_md(prism_archive_subset("tmin", "daily", dates = "1981-01-01")),
y <- pd_get_md(prism_archive_subset("tmin", "daily", dates = "1981-01-01")),
"data.frame"
)

Expand Down
10 changes: 8 additions & 2 deletions tests/testthat/test-prism_archive_clean.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ test_that("prism_archive_clean() works", {
expect_setequal(prism_archive_clean("ppt", "daily", years = 2020), day_delete)
expect_setequal(prism_archive_subset("ppt", "daily", years = 2020), day_keep)

expect_setequal(prism_archive_clean("ppt", "monthly", years = 2020), mon_delete)
expect_setequal(prism_archive_subset("ppt", "monthly", years = 2020), mon_keep)
expect_setequal(
prism_archive_clean("ppt", "monthly", years = 2020),
mon_delete
)
expect_setequal(
prism_archive_subset("ppt", "monthly", years = 2020),
mon_keep
)
})
45 changes: 31 additions & 14 deletions tests/testthat/test-prism_archive_subset.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,30 @@ folders <-
"PRISM_ppt_stable_4kmM2_197009_bil", "PRISM_ppt_stable_4kmM2_197010_bil",
"PRISM_ppt_stable_4kmM2_197011_bil", "PRISM_ppt_stable_4kmM2_197012_bil",
"PRISM_ppt_stable_4kmM2_1971_bil", "PRISM_tdmean_stable_4kmM3_201001_bil",
"PRISM_tdmean_stable_4kmM3_201002_bil", "PRISM_tmean_30yr_normal_4kmM2_01_bil",
"PRISM_tmean_30yr_normal_4kmM2_02_bil", "PRISM_tmean_30yr_normal_4kmM2_03_bil",
"PRISM_tmean_30yr_normal_4kmM2_04_bil", "PRISM_tmean_30yr_normal_4kmM2_05_bil",
"PRISM_tmean_30yr_normal_4kmM2_06_bil", "PRISM_tmean_30yr_normal_4kmM2_annual_bil",
"PRISM_tmean_stable_4kmD2_20130601_bil", "PRISM_tmean_stable_4kmD2_20130602_bil",
"PRISM_tmean_stable_4kmD2_20130603_bil", "PRISM_tmean_stable_4kmD2_20130604_bil",
"PRISM_tmean_stable_4kmD2_20130605_bil", "PRISM_tmean_stable_4kmD2_20130606_bil",
"PRISM_tmean_stable_4kmD2_20130607_bil", "PRISM_tmean_stable_4kmD2_20130608_bil",
"PRISM_tmean_stable_4kmD2_20130609_bil", "PRISM_tmean_stable_4kmD2_20130610_bil",
"PRISM_tmean_stable_4kmD2_20130611_bil", "PRISM_tmean_stable_4kmD2_20130612_bil",
"PRISM_tmean_stable_4kmD2_20130613_bil", "PRISM_tmean_stable_4kmD2_20130614_bil",
"PRISM_tmean_stable_4kmM3_1965_bil", "PRISM_tmean_stable_4kmM3_196502_bil",
"PRISM_tdmean_stable_4kmM3_201002_bil",
"PRISM_tmean_30yr_normal_4kmM2_01_bil",
"PRISM_tmean_30yr_normal_4kmM2_02_bil",
"PRISM_tmean_30yr_normal_4kmM2_03_bil",
"PRISM_tmean_30yr_normal_4kmM2_04_bil",
"PRISM_tmean_30yr_normal_4kmM2_05_bil",
"PRISM_tmean_30yr_normal_4kmM2_06_bil",
"PRISM_tmean_30yr_normal_4kmM2_annual_bil",
"PRISM_tmean_stable_4kmD2_20130601_bil",
"PRISM_tmean_stable_4kmD2_20130602_bil",
"PRISM_tmean_stable_4kmD2_20130603_bil",
"PRISM_tmean_stable_4kmD2_20130604_bil",
"PRISM_tmean_stable_4kmD2_20130605_bil",
"PRISM_tmean_stable_4kmD2_20130606_bil",
"PRISM_tmean_stable_4kmD2_20130607_bil",
"PRISM_tmean_stable_4kmD2_20130608_bil",
"PRISM_tmean_stable_4kmD2_20130609_bil",
"PRISM_tmean_stable_4kmD2_20130610_bil",
"PRISM_tmean_stable_4kmD2_20130611_bil",
"PRISM_tmean_stable_4kmD2_20130612_bil",
"PRISM_tmean_stable_4kmD2_20130613_bil",
"PRISM_tmean_stable_4kmD2_20130614_bil",
"PRISM_tmean_stable_4kmM3_1965_bil",
"PRISM_tmean_stable_4kmM3_196502_bil",
"PRISM_tmean_stable_4kmM3_196503_bil", "PRISM_tmean_stable_4kmM3_196504_bil",
"PRISM_tmean_stable_4kmM3_196505_bil", "PRISM_tmean_stable_4kmM3_196506_bil",
"PRISM_tmean_stable_4kmM3_196507_bil", "PRISM_tmean_stable_4kmM3_196508_bil",
Expand Down Expand Up @@ -246,7 +258,10 @@ test_that("prism:::filter_folders monthly", {
))

expect_length(filter_folders(folders, "ppt", "monthly", years = 1965), 12)
expect_length(filter_folders(folders, "ppt", "monthly", years = 1965:1967), 24)
expect_length(
filter_folders(folders, "ppt", "monthly", years = 1965:1967),
24
)
})

# filter_folders daily ------------------
Expand Down Expand Up @@ -303,7 +318,9 @@ test_that("prism:::filter_folders normals", {

expect_true(all_in(
filter_folders(folders, "tdmean", "monthly normals", resolution = "4km"),
paste0("PRISM_tdmean_30yr_normal_4kmM2_", prism:::mon_to_string(1:3), "_bil")
paste0(
"PRISM_tdmean_30yr_normal_4kmM2_", prism:::mon_to_string(1:3), "_bil"
)
))

expect_true(all_in(
Expand Down

0 comments on commit 8b54373

Please sign in to comment.