Skip to content

Commit 2a12a16

Browse files
parse SAM cash flow profile in client, add example_SAM-single-owner-PPA
1 parent 30a9739 commit 2a12a16

File tree

7 files changed

+513
-1
lines changed

7 files changed

+513
-1
lines changed

src/geophires_x/EconomicsSamCashFlow.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import os
66
import re
77
import sys
8+
import math
89
from functools import lru_cache
910
from typing import Any
1011

@@ -54,6 +55,9 @@ def adj(x_):
5455
if isinstance(x_, str):
5556
return x_
5657
else:
58+
if math.isnan(x_):
59+
return 'NaN'
60+
5761
return rnd(x_)
5862

5963
return adj

src/geophires_x/GeoPHIRESUtils.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,3 +621,21 @@ def static_pressure_MPa(rho_kg_per_m3: float, depth_m: float) -> float:
621621

622622
return pressure_mpa
623623

624+
625+
def is_int(o: Any) -> bool:
626+
try:
627+
float_n = float(o)
628+
int_n = int(float_n)
629+
except ValueError:
630+
return False
631+
else:
632+
return float_n == int_n
633+
634+
635+
def is_float(o: Any) -> bool:
636+
try:
637+
float(o)
638+
except ValueError:
639+
return False
640+
else:
641+
return True

src/geophires_x/Outputs.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,22 @@ def get_sam_cash_flow_profile_output(self, model):
770770
ret += ' * SAM CASH FLOW PROFILE *\n'
771771
ret += ' ***************************\n'
772772

773-
ret += get_sam_cash_flow_profile_tabulated_output(model)
773+
cfp_o: str = get_sam_cash_flow_profile_tabulated_output(model)
774+
775+
# Ideally the separator line would be exactly the print width of the widest column, but the actual print width
776+
# of tabs varies (at least according to https://stackoverflow.com/a/7643592). 4 spaces seems to be the minimum
777+
# number that results in a separator line at least as wide as the table (narrower would be unsightly).
778+
spaces_per_tab = 4
779+
780+
# The tabluate library has native separating line functionality (per https://pypi.org/project/tabulate/) but
781+
# I wasn't able to get it to replicate the formatting as coded below.
782+
separator_line = len(cfp_o.split('\n')[0].replace('\t',' ' * spaces_per_tab)) * '-'
783+
784+
ret += separator_line + '\n'
785+
ret += cfp_o
786+
ret += '\n' + separator_line
787+
788+
ret += '\n\n'
774789

775790
return ret
776791

src/geophires_x_client/geophires_x_result.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@
66
from io import StringIO
77
from pathlib import Path
88
from types import MappingProxyType
9+
from typing import Any
910
from typing import ClassVar
1011

12+
from geophires_x.GeoPHIRESUtils import is_float
13+
from geophires_x.GeoPHIRESUtils import is_int
14+
1115
from .common import _get_logger
1216
from .geophires_input_parameters import EndUseOption
1317

@@ -402,6 +406,10 @@ def __init__(self, output_file_path, logger_name=None):
402406
if sdacgt_profile is not None:
403407
self.result['S-DAC-GT PROFILE'] = sdacgt_profile
404408

409+
sam_cash_flow_profile = self._get_sam_cash_flow_profile()
410+
if sam_cash_flow_profile is not None:
411+
self.result['SAM CASH FLOW PROFILE'] = sam_cash_flow_profile
412+
405413
self.result['metadata'] = {'output_file_path': self.output_file_path}
406414
for metadata_field in GeophiresXResult._METADATA_FIELDS:
407415
self.result['metadata'][metadata_field] = self._get_equal_sign_delimited_field(metadata_field)
@@ -726,6 +734,46 @@ def extract_table_header(lines: list) -> list:
726734
self._logger.debug(f'Failed to get legacy {GeophiresXResult.CCUS_PROFILE_LEGACY_NAME}: {e}')
727735
return None
728736

737+
def _get_sam_cash_flow_profile(self) -> list[Any]:
738+
profile_name = 'SAM CASH FLOW PROFILE'
739+
740+
def _get_sam_cash_flow_profile_lines():
741+
s1 = f'* {profile_name} *'
742+
s2 = '-' * 50
743+
return ''.join(self._lines).split(s1)[1].split(s2)[0].split(s2)[0] # [5:]
744+
745+
try:
746+
s1 = f'* {profile_name} *'
747+
profile_text = ''.join(self._lines).split(s1)[1]
748+
profile_text = re.split(r'^\s*-{20,}\s*$\n?', profile_text, flags=re.MULTILINE)[1]
749+
rd = csv.reader(StringIO(profile_text), delimiter='\t', skipinitialspace=True)
750+
profile_lines = []
751+
for row in rd:
752+
row_clean = []
753+
for entry in row:
754+
entry_val = entry.rstrip()
755+
if is_int(entry_val):
756+
entry_val = int(float(entry_val))
757+
758+
if is_float(entry_val):
759+
# FIXME WIP
760+
import math
761+
762+
entry_val_float = float(entry_val)
763+
if math.isnan(entry_val_float):
764+
# entry_val = 'NaN' # FIXME WIP
765+
entry_val = math.nan
766+
else:
767+
entry_val = entry_val_float
768+
769+
row_clean.append(entry_val)
770+
profile_lines.append(row_clean)
771+
772+
return profile_lines
773+
except BaseException as e:
774+
self._logger.debug(f'Failed to get SAM cash flow profile: {e}')
775+
return None
776+
729777
def _extract_addons_style_table_data(self, lines: list):
730778
"""TODO consolidate with _get_data_from_profile_lines"""
731779

0 commit comments

Comments
 (0)