Skip to content

Commit

Permalink
Adding a check for ISTP variable name compliance (#259)
Browse files Browse the repository at this point in the history
  • Loading branch information
bryan-harter authored May 14, 2024
1 parent 4244172 commit 762fa87
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions cdflib/xarray/xarray_to_cdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@
52: np.str_(" "),
}

# Regular expression to match valid ISTP variable/attribute names
ISTP_COMPLIANT_NAME = re.compile(r"^[A-Za-z][A-Za-z0-9_]*$")


class ISTPError(Exception):
"""
Expand Down Expand Up @@ -468,6 +471,32 @@ def _verify_monotonically_increasing(epoch_data: npt.NDArray) -> np.bool_:
return np.all(epoch_data[1:] > epoch_data[:-1])


def _validate_varatt_names(dataset: xr.Dataset, terminate_on_warning: bool) -> None:
for var_name in dataset.variables:
if not ISTP_COMPLIANT_NAME.match(str(var_name)):
_warn_or_except(
f"Invalid ISTP variable name: {str(var_name)}",
terminate_on_warning,
)

# Check attributes in the dataset
for attr_name in dataset.attrs:
if not ISTP_COMPLIANT_NAME.match(str(attr_name)):
_warn_or_except(
f"Invalid ISTP global attribute name: {str(attr_name)}",
terminate_on_warning,
)

# Check attributes of each variable
for var in dataset.variables:
for attr_name in dataset[var].attrs:
if not ISTP_COMPLIANT_NAME.match(str(attr_name)):
_warn_or_except(
f"Invalid ISTP variable attribute name: {str(attr_name)}",
terminate_on_warning,
)


def _add_depend_variables_to_dataset(
dataset: xr.Dataset,
dim_vars: List[str],
Expand Down Expand Up @@ -992,6 +1021,9 @@ def xarray_to_cdf(
_convert_nans_to_fillval(dataset)

if istp:
# This checks all the variable and attribute names to ensure they are ISTP compliant.
_validate_varatt_names(dataset, terminate_on_warning)

# This creates a list of suspected or confirmed label variables
_label_checker(dataset, terminate_on_warning)

Expand Down

0 comments on commit 762fa87

Please sign in to comment.