Skip to content

Commit

Permalink
Refactor the code
Browse files Browse the repository at this point in the history
  • Loading branch information
bilgeacun committed Feb 11, 2022
1 parent a53b327 commit 4909d5f
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 195 deletions.
255 changes: 61 additions & 194 deletions Carbon_Explorer.ipynb

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion EIA_Energy_Data_Analysis.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
"showInput": false
},
"source": [
"`Copyright (c) Meta Platforms, Inc. and affiliates. This source code is licensed under the CC-BY-NC license found in the LICENSE file in the root directory of this source tree.`\n",
"\n",
"## This notebook downloads and analyzes EIA's bulk U.S. Electric System Operating Data \n",
"### [https://www.eia.gov/opendata/bulkfiles.php]\n"
"For description of the data, see: https://www.eia.gov/opendata/bulkfiles.php\n"
]
},
{
Expand Down
80 changes: 80 additions & 0 deletions src/cas.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
# This source code is licensed under the CC-BY-NC license found in the
# LICENSE file in the root directory of this source tree.

import pandas as pd

# Carbon Aware Scheduling Algorithm
# takes a dataframe that contains renewable and dc power, dc_all
# applies cas within the flexible_workload_ratio, and max_capacity constraints
# returns the carbon balanced version of the input dataframe, balanced_df
def cas(df_all, flexible_workload_ratio, max_capacity):
# work on 24 hour basis
# sort the df in terms of ascending renewable en
# take flexible_workload_ratio from the highest carbon intensity hours
# to lowest ones if there is not enough renewables until max_capacity is hit
balanced_df = []
for i in range(0, df_all.shape[0], 24):
sorted_df = df_all[i : i + 24].sort_values(
by=["tot_renewable", "avg_dc_power_mw"]
)
start = 0
end = 23
if sorted_df.shape[0] < 23:
break
work_to_move = 0
while start < end:
renewable_surplus = (
sorted_df["tot_renewable"].iloc[end]
- sorted_df["avg_dc_power_mw"].iloc[end]
)
renewable_gap = (
sorted_df["avg_dc_power_mw"].iloc[start]
- sorted_df["tot_renewable"].iloc[start]
)
available_space = min(
renewable_surplus,
(max_capacity - sorted_df["avg_dc_power_mw"].iloc[end]),
)
if renewable_surplus <= 0:
end = end - 1
continue
if renewable_gap <= 0:
start = start + 1
continue
if work_to_move <= 0 and renewable_gap > 0:
work_to_move = min(
renewable_gap,
(
flexible_workload_ratio
/ 100
* sorted_df["avg_dc_power_mw"].iloc[start]
),
)

if available_space > work_to_move:
sorted_df["avg_dc_power_mw"].iloc[end] = (
sorted_df["avg_dc_power_mw"].iloc[end]
+ work_to_move
)
sorted_df["avg_dc_power_mw"].iloc[start] = (
sorted_df["avg_dc_power_mw"].iloc[start]
- work_to_move
)
start = start + 1
work_to_move = 0
else:
sorted_df["avg_dc_power_mw"].iloc[end] = (
sorted_df["avg_dc_power_mw"].iloc[end]
+ available_space
)
sorted_df["avg_dc_power_mw"].iloc[start] = (
sorted_df["avg_dc_power_mw"].iloc[start]
- available_space
)
work_to_move = work_to_move - available_space
end = end - 1
balanced_df.append(sorted_df)

final_balanced_df = pd.concat(balanced_df).sort_values(by=["index"])
return final_balanced_df
4 changes: 4 additions & 0 deletions src/download_and_process.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
# This source code is licensed under the CC-BY-NC license found in the
# LICENSE file in the root directory of this source tree.

import wget
import zipfile
import pandas as pd
Expand Down

0 comments on commit 4909d5f

Please sign in to comment.