R/ERAdeaccum.R

Defines functions ERAdeaccum

Documented in ERAdeaccum

#' Deaccumulates ERA cumulative time series
#' @description This function is used to deaccumulate variables stored as 12-hour cumulative values by ERA to 3-hour values. It is called by other functions, but may also be useful as a stand-alone functions. Note that this function only works for a single location, i.e. \emph{NOT} areal values.
#' @param ERAobs Required. A \pkg{CRHMr} obs dataframe of ERA data, created by \code{ERAgetnearestTimeSeries}.
#' @param colnum Optional. The column number containing the values to be deaccumulated, not including the datetime. Default is column 1.
#' @param quiet Optional. Suppresses display of messages, except for errors. If you are calling this function in an R script, you will usually leave \code{quiet=TRUE} (i.e. the default). If you are working interactively, you will probably want to set \code{quiet=FALSE}.
#'@param logfile Optional. Name of the file to be used for logging the action. Normally not used
#'
#' @return If successful, returns an obs dataframe containing the deaccumulated value. If unsuccessful, returns the value \code{FALSE}.
#' @author Kevin Shook
#' @export
#'
#' @examples \dontrun{
#' deaccum <- ERAdeaccum(ERAobs)}
#'
ERAdeaccum <- function(ERAobs, colnum=1, quiet=TRUE, logfile=''){
  # check parameters

  obsName <- deparse(substitute(ERAobs))
  if (obsName == ''){
    cat('Error: must specify ERA dataframe\n')
    return(FALSE)
  }
  if (nrow(ERAobs) == 0){
    cat('Error: missing values\n')
    return(FALSE)
  }

  # do deaccumulation
  colnum <- colnum + 1
  deaccum <-  c(ERAobs[1,colnum], diff(ERAobs[,colnum]))


  resets <- seq(from=1, by=4, length.out=length(deaccum)/4)
  deaccum[resets] <- ERAobs[resets, colnum]
  ERAobs[,colnum] <- deaccum


  # output info to screen (if req'd) and write to log file
  file.info <- CRHMr::CRHM_summary(ERAobs)
  if (!quiet)
    print(file.info)

  comment <- paste('ERAdeaccum ERAobs:', obsName, sep='')
  result <- CRHMr::logAction(comment, logfile)

  if(result)
    return(ERAobs)
  else
    return(result)

}
CentreForHydrology/Reanalysis documentation built on Nov. 23, 2022, 2:57 a.m.