@@ -9514,7 +9514,12 @@ def domain_mask(self, **kwargs):
9514
9514
@_inplace_enabled(default=False)
9515
9515
@_manage_log_level_via_verbosity
9516
9516
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,
9518
9523
):
9519
9524
"""Compute non-parametric vertical coordinates.
9520
9525
@@ -9549,7 +9554,7 @@ def compute_vertical_coordinates(
9549
9554
{{default_to_zero: `bool`, optional}}
9550
9555
9551
9556
strict: `bool`
9552
- If False then allow the computation to occur when
9557
+ If False then allow the computation to occur when:
9553
9558
9554
9559
* A domain ancillary construct has no standard name, but
9555
9560
the corresponding term has a standard name that is
@@ -9568,15 +9573,30 @@ def compute_vertical_coordinates(
9568
9573
names, then an exception is raised regardless of the value
9569
9574
of *strict*.
9570
9575
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
+
9571
9588
{{inplace: `bool`, optional}}
9572
9589
9573
9590
{{verbose: `int` or `str` or `None`, optional}}
9574
9591
9575
9592
:Returns:
9576
9593
9577
- `Field` or `None`
9594
+ `Field`, 2-tuple, or `None`
9578
9595
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.
9580
9600
9581
9601
**Examples**
9582
9602
@@ -9603,7 +9623,7 @@ def compute_vertical_coordinates(
9603
9623
>>> print(f.auxiliary_coordinate('altitude', default=None))
9604
9624
None
9605
9625
>>> g = f.compute_vertical_coordinates()
9606
- >>> print(g.auxiliary_coordinates)
9626
+ >>> print(g.auxiliary_coordinates() )
9607
9627
Constructs:
9608
9628
{'auxiliarycoordinate0': <CF AuxiliaryCoordinate: latitude(10, 9) degrees_N>,
9609
9629
'auxiliarycoordinate1': <CF AuxiliaryCoordinate: longitude(9, 10) degrees_E>,
@@ -9619,12 +9639,35 @@ def compute_vertical_coordinates(
9619
9639
Bounds:units = 'm'
9620
9640
Bounds:Data(1, 10, 9, 2) = [[[[5.0, ..., 5415.0]]]] m
9621
9641
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
+
9622
9658
"""
9623
9659
f = _inplace_enabled_define_and_cleanup(self)
9624
9660
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
+
9625
9667
detail = is_log_level_detail(logger)
9626
9668
debug = is_log_level_debug(logger)
9627
9669
9670
+ return_key = None # in case there are no vertical coords to compute
9628
9671
for cr in f.coordinate_references(todict=True).values():
9629
9672
# --------------------------------------------------------
9630
9673
# Compute the non-parametric vertical coordinates, if
@@ -9666,20 +9709,25 @@ def compute_vertical_coordinates(
9666
9709
f"{c.dump(display=False, _level=1)}"
9667
9710
) # pragma: no cover
9668
9711
9669
- key = f.set_construct(c, axes=computed_axes, copy=False)
9712
+ return_key = f.set_construct(c, axes=computed_axes, copy=False)
9670
9713
9671
9714
# Reference the new coordinates from the coordinate
9672
9715
# reference construct
9673
- cr.set_coordinate(key )
9716
+ cr.set_coordinate(return_key )
9674
9717
9675
9718
if debug:
9676
9719
logger.debug(
9677
- f"Non-parametric coordinates construct key: {key!r}\n"
9720
+ "Non-parametric coordinates construct key: "
9721
+ f"{return_key!r}\n"
9678
9722
"Updated coordinate reference construct:\n"
9679
9723
f"{cr.dump(display=False, _level=1)}"
9680
9724
) # pragma: no cover
9681
9725
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
9683
9731
9684
9732
def match_by_construct(self, *identities, OR=False, **conditions):
9685
9733
"""Whether or not there are particular metadata constructs.
0 commit comments