Section 4 Writing Input Files

The SWATBuildR cannot write the actual text files for our model setup, we have to use the SWAT+ Editor for this.

UPDATE: A tool has been created (author unknown) which writes our database for us in code form using python. The old section of doing it manually has been left in the doc (REF).

4.1 Via Python

run_this_chapter = FALSE

The files which execute this operation can be found here:

write_db_py <- "model_data/code/write_swat_db.py"
write_db_r <- "model_data/code/write_swat_db.r"

As you can see, it uses python. The python environment is archived here:

py_env <- "model_data/code/env/"

We can execute our python script it like so:

source("model_data/code/write_swat_db.R")

IMPORTANT: now that the input files have been written, we will ONLY modify these parameters with R and never edit parameters in the Editor. The reason for this is because changing the values of the text files will not change the values of the sqlite database. If the input files are re-written from, the database, our changes to the text files will be overwritten.

Let us test if our model works

4.1.1 SWAT+ Test Run

We will copy in our SWAT+ executable, our input files, and our weather files into a folder and run SWAT+ as a test.

For some reason, when writing the SWAT+ input files, it does not write the weather station data. I am not sure if this is intended or not. the SWAT+ model still runs fine, however maybe it is using the weather generator? The meteo input files are located in the same directory as the sqlite, probably because svatools put them there? #TODO

Our current SWAT+ version is rev60.5.4_64rel.exe but this is subject to change

Make a run directory and enable code running.

dir.create("model_data/cs10_setup/run_swat", showWarnings = F)

Copy all required files into this directory:

sta_files <- list.files("model_data/cs10_setup/optain-cs10/",
             pattern = "sta_", full.names = T)

cli_files <- list.files("model_data/cs10_setup/optain-cs10/", 
                        pattern = ".cli", full.names = T)

input_files <- list.files("model_data/cs10_setup/swat_input/", 
                       full.names = T)

path_to_swat <- "model_data/cs10_setup/rev60.5.4_64rel.exe"


source_files <- c(sta_files, cli_files, input_files, path_to_swat)


status <- file.copy(from = source_files,
                    to = "model_data/cs10_setup/run_swat/",
                    overwrite = T)

Lets run some checks on these files

if(any(status == FALSE)){
  warning("Some files were not copied:")
  print(source_files[which(status==FALSE)])
}

print(
  paste(
    length(which(status)),
    "of",
    length(source_files),
    "files were copied into the run folder, amounting to a size of ",
    round(sum(file.info(source_files)$size * 1e-6), 2),
    "megabytes"
  )
)

Now lets run the model to make sure it works

# update time sim
time_sim <- readLines("model_data/cs10_setup/run_swat/time.sim")
time_sim[3] <- "       0      2011         0      2011         0  "
writeLines(text = time_sim, con = "model_data/cs10_setup/run_swat/time.sim")

msg <- processx::run(command = "rev60.5.4_64rel.exe",
              wd = "model_data/cs10_setup/run_swat/")


simout <- readLines("model_data/cs10_setup/run_swat/simulation.out")

cat(tail(simout), sep = "\n")

It will be useful to test SWAT+ along our journey, so we will reuse this code in the function test_swat()

source("model_data/code/test_swat.R")

We can now continue with our final buildR step

4.2 Via SWAT Editor

If for some reason you cannot or do not want to write the input files via code, a step by step guide is below which shows you how to do it with the editor.

4.2.1 Loading the project in SWAT+ Editor

Open a project
Open a project
Path to the project sqlite
Path to the project sqlite

4.2.2 Writing input files

Project Information. The weather generator has been complete by svatools in Section 3
Project Information. The weather generator has been complete by svatools in Section 3
  1. After clicking on the Run Model / Save Scenario button, you arrive at the “Confirm Simulation Settings” page. Here you need to choose where to write your input files. We have chosen cs10_setup/swat_input.
  2. We have also changed our simulation time period, but this is not required.
  3. Make sure to un-check “Run SWAT” and “Analyze output for visualization”
  4. “Save settings & Run Selected”
Confirm Simulation Settings. Make sure to follow the instructions carefully here.
Confirm Simulation Settings. Make sure to follow the instructions carefully here.

Your SWAT+ input files will be generated, and you will be prompted to Save Scenario. This is not recommended, because in our testing, the scenario saving would recursively generate new scenarios within the same folder, until all file space had been exhausted on the drive. This extremely deep folder brings windows to its knees, even when trying to delete it using explorer (Use Powershell instead).

Option to save scenario. Not Recommended.
Option to save scenario. Not Recommended.

4.2.3 Post-write Changes

It is important to perform the final BuildR function as in Section REF.