-
Notifications
You must be signed in to change notification settings - Fork 9
/
times.r
148 lines (136 loc) · 5.6 KB
/
times.r
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#*****************************************
#
# (C) Copyright IBM Corp. 2017
# Author: Ernesto Arandia & Bradley J Eck
#
#*****************************************
#' Get the value of one or more specific analysis time parameters.
#'
#' \code{ENgettimeparam} retrieves the value of one or more specific analysis time parameters.
#'
#' @export
#' @useDynLib epanet2toolkit RENgettimeparam
#' @param paramcode A character string or integer specifying the parameter code
#' (see below).
#'
#' @return A named integer with the value of the specified time parameter.
#'
#' @details Time parameter codes consist of the following constants:
#' \tabular{lll}{
#' \code{EN_DURATION} \tab 0 \tab Simulation duration\cr
#' \code{EN_HYDSTEP} \tab 1 \tab Hydraulic time step\cr
#' \code{EN_QUALSTEP} \tab 2 \tab Water quality time step\cr
#' \code{EN_PATTERNSTEP} \tab 3 \tab Time pattern time step\cr
#' \code{EN_PATTERNSTART} \tab 4 \tab Time pattern start time\cr
#' \code{EN_REPORTSTEP} \tab 5 \tab Reporting time step\cr
#' \code{EN_REPORTSTART} \tab 6 \tab Report starting time\cr
#' \code{EN_RULESTEP} \tab 7 \tab Time step for evaluating rule-based controls\cr
#' \code{EN_STATISTIC} \tab 8 \tab Type of time series post-processing used:\cr
#' \tab \tab 0 = none\cr
#' \tab \tab 1 = averaged\cr
#' \tab \tab 2 = minimums\cr
#' \tab \tab 3 = maximums\cr
#' \tab \tab 4 = ranges\cr
#' \code{EN_PERIODS} \tab 9 \tab Number of reporting periods saved to binary output file
#' }
#'
#' @examples
#' # path to Net1.inp example file included with this package
#' inp <- file.path( find.package("epanet2toolkit"), "extdata","Net1.inp")
#' ENopen(inp, "Net1.rpt")
#' ENgettimeparam("EN_DURATION")
#' ENgettimeparam("EN_HYDSTEP")
#' ENclose()
ENgettimeparam <- function(paramcode) {
codeTable = c("EN_DURATION", "EN_HYDSTEP", "EN_QUALSTEP", "EN_PATTERNSTEP", "EN_PATTERNSTART",
"EN_REPORTSTEP", "EN_REPORTSTART", "EN_RULESTEP", "EN_STATISTIC", "EN_PERIODS")
# check the arguments
if (missing(paramcode)) {
stop("Need to specify the time parameter code.")
}
if (is.character(paramcode)) {
code <- as.integer(match(paramcode, codeTable) - 1)
}
else if (is.numeric(paramcode)) {
code = as.integer(paramcode)
}
else {
stop("The time parameter code must be a character string or an integer.")
}
if (any(is.na(code))) {
stop("The time parameter code specified is incorrect.")
}
# getOneTimeParam <- function(pcode) {
# tp <- enEvalGetFunction("enGetTimeParam", pcode)
# tp <- .Call("enGetTimeParam", pcode)
# return(tp)
# }
# timeparam <- sapply(code, getOneTimeParam)
# timeparam <- data.frame(PARAMETER = codeTable[code + 1], VALUE = timeparam)
# return(timeparam)
x <- .C("RENgettimeparam", code, "", as.integer(-1))
check_epanet_error(x[[3]])
param_val <- charlong_to_int_or_char(x[[2]])
if( is.character(param_val)) warning("returning character because R does not handle long integers, see ?integer")
return(param_val)
}
#' Set the value of a time parameter.
#'
#' \code{ENsettimeparam} sets the value of a time parameter.
#'
#' @export
#' @useDynLib epanet2toolkit RENsettimeparam
#' @param paramcode An integer or character
#' @param timevalue An integer or character value of the time parameters in seconds.
#'
#' @details Time parameter codes consist of the following constants:
#'
#' \tabular{lrl}{
#' \code{EN_DURATION} \tab 0 \tab Simulation duration\cr
#' \code{EN_HYDSTEP} \tab 1 \tab Hydraulic time step\cr
#' \code{EN_QUALSTEP} \tab 2 \tab Water quality time step\cr
#' \code{EN_PATTERNSTEP} \tab 3 \tab Time pattern time step\cr
#' \code{EN_PATTERNSTART} \tab 4 \tab Time pattern start time\cr
#' \code{EN_REPORTSTEP} \tab 5 \tab Reporting time step\cr
#' \code{EN_REPORTSTART} \tab 6 \tab Reporting starting time\cr
#' \code{EN_RULESTEP} \tab 7 \tab Time step for evaluating rule-based controls\cr
#' \code{EN_STATISTIC} \tab 8 \tab Type of time series post-processing to use:\cr
#' \tab \tab \code{EN_NONE} (0) = none\cr
#' \tab \tab \code{EN_AVERAGE} (1) = averaged\cr
#' \tab \tab \code{EN_MINIMUM} (2) = minimums\cr
#' \tab \tab \code{EN_MAXIMUM} (3) = maximums\cr
#' \tab \tab \code{EN_RANGE} (4) = ranges
#' }
#'
#' Do not change time parameters after calling ENinitH in a hydraulic analysis or
#' \code{ENinitQ} in a water quality analysis
#'
#'
#' @examples
#' # path to Net1.inp example file included with this package
#' inp <- file.path( find.package("epanet2toolkit"), "extdata","Net1.inp")
#' ENopen(inp, "Net1.rpt")
#' ENgettimeparam("EN_HYDSTEP")
#' ENsettimeparam("EN_HYDSTEP", 600)
#' ENgettimeparam("EN_HYDSTEP")
#' ENclose()
ENsettimeparam <- function(paramcode, timevalue) {
if(length(paramcode) > 1) stop("one param at a time")
if(length(timevalue) > 1) stop("one param at a time")
codetable <- c("EN_DURATION", "EN_HYDSTEP", "EN_QUALSTEP",
"EN_PATTERNSTEP", "EN_PATTERNSTART", "EN_REPORTSTEP",
"EN_REPORTSTART", "EN_RULESTEP", "EN_STATISTIC")
if (is.character(paramcode)) {
code <- as.integer(match(paramcode, codetable) - 1)
} else if( is.numeric(paramcode)){
code <- as.integer(paramcode)
} else if( is.integer(paramcode)){
code <- paramcode
} else {
stop(paste("paramcode cannot be of class", class(paramcode)) )
}
value <- as.character(timevalue)
x <- .C("RENsettimeparam", code, value , as.integer(-1))
check_epanet_error(x[[3]])
return( invisible() )
}