|
17 | 17 |
|
18 | 18 | import CoolProp.CoolProp as CP
|
19 | 19 |
|
20 |
| -from geophires_x.Parameter import ParameterEntry |
| 20 | +from geophires_x.Parameter import ParameterEntry, Parameter |
21 | 21 | from geophires_x.Units import get_unit_registry
|
22 | 22 |
|
23 | 23 | _logger = logging.getLogger('root') # TODO use __name__ instead of root
|
|
94 | 94 | _ureg = get_unit_registry()
|
95 | 95 |
|
96 | 96 |
|
| 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 | + |
97 | 222 | def quantity(value: float, unit: str) -> PlainQuantity:
|
98 | 223 | """
|
99 | 224 | :rtype: pint.registry.Quantity - note type annotation uses PlainQuantity due to issues with python 3.8 failing
|
|
0 commit comments