Skip to content

Commit 18021a0

Browse files
parse_units_and_comments working impl + test_csv_with_input_parameters_parse_units_and_comments
1 parent 9cafed8 commit 18021a0

3 files changed

Lines changed: 31 additions & 11 deletions

File tree

src/geophires_x_client/geophires_input_parameters.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -251,22 +251,34 @@ def _row_entries(param_name: str, param_value_raw: str) -> list[str]:
251251
if parse_units_and_comments:
252252
# TODO consolidate with other codebase parameter parsing logic...
253253
param_schema = request_schema['properties'].get(param_name, {param_name: {}})
254+
is_array_type = param_schema.get('type') == 'array'
254255

255256
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 ['', '']
257+
value_entry.split(' ', maxsplit=1) if not is_array_type else value_entry.split(', --', maxsplit=1)
263258
)
259+
value_entry = value_non_value_split[0].rstrip(',').rstrip()
260+
remainder = value_non_value_split[1] if len(value_non_value_split) > 1 else ''
264261

265262
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
267263

268-
if len(unit_and_comment_split) > 1:
269-
comment_entry = unit_and_comment_split[1]
264+
if is_array_type:
265+
# For array-type params, the ', --' split already consumed the comment delimiter;
266+
# whatever remains is the comment (no units possible from the value string).
267+
comment_entry = remainder.lstrip() if remainder else ''
268+
units_entry = default_units_for_param
269+
else:
270+
# For scalar params, remainder is either "<units>" or "-- <comment>"
271+
# (optionally "<units> -- <comment>").
272+
unit_and_comment_split = remainder.split(' --', maxsplit=1) if remainder else ['']
273+
units_part = unit_and_comment_split[0]
274+
if units_part.lstrip().startswith('--'):
275+
# No units, just an inline comment introduced by '-- '.
276+
comment_entry = units_part.lstrip()[2:].lstrip()
277+
units_entry = default_units_for_param
278+
else:
279+
units_entry = units_part if units_part != '' else default_units_for_param
280+
if len(unit_and_comment_split) > 1:
281+
comment_entry = unit_and_comment_split[1].lstrip()
270282

271283
return [
272284
'INPUT PARAMETERS',
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
INPUT PARAMETERS,Reservoir Depth,,3000,m,
2+
INPUT PARAMETERS,Gradient 1,,50,degC/km,
3+
INPUT PARAMETERS,End-Use Option,,1,,Direct-Use Heat
4+
INPUT PARAMETERS,Construction CAPEX Schedule,,"0.014,0.027,0.139,0.431,0.389",,
5+
INPUT PARAMETERS,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",1/year,"No drawdown for first 10 years, then 0.005/year"

tests/test_geophires_x_client.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -684,4 +684,7 @@ def test_csv_with_input_parameters_parse_units_and_comments(self):
684684
with open(result_file, 'w', newline='', encoding='utf-8') as rf:
685685
rf.write(csv_input_with_units_and_comments)
686686

687-
self.assertIsNotNone(csv_input_with_units_and_comments) # FIXME WIP
687+
self.assertIsNotNone(csv_input_with_units_and_comments)
688+
self.assertCsvFileContentsEqual(
689+
self._get_test_file_path('input-parameters-with-parsed-units-and-comments.csv'), result_file
690+
)

0 commit comments

Comments
 (0)