Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: bugs in readAllOutVar function #70

Merged
merged 1 commit into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
41 changes: 31 additions & 10 deletions R/readAllOutVar.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
#'
Expand All @@ -29,39 +30,59 @@
#' # 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)

# 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)
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions man/readAllOutVar.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions vignettes/SUFI2_without_GUI.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## ----include = FALSE----------------------------------------------------------
## ---- include = FALSE---------------------------------------------------------
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
Expand All @@ -8,15 +8,15 @@ 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())
#
# # This is the path the temporal folder
# 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
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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
Expand Down
Loading
Loading