@@ -9141,7 +9141,12 @@ def domain_mask(self, **kwargs):
91419141 @_inplace_enabled(default=False)
91429142 @_manage_log_level_via_verbosity
91439143 def compute_vertical_coordinates(
9144- self, default_to_zero=True, strict=True, inplace=False, verbose=None
9144+ self,
9145+ default_to_zero=True,
9146+ strict=True,
9147+ key=False,
9148+ inplace=False,
9149+ verbose=None,
91459150 ):
91469151 """Compute non-parametric vertical coordinates.
91479152
@@ -9176,7 +9181,7 @@ def compute_vertical_coordinates(
91769181 {{default_to_zero: `bool`, optional}}
91779182
91789183 strict: `bool`
9179- If False then allow the computation to occur when
9184+ If False then allow the computation to occur when:
91809185
91819186 * A domain ancillary construct has no standard name, but
91829187 the corresponding term has a standard name that is
@@ -9195,15 +9200,30 @@ def compute_vertical_coordinates(
91959200 names, then an exception is raised regardless of the value
91969201 of *strict*.
91979202
9203+ key: `bool`
9204+ If True, return alongside the field construct the key
9205+ identifying the auxiliary coordinate of the field with
9206+ the newly-computed vertical coordinates, as a 2-tuple
9207+ of field and then key. If False, the default, then only
9208+ return the field construct.
9209+
9210+ If no coordinates were computed, `None` will be
9211+ returned in the key (second) position of the 2-tuple.
9212+
9213+ .. versionadded:: 3.17.0
9214+
91989215 {{inplace: `bool`, optional}}
91999216
92009217 {{verbose: `int` or `str` or `None`, optional}}
92019218
92029219 :Returns:
92039220
9204- `Field` or `None`
9221+ `Field`, 2-tuple, or `None`
92059222 The field construct with the new non-parametric vertical
9206- coordinates, or `None` if the operation was in-place.
9223+ coordinates, or a 2-tuple of this field construct along
9224+ with the key of the new auxiliary coordinate with the
9225+ computed vertical coordinates, or `None` if the operation
9226+ was in-place.
92079227
92089228 **Examples**
92099229
@@ -9230,7 +9250,7 @@ def compute_vertical_coordinates(
92309250 >>> print(f.auxiliary_coordinate('altitude', default=None))
92319251 None
92329252 >>> g = f.compute_vertical_coordinates()
9233- >>> print(g.auxiliary_coordinates)
9253+ >>> print(g.auxiliary_coordinates() )
92349254 Constructs:
92359255 {'auxiliarycoordinate0': <CF AuxiliaryCoordinate: latitude(10, 9) degrees_N>,
92369256 'auxiliarycoordinate1': <CF AuxiliaryCoordinate: longitude(9, 10) degrees_E>,
@@ -9246,12 +9266,35 @@ def compute_vertical_coordinates(
92469266 Bounds:units = 'm'
92479267 Bounds:Data(1, 10, 9, 2) = [[[[5.0, ..., 5415.0]]]] m
92489268
9269+ >>> g, key = f.compute_vertical_coordinates(key=True)
9270+ >>> g
9271+ <CF Field: air_temperature(atmosphere_hybrid_height_coordinate(1), grid_latitude(10), grid_longitude(9)) K>
9272+ >>> key
9273+ 'auxiliarycoordinate3'
9274+
9275+ >>> i = f.compute_vertical_coordinates(inplace=True)
9276+ >>> print(i)
9277+ None
9278+ >>> print(f.auxiliary_coordinates())
9279+ Constructs:
9280+ {'auxiliarycoordinate0': <CF AuxiliaryCoordinate: latitude(10, 9) degrees_N>,
9281+ 'auxiliarycoordinate1': <CF AuxiliaryCoordinate: longitude(9, 10) degrees_E>,
9282+ 'auxiliarycoordinate2': <CF AuxiliaryCoordinate: long_name=Grid latitude name(10) >,
9283+ 'auxiliarycoordinate3': <CF AuxiliaryCoordinate: altitude(1, 10, 9) m>}
9284+
92499285 """
92509286 f = _inplace_enabled_define_and_cleanup(self)
92519287
9288+ if inplace and key:
9289+ raise ValueError(
9290+ "Can't set both key=True and inplace=True, since inplace "
9291+ "will always do the operation in-place and return None."
9292+ )
9293+
92529294 detail = is_log_level_detail(logger)
92539295 debug = is_log_level_debug(logger)
92549296
9297+ return_key = None # in case there are no vertical coords to compute
92559298 for cr in f.coordinate_references(todict=True).values():
92569299 # --------------------------------------------------------
92579300 # Compute the non-parametric vertical coordinates, if
@@ -9293,20 +9336,25 @@ def compute_vertical_coordinates(
92939336 f"{c.dump(display=False, _level=1)}"
92949337 ) # pragma: no cover
92959338
9296- key = f.set_construct(c, axes=computed_axes, copy=False)
9339+ return_key = f.set_construct(c, axes=computed_axes, copy=False)
92979340
92989341 # Reference the new coordinates from the coordinate
92999342 # reference construct
9300- cr.set_coordinate(key )
9343+ cr.set_coordinate(return_key )
93019344
93029345 if debug:
93039346 logger.debug(
9304- f"Non-parametric coordinates construct key: {key!r}\n"
9347+ "Non-parametric coordinates construct key: "
9348+ f"{return_key!r}\n"
93059349 "Updated coordinate reference construct:\n"
93069350 f"{cr.dump(display=False, _level=1)}"
93079351 ) # pragma: no cover
93089352
9309- return f
9353+ if key:
9354+ # 2-tuple, where return_key will be None if nothing was computed
9355+ return f, return_key
9356+ else:
9357+ return f
93109358
93119359 def match_by_construct(self, *identities, OR=False, **conditions):
93129360 """Whether or not there are particular metadata constructs.
0 commit comments