-
Notifications
You must be signed in to change notification settings - Fork 14
/
replay
executable file
·107 lines (91 loc) · 3.41 KB
/
replay
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
#!/usr/bin/env python3
#
# Copyright (C) 2012, 2013, 2014 Ben Elliston
# Copyright (C) 2014, 2015 The University of New South Wales
#
# This file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
"""Replay runs from a text file of generators."""
import argparse
import json
import re
import sys
from pathlib import Path
from gooey import Gooey
import nemo
from nemo import costs, scenarios, utils
if len(sys.argv) > 1 and '--ignore-gooey' not in sys.argv:
sys.argv.append('--ignore-gooey')
@Gooey(program_name="NEMO replay tool",
richtext_controls=True,
show_success_modal=False,
monospaced_font=True,
optional_cols=1)
def process_options():
"""Process command line options."""
epilog = 'Bug reports via https://nemo.ozlabs.org/'
parser = argparse.ArgumentParser(epilog=epilog)
parser.add_argument("-f", type=str, help='filename of results file',
metavar='FILE', default='results.json')
parser.add_argument("-p", "--plot", action="store_true",
help='plot an energy balance')
parser.add_argument("-v", action="count", help='verbosity level',
default=0)
parser.add_argument("--spills", action="store_true",
help='plot surplus generation')
parser.add_argument("--no-legend", action="store_false",
help="hide legend")
return parser.parse_args()
def run_one(bundle):
"""Run a single simulation."""
options = bundle['options']
context = nemo.Context()
context.nsp_limit = options['nsp_limit']
if not 0 <= context.nsp_limit <= 1:
raise ValueError(context.nsp_limit)
context.min_regional_generation = options['min_regional_generation']
if not 0 <= context.min_regional_generation <= 1:
raise ValueError(context.min_regional_generation)
scenario = options['supply_scenario']
try:
scenarios.supply_scenarios[scenario](context)
except KeyError:
print(f'unknown scenario: {scenario}')
sys.exit(1)
print('scenario', scenario)
try:
cost_class = costs.cost_scenarios[options['costs']]
except KeyError:
cost_class = options['costs']
print(f'unknown cost class: {cost_class}')
sys.exit(1)
context.costs = cost_class(options['discount_rate'],
options['coal_price'], options['gas_price'],
options['ccs_storage_costs'])
context.costs.carbon = options['carbon_price']
capacities = bundle['parameters']
context.set_capacities(capacities)
context.verbose = args.v > 1
nemo.run(context)
context.verbose = args.v > 0
print(context)
print("Done")
print()
if args.plot:
utils.plot(context, spills=args.spills, showlegend=args.no_legend)
args = process_options()
with Path(args.f).open('r', encoding='utf-8') as resultsfile:
for line in resultsfile:
if re.search(r'^\s*$', line):
continue
if re.search(r'^\s*#', line):
print(line, end=' ')
continue
try:
json_bundle = json.loads(line)
except ValueError:
print('skipping malformed input:', line)
continue
run_one(json_bundle)