From 51de8ceb35663c53bae36d9047f945f48838281b Mon Sep 17 00:00:00 2001 From: Jon Thielen Date: Thu, 9 Jan 2020 10:13:00 -0600 Subject: [PATCH] Add percent sign and UDUNITS exponent syntax support via Pint's new preprocessor option --- src/metpy/units.py | 13 ++++++++++++- tests/units/test_units.py | 15 +++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/metpy/units.py b/src/metpy/units.py index bbbb3895490..1059ef42a62 100644 --- a/src/metpy/units.py +++ b/src/metpy/units.py @@ -16,6 +16,7 @@ import functools from inspect import Parameter, signature import logging +import re import warnings import numpy as np @@ -27,7 +28,17 @@ UndefinedUnitError = pint.UndefinedUnitError DimensionalityError = pint.DimensionalityError -units = pint.UnitRegistry(autoconvert_offset_to_baseunit=True) +# Create registry, with preprocessors for UDUNITS-style powers (m2 s-2) and percent signs +try: + units = pint.UnitRegistry(autoconvert_offset_to_baseunit=True, + preprocessors=[functools.partial( + re.sub, + (r'(?<=[A-Za-z])(?![A-Za-z])(?