Skip to content

Commit 22158fe

Browse files
generate categorized hip-ra-x-result.json
1 parent 57ff2a7 commit 22158fe

3 files changed

Lines changed: 250 additions & 36 deletions

File tree

src/geophires_x_schema_generator/__init__.py

Lines changed: 51 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
from __future__ import annotations
2+
13
import json
24
import os
35
import sys
46
from pathlib import Path
5-
from typing import Tuple, Any
7+
from typing import Any, Callable
68

79
# Ruff disabled because imports are order-dependent
810
# ruff: noqa: I001
@@ -38,7 +40,8 @@
3840

3941
class GeophiresXSchemaGenerator:
4042
def __init__(self):
41-
pass
43+
# noinspection PyProtectedMember
44+
self.result_fields_by_category: dict[str, list[Any]] = GeophiresXResult._RESULT_FIELDS_BY_CATEGORY
4245

4346
@staticmethod
4447
def _get_dummy_model():
@@ -85,7 +88,7 @@ def get_parameter_sources(self) -> list:
8588
def get_schema_title(self) -> str:
8689
return 'GEOPHIRES'
8790

88-
def get_parameters_json(self) -> Tuple[str, str]:
91+
def get_parameters_json(self) -> tuple[str, str]:
8992

9093
def with_category(param_dict: dict, category: str):
9194
def _with_cat(p: Parameter, cat: str):
@@ -103,7 +106,7 @@ def _with_cat(p: Parameter, cat: str):
103106

104107
return json_dumpse(input_params), json_dumpse(output_params)
105108

106-
def generate_json_schema(self) -> Tuple[dict, dict]:
109+
def generate_json_schema(self) -> tuple[dict, dict]:
107110
"""
108111
:return: request schema, result schema
109112
:rtype: Tuple[dict, dict]
@@ -153,6 +156,10 @@ def get_result_json_schema(self, output_params_json) -> dict:
153156
properties = {}
154157
required = []
155158

159+
# if result_fields_by_category is None:
160+
# # noinspection PyProtectedMember
161+
# result_fields_by_category = GeophiresXResult._RESULT_FIELDS_BY_CATEGORY
162+
156163
output_params = json.loads(output_params_json)
157164
display_name_aliases = {}
158165
for param_name in output_params:
@@ -165,11 +172,10 @@ def get_result_json_schema(self, output_params_json) -> dict:
165172

166173
output_params = {**output_params, **display_name_aliases}
167174

168-
# noinspection PyProtectedMember
169-
for category in GeophiresXResult._RESULT_FIELDS_BY_CATEGORY:
175+
for category in self.result_fields_by_category:
170176
cat_properties = {}
171-
# noinspection PyProtectedMember
172-
for field in GeophiresXResult._RESULT_FIELDS_BY_CATEGORY[category]:
177+
178+
for field in self.result_fields_by_category[category]:
173179
param_name = field if isinstance(field, str) else field.field_name
174180

175181
ignored_output_param_names = ['After-Tax IRR'] # Silently ignored in favor of "After-tax IRR"
@@ -357,7 +363,7 @@ def _get_key(param: dict, k: str, default_val='') -> Any:
357363
return default_val
358364

359365

360-
def _get_min_and_max(param: dict, default_val='') -> Tuple:
366+
def _get_min_and_max(param: dict, default_val='') -> tuple:
361367
min_val = _get_key(param, 'Min', default_val=default_val)
362368
max_val = _get_key(param, 'Max', default_val=default_val)
363369

@@ -377,13 +383,30 @@ def _fix_floating_point_error(val: Any) -> Any:
377383

378384

379385
class HipRaXSchemaGenerator(GeophiresXSchemaGenerator):
380-
# Single implicit category used to keep the result schema shape consistent
381-
# with the GEOPHIRES result schema (top-level properties -> category -> properties -> fields).
382-
# FIXME WIP refactor to reflect SUMMARY OF RESULTS and SUMMARY OF INPUTS categories
383-
_RESULT_CATEGORY: str = 'HIP-RA-X OUTPUTS'
386+
387+
def __init__(self):
388+
dummy_model = HIP_RA_X()
389+
390+
def _get_result_fields_for_category(
391+
output_config: list[tuple[Parameter, Callable[[Parameter], str]]],
392+
) -> list[Any]:
393+
return [it[0].Name for it in output_config]
394+
395+
# noinspection PyProtectedMember
396+
self.result_fields_by_category: dict[str, list[Any]] = {
397+
HIP_RA_X._SUMMARY_OF_RESULTS_OUTPUT_CATEGORY: _get_result_fields_for_category(
398+
dummy_model._get_output_config_for_summary_of_results_category(None, None)
399+
),
400+
HIP_RA_X._SUMMARY_OF_INPUTS_OUTPUT_CATEGORY: _get_result_fields_for_category(
401+
dummy_model._get_output_config_for_summary_of_inputs_category(None, None)
402+
),
403+
}
384404

385405
def get_parameter_sources(self) -> list:
386406
"""
407+
Single implicit input category used to keep the result schema shape consistent
408+
with the GEOPHIRES result schema (top-level properties -> category -> properties -> fields).
409+
387410
:rtype: list[Tuple[Any, str]]
388411
"""
389412
dummy_model = HIP_RA_X()
@@ -395,22 +418,24 @@ def get_schema_title(self) -> str:
395418
def get_result_json_schema(self, output_params_json) -> dict:
396419
output_params = json.loads(output_params_json)
397420

398-
cat_properties = {}
399-
for param_name, output_param in output_params.items():
400-
description = _get_key(output_param, 'ToolTipText', default_val=None) or None
401-
units_val = output_param['CurrentUnits'] if isinstance(output_param.get('CurrentUnits'), str) else None
402-
cat_properties[param_name] = {
403-
'type': _get_key(output_param, 'json_parameter_type', default_val=None) or None,
404-
'description': description,
405-
'units': units_val,
406-
}
421+
properties = {}
407422

408-
properties = {
409-
self._RESULT_CATEGORY: {
423+
# noinspection PyProtectedMember
424+
for category in [HIP_RA_X._SUMMARY_OF_RESULTS_OUTPUT_CATEGORY, HIP_RA_X._SUMMARY_OF_INPUTS_OUTPUT_CATEGORY]:
425+
cat_properties = {}
426+
for param_name, output_param in output_params.items():
427+
description = _get_key(output_param, 'ToolTipText', default_val=None) or None
428+
units_val = output_param['CurrentUnits'] if isinstance(output_param.get('CurrentUnits'), str) else None
429+
cat_properties[param_name] = {
430+
'type': _get_key(output_param, 'json_parameter_type', default_val=None) or None,
431+
'description': description,
432+
'units': units_val,
433+
}
434+
435+
properties[category] = {
410436
'type': 'object',
411437
'properties': cat_properties,
412438
}
413-
}
414439

415440
result_schema = {
416441
'definitions': {},
@@ -459,4 +484,4 @@ def get_input_schema_reference(self) -> str:
459484
return 'hip-ra-x-request.json'
460485

461486
def get_output_schema_reference(self) -> str:
462-
return None
487+
return 'hip-ra-x-result.json'

src/geophires_x_schema_generator/hip-ra-x-result.json

Lines changed: 181 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,187 @@
55
"title": "HIP-RA-X Result Schema",
66
"required": [],
77
"properties": {
8-
"HIP-RA-X OUTPUTS": {
8+
"SUMMARY OF RESULTS": {
9+
"type": "object",
10+
"properties": {
11+
"Reservoir Volume (reservoir)": {
12+
"type": "number",
13+
"description": null,
14+
"units": "km**3"
15+
},
16+
"Reservoir Volume (rock)": {
17+
"type": "number",
18+
"description": null,
19+
"units": "km**3"
20+
},
21+
"Recoverable Volume (recoverable fluid)": {
22+
"type": "number",
23+
"description": null,
24+
"units": "km**3"
25+
},
26+
"Stored Heat (reservoir)": {
27+
"type": "number",
28+
"description": null,
29+
"units": "kJ"
30+
},
31+
"Stored Heat (rock)": {
32+
"type": "number",
33+
"description": null,
34+
"units": "kJ"
35+
},
36+
"Stored Heat (fluid)": {
37+
"type": "number",
38+
"description": null,
39+
"units": "kJ"
40+
},
41+
"Mass of Reservoir (total)": {
42+
"type": "number",
43+
"description": null,
44+
"units": "kilogram"
45+
},
46+
"Mass of Reservoir (rock)": {
47+
"type": "number",
48+
"description": null,
49+
"units": "kilogram"
50+
},
51+
"Mass of Reservoir (fluid)": {
52+
"type": "number",
53+
"description": null,
54+
"units": "kilogram"
55+
},
56+
"Specific Enthalpy (reservoir)": {
57+
"type": "number",
58+
"description": null,
59+
"units": "kJ/kg"
60+
},
61+
"Specific Enthalpy (rock)": {
62+
"type": "number",
63+
"description": null,
64+
"units": "kJ/kg"
65+
},
66+
"Specific Enthalpy (fluid)": {
67+
"type": "number",
68+
"description": null,
69+
"units": "kJ/kg"
70+
},
71+
"Wellhead Heat (reservoir)": {
72+
"type": "number",
73+
"description": null,
74+
"units": "kJ"
75+
},
76+
"Wellhead Heat (rock)": {
77+
"type": "number",
78+
"description": null,
79+
"units": "kJ"
80+
},
81+
"Wellhead Heat (fluid)": {
82+
"type": "number",
83+
"description": null,
84+
"units": "kJ"
85+
},
86+
"Recovery Factor (reservoir)": {
87+
"type": "number",
88+
"description": null,
89+
"units": "%"
90+
},
91+
"Recovery Factor (rock)": {
92+
"type": "number",
93+
"description": null,
94+
"units": "%"
95+
},
96+
"Recovery Factor (fluid)": {
97+
"type": "number",
98+
"description": null,
99+
"units": "%"
100+
},
101+
"Available Heat (reservoir)": {
102+
"type": "number",
103+
"description": null,
104+
"units": "kJ"
105+
},
106+
"Available Heat (rock)": {
107+
"type": "number",
108+
"description": null,
109+
"units": "kJ"
110+
},
111+
"Available Heat (fluid)": {
112+
"type": "number",
113+
"description": null,
114+
"units": "kJ"
115+
},
116+
"Producible Heat (reservoir)": {
117+
"type": "number",
118+
"description": null,
119+
"units": "kJ"
120+
},
121+
"Producible Heat (rock)": {
122+
"type": "number",
123+
"description": null,
124+
"units": "kJ"
125+
},
126+
"Producible Heat (fluid)": {
127+
"type": "number",
128+
"description": null,
129+
"units": "kJ"
130+
},
131+
"Producible Electricity (reservoir)": {
132+
"type": "number",
133+
"description": null,
134+
"units": "MW"
135+
},
136+
"Producible Electricity (rock)": {
137+
"type": "number",
138+
"description": null,
139+
"units": "MW"
140+
},
141+
"Producible Electricity (fluid)": {
142+
"type": "number",
143+
"description": null,
144+
"units": "MW"
145+
},
146+
"Producible Heat/Unit Area (reservoir)": {
147+
"type": "number",
148+
"description": null,
149+
"units": "kJ/km**2"
150+
},
151+
"Producible Heat/Unit Area (rock)": {
152+
"type": "number",
153+
"description": null,
154+
"units": "kJ/km**2"
155+
},
156+
"Producible Heat/Unit Area (fluid)": {
157+
"type": "number",
158+
"description": null,
159+
"units": "kJ/km**2"
160+
},
161+
"Producible Heat/Unit Volume (reservoir)": {
162+
"type": "number",
163+
"description": null,
164+
"units": "kJ/km**3"
165+
},
166+
"Producible Electricity/Unit Area (reservoir)": {
167+
"type": "number",
168+
"description": null,
169+
"units": "MW/km**2"
170+
},
171+
"Producible Electricity/Unit Area (rock)": {
172+
"type": "number",
173+
"description": null,
174+
"units": "MW/km**2"
175+
},
176+
"Producible Electricity/Unit Area (fluid)": {
177+
"type": "number",
178+
"description": null,
179+
"units": "MW/km**2"
180+
},
181+
"Producible Electricity/Unit Volume (reservoir)": {
182+
"type": "number",
183+
"description": null,
184+
"units": "MW/km**3"
185+
}
186+
}
187+
},
188+
"SUMMARY OF INPUTS": {
9189
"type": "object",
10190
"properties": {
11191
"Reservoir Volume (reservoir)": {

0 commit comments

Comments
 (0)