Skip to content

Commit

Permalink
Merge pull request #68 from tamnva/joss_manuscript
Browse files Browse the repository at this point in the history
add vignettes run with user defined objective function
  • Loading branch information
tamnva authored Feb 10, 2024
2 parents a673063 + 76a1f84 commit 1ac8270
Show file tree
Hide file tree
Showing 30 changed files with 845 additions and 751 deletions.
3 changes: 0 additions & 3 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ export(updateCalibrationFile)
export(updateMultiFile)
export(updateSingleFile)
export(updatedFileContent)
export(userObjFunction)
export(userReadSwatOutput)
export(yearlyOutputLoc)
importFrom(doParallel,registerDoParallel)
importFrom(foreach,"%dopar%")
Expand All @@ -43,7 +41,6 @@ importFrom(lhs,randomLHS)
importFrom(magrittr,"%>%")
importFrom(parallel,makeCluster)
importFrom(parallel,stopCluster)
importFrom(stats,aggregate)
importFrom(stats,cor)
importFrom(stats,quantile)
importFrom(stats,rnorm)
Expand Down
23 changes: 11 additions & 12 deletions R/behaSimulation.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@
#' RMSE: "Minimize"
#'
#' @param samplingApproach the parameter sampling approach, possible values are:
#' 'Sensi_Cali_(uniform_Latin_Hypercube_Sampling)',
#' 'Cali_(from_optimization_package)',
#' 'Cali_(from_nloptr_package)'),
#' 'Cali_(Dynamically_Dimensioned_Search)',
#' 'Cali_(Generalized_Likelihood_Uncertainty_Estimation)',
#' 'Read_User_Parameter_File',
#' 'Sensi_(from_userDefined_package)',
#' 'Sensi_Cali_(uniform_Latin_Hypercube_Sampling)' \cr
#' 'Cali_(from_optimization_package)'\cr
#' 'Cali_(from_nloptr_package)')\cr
#' 'Cali_(Dynamically_Dimensioned_Search)'\cr
#' 'Cali_(Generalized_Likelihood_Uncertainty_Estimation)'\cr
#' 'Read_User_Parameter_File'\cr
#' 'Sensi_(from_userDefined_package)'\cr
#' 'Cali_(from_userDefined_package)'
#'
#' @return a list of dataframes showing the 95PPU, median,
Expand All @@ -50,7 +50,6 @@
#'
#' \donttest{
#' # Please see RSWAT Vignettes
#' vignette("SUFI2_without_GUI",package="RSWAT")
#' }
#'
#'
Expand All @@ -64,9 +63,9 @@ behaSimulation <- function(objValue, simData, parameterValue, behThreshold,
samplingApproach){

# find index of simulations which are behavioral simulations
if ((statIndex == "NSE") | (statIndex == "KGE") | (statIndex == "R2")){
if (statIndex %in% c("NSE", "KGE", "R2")){
behaIndex <- which(objValue >= behThreshold)
} else if(statIndex == "aBIAS" | statIndex == "RMSE") {
} else if(statIndex %in% c("aBIAS", "RMSE")) {
behaIndex <- which(objValue <= abs(behThreshold))
} else {
if (minOrmax == "Maximize"){
Expand Down Expand Up @@ -114,10 +113,10 @@ behaSimulation <- function(objValue, simData, parameterValue, behThreshold,
}

# find the best simulation
if ((statIndex == "NSE") | (statIndex == "KGE") | (statIndex == "R2")){
if (statIndex %in% c("NSE", "KGE", "R2")){
ppuSimData[,4] <- simData[[varNumber]][[which(objValue == max(objValue))[1]]]

} else if(statIndex == "aBIAS" | statIndex == "RMSE") {
} else if(statIndex %in% c("aBIAS","RMSE")) {
ppuSimData[,4] <- simData[[varNumber]][[which (objValue == min(objValue))[1]]]

} else {
Expand Down
9 changes: 4 additions & 5 deletions R/calObjFunction.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#' calibration and validation period and for each variable
#'
#' @inheritParams runSWATpar
#' @param nOutputVar number of output variables
#' @param nOutputVar integer - number of output variables
#' @param userReadSwatOutput TODO
#' @param observedData list of observed data, each observed data is a data frame
#' @param index performance criteria (NSE, KGE, RMSE, R2, or aBIAS)
Expand All @@ -14,7 +14,6 @@
#'
#' \donttest{
#' # Please see RSWAT Vignettes
#' vignette("SUFI2_without_GUI",package="RSWAT")
#' }
#'
#' @export
Expand All @@ -35,7 +34,7 @@ calObjFunction <- function(parameterValue, ncores,
simData <- list()

counter <- rep(0, nOutputVar)
output$objValueCali <- rep(0, nrow(parameterValue))
output$objValueCali <- rep(0, nrow(parameterValue))
output$objValueValid <- rep(0, nrow(parameterValue))
output$error <- FALSE

Expand All @@ -55,8 +54,8 @@ calObjFunction <- function(parameterValue, ncores,
}

#Loop over number of simulation
fileNameSimData <- paste(workingDirectory, "/Output/Core_",
i, "/out_var_", j, ".txt", sep = "")
fileNameSimData <- file.path(workingDirectory, "Output", paste0("Core_", i),
paste0("out_var_", j, ".txt"))
tempSimData <- read.table(fileNameSimData, header = FALSE, sep = "")

#Check if length of observed and simulated data are the same or not
Expand Down
12 changes: 6 additions & 6 deletions R/extractExampleData.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

extracExampleData <- function(exampleData, dataName, toDir){

if(dataName =="swatTxtInOut" | dataName =="all"){
if(dataName %in% c("swatTxtInOut", "all")){
dir.create(file.path(toDir, "swatTxtInOut"))
for (i in 1:length(exampleData$swatTxtInOut)){
writeLines(exampleData$swatTxtInOut[[i]],
Expand All @@ -45,7 +45,7 @@ extracExampleData <- function(exampleData, dataName, toDir){
}


if(dataName =="swatPlusTxtInOut" | dataName =="all"){
if(dataName %in% c("swatPlusTxtInOut", "all")){
dir.create(file.path(toDir, "swatPlusTxtInOut"))
for (i in 1:length(exampleData$swatPlusTxtInOut)){
writeLines(exampleData$swatPlusTxtInOut[[i]],
Expand All @@ -60,13 +60,13 @@ extracExampleData <- function(exampleData, dataName, toDir){

}

if(dataName =="swatParam" | dataName =="all"){
if(dataName %in% c("swatParam", "all")){
writeLines(exampleData$swatParam, file.path(toDir, "swatParam.txt"))
message("Created swatParam.txt file in ", file.path(toDir, "swatParam.txt"))

}

if(dataName =="swatObservedData" | dataName =="all"){
if(dataName %in% c("swatObservedData", "all")){
dir.create(file.path(toDir, "observedSWAT"))
writeLines(exampleData$swatObservedData, file.path(toDir,
"observedSWAT",
Expand All @@ -77,7 +77,7 @@ extracExampleData <- function(exampleData, dataName, toDir){

}

if(dataName =="swatPlusObservedData" | dataName =="all"){
if(dataName %in% c("swatPlusObservedData", "all")){
dir.create(file.path(toDir, "observedSWATPlus"))
writeLines(exampleData$swatPlusObservedData, file.path(toDir,
"observedSWATPlus",
Expand All @@ -88,7 +88,7 @@ extracExampleData <- function(exampleData, dataName, toDir){

}

if(dataName =="swatPlusParam" | dataName =="all"){
if(dataName %in% c("swatPlusParam", "all")){
writeLines(exampleData$swatPlusParam, file.path(toDir, "cal_parms.cal"))
message("Created cal_parms.cal file in ", file.path(toDir, "cal_parms.cal"))
}
Expand Down
27 changes: 11 additions & 16 deletions R/getNumberOutputVar.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,18 @@
#' @export
#'
getNumberOutputVar <- function(outputExtraction){

nOutputVar <- 0
userReadSwatOutput <- c()

# output.rch file tpye
outputRchType <- c("output.rch", "channel_sd_day.txt", 'channel_sd_mon.txt',
'channel_sd_yr.txt', 'channel_sdmorph_day.txt',
'channel_sdmorph_mon.txt','channel_sdmorph_yr.txt',
'lsunit_wb_day.txt','lsunit_wb_mon.txt','lsunit_wb_yr.txt',
'basin_wb_day.txt','basin_wb_mon.txt','basin_wb_yr.txt',
"output.hru","output.sub","output.rsv")

for (i in 1:nrow(outputExtraction)) {
if (outputExtraction[i,1] == "watout.dat"){
temp <- length(strsplit(as.character(outputExtraction[i,3]), ",")[[1]])
Expand All @@ -34,22 +44,7 @@ getNumberOutputVar <- function(outputExtraction){
}
}

} else if(outputExtraction[i,1] == "output.rch" |
outputExtraction[i,1] == "channel_sd_day.txt" |
outputExtraction[i,1] == 'channel_sd_mon.txt' |
outputExtraction[i,1] == 'channel_sd_yr.txt' |
outputExtraction[i,1] == 'channel_sdmorph_day.txt' |
outputExtraction[i,1] == 'channel_sdmorph_mon.txt' |
outputExtraction[i,1] == 'channel_sdmorph_yr.txt' |
outputExtraction[i,1] == 'lsunit_wb_day.txt' |
outputExtraction[i,1] == 'lsunit_wb_mon.txt' |
outputExtraction[i,1] == 'lsunit_wb_yr.txt' |
outputExtraction[i,1] == 'basin_wb_day.txt' |
outputExtraction[i,1] == 'basin_wb_mon.txt' |
outputExtraction[i,1] == 'basin_wb_yr.txt' |
outputExtraction[i,1] == "output.hru" |
outputExtraction[i,1] == "output.sub" |
outputExtraction[i,1] == "output.rsv"){
} else if(outputExtraction[i,1] %in% outputRchType){
if ((nchar(outputExtraction[i,3]) > 0) & (nchar(outputExtraction[i,4]) > 0)){
temp <- sum(lengths(getRchNumber(outputExtraction[i,4])))
nOutputVar <- nOutputVar + temp
Expand Down
6 changes: 3 additions & 3 deletions R/getSimTime.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ getSimTime <- function(TxtInOut){
if(substr(TxtInOut,(nchar(TxtInOut)-3), nchar(TxtInOut)) == ".cio"){
fileCio <- readLines(TxtInOut, 60)
} else {
fileCio <- readLines(paste(TxtInOut, "/file.cio", sep = ""), 60)
fileCio <- readLines(file.path(TxtInOut, "file.cio"), 60)
}

# Check whether this is TxtInOut of SWAT or SWAT+
Expand Down Expand Up @@ -73,7 +73,7 @@ getSimTime <- function(TxtInOut){
} else {

# Read simulation time information from the time.sim file
simTime <- read.table(paste(TxtInOut, "/", "time.sim", sep = ""), skip = 2, sep = "")
simTime <- read.table(file.path(TxtInOut, "time.sim"), skip = 2, sep = "")

# Find the starting date of simulation
startSim <- as.Date(paste(toString(as.numeric(simTime[2])),"0101",
Expand All @@ -93,7 +93,7 @@ getSimTime <- function(TxtInOut){
timeStepCode <- as.numeric(simTime[5])

# Find start of eval or printing
printTime <- readLines(paste(TxtInOut, "/print.prt", sep = ""), 3)
printTime <- readLines(file.path(TxtInOut, "print.prt"), 3)
printTime <- as.numeric(strsplit(trimws(printTime[3]), " +")[[1]])

startPrint <- max(printTime[1] + as.numeric(simTime[2]) , printTime[3])
Expand Down
3 changes: 1 addition & 2 deletions R/readChannelFile.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ readChannelFile <- function(workingDirectory,
"lsunit_wb_mon.txt",
"lsunit_wb_yr.txt")

filePath <- paste(workingDirectory, "/TxtInOut_", coreNumber, "/",
fileName, sep = "")
filePath <- file.path(workingDirectory, paste0("TxtInOut_", coreNumber), fileName)

# Get file content/data
channelData <- read.table(filePath, header = FALSE, sep = "", skip = 3)
Expand Down
2 changes: 1 addition & 1 deletion R/readFileContent.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

readFileContent <- function(fileDirectory, files){

files <- paste(fileDirectory, "/", files, sep = "")
files <- file.path(fileDirectory, files)
fileContent <- list()

for (i in 1:length(files)){
Expand Down
5 changes: 2 additions & 3 deletions R/readSaveOutput.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ readOutputRchFile <- function(workingDirectory,
output){


fileName <- paste(workingDirectory, "/TxtInOut_", coreNumber, "/",
fileName, sep = "")
fileName <- file.path(workingDirectory, paste0("TxtInOut_", coreNumber), fileName)

getOutputRsvData <- read.table(fileName, header = FALSE, sep = "", skip = 9)

Expand Down Expand Up @@ -111,4 +110,4 @@ readOutputRchFile <- function(workingDirectory,
}

return(output)
}
}
3 changes: 1 addition & 2 deletions R/readWatoutFile.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ readWatoutFile <- function(workingDirectory,
fileCioInfo,
output){

fileName <- paste(workingDirectory, "/TxtInOut_",
coreNumber, "/", fileName, sep = "")
fileName <- file.path(workingDirectory, paste0("TxtInOut_", coreNumber), fileName)

getWatoutData <- read.table(fileName, header = FALSE, sep = "", skip = 6)

Expand Down
13 changes: 2 additions & 11 deletions R/runSWATpar.R
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,7 @@ runSWATSequential <- function(coreNumber,
firstRun){

# Set working directory
setwd(paste(workingDirectory,
'/',
'TxtInOut',
'_',
coreNumber,
sep = ""))

setwd(file.path(workingDirectory, paste0('TxtInOut_', coreNumber)))
# Get directory where new TxtInOut files are saved
toDir <- getwd()

Expand Down Expand Up @@ -222,10 +216,7 @@ runSWATSequential <- function(coreNumber,
Sys.time(),
sep =''
),
file= paste(workingDirectory,
'/Output/CurrentSimulationReport.log',
sep =''
),
file= file.path(workingDirectory, 'Output', 'CurrentSimulationReport.log'),
append=TRUE
)
}
Expand Down
33 changes: 14 additions & 19 deletions R/saveOutput.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ saveOutput <- function(workingDirectory,
# Set output as list object
output <- list()

# SWAT+ output files
swatPlusFiles <- c("channel_sd_day.txt", "channel_sd_mon.txt",
"channel_sd_yr.txt", "channel_sdmorph_day.txt",
"channel_sdmorph_mon.txt", "channel_sdmorph_yr.txt",
"lsunit_wb_day.txt", "lsunit_wb_mon.txt",
"lsunit_wb_yr.txt","basin_wb_day.txt",
"basin_wb_mon.txt", "basin_wb_yr.txt")

for (i in 1:length(fileType)){

if (fileType[i] == "watout.dat"){
Expand All @@ -56,9 +64,7 @@ saveOutput <- function(workingDirectory,
fileCioInfo,
output)

} else if (fileType[i] == "output.rch" |
fileType[i] == "output.sub" |
fileType[i] == "output.hru" ){
} else if (fileType[i] %in% c("output.rch", "output.sub", "output.hru")){
output <- readOutputRchFile(workingDirectory,
coreNumber,
fileName[i],
Expand All @@ -68,18 +74,8 @@ saveOutput <- function(workingDirectory,
getRchNumber(rchNumber[i]),
output)

} else if (fileType[i] == "channel_sd_day.txt" |
fileType[i] == "channel_sd_mon.txt" |
fileType[i] == "channel_sd_yr.txt" |
fileType[i] == "channel_sdmorph_day.txt" |
fileType[i] == "channel_sdmorph_mon.txt" |
fileType[i] == "channel_sdmorph_yr.txt" |
fileType[i] == "lsunit_wb_day.txt" |
fileType[i] == "lsunit_wb_mon.txt" |
fileType[i] == "lsunit_wb_yr.txt" |
fileType[i] == "basin_wb_day.txt" |
fileType[i] == "basin_wb_mon.txt" |
fileType[i] == "basin_wb_yr.txt"){
} else if (fileType[i] %in% swatPlusFiles){

output <- readChannelFile(workingDirectory,
coreNumber,
fileName[i],
Expand All @@ -90,7 +86,7 @@ saveOutput <- function(workingDirectory,
output)

} else if (fileType[i] == "userReadSwatOutput"){
workingDir <- paste(workingDirectory, "/TxtInOut_", coreNumber, sep = "")
workingDir <- file.path(workingDirectory, paste0("TxtInOut_", coreNumber))
setwd(workingDir)
userExtractData <- userReadSwatOutput()
output <- c(output, userExtractData)
Expand All @@ -101,14 +97,13 @@ saveOutput <- function(workingDirectory,
}

# Save output
outputDirectory <- paste(workingDirectory, "/Output/Core_", coreNumber, sep = "")
outputDirectory <- file.path(workingDirectory, "Output", paste0("Core_", coreNumber))

# Create directory if it does not exist
if(!dir.exists(outputDirectory)) dir.create(outputDirectory)

for (i in 1:length(output)){
OutputFileName <- paste(outputDirectory, '/out_var_', i,
'.txt', sep ='')
OutputFileName <- file.path(outputDirectory, paste0('out_var_', i, '.txt'))

if (firstRun){file.create(OutputFileName)}

Expand Down
2 changes: 1 addition & 1 deletion R/updateTxtInOut.R
Original file line number Diff line number Diff line change
Expand Up @@ -175,5 +175,5 @@ updateCalibrationFile <- function(paraSelection, parameterValue, currentDirector

}

writeLines(fileContent, paste(currentDirectory, "/", "calibration.cal", sep = ""))
writeLines(fileContent, file.path(currentDirectory, "calibration.cal"))
}
Loading

0 comments on commit 1ac8270

Please sign in to comment.