R/odf_create_hobo.R

Defines functions odf_create_hobo

Documented in odf_create_hobo

#' Format Hobo Temperature Logger Data for conversion to ODF or NetCDF formats
#'
#' @description `odf_create_hobo()` formats and compiles all the necessary
#'   information in order to interpret a Hobo Temperature Logger data file for
#'   addition to a larger dataset.
#'
#' @param file an Onset Hobo Temperature logger data CSV file.
#' @param column_names Character vector of columns names for the file. Defautls
#'   are `datetime`, `abs_pressure`, and `temp`
#' @param timezone The timezone for the temperature logger. Care needs to be
#'   taken here to make sure you understand what timezone the logger was setup
#'   in as this will need to take into account time zones and time changes.
#'   Standard time (AST) in NB/NS is -0400 and Daylight Savings Time (ADT) is
#'   -0300
#'
#'   Function will convert everything to unambiguous ISO8601 formatted date
#'   times. `https:\/\/en.wikipedia.org\/wiki\/ISO_8601
#' @param latitude latitude in decimal degrees North(dd.ddddd\deg) where the
#'   instrument was moored.
#' @param longitude longitude in decimal degrees East (-ddd.dddd\deg) where the
#'   instrument was deployed *should be negative in the Western Hemisphere!*
#' @param depth_m depth of the instrument in meters.
#' @param depth_type depth type for the instrument deployment, either `surface`
#'   if moored a certain distance from the surface (e.g. moored to a floating
#'   platform), or `bottom` if moored a set distance from the water column
#'   bottom (e.g. moored to solid wharf).
#'
#' @returns a list of information regarding the Hobo temperature logger so that
#'   it can be converted into other formats
#'
#' @import dplyr
#' @import stringr
#'
#' @export
#'
#' @examples odf_create_hobo(file, timezone = "+0000", latitude = 45, longitude = -65, depth_m = 1, depth_type = "surface")
#'
odf_create_hobo <- function(file,
                            column_names = c("datetime", "abs_pressure", "temp"),
                            timezone = c("UTC", "-0300", "-0400")) {
  
  header_info <- readLines(file, n = 2) %>% 
    stringr::str_extract(pattern = '(?<=S/N: )[0-9]*(?=,)')
  
  timezone_file <- readLines(file, n = 2) %>% 
    stringr::str_extract(pattern = "(?<=Date Time, GMT).{1,6}") %>%
    stringr::str_remove(pattern = ":")
  
  
  x <- list(
    data = read_csv(
      file,
      skip = 1,
      col_select = 2:4,
      show_col_types = FALSE
    ) %>%
      rename_with(~ column_names) %>%
      mutate(datetime = format(
        as.POSIXct(datetime, format = "%m/%d/%y %H:%M:%S %p", tz = timezone),
        format = "%Y-%m-%dT%H:%M:%OS2%z"
      )),
    metadata = list(tz = timezone,
                    latitude = latitude,
                    longitude = longitude,
                    depth = depth_m,
                    depth_type = depth_type,
                    serial_number = header_info[2])
  )
  
  return(x)
  
}
pkraska/CESD documentation built on June 15, 2025, 6:32 a.m.