R/excel_build.R

#' Generate a formated Excel File from large dataframe
#'
#' This is an internal function used by process() to subset and output a large dataframe as a formated multisheet workbook named after the rainfall distirbution and depth i.e. 'traingular_0.4m.xlsx'.
#' This function heavily utilizes the r2excel functions developed by Alboukadel Kassambara (https://github.com/kassambara).
#' Sheet 1 of each output workbook is a README documnet detailing the date, time, and user who generated the workbook; the modeled event and hill slope; and the contents of the remaining 8 sheets.
#'
#' @param model a model object generated by
#' @export
#' @examples
#' excel_bulid(triangular_rainfall_mod)
#' @author
#' Mike Johnson

excel_build = function(model = NULL){

# Create an Excel workbook.

filename <- paste0(model$param$param$type,"_", model$param$param$depth,  "m.xlsx")

if(file.exists(filename)){file.remove(filename)}

wb <- createWorkbook(type="xlsx")
# Create a sheet in that workbook to contain the data table

read.me <- createSheet(wb, sheetName = "Read Me")
val     <- createSheet(wb, sheetName = "Parmeters")
rain    <- createSheet(wb, sheetName = "Rainfall")
depth   <- createSheet(wb, sheetName = "Runoff Depth (y)")
infil   <- createSheet(wb, sheetName = "Infiltration (F)")
runoff  <- createSheet(wb, sheetName = "Runoff (q)")
wetting <- createSheet(wb, sheetName = "Wetting Front (zf)")
toe     <- createSheet(wb, sheetName = "Depth at toe")

xlsx.addHeader(wb, read.me, value="Read Me",level=1, color="black", underline=1)
xlsx.addLineBreak(read.me, 1)

xlsx.addHeader(wb, val,     value="Model Parameters",level=1, color="black", underline=1)
xlsx.addLineBreak(val, 1)

xlsx.addHeader(wb, rain,    value="Rainfall Distribution",level=1, color="black", underline=1)
xlsx.addLineBreak(rain, 1)

xlsx.addHeader(wb, runoff,  value="Runoff",level=1, color="black", underline=1)
xlsx.addLineBreak(runoff, 1)

xlsx.addHeader(wb, depth,   value="Runoff Depth",level=1, color="black", underline=1)
xlsx.addLineBreak(depth, 1)

xlsx.addHeader(wb, toe,     value="Runoff at toe",level=1, color="black", underline=1)
xlsx.addLineBreak(runoff, 1)

xlsx.addHeader(wb, infil,   value="Infiltration",level=1, color="black", underline=1)
xlsx.addLineBreak(infil, 1)

xlsx.addHeader(wb, wetting, value="Wetting Front Depth",level=1, color="black", underline=1)
xlsx.addLineBreak(wetting, 1)


# Read Me Page ------------------------------------------------------------

author=paste("Workbook generated by:", Sys.getenv("LOGNAME"), "\n",
             "On: ", Sys.time())

summary = paste0("This workbook contains model output for a " , model$param$param$type, " distributed storm that released ",
                model$param$param$depth, " meters of precipitation over ", model$param$param$hours, " hours. The hill slope being studied is ",
                model$param$hill$X, " meters long, with a slope of ", model$param$hill$slope , " and a depth of ", model$param$hill$Z ," meters.\n",
     "\nThe code for this model is described in: \n
      Johnson & Loaiciga (2017): Coupled Infiltration and Kinematic-Wave Runoff Simulation in Slopes: Implications for Slope Stability.
      Loaiciga & Johnson (2017): 'Infiltration on slopeing terrian and its role on slope stability and erosion.'\n
      It is also avialable as an R package on Github at mikejohson51.github \n
      Questions relating to paper @ hugo@ucsb.edu
      Questions relating to code @ mike.johnson@geog.ucsb.edu")

contents = paste0("This workbook contains 8 sheets:
                  (1) Read.me
                  (2) Parameters
                  (3) Rainfall Distribution
                  (4) Runoff
                  (5) Runoff Depth
                  (6) Runoff at toe
                  (7) Infiltration
                  (8) Wetting Front" )

xlsx.addParagraph(wb, read.me, value=author, isItalic=TRUE, colSpan=15,
                  rowSpan=3, fontColor="darkgray", fontSize=12)
#xlsx.addLineBreak(read.me, 1)

xlsx.addParagraph(wb, read.me, value=summary, isItalic=TRUE, colSpan=15,
                  rowSpan=15, fontColor="darkgray", fontSize=12)
#xlsx.addLineBreak(read.me, 1)

xlsx.addParagraph(wb, read.me, value=contents, isItalic=TRUE, colSpan=15,
                  rowSpan=10, fontColor="darkgray", fontSize=12)
#xlsx.addLineBreak(read.me, 1)


# Parameters Page ---------------------------------------------------------

param1 = rbind(t(as.matrix(model$param$hill)),
      t(as.matrix(model$param$soil)),
      t(as.matrix(model$param$param )))

param1 <- cbind(Names = rownames(param1), param1)
colnames(param1) = c("Name", "Value")
rownames(param1) <- NULL

xlsx.addTable(wb, val, param1 , startCol=1, startRow = 4)

# Rainfall Distribution Sheet ---------------------------------------------

xlsx.addTable(wb, rain, model$param$rain , startCol=1, startRow = 4)

# Runoff Sheet ---------------------------------------------

xlsx.addTable(wb, runoff, model$q , startCol=1, startRow = 4)
data
# Runoff Depth Sheet ---------------------------------------------

xlsx.addTable(wb, depth, model$y , startCol=1, startRow = 4)

# Runoff Toe Sheet ---------------------------------------------

xlsx.addTable(wb, toe, model$toe, startCol=1, startRow = 4)

# Infiltration  Sheet ---------------------------------------------

xlsx.addTable(wb, infil, model$F , startCol=1, startRow = 4)

# Wetting Front Sheet ---------------------------------------------

xlsx.addTable(wb, wetting, model$zf , startCol=1, startRow = 4)

# save the workbook to an Excel file
saveWorkbook(wb, filename)
}
mikejohnson51/Rainfall_Infiltration_hillslope documentation built on May 29, 2019, 2:35 p.m.