Skip to content

Commit

Permalink
Merge pull request #1130 from benjwadams/group_search
Browse files Browse the repository at this point in the history
Group search
  • Loading branch information
benjwadams authored Jan 16, 2025
2 parents 4b1c9a2 + a234b4c commit ce31014
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 14 deletions.
59 changes: 58 additions & 1 deletion compliance_checker/cf/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@
from importlib.resources import files
from pkgutil import get_data

from functools import lru_cache
import requests
from cf_units import Unit
from lxml import etree
from netCDF4 import Dataset
from netCDF4 import Variable, Dimension, Dataset, Group

import posixpath

from typing import Tuple, Union

from compliance_checker.cfutil import units_convertible

Expand Down Expand Up @@ -405,6 +410,58 @@ def string_from_var_type(variable):
)


def get_possible_label_variable_dimensions(variable: Variable) -> Tuple[int, ...]:
"""
Return dimensions if non-char variable, or return variable dimensions
without trailing dimension if char variable, treating it as a label variable.
"""
if variable.kind == "C" and len(variable.dimensions) > 0:
return variable.dimensions[:-1]
return variable.dimensions


@lru_cache()
def maybe_lateral_reference_variable_or_dimension(group: Union[Group, Dataset],
name: str,
reference_type: Union[Variable, Dimension]):

def can_lateral_search(name):
return (not name.startswith(".") and posixpath.split(name)[0] == "")

if reference_type == "variable":
# first try to fetch any
# can't set to None with .get
try:
maybe_var = group[name]
except IndexError:
maybe_var = None
else:
if isinstance(maybe_var, Variable):
return maybe_var

# alphanumeric string by itself, not a relative or absolute
# search by proximity
if (posixpath.split(name)[0] == "" and
not (name.startswith(".") or name.startswith("/"))):
group_traverse = group
while group_traverse.parent:
group_traverse = group_traverse.parent
check_target = posixpath.join(group_traverse.path, name)
try:
maybe_var = group_traverse[name]
except IndexError:
maybe_var = None
else:
if isinstance(maybe_var, Variable):
return maybe_var
else:
return VariableReferenceError(name)

# can't find path relative to current group or absolute path
# perform lateral search if we aren't in the root group



def reference_attr_variables(
dataset: Dataset,
attributes_string: str,
Expand Down
16 changes: 3 additions & 13 deletions compliance_checker/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,22 +148,12 @@ def stdout_output(cls, cs, score_dict, verbose, limit):
"""

for ds, score_groups in score_dict.items():
for checker, rpair in score_groups.items():
groups, errors = rpair
for checker, (groups, errors) in score_groups.items():
score_list, points, out_of = cs.standard_output(
ds,
limit,
checker,
groups,
)
ds, limit, checker, groups)
# send list of grouped result objects to stdout & reasoning_routine
cs.standard_output_generation(
groups,
limit,
points,
out_of,
check=checker,
)
groups, limit, points, out_of, check=checker)
return groups

@classmethod
Expand Down

0 comments on commit ce31014

Please sign in to comment.