From 191502df575189a1689a5a70beac8d1773065556 Mon Sep 17 00:00:00 2001 From: Filipe Fernandes Date: Wed, 6 Nov 2024 20:37:13 +0100 Subject: [PATCH] remove last bit of cf_units --- compliance_checker/cf/cf_1_6.py | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/compliance_checker/cf/cf_1_6.py b/compliance_checker/cf/cf_1_6.py index 3979777e..c1727387 100644 --- a/compliance_checker/cf/cf_1_6.py +++ b/compliance_checker/cf/cf_1_6.py @@ -5,7 +5,7 @@ import cftime import numpy as np import regex -from cf_units import Unit +from pyudunits2 import UnresolvableUnitException from compliance_checker import cfutil from compliance_checker.base import BaseCheck, Result, TestCtx @@ -812,8 +812,8 @@ def _check_valid_cf_units(self, ds, variable_name): ) try: - units_conv = Unit(units) - except ValueError: + units_conv = util._units(units) + except (ValueError, UnresolvableUnitException): valid_units.messages.append( f'Unit string "{units}" is not recognized by UDUnits', ) @@ -828,7 +828,7 @@ def _check_valid_cf_units(self, ds, variable_name): # being expressed as "s"/seconds if standard_name not in {"time", "forecast_reference_time"}: valid_units.assert_true( - units_conv.is_convertible(Unit(reference)), + units_conv.is_convertible_to(util._units(reference)), f'Units "{units}" for variable ' f"{variable_name} must be convertible to " f'canonical units "{reference}"', @@ -1494,7 +1494,8 @@ def check_latitude(self, ds): # check that the units aren't in east and north degrees units, # but are convertible to angular units allowed_units.assert_true( - units not in e_n_units and Unit(units) == Unit("degree"), + units not in e_n_units + and util._units(units) == util._units("degree"), f"Grid latitude variable '{latitude}' should use degree equivalent units without east or north components. " f"Current units are {units}", ) @@ -1603,7 +1604,8 @@ def check_longitude(self, ds): # check that the units aren't in east and north degrees units, # but are convertible to angular units allowed_units.assert_true( - units not in e_n_units and Unit(units) == Unit("degree"), + units not in e_n_units + and cfutil._units(units) == cfutil._units("degree"), f"Grid longitude variable '{longitude}' should use degree equivalent units without east or north components. " f"Current units are {units}", ) @@ -1864,8 +1866,7 @@ def check_time_coordinate(self, ds): ret_val.append(result) # IMPLEMENTATION CONFORMANCE 4.4 RECOMMENDED 2/2 # catch non-recommended months or years time interval - unit = Unit(variable.units) - if unit.is_long_time_interval(): + if any(unit in variable.units for unit in ("months", "years")): message = f"Using relative time interval of months or years is not recommended for coordinate variable {variable.name}" result = Result( BaseCheck.MEDIUM, @@ -2844,12 +2845,14 @@ def _cell_measures_core(self, ds, var, external_set, variable_template): f'cell_methods attribute with a measure type of "{cell_measure_type}".' ) try: - cell_measure_units = Unit(cell_measure_var.units) + cell_measure_units = cfutil._units(cell_measure_var.units) except ValueError: valid = False reasoning.append(conversion_failure_msg) else: - if not cell_measure_units.is_convertible(Unit(f"m{exponent}")): + if not cell_measure_units.is_convertible_to( + cfutil._units(f"m{exponent}"), + ): valid = False reasoning.append(conversion_failure_msg) if not set(cell_measure_var.dimensions).issubset(var.dimensions): @@ -3042,7 +3045,7 @@ def _check_cell_methods_paren_info(self, paren_contents, var): # then the units try: - Unit(interval_matches.group("interval_units")) + cfutil._units(interval_matches.group("interval_units")) except ValueError: valid_info.messages.append( 'ยง7.3.3 {}:cell_methods interval units "{}" is not parsable by UDUNITS.'.format(