#' Convert WC Instrument Helper file to trawllight csv format
#'
#' Converts an R table file generated by WC Instrument helper to a CSV format that is compatible with trawllight.
#'
#' @param file_path Path to the file to be converted.
#' @export
#'
mk9_convert_wctab <- function(file_path) {
file_base_name <- basename(file_path)
file_dir_name <- dirname(file_path)
if(!file.exists(file_path)) {
stop(paste0("mk9_convert_wctab: ", file_path, " not found."))
}
if(!grepl("\\.tab$", file_path)) {
stop(paste0("mk9_convert_wctab: ", file_path, " not imported. Must be a readable R data table (.tab) file"))
}
tab_df <- read.table(file = file_path)
if("Wet.Dry.Sensor" %in% names(tab_df)) {
lcond <- tab_df$Wet.Dry.Sensor
tab_df <- dplyr::select(tab_df, -Wet.Dry.Sensor)
} else {
lcond <- NA
}
if(!all(names(tab_df) == c("Time","Depth","Temperature","Light.Level"))) {
stop(paste0("mk9_convert_wctab: Column names or order in ",
file_path,
" is incorrect. Detected: ",
paste(names(tab_df), collapse = ", "), ". Must be: Time, Depth, Temperature, Light.Level"))
}
names(tab_df) <- c("ldatetime", "Depth", "Temperature", "Light Level")
ldatetime <- as.POSIXct(tab_df$ldatetime, origin = "1970-01-01", tz = "UTC")
tab_df <- data.frame(Date = paste(lubridate::month(ldatetime),
sprintf("%02d", lubridate::day(ldatetime)),
lubridate::year(ldatetime), sep = "/"),
Time = paste(sprintf("%02d", lubridate::hour(ldatetime)),
sprintf("%02d", lubridate::minute(ldatetime)),
sprintf("%02d", lubridate::second(ldatetime)), sep = ":"),
Depth = tab_df$Depth,
Temperature = tab_df$Temperature,
`Light Level` = tab_df$`Light Level`)
tab_df$lcond <- lcond
out_path <- paste0(file_dir_name, "/", gsub(pattern = ".tab",
replacement = ".csv",
x = file_base_name))
print(paste0("mk9_convert_wctab: Writing output to ", out_path))
write.csv(x = tab_df, file = out_path, row.names = FALSE)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.