Skip to content

Commit fa204e6

Browse files
committed
Initial proof of concept to plot plan
Makes use of our 'output' and the 'forecast' to plot a timeseries of the CI alongside highlights for the CI of running now or running at the optimal time.
1 parent 81d44d0 commit fa204e6

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

cats/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from .forecast import CarbonIntensityAverageEstimate
1616
from .optimise_starttime import get_avg_estimates # noqa: F401
1717
from .constants import CATS_ASCII_BANNER_COLOUR, CATS_ASCII_BANNER_NO_COLOUR
18+
from .plotting import plotplan
1819

1920
__version__ = "1.0.0"
2021

@@ -193,6 +194,11 @@ def positive_integer(string):
193194
action="store_true",
194195
help="Disable all terminal output colouring (alias to --no-colour)",
195196
)
197+
parser.add_argument(
198+
"--plot",
199+
help="Create a plot of the forcast and optimised plan for the job",
200+
action="store_true",
201+
)
196202

197203
return parser
198204

@@ -360,6 +366,8 @@ def main(arguments=None) -> int:
360366
print(output.to_json(dateformat, sort_keys=True, indent=2))
361367
else:
362368
print(output)
369+
if args.plot:
370+
plotplan(CI_forecast, output)
363371
if args.command and args.scheduler == "at":
364372
if err := schedule_at(output, args.command.split()):
365373
print(err)

cats/plotting.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
4+
def plotplan(CI_forecast, output):
5+
"""
6+
Plot the carbon intensity forcast and optimised plan
7+
"""
8+
# Just for now pull the CI forecast apart... probably belongs as method...
9+
values = []
10+
times = []
11+
now_values = []
12+
now_times = []
13+
opt_values = []
14+
opt_times = []
15+
for point in CI_forecast:
16+
values.append(point.value)
17+
times.append(point.datetime)
18+
if (point.datetime >= output.carbonIntensityOptimal.start and
19+
point.datetime <= output.carbonIntensityOptimal.end):
20+
opt_values.append(point.value)
21+
opt_times.append(point.datetime)
22+
if (point.datetime >= output.carbonIntensityNow.start and
23+
point.datetime <= output.carbonIntensityNow.end):
24+
now_values.append(point.value)
25+
now_times.append(point.datetime)
26+
27+
# Make the plot (should probably take fig and ax as opt args...)
28+
fig, ax = plt.subplots()
29+
ax.fill_between(times, 0.0, values, alpha=0.2, color='b')
30+
ax.fill_between(now_times, 0.0, now_values, alpha=0.6, color='r')
31+
ax.fill_between(opt_times, 0.0, opt_values, alpha=0.6, color='g')
32+
ax.set_xlabel("Time (mm-dd hh)")
33+
ax.set_ylabel("Forecast carbon intensity (gCO2eq/kWh)")
34+
ax.grid(True)
35+
ax.label_outer()
36+
fig.autofmt_xdate()
37+
plt.show()
38+
return None

0 commit comments

Comments
 (0)