From 10f03e063b0ea6e15dd4cac7e30f72e9a8c4bfb6 Mon Sep 17 00:00:00 2001 From: Tam Nguyen Date: Sun, 1 Sep 2024 15:15:42 +0200 Subject: [PATCH] feat: allow extracting results to csv files and behavioral parametersets --- inst/R-SWAT/HTML/helpCheckSaveSimTocsv.html | 10 +++ inst/R-SWAT/HTML/introduction.html | 12 ++-- inst/R-SWAT/R/runSWATUI.R | 31 ++++++--- inst/R-SWAT/R/utilities.R | 1 - inst/R-SWAT/server.R | 73 ++++++++++++++++++++- 5 files changed, 108 insertions(+), 19 deletions(-) create mode 100644 inst/R-SWAT/HTML/helpCheckSaveSimTocsv.html diff --git a/inst/R-SWAT/HTML/helpCheckSaveSimTocsv.html b/inst/R-SWAT/HTML/helpCheckSaveSimTocsv.html new file mode 100644 index 0000000..701ea64 --- /dev/null +++ b/inst/R-SWAT/HTML/helpCheckSaveSimTocsv.html @@ -0,0 +1,10 @@ + + + + + + +

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"(

+ + + diff --git a/inst/R-SWAT/HTML/introduction.html b/inst/R-SWAT/HTML/introduction.html index 3ab7b9a..7e36032 100644 --- a/inst/R-SWAT/HTML/introduction.html +++ b/inst/R-SWAT/HTML/introduction.html @@ -3,19 +3,16 @@

About this app

-
  • This is an R package (RSWAT) with a graphical user interface for parallel parameter sensitivity, calibration, and uncertainty analysis with SWAT, SWAT+, SWAT-Carbon and other modified SWAT
  • The latest version of this app is always in the development branch. If you would like to contribute to the code, have any suggestions, want to report errors, and have scientific collaboration, please contact me. The best way to contact me is via the Google group specially designed for this app. Tutorial videos can be found in RSWAT YouTube channel.
  • 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 & R knowledge for this app are very limited.
-

Usage

-
  • Always follow the numbering scheme in this app, e.g., start with Tab 1. General setting → 2. Parameter sampling → 3. Run SWAT → 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.
@@ -27,22 +24,23 @@

Citing RSWAT

-
  • 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 & Software, 156, 105497. https://doi.org/10.1016/j.envsoft.2022.105497.
-
-

Contribution and Acknowledgement

-
  • This package was developed by Dr. Tam V. Nguyen, Department of Hydrogeology, Helmholtz Centre for Environmental Research - UFZ, Germany.
  • 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.
+
+

Updates/News

+ \ No newline at end of file diff --git a/inst/R-SWAT/R/runSWATUI.R b/inst/R-SWAT/R/runSWATUI.R index 12d65da..57b1ec6 100644 --- a/inst/R-SWAT/R/runSWATUI.R +++ b/inst/R-SWAT/R/runSWATUI.R @@ -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("","5. See simulation report",""), ), @@ -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, @@ -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), + ), #------------------------------------------------------------------------- ) diff --git a/inst/R-SWAT/R/utilities.R b/inst/R-SWAT/R/utilities.R index eb21aa0..3dbfe83 100644 --- a/inst/R-SWAT/R/utilities.R +++ b/inst/R-SWAT/R/utilities.R @@ -1,4 +1,3 @@ - #------------------------------------------------------------------------------- # Binding list object (with 3 levels of list) # ------------------------------------------------------------------------------ diff --git a/inst/R-SWAT/server.R b/inst/R-SWAT/server.R index a79a614..9298c59 100644 --- a/inst/R-SWAT/server.R +++ b/inst/R-SWAT/server.R @@ -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) @@ -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)'){ @@ -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 # **************************************************************************** @@ -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 { @@ -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 # ****************************************************************************