---
title: "Surface Water"
author: "Michael Rustler"
date: "`r Sys.time()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Surface Water}
  %\VignetteEncoding{UTF-8}
  %\VignetteEngine{knitr::rmarkdown}
editor_options: 
  chunk_output_type: console
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
  #, eval = FALSE # to temporarily "switch off" this (long running) vignette
)
```

## Define Helper Functions

```{r helper_functions}
cat_file_enumeration <- function(files) {
  cat(paste0(collapse = "\n\n", sprintf(
    "- [%s](%s/%s)", 
    files, 
    "https://kwb-r.github.io/wasserportal",
    files
  )))
}
```

## Master Data

```{r master_data}
library(wasserportal)

stations <- wasserportal::get_stations()
stations_list <- kwb.utils::selectElements(stations, "overview_list")

is_sw <- stringr::str_detect(names(stations_list), "surface")

files <- wasserportal::list_masters_data_to_csv(stations_list[is_sw])
```

The following surface water master data `.csv` files are available for download: 

```{r master_data_csv, echo = FALSE, results ='asis'}
cat_file_enumeration(files)
```

## Daily Surface Water Data & Overall Surface Water Quality

By running the code below all available `daily surface water` data of monitoring 
stations from Wasserportal Berlin will be downloaded and exported into one `.json`
file for each parameter and all available monitoring stations.

```{r surface_data_daily_export}
variables <- wasserportal::get_surfacewater_variables()

variables

sw_data_daily_list <- wasserportal::get_daily_surfacewater_data(
  stations,
  variables
)

files <- wasserportal::list_timeseries_data_to_zip(sw_data_daily_list)
files

# Data availability per parameter
sw_data_daily_list %>%
  dplyr::bind_rows() %>% 
  dplyr::count(Parameter, Einheit)


station_ids <- stations$overview_list$surface_water.quality$Messstellennummer

swq_data <- wasserportal::get_surfacewater_qualities(station_ids)

files1 <- wasserportal::list_timeseries_data_to_zip(
  list("surface-water_quality" = swq_data)
  )

files2 <- "surface-water_quality.zip"

file.rename(files1, files2)
```

The following `.zip` files are available for download: 

```{r daily_surface_water_data_zip, echo = FALSE, results ='asis'}
cat_file_enumeration(c(files, files2))
```

## Daily Surface Water Levels

```{r surface_waterlevel}
swl_master <- wasserportal::get_wasserportal_masters_data(
  master_urls = stations_list$surface_water.water_level %>%
    dplyr::filter(.data$Betreiber == "Land Berlin") %>%
    dplyr::pull(.data$stammdaten_link)
)

column_level_zero <- "Pegelnullpunkt_m_NHN"
  
swl_data <- sw_data_daily_list$surface_water.water_level %>% 
  dplyr::select(where(~!all(is.na(.x)))) %>%
  dplyr::left_join(
    kwb.utils::selectColumns(swl_master, c("Nummer", column_level_zero)),
    by = c(Messstellennummer = "Nummer")
  ) %>%
  dplyr::mutate(
    Tagesmittelwert_Pegelstand_mNN = as.numeric(.data[[column_level_zero]]) + 
      .data$Tagesmittelwert / 100
  ) %>%
  ### remove -777 for messstellennummer 5867000 (few values in 2000) resulted by
  ### step above
  dplyr::filter(.data[["Tagesmittelwert_Pegelstand_mNN"]] != -777) %>%
  kwb.utils::removeColumns(column_level_zero)

str(swl_data)
```
