Skip to content

Commit

Permalink
feat: allow extracting results to csv files and behavioral parametersets
Browse files Browse the repository at this point in the history
  • Loading branch information
tamnva committed Sep 1, 2024
1 parent fde30bc commit 10f03e0
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 19 deletions.
10 changes: 10 additions & 0 deletions inst/R-SWAT/HTML/helpCheckSaveSimTocsv.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
</head>
<body>

<p>The time series of simulated variables will be saved to ".csv" files in the working folder (e.g., sim_variable_1.csv, sim_variable_2.csv, ...). The variable number => see "1. Define model outputs for extraction" => "Display corresponding observed file names" => "Variable number". The column names: sim_1,sim_2,sim_3,... correspond to simulated results with parameter set 1,2,3,...The number of rows is the number of timesteps => See "2. Select date range"(</p>

</body>
</html>
12 changes: 5 additions & 7 deletions inst/R-SWAT/HTML/introduction.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,16 @@
<p><span style="font-size: 16px; font-family: 'Arial Black',sans-serif;">About this app<br /></span></p>
</div>
<div>
<div>
<ul>
<li style="font-size: 16px; font-family: 'Times New Roman'; margin: 0in 0in 8.0pt 0in;">This is an R package (RSWAT) with a graphical user interface for parallel parameter sensitivity, calibration, and uncertainty analysis with <a href="https://swat.tamu.edu/software/swat-executables/">SWAT</a>, <a href="https://swat.tamu.edu/software/plus/">SWAT+</a>, <a href="https://sites.google.com/view/swat-carbon">SWAT-Carbon</a> and other modified SWAT</li>
<li style="font-size: 16px; font-family: 'Times New Roman'; margin: 0in 0in 8.0pt 0in;">The latest version of this app is always in the <a href="https://github.com/tamnva/R-SWAT/tree/development">development</a> branch. If you would like to contribute to the code, have any suggestions, want to report errors, and have scientific collaboration, please <a href="https://www.ufz.de/index.php?en=46415" target="_blank"><span style="color: blue;">contact me</span></a>. The best way to contact me is via the <a href="https://groups.google.com/g/R-SWAT" target="_blank"><span style="color: blue;">Google group</span></a> specially designed for this app. Tutorial videos can be found in <a href="https://www.youtube.com/channel/UCRK1rKFiNgYbG7qKWxAPtEQ" target="_blank"><span style="color: blue;">RSWAT YouTube channel.</span></a></li>
<li style="font-size: 16px; font-family: 'Times New Roman'; margin: 0in 0in 8.0pt 0in;">The app will be continued to be developed to serve the SWAT community. If you would like to contribute to the development of this app, please let me know. Together we can make this app much better as my time &amp; R knowledge for this app are very limited.</li>
</ul>
</div>
</div>
<div>
<p><span style="font-size: 16px; font-family: 'Arial Black',sans-serif;">Usage<br /></span></p>
</div>
<div>
<div>
<ul>
<li style="font-size: 16px; font-family: 'Times New Roman'; margin: 0in 0in 8.0pt 0in;">Always follow the numbering scheme in this app, e.g., start with Tab 1. General setting &rarr; 2. Parameter sampling &rarr; 3. Run SWAT &rarr; 4. Evaluate outputs. On each Tab, different input fields are required. These fields are also numbered, describing each working step in order. Help is provided for each respective input field by clicking the "Help" icon on the right side.</li>
</ul>
Expand All @@ -27,22 +24,23 @@
<p><span style="font-size: 16px; font-family: 'Arial Black',sans-serif;">Citing RSWAT<br /></span></p>
</div>
<div>
<div>
<ul style="margin-bottom: 0in;" type="square">
<li style="font-size: 16px; font-family: 'Times New Roman'; margin: 0in 0in 8.0pt 0in;">Nguyen, T. V., Dietrich, J., Dang, D. T., Tran, D. A., Doan, B. V., Sarrazin, F. J., Abbaspour, K., Srinivasan, R. (2022). An interactive graphical interface tool for parameter calibration, sensitivity analysis, uncertainty analysis, and visualization for the Soil and Water Assessment Tool. Environmental Modelling &amp; Software, 156, 105497. <a href="https://doi.org/10.1016/j.envsoft.2022.105497" target="_blank" rel="noopener">https://doi.org/10.1016/j.envsoft.2022.105497.</a></li>
</ul>
</div>
</div>
</div>
</div>
<div>
<p><span style="font-size: 16px; font-family: 'Arial Black',sans-serif;">Contribution and Acknowledgement<br /></span></p>
</div>
<div>
<div>
<ul>
<li style="font-size: 16px; font-family: 'Times New Roman'; margin: 0in 0in 8.0pt 0in;">This package was developed by <a href="https://www.ufz.de/index.php?en=46415" target="_blank">Dr. Tam V. Nguyen</a>, Department of Hydrogeology, Helmholtz Centre for Environmental Research - UFZ, Germany.</li>
<li style="font-size: 16px; font-family: 'Times New Roman'; margin: 0in 0in 8.0pt 0in;">We thank reviewers of RSWAT papers, whose comments have helped to improve the quality of RSWAT. We thank RSWAT users for reporting bugs, suggestion on the app functionalities.</li>
</ul>
</div>
<div>
<p><span style="font-size: 16px; font-family: 'Arial Black',sans-serif;">Updates/News<br /></span></p>
</div>
<ul>
<li style="font-size: 16px; font-family: 'Times New Roman'; margin: 0in 0in 8.0pt 0in;">Version 4.01: FEATURES: extract simulated results to .csv file, extract behavioral parametersets; FIX: RSWAT is interrupted when reading HRU information </li>
</ul>
31 changes: 22 additions & 9 deletions inst/R-SWAT/R/runSWATUI.R
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ runSwatUI <- function(id) {
#-------------------------------------------------------------------------
# 5. See simulation report
#-------------------------------------------------------------------------
div( style = "margin-top: 15em",
div( style = "margin-top: 20em",
column(width = 10,
HTML("<b>","5. See simulation report","</b>"),
),
Expand All @@ -140,19 +140,12 @@ runSwatUI <- function(id) {
class = NULL),
),

column(width = 5,
column(width = 10,
checkboxInput('checkCurrentSimulation',
'Open file CurrentSimulationReport.log',
value = FALSE, width = NULL),
),

column(width = 5,
checkboxInput('checkDisplayParameterSet',
'Display all parameter sets',
value = FALSE, width = NULL),
),


conditionalPanel(
condition = "input.checkCurrentSimulation == 1",
column(width = 10,
Expand All @@ -161,12 +154,32 @@ runSwatUI <- function(id) {
),
),

column(width = 10,
checkboxInput('checkDisplayParameterSet',
'Display all parameter sets',
value = FALSE, width = NULL),
),

conditionalPanel(
condition = "input.checkDisplayParameterSet == 1",
column(width = 10,
dataTableOutput("tableDisplayParameterSet"),
),
),

# Check box display all simulation results
column(width = 10,
checkboxInput("checkSaveSimTocsv",
"Save simulated results as .csv files"),
),

column(width = 1,
actionButton("helpCheckSaveSimTocsv",
"Help",
buttonType = "default",
style="background-color: none; border-color: none",
class = NULL),
),
#-------------------------------------------------------------------------

)
Expand Down
1 change: 0 additions & 1 deletion inst/R-SWAT/R/utilities.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

#-------------------------------------------------------------------------------
# Binding list object (with 3 levels of list)
# ------------------------------------------------------------------------------
Expand Down
73 changes: 71 additions & 2 deletions inst/R-SWAT/server.R
Original file line number Diff line number Diff line change
Expand Up @@ -615,8 +615,11 @@ server <- function(input, output, session) {

# If this is a SWAT project
if (globalVariable$SWATProject & globalVariable$TxtInOutSWAT){

# Get HRU information (land use, slope, soil, sub)
globalVariable$HRUinfo <<- getHruInfo(globalVariable$TxtInOutFolder)
spsComps::shinyCatch(
globalVariable$HRUinfo <<- getHruInfo(globalVariable$TxtInOutFolder),
blocking_level = "error")

# Get unique soil, land use, slope, max number of subbasins
uniqueSoil <- unique(globalVariable$HRUinfo$soil)
Expand Down Expand Up @@ -1059,11 +1062,17 @@ server <- function(input, output, session) {
"formatted as follows:","\n",
" First row: Name of the parameters","\n",
" From the second row: Parameter value", "\n",
" Leave one row empty at the end of the file", "\n",
" ", "\n",
" As many column as the number of parameters","\n",
" Columns are seperated by one or more spaces or tabs","\n",
" The order of parameters in these columns follows ",
"the order of the parameters in the above Table", "\n",
" ", "\n",
"Example (content of myParameterFile.txt)", "\n",
" ", "\n",
" GW_DELAY.gw CN2.mgt SOL_K.sol ALPHA_BF.gw ESCO.hru SURLAG.hru CH_K2.rte SURLAG.bsn", "\n",
" 228.03 0.05 -0.21 0.07 0.80 0.14 0.41 3.39", "\n",
" 96.27 -0.12 0.17 0.35 0.66 0.34 0.03 3.00", "\n",
sep = "")

} else if (input$samplingApproach == 'Sensi_(from_userDefined_package)'){
Expand Down Expand Up @@ -1414,6 +1423,21 @@ server <- function(input, output, session) {
easyClose = TRUE
))
})

# ****************************************************************************
# Help button helpCheckSaveSimTocsv
# ****************************************************************************
observe({
req(input$helpCheckSaveSimTocsv)

showModal(modalDialog(
title = "Help: Save simulated results as .csv files",
HTML(readLines(file.path(HTMLdir,"HTML",
"helpCheckSaveSimTocsv.html"),warn=FALSE)),
easyClose = TRUE
))
})

# ****************************************************************************
# Run SWAT
# ****************************************************************************
Expand Down Expand Up @@ -1825,6 +1849,15 @@ server <- function(input, output, session) {
min = 1,
max = globalVariable$nOutputVar,
step = 1)

# Update select variable number
updateSliderInput(session = session,
"showSimResultsVariable",
"Select variable",
value = 1,
min = 1,
max = globalVariable$nOutputVar,
step = 1)
}

} else {
Expand Down Expand Up @@ -1889,6 +1922,42 @@ server <- function(input, output, session) {
})


# ****************************************************************************
# Display simulated results
# ****************************************************************************
observe({
req(input$checkSaveSimTocsv)
# Display observed data in table
spsComps::shinyCatch(
if (TRUE){
# Get simulated data
for (var in 1:globalVariable$nOutputVar){

data <- c()
for (core in 1:globalVariable$ncores){
data <- rbind(data, read.csv(file = file.path(globalVariable$workingFolder,
"Output",
paste0("Core_", core),
paste0("out_var_", var, ".txt")),
header = FALSE))
}
# Convert to data frame
data <- matrix(data$V1, ncol = nrow(globalVariable$parameterValue), byrow = FALSE)
colnames(data) <- paste0("sim_", data[1,])

fileName <- file.path(globalVariable$workingFolder,
paste0("sim_variable_", var, ".csv"))
write.csv(data[-c(1),], file = fileName,
quote = FALSE, row.names = FALSE)

message(paste0("Simulated output variable ", var, " was saved as: ", fileName))
}
},
blocking_level = "error"
)
})


# ****************************************************************************
# Display parameter sets used for simulations
# ****************************************************************************
Expand Down

0 comments on commit 10f03e0

Please sign in to comment.