Skip to content

Commit

Permalink
including custom SNAP colormaps to tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AliceBalfanz committed Jul 9, 2019
1 parent bc8eb10 commit 748b8ad
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 26 deletions.
3 changes: 2 additions & 1 deletion test/webapi/im/test_cmaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ 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')
self.assertEqual(cmaps[3][0], 'Diverging')
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()
Expand Down
45 changes: 29 additions & 16 deletions xcube/webapi/im/cmaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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

Expand All @@ -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

Expand Down
24 changes: 15 additions & 9 deletions xcube/webapi/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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

0 comments on commit 748b8ad

Please sign in to comment.