|
1 | 1 | # copyright, 2023, Malcolm I Ross
|
2 | 2 | from enum import IntEnum, Enum, auto
|
| 3 | +from typing import Any |
3 | 4 |
|
4 | 5 | import pint
|
5 | 6 | import os
|
6 | 7 |
|
7 |
| - |
8 | 8 | _UREG = None
|
| 9 | + |
| 10 | + |
9 | 11 | def get_unit_registry():
|
10 | 12 | global _UREG
|
11 | 13 | if _UREG is None:
|
12 | 14 | _UREG = pint.get_application_registry()
|
13 | 15 | _UREG.load_definitions(os.path.join(os.path.abspath(os.path.dirname(__file__)), 'GEOPHIRES3_newunits.txt'))
|
14 |
| - _UREG.preprocessors.append(lambda s: s.replace('%%', 'percent')) |
15 | 16 |
|
16 | 17 | return _UREG
|
17 | 18 |
|
18 | 19 |
|
19 |
| -def convertible_unit(u:str) -> str: |
20 |
| - if u == Units.PERCENT or u == PercentUnit.PERCENT or u == Units.PERCENT.value: |
| 20 | +def convertible_unit(unit: Any) -> Any: |
| 21 | + """ |
| 22 | + pint can't handle '%' as a unit in python 3.8, so use this method when constructing quantities |
| 23 | +
|
| 24 | + :type unit: str|Enum |
| 25 | + """ |
| 26 | + if unit == Units.PERCENT or unit == PercentUnit.PERCENT or unit == Units.PERCENT.value: |
21 | 27 | return 'percent'
|
22 | 28 |
|
23 |
| - return u |
| 29 | + return unit |
| 30 | + |
24 | 31 |
|
25 | 32 | class Units(IntEnum):
|
26 | 33 | """All possible systems of measure"""
|
@@ -67,8 +74,8 @@ class Units(IntEnum):
|
67 | 74 | POWERPERUNITAREA = auto()
|
68 | 75 | HEATPERUNITVOLUME = auto()
|
69 | 76 | POWERPERUNITVOLUME = auto()
|
70 |
| - DECAY_RATE=auto() |
71 |
| - INFLATION_RATE=auto() |
| 77 | + DECAY_RATE = auto() |
| 78 | + INFLATION_RATE = auto() |
72 | 79 | DYNAMIC_VISCOSITY = auto()
|
73 | 80 |
|
74 | 81 |
|
@@ -166,6 +173,7 @@ class EnergyFrequencyUnit(str, Enum):
|
166 | 173 | MWhPERYEAR = "MWh/year"
|
167 | 174 | GWhPERYEAR = "GWh/year"
|
168 | 175 |
|
| 176 | + |
169 | 177 | class CurrencyUnit(str, Enum):
|
170 | 178 | """Currency Units"""
|
171 | 179 | MDOLLARS = "MUSD"
|
@@ -335,41 +343,41 @@ class MassUnit(str, Enum):
|
335 | 343 | OZ = "ounce"
|
336 | 344 |
|
337 | 345 |
|
338 |
| -class PopDensityUnit(str,Enum): |
| 346 | +class PopDensityUnit(str, Enum): |
339 | 347 | """Population Density Units"""
|
340 | 348 | perkm2 = "Population per square km"
|
341 | 349 |
|
342 | 350 |
|
343 |
| -class HeatPerUnitAreaUnit(str,Enum): |
| 351 | +class HeatPerUnitAreaUnit(str, Enum): |
344 | 352 | """Population Density Units"""
|
345 | 353 | KJPERSQKM = "kJ/km**2"
|
346 | 354 |
|
347 | 355 |
|
348 |
| -class PowerPerUnitAreaUnit(str,Enum): |
| 356 | +class PowerPerUnitAreaUnit(str, Enum): |
349 | 357 | """Population Density Units"""
|
350 | 358 | MWPERSQKM = "MW/km**2"
|
351 | 359 |
|
352 | 360 |
|
353 |
| -class HeatPerUnitVolumeUnit(str,Enum): |
| 361 | +class HeatPerUnitVolumeUnit(str, Enum): |
354 | 362 | """Population Density Units"""
|
355 | 363 | KJPERCUBICKM = "kJ/km**3"
|
356 | 364 |
|
357 | 365 |
|
358 |
| -class PowerPerUnitVolumeUnit(str,Enum): |
| 366 | +class PowerPerUnitVolumeUnit(str, Enum): |
359 | 367 | """Population Density Units"""
|
360 | 368 | MWPERCUBICKM = "MW/km**3"
|
361 | 369 |
|
362 | 370 |
|
363 |
| -class Decay_RateUnit(str,Enum): |
| 371 | +class Decay_RateUnit(str, Enum): |
364 | 372 | """Decay rate Units"""
|
365 | 373 | PERCENTPERYEAR = "%/yr"
|
366 | 374 |
|
367 | 375 |
|
368 |
| -class Inflation_RateUnit(str,Enum): |
| 376 | +class Inflation_RateUnit(str, Enum): |
369 | 377 | """Decay rate Units"""
|
370 | 378 | KPASCALPERYEAR = "kPa/yr"
|
371 | 379 |
|
372 | 380 |
|
373 |
| -class Dynamic_ViscosityUnit(str,Enum): |
| 381 | +class Dynamic_ViscosityUnit(str, Enum): |
374 | 382 | """Dynamic Viscosity Units"""
|
375 | 383 | PASCALSEC = "PaSec"
|
0 commit comments