Skip to content

Commit 6806f16

Browse files
format SAM cash flow profile with commas
1 parent 8f0cd67 commit 6806f16

File tree

7 files changed

+189
-157
lines changed

7 files changed

+189
-157
lines changed

src/geophires_x/EconomicsSam.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import json
44
import os
55
from functools import lru_cache
6+
from math import isnan
67
from pathlib import Path
78
from typing import Any
89

@@ -25,6 +26,7 @@
2526

2627
from geophires_x import Model as Model
2728
from geophires_x.EconomicsSamCashFlow import _calculate_sam_economics_cash_flow
29+
from geophires_x.GeoPHIRESUtils import is_float, is_int
2830
from geophires_x.Parameter import Parameter
2931
from geophires_x.Units import convertible_unit
3032

@@ -98,13 +100,26 @@ def get_sam_cash_flow_profile_tabulated_output(model: Model, **tabulate_kw_args)
98100
# fmt:off
99101
_tabulate_kw_args = {
100102
'tablefmt': 'tsv',
101-
'floatfmt': '.2f',
102-
'intfmt': ',',
103+
'floatfmt': ',.2f',
104+
# 'floatfmt': ':,',
103105
**tabulate_kw_args
104106
}
107+
105108
# fmt:on
106109

107-
return tabulate(model.economics.sam_economics.value[_SAM_CASH_FLOW_PROFILE_KEY], **_tabulate_kw_args)
110+
def get_entry_display(entry: Any) -> str:
111+
if is_float(entry):
112+
if not isnan(float(entry)):
113+
entry_display = f'{entry:,.2f}' if not is_int(entry) else f'{entry:,}'
114+
return entry_display
115+
return entry
116+
117+
profile_display = model.economics.sam_economics.value[_SAM_CASH_FLOW_PROFILE_KEY].copy()
118+
for i in range(len(profile_display)):
119+
for j in range(len(profile_display[i])):
120+
profile_display[i][j] = get_entry_display(profile_display[i][j])
121+
122+
return tabulate(profile_display, **_tabulate_kw_args)
108123

109124

110125
def _get_utility_rate_parameters(model: Model) -> dict[str, Any]:
@@ -182,7 +197,7 @@ def _get_average_net_generation_MW(model: Model) -> float:
182197
return np.average(model.surfaceplant.NetElectricityProduced.value)
183198

184199

185-
def _sig_figs(val: float, num_sig_figs: int) -> float:
200+
def _sig_figs(val: float | list | tuple, num_sig_figs: int) -> float:
186201
"""
187202
TODO move to utilities, probably
188203
"""

src/geophires_x/GeoPHIRESUtils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,5 +637,7 @@ def is_float(o: Any) -> bool:
637637
float(o)
638638
except ValueError:
639639
return False
640+
except TypeError:
641+
return False
640642
else:
641643
return True
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
11
ssc*
22
!single-owner-output-properties.json
3+
4+
# scratch files
5+
draft_issue.md
6+
sam-cash-flow-table-comparison.csv
7+
sam-cash-flow-table-saved.csv
8+

src/geophires_x_client/geophires_x_result.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -751,26 +751,29 @@ def _get_sam_cash_flow_profile_lines():
751751
profile_lines = []
752752
for row in rd:
753753
row_clean = []
754-
for entry in row:
755-
entry_val = entry.rstrip()
756-
if is_int(entry_val):
757-
entry_val = int(float(entry_val))
758-
759-
if is_float(entry_val):
760-
entry_val_float = float(entry_val)
761-
if math.isnan(entry_val_float):
762-
entry_val = math.nan
763-
else:
764-
entry_val = entry_val_float
765-
766-
row_clean.append(entry_val)
754+
for entry_display in row:
755+
row_clean.append(
756+
GeophiresXResult._get_sam_cash_flow_profile_entry_display_to_entry_val(entry_display)
757+
)
767758
profile_lines.append(row_clean)
768759

769760
return profile_lines
770761
except BaseException as e:
771762
self._logger.debug(f'Failed to get SAM cash flow profile: {e}')
772763
return None
773764

765+
@staticmethod
766+
def _get_sam_cash_flow_profile_entry_display_to_entry_val(entry_display: str) -> Any:
767+
if entry_display is None:
768+
return None
769+
770+
ed_san = entry_display.replace(',', '') if type(entry_display) is str else entry_display
771+
if is_float(ed_san):
772+
if not math.isnan(float(ed_san)):
773+
return float(ed_san) if not is_int(ed_san) else int(ed_san)
774+
775+
return entry_display
776+
774777
def _extract_addons_style_table_data(self, lines: list):
775778
"""TODO consolidate with _get_data_from_profile_lines"""
776779

0 commit comments

Comments
 (0)