dleutnant / swmmr

R Interface for US EPA's SWMM
https://cran.r-project.org/package=swmmr
39 stars 15 forks source link

temporary out file not found #77

Closed thwald closed 2 years ago

thwald commented 2 years ago

My aim is to autocalibrate my model using swmmr, according to the available vignette. Following the example shipped, everything works fine. (Because of using Modified Green-Ampt as infiltration model, I installed swmmr from GitHub, using changes made by @hsonne. However, I had the same issue before, using the CRAN-package.)

Changing the script according to my model, I receive the following error when starting run_swmm. (for complete script see the end) I have seen similar questions in issues #37 and #17, but it appears as if they were not really solved. Running the model in SWMM works fine.

Warning message:
In normalizePath(path.expand(path), winslash, mustWork) :
  path[1]="C:\Users\STUDEN~1\AppData\Local\Temp\RtmpgVzIUH\file17fc15107bbf": system cannot find the file specified

Checking file.exists() gives me the following output:

> file.exists(tmp_rpt_file)
[1] TRUE
> file.exists(tmp_out_file)
[1] FALSE
> file.exists(exec)
[1] TRUE

Works for the tmp_rpt_file. Seems to me as if the temporary out-file is not being created/not available. I couldn't figure out why it works for the rpt-file, but not for the out-file. Does anybody have an idea what the explanation / a solution could be or if it is a bug?

Thank you.

Complete script:


install.packages("devtools")
install.packages("githubinstall")
library(devtools)
install_github("hsonne/swmmr")
library(swmmr)
library(DEoptim)

#### setup ####
# set path to inp
inp_file <- "D:/Studenten_Innen/MA_Theresa/SWMM/Kelze_with_groundwater.inp"

# both rpt and out files are temporary files
tmp_out_file <- tempfile()
tmp_rpt_file <- tempfile()

exec <- "D:/Studenten_Innen/MA_Theresa/EPA SWMM 5.2.0 (64-bit)/runswmm.exe"

# initiate the simulation
swmm_files <- run_swmm(
  inp = inp_file,
  rpt = tmp_rpt_file,
  out = tmp_out_file,
  exec =  "D:/Studenten_Innen/MA_Theresa/EPA SWMM 5.2.0 (64-bit)/runswmm.exe"
)
# script continues according to shipped example
``
[Kelze_with_groundwater.txt](https://github.com/dleutnant/swmmr/files/9184056/Kelze_with_groundwater.txt)
`
hsonne commented 2 years ago

Hello @thwald,

I tried to reproduce your examle. The function run_swmm() returns without giving an error. However, when running swmm from the command line I got the following output

... EPA SWMM 5.2 (Build 5.2.0)
o  Retrieving project data
... EPA SWMM completed in 0.00 seconds. There are errors.

In the created report file Kelze_with_groundwater.rpt I found:

  *********************
  Rainfall File Summary
  *********************
  Station    First        Last         Recording   Periods    Periods    Periods
  ID         Date         Date         Frequency  w/Precip    Missing    Malfunc.
  -------------------------------------------------------------------------------

  ERROR 317: cannot open rainfall data file C:\Users\hsonne\Documents\github-repos\S\swmmr\rain_5min.dat.
  1. Do you find the same "ERROR" message in your .rpt file?
  2. Do you fine any other "ERROR" messages in your .rpt file?
  3. If not, could you somehow provide the file rain_5min.dat?

Hint: You may look for error messages in the report file from within the script like that:

grep("ERROR", readLines(tmp_rpt_file, warn = FALSE), value = TRUE)
thwald commented 2 years ago

Hello @hsonne, thank you for your response.

When running the model via SWMM interface, I cannot find the same or any error in the created .rpt file. Reading the tmp_rpt_file created via swmmr, I found this:

$raingage_summary
# A tibble: 1 × 4
  Name  Data_Source                                                                Data_Type Recording_Interval
  <chr> <chr>                                                                      <lgl>     <lgl>             
1 RG1   D:/Studenten_Innen/MA_Theresa/SWMM/Kelze_with_groundwater.inprain_5min.dat NA        NA                

Plus, searching for error messages as suggested by you gives me the same error:

> grep("ERROR", readLines(tmp_rpt_file, warn = FALSE), value = TRUE)
[1] "  ERROR 317: cannot open rainfall data file D:/Studenten_Innen/MA_Theresa/SWMM/Kelze_with_groundwater.inprain_5min.dat."

The path to the rain file doesn't look right to me, it should be just "D:\Studenten_Innen\MA_Theresa\SWMM\rain_5min.dat". It is not clear to me why the path is set like that. Do I have to specify the right path somewhere in the script? You can find the file rain_5min.dat attached. rain_5min.txt

hsonne commented 2 years ago

Hello @thwald,

please try the following:

swmm_files <- run_swmm(
  inp = normalizePath(inp_file), # convert slashes to backslashes on Windows
  rpt = tmp_rpt_file,
  out = tmp_out_file,
  exec = exec # you should use the variable that you defined above ;)
)

I hope that this works for you. I was able to make the simulation run.

Background: It seems that SWMM creates the full path to the rain data file from the path to the input_file inp_file. To get the path to the parent folder in which the input file resides (in your case D:\Studenten_Innen\MA_Theresa\SWMM), SWMM seems to split the path at backslashes "\" and removes the last part, i.e. the name of the file, in your case Kelze_with_groundwater.inp. Unfortunately, you gave the path with forward slashes "/" (which is normally a good thing when working in R). SWMM could not determine the path to the parent folder correctly. By passing inp_file to normalizePath() the forward slashes in the path are converted to backslashes and SWMM (hopefully) correctly determines the path to your raindata file.

This should be handled by the function run_swmm(). For now, I hope you can life with this workaround (given that it works for you).

thwald commented 2 years ago

Running the simulation works now - Thank you very much for your help and the explanation!

hsonne commented 2 years ago

Welcome!