import click
from arbol.arbol import aprint, asection

from dexp.cli.defaults import DEFAULT_CLEVEL, DEFAULT_CODEC, DEFAULT_STORE
from dexp.cli.parsing import _get_output_path, _parse_channels, _parse_chunks
from dexp.datasets.open_dataset import glob_datasets
from dexp.datasets.operations.crop import dataset_crop


@click.command()
@click.argument("input_paths", nargs=-1)  # ,  help='input path'
@click.option("--output_path", "-o")  # , help='output path'
@click.option("--channels", "-c", default=None, help="List of channels, all channels when ommited.")
@click.option(
    "--quantile",
    "-q",
    default=0.99,
    type=float,
    help="Quantile parameter for lower bound of brightness for thresholding.",
    show_default=True,
)
@click.option(
    "--reference-channel",
    "-rc",
    default=None,
    help="Reference channel to estimate cropping. If no provided it picks the first one.",
)
@click.option("--store", "-st", default=DEFAULT_STORE, help="Zarr store: ‘dir’, ‘ndir’, or ‘zip’", show_default=True)
@click.option("--chunks", "-chk", default=None, help="Dataset chunks dimensions, e.g. (1, 126, 512, 512).")
@click.option(
    "--codec",
    "-z",
    default=DEFAULT_CODEC,
    help="Compression codec: zstd for ’, ‘blosclz’, ‘lz4’, ‘lz4hc’, ‘zlib’ or ‘snappy’ ",
    show_default=True,
)
@click.option("--clevel", "-l", type=int, default=DEFAULT_CLEVEL, help="Compression level", show_default=True)
@click.option("--overwrite", "-w", is_flag=True, help="Forces overwrite of target", show_default=True)
@click.option(
    "--workers",
    "-wk",
    default=-4,
    help="Number of worker threads to spawn. Negative numbers n correspond to: number_of _cores / |n| ",
    show_default=True,
)  #
@click.option("--check", "-ck", default=True, help="Checking integrity of written file.", show_default=True)  #
def crop(
    input_paths,
    output_path,
    channels,
    quantile,
    reference_channel,
    store,
    chunks,
    codec,
    clevel,
    overwrite,
    workers,
    check,
):

    input_dataset, input_paths = glob_datasets(input_paths)
    output_path = _get_output_path(input_paths[0], output_path, "_crop")
    channels = _parse_channels(input_dataset, channels)
    if reference_channel is None:
        reference_channel = input_dataset.channels()[0]
    chunks = _parse_chunks(chunks)

    with asection(
        f"Cropping from: {input_paths} to {output_path} for channels: {channels}, "
        f"using channel {reference_channel} as a reference."
    ):

        dataset_crop(
            input_dataset,
            output_path,
            channels=channels,
            reference_channel=reference_channel,
            quantile=quantile,
            store=store,
            chunks=chunks,
            compression=codec,
            compression_level=clevel,
            overwrite=overwrite,
            workers=workers,
            check=check,
        )

        input_dataset.close()
        aprint("Done!")

[((319, 334), 'click.command', 'click.command', ([], {}), '()\n', (332, 334), False, 'import click\n'), ((336, 375), 'click.argument', 'click.argument', (['"""input_paths"""'], {'nargs': '(-1)'}), "('input_paths', nargs=-1)\n", (350, 375), False, 'import click\n'), ((401, 436), 'click.option', 'click.option', (['"""--output_path"""', '"""-o"""'], {}), "('--output_path', '-o')\n", (413, 436), False, 'import click\n'), ((462, 566), 'click.option', 'click.option', (['"""--channels"""', '"""-c"""'], {'default': 'None', 'help': '"""List of channels, all channels when ommited."""'}), "('--channels', '-c', default=None, help=\n    'List of channels, all channels when ommited.')\n", (474, 566), False, 'import click\n'), ((563, 724), 'click.option', 'click.option', (['"""--quantile"""', '"""-q"""'], {'default': '(0.99)', 'type': 'float', 'help': '"""Quantile parameter for lower bound of brightness for thresholding."""', 'show_default': '(True)'}), "('--quantile', '-q', default=0.99, type=float, help=\n    'Quantile parameter for lower bound of brightness for thresholding.',\n    show_default=True)\n", (575, 724), False, 'import click\n'), ((744, 897), 'click.option', 'click.option', (['"""--reference-channel"""', '"""-rc"""'], {'default': 'None', 'help': '"""Reference channel to estimate cropping. If no provided it picks the first one."""'}), "('--reference-channel', '-rc', default=None, help=\n    'Reference channel to estimate cropping. If no provided it picks the first one.'\n    )\n", (756, 897), False, 'import click\n'), ((908, 1029), 'click.option', 'click.option', (['"""--store"""', '"""-st"""'], {'default': 'DEFAULT_STORE', 'help': '"""Zarr store: ‘dir’, ‘ndir’, or ‘zip’"""', 'show_default': '(True)'}), "('--store', '-st', default=DEFAULT_STORE, help=\n    'Zarr store: ‘dir’, ‘ndir’, or ‘zip’', show_default=True)\n", (920, 1029), False, 'import click\n'), ((1026, 1137), 'click.option', 'click.option', (['"""--chunks"""', '"""-chk"""'], {'default': 'None', 'help': '"""Dataset chunks dimensions, e.g. (1, 126, 512, 512)."""'}), "('--chunks', '-chk', default=None, help=\n    'Dataset chunks dimensions, e.g. (1, 126, 512, 512).')\n", (1038, 1137), False, 'import click\n'), ((1134, 1301), 'click.option', 'click.option', (['"""--codec"""', '"""-z"""'], {'default': 'DEFAULT_CODEC', 'help': '"""Compression codec: zstd for ’, ‘blosclz’, ‘lz4’, ‘lz4hc’, ‘zlib’ or ‘snappy’ """', 'show_default': '(True)'}), "('--codec', '-z', default=DEFAULT_CODEC, help=\n    'Compression codec: zstd for ’, ‘blosclz’, ‘lz4’, ‘lz4hc’, ‘zlib’ or ‘snappy’ '\n    , show_default=True)\n", (1146, 1301), False, 'import click\n'), ((1316, 1430), 'click.option', 'click.option', (['"""--clevel"""', '"""-l"""'], {'type': 'int', 'default': 'DEFAULT_CLEVEL', 'help': '"""Compression level"""', 'show_default': '(True)'}), "('--clevel', '-l', type=int, default=DEFAULT_CLEVEL, help=\n    'Compression level', show_default=True)\n", (1328, 1430), False, 'import click\n'), ((1427, 1533), 'click.option', 'click.option', (['"""--overwrite"""', '"""-w"""'], {'is_flag': '(True)', 'help': '"""Forces overwrite of target"""', 'show_default': '(True)'}), "('--overwrite', '-w', is_flag=True, help=\n    'Forces overwrite of target', show_default=True)\n", (1439, 1533), False, 'import click\n'), ((1530, 1704), 'click.option', 'click.option', (['"""--workers"""', '"""-wk"""'], {'default': '(-4)', 'help': '"""Number of worker threads to spawn. Negative numbers n correspond to: number_of _cores / |n| """', 'show_default': '(True)'}), "('--workers', '-wk', default=-4, help=\n    'Number of worker threads to spawn. Negative numbers n correspond to: number_of _cores / |n| '\n    , show_default=True)\n", (1542, 1704), False, 'import click\n'), ((1722, 1834), 'click.option', 'click.option', (['"""--check"""', '"""-ck"""'], {'default': '(True)', 'help': '"""Checking integrity of written file."""', 'show_default': '(True)'}), "('--check', '-ck', default=True, help=\n    'Checking integrity of written file.', show_default=True)\n", (1734, 1834), False, 'import click\n'), ((2050, 2076), 'dexp.datasets.open_dataset.glob_datasets', 'glob_datasets', (['input_paths'], {}), '(input_paths)\n', (2063, 2076), False, 'from dexp.datasets.open_dataset import glob_datasets\n'), ((2095, 2149), 'dexp.cli.parsing._get_output_path', '_get_output_path', (['input_paths[0]', 'output_path', '"""_crop"""'], {}), "(input_paths[0], output_path, '_crop')\n", (2111, 2149), False, 'from dexp.cli.parsing import _get_output_path, _parse_channels, _parse_chunks\n'), ((2165, 2205), 'dexp.cli.parsing._parse_channels', '_parse_channels', (['input_dataset', 'channels'], {}), '(input_dataset, channels)\n', (2180, 2205), False, 'from dexp.cli.parsing import _get_output_path, _parse_channels, _parse_chunks\n'), ((2309, 2330), 'dexp.cli.parsing._parse_chunks', '_parse_chunks', (['chunks'], {}), '(chunks)\n', (2322, 2330), False, 'from dexp.cli.parsing import _get_output_path, _parse_channels, _parse_chunks\n'), ((2341, 2485), 'arbol.arbol.asection', 'asection', (['f"""Cropping from: {input_paths} to {output_path} for channels: {channels}, using channel {reference_channel} as a reference."""'], {}), "(\n    f'Cropping from: {input_paths} to {output_path} for channels: {channels}, using channel {reference_channel} as a reference.'\n    )\n", (2349, 2485), False, 'from arbol.arbol import aprint, asection\n'), ((2512, 2764), 'dexp.datasets.operations.crop.dataset_crop', 'dataset_crop', (['input_dataset', 'output_path'], {'channels': 'channels', 'reference_channel': 'reference_channel', 'quantile': 'quantile', 'store': 'store', 'chunks': 'chunks', 'compression': 'codec', 'compression_level': 'clevel', 'overwrite': 'overwrite', 'workers': 'workers', 'check': 'check'}), '(input_dataset, output_path, channels=channels,\n    reference_channel=reference_channel, quantile=quantile, store=store,\n    chunks=chunks, compression=codec, compression_level=clevel, overwrite=\n    overwrite, workers=workers, check=check)\n', (2524, 2764), False, 'from dexp.datasets.operations.crop import dataset_crop\n'), ((2946, 2961), 'arbol.arbol.aprint', 'aprint', (['"""Done!"""'], {}), "('Done!')\n", (2952, 2961), False, 'from arbol.arbol import aprint, asection\n')]