Beispiel #1
0
def new_color_mapped_image(ml_dataset, image_id, var_name, cmap_name,
                           cmap_range, z, labels, labels_are_indices,
                           tile_cache, trace_perf, exception_type):
    array, no_data_value, valid_range, var = _get_var_2d_array_and_mask_info(
        ml_dataset, var_name, z, labels, labels_are_indices, exception_type)
    cmap_name, cmap_range = get_var_cmap_params(var, cmap_name, cmap_range,
                                                valid_range)
    tile_grid = ml_dataset.tile_grid
    image = ArrayImage(array.values,
                       image_id=f'ai-{image_id}',
                       tile_size=tile_grid.tile_size,
                       trace_perf=trace_perf)
    image = TransformArrayImage(image,
                                image_id=f'tai-{image_id}',
                                flip_y=tile_grid.inv_y,
                                force_masked=True,
                                no_data_value=no_data_value,
                                valid_range=valid_range,
                                norm_range=cmap_range,
                                trace_perf=trace_perf)
    return ColorMappedRgbaImage(image,
                                image_id=f'cmrgbai-{image_id}',
                                cmap_name=cmap_name,
                                encode=True,
                                format='PNG',
                                tile_cache=tile_cache,
                                trace_perf=trace_perf)
Beispiel #2
0
def new_rgb_image(ml_dataset, image_id, var_names, norm_ranges, z, labels,
                  labels_are_indices, tile_cache, trace_perf, exception_type):
    tile_grid = ml_dataset.tile_grid
    images = []
    for i in range(3):
        var_name = var_names[i]
        array, no_data_value, valid_range, _ = _get_var_2d_array_and_mask_info(
            ml_dataset, var_name, z, labels, labels_are_indices,
            exception_type)
        image = ArrayImage(array,
                           image_id=f'ai-{image_id}',
                           tile_size=tile_grid.tile_size,
                           trace_perf=trace_perf)
        image = TransformArrayImage(image,
                                    image_id=f'tai-{image_id}',
                                    flip_y=tile_grid.inv_y,
                                    force_masked=True,
                                    no_data_value=no_data_value,
                                    valid_range=valid_range,
                                    norm_range=norm_ranges[i],
                                    trace_perf=trace_perf)
        images.append(image)

    return DirectRgbaImage((images[0], images[1], images[2]),
                           image_id=f'drgba-{image_id}',
                           encode=True,
                           format='PNG',
                           tile_cache=tile_cache,
                           trace_perf=trace_perf)
Beispiel #3
0
def get_ml_dataset_tile(ml_dataset: MultiLevelDataset,
                        var_name: str,
                        x: int,
                        y: int,
                        z: int,
                        labels: Mapping[str, Any] = None,
                        labels_are_indices: bool = False,
                        cmap_name: str = None,
                        cmap_vmin: float = None,
                        cmap_vmax: float = None,
                        image_cache: MutableMapping[str, TiledImage] = None,
                        tile_cache: Cache = None,
                        tile_comp_mode: int = 0,
                        trace_perf: bool = False,
                        exception_type: Type[Exception] = ValueError):
    measure_time = measure_time_cm(logger=_LOG, disabled=not trace_perf)

    dataset = ml_dataset.get_dataset(ml_dataset.num_levels - 1 - z)
    var = dataset[var_name]

    labels = labels or {}

    ds_id = hex(id(ml_dataset))
    image_id = '-'.join(
        map(str, [ds_id, z, var_name, cmap_name, cmap_vmin, cmap_vmax] + [
            f'{dim_name}={dim_value}'
            for dim_name, dim_value in labels.items()
        ]))

    if image_cache and image_id in image_cache:
        image = image_cache[image_id]
    else:
        no_data_value = var.attrs.get('_FillValue')
        valid_range = get_var_valid_range(var)
        cmap_name, cmap_vmin, cmap_vmax = get_var_cmap_params(
            var, cmap_name, cmap_vmin, cmap_vmax, valid_range)
        array = get_var_2d_array(var, labels, labels_are_indices,
                                 exception_type, ml_dataset.ds_id)
        tile_grid = ml_dataset.tile_grid

        if not tile_comp_mode:
            image = NdarrayImage(array.values,
                                 image_id=f'ndai-{image_id}',
                                 tile_size=tile_grid.tile_size,
                                 trace_perf=trace_perf)
            image = TransformArrayImage(image,
                                        image_id=f'tai-{image_id}',
                                        flip_y=tile_grid.inv_y,
                                        force_masked=True,
                                        no_data_value=no_data_value,
                                        valid_range=valid_range,
                                        trace_perf=trace_perf)
            image = ColorMappedRgbaImage(image,
                                         image_id=f'rgb-{image_id}',
                                         cmap_range=(cmap_vmin, cmap_vmax),
                                         cmap_name=cmap_name,
                                         encode=True,
                                         format='PNG',
                                         tile_cache=tile_cache,
                                         trace_perf=trace_perf)
        else:
            image = ColorMappedRgbaImage2(array.values,
                                          image_id=f'rgb-{image_id}',
                                          tile_size=tile_grid.tile_size,
                                          cmap_range=(cmap_vmin, cmap_vmax),
                                          cmap_name=cmap_name,
                                          encode=True,
                                          format='PNG',
                                          flip_y=tile_grid.inv_y,
                                          no_data_value=no_data_value,
                                          valid_range=valid_range,
                                          tile_cache=tile_cache,
                                          trace_perf=trace_perf)

        if image_cache:
            image_cache[image_id] = image

        if trace_perf:
            _LOG.info(
                f'Created tiled image {image_id!r} of size {image.size} with tile grid:'
            )
            _LOG.info(f'  num_levels: {tile_grid.num_levels}')
            _LOG.info(f'  num_level_zero_tiles: {tile_grid.num_tiles(0)}')
            _LOG.info(f'  tile_size: {tile_grid.tile_size}')
            _LOG.info(f'  geo_extent: {tile_grid.geo_extent}')
            _LOG.info(f'  inv_y: {tile_grid.inv_y}')

    if trace_perf:
        _LOG.info(f'>>> tile {image_id}/{z}/{y}/{x}')

    with measure_time() as measured_time:
        tile = image.get_tile(x, y)

    if trace_perf:
        _LOG.info(f'<<< tile {image_id}/{z}/{y}/{x}: took ' +
                  '%.2f seconds' % measured_time.duration)

    return tile
Beispiel #4
0
def get_dataset_tile(ctx: ServiceContext, ds_id: str, var_name: str, x: str,
                     y: str, z: str, params: RequestParams):
    x = RequestParams.to_int('x', x)
    y = RequestParams.to_int('y', y)
    z = RequestParams.to_int('z', z)

    tile_comp_mode = params.get_query_argument_int('mode', ctx.tile_comp_mode)
    trace_perf = params.get_query_argument_int('debug', ctx.trace_perf) != 0

    measure_time = measure_time_cm(logger=_LOG, disabled=not trace_perf)

    var = ctx.get_variable_for_z(ds_id, var_name, z)

    dim_names = list(var.dims)
    if 'lon' not in dim_names or 'lat' not in dim_names:
        raise ServiceBadRequestError(
            f'Variable "{var_name}" of dataset "{ds_id}" is not geo-spatial')

    dim_names.remove('lon')
    dim_names.remove('lat')

    var_indexers = ctx.get_var_indexers(ds_id, var_name, var, dim_names,
                                        params)

    cmap_cbar = params.get_query_argument('cbar', default=None)
    cmap_vmin = params.get_query_argument_float('vmin', default=None)
    cmap_vmax = params.get_query_argument_float('vmax', default=None)
    if cmap_cbar is None or cmap_vmin is None or cmap_vmax is None:
        default_cmap_cbar, default_cmap_vmin, default_cmap_vmax = ctx.get_color_mapping(
            ds_id, var_name)
        cmap_cbar = cmap_cbar or default_cmap_cbar
        cmap_vmin = cmap_vmin or default_cmap_vmin
        cmap_vmax = cmap_vmax or default_cmap_vmax

    image_id = '-'.join(
        map(str, [ds_id, z, var_name, cmap_cbar, cmap_vmin, cmap_vmax] + [
            f'{dim_name}={dim_value}'
            for dim_name, dim_value in var_indexers.items()
        ]))

    if image_id in ctx.image_cache:
        image = ctx.image_cache[image_id]
    else:
        no_data_value = var.attrs.get('_FillValue')
        valid_range = var.attrs.get('valid_range')
        if valid_range is None:
            valid_min = var.attrs.get('valid_min')
            valid_max = var.attrs.get('valid_max')
            if valid_min is not None and valid_max is not None:
                valid_range = [valid_min, valid_max]

        # Make sure we work with 2D image arrays only
        if var.ndim == 2:
            assert len(var_indexers) == 0
            array = var
        elif var.ndim > 2:
            assert len(var_indexers) == var.ndim - 2
            array = var.sel(method='nearest', **var_indexers)
        else:
            raise ServiceBadRequestError(
                f'Variable "{var_name}" of dataset "{var_name}" '
                'must be an N-D Dataset with N >= 2, '
                f'but "{var_name}" is only {var.ndim}-D')

        cmap_vmin = np.nanmin(
            array.values) if np.isnan(cmap_vmin) else cmap_vmin
        cmap_vmax = np.nanmax(
            array.values) if np.isnan(cmap_vmax) else cmap_vmax

        tile_grid = ctx.get_tile_grid(ds_id)

        if not tile_comp_mode:
            image = NdarrayImage(
                array,
                image_id=f'ndai-{image_id}',
                tile_size=tile_grid.tile_size,
                # tile_cache=ctx.tile_cache,
                trace_perf=trace_perf)
            image = TransformArrayImage(
                image,
                image_id=f'tai-{image_id}',
                flip_y=tile_grid.inv_y,
                force_masked=True,
                no_data_value=no_data_value,
                valid_range=valid_range,
                # tile_cache=ctx.tile_cache,
                trace_perf=trace_perf)
            image = ColorMappedRgbaImage(image,
                                         image_id=f'rgb-{image_id}',
                                         value_range=(cmap_vmin, cmap_vmax),
                                         cmap_name=cmap_cbar,
                                         encode=True,
                                         format='PNG',
                                         tile_cache=ctx.tile_cache,
                                         trace_perf=trace_perf)
        else:
            image = ColorMappedRgbaImage2(array,
                                          image_id=f'rgb-{image_id}',
                                          tile_size=tile_grid.tile_size,
                                          cmap_range=(cmap_vmin, cmap_vmax),
                                          cmap_name=cmap_cbar,
                                          encode=True,
                                          format='PNG',
                                          flip_y=tile_grid.inv_y,
                                          no_data_value=no_data_value,
                                          valid_range=valid_range,
                                          tile_cache=ctx.tile_cache,
                                          trace_perf=trace_perf)

        ctx.image_cache[image_id] = image
        if trace_perf:
            _LOG.info(
                f'Created tiled image {image_id!r} of size {image.size} with tile grid:'
            )
            _LOG.info(f'  num_levels: {tile_grid.num_levels}')
            _LOG.info(f'  num_level_zero_tiles: {tile_grid.num_tiles(0)}')
            _LOG.info(f'  tile_size: {tile_grid.tile_size}')
            _LOG.info(f'  geo_extent: {tile_grid.geo_extent}')
            _LOG.info(f'  inv_y: {tile_grid.inv_y}')

    if trace_perf:
        _LOG.info(f'>>> tile {image_id}/{z}/{y}/{x}')

    with measure_time() as measured_time:
        tile = image.get_tile(x, y)

    if trace_perf:
        _LOG.info(f'<<< tile {image_id}/{z}/{y}/{x}: took ' +
                  '%.2f seconds' % measured_time.duration)

    return tile