Skip to content

Commit

Permalink
feat(write_stats)
Browse files Browse the repository at this point in the history
fixes #52
  • Loading branch information
dimfalk committed Feb 28, 2024
1 parent 352c44d commit 8aee96f
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 9 deletions.
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
Package: kostra2010R
Title: R Interface for KOSTRA-DWD-2010r Dataset
Version: 0.8.7
Date: 2023-09-26
Version: 0.9.0
Date: 2024-02-28
Authors@R:
person("Dimitri", "Falk", , "[email protected]", role = c("aut", "cre"))
Description: Extract cell-specific precipitation statistics from KOSTRA-DWD-2010R.
License: GPL (>= 3)
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
Depends: R (>= 4.2.0)
Imports:
checkmate,
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ export(get_returnp)
export(get_stats)
export(idx_build)
export(idx_exists)
export(write_stats)
import(sf)
3 changes: 2 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# version 0.8.7
# version 0.9.0

## features

Expand All @@ -9,6 +9,7 @@
- `get_returnp()` now allows interpolation of return periods with `interpolate = TRUE`
- `get_depth()` now allows to consider uncertainties with `uc = TRUE`
- `get_stats()` now optionally return precipitation yield values with `as_depth = FALSE`
- `write_stats()` now wraps `write.table()` to facilitate dumping stats to disk


## enhancements
Expand Down
54 changes: 54 additions & 0 deletions R/write_stats.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#' Write cell-specific statistics from the KOSTRA-DWD-2010R dataset to disk
#'
#' @param x Tibble containing grid cell statistics from KOSTRA-DWD-2010R,
#' as provided by `get_stats()`.
#' @param file character. Filename with csv extension to be used for data output.
#'
#' @export
#'
#' @seealso \link{get_stats}
#'
#' @examples
#' \dontrun{
#' kostra <- get_stats("49011")
#'
#' write_stats(kostra)
#' write_stats(kostra, file = tempfile(fileext = ".csv"))
#' }
write_stats <- function(x = NULL,
file = NULL) {

# debugging ------------------------------------------------------------------

# x <- get_stats("49011")
# file <- "kostra2010R_hN_49011.csv"

# check arguments ------------------------------------------------------------

checkmate::assert_tibble(x)

checkmate::assert(

checkmate::test_null(file),
checkmate::test_character(file, len = 1, pattern = "csv$")
)

# main -----------------------------------------------------------------------

# construct default filename if not specified
if (is.null(file)) {

file <- paste0("kostra2010R_",
attr(x, "type"), "_",
attr(x, "id"), ".csv")
}

# dump to disk
utils::write.table(x,
file,
sep = "; ",
dec = ",",
na = "",
row.names = FALSE,
quote = FALSE)
}
2 changes: 1 addition & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ ggplot(longdata, aes(D_min, value, colour = name)) +
subtitle = paste0("INDEX_RC: ", attr(kostra, "id")))
```

... or exported to disk using `write.csv2()`.
... or exported to disk using `write_stats()`.

## Contributing

Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ and load the package via

``` r
library(kostra2010R)
#> 0.8.7
#> 0.9.0
```

## Getting started
Expand Down Expand Up @@ -115,9 +115,9 @@ p3
#> Geometry set for 1 feature
#> Geometry type: POINT
#> Dimension: XY
#> Bounding box: xmin: 6.784479 ymin: 51.23912 xmax: 6.784479 ymax: 51.23912
#> Bounding box: xmin: 6.784486 ymin: 51.23912 xmax: 6.784486 ymax: 51.23912
#> Geodetic CRS: WGS 84
#> POINT (6.784479 51.23912)
#> POINT (6.784486 51.23912)

p4 <- get_centroid("Freiburg im Breisgau")
p4
Expand Down Expand Up @@ -387,7 +387,7 @@ ggplot(longdata, aes(D_min, value, colour = name)) +

<img src="man/figures/README-unnamed-chunk-21-1.png" width="100%" />

… or exported to disk using `write.csv2()`.
… or exported to disk using `write_stats()`.

## Contributing

Expand Down
28 changes: 28 additions & 0 deletions man/write_stats.Rd

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

32 changes: 32 additions & 0 deletions tests/testthat/test-write_stats.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
test_that("Data I/O works", {

filename <- "kostra2010R_HN_49011.csv"

write_stats(kostra_ref)

kostra <- utils::read.table(file = filename,
header = TRUE,
sep = ";",
dec = ",",
na = "")

unlink(filename)

testthat::expect_equal(as.matrix(kostra),
as.matrix(kostra_ref))



filename <- tempfile(fileext = ".csv")

write_stats(kostra_ref, file = filename)

kostra <- utils::read.table(file = filename,
header = TRUE,
sep = ";",
dec = ",",
na = "")

testthat::expect_equal(as.matrix(kostra),
as.matrix(kostra_ref))
})

0 comments on commit 8aee96f

Please sign in to comment.