Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Coord and Cube attributes printout #4528

Merged
merged 4 commits into from
Jan 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions docs/src/further_topics/lenient_maths.rst
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ represents the output of an low-resolution global atmospheric ``experiment``,
forecast_reference_time 2009-09-09 17:10:00
time 2009-09-09 17:10:00
Attributes:
Conventions CF-1.5
Conventions 'CF-1.5'
STASH m01s00i004
experiment-id RT3 50
source Data from Met Office Unified Model 7.04
experiment-id 'RT3 50'
source 'Data from Met Office Unified Model 7.04'

Consider also the following :class:`~iris.cube.Cube`, which has the same global
spatial extent, and acts as a ``control``,
Expand All @@ -103,9 +103,9 @@ spatial extent, and acts as a ``control``,
model_level_number 1
time 2009-09-09 17:10:00
Attributes:
Conventions CF-1.7
Conventions 'CF-1.7'
STASH m01s00i004
source Data from Met Office Unified Model 7.04
source 'Data from Met Office Unified Model 7.04'

Now let's subtract these cubes in order to calculate a simple ``difference``,

Expand All @@ -129,8 +129,8 @@ Now let's subtract these cubes in order to calculate a simple ``difference``,
forecast_reference_time 2009-09-09 17:10:00
time 2009-09-09 17:10:00
Attributes:
experiment-id RT3 50
source Data from Met Office Unified Model 7.04
experiment-id 'RT3 50'
source 'Data from Met Office Unified Model 7.04'

Note that, cube maths automatically takes care of broadcasting the
dimensionality of the ``control`` up to that of the ``experiment``, in order to
Expand Down Expand Up @@ -218,7 +218,7 @@ time perform **strict** cube maths instead,
Scalar coordinates:
time 2009-09-09 17:10:00
Attributes:
source Data from Met Office Unified Model 7.04
source 'Data from Met Office Unified Model 7.04'

Although the numerical result of this strict cube maths operation is identical,
it is not as rich in metadata as the :ref:`lenient alternative <lenient example>`.
Expand Down
6 changes: 3 additions & 3 deletions docs/src/further_topics/metadata.rst
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,10 @@ For example, given the following :class:`~iris.cube.Cube`,
Cell methods:
mean time (6 hour)
Attributes:
Conventions CF-1.5
Model scenario A1B
Conventions 'CF-1.5'
Model scenario 'A1B'
STASH m01s03i236
source Data from Met Office Unified Model 6.05
source 'Data from Met Office Unified Model 6.05'

We can easily get all of the associated metadata of the :class:`~iris.cube.Cube`
using the ``metadata`` property:
Expand Down
6 changes: 3 additions & 3 deletions docs/src/userguide/cube_maths.rst
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ but with the data representing their difference:
forecast_reference_time 1859-09-01 06:00:00
height 1.5 m
Attributes:
Conventions CF-1.5
Model scenario E1
source Data from Met Office Unified Model 6.05
Conventions 'CF-1.5'
Model scenario 'E1'
source 'Data from Met Office Unified Model 6.05'


.. note::
Expand Down
14 changes: 7 additions & 7 deletions docs/src/userguide/cube_statistics.rst
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ For instance, suppose we have a cube:
forecast_reference_time 2009-11-19 04:00:00
Attributes:
STASH m01s00i004
source Data from Met Office Unified Model
um_version 7.3
source 'Data from Met Office Unified Model'
um_version '7.3'


In this case we have a 4 dimensional cube;
Expand Down Expand Up @@ -84,8 +84,8 @@ we can pass the coordinate name and the aggregation definition to the
mean model_level_number
Attributes:
STASH m01s00i004
source Data from Met Office Unified Model
um_version 7.3
source 'Data from Met Office Unified Model'
um_version '7.3'


Similarly other analysis operators such as ``MAX``, ``MIN`` and ``STD_DEV``
Expand Down Expand Up @@ -143,8 +143,8 @@ These areas can now be passed to the ``collapsed`` method as weights:
mean grid_longitude, grid_latitude
Attributes:
STASH m01s00i004
source Data from Met Office Unified Model
um_version 7.3
source 'Data from Met Office Unified Model'
um_version '7.3'

Several examples of area averaging exist in the gallery which may be of interest,
including an example on taking a :ref:`global area-weighted mean
Expand Down Expand Up @@ -229,7 +229,7 @@ Printing this cube now shows that two extra coordinates exist on the cube:
Cell methods:
mean month, year
Attributes:
Conventions CF-1.5
Conventions 'CF-1.5'
STASH m01s00i024


Expand Down
4 changes: 2 additions & 2 deletions docs/src/userguide/interpolation_and_regridding.rst
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ Let's take the air temperature cube we've seen previously:
mean over years time
Attributes:
STASH m01s16i203
source Data from Met Office Unified Model
source 'Data from Met Office Unified Model'

We can interpolate specific values from the coordinates of the cube:

Expand All @@ -98,7 +98,7 @@ We can interpolate specific values from the coordinates of the cube:
mean over years time
Attributes:
STASH m01s16i203
source Data from Met Office Unified Model
source 'Data from Met Office Unified Model'

As we can see, the resulting cube is scalar and has longitude and latitude coordinates with
the values defined in our sample points.
Expand Down
4 changes: 2 additions & 2 deletions docs/src/userguide/iris_cubes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,8 @@ output as this is the quickest way of inspecting the contents of a cube. Here is
forecast_reference_time 2009-11-19 04:00:00
Attributes:
STASH m01s00i004
source Data from Met Office Unified Model
um_version 7.3
source 'Data from Met Office Unified Model'
um_version '7.3'


Using this output we can deduce that:
Expand Down
4 changes: 2 additions & 2 deletions docs/src/userguide/loading_iris_cubes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ list indexing can be used:
forecast_reference_time 2009-11-19 04:00:00
Attributes:
STASH m01s00i004
source Data from Met Office Unified Model
um_version 7.3
source 'Data from Met Office Unified Model'
um_version '7.3'

Notice that the result of printing a **cube** is a little more verbose than
it was when printing a **list of cubes**. In addition to the very short summary
Expand Down
8 changes: 4 additions & 4 deletions docs/src/userguide/navigating_a_cube.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ We have already seen a basic string representation of a cube when printing:
forecast_reference_time 2006-06-15 00:00:00
time 2006-06-15 00:00:00
Attributes:
Conventions CF-1.5
Conventions 'CF-1.5'
STASH m01s16i222
source Data from Met Office Unified Model 6.01
source 'Data from Met Office Unified Model 6.01'


This representation is equivalent to passing the cube to the :func:`str` function. This function can be used on
Expand Down Expand Up @@ -169,9 +169,9 @@ We can add and remove coordinates via :func:`Cube.add_dim_coord<iris.cube.Cube.a
my_custom_coordinate 1
time 2006-06-15 00:00:00
Attributes:
Conventions CF-1.5
Conventions 'CF-1.5'
STASH m01s16i222
source Data from Met Office Unified Model 6.01
source 'Data from Met Office Unified Model 6.01'


The coordinate ``my_custom_coordinate`` now exists on the cube and is listed under the non-dimensioned single valued scalar coordinates.
Expand Down
6 changes: 3 additions & 3 deletions docs/src/userguide/subsetting_a_cube.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ A subset of a cube can be "extracted" from a multi-dimensional cube in order to
Scalar coordinates:
grid_latitude 0.0 degrees
Attributes:
Conventions CF-1.5
Conventions 'CF-1.5'


In this example we start with a 3 dimensional cube, with dimensions of ``height``, ``grid_latitude`` and ``grid_longitude``,
Expand Down Expand Up @@ -97,8 +97,8 @@ same way as loading with constraints:
time 2009-11-19 10:00:00
Attributes:
STASH m01s00i004
source Data from Met Office Unified Model
um_version 7.3
source 'Data from Met Office Unified Model'
um_version '7.3'


Cube Iteration
Expand Down
2 changes: 1 addition & 1 deletion lib/iris/_representation/cube_summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ def __init__(self, title, attributes):
self.values = []
self.contents = []
for name, value in sorted(attributes.items()):
value = value_repr(value)
value = value_repr(value, quote_strings=True)
pp-mo marked this conversation as resolved.
Show resolved Hide resolved
value = iris.util.clip_string(value)
self.names.append(name)
self.values.append(value)
Expand Down
40 changes: 20 additions & 20 deletions lib/iris/common/resolve.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,10 @@ class Resolve:
Cell methods:
mean time (6 hour)
Attributes:
Conventions CF-1.5
Model scenario A1B
Conventions 'CF-1.5'
Model scenario 'A1B'
STASH m01s03i236
source Data from Met Office Unified Model 6.05
source 'Data from Met Office Unified Model 6.05'

>>> print(cube2)
air_temperature / (K) (longitude: 49; latitude: 37)
Expand All @@ -130,10 +130,10 @@ class Resolve:
Cell methods:
mean time (6 hour)
Attributes:
Conventions CF-1.5
Model scenario E1
Conventions 'CF-1.5'
Model scenario 'E1'
STASH m01s03i236
source Data from Met Office Unified Model 6.05
source 'Data from Met Office Unified Model 6.05'

>>> print(data.shape)
(240, 37, 49)
Expand All @@ -153,9 +153,9 @@ class Resolve:
Cell methods:
mean time (6 hour)
Attributes:
Conventions CF-1.5
Conventions 'CF-1.5'
STASH m01s03i236
source Data from Met Office Unified Model 6.05
source 'Data from Met Office Unified Model 6.05'

Secondly, creating an *empty* ``resolver`` instance, that may be called *multiple*
times with *different* :class:`~iris.cube.Cube` operands and *different* ``data``,
Expand Down Expand Up @@ -2413,10 +2413,10 @@ def mapped(self):
Cell methods:
mean time (6 hour)
Attributes:
Conventions CF-1.5
Model scenario A1B
Conventions 'CF-1.5'
Model scenario 'A1B'
STASH m01s03i236
source Data from Met Office Unified Model 6.05
source 'Data from Met Office Unified Model 6.05'
>>> print(cube2)
air_temperature / (K) (longitude: 49; latitude: 37)
Dimension coordinates:
Expand All @@ -2430,10 +2430,10 @@ def mapped(self):
Cell methods:
mean time (6 hour)
Attributes:
Conventions CF-1.5
Model scenario E1
Conventions 'CF-1.5'
Model scenario 'E1'
STASH m01s03i236
source Data from Met Office Unified Model 6.05
source 'Data from Met Office Unified Model 6.05'
>>> Resolve().mapped is None
True
>>> resolver = Resolve(cube1, cube2)
Expand Down Expand Up @@ -2481,10 +2481,10 @@ def shape(self):
Cell methods:
mean time (6 hour)
Attributes:
Conventions CF-1.5
Model scenario A1B
Conventions 'CF-1.5'
Model scenario 'A1B'
STASH m01s03i236
source Data from Met Office Unified Model 6.05
source 'Data from Met Office Unified Model 6.05'
>>> print(cube2)
air_temperature / (K) (longitude: 49; latitude: 37)
Dimension coordinates:
Expand All @@ -2498,10 +2498,10 @@ def shape(self):
Cell methods:
mean time (6 hour)
Attributes:
Conventions CF-1.5
Model scenario E1
Conventions 'CF-1.5'
Model scenario 'E1'
STASH m01s03i236
source Data from Met Office Unified Model 6.05
source 'Data from Met Office Unified Model 6.05'
>>> Resolve().shape is None
True
>>> Resolve(cube1, cube2).shape
Expand Down
23 changes: 19 additions & 4 deletions lib/iris/coords.py
Original file line number Diff line number Diff line change
Expand Up @@ -571,10 +571,25 @@ def reindent_data_string(text, n_indent):
# work for all those defined so far.
show = val is not None and val is not False
if show:
# add a section for this property (metadata item)
# TODO: modify to do multi-line attribute output
add_output(newline_indent)
add_output(f"{name}: {val!r}", section=name)
if name == "attributes":
# Use a multi-line form for this.
add_output(newline_indent)
add_output("attributes:", section="attributes")
max_attname_len = max(len(attr) for attr in val.keys())
for attrname, attrval in val.items():
attrname = attrname.ljust(max_attname_len)
if isinstance(attrval, str):
# quote strings
attrval = repr(attrval)
# and abbreviate really long ones
attrval = iris.util.clip_string(attrval)
attr_string = f"{attrname} {attrval}"
pp-mo marked this conversation as resolved.
Show resolved Hide resolved
add_output(newline_indent + indent + attr_string)
else:
# add a one-line section for this property
# (aka metadata field)
add_output(newline_indent)
add_output(f"{name}: {val!r}", section=name)

return "\n".join(output_lines)

Expand Down
14 changes: 7 additions & 7 deletions lib/iris/cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,7 @@ class Cube(CFVariableMixin):
mean over years time
Attributes:
STASH m01s16i203
source Data from Met Office Unified Model
source 'Data from Met Office Unified Model'


See the :doc:`user guide</userguide/index>` for more information.
Expand Down Expand Up @@ -3636,7 +3636,7 @@ def collapsed(self, coords, aggregator, **kwargs):
mean month, year
mean longitude
Attributes:
Conventions CF-1.5
Conventions 'CF-1.5'
STASH m01s00i024


Expand Down Expand Up @@ -3871,7 +3871,7 @@ def aggregated_by(self, coords, aggregator, **kwargs):
mean month, year
mean year
Attributes:
Conventions CF-1.5
Conventions 'CF-1.5'
STASH m01s00i024

"""
Expand Down Expand Up @@ -4076,8 +4076,8 @@ def rolling_window(self, coord, aggregator, window, **kwargs):
Attributes:
STASH m01s00i024
source \
Data from Met Office Unified Model
um_version 7.6
'Data from Met Office Unified Model'
um_version '7.6'


>>> print(air_press.rolling_window('time', iris.analysis.MEAN, 3))
Expand All @@ -4102,8 +4102,8 @@ def rolling_window(self, coord, aggregator, window, **kwargs):
Attributes:
STASH m01s00i024
source \
Data from Met Office Unified Model
um_version 7.6
'Data from Met Office Unified Model'
um_version '7.6'

Notice that the forecast_period dimension now represents the 4
possible windows of size 3 from the original cube.
Expand Down
Loading