Skip to content

Commit

Permalink
make cf-units optional
Browse files Browse the repository at this point in the history
  • Loading branch information
ocefpaf committed Nov 12, 2024
1 parent cc630ca commit c4a1619
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 13 deletions.
45 changes: 34 additions & 11 deletions compliance_checker/cfutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
from functools import lru_cache, partial

from importlib_resources import files
from pyudunits2 import UnitSystem, UnresolvableUnitException

ut_system = UnitSystem.from_udunits2_xml()

_UNITLESS_DB = None
_SEA_NAMES = None
Expand Down Expand Up @@ -2032,30 +2029,56 @@ def guess_feature_type(nc, variable):
return "reduced-grid"


class _CCUnit:
def __init__(self, units):
import cf_units

self.u = cf_units.Unit(units)
self.is_time_reference = self.u.is_time_reference()

def is_convertible_to(self, other):
if isinstance(other, str):
ret = self.u.is_convertible(other)
else:
ret = self.u.is_convertible(other.u)
return ret

def is_dimensionless(self):
return self.u.is_dimensionless()

def expanded(self):
return self.u.definition


def _units(units: str):
"""PLACEHOLDER."""
# FIXME:
# cf_units will create:
# cf_units.Unit(None)
# Unit('unknown')
# that will contain all the methods we use here.
# FIXME: Try to make cf_units optional
try:
return _CCUnit(units)
except ImportError:
from pyudunits2 import UnitSystem, UnresolvableUnitException

ut_system = UnitSystem.from_udunits2_xml()

# FIXME: cf_units.Unit(None) -> Unit('unknown')
if units is None:
units = ""
# FIXME: Syntax Error when HH:MM:SS is present in time reference.
if "T00:00:00" in units:
units = units.replace("T00:00:00", "")

# FIXME: cf_units raised only ValueError
try:
u = ut_system.unit(units)
except (SyntaxError, UnresolvableUnitException) as err:
raise ValueError from err
# FIXME: cf_units defined .is_time_reference for time reference units.
u.is_time_reference = False
u.is_long_time_interval = False
try:
if hasattr(u._definition, "shift_from"):
u.is_time_reference = True
if u._definition.unit.content in ("months", "years"):
u.is_long_time_interval = True
except KeyError:
# FIXME: hasattr should return None in that case.
# pyudunits2/_expr_graph.py:27, in Node.__getattr__(self, name)
# 25 def __getattr__(self, name):
# 26 # Allow the dictionary to raise KeyError if the key doesn't exist.
Expand Down
4 changes: 4 additions & 0 deletions compliance_checker/tests/test_cf.py
Original file line number Diff line number Diff line change
Expand Up @@ -957,6 +957,10 @@ def test_latitude(self):
dataset = self.load_dataset(STATIC_FILES["rotated_pole_grid"])
results = self.cf.check_latitude(dataset)
scored, out_of, messages = get_results(results)
print("\nHERE")
print(f"{dataset['rlat']=}")
print(f"{messages=}")
print("HERE\n")
assert len(results) == 6
assert scored == out_of
assert (r.name == "§4.1 Latitude Coordinate" for r in results)
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ dynamic = [
"dependencies",
"version",
]
optional-dependencies.extras = [ "cf-units>=2" ]
optional-dependencies.readme = { file = "README.md", content-type = "text/markdown" }
urls.documentation = "https://ioos.github.io/compliance-checker"
urls.homepage = "https://compliance.ioos.us/index.html"
urls.repository = "https://github.com/ioos/compliance-checker"
Expand Down Expand Up @@ -81,7 +83,6 @@ compliance_checker = [
dependencies = { file = [
"requirements.txt",
] }
readme = { file = "README.md", content-type = "text/markdown" }

[tool.setuptools_scm]
write_to = "compliance_checker/_version.py"
Expand Down
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
cf-units>=2
cftime>=1.1.0
importlib-metadata
importlib-resources
Expand Down

0 comments on commit c4a1619

Please sign in to comment.