dleutnant / swmmr

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

Save time series results using the "read_out" function #46

Closed ghost closed 4 years ago

ghost commented 4 years ago

Hi everyone, I would like to ask you for help regarding the use of the read_out function contained in the swmmr package.

In particular I would like to know if it is possible to make sure that the item object name considers all the node or link elements present in the list of the .out file, without having to type each name myself.

for example instead of writing:

results <- read_out(simulation$out,  iType=1, object_name=c("SS1052","SS942"), vIndex=c(0,4,5))

I would like instead that:

results <- read_out(simulation$out,  iType=1, object_name=c("ALL NODES IN THE .OUT"), vIndex=c(0,4,5))

Maybe the question is very simple but i don't know how to do it, thanks a lot for the help.

ghost commented 4 years ago

I updated my question to make it easier and more precise, I hope you have time to help me, thanks.

dleutnant commented 4 years ago

Please have a look at the following reproducible example, especially the wrapper around the unexportedswmmr:::OpenSwmmOutFile(x):

library(swmmr)
# use a known example
inp_file <- system.file("extdata", "Example1.inp", package = "swmmr", mustWork = TRUE)

# read inp file and show that results of ALL NODES are exported
model <- read_inp(inp_file)
model$report
#> # A tibble: 5 x 2
#>   `Reporting Options` value
#>   <chr>               <chr>
#> 1 INPUT               NO   
#> 2 CONTROLS            NO   
#> 3 SUBCATCHMENTS       ALL  
#> 4 NODES               ALL  
#> 5 LINKS               ALL

# create dummy file
tmp_file <- tempfile()
rpt <- paste0(tmp_file, ".rpt")
out <-  paste0(tmp_file, ".out")

# run simulation
run_swmm(inp_file, rpt = rpt, out)
#> Argumente 'minimized' und 'invisible' sind nur für Windows

# wrapper function to extract all node names using swmmr's unexported functions
get_all_nodes <- function(x) {

  list_of_names <- swmmr:::OpenSwmmOutFile(x)

  swmmr:::CloseSwmmOutFile()

  return(list_of_names$nodes$names)

}

# get all node names
nodes <- get_all_nodes(out)
nodes
#>  [1] "9"  "10" "13" "14" "15" "16" "17" "19" "20" "21" "22" "23" "24" "18"

# import the data
results <- read_out(file = out, 
                    iType = 1, 
                    object_name = nodes, vIndex = c(0,4,5))

summary(results)
#>    Length Class  Mode
#> 9  3      -none- list
#> 10 3      -none- list
#> 13 3      -none- list
#> 14 3      -none- list
#> 15 3      -none- list
#> 16 3      -none- list
#> 17 3      -none- list
#> 19 3      -none- list
#> 20 3      -none- list
#> 21 3      -none- list
#> 22 3      -none- list
#> 23 3      -none- list
#> 24 3      -none- list
#> 18 3      -none- list

Created on 2020-06-12 by the reprex package (v0.3.0)

ghost commented 4 years ago

Thank you so much for your help, the get_all_nodes function does exactly what I wanted and in the same way I can create the get_all_links function for links. I take this opportunity to ask you for further advice, is there an easy way to transform the lists of xts elements created by the read_out function into data.frames? thanks again for your availability. Enrico

dleutnant commented 4 years ago

maybe like that?

library(broom)

merged_xts <- lapply(results, FUN = function(x) do.call(cbind, x))

xts_as_df <- lapply(merged_xts, FUN = function(x) broom::tidy(x))

Created on 2020-06-12 by the reprex package (v0.3.0)

ghost commented 4 years ago

I admire this ability to manage data on R in this way, it works great, thanks again for your help! Enrico