Skip to content

Commit

Permalink
add function plot_measurementchain_and_welloperation()
Browse files Browse the repository at this point in the history
first step to include plots for wells with
EC (or TEMP) together with well operation data (i.e. Q per day) for each of the wells 9,10 and 13 equipped with EC chains. In addition to @chsprenger plot for each well I also added the total wellfield production rate.

to do:
- add to automated workflow
- add 📖
- improve style
  • Loading branch information
mrustl committed Mar 12, 2024
1 parent 4a7bc76 commit e08e422
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 1 deletion.
94 changes: 94 additions & 0 deletions R/plot_measurementchain_and_welloperation.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
plot_measurementchain_and_well_operation <- function(mc_dat,
well_op_data_meta,
brunnen_nr = 9,
para = "Leitfaehigkeit",
y_label = "elektr. Leitf\u00E4higkeit (\u00B5S/cm)") {


stopifnot("")


# plot time series Brunnen 9
selection <- mc_dat %>%
dplyr::filter(.data[["parameter"]] == para,
.data[["brunnen_nummer"]] == brunnen_nr)


n_sensors <- length(unique(selection$einbau_sensor_muGOK))

custom_palette <- RColorBrewer::brewer.pal(n_sensors,
"Dark2")

p_well <- ggplot2::ggplot(selection,
ggplot2::aes(x = datum_uhrzeit,
y = messwert,
group = einbau_sensor_muGOK,
color = as.factor(einbau_sensor_muGOK))) +
ggplot2::geom_line() +
ggplot2::scale_color_manual(values = custom_palette) +
ggplot2::labs(x="", y = y_label, color = "Sensor [muGOK]") +
ggplot2::theme_bw() +
#ggplot2::xlim(as.Date("2023-05-10"), as.Date("2024-03-31")) +
#ggplot2::ylim(500,3000) +
ggplot2::theme(legend.position = "top",
axis.text.x = ggplot2::element_blank()) +
ggplot2::guides(color = ggplot2::guide_legend(ncol = n_sensors))

#p_well

dat_well <- well_op_data_meta %>% dplyr::filter(.data$brunnen_nummer == brunnen_nr)

sum_well <- dat_well %>%
dplyr::group_by(.data$bwb_datum) %>%
dplyr::summarise(n = dplyr::n(),
total_q = sum(.data$menge_summe_m3, na.rm = TRUE) )

sum_well$ma7 <- zoo::rollmean(sum_well$total_q, k = 7, fill = NA, align = "right")
sum_well$ma10 <- zoo::rollmean(sum_well$total_q, k = 10, fill = NA, align = "right")

plot_q_well <- ggplot2::ggplot(sum_well, ggplot2::aes(x = as.Date(bwb_datum), y = total_q)) +
ggplot2::geom_bar(stat = "identity", width=1, color = "blue") +
ggplot2::labs(x="", y = sprintf("Q, Brunnen %2d (m3/d)", brunnen_nr)) +
ggplot2::theme_bw() +
ggplot2::theme(axis.text.x = ggplot2::element_blank())
# ggplot2::scale_x_date(date_breaks = "1 month", date_labels = "%b %Y") +
# ggplot2::theme(axis.text.x = ggplot2::element_text(angle = 0,
# vjust = 0.5,
# hjust = 1)) #+
#ggplot2::xlim(as.Date("2023-05-10"), as.Date("2024-03-31"))


sum_wellfield <- well_op_data_meta %>%
dplyr::group_by(.data$bwb_datum) %>%
dplyr::summarise(n = dplyr::n(),
total_q = sum(.data$menge_summe_m3, na.rm = TRUE) )

sum_wellfield$ma7 <- zoo::rollmean(sum_well$total_q, k = 7, fill = NA, align = "right")
sum_wellfield$ma10 <- zoo::rollmean(sum_well$total_q, k = 10, fill = NA, align = "right")

plot_q_wellfield <- ggplot2::ggplot(sum_wellfield, ggplot2::aes(x = as.Date(bwb_datum), y = total_q)) +
ggplot2::geom_bar(stat = "identity", width=1, color = "blue") +
ggplot2::labs(x="Zeit", y = "Q, Brunnenfeld K-Galerie (m3/d)") +
ggplot2::theme_bw() +
ggplot2::scale_x_date(date_breaks = "1 month", date_labels = "%b %Y") +
ggplot2::theme(axis.text.x = ggplot2::element_text(angle = 0,
vjust = 0.5,
hjust = 1))


combined_plot <- cowplot::plot_grid(p_well,
plot_q_well,
plot_q_wellfield,
ncol = 1, align = 'v')

combined_plot_with_title <- cowplot::ggdraw() +
cowplot::draw_plot(combined_plot, 0, 0, 1, 1) +
cowplot::draw_label(sprintf("Brunnen %2d", brunnen_nr), x = 0.2, y = 0.8, size = 12, hjust = 0.5)

combined_plot_with_title

}

plot_measurementchain_and_well_operation(mc_dat = mc_dat,
well_op_data_meta = well_op_data_meta,
brunnen_nr = 9)
66 changes: 65 additions & 1 deletion vignettes/measurement-chains.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ paths <- kwb.utils::resolve(list(
export_dir = "<local_dir>/export",
# KWB cloud directory to which data in "export_dir" is uploaded
upload_dir = "projects/GeoSalz/Monitoring/messketten"
upload_dir = "projects/GeoSalz/Monitoring/messketten",
# KWB cloud directory with latest BWB well operation data
well_operation = "<upload_dir>/BWB_Brunnen_Prozessdaten"
))
# Print all paths
Expand Down Expand Up @@ -374,3 +377,64 @@ if (kwb.utils::isTryError(result)) {
)
}
```

### Download Well Operation Data from Cloud

```{r download_well_operation_data_from_cloud}
mc_dat <- mc_data %>%
dplyr::left_join(metadata[,c("sensor_id", "einbau_sensor_muGOK")], by = "sensor_id") %>%
dplyr::left_join(mc_files %>% dplyr::select(sftp_path, galerie, brunnen_nummer),
by = c(file = "sftp_path"))
well_op_file <- kwb.nextcloud::list_files(path = paths$well_operation,
full_info = TRUE,
) %>%
dplyr::filter(lastmodified == max(lastmodified))
file <- well_op_file$file[1]
tdir <- fs::path_norm(paths$download_dir)
xlsx_file <- kwb.nextcloud::download_files(hrefs = well_op_file$href,
target_dir = tdir)
well_op_data <- readxl::read_xlsx(path = xlsx_file) %>%
janitor::clean_names() %>%
dplyr::filter(.data$menge_summe_m3 < 2000)
separate_name_der_messstelle_gms <- function(string) {
tibble::tibble(
wasserwerk = stringr::str_sub(string, 1L, 3L),
galerie = stringr::str_sub(string, 4L, 4L) %>% toupper(),
brunnen_nummer = stringr::str_sub(string, 5L, 9L) %>%
stringr::str_remove_all(pattern = "-") %>%
as.integer(),
unbekannter_buchstabe = stringr::str_sub(string, 10L, 10L) %>%
stringr::str_remove_all(pattern = "-") %>%
as.character(),
brunnen_baujahr = stringr::str_sub(string, 12L, 15L) %>%
stringr::str_remove_all(pattern = "-") %>%
as.integer(),
brunnen_bauart = stringr::str_sub(string, 16L, 16L) %>%
stringr::str_remove_all(pattern = "-") %>%
as.character()
)
}
well_op_data_meta <- well_op_data %>%
dplyr::bind_cols(separate_name_der_messstelle_gms(well_op_data$name_der_messstelle_gms))
```


### Make combined EC and well operation plot



0 comments on commit e08e422

Please sign in to comment.