From f45b080e2896c30437fed865fe3eed95b768f408 Mon Sep 17 00:00:00 2001 From: brry Date: Sat, 26 Oct 2019 18:49:26 +0200 Subject: [PATCH] dataDWD: force can now be number of hours after which to re-download --- DESCRIPTION | 2 +- NEWS | 2 -- R/dataDWD.R | 14 ++++++++++---- man/dataDWD.Rd | 4 +++- 4 files changed, 14 insertions(+), 8 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 65c1045..0cfcdbd 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: rdwd Title: Select and Download Climate Data from 'DWD' (German Weather Service) -Version: 1.1.34 +Version: 1.1.35 Date: 2019-10-26 Depends: R(>= 2.10) SystemRequirements: Pandoc (>= 1.12.3), pandoc-citeproc diff --git a/NEWS b/NEWS index 4fe84d1..e4d95f3 100644 --- a/NEWS +++ b/NEWS @@ -3,8 +3,6 @@ rdwd ToDo-list / wishlist / issues - readDWD.data: figure out reason for fread trouble (reported in emails by some users) - readDWD.meta: better analasis of fwf widths needed - indexFTP: read tree with data.table/vroom -- dataDWD: change/allow force to be number of hours after which to re-download -- function for index updates - selectDWD("Berlin", res="monthly", var="kl", per="h", exactmatch = F) should only return stations with data, with a single warning only diff --git a/R/dataDWD.R b/R/dataDWD.R index 03066bb..7314cb7 100644 --- a/R/dataDWD.R +++ b/R/dataDWD.R @@ -75,7 +75,9 @@ #' Created if not existent. DEFAULT: "DWDdata" at current \code{\link{getwd}()} #' @param force Logical (vector): always download, even if the file already exists in \code{dir}? #' Use NA to force re-downloading files older than 24 hours. -#' If FALSE, it is still read (or name returned). DEFAULT: FALSE +#' Use a numerical value to force after that amount of hours. +#' Note you might want to set \code{overwrite=TRUE} as well. +#' If FALSE, the file is still read (or name returned). DEFAULT: FALSE #' @param overwrite Logical (vector): if force=TRUE, overwrite the existing file #' rather than append "_1"/"_2" etc to the filename? DEFAULT: FALSE #' @param sleep Number. If not 0, a random number of seconds between 0 and @@ -150,10 +152,14 @@ outfile <- gsub(paste0(base,"/"), "", file) outfile <- gsub("/", "_", outfile) # force=NA management -force_old <- difftime(Sys.time(), file.mtime(outfile), units="h") > 24 +if(is.null(force)) stop("force cannot be NULL. Must be TRUE, FALSE, NA or a number.") force <- rep(force, length=length(outfile)) # recycle vector -force[is.na(force) & force_old] <- TRUE # force if old -force[is.na(force)] <- FALSE # otherwise don't +fT <- sapply(force, isTRUE) +fF <- sapply(force, isFALSE) +if(any(fT)) force[fT] <- 0 +if(any(fF)) force[fF] <- Inf +force[is.na(force)] <- 24 +force <- difftime(Sys.time(), file.mtime(outfile), units="h") > force dontdownload <- file.exists(outfile) & !force if( any(dontdownload) & !quiet ) diff --git a/man/dataDWD.Rd b/man/dataDWD.Rd index 49eba77..e3b1f34 100644 --- a/man/dataDWD.Rd +++ b/man/dataDWD.Rd @@ -24,7 +24,9 @@ Created if not existent. DEFAULT: "DWDdata" at current \code{\link{getwd}()}} \item{force}{Logical (vector): always download, even if the file already exists in \code{dir}? Use NA to force re-downloading files older than 24 hours. -If FALSE, it is still read (or name returned). DEFAULT: FALSE} +Use a numerical value to force after that amount of hours. +Note you might want to set \code{overwrite=TRUE} as well. +If FALSE, the file is still read (or name returned). DEFAULT: FALSE} \item{overwrite}{Logical (vector): if force=TRUE, overwrite the existing file rather than append "_1"/"_2" etc to the filename? DEFAULT: FALSE}