@@ -9514,7 +9514,12 @@ def domain_mask(self, **kwargs):
95149514 @_inplace_enabled(default=False)
95159515 @_manage_log_level_via_verbosity
95169516 def compute_vertical_coordinates(
9517- self, default_to_zero=True, strict=True, inplace=False, verbose=None
9517+ self,
9518+ default_to_zero=True,
9519+ strict=True,
9520+ key=False,
9521+ inplace=False,
9522+ verbose=None,
95189523 ):
95199524 """Compute non-parametric vertical coordinates.
95209525
@@ -9549,7 +9554,7 @@ def compute_vertical_coordinates(
95499554 {{default_to_zero: `bool`, optional}}
95509555
95519556 strict: `bool`
9552- If False then allow the computation to occur when
9557+ If False then allow the computation to occur when:
95539558
95549559 * A domain ancillary construct has no standard name, but
95559560 the corresponding term has a standard name that is
@@ -9568,15 +9573,30 @@ def compute_vertical_coordinates(
95689573 names, then an exception is raised regardless of the value
95699574 of *strict*.
95709575
9576+ key: `bool`
9577+ If True, return alongside the field construct the key
9578+ identifying the auxiliary coordinate of the field with
9579+ the newly-computed vertical coordinates, as a 2-tuple
9580+ of field and then key. If False, the default, then only
9581+ return the field construct.
9582+
9583+ If no coordinates were computed, `None` will be
9584+ returned in the key (second) position of the 2-tuple.
9585+
9586+ .. versionadded:: 3.17.0
9587+
95719588 {{inplace: `bool`, optional}}
95729589
95739590 {{verbose: `int` or `str` or `None`, optional}}
95749591
95759592 :Returns:
95769593
9577- `Field` or `None`
9594+ `Field`, 2-tuple, or `None`
95789595 The field construct with the new non-parametric vertical
9579- coordinates, or `None` if the operation was in-place.
9596+ coordinates, or a 2-tuple of this field construct along
9597+ with the key of the new auxiliary coordinate with the
9598+ computed vertical coordinates, or `None` if the operation
9599+ was in-place.
95809600
95819601 **Examples**
95829602
@@ -9603,7 +9623,7 @@ def compute_vertical_coordinates(
96039623 >>> print(f.auxiliary_coordinate('altitude', default=None))
96049624 None
96059625 >>> g = f.compute_vertical_coordinates()
9606- >>> print(g.auxiliary_coordinates)
9626+ >>> print(g.auxiliary_coordinates() )
96079627 Constructs:
96089628 {'auxiliarycoordinate0': <CF AuxiliaryCoordinate: latitude(10, 9) degrees_N>,
96099629 'auxiliarycoordinate1': <CF AuxiliaryCoordinate: longitude(9, 10) degrees_E>,
@@ -9619,12 +9639,35 @@ def compute_vertical_coordinates(
96199639 Bounds:units = 'm'
96209640 Bounds:Data(1, 10, 9, 2) = [[[[5.0, ..., 5415.0]]]] m
96219641
9642+ >>> g, key = f.compute_vertical_coordinates(key=True)
9643+ >>> g
9644+ <CF Field: air_temperature(atmosphere_hybrid_height_coordinate(1), grid_latitude(10), grid_longitude(9)) K>
9645+ >>> key
9646+ 'auxiliarycoordinate3'
9647+
9648+ >>> i = f.compute_vertical_coordinates(inplace=True)
9649+ >>> print(i)
9650+ None
9651+ >>> print(f.auxiliary_coordinates())
9652+ Constructs:
9653+ {'auxiliarycoordinate0': <CF AuxiliaryCoordinate: latitude(10, 9) degrees_N>,
9654+ 'auxiliarycoordinate1': <CF AuxiliaryCoordinate: longitude(9, 10) degrees_E>,
9655+ 'auxiliarycoordinate2': <CF AuxiliaryCoordinate: long_name=Grid latitude name(10) >,
9656+ 'auxiliarycoordinate3': <CF AuxiliaryCoordinate: altitude(1, 10, 9) m>}
9657+
96229658 """
96239659 f = _inplace_enabled_define_and_cleanup(self)
96249660
9661+ if inplace and key:
9662+ raise ValueError(
9663+ "Can't set both key=True and inplace=True, since inplace "
9664+ "will always do the operation in-place and return None."
9665+ )
9666+
96259667 detail = is_log_level_detail(logger)
96269668 debug = is_log_level_debug(logger)
96279669
9670+ return_key = None # in case there are no vertical coords to compute
96289671 for cr in f.coordinate_references(todict=True).values():
96299672 # --------------------------------------------------------
96309673 # Compute the non-parametric vertical coordinates, if
@@ -9666,20 +9709,25 @@ def compute_vertical_coordinates(
96669709 f"{c.dump(display=False, _level=1)}"
96679710 ) # pragma: no cover
96689711
9669- key = f.set_construct(c, axes=computed_axes, copy=False)
9712+ return_key = f.set_construct(c, axes=computed_axes, copy=False)
96709713
96719714 # Reference the new coordinates from the coordinate
96729715 # reference construct
9673- cr.set_coordinate(key )
9716+ cr.set_coordinate(return_key )
96749717
96759718 if debug:
96769719 logger.debug(
9677- f"Non-parametric coordinates construct key: {key!r}\n"
9720+ "Non-parametric coordinates construct key: "
9721+ f"{return_key!r}\n"
96789722 "Updated coordinate reference construct:\n"
96799723 f"{cr.dump(display=False, _level=1)}"
96809724 ) # pragma: no cover
96819725
9682- return f
9726+ if key:
9727+ # 2-tuple, where return_key will be None if nothing was computed
9728+ return f, return_key
9729+ else:
9730+ return f
96839731
96849732 def match_by_construct(self, *identities, OR=False, **conditions):
96859733 """Whether or not there are particular metadata constructs.
0 commit comments