/
plotter.py
executable file
·305 lines (231 loc) · 8.13 KB
/
plotter.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#!/usr/bin/env python
"""
csv plotter
csv_plot.py
Rawser Spicer
created: 2014/02/03
modified: 2015/01/05
This utility is designed to plot csv files. It can plot up to 10
files at a time, or 1 file with an arbitary number of columns of data
version 2015.1.5.1:
added example usage
version 2014.12.2.1:
undid changes in chec_files, becuase the bug causing the problem was
found in csv_lib/csv_args
version 2014.12.1.1:
fixed the check_files function, which stoped working at an unknown time
version 2014.7.31.1:
updated documentation
version 2014.5.30.1:
fixed typo in date portion of file header
version 2013.3.17.1:
added support for the new polot class in csvPlot
version 2014.3.12.1:
added ArgClass support
version 2014.2.28.1:
added support for multiple columns of data
version 2014.2.12.2:
fixed up documentation and help
version 2014.2.12.1:
added --plot_avg flag, which will plot the a data set over the average
of all other data sets
version 2014.2.10.1:
the legend will now show the data titile instead of the file name
version 2014.2.7.2:
added the legend
version 2014.2.7.1 (working version 1):
this is the working version. added a fuction process interval to further
subdevide the work of csv_plotter function. fixed all documentation
version 2014.2.6.2:
added the plotter function to act like a main function in c++
version 2014.2.6.1:
polts a csv file with the csv_plot module
version 2014.2.3.1:
plots a csv file
"""
import csv_lib.csv_args as csva
from csv_lib.csv_utilities import print_center, check_file, \
exit_on_failure, exit_on_success
import csv_lib.csv_plot as csvp
from csv_lib.csv_date import get_last_date
import datetime as dtime
def get_year(value):
"""
get a year
arguments
value: (string) the argument from client
returns:
a interger year
"""
if (value == ""):
return 0
else:
return int(value)
def get_interval(value):
"""
gets an interval from the command line
arguments:
value: (string) the argument from client
returns:
an interval string
"""
if(value == ""):
return "0000-00-00,0000-00-00"
else:
return value
def get_string(value):
"""
gets a string form clinet or returns "" for no argument
arguments:
value: (string)the argument from client
returns:
a string
"""
return value
def get_delta(value):
"""
turns a number of days input into a datetime.timedelat object
arguments:
value: (string) the argument from client
returns:
a datetime.timedelat object
"""
if (value == ""):
return dtime.timedelta(0)
else :
return dtime.timedelta(int(value))
def get_bool(value):
"""
get boolean from command line
arguments:
value: (string)the argument from client
returns:
a bool
"""
if (value == ""):
return False
elif(value == 'T' or value == 't' or value == 'TRUE' or
value == 'True' or value == 'true' ):
return True
else:
return False
def check_files(cmds, file_keys):
"""
creates a list of files to plot
arguments:
cmds: ((string)list)the list of command imputs
file_keys: ((string)list)the keys that might contain files
returns:
a list of valid files
"""
count = 0
files = []
for key in file_keys:
try:
#~ if cmds[key] == "" and key != "--data_0":
#~ continue
if not (check_file(cmds[key])):
print_center("ERROR: invalid data_file, " + cmds[key])
exit_on_failure()
else:
count += 1
files.append(cmds[key])
except KeyError:
if (count == 0) :
raise (KeyError, "no files indicated" )
#print_center("ERROR: no files indicated", '*')
#print_center("use --data_0 as flag for a singal file")
#exit_on_failure()
else:
continue
return files
def process_interval(commands):
"""
this function processes the commands for making a plot interval
arguments
commands: ((string)list) the commands
returns:
a interval
"""
year = commands.get_command_value("--year", get_year)
interval_string = commands.get_command_value("--time_interval"
, get_interval)
days = commands.get_command_value( "--days", get_delta)
if (year != 0):
return str(year)+"-01-01" , str(year)+"-12-31"
elif (interval_string != "0000-00-00,0000-00-00"):
return interval_string.split(',')[0], interval_string.split(',')[1]
elif (days):
end = get_last_date(commands['--data_0'])
return end - days, end
else:
return "min", "max"
#return start, end
UTILITY_TITLE = " plotting utility "
FILES = ("--data_0", "--data_1", "--data_2", "--data_3", "--data_4",
"--data_5", "--data_6", "--data_7", "--data_8", "--data_9")
OPT_FLAGS = ("--time_interval", "--output_png", "--title", "--y_label",
"--x_label", "--year", "--days", "--show", "--plot_avg",
"--multi_col_mode", "--num_cols") + FILES[1:]
REQ_FLAGS = (FILES[0],)
HELP_STRING = """
example usage:
python plotter.py --data_0=data.csv --output_png=graph.png
--year=2014 --title=graph --x_label=time
--y_label=precip
--data_0: the csv file to plot
--data_[1-9]: other csv fils to plot (optional)
--output_png: name of the .png file to save the plot to
--time_interval: the time interval to plot <2000-01-01,2000-12-31>(optional)
--year: the year to plot (optional)
--days: days from the last date in the file to plot (optional)
+++ NOTICE: if one of the above time interval +++
+++ options in not selected the entire data +++
+++ sets will be plotted +++
--title: plot title (optional: will be detrimined by the class by default)
--y_label: y-axis label (optional: will be detrimined by the class by default)
--x_label: x-axis label (optional: "" by default)
--show: set to true to show the plot instead of saving it
(optional: false by default)
WARNING >>> plot will not be witten to screen if unless <<<
>>> back end is changed in csv_plot.py <<<
--plot_avg: set to true to plot all data sets over an averge of
the other data sets (optional: false by default)
--multi_col_mode: this flag no longer does any thing as the
PlotClass should work by auto
--num_cols: this flag no longer does any thing as the
PlotClass should work by auto
"""
def csv_plotter():
"""
this is the csv plotter utility this functon acts like main
"""
print_center(UTILITY_TITLE, '-')
try:
commands = csva.ArgClass(REQ_FLAGS, OPT_FLAGS, HELP_STRING)
except (RuntimeError, error_message):
exit_on_failure(error_message[0])
files_to_plot = check_files(commands, FILES)
start, end = process_interval(commands)
plot = csvp.PlotClass(files_to_plot, commands["--output_png"])
plot.set_interval(start, end)
plot.set_up_plot()
temp = commands.get_command_value("--y_label", get_string)
if (temp != ""):
plot.set_y_label(temp)
temp = commands.get_command_value( "--x_label", get_string)
if (temp != ""):
plot.set_x_label(temp)
temp = commands.get_command_value( "--title", get_string)
if (temp != ""):
plot.set_title(temp)
if (commands.get_command_value( "--plot_avg", get_bool)):
plot.plot_avg()
else:
plot.plot()
plot.set_legend()
plot.save_plot()
exit_on_success()
#---run utility----
if __name__ == "__main__":
csv_plotter()