Skip to content

Commit

Permalink
Factor out cube.summary dim-naming + remove code for (nonexistent) mu…
Browse files Browse the repository at this point in the history
…lti-dim dim-coords.
  • Loading branch information
pp-mo committed Oct 26, 2020
1 parent 1938e14 commit 6bbf0f5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 34 deletions.
46 changes: 29 additions & 17 deletions lib/iris/cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -2263,35 +2263,46 @@ def _summary_extra(self, coords, summary, indent):
new_summary.append(extra)
return new_summary

def summary(self, shorten=False, name_padding=35):
def _summary_dim_name(self, dim):
"""
Unicode string summary of the Cube with name, a list of dim coord names
versus length and optionally relevant coordinate information.
Get the dim_coord name that labels a data dimension,
or "--" if there is none (i.e. dimension is anonymous).
"""
# Create a set to contain the axis names for each data dimension.
dim_names = [set() for dim in range(len(self.shape))]
dim_coords = self.coords(contains_dimension=dim, dim_coords=True)

# Add the dim_coord names that participate in the associated data
# dimensions.
for dim in range(len(self.shape)):
dim_coords = self.coords(contains_dimension=dim, dim_coords=True)
if dim_coords:
dim_names[dim].add(dim_coords[0].name())
else:
dim_names[dim].add("-- ")
if len(dim_coords) == 1:
# Only 1 dimension coordinate can map to a dimension.
name = dim_coords[0].name() # N.B. can only be 1
elif len(dim_coords) == 0:
# Anonymous dimension : NOTE this has an extra space on the end.
name = "-- "
else:
# >1 matching dim coord : This really can't happen !
names_list = [co.name() for co in dim_coords]
msg = (
"Cube logic error : "
"multiple dimension coords map to "
"dimension {dim} : {names!r}"
)
raise ValueError(msg.format(dim=dim, names=names_list))

# Convert axes sets to lists and sort.
dim_names = [sorted(names, key=sorted_axes) for names in dim_names]
return name

def summary(self, shorten=False, name_padding=35):
"""
Unicode string summary of the Cube with name, a list of dim coord names
versus length and optionally relevant coordinate information.
"""
# Generate textual summary of the cube dimensionality.
if self.shape == ():
dimension_header = "scalar cube"
else:
dimension_header = "; ".join(
[
", ".join(dim_names[dim]) + ": %d" % dim_shape
for dim, dim_shape in enumerate(self.shape)
self._summary_dim_name(dim) + ": %d" % dim_len
for dim, dim_len in enumerate(self.shape)
]
)

Expand All @@ -2301,6 +2312,7 @@ def summary(self, shorten=False, name_padding=35):
cube_header = "{nameunit!s:{length}} ({dimension})".format(
length=name_padding, nameunit=nameunit, dimension=dimension_header
)

summary = ""

# Generate full cube textual summary.
Expand Down
25 changes: 8 additions & 17 deletions lib/iris/experimental/representation.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,25 +117,16 @@ def __init__(self, cube):

def _get_dim_names(self):
"""
Get dimension-describing coordinate names, or '--' if no coordinate]
describes the dimension.
Note: borrows from `cube.summary`.
Get all the dimension-describing coordinate names, as used by
cube.summary.
"""
# Create a set to contain the axis names for each data dimension.
dim_names = list(range(len(self.cube.shape)))

# Add the dim_coord names that participate in the associated data
# dimensions.
for dim in range(len(self.cube.shape)):
dim_coords = self.cube.coords(
contains_dimension=dim, dim_coords=True
)
if dim_coords:
dim_names[dim] = dim_coords[0].name()
else:
dim_names[dim] = "--"
# NOTE: use ".strip()" on these, because the name of an anonymous dim
# has an unwanted extra space at the end, i.e. "-- ".
dim_names = [
self.cube._summary_dim_name(dim).strip()
for dim in range(len(self.cube.shape))
]
return dim_names

def _dim_names(self):
Expand Down

0 comments on commit 6bbf0f5

Please sign in to comment.