/
import_data_basel.R
556 lines (470 loc) · 20.7 KB
/
import_data_basel.R
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
# multiSubstitute (copied from: "kwb.utils" R package to avoid importing it)
# Source: file.path(https://github.com/KWB-R/kwb.utils/blob",
# "59d4de1357932d2b8e20e9b7e48362350364c078/R/string.R")
#' Multiple Substitutions
#'
#' apply multiple substitutions on a vector of character. For each element in
#' \emph{replacements} gsub is called with the element name being the pattern
#' and the element value being the replacement.
#'
#' @param strings vector of character
#' @param replacements list of pattern = replacement pairs.
#' @param \dots additional arguments passed to gsub
#' @param dbg if \code{TRUE} (the default is \code{FALSE}) it is shown which
#' strings were replaced
#'
multiSubstitute <- function(strings, replacements, ..., dbg = FALSE)
{
for (pattern in names(replacements)) {
if (dbg) {
strings.bak <- strings
}
replacement <- replacements[[pattern]]
strings <- gsub(pattern, replacement, strings, ...)
if (dbg) {
changed <- strings != strings.bak
if (any(changed)) {
frequencies <- table(strings.bak[changed])
items <- sprintf("'%s' (%d-times)", names(frequencies), frequencies)
cat(sprintf(
paste0(
"In the following strings the parts matching the pattern ",
"'%s' are replaced with '%s':\n %s\n"
),
pattern, replacement, collapsed(items, ",\n ")
))
}
}
}
strings
}
#' Imports operational data for Basel (without metadata and only for one site
#' at once, e.g. "rhein" or "wiese")
#' @param xlsx_dir Define directory with raw data in EXCEL spreadsheet (.xlsx) to
#' be imported (default: system.file("shiny/basel/data/operation/wiese",
#' package = "aquanes.report"))
#' @return returns data frame with imported raw operational data
#' @importFrom readxl read_excel
#' @importFrom tidyr gather_
#' @export
import_operation_basel <- function(xlsx_dir = system.file(
"shiny/basel/data/operation/wiese",
package = "aquanes.report"
)) {
xlsx_files <- list.files(
path = xlsx_dir,
pattern = "\\.xls",
full.names = TRUE
)
for (xlsx_file in xlsx_files) {
print(sprintf("Importing: %s", xlsx_file))
tmp <- readxl::read_excel(path = xlsx_file)
if (xlsx_file == xlsx_files[1]) {
raw_data <- tmp
} else {
raw_data <- rbind(raw_data, tmp)
}
}
names(raw_data)[1] <- "DateTime"
print(sprintf("Setting time zone to 'CET'"))
raw_data <- aquanes.report::set_timezone(raw_data, tz = "CET")
raw_data_tidy <- tidyr::gather_(
data = raw_data,
key_col = "Parameter_Site_Unit",
value_col = "ParameterValue",
gather_cols = setdiff(names(raw_data), "DateTime")
)
raw_data_tidy$Source <- "online"
raw_data_tidy$DataType <- "raw"
return(raw_data_tidy)
}
#' Imports analytical data for Basel (without metadata)
#' @param csv_dir Define directory with raw analytical data in CSV (.csv) format to
#' be imported (default: system.file("shiny/basel/data/analytics",
#' package = "aquanes.report"))
#' @return returns data frame with imported raw analytics data
#' @importFrom janitor clean_names
#' @importFrom readxl read_excel
#' @importFrom utils read.csv2
#' @import dplyr
#' @export
import_analytics_basel <- function(csv_dir = system.file(
"shiny/basel/data/analytics",
package = "aquanes.report"
)) {
csv_files <- list.files(
path = csv_dir,
pattern = "\\.csv",
full.names = TRUE
)
for (csv_file in csv_files) {
print(sprintf("Importing: %s", csv_file))
tmp <- read.csv2(
file = csv_file,
na.strings = "",
stringsAsFactors = FALSE
) %>%
janitor::clean_names()
### Correct manually "prufpunkt_bezeichnung" for all "prufpunkt >= 94000" in
### case these are different from the "prufpunkt_bezeichnung" compared to
### "prufpunkt < 94000"
rep_strings <- list("Metolachlor OA" = "Metolachlor-OXA",
"Metolachlor ESA" = "Metolachlor-ESA",
"N-Acetyl-4-aminoantipyri" = "N-Acetyl-4-Aminoantipyri",
"\\<Cyprosulfamid\\>" = "Cyprosulfamide")
tmp$prufpunkt_bezeichnung_cor <-
multiSubstitute(strings = tmp$prufpunkt_bezeichnung,
replacements = rep_strings)
correction_df <- tmp %>%
dplyr::group_by_("prufpunkt_bezeichnung",
"prufpunkt") %>%
dplyr::summarise(total.count = n()) %>%
dplyr::select_("prufpunkt_bezeichnung",
"prufpunkt") %>%
dplyr::filter_("prufpunkt < 94000 | prufpunkt %in% c(94004,94006)") %>%
dplyr::rename_("prufpunkt_bezeichnung_cor" = "prufpunkt_bezeichnung",
"prufpunkt_cor" = "prufpunkt")
tmp <- tmp %>%
dplyr::left_join(correction_df) %>%
dplyr::mutate(DateTime = as.POSIXct(strptime(
x = paste(
tmp$datum,
tmp$uhrzeit
),
format = "%d.%m.%Y %H:%M"
))) %>%
dplyr::rename(
SiteCode = "probestelle",
ParameterCode_Org = "prufpunkt",
ParameterCode = "prufpunkt_cor",
ParameterName_Org = "prufpunkt_bezeichnung",
ParameterName_Cor = "prufpunkt_bezeichnung_cor",
ParameterOperator = "operator",
ParameterValue = "messwert",
ParameterUnit_Org = "einheit",
Method_Org = "methode",
MethodName_Org = "methoden_bezeichnung"
) %>%
dplyr::select(
"DateTime",
"SiteCode",
"ParameterCode_Org",
"ParameterCode",
"ParameterName_Org",
"ParameterName_Cor",
"ParameterOperator",
"ParameterValue",
"ParameterUnit_Org",
"Method_Org",
"MethodName_Org"
)
if (csv_file == csv_files[1]) {
raw_data <- tmp
} else {
raw_data <- rbind(raw_data, tmp)
}
}
print(sprintf("Setting time zone to 'CET'"))
raw_data <- aquanes.report::set_timezone(raw_data, tz = "CET")
raw_data$ParameterValue <- as.numeric(raw_data$ParameterValue)
raw_data$Source <- "offline"
raw_data$DataType <- "raw"
return(raw_data)
}
#' Helper function: add site metadata
#' @param df data frame containing at least a column "SiteCode"
#' @param df_col_sitecode column in df containing site code (default: "SiteCode")
#' @param meta_site_path Define path of "meta_site.csv" to be imported
#' (default: system.file("shiny/basel/data/metadata/meta_site.csv",
#' package = "aquanes.report"))
#' @return returns input data frame with joined metadata
#' @importFrom tidyr separate_
#' @export
add_site_metadata <- function(df,
df_col_sitecode = "SiteCode",
meta_site_path = system.file(
"shiny/basel/data/metadata/meta_site.csv",
package = "aquanes.report"
)) {
meta_site <- read.csv(
file = meta_site_path,
stringsAsFactors = FALSE,
na.strings = ""
)
res <- df %>%
tidyr::separate_(
col = df_col_sitecode,
sep = "-",
into = paste0("SiteName", 1:3),
remove = FALSE
)
for (siteID in 1:3) {
print(sprintf(
"Replacing SiteCode%d with SiteName%d",
siteID,
siteID
))
col_sitename <- paste0("SiteName", siteID)
sites <- meta_site[meta_site$SiteID == siteID, ]
if (nrow(sites) > 0) {
for (site_idx in 1:nrow(sites)) {
sel_site <- sites[site_idx, ]
strings_to_replace <- !is.na(res[, col_sitename]) & res[, col_sitename] == sel_site$SiteLocation
if (sum(strings_to_replace) > 0) {
res[strings_to_replace, col_sitename] <- sel_site$SiteLocationName
}
}
}
res[is.na(res[, col_sitename]), col_sitename] <- ""
}
res$SiteName <- sprintf(
"%s (%s %s)",
res$SiteName1,
res$SiteName2,
res$SiteName3
)
return(res)
}
#' Helper function: add parameter metadata
#' @param df data frame containing at least a column "ParameterCode"
#' @param meta_parameter_path Define path of "meta_parameter.csv" to be imported
#' (default: system.file("shiny/basel/data/metadata/meta_parameter.csv",
#' package = "aquanes.report"))
#' @return returns input data frame with joined metadata (parameter codes/ methods
#' not included in meta_parameter file will not be imported!!!!)
#' @importFrom dplyr left_join
#' @export
add_parameter_metadata <- function(df,
meta_parameter_path = system.file(
"shiny/basel/data/metadata/meta_parameter.csv",
package = "aquanes.report"
)) {
meta_parameter <- read.csv(
file = meta_parameter_path,
stringsAsFactors = FALSE,
na.strings = ""
)
res <- df %>%
dplyr::inner_join(meta_parameter)
return(res)
}
#' Helper function: add label ("SiteName_ParaName_Unit_Method")
#' @param df data frame containing at least a columns "SiteName", "ParameterName",
#' "ParameterUnit" and optionally "Method_Org" (if not existent no "Method_Org" will be
#' available!)
#' @param col_sitename column in df containing site name (default: "SiteName")
#' @param col_parametername column in df containing parameter name (default: "ParameterName")
#' @param col_parameterunit column in df containing parameter unit (default: "ParameterUnit")
#' @param col_method column in df containing method code (default: "Method_Org")
#' @return returns input data frame with added column "SiteName_ParaName_Unit_Method"
#' @export
add_label <- function(df,
col_sitename = "SiteName",
col_parametername = "ParameterName",
col_parameterunit = "ParameterUnit",
col_method = "Method_Org") {
col_method_exists <- col_method %in% names(df)
if(col_method_exists) {
boolean_no_method <- is.na(df[,col_method]) | df[,col_method] == ""
} else {
boolean_no_method <- rep(TRUE, times = nrow(df))
}
df$SiteName_ParaName_Unit_Method <- ""
if(any(boolean_no_method)) {
ind <- which(boolean_no_method)
df$SiteName_ParaName_Unit_Method[ind] <- sprintf(
"%s: %s (%s)",
df[ind , col_sitename],
df[ind , col_parametername],
df[ind , col_parameterunit]) }
if(any(!boolean_no_method)) {
ind <- which(!boolean_no_method)
df$SiteName_ParaName_Unit_Method[!boolean_no_method] <- sprintf(
"%s: %s (%s, Method: %s)",
df[ind , col_sitename],
df[ind , col_parametername],
df[ind , col_parameterunit],
df[ind , col_method]
)
}
return(df)
}
#' Imports operational data for Basel (with metadata for
#' both sites at once, i.e. "rhein" and "wiese")
#' @param raw_dir_rhein Define directory for site "rhein" with raw data in
#' EXCEL spreadsheet format (.xlsx) to be imported (default:
#' system.file("shiny/basel/data/operation/rhein", package = "aquanes.report"))
#' @param raw_dir_wiese Define directory for site "rhein" with raw data in
#' EXCEL spreadsheet format (.xlsx) to be imported (default:
#' system.file("shiny/basel/data/operation/wiese", package = "aquanes.report"))
#' @param meta_online_path path to file containing metadata for online data
#' (default: system.file("shiny/basel/data/metadata/meta_online.csv",
#' package = "aquanes.report"))
#' @param meta_site_path Define path of "meta_site.csv" to be imported
#' (default: system.file("shiny/basel/data/metadata/meta_site.csv",
#' package = "aquanes.report"))
#' @param meta_parameter_path Define path of "meta_parameter.csv" to be imported
#' (default: system.file("shiny/basel/data/metadata/meta_parameter.csv",
#' package = "aquanes.report"))
#' @return returns data frame with imported raw operational data with metadata
#' for both sites (i.e."rhein" and "wiese")
#' @importFrom dplyr left_join
#' @return data.frame with operational data for Basel sites including metadata
#' @export
import_operation_meta_basel <- function(
raw_dir_rhein = system.file(file.path(
"shiny",
"basel/data/operation/rhein"
), package = "aquanes.report"),
raw_dir_wiese = system.file(
"shiny/basel/data/operation/wiese",
package = "aquanes.report"
),
meta_online_path =
system.file(
"shiny/basel/data/metadata/meta_online.csv",
package = "aquanes.report"
),
meta_site_path =
system.file(
"shiny/basel/data/metadata/meta_site.csv",
package = "aquanes.report"
),
meta_parameter_path =
system.file(
"shiny/basel/data/metadata/meta_parameter.csv",
package = "aquanes.report"
)) {
meta_online <- read.csv(
file = meta_online_path,
stringsAsFactors = FALSE,
na.strings = ""
)
online_meta <- add_site_metadata(
df = meta_online,
meta_site_path = meta_site_path
) %>%
add_parameter_metadata(meta_parameter_path = meta_parameter_path) %>%
add_label()
### 1.1) Wiese: Import XLSX data and join with metadata
print("###################################################################")
print("######## Importing operational data with metadata for site 'Wiese'")
print("###################################################################")
wiese <- import_operation_basel(xlsx_dir = raw_dir_wiese) %>%
dplyr::left_join(online_meta[grep(
pattern = "WF",
online_meta$SiteCode
), ])
### 1.2) Rhein: Import XLSX data and join with metadata
print("###################################################################")
print("######## Importing operational data with metadata for site 'Rhein'")
print("###################################################################")
rhein <- import_operation_basel(xlsx_dir = raw_dir_rhein) %>%
dplyr::left_join(online_meta[grep(
pattern = "RF",
online_meta$SiteCode
), ])
basel <- rbind(wiese, rhein)
return(basel)
}
#' Imports analytical data for Basel (with metadata for both sites at once, i.e.
#' "rhein" and "wiese")
#' @param analytics_dir Define directory with raw analytical data in CSV (.csv) format to
#' be imported (default: system.file("shiny/basel/data/analytics",
#' package = "aquanes.report"))
#' @param meta_site_path Define path of "meta_site.csv" to be imported
#' (default: system.file("shiny/basel/data/metadata/meta_site.csv",
#' package = "aquanes.report"))
#' @param meta_parameter_path Define path of "meta_parameter.csv" to be imported
#' (default: system.file("shiny/basel/data/metadata/meta_parameter.csv",
#' package = "aquanes.report"))
#' @return data.frame with analytics data for Basel sites including metadata
#' @export
import_analytics_meta_basel <- function(
analytics_dir = system.file(
"shiny/basel/data/analytics",
package = "aquanes.report"
),
meta_site_path = system.file(
"shiny/basel/data/metadata/meta_site.csv",
package = "aquanes.report"
),
meta_parameter_path = system.file(
"shiny/basel/data/metadata/meta_parameter.csv",
package = "aquanes.report"
)) {
print("###################################################################")
print("###### Importing analytics data with metadata for sites 'Wiese' and Rhein'")
print("###################################################################")
analytics_meta_data <- import_analytics_basel(csv_dir = analytics_dir) %>%
add_site_metadata(meta_site_path = meta_site_path) %>%
add_parameter_metadata(meta_parameter_path = meta_parameter_path) %>%
add_label()
return(analytics_meta_data)
}
#' Imports operational & analytical data for Basel (with metadata for both sites
#' at once, i.e. "rhein" and "wiese")
#' @param analytics_dir Define directory with raw analytical data in CSV (.csv) format to
#' be imported (default: system.file("shiny/basel/data/analytics",
#' package = "aquanes.report"))
#' @param raw_dir_rhein Define directory for site "rhein" with raw data in
#' EXCEL spreadsheet format (.xlsx) to be imported (default:
#' system.file("shiny/basel/data/operation/rhein", package = "aquanes.report"))
#' @param raw_dir_wiese Define directory for site "rhein" with raw data in
#' EXCEL spreadsheet format (.xlsx) to be imported (default:
#' system.file("shiny/basel/data/operation/wiese", package = "aquanes.report"))
#' @param meta_online_path path to file containing metadata for online data
#' (default: system.file("shiny/basel/data/metadata/meta_online.csv",
#' package = "aquanes.report"))
#' @param meta_parameter_path Define path of "meta_parameter.csv" to be imported
#' (default: system.file("shiny/basel/data/metadata/meta_parameter.csv",
#' package = "aquanes.report"))
#' @param meta_site_path Define path of "meta_site.csv" to be imported
#' (default: system.file("shiny/basel/data/metadata/meta_site.csv",
#' package = "aquanes.report"))
#' @return data.frame with analytical & operational data for Basel
#' @importFrom plyr rbind.fill
#' @export
import_data_basel <- function(
analytics_dir = system.file(
"shiny/basel/data/analytics",
package = "aquanes.report"
),
raw_dir_rhein = system.file(
"shiny/basel/data/operation/rhein",
package = "aquanes.report"
),
raw_dir_wiese = system.file(
"shiny/basel/data/operation/wiese",
package = "aquanes.report"
),
meta_online_path = system.file(
"shiny/basel/data/metadata/meta_online.csv",
package = "aquanes.report"
),
meta_parameter_path = system.file(
"shiny/basel/data/metadata/meta_parameter.csv",
package = "aquanes.report"
),
meta_site_path = system.file(
"shiny/basel/data/metadata/meta_site.csv",
package = "aquanes.report"
)) {
operation_meta <- import_operation_meta_basel(
raw_dir_rhein = raw_dir_rhein,
raw_dir_wiese = raw_dir_wiese,
meta_online_path = meta_online_path,
meta_site_path = meta_site_path,
meta_parameter_path = meta_parameter_path
)
analytics_meta <- import_analytics_meta_basel(
analytics_dir = analytics_dir,
meta_site_path = meta_site_path,
meta_parameter_path = meta_parameter_path
)
print("###################################################################")
print("######## Add analytical to the operational data (including metadata)")
print("###################################################################")
data_basel <- plyr::rbind.fill(operation_meta, analytics_meta)
return(data_basel)
}