Skip to content

Commit

Permalink
Merge pull request #342 from davidhassell/aggregate-ncdim
Browse files Browse the repository at this point in the history
Fixes setting both relaxed_identities=True and ncvar_identities=True in `cf.aggregate`
  • Loading branch information
davidhassell committed Mar 10, 2022
2 parents 4f1be43 + 6cd6d97 commit 7469d58
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 26 deletions.
46 changes: 20 additions & 26 deletions cf/aggregate.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ def __init__(
self.has_data = f.has_data()
self.identity = f.identity(
strict=strict_identities,
relaxed=relaxed_identities,
relaxed=relaxed_identities and not ncvar_identities,
nc_only=ncvar_identities,
default=None,
)
Expand Down Expand Up @@ -309,7 +309,7 @@ def __init__(
else:
self.coordrefs = list(refs.values())

for axis in f.domain_axes(todict=True):
for axis, domain_axis in f.domain_axes(todict=True).items():

# List some information about each 1-d coordinate which
# spans this axis. The order of elements is arbitrary, as
Expand Down Expand Up @@ -400,36 +400,30 @@ def __init__(
# auxiliary coordinate information
info_1d_coord = info_dim + info_aux

# if not info_1d_coord:
# self.message = ("axis has no one-dimensional nor "
# "scalar coordinates")
# return

# Find the canonical identity for this axis
identity = None
if info_1d_coord:
identity = info_1d_coord[0]["identity"]
elif not self.relaxed_identities:
self.message = (
"axis has no one-dimensional nor " "scalar coordinates"
"axis has no one-dimensional nor scalar coordinates"
)
return

ncdim = False
size = None
if identity is None and self.relaxed_identities:
# There are no 1-d coordinates, so see if we can
# identify the domain axis by their netCDF dimension
# name.
domain_axis = f.axis(axis) # TODO
# There are no 1-d coordinates and relaxed identities
# are on, so see if we can identify the domain axis by
# its netCDF dimension name.
identity = domain_axis.nc_get_dimension(None)
if identity is None:
self.message = (
f"axis {f.constructs.domain_axis_identity(axis)!r} "
"has no netCDF dimension name"
) # TODO
return
else:
ncdim = True

size = domain_axis.get_size()

axis_identities = {
"ids": "identity",
Expand All @@ -438,7 +432,6 @@ def __init__(
"hasdata": "hasdata",
"hasbounds": "hasbounds",
"coordrefs": "coordrefs",
# 'size': None,
}
self.axis[identity] = {
name: tuple(i[idt] for i in info_1d_coord)
Expand All @@ -450,11 +443,10 @@ def __init__(
else:
self.axis[identity]["dim_coord_index"] = None

# Store the axis size if the axis has no 1-d coordinates
if ncdim:
self.axis[identity]["size"] = domain_axis.get_size()
else:
self.axis[identity]["size"] = None
# Store the axis size, which will be None unless we
# identified the dimension solely by its netCDF dimension
# name.
self.axis[identity]["size"] = size

self.id_to_axis[identity] = axis
self.axis_to_id[axis] = identity
Expand Down Expand Up @@ -933,7 +925,7 @@ def coord_has_identity_and_data(self, coord, axes=None):
"""
identity = coord.identity(
strict=self.strict_identities,
relaxed=self.relaxed_identities,
relaxed=self.relaxed_identities and not self.ncvar_identities,
nc_only=self.ncvar_identities,
default=None,
)
Expand Down Expand Up @@ -973,7 +965,7 @@ def field_ancillary_has_identity_and_data(self, anc):
"""
identity = anc.identity(
strict=self.strict_identities,
relaxed=self.relaxed_identities,
relaxed=self.relaxed_identities and not self.ncvar_identities,
nc_only=self.ncvar_identities,
default=None,
)
Expand Down Expand Up @@ -1055,7 +1047,7 @@ def domain_ancillary_has_identity_and_data(self, anc, identity=None):
else:
anc_identity = anc.identity(
strict=self.strict_identities,
relaxed=self.relaxed_identities,
relaxed=self.relaxed_identities and not self.ncvar_identities,
nc_only=self.ncvar_identities,
default=None,
)
Expand Down Expand Up @@ -1459,7 +1451,9 @@ def aggregate(
relaxed_identities: `bool`, optional
If True and there is no standard name property nor "id"
attribute, then allow field and metadata constructs to be
identifiable by long name properties or netCDF variable names.
identifiable by long name properties or netCDF variable
names. Also allows netCDF dimension names to be used when
there are no spanning 1-d coordinates.
field_identity: `str`, optional
Specify a property with which to identify field constructs
Expand Down Expand Up @@ -1877,7 +1871,7 @@ def aggregate(

coord_identity = coord.identity(
strict=strict_identities,
relaxed=relaxed_identities,
relaxed=relaxed_identities and not ncvar_identities,
nc_only=ncvar_identities,
default=None,
)
Expand Down
8 changes: 8 additions & 0 deletions cf/test/test_aggregate.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,14 @@ def test_aggregate_dimension(self):
a = a[0]
self.assertEqual(len(a.auxiliary_coordinates()), 1)

def test_aggregate_keyword_consistency(self):
"""Test acceptable keyword combinations."""
f = cf.example_field(0)
a = cf.aggregate(
[f[:2], f[2:]], relaxed_identities=True, ncvar_identities=True
)
self.assertEqual(len(a), 1)


if __name__ == "__main__":
print("Run date:", datetime.datetime.now())
Expand Down

0 comments on commit 7469d58

Please sign in to comment.