1414from .metadata import *
1515from .mixin import *
1616from .resolve import *
17+ from ..util import guess_coord_axis
1718
1819
1920def filter_cf (
@@ -23,10 +24,17 @@ def filter_cf(
2324 long_name = None ,
2425 var_name = None ,
2526 attributes = None ,
27+ axis = None ,
2628):
2729 """
28- Filter a list of :class:`iris.common.CFVariableMixin` subclasses to fit
29- the given criteria.
30+ Filter a collection of objects by their metadata to fit the given metadata
31+ criteria. Criteria be one or both of: specific properties / other objects
32+ carrying metadata to be matched.
33+
34+ Args:
35+
36+ * instances
37+ An iterable of objects to be filtered.
3038
3139 Kwargs:
3240
@@ -36,15 +44,15 @@ def filter_cf(
3644 (a) a :attr:`standard_name`, :attr:`long_name`, or
3745 :attr:`var_name`. Defaults to value of `default`
3846 (which itself defaults to `unknown`) as defined in
39- :class:`iris.common.CFVariableMixin`.
47+ :class:`~ iris.common.CFVariableMixin`.
4048
4149 (b) a 'coordinate' instance with metadata equal to that of
4250 the desired coordinates. Accepts either a
43- :class:`iris.coords.DimCoord`, :class:`iris.coords.AuxCoord`,
44- :class:`iris.aux_factory.AuxCoordFactory`,
45- :class:`iris.common.CoordMetadata` or
46- :class:`iris.common.DimCoordMetadata` or
47- :class:`iris.experimental.ugrid.ConnectivityMetadata`.
51+ :class:`~ iris.coords.DimCoord`, :class:`~ iris.coords.AuxCoord`,
52+ :class:`~ iris.aux_factory.AuxCoordFactory`,
53+ :class:`~ iris.common.CoordMetadata` or
54+ :class:`~ iris.common.DimCoordMetadata` or
55+ :class:`~ iris.experimental.ugrid.ConnectivityMetadata`.
4856 * standard_name
4957 The CF standard name of the desired coordinate. If None, does not
5058 check for standard name.
@@ -57,40 +65,44 @@ def filter_cf(
5765 * attributes
5866 A dictionary of attributes desired on the coordinates. If None,
5967 does not check for attributes.
68+ * axis
69+ The desired coordinate axis, see
70+ :func:`~iris.util.guess_coord_axis`. If None, does not check for
71+ axis. Accepts the values 'X', 'Y', 'Z' and 'T' (case-insensitive).
72+
73+ Returns:
74+ A list of the objects supplied in the ``instances`` argument, limited
75+ to only those that matched the given criteria.
6076
6177 """
6278 name = None
63- instance = None
79+ obj = None
6480
6581 if isinstance (item , str ):
6682 name = item
6783 else :
68- instance = item
84+ obj = item
6985
7086 result = instances
7187
7288 if name is not None :
73- result = [
74- instance_ for instance_ in result if instance_ .name () == name
75- ]
89+ result = [instance for instance in result if instance .name () == name ]
7690
7791 if standard_name is not None :
7892 result = [
79- instance_
80- for instance_ in result
81- if instance_ .standard_name == standard_name
93+ instance
94+ for instance in result
95+ if instance .standard_name == standard_name
8296 ]
8397
8498 if long_name is not None :
8599 result = [
86- instance_
87- for instance_ in result
88- if instance_ .long_name == long_name
100+ instance for instance in result if instance .long_name == long_name
89101 ]
90102
91103 if var_name is not None :
92104 result = [
93- instance_ for instance_ in result if instance_ .var_name == var_name
105+ instance for instance in result if instance .var_name == var_name
94106 ]
95107
96108 if attributes is not None :
@@ -101,28 +113,36 @@ def filter_cf(
101113 )
102114 raise ValueError (msg )
103115
104- def attr_filter (instance_ ):
116+ def attr_filter (instance ):
105117 return all (
106- k in instance_ .attributes
107- and metadata ._hexdigest (instance_ .attributes [k ])
118+ k in instance .attributes
119+ and metadata ._hexdigest (instance .attributes [k ])
108120 == metadata ._hexdigest (v )
109121 for k , v in attributes .items ()
110122 )
111123
112- result = [instance_ for instance_ in result if attr_filter (instance_ )]
124+ result = [instance for instance in result if attr_filter (instance )]
125+
126+ if axis is not None :
127+ axis = axis .upper ()
128+ result = [
129+ instance
130+ for instance in result
131+ if guess_coord_axis (instance ) == axis
132+ ]
113133
114- if instance is not None :
115- if hasattr (instance , "__class__" ) and issubclass (
116- instance .__class__ , BaseMetadata
134+ if obj is not None :
135+ if hasattr (obj , "__class__" ) and issubclass (
136+ obj .__class__ , BaseMetadata
117137 ):
118- target_metadata = instance
138+ target_metadata = obj
119139 else :
120- target_metadata = instance .metadata
140+ target_metadata = obj .metadata
121141
122142 result = [
123- instance_
124- for instance_ in result
125- if instance_ .metadata == target_metadata
143+ instance
144+ for instance in result
145+ if instance .metadata == target_metadata
126146 ]
127147
128148 return result
0 commit comments