Skip to content

Commit

Permalink
fix lints
Browse files Browse the repository at this point in the history
  • Loading branch information
ocefpaf committed Aug 14, 2024
1 parent 1586401 commit 170bd2f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
4 changes: 2 additions & 2 deletions compliance_checker/cf/cf_1_6.py
Original file line number Diff line number Diff line change
Expand Up @@ -3340,7 +3340,7 @@ def check_packed_data(self, ds):

# IMPLEMENTATION CONFORMANCE 8.1 REQUIRED 1/3
# scale_factor and add_offset same type
if type(add_offset) != type(scale_factor):
if not isinstance(add_offset, type(scale_factor)):
valid = False
reasoning.append(
"Attributes add_offset and scale_factor have different data type.",
Expand All @@ -3350,7 +3350,7 @@ def check_packed_data(self, ds):
# if not the same type
# FIXME: Check add_offset too.

elif type(scale_factor) != var.dtype.type:
elif not isinstance(scale_factor, var.dtype.type):
# Check both attributes are type float or double
if not isinstance(scale_factor, (float, np.floating)):
valid = False
Expand Down
25 changes: 18 additions & 7 deletions compliance_checker/cf/cf_1_9.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,10 @@ def check_domain_variables(self, ds: Dataset):
continue
appendix_a_not_recommended_attrs = []
for attr_name in domain_var.ncattrs():
if attr_name in self.appendix_a and "D" not in self.appendix_a[attr_name]["attr_loc"]:
if (
attr_name in self.appendix_a
and "D" not in self.appendix_a[attr_name]["attr_loc"]
):
appendix_a_not_recommended_attrs.append(attr_name)

if appendix_a_not_recommended_attrs:
Expand All @@ -163,21 +166,29 @@ def check_domain_variables(self, ds: Dataset):
# no errors occurred
domain_valid.score += 1


# IMPLEMENTATION CONFORMANCE 5.8 REQUIRED 4/4
if hasattr(domain_var, "cell_measures"):
cell_measures_var_names = regex.findall(r"\b(?:area|volume):\s+(\w+)", domain_var.cell_measures)
cell_measures_var_names = regex.findall(
r"\b(?:area|volume):\s+(\w+)",
domain_var.cell_measures,
)
# check exist
for var_name in cell_measures_var_names:
try:
cell_measures_variable = ds.variables[var_name]
except ValueError:
# TODO: what to do here?
continue
domain_coord_var_names = {var_like.name for var_like in domain_coord_vars}
domain_valid.assert_true(set(cell_measures_variable.dimensions).issubset(domain_coord_var_names),
"Variables named in the cell_measures attributes must have a dimensions attribute with "
"values that are a subset of the referring domain variable's dimension attribute")
domain_coord_var_names = {
var_like.name for var_like in domain_coord_vars
}
domain_valid.assert_true(
set(cell_measures_variable.dimensions).issubset(
domain_coord_var_names,
),
"Variables named in the cell_measures attributes must have a dimensions attribute with "
"values that are a subset of the referring domain variable's dimension attribute",
)

results.append(domain_valid.to_result())

Expand Down
12 changes: 7 additions & 5 deletions compliance_checker/tests/test_cf.py
Original file line number Diff line number Diff line change
Expand Up @@ -3249,7 +3249,7 @@ def test_domain(self):
dataset.createDimension("lat", 20)
dataset.createDimension("depth", 20)
domain_var.setncattr("dimensions", "lon lat depth")
cube = dataset.createVariable("cube", "f8", ("lon", "lat", "depth"))
dataset.createVariable("cube", "f8", ("lon", "lat", "depth"))
# OK, coordinates in cell_measures are subset of coordinates of
# referring domain variable's coordinates attribute
results = self.cf.check_domain_variables(dataset)
Expand All @@ -3258,10 +3258,12 @@ def test_domain(self):
domain_var.cell_measures = "volume: cube_bad"
dataset.createVariable("cube_bad", "f8", ("lon", "lat", "depth", "time"))
results = self.cf.check_domain_variables(dataset)
self.assertTrue("Variables named in the cell_measures attributes must "
"have a dimensions attribute with values that are a "
"subset of the referring domain variable's dimension "
"attribute" in results[0].msgs)
self.assertTrue(
"Variables named in the cell_measures attributes must "
"have a dimensions attribute with values that are a "
"subset of the referring domain variable's dimension "
"attribute" in results[0].msgs,
)
del dataset
dataset = MockTimeSeries()
# domain should be dimensionless -- currently not an error in
Expand Down

0 comments on commit 170bd2f

Please sign in to comment.