From 3e4750ec80e2de860d824aea9a0ef1a911a92901 Mon Sep 17 00:00:00 2001 From: nguyenta Date: Wed, 14 Feb 2024 09:58:55 +0100 Subject: [PATCH] fix: bugs in readAllOutVar function --- DESCRIPTION | 2 +- R/readAllOutVar.R | 41 +- man/readAllOutVar.Rd | 5 +- vignettes/SUFI2_without_GUI.R | 10 +- vignettes/SUFI2_without_GUI.html | 764 ++++++++++++-------- vignettes/run_SWATplus_user_parameters.R | 12 +- vignettes/run_SWATplus_user_parameters.Rmd | 6 +- vignettes/run_SWATplus_user_parameters.html | 557 +++++++++----- vignettes/userReadSwatOutput.R | 6 +- vignettes/userReadSwatOutput.Rmd | 6 +- vignettes/userReadSwatOutput.html | 503 +++++++++---- vignettes/user_objective_function.R | 6 +- vignettes/user_objective_function.html | 523 ++++++++++---- 13 files changed, 1633 insertions(+), 808 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 027209a..1975830 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -19,7 +19,7 @@ Imports: stats (>= 4.1.2), utils (>= 4.1.2), foreach (>= 1.2.0), - htmltools (>= 0.5.7), + htmltools (>= 0.5.7), parallel (>= 4.1.2), lhs (>= 1.1.4), plotly (>= 4.10.0), diff --git a/R/readAllOutVar.R b/R/readAllOutVar.R index dfc501c..59cec4e 100644 --- a/R/readAllOutVar.R +++ b/R/readAllOutVar.R @@ -17,7 +17,8 @@ #' #' @return a list of data frame for each output variable. Each data frame has n #' rows (number of rows are number of time steps) and m columns (m is the number -#' of iterations) +#' of iterations: column 1 = result from iteration 1 and column i = result from +#' iteration i) #' #' @examples #' @@ -29,27 +30,47 @@ #' # if you have two output variables with different number of time steps #' # numTimesteps <- c(3000, 4000) #' -#' readAllOutVar("C:/data/workingFolder") +#' output <- readAllOutVar("C:/data/workingFolder", numTimesteps) #'} #' #' @export #' #' + readAllOutVar <- function(workingFolder, numTimesteps){ - # List of folders with Core_ where output_var_xx.txt files are stored - coreFolders <- dir(file.path(workingFolder, "Output"), pattern = "Core_", - full.names = TRUE) + # Find number of cores + numberOfCores <- length(dir(file.path(workingFolder, "Output"), + pattern = "Core_")) + + # List of folder names starts with "Core_1" in the Workingfolder/Output + coreFolders <- paste0("Core_", c(1:numberOfCores)) - # List of files in each coreFolders (all coreFolders have the same file list) - outVarFiles <- list.files(coreFolders[1], full.names = TRUE) + # Number of output variables = number of files in Workingfolder/Output/Core_1 + numOuputVariables <- length(list.files(file.path(workingFolder, + "Output", + coreFolders[1]) + ) + ) + # List of output files starts in Workingfolder/Output/Core_1 + outVarFiles <- paste0("out_var_", + c(1:numOuputVariables), + ".txt") + # Now read all output files (out_var_xx.txt) in all Core_xx folders output <- list() for (ifolder in 1:length(coreFolders)){ + # Loop over each outVarfiles for (var in 1:length(outVarFiles)){ + # Read content of each file and store - data <- read.table(outVarFiles[var], header = FALSE, sep="") + outFile <- file.path(workingFolder, + "Output", + coreFolders[ifolder], + outVarFiles[var]) + + data <- read.table(outFile, header = FALSE, sep="") # convert to matrix data <- matrix(data[,1], nrow = numTimesteps[var] + 1) @@ -57,11 +78,11 @@ readAllOutVar <- function(workingFolder, numTimesteps){ # Remove the first rows data <- data[-c(1),] - if ((var == 1) & (ifolder == 1)){ + if (ifolder == 1){ output[[var]] <- list() output[[var]] <- data } else { - output[[var]] <- cbind(output[[var]],data) + output[[var]] <- cbind(output[[var]], data) } } } diff --git a/man/readAllOutVar.Rd b/man/readAllOutVar.Rd index 76e5d45..7f9cd83 100644 --- a/man/readAllOutVar.Rd +++ b/man/readAllOutVar.Rd @@ -17,7 +17,8 @@ numTimesteps <- c(3000,4000)} \value{ a list of data frame for each output variable. Each data frame has n rows (number of rows are number of time steps) and m columns (m is the number -of iterations) +of iterations: column 1 = result from iteration 1 and column i = result from +iteration i) } \description{ When running RSWAT, output files will be saved in the working folder/Output/Core @@ -35,7 +36,7 @@ numTimesteps <- c(3000) # if you have two output variables with different number of time steps # numTimesteps <- c(3000, 4000) -readAllOutVar("C:/data/workingFolder") +output <- readAllOutVar("C:/data/workingFolder", numTimesteps) } } diff --git a/vignettes/SUFI2_without_GUI.R b/vignettes/SUFI2_without_GUI.R index 69fc863..bfc0f00 100644 --- a/vignettes/SUFI2_without_GUI.R +++ b/vignettes/SUFI2_without_GUI.R @@ -1,4 +1,4 @@ -## ----include = FALSE---------------------------------------------------------- +## ---- include = FALSE--------------------------------------------------------- knitr::opts_chunk$set( collapse = TRUE, comment = "#>" @@ -8,7 +8,7 @@ knitr::opts_chunk$set( # Load require packages library(RSWAT) -## ----message=FALSE, eval = FALSE---------------------------------------------- +## ---- message=FALSE, eval = FALSE--------------------------------------------- # # Extract all of examples data to the temporal working directory # extracExampleData(exampleData, "all", tempdir()) # @@ -16,7 +16,7 @@ library(RSWAT) # tempdir() # -## ----message=FALSE, eval = FALSE---------------------------------------------- +## ---- message=FALSE, eval = FALSE--------------------------------------------- # # # # 1. Set SWATproject is TRUE if you run SWAT, set to FALSE if you run SWAT+ # SWATproject <- TRUE @@ -117,7 +117,7 @@ library(RSWAT) # # observedDataFile <- c("D:/example/obs_var_1.txt", "D:/example/obs_var_2.txt") # -## ----message=FALSE, eval = FALSE---------------------------------------------- +## ---- message=FALSE, eval = FALSE--------------------------------------------- # # # Generate parameter samples using Latin Hypercube Sampling # parameterValue <- lhsRange(sensCaliCommand, getParamRange(paraSelection)) @@ -150,7 +150,7 @@ library(RSWAT) # dateRangeCali,firstRun) # -## ----message=FALSE, eval = FALSE---------------------------------------------- +## ---- message=FALSE, eval = FALSE--------------------------------------------- # # Number of output variables (from the output extraction data frame) # OutputVar <- getNumberOutputVar(outputExtraction) # nOutputVar <- OutputVar$nOutputVar diff --git a/vignettes/SUFI2_without_GUI.html b/vignettes/SUFI2_without_GUI.html index cef1105..1f6fcbb 100644 --- a/vignettes/SUFI2_without_GUI.html +++ b/vignettes/SUFI2_without_GUI.html @@ -14,26 +14,38 @@ Run SWAT/SWAT+ without GUI using SUFI-2 approach - + +code{white-space: pre-wrap;} +span.smallcaps{font-variant: small-caps;} +span.underline{text-decoration: underline;} +div.column{display: inline-block; vertical-align: top; width: 50%;} +div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;} +ul.task-list{list-style: none;} + + +code{white-space: pre-wrap;} +span.smallcaps{font-variant: small-caps;} +span.underline{text-decoration: underline;} +div.column{display: inline-block; vertical-align: top; width: 50%;} +div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;} +ul.task-list{list-style: none;} + + +code{white-space: pre-wrap;} +span.smallcaps{font-variant: small-caps;} +span.underline{text-decoration: underline;} +div.column{display: inline-block; vertical-align: top; width: 50%;} +div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;} +ul.task-list{list-style: none;} + + +code{white-space: pre-wrap;} +span.smallcaps{font-variant: small-caps;} +span.underline{text-decoration: underline;} +div.column{display: inline-block; vertical-align: top; width: 50%;} +div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;} +ul.task-list{list-style: none;} +