Skip to content

Commit

Permalink
Add argument "crs" to read_asc_[gz_]file()
Browse files Browse the repository at this point in the history
and remove argument "projection" from read_asc_file()
  • Loading branch information
hsonne committed Apr 20, 2024
1 parent fcc1fb9 commit 15277a4
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 33 deletions.
11 changes: 10 additions & 1 deletion R/get_example_grid_germany.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@
#' * [get_shapes_of_germany].
get_example_grid_germany <- function()
{
#kwb.utils::assignPackageObjects("kwb.dwd")

path <- "evapo_p/grids_germany_monthly_evapo_p_202201.asc.gz"
read_asc_gz_file(url = ftp_path_grids_germany("monthly", path))
url <- ftp_path_grids_germany("monthly", path)

#epsg <- get_epsg_from_url(url)
epsg <- 31467L

crs <- sf::st_crs(epsg)

read_asc_gz_file(url = url, crs = crs)
}
27 changes: 12 additions & 15 deletions R/read_asc_gz_file.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@
#' @seealso
#' * [read_binary_radolan_file],
#' * [unzip_asc_gz_file].
read_asc_gz_file <- function(file, url = NULL)
read_asc_gz_file <- function(
file,
url = NULL,
crs = if (!is.null(url)) get_epsg_from_url(url)
)
{
target_dir <- if (is.null(url)) {
dirname(file)
Expand All @@ -26,39 +30,32 @@ read_asc_gz_file <- function(file, url = NULL)
)

#dir(target_dir)
#kwb.utils::hsOpenWindowsExplorer(dirname(grid_file))

# Read the .asc file into a raster object (with appropriate projection)
read_asc_file(grid_file)
read_asc_file(file = grid_file, crs = crs)
}

# read_asc_file ----------------------------------------------------------------

#' Read Raster Data from .ASC File
#'
#' @param file path to .asc file
#' @param projection projection string used in Radolan data. Currently not used!
#' @param crs coordinate reference system to be applied to the data. An object
#' of class "crs" (as returned by \code{\link[sf]{st_crs}} is expected here.
#' @param dbg logical indicating whether to show debug messages
#' @importFrom kwb.utils catAndRun
#' @importFrom raster `crs<-` raster
read_asc_file <- function(
file,
projection = get_radolan_projection_string(),
dbg = TRUE
)
read_asc_file <- function(file, crs = NULL, dbg = TRUE)
{
kwb.utils::catAndRun(
sprintf("Reading %s using raster::raster()", basename(file)),
dbg = dbg,
expr = {

# Provide a copy of the projection file in the same folder
provide_projection_file(file)
result <- raster::raster(file)

#result <- raster::raster(file, values = TRUE)
#raster::crs(result) <- projection

result
#prj_file <- provide_projection_file(file)
raster::raster(file, native = TRUE, crs = crs)
}
)
}
4 changes: 2 additions & 2 deletions man/get_epsg_from_url.Rd

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

5 changes: 3 additions & 2 deletions man/read_asc_file.Rd

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

6 changes: 5 additions & 1 deletion man/read_asc_gz_file.Rd

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

2 changes: 1 addition & 1 deletion tests/testthat/test-function-get_epsg_from_url.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ test_that("get_epsg_from_url() works", {
url_2 <- paste0(start_url, "precipitation/01_Jan/grids_germany_monthly_precipitation_188101.asc.gz")
url_3 <- kwb.dwd:::get_radolan_url("hourly", year_month = "202001")

expect_error(f(url_1))
capture.output(expect_error(f(url_1)))
expect_identical(f(url_2), 31467L)
expect_identical(f(url_3), 3034L)
})
1 change: 0 additions & 1 deletion tests/testthat/test-function-read_asc_file.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ test_that("read_asc_file() works", {

expect_error(f())
expect_error(expect_warning(f("no-such-file", dbg = FALSE)))

})
26 changes: 16 additions & 10 deletions tests/testthat/test-function-read_asc_gz_file.R
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
#
# This test file has been generated by kwb.test::create_test_files()
# launched by user hsonne on 2022-09-16 16:43:24.
# Your are strongly encouraged to modify the dummy functions
# so that real cases are tested. You should then delete this comment.
#
#library(testthat)

test_that("read_asc_gz_file() works", {

f <- kwb.dwd:::read_asc_gz_file

expect_error(
kwb.dwd:::read_asc_gz_file()
# argument "file" is missing, with no default
)
expect_error(f())

start_url <- "ftp://opendata.dwd.de/climate_environment/CDC/grids_germany/monthly/"

#head(grep("\\.asc.gz$", kwb.dwd::dwd_files$file, value = TRUE))

url_1 <- paste0(start_url, "evapo_p/grids_germany_monthly_evapo_p_202201.asc.gz")
url_2 <- paste0(start_url, "precipitation/01_Jan/grids_germany_monthly_precipitation_188101.asc.gz")

crs <- sf::st_crs(31467L)

expect_output(result_1 <- f(url = url_1, crs = crs))
expect_output(result_2 <- f(url = url_2, crs = crs))

expect_s4_class(result_1, "RasterLayer")
expect_s4_class(result_2, "RasterLayer")
})

0 comments on commit 15277a4

Please sign in to comment.