R/load_3d.R

Defines functions load_3d

Documented in load_3d

#' Load 3d model variable
#'
#' Loads a 3d text output file generated by GOTM as a dataframe. If water level is constant depths of measurements can be assigned to column headers
#'
#' @param val.file value file; File path to GOTM output file containing the values
#' @param z.file depths file; File path to GOTM output file containing the depths which corresponds to the value file
#' @param col.deps logical; Assigns depths to column names in the format for rLakeAnalyzer. Defaults to FALSE
#' @param tz Timezone string to be supplied to as.POSIXct. Defaults to 'UTC'. This often can be left to the default unless timezone support is specifically required.
#' @return Dataframe with Date time and values in wide format
#' @import utils
#' @export
load_3d <- function(val.file,z.file =NULL, col.deps = F, tz = 'UTC'){
  #Read in modelled water temp
  mod = read.delim(val.file, skip = 9, header = FALSE)

  #sequence from the max depth to 0.01 in 50 steps to match up the layers
  if(col.deps == T){
    #read in the layers file to get the depths of the layers
    layers = read.delim(z.file, skip = 9, header = FALSE)
    #layers[,1] = as.POSIXct(layers[,1], tz = 'UTC')
    #layers = layers[,c(1,51:2)]
    dep = round(-layers[1,ncol(layers):2],2)
    #dep = round(seq(to = 0.01, from = (range(layers[,2:ncol(layers)])[2]-range(layers[,2:ncol(layers)])[1]), length.out = (ncol(layers)-1)),2)
    colnames(mod) = c('datetime',paste0('wtr_', dep))#wtr_1 is top of water, wtr_50 is bottom
  }
  #Format Dataframe for working with LakeAnalyzer
  colnames(mod)[1] = c('datetime')
  mod = mod[,c(1,ncol(mod):2)] # reverse the order of the columns smallest to largest
  mod[,1] = as.POSIXct(mod[,1], tz = tz) #format datetime
  return(mod)
}
tadhg-moore/gotmtools documentation built on Oct. 9, 2019, 2:48 p.m.