Skip to content

Commit

Permalink
don't use a netCDF object as index for caching
Browse files Browse the repository at this point in the history
  • Loading branch information
ocefpaf committed May 14, 2024
1 parent 1b2fabe commit 6b98a9d
Showing 1 changed file with 34 additions and 29 deletions.
63 changes: 34 additions & 29 deletions compliance_checker/cf/cf_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,11 +495,12 @@ def _find_aux_coord_vars(self, ds, refresh=False):
:return: List of variable names (str) that are defined to be auxiliary
coordinate variables.
"""
ds_str = ds.__str__()
if self._aux_coords.get(ds, None) and refresh is False:
return self._aux_coords[ds]
return self._aux_coords[ds_str]

Check warning on line 500 in compliance_checker/cf/cf_base.py

View check run for this annotation

Codecov / codecov/patch

compliance_checker/cf/cf_base.py#L500

Added line #L500 was not covered by tests

self._aux_coords[ds] = cfutil.get_auxiliary_coordinate_variables(ds)
return self._aux_coords[ds]
self._aux_coords[ds_str] = cfutil.get_auxiliary_coordinate_variables(ds)
return self._aux_coords[ds_str]

def _find_boundary_vars(self, ds, refresh=False):
"""
Expand All @@ -512,12 +513,13 @@ def _find_boundary_vars(self, ds, refresh=False):
:rtype: list
:return: A list containing strings with boundary variable names.
"""
ds_str = ds.__str__()
if self._boundary_vars.get(ds, None) and refresh is False:
return self._boundary_vars[ds]
return self._boundary_vars[ds_str]

Check warning on line 518 in compliance_checker/cf/cf_base.py

View check run for this annotation

Codecov / codecov/patch

compliance_checker/cf/cf_base.py#L518

Added line #L518 was not covered by tests

self._boundary_vars[ds] = cfutil.get_cell_boundary_variables(ds)
self._boundary_vars[ds_str] = cfutil.get_cell_boundary_variables(ds)

return self._boundary_vars[ds]
return self._boundary_vars[ds_str]

def _find_ancillary_vars(self, ds, refresh=False):
"""
Expand All @@ -541,26 +543,26 @@ def _find_ancillary_vars(self, ds, refresh=False):
:return: List of variable names (str) that are defined as ancillary
variables in the dataset ds.
"""

ds_str = ds.__str__()
# Used the cached version if it exists and is not empty
if self._ancillary_vars.get(ds, None) and refresh is False:
return self._ancillary_vars[ds]
return self._ancillary_vars[ds_str]

Check warning on line 549 in compliance_checker/cf/cf_base.py

View check run for this annotation

Codecov / codecov/patch

compliance_checker/cf/cf_base.py#L549

Added line #L549 was not covered by tests

# Invalidate the cache at all costs
self._ancillary_vars[ds] = []
self._ancillary_vars[ds_str] = []

for var in ds.variables.values():
if hasattr(var, "ancillary_variables"):
for anc_name in var.ancillary_variables.split(" "):
if anc_name in ds.variables:
self._ancillary_vars[ds].append(anc_name)
self._ancillary_vars[ds_str].append(anc_name)

if hasattr(var, "grid_mapping"):
gm_name = var.grid_mapping
if gm_name in ds.variables:
self._ancillary_vars[ds].append(gm_name)
self._ancillary_vars[ds_str].append(gm_name)

return self._ancillary_vars[ds]
return self._ancillary_vars[ds_str]

def _find_clim_vars(self, ds, refresh=False):
"""
Expand All @@ -573,15 +575,15 @@ def _find_clim_vars(self, ds, refresh=False):
:return: A list containing strings with geophysical variable
names.
"""

ds_str = ds.__str__()
if self._clim_vars.get(ds, None) and refresh is False:
return self._clim_vars[ds]
return self._clim_vars[ds_str]

Check warning on line 580 in compliance_checker/cf/cf_base.py

View check run for this annotation

Codecov / codecov/patch

compliance_checker/cf/cf_base.py#L580

Added line #L580 was not covered by tests

climatology_variable = cfutil.get_climatology_variable(ds)
if climatology_variable:
self._clim_vars[ds].append(climatology_variable)
self._clim_vars[ds_str].append(climatology_variable)

Check warning on line 584 in compliance_checker/cf/cf_base.py

View check run for this annotation

Codecov / codecov/patch

compliance_checker/cf/cf_base.py#L584

Added line #L584 was not covered by tests

return self._clim_vars[ds]
return self._clim_vars[ds_str]

def _find_cf_standard_name_table(self, ds):
"""
Expand Down Expand Up @@ -693,12 +695,13 @@ def _find_coord_vars(self, ds, refresh=False):
:return: A list of variables names (str) that are defined as coordinate
variables in the dataset ds.
"""
if ds in self._coord_vars and refresh is False:
return self._coord_vars[ds]
ds_str = ds.__str__()
if ds_str in self._coord_vars and refresh is False:
return self._coord_vars[ds_str]

self._coord_vars[ds] = cfutil.get_coordinate_variables(ds)
self._coord_vars[ds_str] = cfutil.get_coordinate_variables(ds)

return self._coord_vars[ds]
return self._coord_vars[ds_str]

def _find_geophysical_vars(self, ds, refresh=False):
"""
Expand All @@ -712,12 +715,13 @@ def _find_geophysical_vars(self, ds, refresh=False):
:return: A list containing strings with geophysical variable
names.
"""
ds_str = ds.__str__()
if self._geophysical_vars.get(ds, None) and refresh is False:
return self._geophysical_vars[ds]
return self._geophysical_vars[ds_str]

Check warning on line 720 in compliance_checker/cf/cf_base.py

View check run for this annotation

Codecov / codecov/patch

compliance_checker/cf/cf_base.py#L720

Added line #L720 was not covered by tests

self._geophysical_vars[ds] = cfutil.get_geophysical_variables(ds)
self._geophysical_vars[ds_str] = cfutil.get_geophysical_variables(ds)

return self._geophysical_vars[ds]
return self._geophysical_vars[ds_str]

def _find_metadata_vars(self, ds, refresh=False):
"""
Expand All @@ -731,10 +735,11 @@ def _find_metadata_vars(self, ds, refresh=False):
variable candidates.
"""
ds_str = ds.__str__()
if self._metadata_vars.get(ds, None) and refresh is False:
return self._metadata_vars[ds]
return self._metadata_vars[ds_str]

Check warning on line 740 in compliance_checker/cf/cf_base.py

View check run for this annotation

Codecov / codecov/patch

compliance_checker/cf/cf_base.py#L740

Added line #L740 was not covered by tests

self._metadata_vars[ds] = []
self._metadata_vars[ds_str] = []
for name, var in ds.variables.items():
if name in self._find_ancillary_vars(ds) or name in self._find_coord_vars(
ds,
Expand All @@ -749,17 +754,17 @@ def _find_metadata_vars(self, ds, refresh=False):
"platform_id",
"surface_altitude",
):
self._metadata_vars[ds].append(name)
self._metadata_vars[ds_str].append(name)

Check warning on line 757 in compliance_checker/cf/cf_base.py

View check run for this annotation

Codecov / codecov/patch

compliance_checker/cf/cf_base.py#L757

Added line #L757 was not covered by tests

elif getattr(var, "cf_role", "") != "":
self._metadata_vars[ds].append(name)
self._metadata_vars[ds_str].append(name)

elif (
getattr(var, "standard_name", None) is None and len(var.dimensions) == 0
):
self._metadata_vars[ds].append(name)
self._metadata_vars[ds_str].append(name)

return self._metadata_vars[ds]
return self._metadata_vars[ds_str]

def _get_coord_axis_map(self, ds):
"""
Expand Down

0 comments on commit 6b98a9d

Please sign in to comment.