Skip to content

Commit

Permalink
Merge branch 'develop' into domain_variables_cell_measures_coordinates
Browse files Browse the repository at this point in the history
  • Loading branch information
benjwadams authored Jan 16, 2025
2 parents dde12c8 + ce31014 commit 8e792bd
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ repos:


- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.8.1
rev: v0.8.6
hooks:
- id: ruff
args: ["--fix", "--show-fixes"]
Expand Down
48 changes: 47 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 @@ -414,6 +419,47 @@ def get_possible_label_variable_dimensions(variable: Variable) -> Tuple[int, ...
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

0 comments on commit 8e792bd

Please sign in to comment.