From 748b8adf8363e485a4abfa49f39d94346272698c Mon Sep 17 00:00:00 2001 From: AliceBalfanz Date: Tue, 9 Jul 2019 10:11:40 +0200 Subject: [PATCH] including custom SNAP colormaps to tests --- test/webapi/im/test_cmaps.py | 3 ++- xcube/webapi/im/cmaps.py | 45 +++++++++++++++++++++++------------- xcube/webapi/service.py | 24 +++++++++++-------- 3 files changed, 46 insertions(+), 26 deletions(-) diff --git a/test/webapi/im/test_cmaps.py b/test/webapi/im/test_cmaps.py index bff82775c..ab470eff0 100644 --- a/test/webapi/im/test_cmaps.py +++ b/test/webapi/im/test_cmaps.py @@ -26,7 +26,7 @@ def test_get_cmaps_retruns_equal_size_recs(self): def test_get_cmaps_categories(self): cmaps = get_cmaps() - self.assertGreaterEqual(len(cmaps), 7) + self.assertGreaterEqual(len(cmaps), 8) self.assertEqual(cmaps[0][0], 'Perceptually Uniform Sequential') self.assertEqual(cmaps[1][0], 'Sequential 1') self.assertEqual(cmaps[2][0], 'Sequential 2') @@ -34,6 +34,7 @@ def test_get_cmaps_categories(self): self.assertEqual(cmaps[4][0], 'Qualitative') self.assertEqual(cmaps[5][0], 'Ocean') self.assertEqual(cmaps[6][0], 'Miscellaneous') + self.assertEqual(cmaps[7][0], 'Custom SNAP Colormaps') def test_get_cmaps_category_descr(self): cmaps = get_cmaps() diff --git a/xcube/webapi/im/cmaps.py b/xcube/webapi/im/cmaps.py index b9fd801ed..502266f7a 100644 --- a/xcube/webapi/im/cmaps.py +++ b/xcube/webapi/im/cmaps.py @@ -93,9 +93,11 @@ ('Custom SNAP Colormaps', 'Custom SNAP colormaps derived from a .cpd file. ', ())) + _CBARS_LOADED = False _LOCK = Lock() + def get_cmaps(): """ Return a JSON-serializable tuple containing records of the form: @@ -127,6 +129,8 @@ def ensure_cmaps_loaded(): cbar_list = [] if cmap_category == 'Custom SNAP Colormaps': cmap_names = tuple(SNAP_CPD_LIST) + if len(cmap_names) == 0: + _LOG.warning('No custom SNAP colormaps found in server configuration file.') for cmap_name in cmap_names: try: if '.cpd' in cmap_name: @@ -208,25 +212,30 @@ def _get_cbar_png_bytes(cmap): def _get_custom_colormap(colortext): - colors = _get_color(colortext) - values = get_tick_val_col(colortext) - - norm = plt.Normalize(min(values), max(values)) - tuples = list(zip(map(norm, values), colors)) - cmap = matplotlib.colors.LinearSegmentedColormap.from_list("", tuples) + 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("", tuples) - return cmap + return cmap def _get_color(colortext): f = open(colortext, "r") lines = f.readlines() c = [] - for x in lines: - if "color" in x: - r, g, b = (((re.split('\W+', x, 1)[1:])[0].strip()).split(',')) - hex_col = ('#%02x%02x%02x' % (int(r), int(g), int(b))) - c.append(hex_col) + if any('color' in line for line in lines): + for line in lines: + if "color" in line: + r, g, b = (((re.split('\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 @@ -235,10 +244,14 @@ def get_tick_val_col(colortext): f = open(colortext, "r") lines = f.readlines() values = [] - for x in lines: - if "sample" in x: - value = ((re.split('\W+', x, 1)[1:])[0].strip()) - values.append(float(value)) + if any('sample' in line for line in lines): + for line in lines: + if "sample" in line: + value = ((re.split('\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 diff --git a/xcube/webapi/service.py b/xcube/webapi/service.py index 3a59c743d..7b6f24140 100644 --- a/xcube/webapi/service.py +++ b/xcube/webapi/service.py @@ -100,15 +100,7 @@ def __init__(self, global SNAP_CPD_LIST if config_file: - config = load_configs(config_file) if config_file else {} - x = config['Styles'] - for style in x: - cm = style['ColorMappings'] - for key in cm.keys(): - if 'ColorFile' in cm[key]: - cf = cm[key]['ColorFile'] - if cf not in SNAP_CPD_LIST: - SNAP_CPD_LIST.append(cf) + SNAP_CPD_LIST = _get_custom_color_list(config_file) log_dir = os.path.dirname(log_file_prefix) if log_dir and not os.path.isdir(log_dir): @@ -440,3 +432,17 @@ def new_default_config(cube_paths: List[str], styles: Dict[str, Tuple] = None): color_mappings[var_name] = style config["Styles"] = [dict(Identifier="default", ColorMappings=color_mappings)] return config + + +def _get_custom_color_list(config_file): + global SNAP_CPD_LIST + config = load_configs(config_file) if config_file else {} + styles = config['Styles'] + for style in styles: + cm = style['ColorMappings'] + for key in cm.keys(): + if 'ColorFile' in cm[key]: + cf = cm[key]['ColorFile'] + if cf not in SNAP_CPD_LIST: + SNAP_CPD_LIST.append(cf) + return SNAP_CPD_LIST \ No newline at end of file