R/process_storage_SRE.R

Defines functions extract_reservoir

Documented in extract_reservoir

#' extract_reservoir
#'
#' @param storage_path path to WinWRAP SRE file
#' @details extracts values from SRE file into a usable time series
#' @return A time series of values.
#' @import readr
#' @import dplyr
#' @import sf
#' @import rgdal
#' @import purrr
#' @import sqldf
#' @import janitor
#' @import tidyr
#' @import ggplot2
#' @import av
#' @import gganimate
#' @export

extract_reservoir <- function(storage_path = "C:/Users/nels863/OneDrive - PNNL/Documents/wrapwrangler/inst/extdata/C3_ResV3.TOU"){

  # Scan the output text file that is produced from WinWRAP.
  # The code below splits each control point dataset into its own list.
  message("Loading in WinWRAP output.")
  a <- suppressMessages(scan(storage_path,
                             what="", sep="\n",
                             skip = 2,
                             blank.lines.skip = TRUE,
                             skipNul = TRUE))
  b <- strsplit(a, "[[:space:]]+")
  c <- b[lengths(b)>5]
  c2 <- c[lengths(c)!=8]
  d <- split(c2, ceiling(seq_along(c2)/927))

  # Create the final list where all of the new dataframes will be placed.
  control_df_list <- list()

  # Map through each of the control point datasets and create a new dataframe.
  map_cycles <- 1:length(d)
  message("Cycling through data chunks.")
  for(i in map_cycles){

    # Load in the cycle and find what control point we are looking at.
    d[[i]] -> set
    set[[1]] -> title_row
    title_row[[6]] -> reservoir

    # Create a table from the rest of the rows and make the first row the names of columns.
    data.frame(matrix(unlist(set[c(4:927)]),
                      nrow = 924,
                      byrow=T),stringsAsFactors=FALSE) %>%
      dplyr::select(1:4)-> final_table
    names(final_table)<-c("Year","Month", "EOP_Storage_AcFt", "Storage_AcFt")

    final_table$Reservoir_ID <- reservoir

    i -> number
    # Add this table to the list of control dataframes.
    final_table -> control_df_list[[number]]
  }

  # Join all of these tables together into a single control point dataframe.
  bind_rows(control_df_list) -> final_RES_table

  return(final_RES_table)
}
IMMM-SFA/wrapwrangler documentation built on Jan. 23, 2021, 12:42 a.m.