-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathncl_colormap.py
62 lines (48 loc) · 1.52 KB
/
ncl_colormap.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import numpy as np
from matplotlib.colors import ListedColormap, BoundaryNorm
import os
def is_float(element) -> bool:
try:
float(element)
return True
except ValueError:
return False
def cmap(name):
if name.endswith('_r'):
name = name[:-2]
flag_reverse = True
else:
flag_reverse = False
cwd = os.path.dirname(os.path.abspath(__file__))
f = open(cwd+'/cmap_data/{}.rgb'.format(name), 'r')
lines = f.readlines()
lines = list(map(lambda s: s.strip('\n'), lines))
li_rgb = []
for i in range(len(lines)):
line = lines[i]
colors = [float(s) for s in line.split() if is_float(s)]
#list only numeric
if len(colors) == 3: #append to list if fully rgb
li_rgb.append(colors)
if flag_reverse:
li_rgb = list(reversed(li_rgb))
data = np.array(li_rgb)
data = data / np.max(data)
cmap = ListedColormap(data, name=name)
return cmap
def boundaries(name):
cwd = os.path.dirname(os.path.abspath(__file__))
f = open(cwd+'/cmap_data/{}.bound'.format(name), 'r')
lines = f.readlines()
lines = list(map(lambda s: s.strip('\n'), lines))
boundaries = []
for i in range(len(lines)):
boundaries.append(float(lines[i]))
return boundaries
def norm(name):
_boundaries = boundaries(name)
_cmap = cmap(name)
norm = BoundaryNorm(_boundaries, _cmap.N, clip=True)
return norm
def cmap_norm_boundaries(name):
return cmap(name), norm(name), boundaries(name)