Skip to content

Commit 412d862

Browse files
authored
Merge pull request #865 from davidhassell/create-bounds
Add `inplace` keyword to `cf.DimensionCoordinate.create_bounds`
2 parents 747e7ed + 24413c5 commit 412d862

File tree

3 files changed

+50
-10
lines changed

3 files changed

+50
-10
lines changed

Changelog.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
version NEXTVERSION
2+
-------------------
3+
4+
**2025-??-??**
5+
6+
* New keyword parameter to `cf.DimensionCoordinate.create_bounds`:
7+
``inplace`` (https://github.com/NCAS-CMS/cf-python/issues/855)
8+
9+
----
10+
111
version 3.17.0
212
--------------
313

cf/dimensioncoordinate.py

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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()):

cf/test/test_DimensionCoordinate.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,15 @@ def test_DimensionCoordinate_create_bounds(self):
544544
).all()
545545
)
546546

547+
# In-place
548+
self.assertFalse(d.has_bounds())
549+
self.assertIsNone(d.create_bounds(inplace=True))
550+
self.assertTrue(d.has_bounds())
551+
552+
# Fail when inplace=True and bounds already exist
553+
with self.assertRaises(ValueError):
554+
d.create_bounds(inplace=True)
555+
547556
# Cellsize units must be equivalent to the coordinate units,
548557
# or if the cell has no units then they are assumed to be
549558
# the same as the coordinates:

0 commit comments

Comments
 (0)