/
import_data_haridwar.R
198 lines (154 loc) · 7.31 KB
/
import_data_haridwar.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
#' Imports Haridwar data
#' @param analytics_path Define path of analytics EXCEL spreadsheet to be
#' imported (default: system.file(file.path("shiny/haridwar/data",
#' "analytics.xlsx"),
#' package = "aquanes.report"))
#' @param operation_mySQL_conf column name pattern for identifying raw data
#' (default: system.file("shiny/haridwar/.my.cnf", package = "aquanes.report"))
#' @param operation_meta_path path to table with meta data for operational
#' parameters (default: system.file(file.path("shiny/haridwar/data",
#' "operation_parameters.csv"), package = "aquanes.report"))
#' @param excludedSheets all sheets, which are not listed here will be imported
#' as lab data sheets (default: c("Parameters", "Location", "Sites", "#Summary",
#' "Site_and_Parameter", "Observations", "dP", "ORP", "Flow", "Current_Voltage",
#' "As_total_Arsenator"))
#' @param skip number of rows to skip for each lab data sheet (default: 69), i.e.
#' for all sheets which are not explictly excluded with parameter "excludedSheets"
#' @param debug if TRUE print debug messages (default: TRUE)
#' @return returns data frame with Haridwar raw data (operation & analytics)
#' @import readxl
#' @import tidyr
#' @import dplyr
#' @importFrom plyr rbind.fill
#' @importFrom utils read.csv
#' @export
import_data_haridwar <- function(analytics_path = system.file(
file.path(
"shiny",
"haridwar/data/analytics.xlsx"
),
package = "aquanes.report"
),
operation_mySQL_conf = system.file(
"shiny/haridwar/.my.cnf",
package = "aquanes.report"
),
operation_meta_path = system.file(
file.path(
"shiny/haridwar/data",
"operation_parameters.csv"
),
package = "aquanes.report"
),
excludedSheets = c(
"Parameters",
"Location",
"Sites",
"#Summary",
"Site_and_Parameter",
"Observations",
"dP",
"ORP",
"Flow",
"Current_Voltage",
#"SAK_254",
#"SAK_463",
"As_total_Arsenator"
),
skip = 69,
debug = TRUE) {
if (!file.exists(analytics_path)) {
msg <- sprintf(
"No analytics file %s is located under: %s",
basename(analytics_path),
dirname(analytics_path)
)
stop(
msg,
call. = FALSE
)
}
if (!file.exists(operation_mySQL_conf)) {
msg <- sprintf("No '.my.cnf' file located under: %s\n.
Please once specify the path to a valid MySQL config file with parameter
'mySQL_conf'", dirname(operation_mySQL_conf))
stop(
msg,
call. = FALSE
)
}
###############################################################################
###############################################################################
###############################################################################
#### Site 1: Haridwar
###############################################################################
###############################################################################
###############################################################################
###############################################################################
#### 1) Import analytics data from EXCEL spreadsheet
###############################################################################
if (debug) print("### Step 1: Import analytics data ##########################")
all_sheets <- readxl::excel_sheets(path = analytics_path)
analytics_to_import <- all_sheets[!all_sheets %in% excludedSheets]
analytics_4014 <- import_sheets(
xlsPath = analytics_path,
sheets_analytics = analytics_to_import,
skip = skip
)
drop.cols <- c("Who", "Comments", "LocationName", "LocationID")
select.cols <- dplyr::setdiff(names(analytics_4014), drop.cols)
analytics_4014 <- analytics_4014[, select.cols] %>%
dplyr::filter_("!is.na(ParameterValue)") %>%
dplyr::mutate_(Source = "as.character('offline')")
###############################################################################
#### 2) Operational data
###############################################################################
if (debug) print("### Step 2: Import operational data ##########################")
#### 2.1) Import
operation <- import_operation(mysql_conf = operation_mySQL_conf)
if (debug) print("### Step 3: Standardise analytics & operational data ##########################")
drop.cols <- c("AnlagenID", "LocationName", "id", "localTime")
select.cols <- dplyr::setdiff(names(operation), drop.cols)
operation <- operation[, select.cols]
operation_list <- tidyr::gather_(
data = operation,
key_col = "ParameterCode",
value_col = "ParameterValue",
gather_cols = dplyr::setdiff(names(operation), "DateTime")
) %>%
dplyr::filter_("!is.na(ParameterValue)")
sites_meta <- analytics_4014 %>%
group_by_(~SiteCode, ~SiteName) %>%
summarise_(n = "n()") %>%
select_(~SiteCode, ~SiteName)
operation_para_names <- utils::read.csv(
file = operation_meta_path,
stringsAsFactors = FALSE
)
cols_to_drop <- c(
"Comments",
"ParameterThresholdComparison",
"ParameterThreshold",
"ParameterThresholdSource"
)
operation_para_names <- operation_para_names %>%
dplyr::select_(.dots = dplyr::setdiff(names(operation_para_names), cols_to_drop)) %>%
left_join(sites_meta)
operation_para_names$SiteName[is.na(operation_para_names$SiteName)] <- "General"
operation_list <- operation_list %>%
dplyr::left_join(operation_para_names) %>%
dplyr::mutate_(
Source = "as.character('online')",
DataType = "as.character('raw')"
)
haridwar_raw_list <- plyr::rbind.fill(operation_list, analytics_4014) %>%
dplyr::filter_("!is.na(ParameterValue)") %>%
dplyr::mutate_(
"SiteName_ParaName_Unit" = "ifelse(test = ParameterName == 'Redox potential' & SiteName == 'Tank water',
sprintf('%s: %s %d (%s)', SiteName, ParameterName, measurementID, ParameterUnit),
sprintf('%s: %s (%s)', SiteName, ParameterName, ParameterUnit))",
"measurementID" = "ifelse(test = ParameterName == 'Redox potential' & SiteName == 'Tank water',
1, measurementID)"
)
return(haridwar_raw_list)
}