@@ -533,18 +533,25 @@ def create_regular(cls, args, units=None, standard_name=None, bounds=True):
533
533
return coordinate
534
534
535
535
def create_bounds (
536
- self , bound = None , cellsize = None , flt = 0.5 , max = None , min = None
536
+ self ,
537
+ bound = None ,
538
+ cellsize = None ,
539
+ flt = 0.5 ,
540
+ max = None ,
541
+ min = None ,
542
+ inplace = False ,
537
543
):
538
544
"""Create cell bounds.
539
545
540
- Creates new cell bounds, irrespective of whether the cells
541
- already have cell bounds. The new bounds are not set on the
542
- dimension coordinate construct, but if that is desired they
543
- may always be added with the `set_bounds` method, for
544
- instance:
546
+ When the operation is *not* in-place (the default), new bounds
547
+ will be created and returned, regardless of whether or not
548
+ bounds already exist, but the bounds are not set on the
549
+ dimension coordinate construct.
545
550
546
- >>> b = d.create_bounds()
547
- >>> d.set_bounds(b)
551
+ If the operation is in-place (i.e. the *inplace* parameter is
552
+ True) then the newly created bounds will be set on the
553
+ dimension coordinate construct and `None` is returned, but
554
+ only if there are no existing bounds.
548
555
549
556
By default, Voronoi cells are created by defining cell bounds
550
557
that are half way between adjacent coordinate values. For
@@ -640,10 +647,13 @@ def create_bounds(
640
647
``-90``: ``min=-90``, or ``min=cf.Data(-90,
641
648
'degrees_north')``.
642
649
650
+ {{inplace: `bool`, optional}}
651
+
643
652
:Returns:
644
653
645
- `Bounds`
646
- The new coordinate cell bounds.
654
+ `Bounds` or `None`
655
+ The new coordinate cell bounds, or `None` if the
656
+ operation was in-place.
647
657
648
658
**Examples**
649
659
@@ -756,6 +766,13 @@ def create_bounds(
756
766
cftime.DatetimeGregorian(1985, 12, 1, 0, 0, 0, 0)]]
757
767
758
768
"""
769
+ if inplace and self .has_bounds ():
770
+ raise ValueError (
771
+ "Can't create dimension coordinate bounds in-place when "
772
+ "bounds already exist. Existing bounds may be removed "
773
+ "with the 'del_bounds' method."
774
+ )
775
+
759
776
array = self .array
760
777
size = array .size
761
778
@@ -907,6 +924,10 @@ def create_bounds(
907
924
# Create coordinate bounds object
908
925
bounds = Bounds (data = Data (bounds , units = self .Units ), copy = False )
909
926
927
+ if inplace :
928
+ self .set_bounds (bounds )
929
+ return
930
+
910
931
return bounds
911
932
912
933
def del_cell_characteristics (self , default = ValueError ()):
0 commit comments