-
Notifications
You must be signed in to change notification settings - Fork 300
Connectivity manager #4017
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
Connectivity manager #4017
Changes from all commits
c14409f
f3d109e
5a46ea1
91a3ed6
487859f
5cb5588
bc8f423
20fbeed
6675cd8
1d279c5
97cfb6d
a564895
4a6ddb3
24c0a87
a39b26b
3ca9fa1
80fb8e0
a8897ab
f5be558
d9177cc
2744cf1
4cf57f0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -43,6 +43,9 @@ | |
|
|
||
|
|
||
| # https://www.unidata.ucar.edu/software/netcdf/docs/netcdf_data_set_components.html#object_name | ||
|
|
||
| from ..util import guess_coord_axis | ||
|
|
||
| _TOKEN_PARSE = re.compile(r"""^[a-zA-Z0-9][\w\.\+\-@]*$""") | ||
|
|
||
| # Configure the logger. | ||
|
|
@@ -1339,6 +1342,136 @@ def equal(self, other, lenient=None): | |
| return super().equal(other, lenient=lenient) | ||
|
|
||
|
|
||
| def metadata_filter( | ||
| instances, | ||
| item=None, | ||
| standard_name=None, | ||
| long_name=None, | ||
| var_name=None, | ||
| attributes=None, | ||
| axis=None, | ||
| ): | ||
| """ | ||
| Filter a collection of objects by their metadata to fit the given metadata | ||
| criteria. Criteria be one or both of: specific properties / other objects | ||
| carrying metadata to be matched. | ||
|
|
||
| Args: | ||
|
|
||
| * instances | ||
| An iterable of objects to be filtered. | ||
|
|
||
| Kwargs: | ||
|
|
||
| * item | ||
| Either | ||
|
|
||
| (a) a :attr:`standard_name`, :attr:`long_name`, or | ||
| :attr:`var_name`. Defaults to value of `default` | ||
| (which itself defaults to `unknown`) as defined in | ||
| :class:`~iris.common.CFVariableMixin`. | ||
|
|
||
| (b) a 'coordinate' instance with metadata equal to that of | ||
| the desired coordinates. Accepts either a | ||
| :class:`~iris.coords.DimCoord`, :class:`~iris.coords.AuxCoord`, | ||
| :class:`~iris.aux_factory.AuxCoordFactory`, | ||
| :class:`~iris.common.CoordMetadata` or | ||
| :class:`~iris.common.DimCoordMetadata` or | ||
| :class:`~iris.experimental.ugrid.ConnectivityMetadata`. | ||
| * standard_name | ||
| The CF standard name of the desired coordinate. If None, does not | ||
| check for standard name. | ||
| * long_name | ||
| An unconstrained description of the coordinate. If None, does not | ||
| check for long_name. | ||
| * var_name | ||
| The netCDF variable name of the desired coordinate. If None, does | ||
| not check for var_name. | ||
| * attributes | ||
| A dictionary of attributes desired on the coordinates. If None, | ||
| does not check for attributes. | ||
| * axis | ||
| The desired coordinate axis, see | ||
| :func:`~iris.util.guess_coord_axis`. If None, does not check for | ||
| axis. Accepts the values 'X', 'Y', 'Z' and 'T' (case-insensitive). | ||
|
|
||
| Returns: | ||
| A list of the objects supplied in the ``instances`` argument, limited | ||
| to only those that matched the given criteria. | ||
|
|
||
| """ | ||
| name = None | ||
| obj = None | ||
|
|
||
| if isinstance(item, str): | ||
| name = item | ||
| else: | ||
| obj = item | ||
|
|
||
| result = instances | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to ensure that this is an iterable, now that it's a public API function. |
||
|
|
||
| if name is not None: | ||
| result = [instance for instance in result if instance.name() == name] | ||
|
|
||
| if standard_name is not None: | ||
| result = [ | ||
| instance | ||
| for instance in result | ||
| if instance.standard_name == standard_name | ||
| ] | ||
|
|
||
| if long_name is not None: | ||
| result = [ | ||
| instance for instance in result if instance.long_name == long_name | ||
| ] | ||
|
|
||
| if var_name is not None: | ||
| result = [ | ||
| instance for instance in result if instance.var_name == var_name | ||
| ] | ||
|
|
||
| if attributes is not None: | ||
| if not isinstance(attributes, Mapping): | ||
| msg = ( | ||
| "The attributes keyword was expecting a dictionary " | ||
| "type, but got a %s instead." % type(attributes) | ||
| ) | ||
| raise ValueError(msg) | ||
|
|
||
| def attr_filter(instance): | ||
| return all( | ||
| k in instance.attributes | ||
| and hexdigest(instance.attributes[k]) == hexdigest(v) | ||
| for k, v in attributes.items() | ||
| ) | ||
|
|
||
| result = [instance for instance in result if attr_filter(instance)] | ||
|
|
||
| if axis is not None: | ||
| axis = axis.upper() | ||
| result = [ | ||
| instance | ||
| for instance in result | ||
| if guess_coord_axis(instance) == axis | ||
| ] | ||
|
Comment on lines
+1450
to
+1456
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to check whether the instance actually has an |
||
|
|
||
| if obj is not None: | ||
| if hasattr(obj, "__class__") and issubclass( | ||
| obj.__class__, BaseMetadata | ||
| ): | ||
| target_metadata = obj | ||
| else: | ||
| target_metadata = obj.metadata | ||
|
|
||
| result = [ | ||
| instance | ||
| for instance in result | ||
| if instance.metadata == target_metadata | ||
| ] | ||
|
|
||
| return result | ||
|
|
||
|
|
||
| def metadata_manager_factory(cls, **kwargs): | ||
| """ | ||
| A class instance factory function responsible for manufacturing | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change to
Criteria be one->Criteria can be one