-
Notifications
You must be signed in to change notification settings - Fork 1
/
tutorial.Rmd
791 lines (640 loc) · 25.2 KB
/
tutorial.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
---
title: "R Script Supporting the Creation of a Volume-Proportional Composite Sample"
author: "Hauke Sonnenberg"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Tutorial}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
```
# Install Required Packages
In order to run the script you need a bunch of R packages all of which are
are publicly available, either on
[CRAN (Comprehensive R Archive Network)](http://cran.r-project.org/web/packages/available_packages_by_name.html)
or on [our GitHub-Account](https://github.com/kwb-r).
The script described in the following requires the package
[kwb.monitoring](https://github.com/kwb-r/kwb.monitoring) and all its
dependencies. All these packages are automatically installed by
running the following code. Make sure to have none of the packages to be
installed already loaded.
```{r eval = FALSE}
# Install devtools, if required, from CRAN
# install.packages("devtools")
# Install kwb.monitoring from GitHub
devtools::install_github("kwb-r/kwb.monitoring", dependencies = TRUE)
```
# Load kwb.monitoring
We are now ready to start the actual script. First, you need to load the
package kwb.monitoring and all the packages that it depends on:
```{r}
library(kwb.monitoring)
```
# Configuration
We need to start with a configuration section that defines how the script will
behave, depending on the monitoring station of which data is being analysed.
We are using three letter codes to identify a monitoring station. Let's assume
that we have two monitoring stations: **ALT** and **NEU** (taken from KWB project
**[OGRE](http://www.kompetenz-wasser.de/OgRe-Relevanz-organischer-Spurens.568.0.html)**
where they represent "Altbau" and "Neubau"). At both stations at least
the water level and the flow are measured.
The code section printed in the following defines different types of settings:
* `eventSettings` define for each monitoring station time intervals for which
to apply different thresholds of water level and flow. In the example below
two time windows are defined in each case but you are free to adapt the list
for more or less time intervals. The event settings have the following effect:
If for a timestamp belonging to the time interval between `tBeg` and `tEnd`
the measured water level is above `Hthreshold` and the measured flow is above
`Qthreshold`, then, and only then, this timestamp is considered to lie within
a "hydraulic event". The script will detect hydraulic events and will generate
plots and statistics for each event.
* `regressionSettings` define which H/Q-regression models to use if flow data
are lacking.
Different models can be applied to different time intervals (sublist `models`).
The sublist `usage` defines in which time intervals missing values are replaced
by values that are predicted by the regression models rather than doing a linear
interpolation (standard behaviour).
* `intervalsToRemove` defines time intervals of which data shall not be used
for the analysis. These intervals are removed before showing the event overview
plots so that these intervals will not be shown. Also, you will not be able to
calculate any composite sampling for these intervals.
* `settings` defines diverse main settings that control the behaviour of the
script. For an explanation of these settings, see the comments in the script
below or the help page of the function `kwb.monitoring::configure`.
## Define Helper Functions
```{r}
as_utc <- function(x) as.POSIXct(x, tc = "UTC")
```
## Define Event Settings
```{r}
# eventSettings ----------------------------------------------------------------
eventSettings <- list(
#Hthresholds = c(ALT = 0.07, NEU = 0.07),
#Qthresholds = c(ALT = 5, NEU = 2.5),
ALT = rbind(
data.frame(
tBeg = as_utc("2014-03-27 10:04:00"),
tEnd = as_utc("2014-04-23 17:14:59"),
Hthreshold = 0.07,
Qthreshold = 2
),
data.frame(
tBeg = as_utc("2014-04-23 17:00:00"),
tEnd = as_utc("2014-04-23 21:01:00"),
Hthreshold = 0.07,
Qthreshold = 4
)
# you may continue...
# , data.frame(...)
),
NEU = rbind(
data.frame(
tBeg = as_utc("2014-01-01 00:00:00"),
tEnd = as_utc("2014-07-09 19:00:00"),
Hthreshold = 0.07,
Qthreshold = 2.5
),
data.frame(
tBeg = as_utc("2014-07-09 19:00:01"),
tEnd = as_utc("2014-07-09 22:00:00"),
Hthreshold = 0.07,
Qthreshold = 5
)
# you may continue...
# , data.frame(...)
)
)
```
## Define Regression Settings
```{r}
# regressionSettings -----------------------------------------------------------
# models: which model file is to be used within which time interval
# usage: in which time intervals are the correlated values to be used
# instead of the linearly interpolated values
regressionSettings <- list(
# Which regression models are to be used within different time intervals?
models = list(
# ALT = rbind(
# data.frame(from = "2000-01-01",
# to = "2020-01-01",
# modelFile = "ALT_01_regressionModel.txt")
# ),
#
# NEU = rbind(
# data.frame(from = "2014-03-01",
# to = "2014-10-01",
# modelFile = "NEU_01_regressionModel.txt"),
#
# data.frame(from = "2014-10-02",
# to = "2015-02-05",
# modelFile = "NEU_02_regressionMode.txt"),
#
# data.frame(from = "2015-02-06",
# to = "2015-12-31",
# modelFile = "March_to_october_NEU_regressionModel.txt")
# )
),
# In which time intervals shall the regression models actually be applied?
usage = list(
ALT = rbind(
data.frame(from = "2015-01-30", to = "2015-02-01")
),
NEU = rbind(
data.frame(from = "2014-04-18", to = "2014-06-13"),
data.frame(from = "2014-07-09", to = "2014-08-31")
)
)
)
```
## Define Time Intervals to be Removed
```{r}
# intervalsToRemove ------------------------------------------------------------
intervalsToRemove <- list(
ALT = list(
lubridate::interval(
lubridate::ymd_hm("2014-04-03 17:38"),
lubridate::ymd_hm("2014-04-03 18:12")
),
lubridate::interval(
lubridate::ymd_hm("2014-04-04 12:00"),
lubridate::ymd_hm("2014-04-04 13:10")
)
),
NEU = list(
lubridate::interval(
lubridate::ymd_hm("2014-04-18 10:58"),
lubridate::ymd_hm("2014-04-18 21:18")
),
lubridate::interval(
lubridate::ymd_hm("2014-04-21 12:46"),
lubridate::ymd_hm("2014-04-21 16:30")
)
)
)
```
## Create a Testing Environment
```{r}
testdir <- system.file("extdata", package = "kwb.monitoring")
stopifnot(testdir != "")
rawdir <- kwb.utils::createDirectory(file.path(testdir, "rawdata"))
```
## Configure the Script
```{r}
# Main Configuration -----------------------------------------------------------
settings <- configure(
# Where is the "root" directory to raw data?
rawdir = rawdir,
# which station?
station = "ALT", # "NEU"
# "left", "centre" or "right"
sampleEventMethod = "centre",
# either "interpolate" or "predict"
# note the regressionSettings -> predict is only used for the intervals set
# in "usage", otherwise interpolate will be used
replaceMissingQMethod = "interpolate",
# which bottles are to be considered (read from the sampler file)?
bottlesToConsider = NA, # NA means: all bottles
#bottlesToConsider = 1:4,
# which bottles are to be discarded (because they are not full)?
bottlesToDiscard = NA,
#bottlesToDiscard = 7,
# sample volume (in mL) given to the bottle representing the time
# interval with highest flow volume
Vbottle = 1600,
# maximum total volume for mixed sample, in mL
Vmax = 5000,
# What are the level thresholds (in m) that trigger the start of the sampler?
Hthresholds = c(ALT=0.055, NEU=0.05),
# What are the level thresholds (in l/s) that can define the start of an event?
Qthresholds = c(ALT=3, NEU=2.5),
# What are the minimum volumes that are to be considered in the plots?
Vthresholds = c(ALT=50, NEU=32),
# time step in hydraulic data
#tstep.s = 120, still needed?
tstep.fill.s = 120,
# separation of events (how long in seconds may Hthreshold be underrun
# within an event?)
evtSepTime = 4*3600, # in seconds, here: 4 hours
# separation of sampled events within one and the same sampler file.
# If the difference between two sample times is greater than this time
# (in seconds) two sampled events are distinguished. Set to NA to prevent
# splitting of sampled events.
sampleEventSeparationTime = 4*3600, # NA, # 3600
# minimum duration (in minutes) of hydraulic events to be considered
durationThreshold = 10, # min
# separator to be used in output files (csv)
outsep = ";",
# decimal character to be used in output files (csv)
outdec = ",",
# time interval in seconds for rain data aggregation, e.g. 600 = 10 minutes
# NA = no aggregation of original rain data
rain.aggregation.interval = 600
)
# Set the path to a path dictionary file
dictionaryFile <- system.file("extdata/pathDictionary.txt", package = "kwb.monitoring")
# Read the path dictionary. The dictionary describes paths, file names and file
# name patterns in the form of a "grammar". By using the dictioanry keywords can
# be resolved to full paths.
dictionary <- pathDictionary(
dictionaryFile = dictionaryFile,
RAW_DIR = settings$rawdir,
STATION = settings$station
)
# Assign the path dictionary to the main settings.
settings$dictionary <- dictionary
# Make the event settings and regression settings part of the main settings
settings$event = eventSettings
settings$regression = regressionSettings
# Define the path to a .RData file containing rain data
rainDataFile <- file.path(settings$rawdir, "..", "RData", "data_rain.RData")
```
# Provide Create a Testing Environment
```{r}
# Create a test directory structure for raw data
flow_dir <- getOrCreatePath("FLOW_DIR", dictionary)
last_flow_dir <- kwb.utils::createDirectory(file.path(flow_dir, "20180601"))
file <- file.path(last_flow_dir, "flows.csv")
writeLines(con = file, c(
"NIVUS",
"CPU32",
"Datum",
"Fenster min",
"Fenster max",
paste0("skip_", 6:8),
"Datum\tUhrzeit\tH\tv\tQ\tT",
"01.06.2018\t00:00:00\t0.1\t0.1\t0.1\t0.1",
"01.06.2018\t00:01:00\t0.2\t0.1\t0.1\t0.1",
"01.06.2018\t00:02:00\t0.3\t0.1\t0.1\t0.1",
"01.06.2018\t00:03:00\t0.2\t0.1\t0.1\t0.1",
"01.06.2018\t00:04:00\t0.1\t0.1\t0.1\t0.1",
"01.06.2018\t00:05:00\t0.0\t0.1\t0.1\t0.1"
))
kwb.utils::createDirectory(getOrCreatePath("SAMPLE_DIR", dictionary))
```
# User defined functions
Before we start, we need to define some helper functions.
The following functions are defined:
* `read_hydraulics` reads the hydraulic data (water level and flow). It finds
all the needed information on monitoring station and paths in the `settings`
given as the only parameter.
* `getCurrentFlowSubDirectory` is a helper function used by `read_hydraulics`.
It finds the flow subfolder containing the most current data, identified by
sorting subfolder names, in our case representing the date in "yyyymmdd" format,
decreasingly.
* `readSamplerFile` provides a data frame containing the information on when
samples were taken into which bottle, considering the numbers of the bottles to
be considered (you may e.g. want to consider only bottles with an even or odd
number).
* `rainGaugesCorrelatedWithStation` returns for a given
monitoring station a vector of names of rain gauges that have been found to
measure rain heights correlating with the discharge volume measured at the
monitoring station.
* `processSampleEvent` defines how to proceed during the
analysis of one so called "sample event". With sample event we mean the time
limits that are logged by the auto sampler that is triggered by the
measured water level.
```{r}
# read_hydraulics --------------------------------------------------------------
read_hydraulics <- function(settings)
{
flowDirectory <- getOrCreatePath("FLOW_DIR", dictionary = settings$dictionary)
flowSubdirCurrent <- getCurrentFlowSubDirectory(flowDirectory)
csv <- getOrCreatePath(
"FLOW_CSV",
dictionary = settings$dictionary,
FLOW_SUBDIR_CURRENT = flowSubdirCurrent
)
hydraulics <- kwb.logger::readLogger_NIVUS_PCM4_2(kwb.utils::safePath(csv))
renames <- list(
myDateTime = "DateTime",
Fuellstand_m = "H",
Geschw_m_s = "v",
Durchfluss_l_s = "Q",
T_.C = "T"
)
columnNames <- as.character(renames)
hydraulics <- kwb.utils::selectColumns(
kwb.utils::renameColumns(hydraulics, renames),
columnNames
)
hydraulics$DateTime <- as_utc(hydraulics$DateTime)
hydraulics
### data frame with columns "DateTime" (POSIXct, UTC), "H",
### "v", "Q", "T"
}
# getCurrentFlowSubDirectory ---------------------------------------------------
getCurrentFlowSubDirectory <- function
(
flowDirectory
)
{
subDirectories <- dir(flowDirectory)
patternSubdir <- "^\\d{8}$"
unexpected <- grep(patternSubdir, subDirectories, value=TRUE, invert=TRUE)
if (! kwb.utils::isNullOrEmpty(unexpected)) {
stop(
sprintf("There are unexpected files/folders in \"%s\": %s\n%s\n",
flowDirectory, paste(kwb.utils::hsQuoteChr(unexpected)),
"Only folders of type 'YYYYMMDD' expected!")
)
}
currentSubdir <- sort(setdiff(subDirectories, unexpected), decreasing=TRUE)[1]
if (is.na(currentSubdir)) {
warning("There is no subdirectory 'YYYYMMDD' in ", flowDirectory)
}
else {
cat("Most recent flow sub-directory:", currentSubdir, "\n")
}
return(currentSubdir)
}
# readSamplerFile --------------------------------------------------------------
readSamplerFile <- function
(
samplerFile, bottlesToConsider, siteCode = NA
)
{
cat(sprintf("Reading sample data from \"%s\"... ", basename(samplerFile)))
if (containsNulString(samplerFile)) {
cat("skipped!\n")
warning(paste(
sprintf(
"File \"%s\" contains null string and is skipped!",
basename(samplerFile)),
"Remove first two bytes of the file!"))
sampleDataExtended <- NULL
}
else {
sampleData <- kwb.logger::readLogger_SIGMA_SD900(samplerFile)
cat("ok.\n")
sampleSite <- attr(sampleData, "metadata")$SITE_ID
if (!is.na(siteCode) && sampleSite != siteCode) {
stop(sprintf("The SITE_ID indicated in \"%s\" is not \"%s\" as expected!",
sampleSite, siteCode))
}
sampleDataExtended <- cbind(
samplerFile = basename(samplerFile),
sampleData,
stringsAsFactors = FALSE
)
}
# filter for relevant bottles
if (!is.null(bottlesToConsider) &
! all(is.na(bottlesToConsider))) {
cat("Filtering for bottles", commaCollapsed(bottlesToConsider), "... ")
indices <- which(sampleDataExtended$bottle %in% bottlesToConsider)
sampleDataExtended <- sampleDataExtended[indices, ]
cat("ok.\n")
}
kwb.utils::renameColumns(sampleDataExtended, list(myDateTime = "sampleTime"))
}
# rainGaugesCorrelatedWithStation ----------------------------------------------
rainGaugesCorrelatedWithStation <- function
### selects gauges after the result of the volume correlation test
(
station = NULL
)
{
x <- list(
ALT = c("ReiI", "BlnXI", "BlnIX", "BlnX"),
NEU = c("Wit", "ReiI", "BlnIX")
)
if (!is.null(station)) {
x <- x[[station]]
}
x
### list (one list element per monitoring site) of character vectors
### representing rain gauge names
}
# processSampleEvent -----------------------------------------------------------
processSampleEvent <- function
(
hydraulicData,
settings,
events,
eventsAndStat,
sampleEventIndex = -1,
to.pdf = TRUE,
verbose = FALSE
)
{
stopifnot(length(sampleEventIndex) == 1)
# Filter for sampler event by index
fileName <- getByPositiveOrNegativeIndex(
elements = sort(unique(events$samplerEvents$samplerFile)),
index = sampleEventIndex
)
sampleInformation <- filterSampleEventsForFilename(
events = events,
fileName = fileName
)
samplerEvent <- sampleInformation$samplerEvents
if (verbose) {
printSampleInformation(sampleInformation)
}
saveSampleInformation(sampleInformation, settings, sampleFile=samplerEvent$samplerFile)
# find "merged" event(s) containing the "sampler event"
mergedEventNumber <- indicesOfEventsContainingEvent(eventsAndStat$merged, samplerEvent)
stopifnot(length(mergedEventNumber) == 1)
mergedEventAndStat <- eventsAndStat$merged[mergedEventNumber, ]
# select subset of data within the sampler event
hydraulicSubset <- hsGetEvent(hydraulicData, events = mergedEventAndStat, 1)
if (kwb.utils::isNullOrEmpty(hydraulicSubset)) {
warning("There is no hydraulic data for this event:\n",
formatEvent(mergedEventAndStat))
return()
}
# generate a column containing the bottle number for each timestep
hydraulicSubset$bottle <- hsEventNumber(
hydraulicSubset$DateTime,
events = sampleInformation$bottleEvents,
eventNumbers=sampleInformation$bottleEvents$bottle
)
# generate a column containing the cumulative volume
hydraulicSubset$Vcum_m3 <- cumsum(hydraulicSubset$Q)*settings$tstep.fill.s/1000
# write interpolated data to files for "quality assurance"
eventName <- sampleLogFileToSampleName(samplerEvent$samplerFile)
writeCsvToPathFromDictionary(
dataFrame = hydraulicSubset,
key = "SAMPLED_EVENT_CSV_HYDRAULICS",
SAMPLED_EVENT_NAME = eventName,
settings = settings,
open.directory = FALSE
)
# calculate sum of flows per bottle
volumeCompositeSample <- calculateVolumeCompositeSample(
hydraulicSubset, settings
)
writeCsvToPathFromDictionary(
dataFrame = addSumRow(volumeCompositeSample),
key = "SAMPLED_EVENT_CSV_COMPOSITE",
SAMPLED_EVENT_NAME = eventName,
settings = settings,
open.directory = FALSE
)
plot_sampled_event(
hydraulicData = hydraulicData,
settings = settings,
sampleInformation = sampleInformation,
mergedEventAndStat = mergedEventAndStat,
volumeCompositeSample = volumeCompositeSample,
to.pdf = to.pdf
)
}
```
# Script I: Data Visualisation and Composite Sample Calculation
After having done all the configuration above you may run the main script that
is printed in the following. The following steps of which some are optional, are
performed:
* loading raw data (water level, flow) from csv files into `hydraulicData.raw`,
* writing raw data (no meta data, exacly one header line) to a CSV file
(optional),
* generating a plot (by default in a PDF file that will be opened) showing
the complete timeseries of hydraulic data and one plot per day,
* providing "valid" data in `hydraulicData.all` by filling gaps in the raw data,
* writing validated data to a CSV file (optional),
* removing data from user-defined time intervals containing "invalid" data,
* building different kinds of `events` (hydraulic = defined by H and Q,
sampling = defined by the sampling times, merged = overlay of hydraulic events
and sampling events),
* generating a plot showing the event limits of hydraulic, sampling and merged
events,
* writing event data to files (optional),
* adding hydraulic statistics (such as Qmax, discharge volume) as new columns
to the tables of events, resulting in `eventsAndStat`,
* writing event statistics to CSV files (optional),
* loading rain data from an .RData file (optional),
* filtering for events with a minimum discharge volume,
* generating a plot (by default in a PDF file that will be opened) showing an
overview of hydraulic events and one page showing level, discharge, rain
(optional) and the event statistics per event,
* finally calculating and visualising the shares of volumes that need to be
taken from each bottle in order to create a volume-proportional composite
sample, i.e. a composite sample in which concentrations are weighted with the
discharge volumes that have been calculated for the time intervals that are
represented by the different bottles.
And here comes the script:
```{r}
# Read last available hydraulic data (required if station changed)
# "-10d" = last 10 days
hydraulicData.raw <- kwb.base::selectTimeInterval(
read_hydraulics(settings), width = "-30d"
)
writeCsvToPathFromDictionary(
hydraulicData.raw, key = "HYDRAULIC_DATA_RAW", settings,
open.directory = FALSE
)
# Show overview plots (complete and daily), optional
showOverview(
hydraulicData.raw, settings, Qmax = 20, Hmax = 35, to.pdf = TRUE,
save.pdf = TRUE
)
# Prepare hydraulic data (fill gaps, interpolate/predict)
hydraulicData.all <- validateAndFillHydraulicData(
hydraulicData = hydraulicData.raw,
settings = settings
)
# Write data to file (optional)
writeCsvToPathFromDictionary(
hydraulicData.all, key = "HYDRAULIC_DATA_VAL", settings,
open.directory = FALSE
)
hydraulicData <- removeIntervals(
hydraulicData.all,
intervals = intervalsToRemove[[settings$station]]
)
# Build different kinds of events (hydraulic, sample, merged), required
events <- getAllTypesOfEvents(
hydraulicData, settings, FUN.readSamplerFile = readSamplerFile
)
# Plot overview of event limits (optional)
# plotEventOverview(events[c("hydraulic", "samplerEvents", "merged")], settings)
#
# # Write events to files (optional)
# writeCsvToPathFromDictionary(events$samplingEvents, "SAMPLING_EVENTS", settings,
# open.directory = FALSE)
# writeCsvToPathFromDictionary(events$bottleEvents, "BOTTLE_EVENTS", settings,
# open.directory = FALSE)
# writeCsvToPathFromDictionary(events$samplerEvents, "SAMPLER_EVENTS", settings,
# open.directory = FALSE)
#
# # Add statistics to hydraulic events
# eventsAndStat <- addStatisticsToEvents(events, hydraulicData.all)
#
# # Write event including statistics to file (optional)
# writeCsvToPathFromDictionary(
# eventsAndStat$hydraulic, "HYDRAULIC_EVENTS", settings,
# open.directory = FALSE
# )
#
# # Load rain data if available
# if (file.exists(rainDataFile)) {
# load(rainDataFile)
# } else {
# rainData <- NULL #(if you dont want to show rain data)
# }
#
# seriesNames <- rainGaugesCorrelatedWithStation(station = settings$station)
#
# hydraulicEvents <- eventsAndStat$hydraulic
#
# Vt <- settings$Vthresholds[settings$station]
#
# hydraulicEvents <- kwb.utils::renameColumns(
# hydraulicEvents, list(event = "eventNumber")
# )
#
# # Select events with a minimum volume
# selected <- !is.na(hydraulicEvents$V.m3) & hydraulicEvents$V.m3 > Vt
# events.to.plot <- hydraulicEvents[selected,]
#
# # plot hydraulic events with rain
# plot_hydraulic_events(
# hydraulicData = hydraulicData,
# settings = settings,
# eventsAndStat = events.to.plot,
# to.pdf = TRUE,
# rainData = rainData,
# gauges = seriesNames[1:3]
# )
#
# # Analyse one sampler event given by its number ("sampleEventIndex")
# # according to the order in the sample log files. By giving a negative index
# # you may choose events from the end (-1 means "last", -2 one before last,
# # etc.)
# processSampleEvent(
# hydraulicData, settings, events, eventsAndStat, sampleEventIndex = -2,
# to.pdf = FALSE
# )
```
# Script II: Finding and storing H/Q-regression models
The following script helps you to generate a regression model between water
level and discharge values. Raw data are loaded into `hydraulicData.raw`. Data
of time intervals that have been assessed as "invalid" (defined in
`intervalsToRemove`, see above) are removed. The resulting timeseries of H and Q are
displayed as well as a plot showing H over Q. You are provided a simple user
interface to further manipulate the selection of values to be used to calculate
a regression model.
```{r}
# Find H-Q regression models ---------------------------------------------------
if (FALSE)
{
# Read last available hydraulic data (required if station changed)
# "-10d" = last 10 days
hydraulicData.raw <- kwb.base::selectTimeInterval(read_hydraulics(settings), width="-30d")
#Remove bad Intervals
hydraulicData <- removeIntervals(
hydraulicData.raw,
intervals = intervalsToRemove[[settings$station]]
)
# run "regression finder" with a subset of the raw data
selectIntervalsForCorrelation(kwb.base::selectTimeInterval(hydraulicData, width="-30d"), settings)
# the model last applied is now available in the variable "regresssionState"
saveRegressionModel(regressionState$model, settings)
}
```