Solution

40 views
Skip to first unread message

Ana Carolina Bonfim

unread,
Apr 14, 2024, 8:15:25 AMApr 14
to R-SWAT
Hi, here is the solution to the problem which I was facing.
I was trying to extract  month data from the output.wql file. In this file, the data are daily, even if I select in SWAT to run monthly.
The script attached was made by Dr. Tam to extract the variable in column 16 for the reach 22.
userReadSwatOutput (2).R

osama Hassan

unread,
May 8, 2024, 4:28:04 PMMay 8
to R-SWAT
Dear Ana Carolina Bonfim

Thank you very much for the provided solution. However, I still got NA monthly values when i ran the calibration monthly using 'userReadSwatOutput'.
I'm trying to extract the monthly outflow from subbasin 11 (output.rch), and the simulated discharge is located in column 7. 
Please take a look at my file below! I would appreciate your help!
Thank you!

#########################################################################################################################

#' User-defined function for reading SWAT/SWAT+
#'
#' @description
#' This is a user-defined function for reading SWAT/SWAt+ outputs, do not use
#' as it is now, please modify the source code of this function if you use
#' according to your need and then install RSWAT app again from your local
#' folder to update this function.DO NOT CHANGE the name of this function
#' (mus be updateUserReadSwatOutput). ONLY CHANGE the code at the place where indicates
#' "START TO CHANGE FROM HERE" and STOP at "END OF CHANGE". This functions
#' must NOT receive any input. By default, you are NOW INSIDE the TxtInOut folder
#' and you can read any output files there by just using the file name.
#' RSWAT will use this function to get SWAT outputs (from the respective core)
#' after each model run and save it in combination with other outputs.
#'
#' Please TEST your function before running R-SWAT
#'
#' @return a list of vector (of simulated data)
#' @importFrom stats aggregate
#'
#'
#' @export
#'

updateUserReadSwatOutput <- function(){

  output <- list()
  # -------------------------------------------------------------------------#
  #       Delete the existing code below and add your code below this line   #
  #                 START TO CHANGE FROM HERE                                #
  # -------------------------------------------------------------------------#
  # Example: This function extracts data in the 7th column of the file  output.rch
  #          and aggregate to monthly

  # --------------------------------------------------------------------------
  date <- seq(from = as.Date("1998-01-01", "%Y-%m-%d"),
              to = as.Date("2012-12-31", "%Y-%m-%d"),
              by = 1)
  year <- as.numeric(format(date, "%Y"))
  month <- as.numeric(format(date, "%m"))

  # Reach number to extract
  reach <- 11
  column <- 7

  # --------------------------------------------------------------------------

  # Read all data from output.rch
  output_rch<- read.table("output.rch", header = FALSE, sep = "", skip = 9)

  # Subset only selected colum and reach
  output_rch <- output_rch[output_rch[,2]  == reach ,column]

  # Aggregate to monthly
  dataFrame <- data.frame(month = month,
                          year = year,
                          data = output_rch)

  # Aggreate by sum or mean
  dataFrameMonthly <- aggregate(.~ month + year, dataFrame, FUN = sum)

  # Only get the values of aggregated data
  output[[1]] <- dataFrameMonthly$data


  # ----------------------------------------------------------------------------
  # END OF CHANGE: Don't modify anything after this line
  # ----------------------------------------------------------------------------
  print(output)
  return(output)

}

# Overwrite the userReadSwatOutput with our updateUserReadSwatOutput
environment(updateUserReadSwatOutput) <- asNamespace('RSWAT')
assignInNamespace("userReadSwatOutput", updateUserReadSwatOutput, ns = "RSWAT")
Reply all
Reply to author
Forward
0 new messages