@@ -4994,7 +4994,7 @@ def healpix_decrease_refinement_level(
49944994
49954995 Set the refinement level to 0 using the ``'`range'`` *method*,
49964996 which requires a new *reduction* function to be defined:
4997-
4997+
49984998 >>> import numpy as np
49994999 >>> def range_func(a, axis=None):
50005000 ... return np.max(a, axis=axis) - np.min(a, axis=axis)
@@ -5114,16 +5114,17 @@ def healpix_decrease_refinement_level(
51145114 f"Can't decrease HEALPix refinement level of {f!r}: "
51155115 "'level' must be a non-negative integer less than "
51165116 "or equal to the current refinement level of "
5117- f"{old_refinement_level}. Got {refinement_level!r}"
5117+ f"{old_refinement_level}. Got: {refinement_level!r}"
51185118 )
51195119
5120+ # Parse 'level'
51205121 if refinement_level == old_refinement_level:
51215122 # No change in refinement level
51225123 return f
51235124
51245125 # Get the number of cells at the original refinement level
5125- # which are contained in one cell at the lower refinement
5126- # level
5126+ # which are contained in one larger cell at the new lower
5127+ # refinement level
51275128 ncells = 4 ** (old_refinement_level - refinement_level)
51285129
51295130 # Get the healpix_index coordinates
@@ -5260,18 +5261,20 @@ def healpix_decrease_refinement_level(
52605261 def healpix_increase_refinement_level(self, refinement_level, quantity):
52615262 """Increase the refinement level of a HEALPix grid.
52625263
5263- Increasing the refinement level increases the resolution of
5264- the HEALPix grid by broadcasting the Field data from each
5265- original cell to all of the new smaller cells at the new
5266- higher refinement level that lie inside it.
5267-
5268- For an extensive field quantity (i.e. one that depends on the
5269- size of the cells, such as "sea_ice_mass" with units of kg),
5270- the broadcast values are also reduced to be consistent with
5271- the new smaller cell areas. For an intensive field quantity
5272- (i.e. one that does not depend on the size of the cells, such
5273- as "sea_ice_amount" with units of kg m-2), the broadcast
5274- values are not changed.
5264+ Increasing the HEALPix refinement level increases the
5265+ resolution of the HEALPix grid by broadcasting the Field data
5266+ from each original cell to all of the new smaller cells at the
5267+ new higher refinement level that lie inside it.
5268+
5269+ It must be specified whether the field data contains an
5270+ extensive or intensive quantity. An extensive quantity depends
5271+ on the size of the cells (such as "sea_ice_mass" with units of
5272+ kg, or "cell_area" with units of m2), and an intensive
5273+ quantity does not depend on the size of the cells (such as
5274+ "sea_ice_amount" with units of kg m-2, or "air_temperature"
5275+ with units of K). For an extensive quantity only, the
5276+ broadcast values are reduced to be consistent with the new
5277+ smaller cell areas.
52755278
52765279 **References**
52775280
@@ -5332,12 +5335,12 @@ def healpix_increase_refinement_level(self, refinement_level, quantity):
53325335 >>> print(g[0, :8] .array)
53335336 [[291.5 291.5 291.5 291.5 293.5 293.5 293.5 293.5]]
53345337
5335- For an extensive quantity (which ``f`` is not in this example,
5336- but we can assume that it is for demonstration purposes), each
5337- cell at the higher refinement level has the value of a cell at
5338- the original refinement level after dividing it by the number
5339- of cells at the higher refinement level that lie in one cell
5340- of the original refinement level (4 in this case):
5338+ For an extensive quantity (which the ``f`` is this example is
5339+ not, but we can assume that it is for demonstration purposes),
5340+ each cell at the higher refinement level has the value of a
5341+ cell at the original refinement level after dividing it by the
5342+ number of cells at the higher refinement level that lie in one
5343+ cell of the original refinement level (4 in this case):
53415344
53425345 >>> g = f.healpix_increase_refinement_level(2, 'extensive')
53435346 >>> print(f[0, :2] .array)
@@ -5346,11 +5349,11 @@ def healpix_increase_refinement_level(self, refinement_level, quantity):
53465349 [[72.875 72.875 72.875 72.875 73.375 73.375 73.375 73.375]]
53475350
53485351 """
5349- from .data.dask_utils import (
5350- cf_healpix_increase_refinement,
5351- cf_healpix_increase_refinement_indices,
5352+ from .data.dask_utils import cf_healpix_increase_refinement_indices
5353+ from .healpix import (
5354+ _healpix_increase_refinement_level,
5355+ healpix_max_refinement_level,
53525356 )
5353- from .healpix import healpix_max_refinement_level
53545357
53555358 # Increasing the refinement level requires the nested indexing
53565359 # scheme
@@ -5380,7 +5383,7 @@ def healpix_increase_refinement_level(self, refinement_level, quantity):
53805383 "'level' must be an integer greater than or equal to the "
53815384 f"current refinement level of {old_refinement_level}, and "
53825385 f"less than or equal to {healpix_max_refinement_level()}. "
5383- f"Got {refinement_level!r}"
5386+ f"Got: {refinement_level!r}"
53845387 )
53855388
53865389 if refinement_level == old_refinement_level:
@@ -5393,7 +5396,7 @@ def healpix_increase_refinement_level(self, refinement_level, quantity):
53935396 raise ValueError(
53945397 f"Can't increase HEALPix refinement level of {f!r}: "
53955398 f"'quantity' keyword must be one of {valid_quantities}. "
5396- f"Got {quantity!r}"
5399+ f"Got: {quantity!r}"
53975400 )
53985401
53995402 # Get the number of cells at the higher refinement level which
@@ -5424,97 +5427,35 @@ def healpix_increase_refinement_level(self, refinement_level, quantity):
54245427 )
54255428 )
54265429
5427- # Increase the refinement of the Field data
5428- dx = f.data.to_dask_array(
5429- _force_mask_hardness=False, _force_to_memory=False
5430- )
5431-
5432- if quantity == "extensive":
5433- # Extensive data get divided by 'ncells' in
5434- # `cf_healpix_increase_refinement`, so they end up with a
5435- # data type of float64.
5436- dtype = np.dtype("float64")
5437- else:
5438- dtype = dx.dtype
5439-
5440- # Each chunk is going to get larger by a factor of 'ncells'
5441- chunks = list(dx.chunks)
5442- chunks[iaxis] = (np.array(chunks[iaxis]) * ncells).tolist()
5443-
5444- dx = dx.map_blocks(
5445- cf_healpix_increase_refinement,
5446- chunks=tuple(chunks),
5447- dtype=dtype,
5448- meta=np.array((), dtype=dtype),
5449- ncells=ncells,
5450- iaxis=iaxis,
5451- quantity=quantity,
5452- )
5453-
54545430 # Re-size the HEALPix axis
54555431 domain_axis = f.domain_axis(axis)
5456- domain_axis.set_size(dx .shape[iaxis])
5432+ domain_axis.set_size(f .shape[iaxis] * ncells )
54575433
5458- f.set_data(dx, copy=False)
5434+ # Increase the refinement of the Field data
5435+ _healpix_increase_refinement_level(f, ncells, iaxis, quantity)
54595436
54605437 # Increase the refinement level of domain ancillary constructs
54615438 # that span the HEALPix axis. We're assuming that domain
54625439 # ancillary data are intensive (i.e. do not depend on the size
54635440 # of the cell).
5464- meta = np.array((), dtype=dtype)
54655441 for key, domain_ancillary in f.domain_ancillaries(
54665442 filter_by_axis=(axis,), axis_mode="and", todict=True
54675443 ).items():
54685444 iaxis = f.get_data_axes(key).index(axis)
5469- dx = domain_ancillary.data.to_dask_array(
5470- _force_mask_hardness=False, _force_to_memory=False
5471- )
5472- dtype = dx.dtype
5473-
5474- # Each chunk is going to get larger by a factor of
5475- # 'ncells'
5476- chunks = list(dx.chunks)
5477- chunks[iaxis] = (np.array(chunks[iaxis]) * ncells).tolist()
5478-
5479- dx = dx.map_blocks(
5480- cf_healpix_increase_refinement,
5481- chunks=tuple(chunks),
5482- dtype=dtype,
5483- meta=meta,
5484- ncells=ncells,
5485- iaxis=iaxis,
5486- quantity="intensive",
5445+ _healpix_increase_refinement_level(
5446+ domain_ancillary, ncells, iaxis, "intensive"
54875447 )
5488- domain_ancillary.set_data(dx, copy=False)
54895448
54905449 # Increase the refinement level of cell measure constructs
54915450 # that span the HEALPix axis. Cell measure data are extensive
54925451 # (i.e. depend on the size of the cell).
5493- dtype = np.dtype("float64")
5494- meta = np.array((), dtype=dtype)
54955452 for key, cell_measure in f.cell_measures(
54965453 filter_by_axis=(axis,), axis_mode="and", todict=True
54975454 ).items():
54985455 iaxis = f.get_data_axes(key).index(axis)
5499- dx = cell_measure.data.to_dask_array(
5500- _force_mask_hardness=False, _force_to_memory=False
5501- )
5502-
5503- # Each chunk is going to get larger by a factor of
5504- # 'ncells'
5505- chunks = list(dx.chunks)
5506- chunks[iaxis] = (np.array(chunks[iaxis]) * ncells).tolist()
5507-
5508- dx = dx.map_blocks(
5509- cf_healpix_increase_refinement,
5510- chunks=tuple(chunks),
5511- dtype=dtype,
5512- meta=meta,
5513- ncells=ncells,
5514- iaxis=iaxis,
5515- quantity="extensive",
5456+ _healpix_increase_refinement_level(
5457+ cell_measure, ncells, iaxis, "extensive"
55165458 )
5517- cell_measure.set_data(dx, copy=False)
55185459
55195460 # Remove all other metadata constructs that span the HEALPix
55205461 # axis (including the original healpix_index coordinate
0 commit comments