Skip to content

Commit 9cafed8

Browse files
parse_units_and_comments initial impl (WIP)
1 parent aa3cb70 commit 9cafed8

2 files changed

Lines changed: 51 additions & 5 deletions

File tree

src/geophires_x_client/geophires_input_parameters.py

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import csv
2+
import json
3+
import os
24
import tempfile
35
import uuid
46
from dataclasses import dataclass
@@ -231,22 +233,48 @@ def as_csv(self, parse_units_and_comments: bool = False) -> str:
231233
raise NotImplementedError('CSV from file path is not implemented.')
232234

233235
if parse_units_and_comments:
234-
raise NotImplementedError # FIXME WIP
236+
237+
def _get_file_path(file_name: str | Path) -> str:
238+
return os.path.join(os.path.abspath(os.path.dirname(__file__)), str(file_name))
239+
240+
with open(_get_file_path('../geophires_x_schema_generator/geophires-request.json'), encoding='utf-8') as f:
241+
request_schema: dict[str, Any] = json.loads(f.read())
235242

236243
f = StringIO()
237244
w = csv.writer(f)
238245

239246
def _row_entries(param_name: str, param_value_raw: str) -> list[str]:
240-
value_entry = param_value_raw
247+
value_entry = (str(param_value_raw) if param_value_raw is not None else '').strip()
241248
units_entry = ''
242249
comment_entry = ''
250+
251+
if parse_units_and_comments:
252+
# TODO consolidate with other codebase parameter parsing logic...
253+
param_schema = request_schema['properties'].get(param_name, {param_name: {}})
254+
255+
value_non_value_split = (
256+
value_entry.split(' ', maxsplit=1)
257+
if not param_schema.get('type') == 'array'
258+
else value_entry.split(', --', maxsplit=1)
259+
)
260+
value_entry = value_non_value_split[0]
261+
unit_and_comment_split = (
262+
value_non_value_split[1].split(' --', maxsplit=1) if len(value_non_value_split) > 1 else ['', '']
263+
)
264+
265+
default_units_for_param = param_schema.get('units', '')
266+
units_entry = unit_and_comment_split[0] if unit_and_comment_split[0] != '' else default_units_for_param
267+
268+
if len(unit_and_comment_split) > 1:
269+
comment_entry = unit_and_comment_split[1]
270+
243271
return [
244272
'INPUT PARAMETERS',
245273
param_name,
246274
'', # Year column, N/A for input parameters
247275
value_entry,
248276
units_entry,
249-
# comment_entry
277+
comment_entry,
250278
]
251279

252280
w.writerows([_row_entries(key, value) for key, value in self.params.items()])

tests/test_geophires_x_client.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -655,8 +655,8 @@ def test_csv_with_input_parameters(self):
655655
# Ensure the returned CSV are as expected.
656656
csv_lines = csv_result.splitlines()
657657
self.assertEqual('Category,Field,Year,Value,Units', csv_lines[0])
658-
self.assertEqual('INPUT PARAMETERS,Reservoir Depth,,3,', csv_lines[1])
659-
self.assertEqual('INPUT PARAMETERS,Gradient 1,,50,', csv_lines[2])
658+
self.assertEqual('INPUT PARAMETERS,Reservoir Depth,,3,,', csv_lines[1])
659+
self.assertEqual('INPUT PARAMETERS,Gradient 1,,50,,', csv_lines[2])
660660
self.assertEqual('SUMMARY OF RESULTS,End-Use Option,,Direct-Use Heat,', csv_lines[3])
661661
self.assertEqual(
662662
'HEAT AND/OR ELECTRICITY EXTRACTION AND GENERATION PROFILE,PERCENTAGE OF TOTAL HEAT MINED,25,42.7,%',
@@ -667,3 +667,21 @@ def test_csv_with_input_parameters(self):
667667
result_file = Path(tempfile.gettempdir(), f'geophires-result_{uuid.uuid1()!s}.csv')
668668
with open(result_file, 'w', newline='', encoding='utf-8') as rf:
669669
rf.write(csv_result)
670+
671+
def test_csv_with_input_parameters_parse_units_and_comments(self):
672+
csv_input_with_units_and_comments = ImmutableGeophiresInputParameters(
673+
params={
674+
'Reservoir Depth': '3000 m',
675+
'Gradient 1': 50,
676+
'End-Use Option': '1, -- Direct-Use Heat',
677+
'Construction CAPEX Schedule': '0.014,0.027,0.139,0.431,0.389',
678+
'Drawdown Parameter Schedule': '0.003,0.001,0.0 * 10,0.001 * 3,0.002 * 3,0.003 * 3,0.004 * 3,0.005 * 4,0.006, -- No drawdown for first 10 years, then 0.005/year',
679+
}
680+
).as_csv(parse_units_and_comments=True)
681+
682+
# Export the CSV for testing in Excel (or other spreadsheet software).
683+
result_file = Path(tempfile.gettempdir(), f'geophires-result_{uuid.uuid1()!s}.csv')
684+
with open(result_file, 'w', newline='', encoding='utf-8') as rf:
685+
rf.write(csv_input_with_units_and_comments)
686+
687+
self.assertIsNotNone(csv_input_with_units_and_comments) # FIXME WIP

0 commit comments

Comments
 (0)