-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabase_Loading.R
1388 lines (1074 loc) · 52.7 KB
/
Database_Loading.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
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
################################################################################
# TITLE: Database_Loading
# DESCRIPTION: Script loads database from Microsoft SQL Server, it then imports other data from files,
# and calls functions to display plots and table.
#
#
# AUTHOR: Jessica Van Os
# CONTACT: jvanos@ualberta.ca
# CREATED: May 2022
#
# NOTES: Make sure the project file is open first or "here" commands wont work right.
# Before running, create folder called "Data Files" inside project directory and populate it with
# any data you want to access.
# Once this file is run through completion, can call any functions with environment that is loaded
# (ie: you do not need to run it all again).
################################################################################
################################################################################
## LOAD REQUIRED PACKAGES AND SOURCE FUNCTIONS
################################################################################
{# Package Info
# tidyverse: Data science package
# ggplot: Used for graphical packages and aesthetic
# grid: Used for plotting, adds grid to the plot
# scales: Use to re-format plots
# gtable: Grob tables, more tools
# gridExtra: User functions for grid graphics
# odbc: Driver for Database Loading
# ggpubr: Used to reformat plots from ggplot
# DBI: Package for interface between database and R
# lubridate: Allow time and data manipulation
# cowplot: Quality features for ggplots
# scales: Graphical mapping stuff
# dplyr: Data manipulation package
# reshape2: Restructure data
# zoo: Used for time series indexing
# ggpattern: Geoms for ggplot2, allow pattern fills (like stripes)
# here: Package to set filepaths inside R project
# beepr: Allows sound to paly when code is done
# showtext: Allows fonts changes in ggplot
# DescTools: Stats tools
# pivottabler: Allows pivot tables to easily be created in R
# openxlsx: Used to interact with xlsx files from R environment
# timeDate: Used for time zone information and holidays.
# writexl: Easy way to write dataframes to excel files with multiple pages
# viridis: A color pallete for plots
# ggnewscale: Allow multiple color scales in one plot
# extrafont: Add windows font options to R environment.
# ggridges: Allow ridgeline plots
}
{ # Must load the here package in order to make sure internal project directories work
library(here)
# Import functions from other R files, take from the functions folder in R project
source(here('Functions','Output_Gen_Functions.R')) # Output and generation plots as well as other misc plots
source(here('Functions','Emission_Functions.R')) # Emission plots
source(here('Functions','Price_Functions.R')) # Plots related to prices
source(here('Functions','Build_Retire_Functions.R'))# Plots on new and retired resources
source(here('Functions','Intertie_Functions.R')) # Plots on trade information and BC/MT/SK information
source(here('Functions','Table_Functions.R')) # Summary pivot tables
source(here('Functions','Res_Filter_Functions.R')) # Filtering by resource type, required for plots
source(here('Functions','Daily_Output_Functions.R'))# Filtering by resource type, required for plots
source(here('Functions','Other_Functions.R')) # Other functions used in plotting functions
#source(here('Functions','Developing_Functions.R')) # Under construction functions
source(here('Functions','Data_Filt_To_File.R')) # Functions that filter data and export it to excel sheets
source(here('Functions','aeso_gen.R')) #
source(here('Functions','aseo_sim_comp_1.R')) #
source(here('Functions','Group_PlotSave.R')) #
# Packages required
packs_to_load = c("tidyverse","ggplot2","scales","grid","gtable","gridExtra","odbc","ggpubr","extrafont",
"DBI","lubridate","cowplot","scales","dplyr","reshape2","zoo",
"ggpattern","here","beepr","showtext","DescTools","pivottabler",
"openxlsx","sqldf","timeDate","writexl","viridis","ggnewscale","ggridges")
# Function to check for packages, install if not present, and load
packs_check(packs_to_load)
}
################################################################################
## CONNECT TO MICROSOFT SQL SERVER
################################################################################
{ #Input Database Name below:
SourceDB<-"validate_2022_2023"
#Connect to database specified (via server, user, and password)
con <- dbConnect(odbc(),
Driver = "SQL Server",
Server = rstudioapi::askForPassword("Server(IP_Address,Port)"),
Database = SourceDB,
UID = rstudioapi::askForPassword("User Name"),
PWD = rstudioapi::askForPassword("Password"))
}
################################################################################
## DEFINE CASES TO STUDY (IE: RUN ID) & COLOR SETTINGS
## Value can be found in the "Run_Id" column of any AURORA table
################################################################################
BC <- "Base Case"
# 1 = original, #3 - grayscale, #3 - black/white safe color (default)
COL_choice = 3
################################################################################
## READ TABLES FROM DATABASE INTO ENVIRONMENT
## Can edit to select required tables only, DOUBLE CHECK all tables are in database
## It will just skip tables that are not there and all the ones after
################################################################################
{ # Fuel tables
FuelYr <- dbReadTable(con,'FuelYear1')
# FuelMn <- dbReadTable(con,'FuelMonth1')
# Resource Tables
ResYr <- dbReadTable(con,'ResourceYear1')
ResMn <- dbReadTable(con,'ResourceMonth1')
ResHr <- dbReadTable(con,'ResourceHour1') # This one takes eons
ResSt <-dbReadTable(con,'ResourceStudy1')
ResEmYr <-dbReadTable(con,'ResourceEmissionsYear1')
# Resource Group Tables
ResGroupYr <- dbReadTable(con,'ResourceGroupYear1')
ResGroupMn <- dbReadTable(con,'ResourceGroupMonth1')
ResGroupHr <- dbReadTable(con,'ResourceGroupHour1')
ResGroupEmYr <- dbReadTable(con,'ResourceGroupEmissionsYear1')
ResGroupEmHr <- dbReadTable(con,'ResourceGroupEmissionsHour1')
# ResGroupEmSt <- dbReadTable(con,'ResourceGroupEmissionsStudy1')
# Other Tables
ResStackYr <- dbReadTable(con,'ResourceStackYear1')
# ResStackHr <- dbReadTable(con,'ResourceStackHour1')
LinkYr <- dbReadTable(con,'LinkYear1')
# LinkMn <- dbReadTable(con,'LinkMonth1')
# LinkHr <- dbReadTable(con,'LinkHour1')
# CC <- dbReadTable(con,'CustomConstraint1')
ZoneYr <- dbReadTable(con,'ZoneYear1')
ZoneMn <- dbReadTable(con,'ZoneMonth1')
ZoneHr <- dbReadTable(con,'ZoneHour1')
# LT Tables
LTRes <- dbReadTable(con,'LTResValue1')
Build <- dbReadTable(con,'LTBuildReport1')
Study <- dbReadTable(con,'LTStudyLog1')
#CReport <- dbReadTable(con,'LTConstraintReport1')
# Get rid of Unused R memory to keep speed up
gc()
}
################################################################################
## REFLECT PROPER TIME PERIODS
## Adds a column which is formated in a way that R can understand for dates and times
################################################################################
{ # Fuel Tables
FuelYr$Time_Period <- as.Date(as.character(FuelYr$Time_Period),
format = "%Y")
# FuelMn$Time_Period <- ym(FuelMn$Time_Period)
# Resource Tables
ResYr$YEAR <- as.POSIXct(as.character(ResYr$Time_Period),
format = "%Y")
ResYr$YEAR <- format(ResYr$YEAR,format="%Y") # Reformat for year only
ResMn$Time_Period <- ym(ResMn$Time_Period)
ResHr$date <- as.POSIXct(as.character(ymd_h(gsub(" Hr ", "_",ResHr$Time_Period))),
tz = "MST")-(60*60)
ResEmYr$YEAR <- as.POSIXct(as.character(ResEmYr$Time_Period),
format = "%Y")
# Resource Group Tables
ResGroupHr$date <- as.POSIXct(as.character(ymd_h(gsub(" Hr ", "_",ResGroupHr$Time_Period))),
tz = "MST")-(60*60)
ResGroupMn$Time_Period <- ym(ResGroupMn$Time_Period)
ResGroupYr$Time_Period <- as.Date(as.character(ResGroupYr$Time_Period),
format = "%Y")
ResGroupEmYr$Time_Period <- as.Date(as.character(ResGroupEmYr$Time_Period),
format = "%Y")
ResGroupEmHr$date <- as.POSIXct(as.character(ymd_h(gsub(" Hr ", "_",ResGroupEmHr$Time_Period))),
tz = "MST")-(60*60)
# Other Tables
ResStackYr$Time_Period <- as.Date(as.character(ResStackYr$Time_Period),
format = "%Y")
# ResStackHr$date <- as.POSIXct(as.character(ymd_h(gsub(" Hr ", "_",ResStackHr$Time_Period))),
# tz = "MST")-(60*60)
LinkYr$Time_Period <- as.Date(as.character(LinkYr$Time_Period),
format = "%Y")
# LinkMn$Time_Period <- ym(LinkMn$Time_Period)
# LinkHr$Time_Period <- as.POSIXct(as.character(ymd_h(gsub(" Hr ", "_",LinkHr$Time_Period))),
# tz = "MST")-(60*60)
ZoneYr$Time_Period <- as.Date(as.character(ZoneYr$Time_Period),
format = "%Y")
ZoneMn$Time_Period <- ym(ZoneMn$Time_Period)
ZoneHr$date <- as.POSIXct(as.character(ymd_h(gsub(" Hr ", "_",ZoneHr$Time_Period))),
tz = "MST")-(60*60)
gc()
}
################################################################################
## FILTER TABLES TO GET RID OF COLUMNS I DONT CARE ABOUT & PULL OUT IMPORT/EXPORT
################################################################################
{
# HOURLY DATA
# Resourse Group Hourly Tables, choose specific columns
ResGroupHr_sub <- ResGroupHr %>%
subset(., select = c(ID, date, Report_Year, Output_MWH, Run_ID, Capacity_Factor))
# Zone Hourly Tables, for average hourly results
ZoneHr_Avg <- ZoneHr %>%
filter(Name == "WECC_Alberta") %>%
filter(Condition == "Average") %>%
subset(., select = c(date, Price, Baseline_Demand, Demand, Demand_Total,
Net_Load, Net_Load_Total, Marginal_Resource,
Smp_Max_Date_Time, Smp_Max_Demand, Smp_Max_Capacity,Demand_Side_Output,
Run_ID, Imports, Exports))
# Zone Hourly for Everything
ZoneHr_All <- ZoneHr %>%
filter(Name == "WECC_Alberta") %>%
subset(., select = c(date, Condition, Price, Demand, Marginal_Resource,
Name,Report_Year, Report_Month,Demand_Side_Output,
Run_ID))
# #Hourly individual resources in BC or SK
# ResHr_Int <- ResHr
# filter(Zone == -c("WECC_Alberta")) %>%
# subset(., select = c(ID, Name, Beg_Date, End_Date, date, Capability, Capacity,
# Dispatch_Cost, Incr_Cost, Fixed_Cost, Fuel_Cost,
# Output_MWH, Percent_Marginal, Percent_Committed,
# Revenue, Variable_OM_Cost, Capacity_Factor,
# Total_Emission_Cost, Total_Hours_Run, Condition,
# Report_Year, Run_ID, Peak_Capacity,
# Primary_Fuel,Zone))
#Hourly For individual resources in AB
ResHr <- ResHr %>%
filter(Zone == "WECC_Alberta") %>%
subset(., select = c(ID, Name, Beg_Date, End_Date, date, Capability, Capacity,
Dispatch_Cost, Incr_Cost, Fixed_Cost, Fuel_Cost,Fuel_Usage,
Output_MWH, Percent_Marginal, Percent_Committed,
Revenue,Energy_Revenue_MWh,Value,Value_MWh,
Net_Cost,Total_Cost_MWh,Fixed_Cost,Variable_OM_Cost,Startup_Cost,Build_Cost,Capacity_Factor,
Total_Emission_Cost, Total_Hours_Run, Condition,
Report_Year, Run_ID, Peak_Capacity,
Used_For_Op_Reserve, Forced_Outage,Maint_Outage,
Primary_Fuel,Zone))
#IMPORT/EXPORT
# Select the Import/Export data Hourly
Import <- ZoneHr_Avg %>%
subset(., select = c(date, Imports, Run_ID)) %>%
'colnames<-'(c("date", "Output_MWH", "Run_ID")) %>%
add_column(ID = "Import")
Export <- ZoneHr_Avg %>%
subset(., select = c(date, Exports, Run_ID)) %>%
'colnames<-'(c("date", "Output_MWH", "Run_ID")) %>%
add_column(ID = "Export")
# Select Import/Export data Yearly
Import_Yr <- ZoneYr %>%
filter(Condition == "Average") %>%
subset(., select = c(Time_Period, Imports_Total, Run_ID,Name)) %>%
'colnames<-'(c("Time_Period", "Output_MWH", "Run_ID","Name")) %>%
add_column(ID = "Import")
Export_Yr <- ZoneYr %>%
filter(Condition == "Average") %>%
subset(., select = c(Time_Period, Exports_Total, Run_ID,Name)) %>%
'colnames<-'(c("Time_Period", "Output_MWH", "Run_ID","Name")) %>%
add_column(ID = "Export")
#Fix Aurora sign convention if needed
if (min(Export$Output_MWH) < 0) {
Export$Output_MWH <- Export$Output_MWH * -1 }
gc()
}
################################################################################
## BRING IN DATA FROM AESO FILES & FORMAT (OPTIONAL)
## SAVE NEW FILES:
## Only need to run if new files are available
## Place new raw files in "Data Files">"Alberta Data"
## Replace file names as needed
##
## LOAD R FILES
## Grab saves R files and load in workspace
################################################################################
{
# Input file names
student_data_name <- "student_data_2023_Aug_15_16_56.csv.gz"
nrg_raw_name <- "nrgstream_gen03Mar2023.RData"
# Output file names
merit_file_name <- "Leach_MeritData15Aug2023.RData"
nrg_file_name <- "nrgstream_gen_corrected03Mar2023.RData"
demand_file_name <-"nrgstream_demand03Mar2023.RData"
date_filt<-"2022-01-1"
yr_max <- 2024
# # FILTER AND SAVE NEW DATA
# NRG_student_generate_R(student_data_name,nrg_raw_name,merit_file_name,nrg_file_name,demand_file_name)
# READ R DATA AND FILTER BY YEAR
df1a <- Load_NRG_hourly(date_filt,yr_max,nrg_file_name,reformat_names=FALSE)
Actdemand <- Load_NRG_demand(date_filt,demand_file_name)
merit_filt <- readRDS(here("Data Files","Alberta Data",merit_file_name))
merit_filt <- filter(merit_filt,date >= as.Date(date_filt))
# # AESO Market stats wind data
# AESO_MS_Wind <- readRDS(here("Data Files","Alberta Data","AESO_market_stats_wind.RData"))
#
# AESO_MS_Wind <- AESO_MS_Wind %>%
# mutate(time = as.POSIXct(Date, format = "%m/%d/%Y %I:%M:%S %p"),
# Day = date(time),
# Year = year(time),
# Hour = hour(time))%>%
# rename(CF=`Capacity Factor`,
# Output=`Total Generation`)
}
################################################################################
## PLOT SETTINGS
################################################################################
{ # Available Fonts for plotting, can choose different one and change Plot_Text if needed
# Uses local computer font files (search font in search bar to confirm font names)
#font_import()
font_add(family="Times",regular="times.ttf")
font_add(family="times_bf", regular = "timesbd.ttf")
font_add("times_it", regular = "/path/to/your/fonts/timesi.ttf")
Plot_Text <- "Times"
Plot_Text_bf <- "times_bf"
Plot_Text_it <- "times_it"
showtext_auto()
# font_add(family="Cambrai",regular="CAMBRIA.ttc")
# Plot_Text <- 'Cambrai'
# Set default size for plot features to be constant. All based on general text size
{ GenText_Sz =46 # GGsave
#GenText_Sz = 20 # Windows save
Tit_Sz = GenText_Sz-2
XTit_Sz = GenText_Sz+6
YTit_Sz = GenText_Sz+6
Leg_Sz=GenText_Sz-6
Overall_Sz=GenText_Sz}
{ # Define fuel types for new builds
solar <- "SUN"
wind <- "WND"
other <- "OT"
storage <- "PS"
gas1 <- "Gas1"
gas0 <- "Gas0"
}
# Set legend color schemes for constancy
# Can define a new set if wanted here!
{
if (COL_choice == 1){
# Normal Color
# Import/Export
cOL_IMPORT <- "#F8B660"
cOL_EXPORT <- "burlywood4"
# Coal/Cogen
cOL_NUCLEAR <- "midnightblue"
cOL_COAL <- "black"
cOL_COGEN <- "gray30"
# H2 groups (blues)
cOL_SCGT_H2 <- "#7e4e90ff"
cOL_NGCC_H2 <- "darkorchid4"
COL_H2 <- cOL_NGCC_H2
#cOL_SCGT_Blend <- "#7FABD3"
#cOL_NGCC_Blend <- "#3573B9"
#COL_Blend <- cOL_NGCC_Blend
# Gas Groups (Purples)
cOL_COal2Gas <- "gray65"
cOL_NGConv <- cOL_COal2Gas
cOL_SCGT <- "gray50"
cOL_NGCC <- "gray85"
COL_NatGas <-cOL_NGCC
cOL_NGCC_CCS <-"#A79FE1"
# Renewables and Other
cOL_OTHER <- "steelblue1"
cOL_HYDRO <- "royalblue3"
cOL_SOLAR <- "#FDE725FF"#'darkgoldenrod1'#
cOL_WIND <- "#73D055FF" #"#238b45"#
col_WIND_CURT <-"#1d632d"
col_SOLAR_CURT <-'goldenrod1'
# Storage Groups
cOL_STORAGE <- "rosybrown1"
COL_Battery <-"rosybrown1"
COL_CompAir <-"coral3"
COL_Pumped <-"firebrick4"
}else if (COL_choice == 2){
# Gray-scale safe colors
# Renewables and Other
cOL_SOLAR <- "#BDBDBD"
cOL_WIND <- "#565656"
col_WIND_CURT <-cOL_WIND
col_SOLAR_CURT <-cOL_SOLAR
cOL_HYDRO <- "#717171"
cOL_OTHER <- #7b7b7b"
# H2 groups (blues)
cOL_SCGT_H2 <- "#272727"
cOL_NGCC_H2 <- "#7e7e7e"
COL_H2 <- cOL_SCGT_H2
# Gas Groups (Purples)
cOL_NGCC_CCS <-"#464646"
cOL_SCGT <-"#9B9B9B"
cOL_NGCC <- "#DDDDDD"
cOL_COal2Gas <- "#BDBDBD"
cOL_NGConv <- cOL_COal2Gas
COL_NatGas <-cOL_NGCC
# Coal/Cogen
cOL_COAL <- "#282828"
cOL_COGEN <-"#0A0A0A"
cOL_NUCLEAR <- "midnightblue"
# Import/Export
cOL_IMPORT <- "white"
cOL_EXPORT <- "white"
# Storage Groups
COL_Battery <-"#E7E7E7"
COL_CompAir <-"#EFEFEF"
COL_Pumped <-"#F5F5F5"
cOL_STORAGE <- COL_Battery
}else{
# Gray-scale safe colors
cOL_SOLAR <- "#D9D012"
cOL_WIND <- "#237636"
col_WIND_CURT <-cOL_WIND
col_SOLAR_CURT <-cOL_SOLAR
cOL_HYDRO <- "#5965F8"
cOL_OTHER <- "#3EA836"
# H2 groups (blues)
cOL_SCGT_H2 <- "#0E3239"
cOL_NGCC_H2 <- "#7e7e7e"
COL_H2 <- cOL_SCGT_H2
# Gas Groups (Purples)
cOL_NGCC_CCS <-"#464646"
cOL_SCGT <-"#9B9B9B"
cOL_NGCC <- "#DDDDDD"
cOL_COal2Gas <- "#C4AEFC"
cOL_NGConv <- cOL_COal2Gas
COL_NatGas <-cOL_NGCC
# Coal/Cogen
cOL_COAL <- "#460E65"
cOL_COGEN <-"#0A0A0A"
cOL_NUCLEAR <- "midnightblue"
# Import/Export
cOL_IMPORT <- "white"
cOL_EXPORT <- "white"
#cOL_EXPORT <- "#FFD4DA"
# Storage Groups
COL_Battery <-"#F4EEA0"
COL_CompAir <-"#EFEFEF"
COL_Pumped <-"#F5F5F5"
cOL_STORAGE <- COL_Battery
}
# Set plot color transparacny
Plot_Trans<-1
## Now Define Lists to assign legends and colors in plots
colours1=c("Trade"= cOL_EXPORT, "Coal"=cOL_COAL, "Cogeneration"=cOL_COGEN,
"Coal-to-Gas"=cOL_NGConv,"Hydrogen Simple Cycle"=cOL_SCGT_H2,"Hydrogen Combined Cycle"=cOL_NGCC_H2,
#"Blended Simple Cycle"=cOL_SCGT_Blend,"Blended Combined Cycle"=cOL_NGCC_Blend,
"Natural Gas Combined Cycle + CCS"=cOL_NGCC_CCS,
"Natural Gas Simple Cycle"=cOL_SCGT, "Natural Gas Combined Cycle"=cOL_NGCC,
"Hydro"=cOL_HYDRO, "Other"=cOL_OTHER, "Wind"=cOL_WIND,
"Solar"=cOL_SOLAR, "Storage"=cOL_STORAGE)
colours1_exist=c("Trade"= cOL_EXPORT, "Coal"=cOL_COAL, "Cogeneration"=cOL_COGEN,
"Coal-to-Gas"=cOL_NGConv,
"Natural Gas Simple Cycle"=cOL_SCGT, "Natural Gas Combined Cycle"=cOL_NGCC,
"Hydro"=cOL_HYDRO, "Other"=cOL_OTHER, "Wind"=cOL_WIND,
"Solar"=cOL_SOLAR, "Storage"=cOL_STORAGE)
colours1b=c("Trade"= cOL_EXPORT, "Coal"=cOL_COAL, "Cogeneration"=cOL_COGEN,
"Coal-to-Gas"=cOL_NGConv,"Hydrogen Simple Cycle"=cOL_SCGT_H2,"Hydrogen Combined Cycle"=cOL_NGCC_H2,
#"Blended Simple Cycle"=cOL_SCGT_Blend,"Blended Combined Cycle"=cOL_NGCC_Blend,
"Natural Gas Combined Cycle + CCS"=cOL_NGCC_CCS,
"Natural Gas Simple Cycle"=cOL_SCGT, "Natural Gas Combined Cycle"=cOL_NGCC,
"Hydro"=cOL_HYDRO, "Other"=cOL_OTHER, "Wind"=cOL_WIND,
"Solar"=cOL_SOLAR, "Storage"=cOL_STORAGE,"Demand Curtailment"="black")
# Used in Day2
colours1c=c("Import"= cOL_IMPORT, "Coal"=cOL_COAL, "Cogeneration"=cOL_COGEN,
"Coal-to-Gas"=cOL_NGConv,"H2SC"=cOL_SCGT_H2,"H2CC"=cOL_NGCC_H2,
#"Blended Simple Cycle"=cOL_SCGT_Blend,"Blended Combined Cycle"=cOL_NGCC_Blend,
"NGCC+CCS"=cOL_NGCC_CCS,
"SCCT"=cOL_SCGT, "NGCC"=cOL_NGCC,
"Hydro"=cOL_HYDRO, "Other"=cOL_OTHER, "Wind"=cOL_WIND,
"Solar"=cOL_SOLAR, "Storage"=cOL_STORAGE)
colours1_daily=c("Import"= cOL_IMPORT, "Coal"=cOL_COAL, "Cogeneration"=cOL_COGEN,
"Coal-to-Gas"=cOL_NGConv,
"NGSC"=cOL_SCGT, "NGCC"=cOL_NGCC,
"Hydro"=cOL_HYDRO, "Other"=cOL_OTHER, "Wind"=cOL_WIND,
"Solar"=cOL_SOLAR, "Storage"=cOL_STORAGE)
# Used for curtail graph
colours1_rcurt = c("Trade"= cOL_EXPORT, "Coal"=cOL_COAL, "Cogeneration"=cOL_COGEN,
"Coal-to-Gas"=cOL_NGConv,"Hydrogen Simple Cycle"=cOL_SCGT_H2,"Hydrogen Combined Cycle"=cOL_NGCC_H2,
#"Blended Simple Cycle"=cOL_SCGT_Blend,"Blended Combined Cycle"=cOL_NGCC_Blend,
"Natural Gas Combined Cycle + CCS"=cOL_NGCC_CCS,
"Natural Gas Simple Cycle"=cOL_SCGT, "Natural Gas Combined Cycle"=cOL_NGCC,
"Hydro"=cOL_HYDRO, "Other"=cOL_OTHER, "Wind"=cOL_WIND,
"Solar"=cOL_SOLAR, "Storage"=cOL_STORAGE,"Curtailed Solar"=cOL_SOLAR,"Curtailed Wind"=cOL_WIND)
pattern1_rcurt = c("Trade"= "none", "Coal"="none", "Cogeneration"="none",
"Coal-to-Gas"="none","Hydrogen Simple Cycle"="none","Hydrogen Combined Cycle"="none",
#"Blended Simple Cycle"="none","Blended Combined Cycle"="none",
"Natural Gas Combined Cycle + CCS"="none",
"Natural Gas Simple Cycle"="none", "Natural Gas Combined Cycle"="none",
"Hydro"="none", "Other"="none", "Wind"="none",
"Solar"="none", "Storage"="none","Curtailed Solar"="stripe","Curtailed Wind"="stripe")
colours2 = c("Coal"= cOL_COAL, "Coal-to-Gas"=cOL_COal2Gas, "Cogen"=cOL_COGEN,
"Natural Gas"=COL_NatGas,"Natural Gas + CCS"=cOL_NGCC_CCS,"Hydrogen"=COL_H2,
#"Natual Gas and Hydrogen Blend"=COL_Blend,
"Hydro"=cOL_HYDRO, "Other"=cOL_OTHER,
"Wind"=cOL_WIND, "Solar"=cOL_SOLAR, "Storage"=cOL_STORAGE,"Nuclear"=cOL_NUCLEAR)
colours3 = c("Coal"=cOL_COAL, "Cogeneration"=cOL_COGEN,
"Coal-to-Gas"=cOL_NGConv,"Hydrogen Simple Cycle"=cOL_SCGT_H2,"Hydrogen Combined Cycle"=cOL_NGCC_H2,
#"Blended Simple Cycle"=cOL_SCGT_Blend,"Blended Combined Cycle"=cOL_NGCC_Blend,
"Natural Gas Combined Cycle + CCS"=cOL_NGCC_CCS,
"Natural Gas Simple Cycle"=cOL_SCGT, "Natural Gas Combined Cycle"=cOL_NGCC,
"Hydro"=cOL_HYDRO, "Other"=cOL_OTHER, "Wind"=cOL_WIND,
"Solar"=cOL_SOLAR, "Storage"=cOL_STORAGE)
Patterns3 = c("Coal"="none", "Cogeneration"="none",
"Coal-to-Gas"="stripe","Hydrogen Simple Cycle"="none","Hydrogen Combined Cycle"="none",
#"Blended Simple Cycle"="stripe","Blended Combined Cycle"="stripe",
"Natural Gas Combined Cycle + CCS"="none",
"Natural Gas Simple Cycle"="none", "Natural Gas Combined Cycle"="none",
"Hydro"="none", "Other"="none", "Wind"="none",
"Solar"="none", "Storage"="none")
colours4=c("Import"= cOL_IMPORT, "Coal-to-Gas"=cOL_COal2Gas, "Coal"=cOL_COAL,"Cogen"=cOL_COGEN,
"Natural Gas"=COL_NatGas,"Natural Gas + CCS"=cOL_NGCC_CCS,"Hydrogen"=COL_H2,
#"Natual Gas and Hydrogen Blend"=COL_Blend,
"Hydro"=cOL_HYDRO, "Other"=cOL_OTHER, "Nuclear"=cOL_NUCLEAR,
"Wind"=cOL_WIND, "Solar"=cOL_SOLAR, "Storage"=cOL_STORAGE)
colours5 = c("Cogeneration"=cOL_COGEN,
"Coal-to-Gas"=cOL_NGConv,"Hydrogen Simple Cycle"=cOL_SCGT_H2,"Hydrogen Combined Cycle"=cOL_NGCC_H2,
#"Blended Simple Cycle"=cOL_SCGT_Blend,"Blended Combined Cycle"=cOL_NGCC_Blend,
"Natural Gas Combined Cycle + CCS"=cOL_NGCC_CCS,"Natural Gas Combined Cycle CCS Retrofit"=cOL_NGCC_CCS,
"Natural Gas Simple Cycle"=cOL_SCGT, "Natural Gas Combined Cycle"=cOL_NGCC,
"Hydro"=cOL_HYDRO, "Other"=cOL_OTHER, "Wind"=cOL_WIND,
"Solar"=cOL_SOLAR, "Storage - Battery"=COL_Battery,
"Storage - Compressed Air"=COL_CompAir, "Storage - Pumped Hydro"=COL_Pumped,
"Nuclear"=cOL_NUCLEAR
)
Patterns5 = c("Cogeneration"="none",
"Coal-to-Gas"="stripe","Hydrogen Simple Cycle"="none","Hydrogen Combined Cycle"="none",
#"Blended Simple Cycle"="none","Blended Combined Cycle"="none",
"Natural Gas Combined Cycle + CCS"="none","Natural Gas Combined Cycle CCS Retrofit"="stripe",
"Natural Gas Simple Cycle"="none", "Natural Gas Combined Cycle"="none",
"Hydro"="none", "Other"="none", "Wind"="none",
"Solar"="none", "Storage - Battery"="none",
"Storage - Compressed Air"="none", "Storage - Pumped Hydro"="none",
"Nuclear"="none"
)
colours6=c("Natural Gas"=COL_NatGas,"Hydrogen"=COL_H2,
#"Natual Gas and Hydrogen Blend"=COL_Blend,
"Hydro"=cOL_HYDRO, "Other"=cOL_OTHER,
"Wind"=cOL_WIND, "Solar"=cOL_SOLAR, "Storage - Battery"=COL_Battery,
"Storage - Compressed Air"=COL_CompAir, "Storage - Pumped Hydro"=COL_Pumped)
colours7=c("Total Emissions"="black","Coal"=cOL_COAL, "Cogeneration"=cOL_COGEN,"NAICS 221112 Cogeneration"= cOL_COGEN,
"Coal-to-Gas"=cOL_NGConv,
#"Blended Simple Cycle"=cOL_SCGT_Blend,"Blended Combined Cycle"=cOL_NGCC_Blend,
"Natural Gas Combined Cycle + CCS"=cOL_NGCC_CCS,
"Natural Gas Simple Cycle"=cOL_SCGT, "Natural Gas Combined Cycle"=cOL_NGCC, "Other"=cOL_OTHER)
Lines7=c("Total Emissions"=1,"Coal"=1, "Cogeneration"=6,
"Coal-to-Gas"=2,
#"Blended Simple Cycle"=5,"Blended Combined Cycle"=6,
"Natural Gas Combined Cycle + CCS"=2,
"Natural Gas Simple Cycle"=5, "Natural Gas Combined Cycle"=3, "Other"=4)
# EVERYTHING
colours8 = c("Coal"=cOL_COAL,"Coal-to-Gas"=cOL_NGConv,
"Natural Gas Combined Cycle"=cOL_NGCC,"Natural Gas Simple Cycle"=cOL_SCGT,
"Natural Gas Combined Cycle + CCS"=cOL_NGCC_CCS,"Natural Gas Combined Cycle CCS Retrofit"=cOL_NGCC_CCS,
"Hydrogen Simple Cycle"=cOL_SCGT_H2,
#"Hydrogen Combined Cycle"=cOL_NGCC_H2,
#"Blended Simple Cycle"=cOL_SCGT_Blend,"Blended Combined Cycle"=cOL_NGCC_Blend,
"Hydro"=cOL_HYDRO, "Other"=cOL_OTHER, "Wind"=cOL_WIND,
"Solar"=cOL_SOLAR, "Storage - Battery"=COL_Battery,
"Storage - Compressed Air"=COL_CompAir, "Storage - Pumped Hydro"=COL_Pumped,"Cogeneration"=cOL_COGEN)
Patterns8 = c("Coal"="none","Coal-to-Gas"="stripe",
"Natural Gas Combined Cycle"="none","Natural Gas Simple Cycle"="none",
"Natural Gas Combined Cycle + CCS"="none","Natural Gas Combined Cycle CCS Retrofit"="stripe",
"Hydrogen Simple Cycle"="none",
#"Hydrogen Combined Cycle"="none",
#"Blended Simple Cycle"=cOL_SCGT_Blend,"Blended Combined Cycle"=cOL_NGCC_Blend,
"Hydro"="none", "Other"="none", "Wind"="none",
"Solar"="none", "Storage - Battery"="none",
"Storage - Compressed Air"="none", "Storage - Pumped Hydro"="none","Cogeneration"="none")
AESO_colours <- c("goldenrod1", "gray60", "yellowgreen", "cornflowerblue",
"#001933")
}
# Gives years to summarize info from
Years2Disp <- c(2023,2025,2030,2035,2040,2045) # Years to show in figures
Years2Pivot <- c(2023,2025,2030,2035,2040,2045) # Years to display in tables
# Get max year to display
MaxYrStudy<-2045
#MaxYrStudy <-max(as.numeric(ResYr$Time_Period))-5
# Adjust capacity manually for 2025 (Manual add vs Aurora)
AESO_SUN_2025=242.5
AESO_WND_2025=400
AESO_PS_2025=29
AESO_COGEN_2025=25
AESO_PHS_2026=75
prj_names <- paste("EXOGENOUS CHANGES AFTER 2024:",
"New Solar",
"...P2292 FortisAlberta Killarney Lake 267S DER Solar (22.5 MW)",
"...P2347 Forty Mile Granlea Solar Phase 2 (220 MW)",
"New Wind",
"...P1885 Northern Lights WAGF (400 MW)",
"New Cogen",
"...P2354 ENMAX FMC DER Cogen (7 MW)",
"...P2192 ENMAX FMC Change (18 MW",
"New Storage",
"...P2292 FortisAlberta Killarney Lake 267S DER Storage (22.5 MW)",
"...P2334 TCE Saddlebrook Storage (6.5 MW)",
"...P1756 TPG Canyon Creek PHES Storage Project (75 MW)",
sep="\n")
#For fun, make the code beep when its all done
beep(3)
}
### CAN DELTE LATER. ##
# Plot all colors used
Legend_Plot_curt()
# Plot main colors used
Legend_PlotMain(0.7)
# The gray colors used
Legend_PlotGray(1)
# Create folder name to save as
# Casename is long description for figures/files
# NameShort is short name for later reference in r files
CaseName <- "Compare AESO"
NameShort<-'2022_2023'
################################################################################
## OUTPUT PLOTS AND DATA TO FOLDERS:
## Data: Data Files > Result Files
## Figures: Figures (Local)
################################################################################
GenText_Sz=60
GGSave_Loc_narrow(CaseName,"Price Duration Curve Avg only",Sim_dur_avg(BC),300)
GGSave_Loc_hourly(CaseName,"2030 4 month sum (Stacked Area + price)",FourMonthSummary(2030,01,04,07,10,BC),300)
GGSave_Loc_hourly(CaseName,"2045 4 month sum (Stacked Area + price)",FourMonthSummary(2045,01,04,07,10,BC),300)
# SAVE PLOTS AND FIGURES
# ANALYSIS
# Normal analysis
Analysis_saveall(CaseName)
# Detailed generation plots
Detail_Gen_save(CaseName)
# ADDITIONAL ANALYSIS
# Value plots
Value_saveall(CaseName)
# Slack plots
Slack_saveall(CaseName)
# CER ANALYSIS
CER_saveall(CaseName)
# Estimate curtailed energy
Ren_Curtail_Gen_save(CaseName)
# SAVE DATA
# WRITE TO EXCEL
# Annual data ('long name','short name',case)
AnnualDataExcel(CaseName,NameShort,BC)
# Hourly data ('long name','short name',case)
HourlyDataExcel(CaseName,NameShort,BC)
# GENERATE R FILES TO COMPARE LATER ('short name',case) - skip if this is not needed
AnnualDataR(NameShort,BC)
################################################################################
## COMMON INDIVIDUAL PLOT SAVING OPTIONS
################################################################################
GenText_Sz = 20 # Better for viewing individual plots :)
# HOURLY GENERATION
# Grid of weekly output - need to edit for more than one week of data
year_weeks(2043,BC,'g')
SaveRun_Loc(CaseName,"2023 Hourly Generation for One Week (Stacked Area)")
Week12(2045,01,08,BC,'g')
# Four months of generation and pool price
FourMonthSummary(2040,01,04,07,10,BC) ### Redo without trade included
SaveRun_Loc(CaseName,"2040 Output, Trade, Price")
# GENERATION
# Save all full size images
windows(14,10,buffered=FALSE)
# RUN TO CHECK CURTAIL OR SPECIFIC RESOURCE
# One week
week12_Curt(2043,08,08,BC)
SaveRun_Loc(CaseName,"Week 2043 Aug")
# One week each resource ("free_y", "fixed")
EachResWeek(2030,02,08,BC,"free_y")
SaveRun_Loc(CaseName,"Indv Week Oct 2040")
# capacity factors for 2 years
CFcompare(2023,2040,BC)
SaveRun_Loc(CaseName,"Capacity Factors 2022 and 2045")
# Yearly Output
Evalyr(BC,"g")
SaveRun_Loc(CaseName,"Annual Generation (Stacked Area)")
# Yearly Capacity
Evalcap(BC,"g")
SaveRun_Loc(CaseName,"Annual Capacity")
# Yearly percentage of generation
EvalPerc(BC,"n")
SaveRun_Loc(CaseName,"Annual Generation (Percent)")
# Bar chart showing each resource groups yearly output
Output_Comp(BC)
SaveRun_Loc(CaseName,"Annual Generation (Bar Chart)")
# Annual average capacity factors for all resource types
CF_Annual(BC)
SaveRun_Loc(CaseName,"Annual Capacity Factors")
# Wind duration curve with output as is
Wind_Dur(BC)
SaveRun_Loc(CaseName,"Wind Load Duration Curve")
# Wind duration curve with Output normalized
Wind_DurNorm(BC)
SaveRun_Loc(CaseName,"Wind Capacity Factor Duration Curve")
# Tell R the files are done and close window
dev.off()
# LTCE RESULTING BUILD/RETIRE
# New window
windows(14,6,buffered=FALSE)
# Retirements by capacity (grouped by fuel type)
RetireMW(BC)
SaveRun_Loc(CaseName,"Retirements")
# Capacity built by Aurora over study period
Build_A_MW(BC)
# All new capacity
BuildMW(BC)
SaveRun_Loc(CaseName,"Additions")
Build_Totals(BC)
# Tell R the files are done and close window
dev.off()
# New window
windows(14,10,buffered=FALSE)
# Difference in capacity
TotalCapChange(BC,'g')
SaveRun_Loc(CaseName,"Capacity Changes")
# Net difference in capacity
Eval_CapChange(BC,'g')
SaveRun_Loc(CaseName,"Net Capacity Changes")
# Combined cycle study fate by resource
CC_Fate_study(BC)
CC_Fate_year(BC)
SaveRun_Loc(CaseName,"Combined Cycle Gas Fate")
# PRICES AND COSTS
# Shows Prices for simulation duration
Sim_dur(BC)
SaveRun_Loc(CaseName,"Price Duration Curve")
# Shows production costs and fixed costs for full system
System_Cost(BC)
SaveRun_Loc(CaseName,"Total System Cost")
# Price Table
Report_P(Years2Pivot,BC)
# Average monthly prices over full period
AvgMn_price(BC)
SaveRun_Loc(CaseName,"Monthly Pool Prices")
# Average annual pool price
AvgYr_poolprice(BC)
SaveRun_Loc(CaseName,"Average Annual Pool Prices")
#Capture Prices
capture_p(2023,2030,BC)
SaveRun_Loc(CaseName,"Capture Prices")
# Relative capture prices
Relcapture_p(2023,2035,BC)
SaveRun_Loc(CaseName,"Relative Capture Prices")
# Premeium to pool price
ach_poolprem(BC)
SaveRun_Loc(CaseName,"Achived Premium to Pool Price")
# EMISSIONS
# Annual emissions in stacked area chart
AnnualEmStackCol(BC,"None")
SaveRun_Loc(CaseName,"Annual Emissions (Bar)")
# Annual emissions in individual lines
AnnualEmLine(BC,"ALL")
SaveRun_Loc(CaseName,"Annual Emissions (Line)")
# OTHER STUFF
# Annual import and export from AB as a bar chart
Imp_Exp1(BC)
SaveRun_Loc(CaseName,"Annual Imports and Exports")
# Import and export for full year from AB
Imp_Exp2(2043,BC)
# DAILY OUTPUT FUNCTIONS
CompDay_Season(2040,14,BC)
SaveRun_Loc(CaseName,"Daily Output - Season 2040")
CompDay_Wind(2040,BC)
SaveRun_Loc(CaseName,"Daily Output - Max Wind 2040")
CompDay_Solar(2040,BC)
SaveRun_Loc(CaseName,"Daily Output - Max Solar 2040")
CompDay_Years(2023,2043,11,10,BC)
SaveRun_Loc(CaseName,"Daily Output Nov- Years")
CompDay_AESO(2023,2,14,BC)
SaveRun_Loc(CaseName,"Daily Output Compared to AESO Feb Day")
# COMPARING TO AESO
# Compare wind duration curves between AESO from 2025 to max year of sim
AESO_Sim_WindDur(2024,BC)
SaveRun_Loc(CaseName,"Wind Load Duration Curve Compared to AESO")
# Compare wind duration curves between AESO from 2025 to max year of sim, normalize by capacity
AESO_Sim_WindDurNorm(2023,2029,1,BC)
SaveRun_Loc(CaseName,"Wind Capacity Factor Duration Curve Compared to AESO early")
# Capacity factor ridgeline
Resource_Ridge("LTO_Wind",2023,2045,5,BC)
Resource_Ridge("LTO_Solar",2023,2045,5,BC)
# Tell R the files are done and close window
dev.off()
################################################################################
## BUT THERE ARE MORE ... HERE ARE ALL THE AVAILABLE FUNCTIONS!
################################################################################
################################################################################
## Output and Generation Functions (Output_Gen_Functions)
################################################################################
# Gives stacked area chart for single week
Week1(2023,02,08,BC)
SaveRun_Loc(CaseName,"Feb 2035 Output")
#Gives stacked area chart for a single day, output (MWh vs Date), grouped by resource
Day1(2043,08,13,BC)
# Gives weekly storage function
Stor1(2035,01,08,BC)
# Gives weekly storage function with pool price
Stor2(2023,02,08,BC)
# Gives overall picture of Output over time period
Evalyr(BC,"n")
# Gives overall picture of capacity over time period
Evalcap(BC,"n")
# Gives all as % of total generation being met
EvalPerc(BC,"n")
# Capacity of resource groups for selected years as a bar chart
Output_Comp(BC)
# Just shows the average demand
AnnualDemand(ZoneYr,BC)
AnnualDemand(ZoneMn,BC)
# Shows the capacity factor for selected technologies in 2 different years (side by side bars)
CFcompare(2022,2035,BC)
# Average annaul capacity factors for all technolgoies as lines over study
CF_Annual(BC)
# Shows a year of week outputs corresponding to chosen year
year_weeks(2022,BC)
# Shows pool price over a week of resource group outputs
PrOt(2022,01,08,BC)
# Shows pool price over a week of resource group outputs, includes storage utilization
PrOut(2022,01,08,BC)
# Show capacity factor for individual resources included in CER
CF_CER_Res(BC)
# Show hours opperated for individual units included in CER
Hours_CER_Res(BC)
# Show capacity factor based on when CER applies by group
CF_CER_groups(BC)
# Wind duration curve with output as is
Wind_Dur(BC)
# Wind duration curve with Output normalized
Wind_DurNorm(BC)
# Max curtailment that occured
MaxCurtail(BC)
SaveRun_Loc(CaseName,"Max Curtailment")
# One year of weeks for storage output and pool price
year_stor(2035,BC)
SaveRun_Loc(CaseName,"2035 Storage Output with Pool Price")
# Four months of generation, intertie, and pool price