knitr::opts_chunk$set( collapse = TRUE, comment = "#>", eval = FALSE )
# If required, install the remotes package from CRAN # install.packages("remotes") # Install kwb.dwd from GitHub remotes::install_github("KWB-R/kwb.dwd") # Install the current development version remotes::install_github("KWB-R/kwb.dwd@dev")
This package contains a function that lists the files that are available on an FTP server. It allows for recursive listing, i.e. listing the files in folders and subfolders. For example, to list all historical RADOLAN files, do:
# Define the URL to the historical RADOLAN files url <- kwb.dwd:::ftp_path_cdc("grids_germany/daily/radolan/historical") # List all historical RADOLAN files urls <- kwb.dwd::list_url(url, recursive = TRUE)
It takes about six seconds to get the file list. All file paths and file names
(as they were listed on 2022-09-17) are also contained in the data frame
dwd_files
that is available in the package. You can browse through this
data frame to find the files that you are looking for:
# Get data frame of all files dwd_files <- kwb.dwd::dwd_files # Filter for historical RADOLAN files radolan_files <- dwd_files[grep("radolan/historical", dwd_files$file), ] # Shorten the path for better display radolan_files$path <- gsub("^grids_germany/", "", radolan_files$file) # Show the first file paths head(kwb.utils::resetRowNames(radolan_files)) # Show the last file paths tail(kwb.utils::resetRowNames(radolan_files))
# Set the target directory export_dir <- "~/Downloads/radolan" # Create required directories kwb.utils::createDirectory(file.path(export_dir, "daily/historical")) # Download corresponding RADOLAN files from DWD's FTP server kwb.dwd::download_radolan( resolution = "daily", export_dir = export_dir, start_daily = "2018-07", end_daily = "2018-08" )
The output should look like this:
Download: 'daily' historical radolan data ... Download: "daily, historical" and save to ~/Downloads/radolan/daily/historical/SF201807.tar.gz ... versuche URL 'ftp://opendata.dwd.de/climate_environment/CDC/grids_germany/daily/radolan/historical/bin/2018/SF201807.tar.gz' downloaded 132.2 MB ok. (1.48s) Download: "daily, historical" and save to ~/Downloads/radolan/daily/historical/SF201808.tar.gz ... versuche URL 'ftp://opendata.dwd.de/climate_environment/CDC/grids_germany/daily/radolan/historical/bin/2018/SF201808.tar.gz' downloaded 173.5 MB ok. (1.97s) ok. (3.45s)
# Get the paths to the zipped files files <- dir(export_dir, "\\.tar\\.gz", recursive = TRUE, full.names = TRUE) # Unzip all files to the export directory lapply(files, untar, exdir = path.expand(export_dir))
# Get the paths to all binary Radolan files in the export directory files_bin <- dir(export_dir, "---bin$", full.names = TRUE) # Read all Radolan files as raster objects raster_objects <- lapply(files_bin, function(file) kwb.utils::catAndRun( paste("Reading", file), kwb.dwd::read_binary_radolan_file(file) )) # Plot the contents of one file raster::plot(raster_objects[[1]])
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.