-
Notifications
You must be signed in to change notification settings - Fork 0
/
step_function_utility.py
executable file
·226 lines (179 loc) · 6.63 KB
/
step_function_utility.py
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/usr/bin/env python
"""
Step Function Utility
stepFuncUtil.py
Rawser Spicer
created: 2014/01/??
modified: 2014/07/31
This utility applys a step function to processed data
to allow for corrections to be applyed to said data
version 2015.1.5.1:
added example usage
version 2014.8.8.1:
added a class for the utility
version 2014.7.31.1:
updates documantation.
version 2014.3.10.1:
uses csv_args to handel the command line
version 2014.3.6.1:
updated to work with the CsvFile class
the old version apperes to not have been working at all but this one
should. it now also has a main finction, and is executible
version 2014.2.10.1:
updated to use new csv_lib name
version 2014.02.03.2:
uses the basic csv_utilities lib features
version 2014.02.03.1:
added support for the csv_utilites module
update 1:
now the unility creates the out put file if it dose not exist and
appends the data to one that does.
"""
from csv_lib.csv_utilities import print_center, exit_on_failure, exit_on_success
import csv_lib.csv_file as csvf
import csv_lib.csv_args as csva
import csv_lib.utility as util
UTILITY_TITLE = "Step Function Utility"
FLAGS = ("--infile", "--outfile", "--stepfile")
HELP_STRING = """
This utility can be used to correct values in a csve file by applying a
correction value over time periods specified in an second file
exapmle usage:
python step_function_utility.py --infile=file --stepfile=file
--outfile=file
--infile: <<path>/*.csv>
the file with the initial values
--stepfile: <<path>/*.csv>
the file withe the step values
--outfile: <<path>/*.csv>
file to save output too
"""
def linear_step_function(d_val, l_func):
"""
linear step function
a work in progress
"""
o_val = []
for index, time_step in enumerate(d_val):
val = l_func[1] * time_step + l_func[0]
o_val.append(val)
return o_val
def step_function(d_date, d_val, s_date, s_val):
"""
Adds the step values to the initila values
arguments:
d_date: ((datetime.dateime)list) an array of the dates of the
input data.
d_val: ((values)list) an array of values from the input data
s_date: ((datetime.dateime)list) an array of dates from the step
file indcating when values need to be modifyed
s_val: ((values)list) an array of values to modify the input values
returns:
a corrected array of values
"""
d_index = len(d_date)-1
s_index = len(s_date)-1
o_val = []
while (d_index >= 0):
if (d_date[d_index] < s_date[s_index]):
if (s_index > 0):
s_index = s_index - 1
if (d_date[d_index] >= s_date[s_index]):
temp = d_val[d_index] + s_val[s_index]
o_val.insert(0, temp)
else:
temp = d_val[d_index]
o_val.insert(0, temp)
d_index = d_index - 1
return o_val
def main():
"""
main function
"""
print_center(UTILITY_TITLE, '-')
try:
commands = csva.ArgClass(FLAGS, (), HELP_STRING)
except :
print("error on read in")
sys.exit()
#exit_on_failure(error_message[0])
try:
my_file = csvf.CsvFile(commands["--infile"], True)
my_steps = csvf.CsvFile(commands["--stepfile"], True)
except IOError:
print_center("ERROR: a required file was not found", '*')
exit_on_failure()
my_file[1] = step_function(my_file.get_dates(), my_file[1],
my_steps.get_dates(), my_steps[1])
print_center(commands["--infile"], '-')
print_center(commands["--stepfile"],'-')
my_file.save(commands["--outfile"])
exit_on_success()
class StepFunctionUtility(util.utility_base):
"""
this class is the setp function utility class
"""
def __init__(self, title = UTILITY_TITLE, r_args = FLAGS, o_args = (),
help = HELP_STRING):
"""
initilizes the utility
arguments:
title: (string) the utility title
r_args: (list) the required arguments
o_args: (list) the optional arguments
help: (string) the help
"""
super(StepFunctionUtility, self).__init__(title , r_args, o_args, help)
def step_function(self, d_date, d_val, s_date, s_val):
"""
the step function
arguments:
d_date: ((datetime)list) a list of the data dates
d_val: ((float) list) a list of the data values
s_date: ((datetime) list) a list of step dates
s_val: ((float) list) a list of step values
returns:
a list of corrected values
sample step file looks like this:
"2016-08-13 03:00:00",-0.61
"2016-08-15 02:30:00",-0.49
"""
d_index = len(d_date)-1
s_index = len(s_date)-1
o_val = []
# loop from the end of the data csv to the beginning
while (d_index >= 0):
#
if (d_date[d_index] < s_date[s_index]):
# this if statement checks to see which step value to apply.
if (s_index > 0):
s_index = s_index - 1
if (d_date[d_index] >= s_date[s_index]):
# this if statement applies the proper step value for the case where a step value shoudl be applied.
if ( d_val[d_index] != 6999.0 ) :
temp = d_val[d_index] + s_val[s_index]
else:
temp = d_val[d_index]
o_val.insert(0, temp)
else:
# This else covers the period before there are any steps applied.
temp = d_val[d_index]
o_val.insert(0, temp)
d_index = d_index - 1
return o_val
def main(self):
"""
main function
"""
try:
my_file = csvf.CsvFile(self.commands["--infile"], True)
my_steps = csvf.CsvFile(self.commands["--stepfile"], True)
except IOError:
self.print_center("ERROR: a required file was not found", '*')
self.exit()
my_file[1] = self.step_function(my_file.get_dates(), my_file[1],
my_steps.get_dates(), my_steps[1])
my_file.save(self.commands["--outfile"])
if __name__ == "__main__":
util = StepFunctionUtility()
util.run()