Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Group search #1130

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading