Skip to content

Commit aa0f70c

Browse files
committed
dev
1 parent d4feb64 commit aa0f70c

4 files changed

Lines changed: 42 additions & 26 deletions

File tree

cf/data/dask_utils.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -518,11 +518,13 @@ def cf_healpix_bounds(
518518
If True then return the bounds' longitudes.
519519
520520
pole_longitude: `None` or number
521-
The longitude of bounds that lie exactly on the north
522-
(south) pole. If `None` (the default) then the longitude
523-
of such a vertex will be the same as the south (north)
524-
vertex of the same cell. If set to a number, then the
525-
longitudes of all such vertices will be given that value.
521+
Define the longitudes of vertices that lie exactly on the
522+
north or south pole. If `None` (the default) then the
523+
longitude of such a vertex on the north (south) pole will
524+
be the same as the longitude of the south (north) vertex
525+
of the same cell. If set to a number, then the longitudes
526+
of all vertices on the north or south pole will be given
527+
the value *pole_longitude*.
526528
527529
:Returns:
528530

cf/healpix.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -199,30 +199,35 @@ def _healpix_increase_refinement_level(x, ncells, iaxis, quantity):
199199
"""
200200
from dask.array.core import normalize_chunks
201201

202-
# Get the Dask array.
202+
# Get the Dask array (e.g. dx.shape is (12, 19, 48))
203203
dx = x.data.to_dask_array(_force_mask_hardness=False)
204204

205205
# Divide extensive data by the number of new cells
206206
if quantity == "extensive":
207207
dx = dx / ncells
208208

209209
# Add a new size dimension just after the HEALPix dimension
210+
# (e.g. .shape becomes (12, 19, 48, 1))
210211
new_axis = iaxis + 1
211212
dx = da.expand_dims(dx, new_axis)
212213

213-
# Work out what the chunks should be for the new dimension
214+
# Modify the size of the new dimension to be the number of cells
215+
# at the new refinement level which are contained in one cell at
216+
# the original refinement level (e.g. size becomes 16)
214217
shape = list(dx.shape)
215218
shape[new_axis] = ncells
216219

220+
# Work out what the chunks should be for the new dimension
217221
chunks = list(dx.chunks)
218222
chunks[new_axis] = "auto"
219223
chunks = normalize_chunks(chunks, shape, dtype=dx.dtype)
220224

221-
# Broadcast the data along the new dimension
225+
# Broadcast the data along the new dimension (e.g. dx.shape
226+
# becomes (12, 19, 48, 16))
222227
dx = da.broadcast_to(dx, shape, chunks=chunks)
223228

224229
# Reshape the array so that it has a single, larger HEALPix
225-
# dimension
230+
# dimension (e.g. dx.shape becomes (12, 19, 768))
226231
dx = dx.reshape(
227232
shape[:iaxis]
228233
+ [shape[iaxis] * shape[new_axis]]
@@ -272,7 +277,7 @@ def _healpix_increase_refinement_level_indices(
272277
# Get any cached data values
273278
cached = healpix_index.data._get_cached_elements().copy()
274279

275-
# Get the Dask array
280+
# Get the Dask array (e.g. dx.shape is (48,))
276281
dx = healpix_index.data.to_dask_array(_force_mask_hardness=False)
277282

278283
# Set the data type to allow for the largest possible HEALPix
@@ -286,18 +291,23 @@ def _healpix_increase_refinement_level_indices(
286291
dx = dx * ncells
287292

288293
# Add a new size dimension just after the HEALPix dimension
294+
# (e.g. .shape becomes (48, 1))
289295
new_axis = 1
290296
dx = da.expand_dims(dx, new_axis)
291297

292-
# Work out what the chunks should be for the new dimension
298+
# Modify the size of the new dimension to be the number of cells
299+
# at the new refinement level which are contained in one cell at
300+
# the original refinement level (e.g. size becomes 16)
293301
shape = list(dx.shape)
294302
shape[new_axis] = ncells
295303

304+
# Work out what the chunks should be for the new dimension
296305
chunks = list(dx.chunks)
297306
chunks[new_axis] = "auto"
298307
chunks = normalize_chunks(chunks, shape, dtype=dx.dtype)
299308

300-
# Broadcast the data along the new dimension
309+
# Broadcast the data along the new dimension (e.g. dx.shape
310+
# becomes (48, 16))
301311
dx = da.broadcast_to(dx, shape, chunks=chunks)
302312

303313
# Increment the broadcast values along the new dimension, so that
@@ -306,10 +316,12 @@ def _healpix_increase_refinement_level_indices(
306316
# original refinement level.
307317
new_shape = [1] * dx.ndim
308318
new_shape[new_axis] = shape[new_axis]
319+
309320
dx += da.arange(ncells, chunks=chunks[new_axis]).reshape(new_shape)
310321

311322
# Reshape the new array to combine the original HEALPix and
312323
# broadcast dimensions into a single new HEALPix dimension
324+
# (e.g. dx.shape becomes (768,))
313325
dx = dx.reshape(shape[0] * shape[new_axis])
314326

315327
healpix_index.set_data(dx, copy=False)
@@ -324,8 +336,6 @@ def _healpix_increase_refinement_level_indices(
324336
x = np.array(cached[-1], dtype=dtype) * ncells + (ncells - 1)
325337
data._set_cached_elements({-1: x})
326338

327-
return
328-
329339

330340
def _healpix_indexing_scheme(healpix_index, hp, new_indexing_scheme):
331341
"""Change the indexing scheme of HEALPix indices in-place.

cf/mixin/fielddomain.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2268,6 +2268,7 @@ def healpix_to_ugrid(self, inplace=False):
22682268
two_d=False, pole_longitude=0, inplace=True
22692269
)
22702270

2271+
# Get the lat/lon coordinates
22712272
x_key, x = f.auxiliary_coordinate(
22722273
"Y",
22732274
filter_by_axis=(axis,),
@@ -2308,8 +2309,9 @@ def healpix_to_ugrid(self, inplace=False):
23082309
"coordinate bounds"
23092310
)
23102311

2311-
# Create the Domain Topology construct, by creating a unique
2312-
# integer identifer for each node location.
2312+
# Create the UGRID Domain Topology construct, by creating an
2313+
# arbitrary unique integer identifer for each unique node
2314+
# location.
23132315
bounds_y = bounds_y.data.to_dask_array(_force_mask_hardness=False)
23142316
bounds_x = bounds_x.data.to_dask_array(_force_mask_hardness=False)
23152317

@@ -2374,12 +2376,15 @@ def create_latlon_coordinates(
23742376
attempt this.
23752377
23762378
pole_longitude: `None` or number
2377-
The longitude of coordinates, or coordinate bounds,
2378-
that lie exactly on the north or south pole. If `None`
2379-
(the default) then the longitudes of such points will
2380-
vary according to the algorithm being used to create
2381-
them. If set to a number, then the longitudes of such
2382-
points will all be given that value.
2379+
Define the longitudes of coordinates or coordinate
2380+
bounds that lie exactly on the north or south pole. If
2381+
`None` (the default) then the longitudes of such
2382+
points are determined by whatever algorithm was used
2383+
to create the coordinates, which will likely result in
2384+
different points on a pole having different
2385+
longitudes. If set to a number, then the longitudes of
2386+
all points on the north or south pole will be given
2387+
the value *pole_longitude*.
23832388
23842389
overwrite: `bool`, optional
23852390
If True then remove any existing latitude and

cf/test/test_Field.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3173,7 +3173,7 @@ def test_Field_healpix_to_ugrid(self):
31733173
self.assertEqual(len(u.domain_topologies()), 1)
31743174
self.assertEqual(len(u.auxiliary_coordinates()), 2)
31753175

3176-
topology = u.domain_topology().normalise().array
3176+
topology = u.domain_topology().normalise()
31773177
self.assertEqual(np.unique(topology).size, 53)
31783178
self.assertTrue(
31793179
np.array_equal(
@@ -3434,9 +3434,8 @@ def test_Field_healpix_increase_refinement_level(self):
34343434
self.assertEqual(
34353435
g.coordinate("healpix_index").data._get_cached_elements(), {}
34363436
)
3437-
_ = str(
3438-
f.coordinate("healpix_index").data
3439-
) # Create cached elements
3437+
# Create cached elements
3438+
_ = str(f.coordinate("healpix_index").data)
34403439
g = f.healpix_increase_refinement_level(16, "intensive")
34413440
self.assertEqual(
34423441
g.coordinate("healpix_index").data._get_cached_elements(),

0 commit comments

Comments
 (0)