-
Notifications
You must be signed in to change notification settings - Fork 3
/
info.R
324 lines (296 loc) · 13.7 KB
/
info.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
#' Count data
#'
#' This function return data.frame with the number of localities, loggers and sensors of input myClim object.
#'
#' @template param_myClim_object
#' @return data.frame with count of localities, loggers and sensors
#' @export
#' @examples
#' count_table <- mc_info_count(mc_data_example_raw)
mc_info_count <- function(data) {
count_env <- .common_get_count_items(data)
result <- data.frame(item=c("localities", "loggers", "sensors"),
count=c(count_env$localities, count_env$loggers, count_env$sensors))
if(.common_is_agg_format(data)) {
result <- result[-2, ]
}
result
}
#' Call cleaning log
#'
#' @description
#' This function return data.frame with information from cleaning the loggers time series see [myClim::mc_prep_clean()]
#'
#' @template param_myClim_object_raw
#' @return data.frame with columns:
#' * locality_id - when provided by user then locality ID, when not provided identical with serial number
#' * serial_number - serial number of logger when provided or automatically detected from file name or header
#' * start_date - date of the first record on the logger
#' * end_date - date of the last record on the logger
#' * step_seconds - detected time step in seconds of the logger measurements.
#' * count_duplicities - number of duplicated records (identical time)
#' * count_missing - number of missing records (logger outage in time when it should record)
#' * count_disordered - number of records incorrectly ordered in time (newer followed by older)
#' * rounded - T/F indication whether myClim automatically rounded time series minutes to the closes half (HH:00, HH:30) e.g. 13:07 -> 13:00
#' @seealso [myClim::mc_prep_clean()]
#' @export
mc_info_clean <- function(data) {
.common_stop_if_not_raw_format(data)
logger_function <- function (logger) {
list(logger$metadata@serial_number,
min(logger$datetime),
max(logger$datetime),
logger$clean_info@step,
logger$clean_info@count_duplicities,
logger$clean_info@count_missing,
logger$clean_info@count_disordered,
logger$clean_info@rounded)
}
locality_function <- function(locality) {
items <- purrr::map(locality$loggers, logger_function)
purrr::map(items, ~ append(.x, locality$metadata@locality_id, after=0))
}
rows <- purrr::flatten(purrr::map(data$localities, locality_function))
columns <- purrr::transpose(rows)
data.frame(locality_id=unlist(columns[[1]]), serial_number=unlist(columns[[2]]),
start_date=.common_as_utc_posixct(unlist(columns[[3]])),
end_date=.common_as_utc_posixct(unlist(columns[[4]])),
step_seconds=unlist(columns[[5]]), count_duplicities=unlist(columns[[6]]),
count_missing=unlist(columns[[7]]), count_disordered=unlist(columns[[8]]),
rounded=unlist(columns[[9]]))
}
#' Get sensors info table
#'
#' This function return data.frame with info about sensors
#'
#' @template param_myClim_object
#' @return data.frame with columns:
#' * locality_id - when provided by user then locality ID, when not provided identical with serial number
#' * serial_number - serial number of logger when provided or automatically detected from file name or header
#' * sensor_id - original sensor id (e.g.,"GDD", "HOBO_T" ,"TMS_T1", "TMS_T2")
#' * sensor_name - original sensor id if not modified, if renamed then new name (e.g.,"GDD5", "HOBO_T_mean" ,"TMS_T1_max", "my_sensor01")
#' * start_date - the oldest record on the sensor
#' * end_date - the newest record on the sensor
#' * step_seconds - time step of records series (seconds)
#' * period - time step of records series (text)
#' * min_value - minimal recorded values
#' * max_value - maximal recorded value
#' * count_values - number of non NA records
#' * count_na - number of NA records
#' @export
#' @examples
#' mc_info(mc_data_example_agg)
mc_info <- function(data) {
is_raw_format <- .common_is_raw_format(data)
function_with_check_empty <- function(values, f) {
values <- values[!is.na(values)]
if(length(values) == 0) {
return(NA_real_)
}
f(values)
}
sensors_item_function <- function(locality_id, item, step, period) {
serial_number <- NA_character_
if(is_raw_format) {
serial_number <- item$metadata@serial_number
step <- as.integer(item$clean_info@step)
}
count <- length(item$sensors)
tibble::tibble(locality_id=rep(locality_id, count),
serial_number=rep(serial_number, count),
sensor_id=purrr::map_chr(item$sensors, function(x) x$metadata@sensor_id),
sensor_name=names(item$sensors),
start_date=rep(min(item$datetime), count),
end_date=rep(max(item$datetime), count),
step_seconds=rep(step, count),
period=rep(period, count),
min_value=purrr::map_dbl(item$sensors, function(x) function_with_check_empty(x$values, min)),
max_value=purrr::map_dbl(item$sensors, function(x) function_with_check_empty(x$values, max)),
count_values=purrr::map_int(item$sensors, function(x) length(x$values[!is.na(x$values)])),
count_na=purrr::map_int(item$sensors, function(x) length(x$values[is.na(x$values)])))
}
prep_locality_function <- function(locality) {
purrr::pmap_dfr(list(locality_id=locality$metadata@locality_id,
item=locality$loggers,
step=NA_integer_,
period=NA_character_),
sensors_item_function)
}
if(is_raw_format) {
result <- purrr::map_dfr(data$localities, prep_locality_function)
} else {
result <- purrr::pmap_dfr(list(locality_id=names(data$localities),
item=data$localities,
step=as.integer(data$metadata@step),
period=data$metadata@period),
sensors_item_function)
}
as.data.frame(result)
}
#' Get localities metadata table
#'
#' This function return data.frame with localities metadata
#'
#' @template param_myClim_object
#' @return data.frame with columns:
#' * locality_id
#' * lon_wgs84
#' * lat_wgs84
#' * elevation
#' * tz_offset
#' @export
#' @examples
#' mc_info_meta(mc_data_example_agg)
mc_info_meta <- function(data) {
localities <- data$localities
locality_function <- function (locality) {
list(locality_id = locality$metadata@locality_id,
lon_wgs84 = locality$metadata@lon_wgs84,
lat_wgs84 = locality$metadata@lat_wgs84,
elevation = locality$metadata@elevation,
tz_offset = locality$metadata@tz_offset
)
}
result <- purrr::map_dfr(localities, locality_function)
as.data.frame(result)
}
#' Get loggers info table
#'
#' This function returns a data.frame with information about loggers.
#'
#' This function is designed to work only with
#' myClim objects in **Raw-format**, where the loggers are organized at localities.
#' In **Agg-format**, myClim objects do not support loggers; sensors are directly connected to the locality.
#' See [myClim-package]. `mc_info_logger` does not work in Agg-format.
#'
#' @template param_myClim_object_raw
#' @return A data.frame with the following columns:
#' * locality_id - If provided by the user, it represents the locality ID; if not provided, it is identical to the logger's serial number.
#' * index - Logger index at the locality.
#' * serial_number - Serial number of the logger, either provided by the user or automatically detected from the file name or header.
#' * logger_type - Logger type.
#' * start_date - The oldest record on the logger.
#' * end_date - The newest record on the logger.
#' * step_seconds - Time step of the record series (in seconds).
#' @export
#' @examples
#' mc_info_logger(mc_data_example_raw)
mc_info_logger <- function(data) {
.common_stop_if_not_raw_format(data)
logger_function <- function(locality_id, logger_index, logger) {
step <- as.integer(logger$clean_info@step)
return(
list(locality_id=locality_id,
index=logger_index,
serial_number=logger$metadata@serial_number,
logger_type=logger$metadata@type,
start_date=min(logger$datetime),
end_date=max(logger$datetime),
step_seconds=step))
}
locality_function <- function(locality) {
purrr::pmap_dfr(list(
locality_id = locality$metadata@locality_id,
logger_index = seq_along(locality$loggers),
logger = locality$loggers),
logger_function)
}
result <- purrr::map_dfr(data$localities, locality_function)
as.data.frame(result)
}
#' Get joining info table
#'
#' This function returns a data.frame that contains information about the join operations.
#' Although this function performs the join process, it only returns an overview table,
#' not the actual joined data.
#'
#' This function is designed to work only with
#' myClim objects in **Raw-format**, where the loggers are organized at localities.
#' In **Agg-format**, myClim objects do not support loggers; sensors are directly connected to the locality.
#' See [myClim-package]. `mc_info_join` does not work in Agg-format.
#'
#' @template param_myClim_object_raw
#' @param comp_sensors parameter for [mc_join()] function (default NULL)
#' @return A data.frame with the following columns:
#' * locality_id - The ID of the locality.
#' * count_loggers - Number of loggers before the join operation.
#' * count_joined_loggers - Number of loggers after the join operation.
#' * count_data_conflicts - Number of different values in overlapping sensors.
#' * count_errors - Number of join-related errors. An error occurs when all sensors of the loggers have different names.
#' @export
mc_info_join <- function(data, comp_sensors=NULL) {
localities <- as.list(.join_main(data, comp_sensors, TRUE))
param_df <- purrr::map_dfr(localities, ~ .x)
result <- data.frame(locality_id=names(localities))
result[colnames(param_df)] <- param_df
for(colname in colnames(param_df)) {
result[[colname]] <- as.integer(result[[colname]])
}
return(result)
}
#' Get states (tags) info table
#'
#' This function return data.frame with information about sensor states (tags) see [myClim-package]
#'
#' This function is useful not only for inspecting actual states (tags) but also as
#' a template for manually manipulating states (tags) in a table editor such as Excel.
#' The output of `mc_info_states()` can be saved as a table, adjusted outside R (adding/removing/modifying rows),
#' and then read back into R to be used as input for [mc_states_insert] or [mc_states_update].
#'
#' @template param_myClim_object
#' @return data.frame with columns:
#' * locality_id - when provided by user then locality ID, when not provided identical with serial number
#' * logger_index - index of logger in myClim object at the locality
#' * logger_type - type of logger
#' * sensor_name - sensor name either original (e.g., TMS_T1, T_C), or calculated/renamed (e.g., "TMS_T1_max", "my_sensor01")
#' * tag - category of state (e.g., "error", "source", "quality")
#' * start - start datetime
#' * end - end datetime
#' * value - value of tag (e.g., "out of soil", "c:/users/John/tmsData/data_911235678.csv")
#' @export
#' @examples
#' mc_info_states(mc_data_example_raw)
mc_info_states <- function(data) {
is_raw_format <- .common_is_raw_format(data)
sensor_function <- function(locality_id, logger_index, logger_type, sensor) {
count <- nrow(sensor$states)
if(count == 0) {
return(tibble::tibble())
}
result <- tibble::tibble(locality_id=rep(locality_id, count),
logger_index=rep(logger_index, count),
logger_type=rep(logger_type, count),
sensor_name=rep(sensor$metadata@name),
tag=sensor$states$tag,
start=sensor$states$start,
end=sensor$states$end,
value=sensor$states$value)
return(result)
}
sensors_item_function <- function(locality_id, logger_index, logger_type, item) {
count <- length(item$sensors)
purrr::pmap_dfr(list(locality_id=rep(locality_id, count),
logger_index=rep(logger_index, count),
logger_type=rep(logger_type, count),
sensor=item$sensors),
sensor_function)
}
prep_locality_function <- function(locality) {
logger_types <- purrr::map_chr(locality$loggers, ~ .x$metadata@type)
purrr::pmap_dfr(list(locality_id=locality$metadata@locality_id,
logger_index=seq_along(locality$loggers),
logger_type=logger_types,
item=locality$loggers),
sensors_item_function)
}
if(is_raw_format) {
result <- purrr::map_dfr(data$localities, prep_locality_function)
} else {
result <- purrr::pmap_dfr(list(locality_id=names(data$localities),
logger_index=NA_integer_,
logger_type=NA_character_,
item=data$localities),
sensors_item_function)
}
as.data.frame(result)
}