Skip to content

Commit 96f98ae

Browse files
authored
Merge pull request NREL#398 from jeffbourdier/input-parameters-in-CSV
Allow input parameters in CSV
2 parents 720dc80 + 62c4843 commit 96f98ae

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

src/geophires_x_client/geophires_input_parameters.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import csv
12
import tempfile
23
import uuid
34
from dataclasses import dataclass
45
from dataclasses import field
56
from enum import Enum
7+
from io import StringIO
68
from pathlib import Path
79
from types import MappingProxyType
810
from typing import Any
@@ -217,3 +219,17 @@ def as_file_path(self) -> Path:
217219
def get_output_file_path(self) -> Path:
218220
"""Returns a unique path for the GEOPHIRES output file."""
219221
return Path(tempfile.gettempdir(), f'geophires-result_{self._instance_id!s}.out')
222+
223+
def as_csv(self) -> str:
224+
"""
225+
This method returns the input parameters in CSV (Comma-Separated Values) format, using its
226+
internal dictionary as the definitive source. It does not include a header, but uses the
227+
same columns as GeophiresXResult.as_csv, and is meant to be used in conjunction with the same.
228+
"""
229+
if self.from_file_path:
230+
raise NotImplementedError('CSV from file path is not implemented.')
231+
232+
f = StringIO()
233+
w = csv.writer(f)
234+
w.writerows([['INPUT PARAMETERS', key, '', value, ''] for key, value in self.params.items()])
235+
return f.getvalue()

tests/test_geophires_x_client.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import csv
12
import tempfile
23
import uuid
34
from pathlib import Path
@@ -631,3 +632,30 @@ def test_stash_cwd(self):
631632
)
632633

633634
self.assertEqual(start_cwd, Path.cwd())
635+
636+
def test_csv_with_input_parameters(self):
637+
with self.assertRaises(NotImplementedError):
638+
# This should fail because CSV from file path is not implemented.
639+
ImmutableGeophiresInputParameters(from_file_path=self._get_test_file_path('input_comments.txt')).as_csv()
640+
641+
# Simulate the main use case of adding input parameters to the CSV download from the web interface.
642+
csv_input = ImmutableGeophiresInputParameters(params={'Reservoir Depth': 3, 'Gradient 1': 50}).as_csv()
643+
csv_output = GeophiresXResult(self._get_test_file_path('geophires-result_example-1.out')).as_csv()
644+
csv_parts = csv_output.split(csv.excel.lineterminator, 1)
645+
csv_result = csv_parts[0] + csv.excel.lineterminator + csv_input + csv_parts[1]
646+
647+
# Ensure the returned CSV are as expected.
648+
csv_lines = csv_result.splitlines()
649+
self.assertEqual(csv_lines[0], 'Category,Field,Year,Value,Units')
650+
self.assertEqual(csv_lines[1], 'INPUT PARAMETERS,Reservoir Depth,,3,')
651+
self.assertEqual(csv_lines[2], 'INPUT PARAMETERS,Gradient 1,,50,')
652+
self.assertEqual(csv_lines[3], 'SUMMARY OF RESULTS,End-Use Option,,Direct-Use Heat,')
653+
self.assertEqual(
654+
csv_lines[len(csv_lines) - 1],
655+
'HEAT AND/OR ELECTRICITY EXTRACTION AND GENERATION PROFILE,PERCENTAGE OF TOTAL HEAT MINED,25,42.7,%',
656+
)
657+
658+
# Export the CSV for testing in Excel (or other spreadsheet software).
659+
result_file = Path(tempfile.gettempdir(), f'geophires-result_{uuid.uuid1()!s}.csv')
660+
with open(result_file, 'w', newline='', encoding='utf-8') as rf:
661+
rf.write(csv_result)

0 commit comments

Comments
 (0)