You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This function is often used to broadcast auxiliary coordinate or cell measure data or weights to the same shape as the data from a cube. If the chunks of the broadcasted data do not match that of the cube, this leads to poor computational performance.
defbroadcast_to_shape(array, shape, chunks, dim_map):
"""Copy of `iris.util.broadcast_to_shape` that allows specifying chunks."""ifisinstance(array, da.Array):
chunks=list(chunks)
forsrc_idx, tgt_idxinenumerate(dim_map):
# Only use the specified chunks along new dimensions.chunks[tgt_idx] =array.chunks[src_idx]
broadcast_to=partial(da.broadcast_to, chunks=chunks)
else:
broadcast_to=np.broadcast_ton_orig_dims=len(array.shape)
n_new_dims=len(shape) -n_orig_dimsarray=array.reshape(array.shape+ (1,) *n_new_dims)
# Get dims in required order.array=np.moveaxis(array, range(n_orig_dims), dim_map)
new_array=broadcast_to(array, shape)
ifnp.ma.isMA(array):
# broadcast_to strips masks so we need to handle them explicitly.mask=np.ma.getmask(array)
ifmaskisnp.ma.nomask:
new_mask=np.ma.nomaskelse:
new_mask=np.broadcast_to(mask, shape)
new_array=np.ma.array(new_array, mask=new_mask)
elifis_lazy_masked_data(array):
# broadcast_to strips masks so we need to handle them explicitly.mask=da.ma.getmaskarray(array)
new_mask=broadcast_to(mask, shape)
new_array=da.ma.masked_array(new_array, new_mask)
returnnew_array
The text was updated successfully, but these errors were encountered:
✨ Feature Request
I would like to be able to specify the target chunks in
iris.util.broadcast_to_shape
.Motivation
This function is often used to broadcast auxiliary coordinate or cell measure data or weights to the same shape as the data from a cube. If the chunks of the broadcasted data do not match that of the cube, this leads to poor computational performance.
Additional context
Here is an example implementation (taken from ESMValGroup/ESMValCore#2120):
The text was updated successfully, but these errors were encountered: