def _transpose_no_dask(reader: Reader, outfname: Union[(Path, str)], start: datetime.datetime=None, end: datetime.datetime=None, chunks: Tuple=None, memory: float=2, zlib: bool=True, complevel: int=4):
    warnings.warn('This is an experimental function and not yet ready for public use!')
    zarr_output = str(outfname).endswith('.zarr')
    new_last_dim = reader.timename
    timestamps = reader.tstamps_for_daterange(start, end)
    variable_fnames = {}
    variable_dims = {}
    for varname in reader.varnames:
        tmp_outfname = (str(outfname) + f'.{varname}.zarr')
        variable_fnames[varname] = tmp_outfname
        first_img = reader.read_block(start=timestamps[0], end=timestamps[0])[varname]
        tmp_chunks = _get_intermediate_chunks(first_img, chunks, new_last_dim, zarr_output, memory)
        new_dim_names = list(tmp_chunks)
        variable_dims[varname] = new_dim_names
        if Path(tmp_outfname).exists():
            logging.info(f'{str(tmp_outfname)} already exists, skipping.')
            continue
        logging.debug(f'write_transposed_dataset: starting zarr array creation for {len(timestamps)} timestamps')
        dims = dict(zip(first_img.dims, first_img.shape))
        transposed_shape = tuple((dims[dim] for dim in tmp_chunks.keys()))
        zarr_array = zarr.create(tuple(new_dim_sizes), chunks=tuple((size for size in tmp_chunks.values())), store=tmp_outfname, overwrite=True, fill_value=np.nan)
        logging.debug(f'write_transposed_dataset: Writing {tmp_outfname}')
        print(f'Constructing array stack for {varname}:')
        pbar = tqdm(range(0, len(timestamps), stepsize))
        stepsize = tmp_chunks[new_last_dim]
        for start_idx in pbar:
            pbar.set_description('Reading')
            end_idx = min(((start_idx + stepsize) - 1), (len(timestamps) - 1))
            block = reader.read_block(timestamps[start_idx], timestamps[end_idx])[varname]
            block = block.transpose(..., new_last_dim)
            pbar.set_description('Writing')
            zarr_array[..., start_idx:(end_idx + 1)] = block.values
    variable_arrays = {}
    encoding = {}
    for (varname, fname) in variable_fnames.items():
        logging.debug(f'Reading {str(fname)}')
        arr = da.from_zarr(fname)
        dims = variable_dims[varname]
        metadata = reader.array_attrs[varname]
        if (chunks is None):
            if zarr_output:
                chunks = infer_chunks(new_dim_sizes, 100, dtype)
            else:
                chunks = infer_chunks(new_dim_sizes, 1, dtype)
        encoding[varname] = {'chunksizes': chunks, 'zlib': zlib, 'complevel': complevel}
        chunk_dict = dict(zip(dims, chunks))
        arr = xr.DataArray(data=arr, dims=dims, attrs=metadata)
        arr = arr.chunk(chunk_dict)
        arr.encoding = encoding[varname]
        variable_arrays[varname] = arr
    logging.debug('Reading test image')
    test_img = reader.read_block(start=timestamps[0], end=timestamps[0])[reader.varnames[0]]
    coords = {c: test_img.coords[c] for c in test_img.coords if (c != reader.timename)}
    coords[reader.timename] = timestamps
    logging.debug('Creating dataset')
    ds = xr.Dataset(variable_arrays, coords=coords)
    ds.attrs.update(reader.global_attrs)
    logging.info(f'write_transposed_dataset: Writing combined file to {str(outfname)}')
    if (not zarr_output):
        ds.to_netcdf(outfname, encoding=encoding)
    else:
        ds.to_zarr(outfname, mode='w', consolidated=True)
    for fname in variable_fnames.values():
        shutil.rmtree(fname)
    logging.info('write_transposed_dataset: Finished writing transposed file.')
def _transpose_no_dask(reader: Reader, outfname: Union[(Path, str)], start: datetime.datetime=None, end: datetime.datetime=None, chunks: Tuple=None, memory: float=2, zlib: bool=True, complevel: int=4):
    warnings.warn('This is an experimental function and not yet ready for public use!')
    zarr_output = str(outfname).endswith('.zarr')
    new_last_dim = reader.timename
    timestamps = reader.tstamps_for_daterange(start, end)
    variable_fnames = {}
    variable_dims = {}
    for varname in reader.varnames:
        tmp_outfname = (str(outfname) + f'.{varname}.zarr')
        variable_fnames[varname] = tmp_outfname
        first_img = reader.read_block(start=timestamps[0], end=timestamps[0])[varname]
        tmp_chunks = _get_intermediate_chunks(first_img, chunks, new_last_dim, zarr_output, memory)
        new_dim_names = list(tmp_chunks)
        variable_dims[varname] = new_dim_names
        if Path(tmp_outfname).exists():
            logging.info(f'{str(tmp_outfname)} already exists, skipping.')
            continue
        logging.debug(f'write_transposed_dataset: starting zarr array creation for {len(timestamps)} timestamps')
        dims = dict(zip(first_img.dims, first_img.shape))
        transposed_shape = tuple((dims[dim] for dim in tmp_chunks.keys()))
        zarr_array = zarr.create(tuple(new_dim_sizes), chunks=tuple((size for size in tmp_chunks.values())), store=tmp_outfname, overwrite=True, fill_value=np.nan)
        logging.debug(f'write_transposed_dataset: Writing {tmp_outfname}')
        print(f'Constructing array stack for {varname}:')
        pbar = tqdm(range(0, len(timestamps), stepsize))
        stepsize = tmp_chunks[new_last_dim]
        for start_idx in pbar:
            pbar.set_description('Reading')
            end_idx = min(((start_idx + stepsize) - 1), (len(timestamps) - 1))
            block = reader.read_block(timestamps[start_idx], timestamps[end_idx])[varname]
            block = block.transpose(..., new_last_dim)
            pbar.set_description('Writing')
            zarr_array[..., start_idx:(end_idx + 1)] = block.values
    variable_arrays = {}
    encoding = {}
    for (varname, fname) in variable_fnames.items():
        logging.debug(f'Reading {str(fname)}')
        arr = da.from_zarr(fname)
        dims = variable_dims[varname]
        metadata = reader.array_attrs[varname]
        if (chunks is None):
            if zarr_output:
                chunks = infer_chunks(new_dim_sizes, 100, dtype)
            else:
                chunks = infer_chunks(new_dim_sizes, 1, dtype)
        encoding[varname] = {'chunksizes': chunks, 'zlib': zlib, 'complevel': complevel}
        chunk_dict = dict(zip(dims, chunks))
        arr = xr.DataArray(data=arr, dims=dims, attrs=metadata)
        arr = arr.chunk(chunk_dict)
        arr.encoding = encoding[varname]
        variable_arrays[varname] = arr
    logging.debug('Reading test image')
    test_img = reader.read_block(start=timestamps[0], end=timestamps[0])[reader.varnames[0]]
    coords = {c: test_img.coords[c] for c in test_img.coords if (c != reader.timename)}
    coords[reader.timename] = timestamps
    logging.debug('Creating dataset')
    ds = xr.Dataset(variable_arrays, coords=coords)
    ds.attrs.update(reader.global_attrs)
    logging.info(f'write_transposed_dataset: Writing combined file to {str(outfname)}')
    if (not zarr_output):
        ds.to_netcdf(outfname, encoding=encoding)
    else:
        ds.to_zarr(outfname, mode='w', consolidated=True)
    for fname in variable_fnames.values():
        shutil.rmtree(fname)
    logging.info('write_transposed_dataset: Finished writing transposed file.')