Skip to content

Commit fa591d3

Browse files
added rich HTML output for All of GEOPHIRES.
1 parent b207b55 commit fa591d3

File tree

6 files changed

+1866
-412
lines changed

6 files changed

+1866
-412
lines changed

src/geophires_monte_carlo/MC_GeoPHIRES3.py

Lines changed: 3 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
from rich.table import Table
2727

2828
from geophires_monte_carlo.common import _get_logger
29-
from geophires_x.Parameter import Parameter
29+
from geophires_x.GeoPHIRESUtils import InsertImagesIntoHTML
30+
from geophires_x.GeoPHIRESUtils import render_default
3031
from geophires_x_client import GeophiresInputParameters
3132
from geophires_x_client import GeophiresXClient
3233
from geophires_x_client import GeophiresXResult
@@ -123,95 +124,7 @@ def Write_HTML_Output(
123124
console.print(statistics_table)
124125
console.save_html(html_path)
125126

126-
# Write a reference to the image(s) into the HTML file by inserting before the "</body>" tag
127-
# build the string to be inserted first
128-
insert_string = ''
129-
for _ in range(len(full_names)):
130-
name_to_use = short_names.pop()
131-
insert_string = insert_string + f'<img src="{name_to_use}.png" alt="{name_to_use}">\n'
132-
133-
match_string = '</body>'
134-
with open(html_path, 'r+', encoding='UTF-8') as html_file:
135-
contents = html_file.readlines()
136-
if match_string in contents[-1]: # Handle last line to prevent IndexError
137-
pass
138-
else:
139-
for index, line in enumerate(contents):
140-
if match_string in line and insert_string not in contents[index + 1]:
141-
contents.insert(index, insert_string)
142-
break
143-
html_file.seek(0)
144-
html_file.writelines(contents)
145-
146-
147-
def UpgradeSymbologyOfUnits(unit: str) -> str:
148-
"""
149-
UpgradeSymbologyOfUnits is a function that takes a string that represents a unit and replaces the **2 and **3
150-
with the appropriate unicode characters for superscript 2 and 3, and replaces "deg" with the unicode character
151-
for degrees.
152-
:param unit: a string that represents a unit
153-
:return: a string that represents a unit with the appropriate unicode characters for superscript 2 and 3, and
154-
replaces "deg" with the unicode character for degrees.
155-
"""
156-
return unit.replace('**2', '\u00b2').replace('**3', '\u00b3').replace('deg', '\u00b0')
157-
158-
159-
def render_default(p: float, unit: str = '') -> str:
160-
"""
161-
RenderDefault - render a float as a string with 2 decimal places, or in scientific notation if it is greater than
162-
10,000 with the unit appended to it if it is not an empty string (the default)
163-
:param p: the float to render
164-
:type p: float
165-
:param unit: the unit to append to the string
166-
:type unit: str
167-
:return: the string representation of the float
168-
:rtype: str
169-
"""
170-
unit = UpgradeSymbologyOfUnits(unit)
171-
# if the number is greater than 10,000, render it in scientific notation
172-
if p > 10_000:
173-
return f'{p:10.2e} {unit}'.strip()
174-
# otherwise, render it with 2 decimal places
175-
else:
176-
return f'{p:10.2f} {unit}'.strip()
177-
178-
179-
def render_scientific(p: float, unit: str = '') -> str:
180-
"""
181-
RenderScientific - render a float as a string in scientific notation with 2 decimal places
182-
and the unit appended to it if it is not an empty string (the default)
183-
:param p: the float to render
184-
:type p: float
185-
:param unit: the unit to append to the string
186-
:type unit: str
187-
:return: the string representation of the float
188-
:rtype: str
189-
"""
190-
unit = UpgradeSymbologyOfUnits(unit)
191-
return f'{p:10.2e} {unit}'.strip()
192-
193-
194-
def render_Parameter_default(p: Parameter) -> str:
195-
"""
196-
RenderDefault - render a float as a string with 2 decimal places, or in scientific notation if it is greater than
197-
10,000 with the unit appended to it if it is not an empty string (the default) by calling the render_default base
198-
function
199-
:param p: the parameter to render
200-
:type p: float
201-
:return: the string representation of the float
202-
"""
203-
return render_default(p.value, p.CurrentUnits.value)
204-
205-
206-
def render_parameter_scientific(p: Parameter) -> str:
207-
"""
208-
RenderScientific - render a float as a string in scientific notation with 2 decimal places
209-
and the unit appended to it if it is not an empty string (the default) by calling the render_scientific base function
210-
:param p: the parameter to render
211-
:type p: float
212-
:return: the string representation of the float
213-
"""
214-
return render_scientific(p.value, p.CurrentUnits.value)
127+
InsertImagesIntoHTML(html_path, full_names, short_names)
215128

216129

217130
def check_and_replace_mean(input_value, args) -> list:

src/geophires_x/GEOPHIRESv3.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,6 @@ def main(enable_geophires_logging_config=True):
8080
for line in content:
8181
sys.stdout.write(line)
8282

83-
# make district heating plot
84-
if model.surfaceplant.plant_type.value == OptionList.PlantType.DISTRICT_HEATING:
85-
model.outputs.MakeDistrictHeatingPlot(model)
86-
8783
logger.info(f'Complete {str(__name__)}: {sys._getframe().f_code.co_name}')
8884

8985

src/geophires_x/GeoPHIRESUtils.py

Lines changed: 126 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import CoolProp.CoolProp as CP
1919

20-
from geophires_x.Parameter import ParameterEntry
20+
from geophires_x.Parameter import ParameterEntry, Parameter
2121
from geophires_x.Units import get_unit_registry
2222

2323
_logger = logging.getLogger('root') # TODO use __name__ instead of root
@@ -94,6 +94,131 @@
9494
_ureg = get_unit_registry()
9595

9696

97+
def InsertImagesIntoHTML(html_path: str, short_names: set, full_names: set) -> None:
98+
99+
# Write a reference to the image(s) into the HTML file by inserting before the "</body>" tag
100+
# build the string to be inserted first
101+
insert_string = ''
102+
for _ in range(len(full_names)):
103+
name_to_use = short_names.pop()
104+
insert_string = insert_string + f'<img src="{name_to_use}.png" alt="{name_to_use}">\n<br>'
105+
106+
match_string = '</body>'
107+
with open(html_path, 'r+', encoding='UTF-8') as html_file:
108+
contents = html_file.readlines()
109+
if match_string in contents[-1]: # Handle last line to prevent IndexError
110+
pass
111+
else:
112+
for index, line in enumerate(contents):
113+
if match_string in line and insert_string not in contents[index + 1]:
114+
contents.insert(index, insert_string)
115+
break
116+
html_file.seek(0)
117+
html_file.writelines(contents)
118+
119+
120+
def UpgradeSymbologyOfUnits(unit: str) -> str:
121+
"""
122+
UpgradeSymbologyOfUnits is a function that takes a string that represents a unit and replaces the **2 and **3
123+
with the appropriate unicode characters for superscript 2 and 3, and replaces "deg" with the unicode character
124+
for degrees.
125+
:param unit: a string that represents a unit
126+
:return: a string that represents a unit with the appropriate unicode characters for superscript 2 and 3, and
127+
replaces "deg" with the unicode character for degrees.
128+
"""
129+
130+
return unit.replace('**2', '\u00b2').replace('**3', '\u00b3').replace('deg', '\u00b0')
131+
132+
133+
def render_default(p: float, unit: str = '', fmt: str = '') -> str:
134+
"""
135+
RenderDefault - render a float as a string with 2 decimal place by default, or whatever format the user specifies,
136+
or in scientific notation if it is greater than 10,000
137+
with the unit appended to it if it is not an empty string (the default)
138+
:param p: the float to render
139+
:type p: float
140+
:param unit: the unit to append to the string
141+
:type unit: str
142+
:param fmt: the format to use for the string representation of the float
143+
:type fmt: str
144+
:return: the string representation of the float
145+
"""
146+
if not np.can_cast(p, float):
147+
raise ValueError(f'Parameter ({p}) must be a float or convertible to float.')
148+
149+
unit = UpgradeSymbologyOfUnits(unit)
150+
# if the number is greater than 10,000, render it in scientific notation
151+
if p > 10_000:
152+
return render_scientific(p, unit)
153+
# otherwise, render it with 2 decimal places
154+
else:
155+
if not fmt:
156+
return f'{p:10.2f} {unit}'.strip()
157+
else:
158+
if ':' in fmt:
159+
fmt = fmt.split(':')[1]
160+
fmt = '{0:' + fmt + '}{1:s}'
161+
return fmt.format(p, unit.strip())
162+
163+
164+
def render_scientific(p: float, unit: str = '', fmt: str = '') -> str:
165+
"""
166+
RenderScientific - render a float as a string in scientific notation with 2 decimal places by default, or whatever
167+
format the user specifies, and the unit appended to it if it is not an empty string (the default)
168+
:param p: the float to render
169+
:type p: float
170+
:param unit: the unit to append to the string
171+
:type unit: str
172+
:param fmt: the format to use for the string representation of the float
173+
:type fmt: str
174+
:return: the string representation of the float
175+
:rtype: str
176+
"""
177+
178+
if not np.can_cast(p, float):
179+
raise ValueError(f'Parameter ({p}) must be a float or convertible to float.')
180+
181+
unit = UpgradeSymbologyOfUnits(unit)
182+
if not fmt:
183+
return f'{p:10.2e} {unit}'.strip()
184+
else:
185+
pass
186+
187+
188+
def render_Parameter_default(p: Parameter, fmt: str = '') -> str:
189+
"""
190+
RenderDefault - render a float parameter in scientific notation as a string with 2 decimal places,
191+
or whatever format the user specifies with the unit appended to it if it is not an empty string (the default)
192+
function
193+
:param p: the parameter to render
194+
:type p: Parameter
195+
:param fmt: the format to use for the string representation of the float
196+
:type fmt: str
197+
:return: the string representation of the float
198+
"""
199+
if not np.can_cast(p.value, float):
200+
raise ValueError(f'Parameter ({p.value}) must be a float or convertible to float.')
201+
202+
return render_default(p.value, p.CurrentUnits.value)
203+
204+
205+
def render_parameter_scientific(p: Parameter, fmt: str = '') -> str:
206+
"""
207+
RenderScientific - render a float as a string in scientific notation with 2 decimal places
208+
and the unit appended to it if it is not an empty string (the default) by calling the render_scientific base function
209+
:param p: the parameter to render
210+
:type p: float
211+
:param fmt: the format to use for the string representation of the float
212+
:type fmt: str
213+
:return: the string representation of the float
214+
"""
215+
216+
if not np.can_cast(p.value, float):
217+
raise ValueError(f'Parameter ({p.value}) must be a float or convertible to float.')
218+
219+
return render_scientific(p.value, p.CurrentUnits.value)
220+
221+
97222
def quantity(value: float, unit: str) -> PlainQuantity:
98223
"""
99224
:rtype: pint.registry.Quantity - note type annotation uses PlainQuantity due to issues with python 3.8 failing

0 commit comments

Comments
 (0)