-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathQuick_Look.py
More file actions
executable file
·2493 lines (1977 loc) · 94.4 KB
/
Quick_Look.py
File metadata and controls
executable file
·2493 lines (1977 loc) · 94.4 KB
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
'''
Quick_Look.py
Function: this file collects defined functions for plotting optimization results
from one optimization run or mutilple runs
Functions defined
plot_results_dispatch_1scenario()
plot_results_dispatch_1scenario()
func_graphics_dispatch_mix_technology_timeseries_1scenario()
func_graphics_dispatch_var_Nscenarios()
func_graphics_system_results_Nscenarios()
call_plot_results_1scenario() -- directly callable
func_optimization_results_system_results_Nscenarios() -- directly callable
func_optimization_results_dispatch_var_Nscenarios() -- directly callable
History
Jun 4-5, 2018 completely rewritten
plot_results_dispatch_1scenario()
func_graphics_dispatch_mix_time_selection()
func_graphics_dispatch_var_Nscenarios()
Jun 17-18, 2018 added a new function, and updated the comments
func_graphics_system_results_Nscenarios()
Jun 19, 2018
fixed some errors in plot_results_dispatch_1scenario()
rewrote some function names
Jun 20, 2018
slight changes due to changes the definition of func_lines_2yaxes_plot()
added the dual y-axes for some figures in func_graphics_system_results_Nscenarios()
added two functions for multiple-scenario analysis
func_optimization_results_snapshot_Nscenarios()
func_optimization_results_dispatch_var_Nscenarios()
Jun 21, 2018
fixed errors caused by using the actual division than the integer division.
added parallel axes for some figures in
plot_results_dispatch_1scenario()
plot_results_dispatch_1scenario()
func_graphics_dispatch_results_1scenario()
func_graphics_dispatch_var_Nscenarios()
func_optimization_results_system_results_Nscenarios()
changed packaging functions' names
func_graphics_dispatch_results_1scenario -> call_plot_results_1scenario
func_optimization_results_snapshot_Nscenarios -> func_optimization_results_system_results_Nscenarios
changed the function plot_results_dispatch_1scenario()
from fixed ranges in time to dynamically select the weeks with the largest/smallest share of a technology
Jun 22-23, 2018
updated the following two functions
func_optimization_results_system_results_Nscenarios()
plot_results_dispatch_1scenario()
.. so that the selected time ranges are determined for the 'extreme' weeks
.. for a technology of interest
Jun 23, 2018 checked the code and comments
June 23-24, 2018 updated texts and labels on figures
Jul 8, 2018 [kc]
Started making changes so this code runs off of dictionaries rather
than pickle files. Name changed to <Quick_Look.py>
@author: Fan Tong
'''
#from __future__ import division # Allows an integer divided by an integer to return a real.
# NOTE: THE ABOVE SEEMS TO ME TO BE BAD FORM, AS THIS IS NOT USED UNIVERSALLY.
import os
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import pickle
import copy
from cycler import cycler
from Supporting_Functions import func_find_period
from Supporting_Functions import func_lines_plot
from Supporting_Functions import func_lines_2yaxes_plot
from Supporting_Functions import func_stack_plot
from Supporting_Functions import func_scatter_plot
from Supporting_Functions import func_time_conversion
from Supporting_Functions import func_load_optimization_results
from matplotlib.backends.backend_pdf import PdfPages
from Save_Basic_Results import read_pickle_raw_results
#%%
def quick_look(global_dic, case_dic_list):
verbose = global_dic['VERBOSE']
if verbose:
print ( 'pickle files read' )
# --------------- define and open output files -------------------------
output_dir = global_dic['OUTPUT_PATH'] + '/' + global_dic['GLOBAL_NAME']
# Create the ouput folder
if not os.path.exists(output_dir):
os.makedirs(output_dir)
# Define file for pdfs containing figures comparing cases
output_all = global_dic['GLOBAL_NAME'] + '_all_cases.pdf'
pdf_all = PdfPages(output_dir + '/' + output_all) # create and open pdf file
# Define file for pdfs containing figures relating to individual cases
output_each = global_dic['GLOBAL_NAME'] + '_each_case.pdf'
pdf_each = PdfPages(output_dir + '/' + output_each) # create and open pdf file
# Define file for text output
output_text = global_dic['GLOBAL_NAME'] + '_text.txt'
text_file = open(output_dir + '/' + output_text,'w')
# --------------- define colors for plots ---------------------
color_DEMAND = 'black'
color_NATGAS = 'red' # not explicitly referenced but referenced through eval()
color_NATGAS_CCS = 'brown' # not explicitly referenced but referenced through eval()
color_SOLAR = 'orange' # not explicitly referenced but referenced through eval()
color_SOLAR2 = 'orangered' # not explicitly referenced but referenced through eval()
color_WIND = 'blue' # not explicitly referenced but referenced through eval()
color_WIND2 = 'darkblue' # not explicitly referenced but referenced through eval()
color_NUCLEAR = 'green' # not explicitly referenced but referenced through eval()
color_STORAGE = 'purple'
color_STORAGE2 = 'violet'
color_PGP_STORAGE = 'pink'
color_CURTAILMENT = 'lightgray'
color_UNMET_DEMAND = 'gray' # not explicitly referenced but referenced through eval()
color_CSP = 'yellow'
num_cases = len (case_dic_list) # number of cases
# 'SYSTEM_COMPONENTS' -- LIST OF COMPONENTS, CHOICES ARE: 'WIND','SOLAR', 'NATGAS','NATGAS_CCS','NUCLEAR','STORAGE', 'PGP_STORAGE', 'UNMET'
# Loop around and make output for individual cases
# ============= CREATE LIST OF input_data DICTIONARIES FOR PLOTTING PROGRAMS =========
for case_idx in range(num_cases):
case_dic = case_dic_list[case_idx] # get the input data for case in question
num_time_periods = len(case_dic['DEMAND_SERIES'])
if verbose:
print ( 'preparing case ',case_idx,' ', case_dic['CASE_NAME'])
result_dic = read_pickle_raw_results(global_dic, case_dic) # get the results data for case in question
input_data = copy.copy(case_dic) # Dictionary for input into graphing functions will be superset of case_dic and result_dic
input_data.update(result_dic) # input_data is now the union of case_dic and result_dic
input_data['pdf_each'] = pdf_each # file handle for pdf output case by case
input_data['text_file'] = text_file # file handle for text output case by case
system_components = case_dic['SYSTEM_COMPONENTS']
# results_matrix_dispatch contains time series of things that add electricity to the grid
# where unmet_demand is considered a pure variable cost source
# Now build the array of dispatch technologies to be plotted for this case
results_matrix_dispatch = []
legend_list_dispatch = []
color_list_dispatch = []
component_index_dispatch = {}
for component in system_components:
addfrom = ''
if component == 'STORAGE' or component == 'STORAGE2' or component == 'PGP_STORAGE' or component == 'CSP':
addfrom = 'FROM_'
results_matrix_dispatch.append(result_dic['DISPATCH_' + addfrom + component ])
legend_list_dispatch.append( 'DISPATCH_' + addfrom + component +' kW' )
color_list_dispatch.append(eval('color_' + component))
component_index_dispatch[component] = len(results_matrix_dispatch)-1 # row index for each component
max_dispatch = np.max([sum(i) for i in zip(*results_matrix_dispatch)])
max_cost = np.max(case_dic['DEMAND_SERIES']*result_dic['PRICE'])
input_data['max_dispatch'] = max_dispatch
input_data['max_cost'] = max_cost
input_data['NUM_TIME_PERIODS'] = num_time_periods
input_data['PRICE'] = result_dic['PRICE']
input_data['NUM_TIME_PERIODS'] = num_time_periods
input_data['results_matrix_dispatch'] = np.transpose(np.array(results_matrix_dispatch))
input_data['legend_list_dispatch'] = legend_list_dispatch
input_data['component_index_dispatch'] = component_index_dispatch
input_data['color_list_dispatch'] = color_list_dispatch
input_data['component_list_dispatch'] = system_components
#----------------------------------------------------------------------
# Now build the array of demand components to be plotted for this case
# NOTE: this should check that all these options are in the scenario
# The next set of things gets electricity from the grid
results_matrix_demand = [case_dic['DEMAND_SERIES']]
legend_list_demand = ['demand (kW)']
color_list_demand = [color_DEMAND]
component_index_demand = {'DEMAND':1}
component_list_storage = []
for component in system_components:
if component == 'STORAGE':
results_matrix_demand.append(result_dic['DISPATCH_TO_STORAGE'])
legend_list_demand.append('dispatch to storage (kW)')
color_list_demand.append(color_STORAGE)
component_index_demand['STORAGE'] = len(results_matrix_demand)-1
component_list_storage.append('STORAGE')
elif component == 'PGP_STORAGE':
results_matrix_demand.append(result_dic['DISPATCH_TO_PGP_STORAGE'])
legend_list_demand.append('dispatch to pgp storage (kW)')
color_list_demand.append(color_PGP_STORAGE)
component_index_demand['PGP_STORAGE'] = len(results_matrix_demand)-1
component_list_storage.append('PGPSTORAGE')
input_data['results_matrix_demand'] = np.transpose(np.array(results_matrix_demand))
input_data['legend_list_demand'] = legend_list_demand
input_data['component_index_demand'] = component_index_demand
input_data['color_list_demand'] = color_list_demand
input_data['component_list_storage'] = component_list_storage
#----------------------------------------------------------------------
# Now build the array of curtailment components to be plotted for this case
curtailment_dic = compute_curtailment(case_dic, result_dic)
results_matrix_curtailment = []
legend_list_curtailment = []
color_list_curtailment = []
component_index_curtailment = {}
for component in curtailment_dic.keys():
results_matrix_curtailment.append(curtailment_dic[component])
legend_list_curtailment.append(component + ' (kW)')
color_list_curtailment.append(eval('color_' + component))
component_index_curtailment[component] = len(results_matrix_curtailment)-1
input_data['results_matrix_curtailment'] = np.transpose(np.array(results_matrix_curtailment))
input_data['legend_list_curtailment'] = legend_list_curtailment
input_data['component_index_curtailment'] = component_index_curtailment
input_data['color_list_curtailment'] = color_list_curtailment
input_data['component_list_curtailment'] = curtailment_dic.keys()
#----------------------------------------------------------------------
# end of section to generate list of input_data dictionaries
# prepare_plot_results_bar_1scenario (input_data) # produce single case barchart plots
call_plot_results_1scenario (input_data) # produce single case time series plots
if verbose:
print ( 'done with call_plot_results_1scenario for case '+input_data['CASE_NAME'])
# close files
pdf_all.close()
pdf_each.close()
text_file.close()
if verbose:
print ( 'files closed')
#%%
def make_result_dic_list(global_dic, case_dic_list):
result_dic_list = []
for idx in range(len(case_dic_list)):
result_dic = read_pickle_raw_results(global_dic,case_dic_list[idx])
result_dic_list.append(result_dic)
return result_dic_list
#%%
def call_plot_results_1scenario(input_data):
# -------------------------------------------------------------------------
num_time_periods = input_data['NUM_TIME_PERIODS']
results_matrix_dispatch = input_data['results_matrix_dispatch']
component_index_dispatch = input_data['component_index_dispatch']
component_name_dispatch = {v: k for k, v in component_index_dispatch.items()}
input_data['page_title'] = 'raw output'
plot_results_dispatch_1scenario(input_data,1) # basic results by hour
plot_results_price_1scenario(input_data,1) # price results by hour
if 'STORAGE' in input_data['SYSTEM_COMPONENTS']:
plot_results_storage_1scenario(input_data,1)
input_data['page_title'] = 'daily averaging'
plot_results_dispatch_1scenario(input_data,min(num_time_periods,24)) # basic results by day
plot_results_price_1scenario(input_data,min(num_time_periods,24)) # price results by day
if 'STORAGE' in input_data['SYSTEM_COMPONENTS']:
plot_results_storage_1scenario(input_data,min(num_time_periods,24))
input_data['page_title'] = '5-day averaging'
plot_results_dispatch_1scenario(input_data,min(num_time_periods,24*5)) # basic results by week
plot_results_price_1scenario(input_data,min(num_time_periods,24*5)) # price results by day
if 'STORAGE' in input_data['SYSTEM_COMPONENTS']:
plot_results_storage_1scenario(input_data,min(num_time_periods,24*5))
# -------------------------------------------------------------------------
# Find the week where storage dispatch is at its weekly max or min use
for idx in range(results_matrix_dispatch.shape[1]):
plot_extreme_dispatch_results_1scenario(input_data, component_name_dispatch[idx],'max',min(num_time_periods,24*5))
return
#%%
def plot_extreme_dispatch_results_1scenario(input_data,component_name,search_option,window_size):
component_index_dispatch = input_data['component_index_dispatch']
component_index = component_index_dispatch[component_name]
results_matrix_dispatch = input_data['results_matrix_dispatch']
study_variable_dict = {
'window_size': window_size,
'data': results_matrix_dispatch[:,component_index],
'print_option': 0,
'search_option': search_option
}
study_output_1 = func_find_period(study_variable_dict)
start_hour = study_output_1['left_index']
end_hour = study_output_1['right_index']
input_data['page_title'] = (
component_name + ' ('+search_option+') supplied {:.2f} kW avg to the grid during hours: {} '
.format(study_output_1['value'], (start_hour,end_hour))
)
plot_results_dispatch_1scenario(input_data,1,start_hour,end_hour) # to storage min for 2 weeks
plot_results_price_1scenario(input_data,1,start_hour,end_hour) # price results by day
# if 'STORAGE' in input_data['SYSTEM_COMPONENTS']:
# plot_results_storage_1scenario(input_data,1,start_hour,end_hour)
#%%
def plot_results_dispatch_1scenario (input_data, hours_to_avg = None, start_hour = None, end_hour = None ):
# Note hours_to_average is assumed to be an integer
# -------------------------------------------------------------------------
# Get the input data
demand = input_data['DEMAND_SERIES']
results_matrix_dispatch = copy.deepcopy(input_data['results_matrix_dispatch'])
results_matrix_demand = copy.deepcopy(input_data['results_matrix_demand'])
results_matrix_curtailment = copy.deepcopy(input_data['results_matrix_curtailment'])
pdf_each = input_data['pdf_each']
legend_list_dispatch = input_data['legend_list_dispatch']
legend_list_demand = input_data['legend_list_demand']
legend_list_curtailment = input_data['legend_list_curtailment']
color_list_dispatch = input_data['color_list_dispatch']
color_list_demand = input_data['color_list_demand']
color_list_curtailment = input_data['color_list_curtailment']
case_name = input_data['CASE_NAME']
#NOTE: Averaging should occur before time subsetting
avg_label = ''
if hours_to_avg != None:
if hours_to_avg > 1:
avg_label = ' ' + str(hours_to_avg) + ' hr moving avg'
for i in range(results_matrix_dispatch.shape[1]):
results_matrix_dispatch [:,i] = func_time_conversion(results_matrix_dispatch[:,i],hours_to_avg)
for i in range(results_matrix_demand.shape[1]):
results_matrix_demand [:,i] = func_time_conversion(results_matrix_demand[:,i],hours_to_avg)
for i in range(results_matrix_curtailment.shape[1]):
results_matrix_curtailment [:,i] = func_time_conversion(results_matrix_curtailment[:,i],hours_to_avg)
demand = func_time_conversion(demand,hours_to_avg)
if start_hour == None:
start_hour = 0
if end_hour == None:
end_hour = len(demand)
# -------------------------------------------------------------------------
# -------------------------------------------------------------------------
# Define the plotting style
plt.close() # Just make sure nothing is open ...
regular_font = 5
small_font = 4
#plt.style.use('default')
plt.style.use('default')
# plt.style.use('bmh')
# plt.style.use('fivethirtyeight')
# plt.style.use('seaborn-white')
#plt.rcParams['font.family'] = 'serif'
#plt.rcParams['font.serif'] = 'Helvetica ' #'Palatino' # 'Ubuntu'
plt.rcParams['font.monospace'] = 'Helvetica Mono' #'Palatino Mono' # 'Ubuntu'
plt.rcParams['font.size'] = regular_font
plt.rcParams['axes.labelsize'] = regular_font
plt.rcParams['axes.linewidth'] = 0.5
plt.rcParams['axes.labelweight'] = 'normal'
plt.rcParams['axes.titlesize'] = regular_font
plt.rcParams['xtick.labelsize'] = regular_font
plt.rcParams['ytick.labelsize'] = regular_font
plt.rcParams['legend.fontsize'] = small_font
plt.rcParams['figure.titlesize'] = regular_font
plt.rcParams['lines.linewidth'] = 0.5
plt.rcParams['grid.color'] = 'k'
plt.rcParams['grid.linestyle'] = ':'
plt.rcParams['grid.linewidth'] = 0.5
plt.rcParams['xtick.major.width'] = 0.5
plt.rcParams['xtick.major.size'] = 3
plt.rcParams['xtick.direction'] = 'in'
plt.rcParams['ytick.major.width'] = 0.5
plt.rcParams['ytick.major.size'] = 3
plt.rcParams['ytick.direction'] = 'in'
#
figsize_oneplot = (6.5,9)
# -------------------------------------------------------------------------
# Figures 1 Hourly time series results
# Four figures: 2 (dispatch, demand) * 2 (line plots, stack plots)
# -------------
num_time_periods = demand.size
x_data = np.arange( num_time_periods)
# -------------
figDispatch, axs = plt.subplots(3, 2,figsize=figsize_oneplot)
axs[0,0].set_prop_cycle(cycler('color', color_list_dispatch))
inputs_dispatch = {
'x_data': x_data[start_hour:end_hour],
# 'y_data': results_matrix_dispatch,
'y_data': results_matrix_dispatch[start_hour:end_hour],
'z_data': demand[start_hour:end_hour],
'ax': axs[0,0],
'x_label': 'Time (hour)',
'y_label': 'kW',
'title': case_name +' hour '+str(start_hour)+' to '+str(end_hour)+'\nElectricity sources '+avg_label,
# If legend is not defined, no legend appears on plot
# legend is provided by accompanying stacked area plot
# 'legend': legend_list_dispatch,
# 'legend_z': 'demand',
'line_width': 0.5,
'line_width_z': 0.2,
'grid_option': 0,
}
axs[0,0].set_ylim([0, input_data['max_dispatch']])
func_lines_plot(inputs_dispatch)
# -------------
axs[0,1].set_prop_cycle(cycler('color', color_list_dispatch))
inputs_dispatch['ax'] = axs[0,1]
inputs_dispatch['legend'] = legend_list_dispatch
inputs_dispatch['legend_z'] = 'demand'
axs[0,1].set_ylim([0, input_data['max_dispatch']])
func_stack_plot(inputs_dispatch)
# ------------- NOW DO DEMAND ---------------------
axs[1,0].set_prop_cycle(cycler('color', color_list_demand))
inputs_demand = {
'x_data': x_data[start_hour:end_hour],
'y_data': results_matrix_demand[start_hour:end_hour],
#'z_data': demand,
'ax': axs[1,0],
'x_label': 'Time (hour)',
'y_label': 'kW',
'title': case_name +' hour '+str(start_hour)+' to '+str(end_hour)+'\nElectricity sinks '+avg_label,
# Don't print ( legend on line plot by not having it defined in this dictionary
# 'legend': legend_list_demand,
#'legend_z': 'demand',
'line_width': 0.5,
#'line_width_z': 0.2,
'grid_option': 0,
}
axs[1,0].set_ylim([0, input_data['max_dispatch']])
func_lines_plot(inputs_demand)
# -------------
axs[1,1].set_prop_cycle(cycler('color', color_list_demand))
inputs_demand['ax'] = axs[1,1]
inputs_demand['legend'] = legend_list_demand
axs[1,1].set_ylim([0, input_data['max_dispatch']])
func_stack_plot(inputs_demand)
# ------------- NOW DO CURTAILMENT ---------------------
axs[2,0].set_prop_cycle(cycler('color', color_list_curtailment))
inputs_curtailment = {
'x_data': x_data[start_hour:end_hour],
'y_data': results_matrix_curtailment[start_hour:end_hour],
#'z_data': demand,
'ax': axs[2,0],
'x_label': 'Time (hour)',
'y_label': 'kW',
'title': case_name +' hour '+str(start_hour)+' to '+str(end_hour)+'\nCurtailment '+avg_label,
# Don't print ( legend on line plot by not having it defined in this dictionary
# 'legend': legend_list_demand,
#'legend_z': 'demand',
'line_width': 0.5,
#'line_width_z': 0.2,
'grid_option': 0,
}
axs[2,0].set_ylim([0, input_data['max_dispatch']])
func_lines_plot(inputs_curtailment)
# -------------
axs[2,1].set_prop_cycle(cycler('color', color_list_curtailment))
inputs_curtailment['ax'] = axs[2,1]
inputs_curtailment['legend'] = legend_list_curtailment
axs[2,1].set_ylim([0, input_data['max_dispatch']])
func_stack_plot(inputs_curtailment)
# -------------
plt.suptitle(input_data['page_title'])
plt.tight_layout(rect=[0,0,0.75,0.975])
pdf_each.savefig(figDispatch)
plt.close()
#%%
#==============================================================================
# plot_results_price_1scenario
#
def plot_results_price_1scenario (input_data, hours_to_avg = None, start_hour = None, end_hour = None ):
# Note hours_to_average is assumed to be an integer
# -------------------------------------------------------------------------
# Get the input data
demand = input_data['DEMAND_SERIES']
price = copy.deepcopy(input_data['PRICE'])
results_matrix_dispatch = copy.deepcopy(input_data['results_matrix_dispatch'])
results_matrix_demand = copy.deepcopy(input_data['results_matrix_demand'])
results_matrix_curtailment = copy.deepcopy(input_data['results_matrix_curtailment'])
pdf_each = input_data['pdf_each']
legend_list_dispatch = input_data['legend_list_dispatch']
legend_list_demand = input_data['legend_list_demand']
legend_list_curtailment = input_data['legend_list_curtailment']
color_list_dispatch = input_data['color_list_dispatch']
color_list_demand = input_data['color_list_demand']
color_list_curtailment = input_data['color_list_curtailment']
case_name = input_data['CASE_NAME']
#NOTE: Averaging should occur before time subsetting
avg_label = ''
dispatch_cost_matrix = results_matrix_dispatch*price[:,np.newaxis]
if hours_to_avg != None:
if hours_to_avg > 1:
avg_label = ' ' + str(hours_to_avg) + ' hr moving avg'
for i in range(results_matrix_dispatch.shape[1]):
results_matrix_dispatch [:,i] = func_time_conversion(results_matrix_dispatch[:,i],hours_to_avg)
dispatch_cost_matrix [:,i] = func_time_conversion(dispatch_cost_matrix[:,i],hours_to_avg)
price = func_time_conversion(price,hours_to_avg)
# Note that mean price is by time, and not demand weighted
demand = func_time_conversion(demand,hours_to_avg)
if start_hour == None:
start_hour = 0
if end_hour == None:
end_hour = len(demand)
# -------------------------------------------------------------------------
# -------------------------------------------------------------------------
# Define the plotting style
plt.close() # Just make sure nothing is open ...
regular_font = 5
small_font = 4
#plt.style.use('default')
plt.style.use('default')
# plt.style.use('bmh')
# plt.style.use('fivethirtyeight')
# plt.style.use('seaborn-white')
#plt.rcParams['font.family'] = 'serif'
#plt.rcParams['font.serif'] = 'Helvetica ' #'Palatino' # 'Ubuntu'
plt.rcParams['font.monospace'] = 'Helvetica Mono' #'Palatino Mono' # 'Ubuntu'
plt.rcParams['font.size'] = regular_font
plt.rcParams['axes.labelsize'] = regular_font
plt.rcParams['axes.linewidth'] = 0.5
plt.rcParams['axes.labelweight'] = 'normal'
plt.rcParams['axes.titlesize'] = regular_font
plt.rcParams['xtick.labelsize'] = regular_font
plt.rcParams['ytick.labelsize'] = regular_font
plt.rcParams['legend.fontsize'] = small_font
plt.rcParams['figure.titlesize'] = regular_font
plt.rcParams['lines.linewidth'] = 0.5
plt.rcParams['grid.color'] = 'k'
plt.rcParams['grid.linestyle'] = ':'
plt.rcParams['grid.linewidth'] = 0.5
plt.rcParams['xtick.major.width'] = 0.5
plt.rcParams['xtick.major.size'] = 3
plt.rcParams['xtick.direction'] = 'in'
plt.rcParams['ytick.major.width'] = 0.5
plt.rcParams['ytick.major.size'] = 3
plt.rcParams['ytick.direction'] = 'in'
#
figsize_oneplot = (6.5,9)
# -------------------------------------------------------------------------
# Upper left will be a time series of price, upper right will be sorted by price high to low
# -------------
num_time_periods = demand.size
x_data = np.arange( num_time_periods)
# -------------
figPrice, axs = plt.subplots(3, 2,figsize=figsize_oneplot)
axs[0,0].set_ylim(min(price), max(price))
axs[0,0].set_prop_cycle(cycler('color', color_list_dispatch))
input_price_a = {
'x_data': x_data[start_hour:end_hour],
# 'y_data': results_matrix_dispatch,
#'y_data': np.asarray(price[start_hour:end_hour]),
'y_data': price[start_hour:end_hour],
'ax': axs[0,0],
'x_label': 'Time (hour)',
'y_label': '$/kWh',
'title': case_name +' hour '+str(start_hour)+' to '+str(end_hour)+'\nElectricity price ($/kWh) '+avg_label,
# If legend is not defined, no legend appears on plot
# legend is provided by accompanying stacked area plot
# 'legend': legend_list_dispatch,
# 'legend_z': 'demand',
'line_width': 0.5,
'line_width_z': 0.2,
'grid_option': 0,
'y_scale': "log"
}
func_lines_plot(input_price_a)
#--------- upper right now do the same thing but sort by price from high to low.
input_price_b = copy.copy(input_price_a)
input_price_b['x_data']= np.arange(end_hour-start_hour)
input_price_b['y_data']= np.sort(price[start_hour:end_hour])[::-1]
axs[0,1].set_ylim(min(price), max(price))
axs[0,1].set_prop_cycle(cycler('color', color_list_dispatch))
input_price_b['ax'] = axs[0,1]
input_price_b['x_label'] = 'hour rank: 0 = highest price'
func_lines_plot(input_price_b)
# =============================================================================
# =============================================================================
# We just had two plots of price by time and price by price ranked hour order
# Now, we should show contributions to cost of electricity (dispatch * price)
# =============================================================================
# =============================================================================
# -------------
# =============================================================================
#axs[1,0].set_ylim([0, input_data['max_cost']])
axs[1,0].set_prop_cycle(cycler('color', color_list_dispatch))
input_price_c = {
'x_data': x_data[start_hour:end_hour],
# 'y_data': results_matrix_dispatch,
'y_data': dispatch_cost_matrix[start_hour:end_hour],
'ax': axs[1,0],
'x_label': 'Time (hour)',
'y_label': '$/hr',
'title': case_name +' hour '+str(start_hour)+' to '+str(end_hour)+'\nCost of dispatched electricity '+avg_label,
# If legend is not defined, no legend appears on plot
# legend is provided by accompanying stacked area plot
#'legend': legend_list_dispatch,
# 'legend_z': 'demand',
'line_width': 0.5,
#'line_width_z': 0.2,
'grid_option': 0,
}
func_stack_plot(input_price_c)
# -------------
# If legend is not defined, no legend appears on plot
# legend is provided by accompanying stacked area plot
# 'legend': legend_list_dispatch,
#
# Now add legend for stack plot
#figure1b = plt.figure(figsize=figsize_oneplot)
input_price_d = copy.copy(input_price_c)
#axs[1,1].set_ylim([0, input_data['max_cost']])
axs[1,1].set_prop_cycle(cycler('color', color_list_dispatch))
sort_order = np.argsort(price[start_hour:end_hour])[::-1]
input_price_d['ax'] = axs[1,1]
input_price_d['x_data'] = np.arange(end_hour-start_hour)
input_price_d['x_label'] = 'hour rank: 0 = highest price'
input_price_d['y_data'] = results_matrix_dispatch [start_hour:end_hour][sort_order]
input_price_d['y_label'] = 'kW'
#input_price_d['z_data'] = demand[start_hour:end_hour],
input_price_d['title']= case_name +' hour '+str(start_hour)+' to '+str(end_hour)+'\nDispatch sorted by electricity price '+avg_label
input_price_d['legend'] = legend_list_dispatch
func_stack_plot(input_price_d)
# # -------------
#figure1b = plt.figure(figsize=figsize_oneplot)
# # # ------------- NOW DO DEMAND ---------------------
# #
# # #figure1c = plt.figure(figsize=figsize_oneplot)
# # axs[1,0] = figPrice.add_subplot(3,2,3)
# # axs[1,0].set_prop_cycle(cycler('color', color_list_demand))
# #
# # inputs_demand = {
# # 'x_data': x_data[start_hour:end_hour],
# # 'y_data': results_matrix_demand[start_hour:end_hour],
# # #'z_data': demand,
# # 'ax': axs[1,0],
# # 'x_label': 'Time (hour)',
# # 'y_label': 'kW',
# # 'title': case_name +' hour '+str(start_hour)+' to '+str(end_hour)+'\nElectricity sinks '+avg_label,
# #
# # # Don't print ( legend on line plot by not having it defined in this dictionary
# # # 'legend': legend_list_demand,
# # #'legend_z': 'demand',
# # 'line_width': 0.5,
# # #'line_width_z': 0.2,
# # 'grid_option': 0,
# # }
# #
# # axs[1,0].set_ylim([0, input_data['max_dispatch']])
# #
# # func_lines_plot(inputs_demand)
# #
# # # -------------
# #
# # #figure1d = plt.figure(figsize=figsize_oneplot)
# # axs[1,1] = figPrice.add_subplot(3,2,4)
# # axs[1,1].set_prop_cycle(cycler('color', color_list_demand))
# #
# # inputs_demand['ax'] = axs[1,1]
# # inputs_demand['legend'] = legend_list_demand
# #
# # axs[1,1].set_ylim([0, input_data['max_dispatch']])
# #
# # func_stack_plot(inputs_demand)
# #
# # # ------------- NOW DO CURTAILMENT ---------------------
# #
# # #figure1c = plt.figure(figsize=figsize_oneplot)
# # axs[2,0] = figPrice.add_subplot(3,2,5)
# # axs[2,0].set_prop_cycle(cycler('color', color_list_curtailment))
# #
# # inputs_curtailment = {
# # 'x_data': x_data[start_hour:end_hour],
# # 'y_data': results_matrix_curtailment[start_hour:end_hour],
# # #'z_data': demand,
# # 'ax': axs[2,0],
# # 'x_label': 'Time (hour)',
# # 'y_label': 'kW',
# # 'title': case_name +' hour '+str(start_hour)+' to '+str(end_hour)+'\nCurtailment '+avg_label,
# #
# # # Don't print ( legend on line plot by not having it defined in this dictionary
# # # 'legend': legend_list_demand,
# # #'legend_z': 'demand',
# # 'line_width': 0.5,
# # #'line_width_z': 0.2,
# # 'grid_option': 0,
# # }
# #
# # axs[2,0].set_ylim([0, input_data['max_dispatch']])
# #
# # func_lines_plot(inputs_curtailment)
# #
# # # -------------
# #
# # #figure1d = plt.figure(figsize=figsize_oneplot)
# # axs[2,1] = figPrice.add_subplot(3,2,6)
# # axs[2,1].set_prop_cycle(cycler('color', color_list_curtailment))
# #
# # inputs_curtailment['ax'] = axs[2,1]
# # inputs_curtailment['legend'] = legend_list_curtailment
# #
# # axs[2,1].set_ylim([0, input_data['max_dispatch']])
# #
# # func_stack_plot(inputs_curtailment)
# # =============================================================================
#
# =============================================================================
# -------------
plt.suptitle(input_data['page_title'])
plt.tight_layout(rect=[0,0,0.75,0.975])
pdf_each.savefig(figPrice)
#plt.close()
#pdf_each.savefig(figure1b)
#plt.close()
#pdf_each.savefig(figure1c)
#plt.close()
#pdf_each.savefig(figure1d)
plt.close()
#%%
#
def plot_results_storage_1scenario (input_data, hours_to_avg = None, start_hour = None, end_hour = None ):
# Note hours_to_average is assumed to be an integer
# -------------------------------------------------------------------------
# Get the input data
# note: At this point, input_data contains both case_dic and result_dic plus additional material.
# Note further that at this point, result_dic already includes storage_dic
# =============================================================================
# storage_dic = {
# "max_headroom": max_headroom,
# "mean_storage_time": mean_storage_time,
# "max_storage_time": max_storage_time,
# "elec_cost_elec_storage": elec_cost_elec_storage,
# "var_cost_elec_storage": var_cost_elec_storage,
# "revenue_elec_storage": revenue_elec_storage,
# "net_revenue": net_revenue.all,
# "net_revenue_perkWh": net_revenue_perkWh,
# "storage_cost_perkWh": storage_cost_perkWh
# }
# # <max_headroom> how much headroom the storage needed to deliver the electricity in each hour.
# # <mean_storage_time> mean storage time of electricity delivered in each hour.
# # <max_storage_time> maximum storage time of electricity delivered in each hour.
#
# # <net_revenue> net_revenue from storage considering cost of electricity plus variable costs associated with storage.
# # <cost_elec_storage> cost of electricity sold from storage in each hour
# # <var_cost_storage> variable costs associated with electricity sold from storage in each hour
# # <revenue_elec_storage> revenue from electricity sold from storage in each hour
#
# =============================================================================
demand = input_data['DEMAND_SERIES']
pdf_each = input_data['pdf_each']
# Catch cases where storage was not included in system
try:
price = copy.deepcopy(input_data['PRICE'])
max_headroom = copy.deepcopy(input_data['max_headroom'])
revenue_elec_storage = copy.deepcopy(input_data['net_revenue'])
net_revenue = copy.deepcopy(input_data['net_revenue'])
net_cost_elec_storage = copy.deepcopy(input_data['net_cost_elec_storage'])
# note: net_revenue = revenue_elec_storage - net_cost_elec_storage
dispatch_to_storage = copy.deepcopy(input_data['DISPATCH_TO_STORAGE'])
dispatch_from_storage = copy.deepcopy(input_data['DISPATCH_FROM_STORAGE'])
energy_storage = copy.deepcopy(input_data['ENERGY_STORAGE'])
except KeyError:
print("Storage not included in system, skipping plotting storage results from Quick_Look.plot_results_storage_1scenario")
return
case_name = input_data['CASE_NAME']
color_list_dispatch = input_data['color_list_dispatch']
#NOTE: Averaging should occur before time subsetting
avg_label = ''
if hours_to_avg != None:
if hours_to_avg > 1:
avg_label = ' ' + str(hours_to_avg) + ' hr moving avg'
price = func_time_conversion(price,hours_to_avg)
max_headroom = func_time_conversion(max_headroom,hours_to_avg)
net_revenue = func_time_conversion(net_revenue,hours_to_avg)
net_cost_elec_storage = func_time_conversion(net_cost_elec_storage,hours_to_avg)
dispatch_to_storage = func_time_conversion(dispatch_to_storage,hours_to_avg)
dispatch_from_storage = func_time_conversion(dispatch_from_storage,hours_to_avg)
energy_storage = func_time_conversion(energy_storage,hours_to_avg)
if start_hour == None:
start_hour = 0
if end_hour == None:
end_hour = len(demand)
# -------------------------------------------------------------------------
# -------------------------------------------------------------------------
# Define the plotting style
plt.close() # Just make sure nothing is open ...
regular_font = 5
small_font = 4
#plt.style.use('default')
plt.style.use('default')
# plt.style.use('bmh')
# plt.style.use('fivethirtyeight')
# plt.style.use('seaborn-white')
#plt.rcParams['font.family'] = 'serif'
#plt.rcParams['font.serif'] = 'Helvetica ' #'Palatino' # 'Ubuntu'
plt.rcParams['font.monospace'] = 'Helvetica Mono' #'Palatino Mono' # 'Ubuntu'
plt.rcParams['font.size'] = regular_font
plt.rcParams['axes.labelsize'] = regular_font
plt.rcParams['axes.linewidth'] = 0.5
plt.rcParams['axes.labelweight'] = 'normal'
plt.rcParams['axes.titlesize'] = regular_font
plt.rcParams['xtick.labelsize'] = regular_font
plt.rcParams['ytick.labelsize'] = regular_font
plt.rcParams['legend.fontsize'] = small_font
plt.rcParams['figure.titlesize'] = regular_font
plt.rcParams['lines.linewidth'] = 0.5
plt.rcParams['grid.color'] = 'k'
plt.rcParams['grid.linestyle'] = ':'
plt.rcParams['grid.linewidth'] = 0.5
plt.rcParams['xtick.major.width'] = 0.5
plt.rcParams['xtick.major.size'] = 3
plt.rcParams['xtick.direction'] = 'in'
plt.rcParams['ytick.major.width'] = 0.5
plt.rcParams['ytick.major.size'] = 3
plt.rcParams['ytick.direction'] = 'in'
#
figsize_oneplot = (6.5,9)
# -------------------------------------------------------------------------
# Upper left will be a time series of price, upper right will be sorted by price high to low
# -------------
num_time_periods = demand.size
x_data = np.arange( num_time_periods)
# -------------
figStorage, axs = plt.subplots(3, 2,figsize=figsize_oneplot)
axs[0,0].set_ylim(min(price), max(price))
axs[0,0].set_prop_cycle(cycler('color', color_list_dispatch))
input_storage_a = {
'x_data': x_data[start_hour:end_hour],
# 'y_data': results_matrix_dispatch,
#'y_data': np.asarray(price[start_hour:end_hour]),
'y_data': np.transpose(np.array((energy_storage[start_hour:end_hour],
max_headroom[start_hour:end_hour]))),
'ax': axs[0,0],
'x_label': 'Time (hour)',
'y_label': 'kWh',
'title': case_name +' hour '+str(start_hour)+' to '+str(end_hour)+'\nEnergy storage and headroom needed (kWh) '+avg_label,
# If legend is not defined, no legend appears on plot
# legend is provided by accompanying stacked area plot
# 'legend': legend_list_dispatch,
# 'legend_z': 'demand',
'line_width': 0.1,
'line_width_z': 0.2,
'grid_option': 0,
'y_scale': "log"
}
# print ('input_storage_a')
# for key in input_storage_a.keys():
# print (key,input_storage_a[key])
func_lines_plot(input_storage_a)