def _get_intermediate_chunks(array, chunks, new_last_dim, zarr_output, memory):
    '\n    Calculates chunk sizes for the given array for the intermediate output\n    files.\n\n    Parameters\n    ----------\n    array : xr.DataArray\n        Array to rechunk and transpose\n    chunks : dict or None\n        Chunks passed to write_transposed_dataset, None if none were given.\n    new_last_dim : str\n        Name of the new last dimension, normally "time".\n    zarr_output : bool\n        Whether the final file will be a zarr file (True) or a netCDf (False).\n    memory : float\n        The amount of memory to be used for buffering in GB.\n\n    Returns\n    -------\n    tmp_chunks : dict\n        Chunks to be used for rechunking the array to a temporary file. The\n        order of keys corresponds to the order of dimensions in the transposed\n        array.\n    '
    dtype = array.dtype
    dims = dict(zip(array.dims, array.shape))
    transposed_shape = [length for (dim, length) in dims.items() if (dim != new_last_dim)]
    transposed_shape.append(dims[new_last_dim])
    if (chunks is None):
        if zarr_output:
            chunksizes = infer_chunks(transposed_shape, 100, dtype)[:(- 1)]
        else:
            chunksizes = infer_chunks(transposed_shape, 1, dtype)[:(- 1)]
        chunks = dict(zip([dim for dim in dims if (dim != new_last_dim)], chunksizes))
        chunks[new_last_dim] = (- 1)
    else:
        chunks = copy.copy(chunks)
    tmp_chunks = {dim: chunks[dim] for dim in dims if (dim != new_last_dim)}
    size = dtype.itemsize
    chunksizes = [(size if (size != (- 1)) else dims[dim]) for (dim, size) in chunks.items()]
    chunksize_MB = ((np.prod(chunksizes) * size) / (1024 ** 2))
    img_shape = transposed_shape[:(- 1)]
    len_time = transposed_shape[(- 1)]
    imagesize_GB = ((np.prod(img_shape) * size) / (1024 ** 3))
    stepsize = (int(math.floor((memory / imagesize_GB))) // 2)
    stepsize = min(stepsize, len_time)
    tmp_chunks[new_last_dim] = stepsize
    tmp_chunks_str = str(tuple(tmp_chunks.values()))
    logging.info(f'write_transposed_dataset: Creating chunks {tmp_chunks_str} with chunksize {chunksize_MB:.2f} MB')
    return tmp_chunks
Calculates chunk sizes for the given array for the intermediate output
files.

Parameters
----------
array : xr.DataArray
    Array to rechunk and transpose
chunks : dict or None
    Chunks passed to write_transposed_dataset, None if none were given.
new_last_dim : str
    Name of the new last dimension, normally "time".
zarr_output : bool
    Whether the final file will be a zarr file (True) or a netCDf (False).
memory : float
    The amount of memory to be used for buffering in GB.

Returns
-------
tmp_chunks : dict
    Chunks to be used for rechunking the array to a temporary file. The
    order of keys corresponds to the order of dimensions in the transposed
    array.
def _get_intermediate_chunks(array, chunks, new_last_dim, zarr_output, memory):
    '\n    Calculates chunk sizes for the given array for the intermediate output\n    files.\n\n    Parameters\n    ----------\n    array : xr.DataArray\n        Array to rechunk and transpose\n    chunks : dict or None\n        Chunks passed to write_transposed_dataset, None if none were given.\n    new_last_dim : str\n        Name of the new last dimension, normally "time".\n    zarr_output : bool\n        Whether the final file will be a zarr file (True) or a netCDf (False).\n    memory : float\n        The amount of memory to be used for buffering in GB.\n\n    Returns\n    -------\n    tmp_chunks : dict\n        Chunks to be used for rechunking the array to a temporary file. The\n        order of keys corresponds to the order of dimensions in the transposed\n        array.\n    '
    dtype = array.dtype
    dims = dict(zip(array.dims, array.shape))
    transposed_shape = [length for (dim, length) in dims.items() if (dim != new_last_dim)]
    transposed_shape.append(dims[new_last_dim])
    if (chunks is None):
        if zarr_output:
            chunksizes = infer_chunks(transposed_shape, 100, dtype)[:(- 1)]
        else:
            chunksizes = infer_chunks(transposed_shape, 1, dtype)[:(- 1)]
        chunks = dict(zip([dim for dim in dims if (dim != new_last_dim)], chunksizes))
        chunks[new_last_dim] = (- 1)
    else:
        chunks = copy.copy(chunks)
    tmp_chunks = {dim: chunks[dim] for dim in dims if (dim != new_last_dim)}
    size = dtype.itemsize
    chunksizes = [(size if (size != (- 1)) else dims[dim]) for (dim, size) in chunks.items()]
    chunksize_MB = ((np.prod(chunksizes) * size) / (1024 ** 2))
    img_shape = transposed_shape[:(- 1)]
    len_time = transposed_shape[(- 1)]
    imagesize_GB = ((np.prod(img_shape) * size) / (1024 ** 3))
    stepsize = (int(math.floor((memory / imagesize_GB))) // 2)
    stepsize = min(stepsize, len_time)
    tmp_chunks[new_last_dim] = stepsize
    tmp_chunks_str = str(tuple(tmp_chunks.values()))
    logging.info(f'write_transposed_dataset: Creating chunks {tmp_chunks_str} with chunksize {chunksize_MB:.2f} MB')
    return tmp_chunks