-
Notifications
You must be signed in to change notification settings - Fork 2
/
conversion_functions.py
1546 lines (1347 loc) · 56.9 KB
/
conversion_functions.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
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
"""
Functions to convert data from PowerGenome for use with Switch
"""
from statistics import mean, mode
from typing import List
import numpy as np
import pandas as pd
import scipy
import math
from powergenome.time_reduction import kmeans_time_clustering
km_per_mile = 1.60934
# convenience functions to get first/final keys/values from dicts
# (e.g., first year in a dictionary organized by years)
# note: these use the order of creation, not lexicographic order
def first_key(d: dict):
return next(iter(d.keys()))
def first_value(d: dict):
return next(iter(d.values()))
def final_key(d: dict):
return next(reversed(d.keys()))
def final_value(d: dict):
return next(reversed(d.values()))
def switch_fuel_cost_table(
aeo_fuel_region_map, fuel_prices, IPM_regions, scenario, year_list
):
"""
Create the fuel_cost input file based on REAM Scenario 178.
Inputs:
* aeo_fuel_region_map: has aeo_fuel_regions and the ipm regions within each aeo_fuel_region
* fuel_prices: output from PowerGenome gc.fuel_prices
* IPM_regions: from settings('model_regions')
* scenario: filtering the fuel_prices table. Suggest using 'reference' for now.
* year_list: the periods - 2020, 2030, 2040, 2050. To filter the fuel_prices year column
Output:
the fuel_cost_table
* load_zone: IPM region
* fuel: based on PowerGenome fuel_prices table
* period: based on year_list
* fuel_cost: based on fuel_prices.price
"""
ref_df = fuel_prices.copy()
ref_df = ref_df.loc[
ref_df["scenario"].isin(scenario)
] # use reference scenario for now
ref_df = ref_df[ref_df["year"].isin(year_list)]
ref_df = ref_df.drop(["full_fuel_name", "scenario"], axis=1)
# loop through aeo_fuel_regions.
# for each of the ipm regions in the aeo_fuel, duplicate the fuel_prices table while adding ipm column
fuel_cost = pd.DataFrame(columns=["year", "price", "fuel", "region", "load_zone"])
data = list()
for region in aeo_fuel_region_map.keys():
df = ref_df.copy()
# lookup all fuels available in this region or with no region specified
# (generally user-defined fuels added earlier)
df = df[df["region"].isin({region, ""})]
for ipm in aeo_fuel_region_map[region]:
ipm_region = ipm
df["load_zone"] = ipm_region
fuel_cost = fuel_cost.append(df)
# fuel_cost = fuel_cost.append(data)
fuel_cost.rename(columns={"year": "period", "price": "fuel_cost"}, inplace=True)
fuel_cost = fuel_cost[["load_zone", "fuel", "period", "fuel_cost"]]
fuel_cost["period"] = fuel_cost["period"].astype(int)
fuel_cost = fuel_cost[fuel_cost["load_zone"].isin(IPM_regions)]
fuel_cost["fuel"] = fuel_cost[
"fuel"
].str.capitalize() # align with energy_source in gen_pro_info? switch error.
return fuel_cost
def switch_fuels(fuel_prices, REAM_co2_intensity):
"""
Create fuels table using fuel_prices (from gc.fuel_prices) and basing other columns on REAM scenario 178
Output columns
* fuel: based on the fuels contained in the PowerGenome fuel_prices table
* co2_intensity: based on REAM scenario 178
* upstream_co2_intensity: based on REAM scenario 178
"""
fuels = pd.DataFrame(fuel_prices["fuel"].unique(), columns=["fuel"])
fuels["co2_intensity"] = fuels["fuel"].apply(lambda x: REAM_co2_intensity[x])
fuels["upstream_co2_intensity"] = 0 # based on REAM scenario 178
# switch error - capitalize to align with gen pro info energy_source?
fuels["fuel"] = fuels["fuel"].str.capitalize()
return fuels
def create_dict_plantgen(df, column):
"""
Create dictionary from two columns, removing na's beforehand
{plant_gen_id: year}
"""
df = df[df[column].notna()]
ids = df["plant_gen_id"].to_list()
dates = df[column].to_list()
dictionary = dict(zip(ids, dates))
return dictionary
def plant_gen_id(df):
"""
Create unique id for generator by combining plant_id_eia and generator_id
"""
plant_id_eia = df["plant_id_eia"]
df["plant_gen_id"] = plant_id_eia.astype(str) + "_" + df["generator_id"].astype(str)
return df
def gen_info_table(
all_gens,
spur_capex_mw_mile,
):
"""
Create the gen_info table
Inputs:
* gens: from PowerGenome gc.create_all_generators() with some extra data
* spur_capex_mw_mile: based on the settings file ('transmission_investment_cost')['spur']['capex_mw_mile']
* cogen_tech, baseload_tech, energy_tech, sched_outage_tech, forced_outage_tech
- these are user defined dictionaries. Will map values based on the technology
Output columns:
* GENERATION_PROJECT: basing on index
* gen_tech: based on technology
* gen_energy_source: based on energy_tech input
* gen_load_zone: IPM region
* gen_max_age: based on retirement_age
* gen_can_retire_early: based on Can_Retire and/or New_Build from PowerGenome
* gen_is_variable: only solar and wind are true
* gen_is_baseload: from PowerGenome
* gen_full_load_heat_rate: based on Heat_Rate_MMBTU_per_MWh from all_gen
* gen_variable_om: based on Var_OM_Cost_per_MWh_mean from all_gen
* gen_connect_cost_per_mw: based on spur_capex_mw_mile * spur_miles plus substation cost
* gen_dbid: same as generation_project
* gen_scheduled_outage_rate: from PowerGenome
* gen_forced_outage_rate: from PowerGenome
* gen_capacity_limit_mw: omitted for new thermal plants; upper limits on new renewables (MW total across all).
* gen_min_load_fraction: from PowerGenome
* gen_ramp_limit_up: from PowerGenome
* gen_ramp_limit_down: from PowerGenome
* gen_min_uptime: from PowerGenome
* gen_min_downtime: from PowerGenome
* gen_startup_om: from PowerGenome
* gen_is_cogen: from PowerGenome
* gen_storage_efficiency: from PowerGenome
* gen_store_to_release_ratio: batteries use 1
* gen_can_provide_cap_reserves: all 1s
* gen_self_discharge_rate, gen_storage_energy_to_power_ratio: blanks
"""
gen_info = all_gens.copy().reset_index(drop=True)
gen_info["GENERATION_PROJECT"] = gen_info["Resource"]
# TODO Change the upstream powergenome code to set up co2_pipeline_capex_mw as 0 when no access to ccs tech
# for now, modifyng the translation layer --RR
if "co2_pipeline_capex_mw" not in gen_info.columns:
gen_info["co2_pipeline_capex_mw"] = 0
# Include co2 pipeline costs as part of connection -- could also be in build capex
gen_info["gen_connect_cost_per_mw"] = gen_info[
["spur_capex", "interconnect_capex_mw", "co2_pipeline_capex_mw"]
].sum(axis=1)
# create gen_connect_cost_per_mw from spur_miles and spur_capex_mw_mile
gen_info["spur_capex_mw_mi"] = gen_info["region"].map(spur_capex_mw_mile)
gen_info["spur_miles"] = gen_info["spur_miles"].fillna(0)
gen_info.loc[
gen_info["gen_connect_cost_per_mw"] == 0, "gen_connect_cost_per_mw"
] = (gen_info["spur_capex_mw_mi"] * gen_info["spur_miles"])
gen_info = gen_info.drop(["spur_miles", "spur_capex_mw_mi"], axis=1)
# clean up gen_is_variable; usually only solar and wind technologies are true
gen_info["gen_is_variable"] = gen_info["gen_is_variable"].astype(bool)
# gen_storage_efficiency and gen_store_to_release_ratio (input vs output MW rating)
storage_gens = gen_info["STOR"] == 1
gen_info.loc[storage_gens, "gen_storage_efficiency"] = (
gen_info["Eff_Up"] * gen_info["Eff_Down"]
)
gen_info.loc[storage_gens, "gen_store_to_release_ratio"] = 1
gen_info["gen_can_provide_cap_reserves"] = 1
gen_info["gen_self_discharge_rate"] = None
gen_info["gen_storage_energy_to_power_ratio"] = None
gen_info["gen_dbid"] = gen_info["GENERATION_PROJECT"]
# get capacity limit if any, but ignore -1 (existing plants that won't
# show up as buildable in the future anyway)
gen_info["gen_capacity_limit_mw"] = gen_info["Max_Cap_MW"].replace({-1: None})
# fill in CCS capture rate, using NaN for non-CCS plants
gen_info["gen_ccs_capture_efficiency"] = gen_info["CO2_Capture_Rate"]
gen_info.loc[
gen_info["gen_ccs_capture_efficiency"] == 0, "gen_ccs_capture_efficiency"
] = None
# identify generators that can retire early
try:
# settings for newer GenX (not yet implemented as of Aug. 2024)
gen_info["gen_can_retire_early"] = gen_info["Can_Retire"].astype("Int64")
except KeyError:
# settings for older GenX
# New_Build == -1 -> existing, cannot retire
# New_Build == 0 -> existing, can retire
# New_Build >= 1 -> new build, can retire in current version of GenX
gen_info["gen_can_retire_early"] = (gen_info["New_Build"] >= 0).astype("Int64")
# rename columns
gen_info.rename(
columns={
"technology": "gen_tech",
"region": "gen_load_zone",
"retirement_age": "gen_max_age",
"Heat_Rate_MMBTU_per_MWh": "gen_full_load_heat_rate",
"Var_OM_Cost_per_MWh_mean": "gen_variable_om",
# gen_amortization_period is optional and often not needed (Switch
# will use gen_max_age by default). But we always report it in case
# the settings use a longer retirement_age (or none) to prevent
# age-based retirement.
"cap_recovery_years": "gen_amortization_period",
"Min_Power": "gen_min_load_fraction",
"Ramp_Up_Percentage": "gen_ramp_limit_up",
"Ramp_Dn_Percentage": "gen_ramp_limit_down",
"Up_Time": "gen_min_uptime",
"Down_Time": "gen_min_downtime",
"Start_Cost_per_MW": "gen_startup_om",
},
inplace=True,
)
# use zero instead of NaN for some columns (other NaNs will get converted
# to "." for Switch when saving later)
gen_info["gen_variable_om"] = gen_info["gen_variable_om"].fillna(0)
gen_info["gen_connect_cost_per_mw"] = gen_info["gen_connect_cost_per_mw"].fillna(0)
cols = [
"GENERATION_PROJECT",
"gen_tech",
"gen_energy_source",
"gen_load_zone",
"gen_max_age",
"gen_amortization_period",
"gen_can_retire_early",
"gen_is_variable",
"gen_is_baseload",
"gen_full_load_heat_rate",
"gen_variable_om",
"gen_connect_cost_per_mw",
"gen_dbid",
"gen_scheduled_outage_rate",
"gen_forced_outage_rate",
"gen_capacity_limit_mw",
"gen_min_load_fraction",
"gen_ramp_limit_up",
"gen_ramp_limit_down",
"gen_min_uptime",
"gen_min_downtime",
"gen_startup_om",
"gen_is_cogen",
"gen_storage_efficiency",
"gen_store_to_release_ratio",
"gen_can_provide_cap_reserves",
"gen_self_discharge_rate",
"gen_ccs_capture_efficiency",
"gen_ccs_energy_load",
"gen_storage_energy_to_power_ratio",
"gen_type",
"ESR_1", # Below are the columns used for current policy
"ESR_2",
"ESR_3",
"ESR_4",
"ESR_5",
"ESR_6",
"ESR_7",
"ESR_8",
"ESR_9",
"ESR_10",
"ESR_11",
"ESR_12",
"ESR_13",
"ESR_14",
"ESR_15",
"ESR_16",
"MinCapTag_1",
"MinCapTag_2",
"MinCapTag_3",
"MinCapTag_4",
"MinCapTag_5",
] # index
gen_info = gen_info[cols]
return gen_info
hydro_forced_outage_tech = {
# "conventional_hydroelectric": 0.05,
# "hydroelectric_pumped_storage": 0.05,
# "small_hydroelectric": 0.05,
"conventional_hydroelectric": 0,
"hydroelectric_pumped_storage": 0,
"small_hydroelectric": 0,
}
def match_hydro_forced_outage_tech(x):
for key in hydro_forced_outage_tech:
if key in x:
return hydro_forced_outage_tech[key]
def fuel_market_tables(fuel_prices, aeo_fuel_region_map, scenario):
"""
Create regional_fuel_markets and zone_to_regional_fuel_market
SWITCH does not seem to like this overlapping with fuel_cost. So all of this might be incorrect.
"""
# create initial regional fuel market. Format: region - fuel
reg_fuel_mar_1 = fuel_prices.copy()
reg_fuel_mar_1 = reg_fuel_mar_1.loc[
reg_fuel_mar_1["scenario"] == scenario
] # use reference for now
reg_fuel_mar_1 = reg_fuel_mar_1.drop(
["year", "price", "full_fuel_name", "scenario"], axis=1
)
reg_fuel_mar_1 = reg_fuel_mar_1.rename(columns={"region": "regional_fuel_market"})
reg_fuel_mar_1 = reg_fuel_mar_1[["regional_fuel_market", "fuel"]]
fuel_markets = reg_fuel_mar_1["regional_fuel_market"].unique()
# from region to fuel
group = reg_fuel_mar_1.groupby("regional_fuel_market")
fuel_market_dict = {}
for region in fuel_markets:
df = group.get_group(region)
fuel = df["fuel"].to_list()
fuel = list(set(fuel))
fuel_market_dict[region] = fuel
# create zone_regional_fuel_market
data = list()
for region in aeo_fuel_region_map.keys():
for i in range(len(aeo_fuel_region_map[region])):
ipm = aeo_fuel_region_map[region][i]
for fuel in fuel_market_dict[region]:
data.append([ipm, ipm + "-" + fuel])
zone_regional_fm = pd.DataFrame(data, columns=["load_zone", "regional_fuel_market"])
# use that to finish regional_fuel_markets
regional_fuel_markets = zone_regional_fm.copy()
regional_fuel_markets["fuel_list"] = regional_fuel_markets[
"regional_fuel_market"
].str.split("-")
regional_fuel_markets["fuel"] = regional_fuel_markets["fuel_list"].apply(
lambda x: x[-1]
)
regional_fuel_markets = regional_fuel_markets[["regional_fuel_market", "fuel"]]
return regional_fuel_markets, zone_regional_fm
def ts_tp_pg_kmeans(
representative_point: pd.DataFrame,
point_weights: List[int],
days_per_period: int,
planning_year: int,
planning_start_year: int,
):
"""Create timeseries and timepoints tables when using kmeans time reduction in PG
Parameters
----------
representative_point : pd.DataFrame
The representative periods used. Single column dataframe with col name "slot"
point_weights : List[int]
The weight assigned to each period. Equal to the number of periods in the year
that each period represents.
days_per_period : int
How long each period lasts in days
planning_periods : List[int]
A list of the planning years
planning_period_start_years : List[int]
A list of the start year for each planning period, used to calculate the number
of years in each period
Returns
-------
pd.DataFrame, pd.DataFrame
A tuple of the timeseries and timepoints dataframes
"""
ts_data = {
"timeseries": [],
"ts_period": [],
"ts_duration_of_tp": [],
"ts_num_tps": [],
"ts_scale_to_period": [],
}
tp_data = {
"timestamp": [],
"timeseries": [],
}
planning_yrs = planning_year - planning_start_year + 1
for p, weight in zip(representative_point, point_weights):
num_hours = days_per_period * 24
ts = f"{planning_year}_{p}"
ts_data["timeseries"].append(ts)
ts_data["ts_period"].append(planning_year)
ts_data["ts_duration_of_tp"].append(1)
ts_data["ts_num_tps"].append(num_hours)
ts_data["ts_scale_to_period"].append(weight * planning_yrs)
tp_data["timestamp"].extend([f"{ts}_{i}" for i in range(num_hours)])
tp_data["timeseries"].extend([ts for i in range(num_hours)])
timeseries = pd.DataFrame(ts_data)
timepoints = pd.DataFrame(tp_data)
timepoints["timepoint_id"] = timepoints.index + 1
timepoints = timepoints[["timepoint_id", "timestamp", "timeseries"]]
return timeseries, timepoints
def hydro_timepoints_pg_kmeans(timepoints: pd.DataFrame) -> pd.DataFrame:
"""Create the timepoints table when using kmeans time reduction in PG
This assumes that the hydro timeseries are identical to the model timeseries.
Parameters
----------
timepoints : pd.DataFrame
The timepoints table
Returns
-------
pd.DataFrame
Identical to the incoming timepoints table except "timepoint_id" is renamed to
"tp_to_hts"
"""
hydro_timepoints = timepoints.copy()
hydro_timepoints = hydro_timepoints.rename(columns={"timeseries": "tp_to_hts"})
return hydro_timepoints[["timepoint_id", "tp_to_hts"]]
def hydro_timeseries_pg_kmeans(
gen: pd.DataFrame,
hydro_variability: pd.DataFrame,
hydro_timepoints: pd.DataFrame,
outage_rate: float = 0,
) -> pd.DataFrame:
"""Create hydro timeseries table when using kmeans time reduction in PG
The hydro timeseries table has columns hydro_project, timeseries, outage_rate,
hydro_min_flow_mw, and hydro_avg_flow_mw. The "timeseries" column links to the
column "tp_to_hts" in hydro_timepoints.csv. "hydro_min_flow_mw" uses the resource
minimum capacity (calculated in PG from EIA860). "hydro_avg_flow_mw" is the average
of flow during each timeseries.
Parameters
----------
existing_gen : pd.DataFrame
All existing generators, one row per generator. Columns must include "Resource",
"Existing_Cap_MW", "Min_Power", and "HYDRO".
hydro_variability : pd.DataFrame
Hourly flow/generation capacity factors. Should have column names that correspond
to the "Resource" column in `existing_gen`. Additional column names will be
filtered out.
hydro_timepoints : pd.DataFrame
All timepoints for hydro, with the column "tp_to_hts"
outage_rate : float, optional
The average outage rate for hydro generators, by default 0.05
Returns
-------
pd.DataFrame
The hydro_timeseries table for Switch
"""
hydro_df = gen.copy()
# ? why multiply Min_Power
# hydro_df["min_cap_mw"] = hydro_df["Existing_Cap_MW"] * hydro_df["Min_Power"]
hydro_df = hydro_df.loc[hydro_df["HYDRO"] == 1, :]
hydro_variability = hydro_variability.loc[:, hydro_df["Resource"]]
# for col in hydro_variability.columns:
# hydro_variability[col] *= hydro_df.loc[
# hydro_df["Resource"] == col, "Existing_Cap_MW"
# ].values[0]
hydro_variability["timeseries"] = hydro_timepoints["tp_to_hts"].values
hydro_ts = hydro_variability.melt(id_vars=["timeseries"])
hydro_ts = hydro_ts.groupby(["timeseries", "Resource"], as_index=False).agg(
hydro_avg_flow_mw=("value", "mean"), hydro_min_flow_mw=("value", "min")
)
# hydro_ts["hydro_min_flow_mw"] = hydro_ts["Resource"].map(
# hydro_df.set_index("Resource")["Min_Power"]
# )
hydro_ts["hydro_avg_flow_mw"] = hydro_ts["hydro_avg_flow_mw"] * hydro_ts[
"Resource"
].map(hydro_df.set_index("Resource")["Existing_Cap_MW"])
hydro_ts["hydro_min_flow_mw"] = hydro_ts["hydro_min_flow_mw"] * hydro_ts[
"Resource"
].map(hydro_df.set_index("Resource")["Existing_Cap_MW"])
hydro_ts["outage_rate"] = outage_rate
hydro_ts = hydro_ts.rename(columns={"Resource": "hydro_project"})
cols = [
"hydro_project",
"timeseries",
# "outage_rate",
"hydro_min_flow_mw",
"hydro_avg_flow_mw",
]
return hydro_ts[cols]
def variable_cf_pg_kmeans(
all_gens: pd.DataFrame, all_gen_variability: pd.DataFrame, timepoints: pd.DataFrame
) -> pd.DataFrame:
"""Create the variable capacity factors table when using kmeans time reduction in PG
Variable generators are identified as those with hourly average capacity factors
less than 1.
Parameters
----------
all_gens : pd.DataFrame
All resources. Must have the columns "Resource" and "gen_is_variable".
all_gen_variability : pd.DataFrame
Wide dataframe with hourly capacity factors of all generators.
timepoints : pd.DataFrame
Timepoints table with column "timepoint_id"
Returns
-------
pd.DataFrame
Tidy dataframe with columns "GENERATION_PROJECT", "timepoint", and
"gen_max_capacity_factor"
"""
vre_gens = all_gens.loc[all_gens["gen_is_variable"] == 1, "Resource"]
vre_variability = all_gen_variability[vre_gens]
vre_variability["timepoint_id"] = timepoints["timepoint_id"].values
vre_ts = vre_variability.melt(
id_vars=["timepoint_id"], value_name="gen_max_capacity_factor"
)
vre_ts = vre_ts.rename(
columns={"Resource": "GENERATION_PROJECT", "timepoint_id": "timepoint"}
)
return vre_ts.reindex(
columns=["GENERATION_PROJECT", "timepoint", "gen_max_capacity_factor"]
)
def load_pg_kmeans(load_curves: pd.DataFrame, timepoints: pd.DataFrame) -> pd.DataFrame:
"""Create the loads table when using kmeans time reduction in PG
Parameters
----------
load_curves : pd.DataFrame
Wide dataframe with one column of demand values for each zone
timepoints : pd.DataFrame
Timepoints table with column "timepoint_id"
Returns
-------
pd.DataFrame
Tidy dataframe with columns "LOAD_ZONE" and "TIMEPOINT"
"""
load_curves = load_curves.astype(int)
load_curves["TIMEPOINT"] = timepoints["timepoint_id"].values
load_ts = load_curves.melt(id_vars=["TIMEPOINT"], value_name="zone_demand_mw")
load_ts = load_ts.rename(columns={"region": "LOAD_ZONE"})
load_ts["zone_demand_mw"] = load_ts["zone_demand_mw"].astype("object")
# change the order of the columns
return load_ts.reindex(columns=["LOAD_ZONE", "TIMEPOINT", "zone_demand_mw"])
def graph_timestamp_map_kmeans(timepoints_df):
"""
Create the graph_timestamp_map table based on REAM Scenario 178
Input:
timeseries_df, timepoints_df: the SWITCH timeseries table
Output columns:
* timestamp: dates based on the timeseries table
* time_row: the period decade year based on the timestamp
* time_column: format: yyyymmdd. Using 2012 because that is the year data is based on.
"""
timepoints_df_copy = timepoints_df.copy()
graph_timeseries_map = pd.DataFrame(columns=["timestamp", "time_row", "timeseries"])
graph_timeseries_map["timestamp"] = timepoints_df_copy["timestamp"]
graph_timeseries_map["timeseries"] = timepoints_df_copy["timeseries"]
graph_timeseries_map["time_row"] = [
x[0] for x in graph_timeseries_map["timestamp"].str.split("_")
]
# using 2012 for financial year
graph_timeseries_map["time_column"] = graph_timeseries_map["timeseries"].apply(
lambda x: str(2012) + x[5:]
)
return graph_timeseries_map
def timeseries(
load_curves,
planning_year,
planning_start_year,
settings,
): # 20.2778, 283.8889
"""
Create the timeseries table based on REAM Scenario 178
Input:
1) load_curves: created using PowerGenome make(final_load_curves(pg_engine, scenario_settings[][]))
2) max_weight: the weight to apply to the days with highest values
3) avg_weight: the weight to apply to the days with average value
3) ts_duration_of_tp: how many hours should the timpoint last
4) ts_num_tps: number of timpoints in the selected day
Output columns:
- TIMESERIES: format: yyyy_yyyy-mm-dd
- ts_period: the period decade
- ts_duration_of_tp: based on input value
- ts_num_tps: based on input value. Should multiply to 24 with ts_duration_of_tp
- ts_scale_to_period: use the max&avg_weights for the average and max days in a month
"""
if settings.get("sample_dates_fn") and settings.get("input_folder"):
sample_dates = pd.read_csv(
settings.get("input_folder") / settings["sample_dates_fn"]
)
else:
sample_year = planning_year
sample_year_start = str(sample_year) + "0101"
sample_year_end = str(sample_year) + "1231"
sample_dates = [
d.strftime("%Y%m%d")
for d in pd.date_range(sample_year_start, sample_year_end)
]
leap_yr = str(sample_year) + "0229"
if leap_yr in sample_dates:
sample_dates.remove(leap_yr) ### why remove Feb 29th? --RR
hr_load_sum = pd.DataFrame(load_curves.sum(axis=1), columns=["sum_across_regions"])
load_hrs = len(load_curves.index) # number of hours PG outputs data for in a year
baseyear_hours = len(sample_dates) * 24
hr_interval = round(load_hrs / baseyear_hours)
# hr_int_list = list(range(1, int(24 / hr_interval) + 1))
hr_interval_load_sum = hr_load_sum.groupby(hr_load_sum.index // hr_interval).sum()
# create initial date list for 2020
timestamp = list()
for d in range(len(sample_dates)):
for i in range(1, 25):
date_hr = sample_dates[d]
timestamp.append(date_hr)
timeseries = [x[:4] + "_" + x[:4] + "-" + x[4:6] + "-" + x[6:8] for x in timestamp]
ts_period = [x[:4] for x in timestamp]
timepoint_id = list(range(len(timestamp)))
column_list = ["timeseries", "ts_period"]
data = np.array([timeseries, ts_period]).T
initial_df = pd.DataFrame(
data, columns=column_list, index=hr_interval_load_sum.index
)
initial_df = initial_df.join(hr_interval_load_sum)
if settings.get("chunk_days"):
chunk_days = settings.get("chunk_days")
# split dataframe into chunks of representative_days
chunk_hr = chunk_days * 24
n_chunks = len(sample_dates) // chunk_days
num_days = chunk_days * n_chunks
chunk_df = []
for i in range(n_chunks):
ck_df = (
(initial_df.iloc[i * chunk_hr : (i + 1) * chunk_hr, :])
.groupby("timeseries")
.sum()
)
chunk_df.append(ck_df)
else:
chunk_days = 8760 / (12 * 24)
month_hrs = [744, 672, 744, 720, 744, 720, 744, 744, 720, 744, 720, 744]
year_cumul = [
744,
1416,
2160,
2880,
3624,
4344,
5088,
5832,
6552,
7296,
8016,
8760,
] # cumulative hours by month
num_days = sum(month_hrs) / 24
# split dataframe into months (grouped by day)
chunk_df = []
chunk_df.append(
(initial_df.iloc[0 : year_cumul[0], :]).groupby("timeseries").sum()
)
for i in range(len(year_cumul) - 1):
M_df = (
(initial_df.iloc[year_cumul[i] : year_cumul[i + 1], :])
.groupby("timeseries")
.sum()
)
chunk_df.append(M_df)
# find mean and max for each month, add date to a dataframe
timeseries_df = pd.DataFrame(
columns=["sum_across_regions", "timeseries", "close_to_mean"]
)
for df in chunk_df:
df["timeseries"] = df.index
mean = df["sum_across_regions"].mean()
df["close_to_mean"] = abs(df["sum_across_regions"] - mean)
df_mean = df.loc[df["close_to_mean"] == df["close_to_mean"].min()]
df_max = df.loc[df["sum_across_regions"] == df["sum_across_regions"].max()]
timeseries_df = timeseries_df.append(df_max)
timeseries_df = timeseries_df.append(df_mean)
timeseries_df["timeseries"] = timeseries_df.index
# add in other columns
timeseries_df["ts_period"] = str(sample_year)
ts_duration_of_tp = settings.get("ts_duration_of_tp", 1)
ts_num_tps = settings.get("ts_num_tps", 24 / ts_duration_of_tp)
timeseries_df["ts_duration_of_tp"] = ts_duration_of_tp # assuming 4 for now
timeseries_df["ts_num_tps"] = ts_num_tps # assuming 6 for now
timeseries_df = timeseries_df.reset_index(drop=True)
timeseries_df = timeseries_df.drop(["sum_across_regions"], axis=1)
timeseries_df["ts_scale_to_period"] = None
planning_yrs = planning_year - planning_start_year + 1
max_days = settings.get("max_days")
sample_to_year_ratio = 8760 / (num_days * 24)
max_weight = round(planning_yrs * max_days * sample_to_year_ratio, 4)
avg_weight = round(planning_yrs * (chunk_days - max_days) * sample_to_year_ratio, 4)
for i in range(len(timeseries_df)):
if i % 2 == 0:
timeseries_df.loc[i, "ts_scale_to_period"] = max_weight
timeseries_df["ts_scale_to_period"].replace(
to_replace=[None], value=avg_weight, inplace=True
)
timeseries_df = timeseries_df[
[
"timeseries",
"ts_period",
"ts_duration_of_tp",
"ts_num_tps",
"ts_scale_to_period",
]
]
timeseries_dates = timeseries_df["timeseries"].to_list()
timestamp_interval = list()
for i in range(ts_num_tps):
s_interval = ts_duration_of_tp * i
stamp_interval = str(f"{s_interval:02d}")
timestamp_interval.append(stamp_interval)
timepoint_id = list(range(1, len(timeseries_dates) + 1))
timestamp = [x[:4] + x[10:12] + x[13:] for x in timeseries_dates]
column_list = ["timepoint_id", "timestamp", "timeseries"]
timepoints_df = pd.DataFrame(columns=column_list)
for i in timestamp_interval:
timestamp_interval = [x + i for x in timestamp]
df_data = np.array([timepoint_id, timestamp_interval, timeseries_dates]).T
df = pd.DataFrame(df_data, columns=column_list)
timepoints_df = timepoints_df.append(df)
timepoints_df["timepoint_id"] = range(
1, len(timepoints_df["timeseries"].to_list()) + 1
)
return timeseries_df, timepoints_df, timestamp_interval
def timeseries_full(
load_curves,
planning_year,
planning_start_year,
settings,
): # 20.2778, 283.8889
"""Create timeseries and timepoints tables when using yearly data with 8760 hours
Apply this function reduce_time_domain: False & full_time_domain: True in settings
Parameters
----------
planning_periods : List[int]
A list of the planning years
planning_period_start_years : List[int]
A list of the start year for each planning period, used to calculate the number
of years in each period
Returns
-------
pd.DataFrame, pd.DataFrame
A tuple of the timeseries and timepoints dataframes
"""
if settings.get("sample_dates_fn") and settings.get("input_folder"):
sample_dates = pd.read_csv(
settings.get("input_folder") / settings["sample_dates_fn"]
)
else:
sample_year = planning_year
sample_year_start = str(sample_year) + "0101"
sample_year_end = str(sample_year) + "1231"
sample_dates = [
d.strftime("%Y%m%d")
for d in pd.date_range(sample_year_start, sample_year_end)
]
leap_yr = str(sample_year) + "0229"
if leap_yr in sample_dates:
sample_dates.remove(leap_yr) ### why remove Feb 29th? --RR
num_days = len(sample_dates)
num_hours = 24 * num_days
sample_to_year_ratio = 8760 / (num_days * 24)
planning_yrs = planning_year - planning_start_year + 1
timeseries_df = pd.DataFrame()
ts = f"{sample_year}_{sample_year}-full"
timeseries_df["timeseries"] = [ts]
timeseries_df["ts_period"] = [f"{sample_year}"]
timeseries_df["ts_duration_of_tp"] = [1] # each hour as one timepoint
timeseries_df["ts_num_tps"] = [num_hours]
timeseries_df["ts_scale_to_period"] = [planning_yrs * sample_to_year_ratio]
timestamp_interval = list()
for i in range(24):
s_interval = i
stamp_interval = str(f"{s_interval:02d}")
timestamp_interval.append(stamp_interval)
timepoints_df = pd.DataFrame()
timepoints_df["timeseries"] = [ts for i in range(num_hours)]
timepoints_df["timepoint_id"] = range(
1, len(timepoints_df["timeseries"].to_list()) + 1
)
timepoints_df["timestamp"] = [
f"{d}{i}" for d in sample_dates for i in timestamp_interval
]
timepoints_df = timepoints_df[["timepoint_id", "timestamp", "timeseries"]]
return timeseries_df, timepoints_df, timestamp_interval
def hydro_time_tables(existing_gen, hydro_variability, timepoints_df, planning_year):
"""
Create the hydro_timepoints table based on REAM Scenario 178
Inputs:
1) timepoints_df: the SWITCH timepoints table
Output Columns
* timepoint_id: from the timepoints table
* tp_to_hts: format: yyyy_M#. Based on the timestamp date from the timepoints table
"""
hydro_timepoints = timepoints_df.copy()
hydro_timepoints = hydro_timepoints.rename(columns={"timeseries": "tp_to_hts"})
convert_to_hts = {
"01": "_M1",
"02": "_M2",
"03": "_M3",
"04": "_M4",
"05": "_M5",
"06": "_M6",
"07": "_M7",
"08": "_M8",
"09": "_M9",
"10": "_M10",
"11": "_M11",
"12": "_M12",
}
def convert(tstamp):
month = tstamp[4:6]
year = tstamp[0:4]
return year + convert_to_hts[month]
hydro_timepoints["tp_to_hts"] = hydro_timepoints["timestamp"].apply(convert)
hydro_timepoints.drop("timestamp", axis=1, inplace=True)
hydro_list = [
"Conventional Hydroelectric",
# "Hydroelectric Pumped Storage",
"Small Hydroelectric",
]
#### edit by RR
# filter existing gen to just hydro technologies
hydro_df = existing_gen.copy()
# hydro_df["index"] = hydro_df.index
hydro_df = hydro_df[hydro_df["technology"].isin(hydro_list)]
hydro_indx = hydro_df["Resource"].to_list()
hydro_region = hydro_df["region"].to_list()
# slice the hours to 8760
hydro_variability = hydro_variability.iloc[:8760]
hydro_variability = hydro_variability.loc[:, hydro_indx]
hydro_variability.columns = hydro_indx
####
# get cap size for each hydro tech
hydro_Cap_Size = hydro_df["Existing_Cap_MW"].to_list() # cap size for each hydro
# multiply cap size by hourly
for i in range(len(hydro_Cap_Size)):
hydro_variability.iloc[:, i] = hydro_variability.iloc[:, i].apply(
lambda x: x * hydro_Cap_Size[i]
)
hydro_transpose = hydro_variability.transpose()
month_hrs = [744, 672, 744, 720, 744, 720, 744, 744, 720, 744, 720, 744]
year_cumul = [
744,
1416,
2160,
2880,
3624,
4344,
5088,
5832,
6552,
7296,
8016,
8760,
] # cumulative hours by month
# split dataframe into months
month_df = []
month_df.append((hydro_transpose.iloc[:, 0 : year_cumul[0]]))
for i in range(len(year_cumul) - 1):
M_df = hydro_transpose.iloc[:, year_cumul[i] : year_cumul[i + 1]]
month_df.append(M_df)
month_names = [
"M1",
"M2",
"M3",
"M4",
"M5",
"M6",
"M7",
"M8",
"M9",
"M10",
"M11",
"M12",
]
df_list = list()
for i in range(len(month_df)):
df = pd.DataFrame(hydro_transpose.index, columns=["hydro_project"])
df["timeseries"] = month_names[i]
df["outage_rate"] = list(
map(match_hydro_forced_outage_tech, hydro_df["Resource"])
)
df["hydro_min_flow_mw"] = month_df[i].min(axis=1).to_list()
df["hydro_avg_flow_mw"] = month_df[i].mean(axis=1).to_list()
df_list.append(df)
hydro_timeseries = pd.concat(df_list, axis=0)
hydro_timeseries["timeseries"] = (
str(planning_year) + "_" + hydro_timeseries["timeseries"]
)
return hydro_timepoints, hydro_timeseries
def hydro_system_module_tables(
gen,
hydro_variability: pd.DataFrame,
hydro_timepoints: pd.DataFrame,
flow_per_mw: float = 1.02,