@@ -9141,7 +9141,12 @@ def domain_mask(self, **kwargs):
9141
9141
@_inplace_enabled(default=False)
9142
9142
@_manage_log_level_via_verbosity
9143
9143
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,
9145
9150
):
9146
9151
"""Compute non-parametric vertical coordinates.
9147
9152
@@ -9176,7 +9181,7 @@ def compute_vertical_coordinates(
9176
9181
{{default_to_zero: `bool`, optional}}
9177
9182
9178
9183
strict: `bool`
9179
- If False then allow the computation to occur when
9184
+ If False then allow the computation to occur when:
9180
9185
9181
9186
* A domain ancillary construct has no standard name, but
9182
9187
the corresponding term has a standard name that is
@@ -9195,15 +9200,30 @@ def compute_vertical_coordinates(
9195
9200
names, then an exception is raised regardless of the value
9196
9201
of *strict*.
9197
9202
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
+
9198
9215
{{inplace: `bool`, optional}}
9199
9216
9200
9217
{{verbose: `int` or `str` or `None`, optional}}
9201
9218
9202
9219
:Returns:
9203
9220
9204
- `Field` or `None`
9221
+ `Field`, 2-tuple, or `None`
9205
9222
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.
9207
9227
9208
9228
**Examples**
9209
9229
@@ -9230,7 +9250,7 @@ def compute_vertical_coordinates(
9230
9250
>>> print(f.auxiliary_coordinate('altitude', default=None))
9231
9251
None
9232
9252
>>> g = f.compute_vertical_coordinates()
9233
- >>> print(g.auxiliary_coordinates)
9253
+ >>> print(g.auxiliary_coordinates() )
9234
9254
Constructs:
9235
9255
{'auxiliarycoordinate0': <CF AuxiliaryCoordinate: latitude(10, 9) degrees_N>,
9236
9256
'auxiliarycoordinate1': <CF AuxiliaryCoordinate: longitude(9, 10) degrees_E>,
@@ -9246,12 +9266,35 @@ def compute_vertical_coordinates(
9246
9266
Bounds:units = 'm'
9247
9267
Bounds:Data(1, 10, 9, 2) = [[[[5.0, ..., 5415.0]]]] m
9248
9268
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
+
9249
9285
"""
9250
9286
f = _inplace_enabled_define_and_cleanup(self)
9251
9287
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
+
9252
9294
detail = is_log_level_detail(logger)
9253
9295
debug = is_log_level_debug(logger)
9254
9296
9297
+ return_key = None # in case there are no vertical coords to compute
9255
9298
for cr in f.coordinate_references(todict=True).values():
9256
9299
# --------------------------------------------------------
9257
9300
# Compute the non-parametric vertical coordinates, if
@@ -9293,20 +9336,25 @@ def compute_vertical_coordinates(
9293
9336
f"{c.dump(display=False, _level=1)}"
9294
9337
) # pragma: no cover
9295
9338
9296
- key = f.set_construct(c, axes=computed_axes, copy=False)
9339
+ return_key = f.set_construct(c, axes=computed_axes, copy=False)
9297
9340
9298
9341
# Reference the new coordinates from the coordinate
9299
9342
# reference construct
9300
- cr.set_coordinate(key )
9343
+ cr.set_coordinate(return_key )
9301
9344
9302
9345
if debug:
9303
9346
logger.debug(
9304
- f"Non-parametric coordinates construct key: {key!r}\n"
9347
+ "Non-parametric coordinates construct key: "
9348
+ f"{return_key!r}\n"
9305
9349
"Updated coordinate reference construct:\n"
9306
9350
f"{cr.dump(display=False, _level=1)}"
9307
9351
) # pragma: no cover
9308
9352
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
9310
9358
9311
9359
def match_by_construct(self, *identities, OR=False, **conditions):
9312
9360
"""Whether or not there are particular metadata constructs.
0 commit comments