R/J_swLog.R

Defines functions swLog

Documented in swLog

###############################################################################
#rSOILWAT2
#    Copyright (C) {2009-2018}  {Ryan Murphy, Daniel Schlaepfer,
#    William Lauenroth, John Bradford}
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
###############################################################################


# TODO: Add comment
#
# Author: Ryan J. Murphy (2013)
###############################################################################


#' Class \code{"swLog"}
#'
#' The methods listed below work on this class and the proper slot of the class
#'   \code{\linkS4class{swInputData}}.
#'
#' @param ... Arguments to the helper constructor function.
#'  Dots can either contain objects to copy into slots of that class
#'  (must be named identical to the corresponding slot) or
#'  be one object of that class (in which case it will be copied and
#'  any missing slots will take their default values).
#'  If dots are missing, then corresponding values of
#'  \code{rSOILWAT2::sw_exampleData}
#'  (i.e., the \pkg{SOILWAT2} "testing" defaults) are copied.
#'
#' @seealso \code{\linkS4class{swInputData}} \code{\linkS4class{swFiles}}
#' \code{\linkS4class{swWeather}} \code{\linkS4class{swCloud}}
#' \code{\linkS4class{swMarkov}} \code{\linkS4class{swProd}}
#' \code{\linkS4class{swSite}} \code{\linkS4class{swSoils}}
#' \code{\linkS4class{swEstab}} \code{\linkS4class{swOUT}}
#' \code{\linkS4class{swSWC}} \code{\linkS4class{swInputData}}
#'
#' @examples
#' showClass("swLog")
#' x <- new("swLog")
#' x <- swLog()
#'
#' @name swLog-class
#' @export
setClass(
  "swLog",
  slot = c(LogData = "character", MaxLines = "integer", UsedLines = "integer"),
  prototype = c(
    LogData = NA_character_,
    MaxLines = NA_integer_,
    UsedLines = NA_integer_
  )
)


#' @rdname swLog-class
#' @export
swLog <- function(...) {
  def <- slot(rSOILWAT2::sw_exampleData, "log")
  sns <- slotNames("swLog")
  dots <- list(...)
  if (length(dots) == 1 && inherits(dots[[1]], "swLog")) {
    # If dots are one object of this class, then convert to list of its slots
    dots <- attributes(unclass(dots[[1]]))
  }
  dns <- names(dots)

  # We don't set values for any slots; this is to prevent simulation runs with
  # accidentally incorrect values
  if (!("MaxLines" %in% dns)) {
    dots[["MaxLines"]] <- 150L
  }
  if (!("LogData" %in% dns)) {
    dots[["LogData"]] <- character(dots[["MaxLines"]])
  }
  if (!("UsedLines" %in% dns)) {
    dots[["UsedLines"]] <- 1L
  }

  # Copy from SOILWAT2 "testing" (defaults), but dot arguments take precedence
  tmp <- lapply(
    sns,
    function(sn) if (sn %in% dns) dots[[sn]] else slot(def, sn)
  )
  names(tmp) <- sns

  do.call("new", args = c("swLog", tmp))
}
DrylandEcology/rSOILWAT2 documentation built on Jan. 12, 2024, 9:06 p.m.