Skip to content

Commit

Permalink
PI-3478: Common metadata API (SciTools#3583)
Browse files Browse the repository at this point in the history
* common metadata api

* rationalise _cube_coord_common into common

* move state into metadata

* MetadataFactory test coverage

* temporarily pin back iris-grib

* test coverage for iris.common.metadata._BaseMeta

* test coverage for iris.common.mixin.LimitedAttributeDict

* remove temporary iris-grib pin

* review actions

* Update lib/iris/tests/unit/common/metadata/test_BaseMetadata.py

Co-Authored-By: lbdreyer <[email protected]>
  • Loading branch information
bjlittle and lbdreyer committed Aug 13, 2020
1 parent d16f676 commit 6e581b4
Show file tree
Hide file tree
Showing 35 changed files with 2,112 additions and 727 deletions.
4 changes: 4 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,7 @@ exclude =
# ignore third-party files
#
gitwash_dumper.py,
#
# convenience imports
#
lib/iris/common/__init__.py
12 changes: 6 additions & 6 deletions lib/iris/_concatenate.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class _CoordMetaData(
Args:
* defn:
The :class:`iris.coords.CoordDefn` metadata that represents a
The :class:`iris.common.CoordMetadata` metadata that represents a
coordinate.
* dims:
Expand All @@ -85,7 +85,7 @@ class _CoordMetaData(
"""

def __new__(cls, coord, dims):
def __new__(mcs, coord, dims):
"""
Create a new :class:`_CoordMetaData` instance.
Expand All @@ -101,7 +101,7 @@ def __new__(cls, coord, dims):
The new class instance.
"""
defn = coord._as_defn()
defn = coord.metadata
points_dtype = coord.points.dtype
bounds_dtype = coord.bounds.dtype if coord.bounds is not None else None
kwargs = {}
Expand All @@ -120,7 +120,7 @@ def __new__(cls, coord, dims):
order = _DECREASING
kwargs["order"] = order
metadata = super().__new__(
cls, defn, dims, points_dtype, bounds_dtype, kwargs
mcs, defn, dims, points_dtype, bounds_dtype, kwargs
)
return metadata

Expand Down Expand Up @@ -403,11 +403,11 @@ def __init__(self, cube):
axes = dict(T=0, Z=1, Y=2, X=3)

# Coordinate sort function - by guessed coordinate axis, then
# by coordinate definition, then by dimensions, in ascending order.
# by coordinate metadata, then by dimensions, in ascending order.
def key_func(coord):
return (
axes.get(guess_coord_axis(coord), len(axes) + 1),
coord._as_defn(),
coord.metadata,
cube.coord_dims(coord),
)

Expand Down
2 changes: 1 addition & 1 deletion lib/iris/_constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def _coordless_match(self, cube):
if self._name:
# Require to also check against cube.name() for the fallback
# "unknown" default case, when there is no name metadata available.
match = self._name in cube.names or self._name == cube.name()
match = self._name in cube._names or self._name == cube.name()
if match and self._cube_func:
match = self._cube_func(cube)
return match
Expand Down
23 changes: 11 additions & 12 deletions lib/iris/_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
is_lazy_data,
multidim_lazy_stack,
)
import iris.cube
import iris.coords
from iris.common import CoordMetadata, CubeMetadata
import iris.cube
import iris.exceptions
import iris.util

Expand Down Expand Up @@ -115,7 +116,7 @@ class _ScalarCoordPayload(
Args:
* defns:
A list of scalar coordinate definitions :class:`iris.coords.CoordDefn`
A list of scalar coordinate metadata :class:`iris.common.CoordMetadata`
belonging to a :class:`iris.cube.Cube`.
* values:
Expand Down Expand Up @@ -1478,9 +1479,7 @@ def axis_and_name(name):
)
else:
bounds = None
kwargs = dict(
zip(iris.coords.CoordDefn._fields, defns[name])
)
kwargs = dict(zip(CoordMetadata._fields, defns[name]))
kwargs.update(metadata[name].kwargs)

def name_in_independents():
Expand Down Expand Up @@ -1560,7 +1559,7 @@ def name_in_independents():
if bounds is not None:
bounds[index] = name_value.bound

kwargs = dict(zip(iris.coords.CoordDefn._fields, defns[name]))
kwargs = dict(zip(CoordMetadata._fields, defns[name]))
self._aux_templates.append(
_Template(dims, points, bounds, kwargs)
)
Expand Down Expand Up @@ -1594,7 +1593,7 @@ def _get_cube(self, data):
(deepcopy(coord), dims)
for coord, dims in self._aux_coords_and_dims
]
kwargs = dict(zip(iris.cube.CubeMetadata._fields, signature.defn))
kwargs = dict(zip(CubeMetadata._fields, signature.defn))

cms_and_dims = [
(deepcopy(cm), dims) for cm, dims in self._cell_measures_and_dims
Expand Down Expand Up @@ -1794,7 +1793,7 @@ def _extract_coord_payload(self, cube):

# Coordinate sort function.
# NB. This makes use of two properties which don't end up in
# the CoordDefn used by scalar_defns: `coord.points.dtype` and
# the metadata used by scalar_defns: `coord.points.dtype` and
# `type(coord)`.
def key_func(coord):
points_dtype = coord.dtype
Expand All @@ -1805,14 +1804,14 @@ def key_func(coord):
axis_dict.get(
iris.util.guess_coord_axis(coord), len(axis_dict) + 1
),
coord._as_defn(),
coord.metadata,
)

# Order the coordinates by hints, axis, and definition.
for coord in sorted(coords, key=key_func):
if not cube.coord_dims(coord) and coord.shape == (1,):
# Extract the scalar coordinate data and metadata.
scalar_defns.append(coord._as_defn())
scalar_defns.append(coord.metadata)
# Because we know there's a single Cell in the
# coordinate, it's quicker to roll our own than use
# Coord.cell().
Expand Down Expand Up @@ -1844,14 +1843,14 @@ def key_func(coord):

factory_defns = []
for factory in sorted(
cube.aux_factories, key=lambda factory: factory._as_defn()
cube.aux_factories, key=lambda factory: factory.metadata
):
dependency_defns = []
dependencies = factory.dependencies
for key in sorted(dependencies):
coord = dependencies[key]
if coord is not None:
dependency_defns.append((key, coord._as_defn()))
dependency_defns.append((key, coord.metadata))
factory_defn = _FactoryDefn(type(factory), dependency_defns)
factory_defns.append(factory_defn)

Expand Down
2 changes: 1 addition & 1 deletion lib/iris/analysis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ def _dimensional_metadata_comparison(*cubes, object_get=None):
eq = (
other_coord is coord
or other_coord.name() == coord.name()
and other_coord._as_defn() == coord._as_defn()
and other_coord.metadata == coord.metadata
)
if eq:
coord_to_add_to_group = other_coord
Expand Down
Loading

0 comments on commit 6e581b4

Please sign in to comment.