Skip to content

Commit

Permalink
test pyudunits2
Browse files Browse the repository at this point in the history
  • Loading branch information
ocefpaf committed Nov 5, 2024
1 parent 6181a34 commit 70ded6b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
30 changes: 23 additions & 7 deletions compliance_checker/cf/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@
from importlib_resources import files
from lxml import etree
from netCDF4 import Dataset
from pyudunits2 import UnitSystem
from pyudunits2._exceptions import UnresolvableUnitException

from compliance_checker.cfutil import units_convertible

ut_system = UnitSystem.from_udunits2_xml()

# copied from paegan
# paegan may depend on these later
_possiblet = {
Expand Down Expand Up @@ -321,16 +325,27 @@ def create_cached_data_dir():

def units_known(units):
try:
# FIXME: pyudnits2 fails with dates.
# File "inline", line 1
# 'seconds since 1970-01-01T00:00:00Z'
# ^^^^^^^^
# SyntaxError: mismatched input ':' expecting <EOF>
# ut_system.unit(units)
Unit(units)
except ValueError:
except (ValueError, UnresolvableUnitException):
return False
return True


def units_temporal(units):
try:
# FIXME: pyudnits2 fails with dates.
# File "inline", line 1
# 'seconds since 1970-01-01T00:00:00Z'
# ^^^^^^^^
# SyntaxError: mismatched input ':' expecting <EOF>
u = Unit(units)
except ValueError:
except (ValueError, UnresolvableUnitException):
return False
# IMPLEMENTATION CONFORMANCE REQUIRED 4.4 1/3
# time units of a time_coordinate variable must contain a reference
Expand Down Expand Up @@ -403,21 +418,22 @@ def compare_unit_types(specified, reference):
msgs = []
err_flag = False
try:
specified_unit = Unit(specified)
except ValueError:
specified_unit = ut_system.unit(specified)
except UnresolvableUnitException:
msgs.append(f"Specified conversion unit f{specified} may not be valid UDUnits")
err_flag = True

try:
reference_unit = Unit(reference)
except ValueError:
reference_unit = ut_system.unit(reference)
except UnresolvableUnitException:
msgs.append(f"Specified conversion unit f{reference} may not be valid UDUnits")
err_flag = True

if err_flag:
return msgs

unit_convertible = specified_unit.is_convertible(reference_unit)
# FIXME: This one passed with the wrong syntax!! Our tests are probably not covering this!!!
unit_convertible = specified_unit.is_convertible_to(reference_unit)
fail_msg = [f'Units "{specified}" are not convertible to "{reference}"']
return msgs if unit_convertible else fail_msg

Expand Down
7 changes: 6 additions & 1 deletion compliance_checker/cfutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

from cf_units import Unit
from importlib_resources import files
from pyudunits2 import UnitSystem

ut_system = UnitSystem.from_udunits2_xml()

_UNITLESS_DB = None
_SEA_NAMES = None
Expand Down Expand Up @@ -111,7 +114,9 @@ def is_dimensionless_standard_name(standard_name_table, standard_name):
f".//entry[@id='{standard_name}']",
)
if found_standard_name is not None:
canonical_units = Unit(found_standard_name.find("canonical_units").text)
canonical_units = ut_system.unit(
found_standard_name.find("canonical_units").text,
)
return canonical_units.is_dimensionless()
# if the standard name is not found, assume we need units for the time being
else:
Expand Down

0 comments on commit 70ded6b

Please sign in to comment.