@@ -533,18 +533,25 @@ def create_regular(cls, args, units=None, standard_name=None, bounds=True):
533533 return coordinate
534534
535535 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 ,
537543 ):
538544 """Create cell bounds.
539545
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.
545550
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.
548555
549556 By default, Voronoi cells are created by defining cell bounds
550557 that are half way between adjacent coordinate values. For
@@ -640,10 +647,13 @@ def create_bounds(
640647 ``-90``: ``min=-90``, or ``min=cf.Data(-90,
641648 'degrees_north')``.
642649
650+ {{inplace: `bool`, optional}}
651+
643652 :Returns:
644653
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.
647657
648658 **Examples**
649659
@@ -756,6 +766,13 @@ def create_bounds(
756766 cftime.DatetimeGregorian(1985, 12, 1, 0, 0, 0, 0)]]
757767
758768 """
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+
759776 array = self .array
760777 size = array .size
761778
@@ -907,6 +924,10 @@ def create_bounds(
907924 # Create coordinate bounds object
908925 bounds = Bounds (data = Data (bounds , units = self .Units ), copy = False )
909926
927+ if inplace :
928+ self .set_bounds (bounds )
929+ return
930+
910931 return bounds
911932
912933 def del_cell_characteristics (self , default = ValueError ()):
0 commit comments