def _transpose(reader: Reader, outfname: Union[(Path, str)], start: datetime.datetime=None, end: datetime.datetime=None, chunks: dict=None, memory: float=2, zlib: bool=True, complevel: int=4):
    zarr_output = str(outfname).endswith('.zarr')
    new_last_dim = reader.timename
    if (isinstance(reader, DirectoryImageReader) and (reader.chunks is None)):
        logging.info('You are using DirectoryImageReader without dask. If you run into memory issues or have large datasets to transpose, consider setting use_dask=True in the constructor of DirectoryImageReader.')
    ds = reader.read_block(start, end)
    variable_chunks = {}
    variable_intermediate_fnames = {}
    for var in reader.varnames:
        tmp_outfname = (str(outfname) + f'.{var}.zarr')
        variable_intermediate_fnames[var] = tmp_outfname
        if Path(tmp_outfname).exists():
            logging.info('Skipping generating intermediate file {tmp_outfname} because it exists')
            continue
        tmp_chunks = _get_intermediate_chunks(ds[var], chunks, new_last_dim, zarr_output, memory)
        chunks = copy.copy(tmp_chunks)
        chunks[new_last_dim] = len(ds[var].time)
        variable_chunks[var] = chunks
        rechunked_transposed = ds[var].chunk(tmp_chunks).transpose(..., new_last_dim)
        rechunked_transposed.to_dataset().to_zarr(tmp_outfname, consolidated=True)
    variable_ds = []
    variable_chunksizes = {}
    for var in reader.varnames:
        ds = xr.open_zarr(variable_intermediate_fnames[var], consolidated=True)
        variable_ds.append(ds)
        transposed_dims = ds[var].dims
        variable_chunksizes[var] = tuple((chunks[dim] for dim in transposed_dims))
    ds = xr.merge(variable_ds, compat='override', join='override', combine_attrs='override')
    ds.attrs.update(reader.global_attrs)
    encoding = {var: {'chunksizes': variable_chunksizes[var], 'zlib': zlib, 'complevel': complevel} for var in reader.varnames}
    if (not zarr_output):
        ds.to_netcdf(outfname, encoding=encoding)
    else:
        for var in reader.varnames:
            del ds[var].encoding['chunks']
            del ds[var].encoding['preferred_chunks']
            ds[var] = ds[var].chunk(variable_chunksizes[var])
        ds.to_zarr(outfname, mode='w', consolidated=True)
    for var in reader.varnames:
        shutil.rmtree(variable_intermediate_fnames[var])
    logging.info('write_transposed_dataset: Finished writing transposed file.')
def _transpose(reader: Reader, outfname: Union[(Path, str)], start: datetime.datetime=None, end: datetime.datetime=None, chunks: dict=None, memory: float=2, zlib: bool=True, complevel: int=4):
    zarr_output = str(outfname).endswith('.zarr')
    new_last_dim = reader.timename
    if (isinstance(reader, DirectoryImageReader) and (reader.chunks is None)):
        logging.info('You are using DirectoryImageReader without dask. If you run into memory issues or have large datasets to transpose, consider setting use_dask=True in the constructor of DirectoryImageReader.')
    ds = reader.read_block(start, end)
    variable_chunks = {}
    variable_intermediate_fnames = {}
    for var in reader.varnames:
        tmp_outfname = (str(outfname) + f'.{var}.zarr')
        variable_intermediate_fnames[var] = tmp_outfname
        if Path(tmp_outfname).exists():
            logging.info('Skipping generating intermediate file {tmp_outfname} because it exists')
            continue
        tmp_chunks = _get_intermediate_chunks(ds[var], chunks, new_last_dim, zarr_output, memory)
        chunks = copy.copy(tmp_chunks)
        chunks[new_last_dim] = len(ds[var].time)
        variable_chunks[var] = chunks
        rechunked_transposed = ds[var].chunk(tmp_chunks).transpose(..., new_last_dim)
        rechunked_transposed.to_dataset().to_zarr(tmp_outfname, consolidated=True)
    variable_ds = []
    variable_chunksizes = {}
    for var in reader.varnames:
        ds = xr.open_zarr(variable_intermediate_fnames[var], consolidated=True)
        variable_ds.append(ds)
        transposed_dims = ds[var].dims
        variable_chunksizes[var] = tuple((chunks[dim] for dim in transposed_dims))
    ds = xr.merge(variable_ds, compat='override', join='override', combine_attrs='override')
    ds.attrs.update(reader.global_attrs)
    encoding = {var: {'chunksizes': variable_chunksizes[var], 'zlib': zlib, 'complevel': complevel} for var in reader.varnames}
    if (not zarr_output):
        ds.to_netcdf(outfname, encoding=encoding)
    else:
        for var in reader.varnames:
            del ds[var].encoding['chunks']
            del ds[var].encoding['preferred_chunks']
            ds[var] = ds[var].chunk(variable_chunksizes[var])
        ds.to_zarr(outfname, mode='w', consolidated=True)
    for var in reader.varnames:
        shutil.rmtree(variable_intermediate_fnames[var])
    logging.info('write_transposed_dataset: Finished writing transposed file.')