mainscntanalysis/secondDbSteps.r

59 lines
1.6 KiB
R
Raw Normal View History

2021-08-09 13:38:11 +02:00
library(DBI)
library(tidyr)
2021-08-09 23:24:42 +02:00
library(dplyr)
library(lubridate)
library(R.utils)
2021-08-09 13:38:11 +02:00
2021-08-09 18:10:31 +02:00
con <- dbConnect(RPostgres::Postgres(),
dbname='mainscnt',
2021-08-09 23:24:42 +02:00
host='db.mainscnt.eu',
2021-08-09 18:10:31 +02:00
user='wn')
2021-08-09 23:24:42 +02:00
START <- "2021-08-03 00:00:00"
INTERVAL <- 3600
for (offset in 0:23) {
startDate <- ymd_hms(START) + INTERVAL * offset
endDate <- startDate + INTERVAL
startStr <- strftime(startDate, "%Y-%m-%d %H:%M:%S", tz="UTC")
endStr <- strftime(endDate, "%Y-%m-%d %H:%M:%S", tz="UTC")
res <-dbSendQuery(con, "select time, location, freq from mainsfrequency where valid=1 and time >= $1 and time < $2")
dbBind(res, list(startStr, endStr))
frequencies <- dbFetch(res)
dbClearResult(res)
freq_wide <- frequencies %>%
pivot_wider(names_from = location,
values_from = freq,
values_fn = mean)
freq_wide$Linz_AT <- freq_wide$Linz_AT + 0.002
THRESHOLD <- 0.5
for (colIdx in 2:length(freq_wide)) {
last <- freq_wide[[1, colIdx]]
for (rowIdx in 1:length(freq_wide[[colIdx]])) {
current <- freq_wide[[rowIdx, colIdx]]
if (!is.na(current) && !is.na(last) && (abs(current - last) > THRESHOLD)) {
freq_wide[[rowIdx, colIdx]] = NA
}
last <- current
2021-08-09 18:10:31 +02:00
}
}
2021-08-09 23:24:42 +02:00
freq_wide <- freq_wide %>%
rowwise() %>%
mutate(mad = mad(c_across(names(freq_wide)[-1]),
na.rm=TRUE))
# print(summary(freq_wide))
printf("start: %s, end: %s, mad/cnt: %f\n", startStr, endStr, sum(freq_wide$mad) / length(freq_wide$mad))
2021-08-09 18:10:31 +02:00
}
2021-08-09 23:24:42 +02:00
2021-08-09 18:10:31 +02:00
dbDisconnect(con)