
Get climate data from the German Weather Service (DWD) FTP-server.
The desired .zip (or .txt) dataset is downloaded into dir
.
If read=TRUE
, it is also read, processed and returned as a data.frame.
dataDWD(file, dir = "DWDdata", force = FALSE, overwrite = FALSE,
sleep = 0, quiet = FALSE, progbar = !quiet, browse = FALSE,
read = TRUE, meta = grepl(".txt$", file), fread = FALSE, format = NA,
ntrunc = 2, ...)
Char (vector): complete file URL(s) (including base and filename.zip) as returned by
selectDWD
. Can be a vector with several filenames.
Char: Writeable directory name where to save the downloaded file.
Created if not existent. DEFAULT: "DWDdata" at current getwd()
Logical (vector): always download, even if the file already exists in dir
?
If FALSE, it is still read (or name returned). DEFAULT: FALSE
Logical (vector): if force=TRUE, overwrite the existing file rather than append "_1"/"_2" etc to the filename? DEFAULT: FALSE
Number. If not 0, a random number of seconds between 0 and
sleep
is passed to Sys.sleep
after each download
to avoid getting kicked off the FTP-Server. DEFAULT: 0
Logical: suppress message about directory / filenames? DEFAULT: FALSE
Logical: present a progress bar with estimated remaining time?
If missing and length(file)==1, progbar is internally set to FALSE.
Only works if the R package pbapply
is available. DEFAULT: TRUE (!quiet)
Logical: open repository via browseURL
and
return URL folder path? If TRUE, no data is downloaded.
If file has several values, only unique folders will be opened.
DEFAULT: FALSE
Logical: read the file(s) with readDWD
? If FALSE,
only download is performed and the filename(s) returned. DEFAULT: TRUE
Logical (vector): is the file
a meta file? Passed to
readDWD
. DEFAULT: TRUE for each file ending in ".txt"
Fast reading? See readDWD
. DEFAULT: FALSE
Single integer: number of filenames printed in messages before they get truncated with message "(and xx more)". DEFAULT: 2
Further arguments passed to download.file
Presuming downloading and processing were successful:
if read=TRUE
, a data.frame of the desired dataset
(as returned by readDWD
),
otherwise the filename as saved on disc
(may have "_n" appended in name, see newFilename
).
If length(file)>1, the output is a list of data.frames / vector of filenames.
The output is always invisible.
selectDWD
. readDWD
, download.file
.
Helpful for plotting: berryFunctions::monthAxis
,
see also berryFunctions::climateGraph
# NOT RUN {
## requires internet connection
# find files for a given station name and file path:
link <- selectDWD("Kupferzell-Rechbach", res="hourly", var="precipitation", per="recent")
# actually download and read files
prec <- dataDWD(link, dir="DWDdata") # the default dir
fname <- dataDWD(link, read=FALSE) # filename, no second download (unless force=TRUE)
# current and historical files:
link <- selectDWD("Potsdam", res="daily", var="kl", per="hr", outvec=TRUE); link
potsdam <- dataDWD(link)
potsdam <- do.call(rbind, potsdam) # this will partly overlap in time
plot(LUFTTEMPERATUR~MESS_DATUM, data=tail(potsdam,1000), type="l")
# Straight line marks the jump back in time
# check for equality:
dup <- which(duplicated(potsdam$MESS_DATUM))
dup_df <- which(duplicated(potsdam))
err <- dup[ ! dup %in% dup_df]
err <- potsdam[potsdam$MESS_DATUM %in% potsdam$MESS_DATUM[err], ]
err <- err[order(err$MESS_DATUM),]
View(err) # WINDGESCHWINDIGKEIT (wind speed) has been slightly changed
# Keep only historical dataset:
potsdam <- potsdam[!duplicated(potsdam$MESS_DATUM),]
# several files:
link <- c(link, selectDWD("Potsdam", res="daily", var="kl", per="hr", outvec=TRUE))
clim <- dataDWD(link)
fname <- dataDWD(link, read=FALSE)
clim <- readDWD(fname)
unzip(zipfile=paste0("DWDdata/",fname[1]), exdir="DWDdata/Testunzip")
# There's quite some important meta information there!
plot(prec$MESS_DATUM, prec$NIEDERSCHLAGSHOEHE, main="DWD hourly rain Kupferzell", col="blue",
xaxt="n", las=1, type="l", xlab="Date", ylab="Hourly rainfall [mm]")
monthAxis(1, ym=T)
d <- dataDWD(selectDWD(id="05692", res="daily", var="kl", per="recent"))
# writes into the same folder (dir="DWDdata")
folder <- dataDWD(link, browse=T)
folder
# With many files, use sleep
links <- selectDWD(res="daily", var="solar", meta=FALSE)
sol <- dataDWD(links, sleep=20) # random waiting time after download (0 to 20 secs)
# Real life example with data completeness check etc:
browseURL("http://github.com/brry/prectemp/blob/master/Code_example.R")
# }
# NOT RUN {
# }
Run the code above in your browser using DataLab