Skip to content

Commit

Permalink
Read all mappings from files
Browse files Browse the repository at this point in the history
  • Loading branch information
bpbond committed Oct 18, 2024
1 parent d394f71 commit e20daa3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
32 changes: 18 additions & 14 deletions synoptic/L0-instrument.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,15 @@ Working directory is `r getwd()`.

```{r AquaTROLL600}
message("Reading aquaTROLL600 column mappings...")
map_raw <- read_csv(file.path(INS_MAPS, "aquaTROLL600.csv"), col_types = "cc")
map <- map_raw$datalogger_name
names(map) <- map_raw$aquaTROLL600_name
map_raw <- read_csv(file.path(INS_MAPS, "aquaTROLL600_fields.csv"), col_types = "cc")
map <- map_raw$datalogger_field
names(map) <- map_raw$aquaTROLL600_field
logger_map <- c("26" = "Compass_CRC_TR_302") # TODO - put into a metadata table
troll_map <- read_csv(file.path(INS_MAPS, "aquaTROLL600_devices.csv"), col_types = "ccc")
logger_names <- troll_map$Logger
names(logger_names) <- troll_map$Serial_number
troll_letters <- troll_map$Letter
names(troll_letters) <- troll_map$Serial_number
files <- list.files(file.path(RAW, "aquaTROLL600"),
pattern = "*.csv$",
Expand All @@ -81,13 +85,13 @@ for(f in files) {
if(!grepl("Format Version = 1", x_raw[2])) {
stop("This file is not in the expected format")
}
device_id <- gsub("Device Id = ", "", x_raw[3])
device_id <- gsub(",", "", device_id)
message("\tDevice ID = ", device_id)
if(!device_id %in% names(logger_map)) {
stop("Unknown device! No entry in the logger_map")
device_sn <- gsub("Device S/N = ", "", x_raw[6])
device_sn <- gsub(",", "", device_sn)
message("\tDevice S/N = ", device_sn)
if(!device_sn %in% troll_map$Serial_number) {
stop("Unknown device! No entry in the troll_map")
}
x <- read_csv(I(x_raw[-1:-9]), show_col_types = FALSE)
message("\tData rows = ", nrow(x))
Expand All @@ -103,21 +107,21 @@ for(f in files) {
}
colnames(x) <- map[colnames(x)]
# Add the "A" or "B", etc., as specified by the logger map
colnames(x)[-1] <- paste0(colnames(x)[-1], "B")
colnames(x)[-1] <- paste0(colnames(x)[-1], troll_letters[device_sn])
# Change timestamp to datalogger format; restructure to
# "Logger","Table","TIMESTAMP","loggernet_variable","value","ID"
x$TIMESTAMP <- as.character(mdy_hm(x$TIMESTAMP))
x_out <- pivot_longer(x, -TIMESTAMP, names_to = "loggernet_variable")
x_out$Table <- "WaterLevel600B"
x_out$Logger <- logger_map[device_id]
x_out$Table <- paste0("WaterLevel600", troll_letters[device_sn])
x_out$Logger <- logger_names[device_sn]
x_out$ID <- sapply(apply(x_out, 1, paste, collapse = ""),
FUN = function(x) {
substr(digest::digest(x, algo = "md5"), 1, 16)
})
out_fn <- paste(logger_map[device_id], x_out$Table[1], basename(f), sep = "_")
out_fn <- paste(logger_names[device_sn], x_out$Table[1], basename(f), sep = "_")
message("\tWriting ", out_fn)
write.csv(x_out, file.path(L0, out_fn), row.names = FALSE)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Serial_number,Logger,Letter
848067,Compass_CRC_TR_302,B
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
datalogger_name,aquaTROLL600_name
datalogger_field,aquaTROLL600_field
TIMESTAMP,Date Time
Aquatroll_ID(1),Device Id (in file header)
Barometric_Pressure600,16: Barometer (16) mmHg (22)
Expand Down

0 comments on commit e20daa3

Please sign in to comment.