Skip to content

Commit 43d02ef

Browse files
committed
Make make_gridcube() dataless, and improve documentation cross-refs.
1 parent f2c1f30 commit 43d02ef

File tree

6 files changed

+20
-12
lines changed

6 files changed

+20
-12
lines changed

docs/src/further_topics/dataless_cubes.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ Data assignment
3838
You can make an existing cube dataless, by setting ``cube.data = None``.
3939
The data array is simply discarded.
4040

41+
Likewise, you can add data by assigning any data array of the correct shape, which
42+
turns it into a 'normal' cube.
43+
44+
Note that ``cube.dtype`` always matches ``cube.data.dtype``. A dataless cube has a
45+
dtype of ``None``.
46+
4147

4248
Cube copy
4349
---------

docs/src/further_topics/netcdf_io.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,8 @@ TBC
190190

191191
.. _save_load_dataless:
192192

193-
Dataless Cubes
194-
--------------
193+
Dataless Cubes in NetCDF files
194+
------------------------------
195195
It now possible to have "dataless" cubes, where ``cube.data is None``.
196196
When these are saved to a NetCDF file interface, this results in a netcdf file variable
197197
with all-unwritten data (meaning that it takes up no storage space).

docs/src/whatsnew/latest.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ This document explains the changes made to Iris for this release
3030
✨ Features
3131
===========
3232

33-
#. `@pp-mo`_ added a new utility function for making a test cube with a specified 2D
34-
horizontal grid.
33+
#. `@pp-mo`_ added the :func:`~iris.util.make_gridcube` utility function, for making a
34+
dataless test-cube with a specified 2D horizontal grid.
3535
(:issue:`5770`, :pull:`6581`)
3636

3737
#. `@bjlittle`_ extended ``zlib`` compression of :class:`~iris.cube.Cube` data

lib/iris/fileformats/pp.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
"save_pairs_from_cube",
5555
]
5656

57-
57+
#: Standard spherical earth radius, as defined for MetOffice Unified Model.
5858
EARTH_RADIUS = 6371229.0
5959

6060

lib/iris/tests/unit/util/test_make_gridcube.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ def test_default(self):
2828
assert cube.standard_name is None
2929
assert cube.long_name == "grid_cube"
3030
assert cube.coord_system() == _GLOBE
31+
assert cube.is_dataless()
32+
assert cube.shape == (20, 30)
3133

3234
assert len(cube.coords()) == 2
3335
co_x, co_y = [cube.coord(axis=ax) for ax in "xy"]
@@ -46,10 +48,6 @@ def test_default(self):
4648
assert np.all(co_y.points == np.linspace(-90.0, 90.0, 20))
4749
assert co_y.points.dtype == np.dtype("f8")
4850

49-
assert cube.has_lazy_data()
50-
assert cube.shape == (20, 30)
51-
assert np.all(cube.data == 0)
52-
5351
def test_regular_region(self):
5452
"""Check use of n? and ?lims args."""
5553
cube = make_gridcube(xlims=(20, 30), nx=5, ylims=(10, 25), ny=4)

lib/iris/util.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2625,6 +2625,9 @@ def make_gridcube(
26252625
It has one-dimensional X and Y coordinates, in a specific coordinate system.
26262626
Both can be given either regularly spaced or irregular points.
26272627
2628+
The cube is dataless, meaning that ``cube.data`` is ``None``, but data can easily be
2629+
assigned if required. See :ref:`dataless-cubes`.
2630+
26282631
Parameters
26292632
----------
26302633
nx : int, optional
@@ -2654,7 +2657,7 @@ def make_gridcube(
26542657
Returns
26552658
-------
26562659
cube: iris.cube.Cube
2657-
A cube with the specified grid, and all-zeroes (lazy) data.
2660+
A cube with the specified grid, but no data.
26582661
26592662
Warnings
26602663
--------
@@ -2671,7 +2674,7 @@ def make_gridcube(
26712674
# float32 zero, to force minimum 'f4' floating point precision
26722675
zero_f4 = np.asarray(
26732676
0.0,
2674-
dtype="f4", # single precision (minimum), or what was passed
2677+
dtype="f4",
26752678
)
26762679

26772680
def dimco(
@@ -2759,7 +2762,8 @@ def dimco(
27592762
xco = dimco("x", x_name, units, x_points, xlims, nx, coord_system=coord_system)
27602763
yco = dimco("y", y_name, units, y_points, ylims, ny, coord_system=coord_system)
27612764
cube = Cube(
2762-
da.zeros((yco.shape[0], xco.shape[0]), dtype=np.int8),
2765+
data=None,
2766+
shape=(yco.shape[0], xco.shape[0]),
27632767
long_name="grid_cube",
27642768
dim_coords_and_dims=((yco, 0), (xco, 1)),
27652769
)

0 commit comments

Comments
 (0)