|
4 | 4 | is populated will influence the utility of using this method |
5 | 5 | """ |
6 | 6 |
|
| 7 | +import argparse |
7 | 8 | import itertools |
8 | | -import os.path |
9 | 9 | import sqlite3 |
10 | 10 |
|
11 | | -from definitions import PROJECT_ROOT |
12 | 11 | from matplotlib import pyplot as plt |
13 | 12 |
|
14 | 13 | # Written by: J. F. Hyink |
|
17 | 16 |
|
18 | 17 | # Created on: 7/18/23 |
19 | 18 |
|
20 | | -# filename of db to analyze... |
21 | | -db = 'US_9R_8D_CT500.sqlite' |
22 | | - |
23 | | -source_db_file = os.path.join(PROJECT_ROOT, 'data_files', 'untracked_data', db) |
24 | | -print(source_db_file) |
25 | | -res = [] |
26 | | -try: |
27 | | - con = sqlite3.connect(source_db_file) |
28 | | - cur = con.cursor() |
29 | | - cur.execute('SELECT max_cap FROM max_capacity') |
30 | | - for row in cur: |
31 | | - res.append(row) |
32 | | - |
33 | | -except sqlite3.Error as e: |
34 | | - print(e) |
35 | | - |
36 | | -finally: |
37 | | - con.close() |
38 | | - |
39 | | -# chain them together into a list |
40 | | -caps = list(itertools.chain(*res)) |
41 | | - |
42 | | -cutoff = 1 # GW : An arbitrary cutoff between big and small capacity systems. |
43 | | -small_cap_sources = [c for c in caps if c <= cutoff] |
44 | | -large_cap_sources = [c for c in caps if c > cutoff] |
45 | | - |
46 | | -aggregate_small_cap = sum(small_cap_sources) |
47 | | -aggregate_large_cap = sum(large_cap_sources) |
48 | | - |
49 | | -print(f'{len(small_cap_sources)} small cap sources account for: {aggregate_small_cap: 0.1f} GW') |
50 | | -print(f'{len(large_cap_sources)} large cap sources account for: {aggregate_large_cap: 0.1f} GW') |
51 | | - |
52 | | -plt.hist(caps, bins=100) |
53 | | -plt.show() |
54 | | - |
55 | | - |
56 | | -# make a cumulative contribution plot, and find a 5% cutoff |
57 | | -cutoff_num_sources = 0 |
58 | | -caps.sort() |
59 | | -total_cap = sum(caps) |
60 | | -cumulative_caps = [ |
61 | | - caps[0] / total_cap, |
62 | | -] |
63 | | -for i, cap in enumerate(caps[1:]): |
64 | | - cumulative_caps.append(cap / total_cap + cumulative_caps[i]) |
65 | | - if cumulative_caps[-1] < 0.05: |
66 | | - cutoff_num_sources += 1 |
67 | | - |
68 | | -plt.plot(range(len(cumulative_caps)), cumulative_caps) |
69 | | -plt.axvline(x=cutoff_num_sources, color='red', ls='--') |
70 | | -plt.xlabel('Aggregated Sources') |
71 | | -plt.ylabel('Proportion of Total Capacity') |
72 | | -plt.title('Aggregate Capacity vs. Number of Sources') |
73 | | - |
74 | | -plt.show() |
| 19 | + |
| 20 | +def analyze_capacity(db_path: str) -> None: |
| 21 | + res = [] |
| 22 | + con = None |
| 23 | + try: |
| 24 | + con = sqlite3.connect(db_path) |
| 25 | + cur = con.cursor() |
| 26 | + cur.execute('SELECT max_cap FROM max_capacity') |
| 27 | + for row in cur: |
| 28 | + res.append(row) |
| 29 | + |
| 30 | + except sqlite3.Error as e: |
| 31 | + print(f'Error connecting to database: {e}') |
| 32 | + return |
| 33 | + |
| 34 | + finally: |
| 35 | + if con: |
| 36 | + con.close() |
| 37 | + |
| 38 | + if not res: |
| 39 | + print('No data found in max_capacity table.') |
| 40 | + return |
| 41 | + |
| 42 | + # chain them together into a list |
| 43 | + caps = list(itertools.chain(*res)) |
| 44 | + |
| 45 | + cutoff = 1 # GW : An arbitrary cutoff between big and small capacity systems. |
| 46 | + small_cap_sources = [c for c in caps if c <= cutoff] |
| 47 | + large_cap_sources = [c for c in caps if c > cutoff] |
| 48 | + |
| 49 | + aggregate_small_cap = sum(small_cap_sources) |
| 50 | + aggregate_large_cap = sum(large_cap_sources) |
| 51 | + |
| 52 | + print(f'{len(small_cap_sources)} small cap sources account for: {aggregate_small_cap: 0.1f} GW') |
| 53 | + print(f'{len(large_cap_sources)} large cap sources account for: {aggregate_large_cap: 0.1f} GW') |
| 54 | + |
| 55 | + plt.hist(caps, bins=100) |
| 56 | + plt.show() |
| 57 | + |
| 58 | + # make a cumulative contribution plot, and find a 5% cutoff |
| 59 | + cutoff_num_sources = 0 |
| 60 | + caps.sort() |
| 61 | + total_cap = sum(caps) |
| 62 | + cumulative_caps = [ |
| 63 | + caps[0] / total_cap, |
| 64 | + ] |
| 65 | + for i, cap in enumerate(caps[1:]): |
| 66 | + cumulative_caps.append(cap / total_cap + cumulative_caps[i]) |
| 67 | + if cumulative_caps[-1] < 0.05: |
| 68 | + cutoff_num_sources += 1 |
| 69 | + |
| 70 | + plt.plot(range(len(cumulative_caps)), cumulative_caps) |
| 71 | + plt.axvline(x=cutoff_num_sources, color='red', ls='--') |
| 72 | + plt.xlabel('Aggregated Sources') |
| 73 | + plt.ylabel('Proportion of Total Capacity') |
| 74 | + plt.title('Aggregate Capacity vs. Number of Sources') |
| 75 | + |
| 76 | + plt.show() |
| 77 | + |
| 78 | + |
| 79 | +def main() -> None: |
| 80 | + parser = argparse.ArgumentParser( |
| 81 | + description='Analyze capacity distribution in a Temoa database.' |
| 82 | + ) |
| 83 | + parser.add_argument('db_path', help='Path to the SQLite database file.') |
| 84 | + args = parser.parse_args() |
| 85 | + |
| 86 | + analyze_capacity(args.db_path) |
| 87 | + |
| 88 | + |
| 89 | +if __name__ == '__main__': |
| 90 | + main() |
0 commit comments