Ejemplo n.º 1
0
def get_cmap(cmap_name: str,
             default_cmap_name='viridis',
             num_colors: int = None):
    """
    Get color mapping for color bar name *cmap_name*.

    If *num_colors* is a positive integer,
    a resampled mapping will be returned that contains *num_colors* color entries.

    If *cmap_name* is not defined, *default_cmap_name* is used.
    If *default_cmap_name* is undefined too, a ValueError is raised.

    Otherwise, a tuple (actual_cmap_name, cmap) is returned.

    :param cmap_name: Color bar name.
    :param num_colors: Number of colours in returned color mapping.
    :param default_cmap_name: Default color bar name.
    :return: A tuple (actual_cmap_name, cmap).
    """
    ensure_cmaps_loaded()
    try:
        cmap = cm.get_cmap(cmap_name, num_colors)
    except ValueError as e:
        LOG.warning(str(e))
        LOG.warning(
            f'color map name {cmap_name!r} undefined, falling back to {default_cmap_name!r}'
        )
        cmap_name = default_cmap_name
        cmap = cm.get_cmap(cmap_name, num_colors)
    return cmap_name, cmap
Ejemplo n.º 2
0
 def start(self):
     address = self.service_info['address']
     port = self.service_info['port']
     test_url = self.context.get_service_url(f"http://{address}:{port}", "datasets")
     LOG.info(f'service running, listening on {address}:{port}, try {test_url}')
     LOG.info(f'press CTRL+C to stop service')
     if len(self.context.config.get('Datasets', {})) == 0:
         LOG.warning('no datasets configured')
     tornado.ioloop.PeriodicCallback(self._try_shutdown, 100).start()
     IOLoop.current().start()
Ejemplo n.º 3
0
def _get_custom_colormap(colortext):
    try:
        colors = _get_color(colortext)
        values = get_tick_val_col(colortext)
        if colors is None or values is None:
            return
        norm = plt.Normalize(min(values), max(values))
        tuples = list(zip(map(norm, values), colors))
        cmap = matplotlib.colors.LinearSegmentedColormap.from_list(colortext, tuples)
    except FileNotFoundError:
        LOG.warning('No such file or directory: "%s"' % colortext)
        return
    return cmap
Ejemplo n.º 4
0
def get_tick_val_col(colortext):
    f = open(colortext, "r")
    lines = f.readlines()
    values = []
    if any('sample' in line for line in lines):
        for line in lines:
            if "sample" in line:
                value = ((re.split(r'\W+', line, 1)[1:])[0].strip())
                values.append(float(value))
    else:
        LOG.warning('Keyword "sample" not found. SNAP .cpd file invalid.')
        return
    f.close()
    return values
Ejemplo n.º 5
0
def _get_color(colortext):
    f = open(colortext, "r")
    lines = f.readlines()
    c = []
    if any('color' in line for line in lines):
        for line in lines:
            if "color" in line:
                r, g, b = (((re.split(r'\W+', line, 1)[1:])[0].strip()).split(','))
                hex_col = ('#%02x%02x%02x' % (int(r), int(g), int(b)))
                c.append(hex_col)
    else:
        LOG.warning('Keyword "color" not found. SNAP .cpd file invalid.')
        return
    f.close()
    return c
Ejemplo n.º 6
0
 def _sig_handler(self, sig, frame):
     LOG.warning(f'caught signal {sig}')
     self._shutdown_requested = True
Ejemplo n.º 7
0
def ensure_cmaps_loaded():
    """
    Loads all color maps from matplotlib and registers additional ones, if not done before.
    """
    from xcube.webapi.service import SNAP_CPD_LIST
    global _CBARS_LOADED, _CMAPS
    if not _CBARS_LOADED:
        _LOCK.acquire()
        if not _CBARS_LOADED:

            new_cmaps = []
            for cmap_category, cmap_description, cmap_names in _CMAPS:
                if cmap_category == 'Ocean' and ocm is None:
                    continue
                cbar_list = []
                if cmap_category == 'Custom SNAP Colormaps':
                    cmap_names = _check_if_exists(SNAP_CPD_LIST)
                for cmap_name in cmap_names:
                    try:
                        if '.cpd' in cmap_name:
                            cmap = _get_custom_colormap(cmap_name)
                            cm.register_cmap(cmap=cmap)
                        elif cmap_category == 'Ocean':
                            cmap = getattr(ocm, cmap_name)
                            cm.register_cmap(cmap=cmap)
                        else:
                            cmap = cm.get_cmap(cmap_name)
                    except ValueError:
                        LOG.warning('detected invalid colormap "%s"' %
                                    cmap_name)
                        continue
                    # Add extra colormaps with alpha gradient
                    # see http://matplotlib.org/api/colors_api.html
                    if type(cmap) == matplotlib.colors.LinearSegmentedColormap:
                        new_name = cmap.name + '_alpha'
                        new_segmentdata = dict(cmap._segmentdata)
                        # let alpha increase from 0.0 to 0.5
                        new_segmentdata['alpha'] = ((0.0, 0.0, 0.0),
                                                    (0.5, 1.0, 1.0), (1.0, 1.0,
                                                                      1.0))
                        new_cmap = matplotlib.colors.LinearSegmentedColormap(
                            new_name, new_segmentdata)
                        cm.register_cmap(cmap=new_cmap)
                    elif type(cmap) == matplotlib.colors.ListedColormap:
                        new_name = cmap.name + '_alpha'
                        new_colors = list(cmap.colors)
                        a_slope = 2.0 / cmap.N
                        a = 0
                        for i in range(len(new_colors)):
                            new_color = new_colors[i]
                            if not isinstance(new_color, str):
                                if len(new_color) == 3:
                                    r, g, b = new_color
                                    new_colors[i] = r, g, b, a
                                elif len(new_color) == 4:
                                    r, g, b, a_old = new_color
                                    new_colors[i] = r, g, b, min(a, a_old)
                            a += a_slope
                            if a > 1.0:
                                a = 1.0
                        new_cmap = matplotlib.colors.ListedColormap(
                            new_colors, name=new_name)
                        cm.register_cmap(cmap=new_cmap)
                    else:
                        new_name = cmap.name + '_alpha' if hasattr(
                            cmap, 'name') else 'unknown'
                        LOG.warning(
                            'could not create colormap "{}" because "{}" is of unknown type {}'
                            .format(new_name, cmap.name, type(cmap)))

                    cbar_list.append((cmap_name, _get_cbar_png_bytes(cmap)))
                    cbar_list.append((new_name, _get_cbar_png_bytes(new_cmap)))

                new_cmaps.append(
                    (cmap_category, cmap_description, tuple(cbar_list)))
            _CMAPS = tuple(new_cmaps)
            _CBARS_LOADED = True
            # import pprint
            # pprint.pprint(_CMAPS)
        _LOCK.release()
Ejemplo n.º 8
0
def get_datasets(ctx: ServiceContext,
                 details: bool = False,
                 client: str = None,
                 point: Tuple[float, float] = None,
                 base_url: str = None,
                 granted_scopes: Set[str] = None) -> Dict:
    granted_scopes = granted_scopes or set()

    dataset_configs = list(ctx.get_dataset_configs())

    dataset_dicts = list()
    for dataset_config in dataset_configs:

        ds_id = dataset_config['Identifier']

        if dataset_config.get('Hidden'):
            continue

        if 'read:dataset:*' not in granted_scopes:
            required_scopes = ctx.get_required_dataset_scopes(dataset_config)
            is_substitute = dataset_config \
                .get('AccessControl', {}) \
                .get('IsSubstitute', False)
            if not check_scopes(required_scopes,
                                granted_scopes,
                                is_substitute=is_substitute):
                continue

        dataset_dict = dict(id=ds_id)

        dataset_dict['title'] = ds_id
        if 'Title' in dataset_config:
            ds_title = dataset_config['Title']
            if ds_title and isinstance(ds_title, str):
                dataset_dict['title'] = ds_title

        if 'BoundingBox' in dataset_config:
            ds_bbox = dataset_config['BoundingBox']
            if ds_bbox \
                    and len(ds_bbox) == 4 \
                    and all(map(lambda c: isinstance(c, float)
                                          or isinstance(c, int),
                                ds_bbox)):
                dataset_dict['bbox'] = ds_bbox

        dataset_dicts.append(dataset_dict)

        # Important note:
        # the "point" parameter is used by
        # the CyanoAlert app only

        if details or point:
            filtered_dataset_dicts = []
            for dataset_dict in dataset_dicts:
                ds_id = dataset_dict["id"]
                try:
                    if point:
                        ds = ctx.get_dataset(ds_id)
                        if "bbox" not in dataset_dict:
                            dataset_dict["bbox"] = list(get_dataset_bounds(ds))
                    if details:
                        dataset_dict.update(
                            get_dataset(ctx,
                                        ds_id,
                                        client,
                                        base_url,
                                        granted_scopes=granted_scopes))
                    filtered_dataset_dicts.append(dataset_dict)
                except (DatasetIsNotACubeError, CubeIsNotDisplayable) as e:
                    LOG.warning(f'skipping dataset {ds_id}: {e}')
            dataset_dicts = filtered_dataset_dicts
    if point:
        is_point_in_dataset_bbox = functools.partial(_is_point_in_dataset_bbox,
                                                     point)
        # noinspection PyTypeChecker
        dataset_dicts = list(filter(is_point_in_dataset_bbox, dataset_dicts))

    return dict(datasets=dataset_dicts)