Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
HGWright committed Oct 25, 2024
1 parent dc247e7 commit 989c9ec
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 14 deletions.
43 changes: 33 additions & 10 deletions lib/iris/fileformats/cf.py
Original file line number Diff line number Diff line change
Expand Up @@ -1418,16 +1418,19 @@ def _translate(self):

for cf_var in formula_terms.values():
for cf_root, cf_term in cf_var.cf_terms_by_root.items():


# Ignore formula terms owned by a bounds variable.
if cf_root not in self.cf_group.bounds:
cf_name = cf_var.cf_name
if cf_var.cf_name not in self.cf_group:
self.cf_group[cf_name] = CFAuxiliaryCoordinateVariable(
cf_name, cf_var.cf_data
)
self.cf_group[cf_name].add_formula_term(cf_root, cf_term)
# if cf_root not in self.cf_group.bounds:
# cf_name = cf_var.cf_name
# if cf_var.cf_name not in self.cf_group:
# self.cf_group[cf_name] = CFAuxiliaryCoordinateVariable(
# cf_name, cf_var.cf_data
# )
# self.cf_group[cf_name].add_formula_term(cf_root, cf_term)

if cf_root not in self.cf_group.bounds:

# Check if cf_root has a bounds attribute.
if cf_root in self.cf_group.coordinates:
# Need to generalise this for if it's a dim or aux coord.
Expand All @@ -1440,7 +1443,7 @@ def _translate(self):
)
form_terms = form_terms.replace(":", "")
form_terms = form_terms.split(" ")
example_dict = {"a": "A", "b": "B", "ps": "PS", "p0": "P0"}
example_dict = {"a": "A", "b": "B", "ps": "PS", "p0": "P0", "orog": "orography"}
for cf_vari in formula_terms.values():
for (
cf_roots,
Expand All @@ -1458,16 +1461,29 @@ def _translate(self):
to_attach_to.lower()
!= attach_from.lower()
):
cf_var.bounds = cf_vari
print(cf_vari.bounds)
self.cf_group[cf_vari.cf_name] = CFBoundaryVariable(cf_vari.cf_name, cf_vari.cf_data)
cf_var.bounds = cf_vari.cf_name

if cf_root not in self.cf_group.bounds:
cf_name = cf_var.cf_name
if cf_var.cf_name not in self.cf_group:
new_var = CFAuxiliaryCoordinateVariable(
cf_name, cf_var.cf_data
)
if hasattr(cf_var, "bounds"):
new_var.bounds = cf_var.bounds
self.cf_group[cf_name] = new_var
self.cf_group[cf_name].add_formula_term(cf_root, cf_term)

# Determine the CF data variables.
data_variable_names = (
set(netcdf_variable_names) - self.cf_group.non_data_variable_names
)
print("name")

for name in data_variable_names:
self.cf_group[name] = CFDataVariable(name, self._dataset.variables[name])
print("name")

def _build_cf_groups(self):
"""Build the first order relationships between CF-netCDF variables."""
Expand Down Expand Up @@ -1523,6 +1539,13 @@ def _build(cf_variable):
category=iris.warnings.IrisCfNonSpanningVarWarning,
)

if hasattr(cf_variable, "bounds"):
if cf_variable.bounds not in cf_group:
bounds_var = self.cf_group[cf_variable.bounds]
# TODO: warning if fails spans
if bounds_var.spans(cf_variable):
cf_group[bounds_var.cf_name] = bounds_var

# Build CF data variable relationships.
if isinstance(cf_variable, CFDataVariable):
# Add global netCDF attributes.
Expand Down
10 changes: 6 additions & 4 deletions lib/iris/tests/unit/fileformats/cf/test_CFReader.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ def netcdf_variable(
standard_name=standard_name,
**{name: None for name in ugrid_identities},
)
if bounds is None:
del ncvar.bounds
return ncvar


Expand All @@ -91,9 +93,9 @@ def test_create_global_attributes(self):

class Test_translate__formula_terms(tests.IrisTest):
def setUp(self):
self.delta = netcdf_variable("delta", "height", np.float64, bounds="delta_bnds")
self.delta = netcdf_variable("delta", "height", np.float64)
self.delta_bnds = netcdf_variable("delta_bnds", "height bnds", np.float64)
self.sigma = netcdf_variable("sigma", "height", np.float64, bounds="sigma_bnds")
self.sigma = netcdf_variable("sigma", "height", np.float64)
self.sigma_bnds = netcdf_variable("sigma_bnds", "height bnds", np.float64)
self.orography = netcdf_variable("orography", "lat lon", np.float64)
formula_terms = "a: delta b: sigma orog: orography"
Expand Down Expand Up @@ -186,9 +188,9 @@ def test_create_formula_terms(self):

class Test_build_cf_groups__formula_terms(tests.IrisTest):
def setUp(self):
self.delta = netcdf_variable("delta", "height", np.float64, bounds="delta_bnds")
self.delta = netcdf_variable("delta", "height", np.float64)
self.delta_bnds = netcdf_variable("delta_bnds", "height bnds", np.float64)
self.sigma = netcdf_variable("sigma", "height", np.float64, bounds="sigma_bnds")
self.sigma = netcdf_variable("sigma", "height", np.float64)
self.sigma_bnds = netcdf_variable("sigma_bnds", "height bnds", np.float64)
self.orography = netcdf_variable("orography", "lat lon", np.float64)
formula_terms = "a: delta b: sigma orog: orography"
Expand Down

0 comments on commit 989c9ec

Please sign in to comment.