Skip to content

Commit

Permalink
code cleanup (units.py)
Browse files Browse the repository at this point in the history
  • Loading branch information
siligam committed Jul 2, 2024
1 parent 36009af commit bc5fe66
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 64 deletions.
26 changes: 9 additions & 17 deletions src/pymorize/units.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import pint
import tokenize
import io
import re
from collections import deque


"""
Expand All @@ -14,7 +11,7 @@
"""


#TODO: decide a place to hold the missing unit definitions that pint is not aware of
# TODO: decide a place to hold the missing unit definitions that pint is not aware of
#from pathlib import Path
#_unitsfile = Path(__file__).parent / "units_en.txt"
#u = pint.UnitRegistry(_unitsfile)
Expand All @@ -36,23 +33,18 @@


def fix_exponent_notation(s, pattern=re.compile(r'(?P<name>\w+)-(?P<exp>\d+)')):
"kg m-2 -> kg / m^2"
"m-2 -> m^-2"

def correction(match):
try:
float(match.group())
except ValueError:
d = match.groupdict()
s = "/ {0[name]}^{0[exp]}".format(d)
if d["exp"] == "1":
s = "/ {0[name]}".format(d)
s = "{0[name]}^-{0[exp]}".format(d)
return s
return match.group()

s = re.sub(pattern, correction, s)
if s.startswith('/'):
s = "1 " + s
return s
return re.sub(pattern, correction, s)

def fix_power_notation(s, pattern=re.compile(r"(?P<name>\w+)(?P<exp>\d+)")):
"m2 -> m^2"
Expand All @@ -70,8 +62,8 @@ def correction(match):
return re.sub(pattern, correction, s)


def to_slash_notation(unit):
"Conver the units so Pint can understand them"
def to_caret_notation(unit):
"Formats the unit so Pint can understand them"
return fix_power_notation(fix_exponent_notation(unit))


Expand All @@ -83,20 +75,20 @@ def convert(a: str, b: str) -> float:
try:
A = ureg(a)
except (pint.errors.DimensionalityError, pint.errors.UndefinedUnitError):
A = to_slash_notation(a)
A = to_caret_notation(a)
A = ureg(A)
try:
B = ureg(b)
except (pint.errors.DimensionalityError, pint.errors.UndefinedUnitError):
B = to_slash_notation(b)
B = to_caret_notation(b)
B = ureg(B)
print(A, B)
return A.to(B).magnitude


def is_equal(a: str, b: str):
"check if both 'a' and 'b' are equal"
return ureg(to_slash_notation(a)) == ureg(to_slash_notation(b))
return ureg(to_caret_notation(a)) == ureg(to_caret_notation(b))


def _quicktest():
Expand Down
14 changes: 14 additions & 0 deletions src/pymorize/units_en.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# units definition file
# These units are not found in Pint library

# convert mol Carbon to grams
molC = 12.0107 * g
molN = 14.007 * g
molFe = 55.874 * g

# <canonical name> = <relation to another unit or dimension> [= <symbol>] [= <alias>] [ = <alias> ] [...]
degree_east = deg = _ = degrees_east = degree_East = degrees_East = degree_E = degrees_E = degreeE
degree_west = -1 * deg = _ = degrees_west = degree_West = degrees_West = degree_W = degrees_W = degreeW
degree_north = deg = _ = degrees_north = degree_North = degrees_North = degree_N = degrees_N = degreeN
degree_south = -1 * deg = _ = degrees_south = degree_South = degrees_South = degree_S = degrees_S = degreeS

94 changes: 47 additions & 47 deletions tests/test_units.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pint
import pytest

from pymorize.units import to_slash_notation, convert
from pymorize.units import to_caret_notation, convert

# input samples that are found in CMIP6 tables and in fesom1 (recom)
allunits = [
Expand All @@ -10,59 +10,59 @@
('(mol/kg) / atm', '(mol/kg) / atm'),
('0.001', '0.001'),
('1', '1'),
('1.e6 J m-1 s-1', '1.e^6 J / m / s'),
('1.e6 J m-1 s-1', '1.e^6 J m^-1 s^-1'),
('1e-06', '1e-06'),
('1e-09', '1e-09'),
('1e-12', '1e-12'),
('1e-3 kg m-2', '1e-3 kg / m^2'),
('1e-6 m s-1', '1e-6 m / s'),
('1e-3 kg m-2', '1e-3 kg m^-2'),
('1e-6 m s-1', '1e-6 m s^-1'),
('1e3 km3', '1e3 km^3'),
('1e6 km2', '1e6 km^2'),
('J m-2', 'J / m^2'),
('J m-2', 'J m^-2'),
('K', 'K'),
('K Pa s-1', 'K Pa / s'),
('K m s-1', 'K m / s'),
('K s-1', 'K / s'),
('K Pa s-1', 'K Pa s^-1'),
('K m s-1', 'K m s^-1'),
('K s-1', 'K s^-1'),
('K2', 'K^2'),
('N m-1', 'N / m'),
('N m-2', 'N / m^2'),
('N m-1', 'N m^-1'),
('N m-2', 'N m^-2'),
('Pa', 'Pa'),
('Pa m s-2', 'Pa m / s^2'),
('Pa s-1', 'Pa / s'),
('Pa2 s-2', 'Pa^2 / s^2'),
('Pa m s-2', 'Pa m s^-2'),
('Pa s-1', 'Pa s^-1'),
('Pa2 s-2', 'Pa^2 s^-2'),
('W', 'W'),
('W m-2', 'W / m^2'),
('W m-2', 'W m^-2'),
('W/m2', 'W/m^2'),
('day', 'day'),
('degC', 'degC'),
('degC kg m-2', 'degC kg / m^2'),
('degC kg m-2', 'degC kg m^-2'),
('degC2', 'degC^2'),
('degree', 'degree'),
('degrees_east', 'degrees_east'),
('degrees_north', 'degrees_north'),
('kg', 'kg'),
('kg kg-1', 'kg / kg'),
('kg m-1 s-1', 'kg / m / s'),
('kg m-2', 'kg / m^2'),
('kg m-2 s-1', 'kg / m^2 / s'),
('kg m-3', 'kg / m^3'),
('kg s-1', 'kg / s'),
('km-2 s-1', '1 / km^2 / s'),
('kg kg-1', 'kg kg^-1'),
('kg m-1 s-1', 'kg m^-1 s^-1'),
('kg m-2', 'kg m^-2'),
('kg m-2 s-1', 'kg m^-2 s^-1'),
('kg m-3', 'kg m^-3'),
('kg s-1', 'kg s^-1'),
('km-2 s-1', 'km^-2 s^-1'),
('m', 'm'),
('m s-1', 'm / s'),
('m s-1 d-1', 'm / s / d'),
('m s-2', 'm / s^2'),
('m-1', '1 / m'),
('m-1 sr-1', '1 / m / sr'),
('m-2', '1 / m^2'),
('m-3', '1 / m^3'),
('m s-1', 'm s^-1'),
('m s-1 d-1', 'm s^-1 d^-1'),
('m s-2', 'm s^-2'),
('m-1', 'm^-1'),
('m-1 sr-1', 'm^-1 sr^-1'),
('m-2', 'm^-2'),
('m-3', 'm^-3'),
('m2', 'm^2'),
('m2 s-1', 'm^2 / s'),
('m2 s-2', 'm^2 / s^2'),
('m2 s-1', 'm^2 s^-1'),
('m2 s-2', 'm^2 s^-2'),
('m3', 'm^3'),
('m3 s-1', 'm^3 / s'),
('m3 s-2', 'm^3 / s^2'),
('m4 s-1', 'm^4 / s'),
('m3 s-1', 'm^3 s^-1'),
('m3 s-2', 'm^3 s^-2'),
('m4 s-1', 'm^4 s^-1'),
('mmol/m2', 'mmol/m^2'),
('mmol/m2/d', 'mmol/m^2/d'),
('mmolC/(m2*d)', 'mmolC/(m^2*d)'),
Expand All @@ -72,17 +72,17 @@
('mmolN/(m2*d)', 'mmolN/(m^2*d)'),
('mmolN/d', 'mmolN/d'),
('mmolN/m2/s', 'mmolN/m^2/s'),
('mol m-2', 'mol / m^2'),
('mol m-2 s-1', 'mol / m^2 / s'),
('mol m-3', 'mol / m^3'),
('mol m-3 s-1', 'mol / m^3 / s'),
('mol mol-1', 'mol / mol'),
('mol s-1', 'mol / s'),
('mol m-2', 'mol m^-2'),
('mol m-2 s-1', 'mol m^-2 s^-1'),
('mol m-3', 'mol m^-3'),
('mol m-3 s-1', 'mol m^-3 s^-1'),
('mol mol-1', 'mol mol^-1'),
('mol s-1', 'mol s^-1'),
('mol/kg', 'mol/kg'),
('s', 's'),
('s m-1', 's / m'),
('s-1', '1 / s'),
('s-2', '1 / s^2'),
('s m-1', 's m^-1'),
('s-1', 's^-1'),
('s-2', 's^-2'),
('uatm', 'uatm'),
('umolFe/m2/s', 'umolFe/m^2/s'),
('year', 'year'),
Expand All @@ -91,17 +91,17 @@


@pytest.mark.parametrize("test_input,expected", allunits)
def test_can_convert_SI_notation_to_slash_notation(test_input, expected):
u = to_slash_notation(test_input)
def test_can_convert_SI_notation_to_caret_notation(test_input, expected):
u = to_caret_notation(test_input)
assert u == expected

mixed_notation_to_slash = [
("mmolC/m2/d", "mmolC/m^2/d")
]

@pytest.mark.parametrize("test_input,expected", mixed_notation_to_slash)
def test_can_convert_mixed_notation_to_slash_notation(test_input, expected):
u = to_slash_notation(test_input)
def test_can_convert_mixed_notation_to_caret_notation(test_input, expected):
u = to_caret_notation(test_input)
assert u == expected

def test_can_convert_to_different_units():
Expand Down

0 comments on commit bc5fe66

Please sign in to comment.