diff --git a/compliance_checker/cf/util.py b/compliance_checker/cf/util.py index 9443eb33..03bdefe1 100644 --- a/compliance_checker/cf/util.py +++ b/compliance_checker/cf/util.py @@ -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 = { @@ -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 + # 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 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 @@ -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 diff --git a/compliance_checker/cfutil.py b/compliance_checker/cfutil.py index 8bcce331..d45f63c9 100644 --- a/compliance_checker/cfutil.py +++ b/compliance_checker/cfutil.py @@ -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 @@ -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: