Exemplo n.º 1
0
Arquivo: level.py Projeto: manzt/xcube
def level(input, output, link, tile_size, num_levels_max):
    """
    Generate multi-resolution levels.
    Transform the given dataset by INPUT into the levels of a multi-level pyramid with spatial resolution
    decreasing by a factor of two in both spatial dimensions and write the result to directory OUTPUT.
    """
    import time
    import os
    from xcube.cli.common import parse_cli_sequence
    from xcube.cli.common import assert_positive_int_item
    from xcube.core.level import write_levels

    input_path = input
    output_path = output
    link_input = link

    if num_levels_max is not None and num_levels_max < 1:
        raise click.ClickException(
            f"NUM_LEVELS_MAX must be a positive integer")

    if not output_path:
        basename, ext = os.path.splitext(input_path)
        output_path = os.path.join(os.path.dirname(input_path),
                                   basename + ".levels")

    if os.path.exists(output_path):
        raise click.ClickException(f"output {output_path!r} already exists")

    spatial_tile_shape = None
    if tile_size is not None:
        tile_size = parse_cli_sequence(tile_size,
                                       metavar='TILE_SIZE',
                                       num_items=2,
                                       item_plural_name='tile sizes',
                                       item_parser=int,
                                       item_validator=assert_positive_int_item)
        spatial_tile_shape = tile_size[1], tile_size[0]

    start_time = t0 = time.perf_counter()

    # noinspection PyUnusedLocal
    def progress_monitor(dataset, index, num_levels):
        nonlocal t0
        print(
            f"Level {index + 1} of {num_levels} written after {time.perf_counter() - t0} seconds"
        )
        t0 = time.perf_counter()

    levels = write_levels(output_path,
                          input_path=input_path,
                          link_input=link_input,
                          progress_monitor=progress_monitor,
                          spatial_tile_shape=spatial_tile_shape,
                          num_levels_max=num_levels_max)
    print(
        f"{len(levels)} level(s) written into {output_path} after {time.perf_counter() - start_time} seconds"
    )
Exemplo n.º 2
0
    def _assert_io_ok(self, shape, tile_shape, link_input: bool,
                      expected_num_levels, expected_shapes, expected_chunks):

        input_path = get_path("pyramid-input.zarr")
        output_path = get_path("pyramid-output")

        rimraf(input_path)
        rimraf(output_path)

        try:
            dataset = self.create_test_dataset(shape, chunks=(1, *tile_shape))
            dataset.to_zarr(input_path)

            t0 = time.perf_counter()

            levels = write_levels(output_path,
                                  dataset=dataset,
                                  spatial_tile_shape=tile_shape,
                                  input_path=input_path,
                                  link_input=link_input)

            print(f"write time total: ", time.perf_counter() - t0)

            self._assert_levels_ok(levels, expected_num_levels,
                                   expected_shapes, expected_chunks)

            t0 = time.perf_counter()

            levels = read_levels(output_path)

            print(f"read time total: ", time.perf_counter() - t0)

            self._assert_levels_ok(levels, expected_num_levels,
                                   expected_shapes, expected_chunks)

        finally:
            rimraf(input_path)
            rimraf(output_path)
Exemplo n.º 3
0
def level(input, output, link, tile_size, num_levels_max):
    """
    Generate multi-resolution levels.
    Transform the given dataset by INPUT into the levels of a multi-level pyramid with spatial resolution
    decreasing by a factor of two in both spatial dimensions and write the result to directory OUTPUT.
    """
    import time
    import os
    from xcube.core.level import write_levels

    input_path = input
    output_path = output
    link_input = link

    if num_levels_max is not None and num_levels_max < 1:
        raise click.ClickException(
            f"NUM-LEVELS-MAX must be a positive integer")

    if not output_path:
        basename, ext = os.path.splitext(input_path)
        output_path = os.path.join(os.path.dirname(input_path),
                                   basename + ".levels")

    if os.path.exists(output_path):
        raise click.ClickException(f"output {output_path!r} already exists")

    spatial_tile_shape = None
    if tile_size is not None:
        try:
            tile_size = int(tile_size)
            tile_size = tile_size, tile_size
        except ValueError:
            tile_size = map(int, tile_size.split(","))
            if tile_size != 2:
                raise click.ClickException(
                    "Expected a pair of positive integers <tile-width>,<tile-height>"
                )
        if tile_size[0] < 1 or tile_size[1] < 1:
            raise click.ClickException(
                "TILE-SIZE must comprise positive integers")
        spatial_tile_shape = tile_size[1], tile_size[0]

    start_time = t0 = time.perf_counter()

    # noinspection PyUnusedLocal
    def progress_monitor(dataset, index, num_levels):
        nonlocal t0
        print(
            f"level {index + 1} of {num_levels} written after {time.perf_counter() - t0} seconds"
        )
        t0 = time.perf_counter()

    levels = write_levels(output_path,
                          input_path=input_path,
                          link_input=link_input,
                          progress_monitor=progress_monitor,
                          spatial_tile_shape=spatial_tile_shape,
                          num_levels_max=num_levels_max)
    print(
        f"{len(levels)} level(s) written into {output_path} after {time.perf_counter() - start_time} seconds"
    )