Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions R/get_prism_annual.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@
#' mean(tmin, tmax)`.
#'
#' @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.
#'
#' @param unzip if `TRUE`, unzip the downloaded zip files in your 'prism.path'.
#'
#' @param keepZip if `TRUE`, leave the downloaded zip files in your
#' 'prism.path', if `FALSE`, they will be deleted. If `unzip` is `FALSE`, this
#' argument has no effect.
#'
#' @param keep_pre81_months The pre-1981 data includes all monthly data and the
#' annual data for the specified year. If you need annual and monthly data it
Expand Down Expand Up @@ -55,7 +58,7 @@
#' @rdname get_prism_data
#'
#' @export
get_prism_annual <- function(type, years, keepZip = TRUE,
get_prism_annual <- function(type, years, unzip = TRUE, keepZip = TRUE,
keep_pre81_months = FALSE, service = NULL)
{
### parameter and error handling
Expand Down Expand Up @@ -101,7 +104,7 @@ get_prism_annual <- function(type, years, keepZip = TRUE,
if(length(uris_post81) > 0){

for(i in seq_along(uris_post81)) {
prism_webservice(uris_post81[i],keepZip)
prism_webservice(uris_post81[i],unzip,keepZip)
setTxtProgressBar(download_pb, i)
}
}
Expand All @@ -115,6 +118,7 @@ get_prism_annual <- function(type, years, keepZip = TRUE,
for(j in seq_along(uris_pre81)){
tmp <- prism_webservice(
uris_pre81[j],
unzip,
keepZip,
returnName = TRUE,
pre81_months = ""
Expand Down
6 changes: 3 additions & 3 deletions R/get_prism_dailys.R
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@
#'
#' @export
get_prism_dailys <- function(type, minDate = NULL, maxDate = NULL,
dates = NULL, keepZip = TRUE, check = "httr",
service = NULL)
dates = NULL, unzip = TRUE, keepZip = TRUE,
check = "httr", service = NULL)
{
prism_check_dl_dir()

Expand Down Expand Up @@ -115,7 +115,7 @@ get_prism_dailys <- function(type, minDate = NULL, maxDate = NULL,

if(length(uris) > 0){
for(i in seq_along(uris)){
prism_webservice(uri = uris[i],keepZip)
prism_webservice(uri = uris[i],unzip,keepZip)
setTxtProgressBar(download_pb, i)
}
} else {
Expand Down
5 changes: 3 additions & 2 deletions R/get_prism_monthlys.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#' @rdname get_prism_data
#'
#' @export
get_prism_monthlys <- function(type, years, mon = 1:12, keepZip = TRUE,
get_prism_monthlys <- function(type, years, mon = 1:12, unzip = TRUE, keepZip = TRUE,
keep_pre81_months = TRUE, service = NULL)
{
### parameter and error handling
Expand Down Expand Up @@ -73,7 +73,7 @@ get_prism_monthlys <- function(type, years, mon = 1:12, keepZip = TRUE,
### Handle post 1981 data
if(length(uris_post81) > 0){
for(i in seq_along(uris_post81)){
prism_webservice(uris_post81[i],keepZip)
prism_webservice(uris_post81[i],unzip,keepZip)
setTxtProgressBar(download_pb, i)
}
}
Expand All @@ -87,6 +87,7 @@ get_prism_monthlys <- function(type, years, mon = 1:12, keepZip = TRUE,
for (j in seq_along(uris_pre81)) {
tmp <- prism_webservice(
uris_pre81[j],
unzip,
keepZip,
returnName = TRUE,
pre81_months = mon
Expand Down
4 changes: 2 additions & 2 deletions R/get_prism_normals.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
#'
#' @export
get_prism_normals <- function(type, resolution, mon = NULL, annual = FALSE,
keepZip = TRUE)
unzip = TRUE, keepZip = TRUE)
{
### parameter and error handling
prism_check_dl_dir()
Expand Down Expand Up @@ -67,7 +67,7 @@ get_prism_normals <- function(type, resolution, mon = NULL, annual = FALSE,
mpb <- txtProgressBar(min = 0, max =length(uris), style = 3)

for(i in seq_along(uris)){
prism_webservice(uris[i],keepZip)
prism_webservice(uris[i],unzip,keepZip)
setTxtProgressBar(mpb, i)

}
Expand Down
48 changes: 32 additions & 16 deletions R/prism_webservice.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
#'
#' @param uri a valid PRISM web service URI
#'
#' @param keepZip TRUE or FALSE, keep zip files once they have been unzipped
#' @param unzip TRUE or FALSE, unzip the zip files after downloading them
#'
#' @param keepZip TRUE or FALSE, keep zip files once they have been unzipped. If
#' `unzip` is FALSE, then this has no effect.
#'
#' @param returnName TRUE or FALSE, if TRUE the name of the file that was
#' downloaded is returned
Expand All @@ -25,7 +28,7 @@
#' }
#' @noRd

prism_webservice <- function(uri, keepZip = FALSE, returnName = FALSE,
prism_webservice <- function(uri, unzip = TRUE, keepZip = FALSE, returnName = FALSE,
pre81_months = NULL)
{
## Get file name
Expand All @@ -42,11 +45,21 @@ prism_webservice <- function(uri, keepZip = FALSE, returnName = FALSE,
fn <- regmatches(fn, regexpr('\\"[a-zA-Z0-9_\\.]+', fn))
fn <- substr(fn, 2, nchar((fn)))

if (length(prism_not_downloaded(fn, pre81_months = pre81_months)) == 0) {
#if files aren't unzipped, circumvent usual download checking with prism_not_downloaded
if (isFALSE(unzip)) {
if (file.exists(file.path(getOption("prism.path"), fn))) {
message("\n", fn, " already exists. Skipping downloading.")
return(fn)
}
}

#check for .zip file and don't download, just unzip
if (length(prism_not_downloaded(fn, pre81_months = pre81_months)) == 0 |
file.exists(file.path(getOption("prism.path"), fn))) {
message("\n", fn, " already exists. Skipping downloading.")
return(NULL)
return(fn)
} else {

outFile <- paste(options("prism.path"), fn, sep="/")

tryNumber <- 1
Expand All @@ -69,7 +82,7 @@ prism_webservice <- function(uri, keepZip = FALSE, returnName = FALSE,

tryNumber <- tryNumber + 1
}

if (!downloaded) {
warning(paste0("Downloading failed"))
} else {
Expand All @@ -87,16 +100,19 @@ prism_webservice <- function(uri, keepZip = FALSE, returnName = FALSE,
return(NULL)
}

ofolder <- strsplit(outFile, ".zip")[[1]]
suppressWarnings(
utils::unzip(outFile, exdir = ofolder)
)

# make sure unzipped folder is not empty
check_unzipped_folder(ofolder, uri)

if (!keepZip) {
file.remove(outFile)
if (isTRUE(unzip)) {

ofolder <- strsplit(outFile, ".zip")[[1]]
suppressWarnings(
utils::unzip(outFile, exdir = ofolder)
)

# make sure unzipped folder is not empty
check_unzipped_folder(ofolder, uri)

if (!keepZip) {
file.remove(outFile)
}
}
}
}
Expand Down
17 changes: 15 additions & 2 deletions man/get_prism_data.Rd

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