-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgmtColormap.py
94 lines (81 loc) · 2.45 KB
/
gmtColormap.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# -*- coding: utf-8 -*-
# http://www.scipy.org/Cookbook/Matplotlib/Loading_a_colormap_dynamically
#from __future__ import division
import colorsys
import numpy as np
import matplotlib as mpl
def gmtColormap(fileName, GMTPath=None, frm=None, to=None):
if not GMTPath:
filePath = "/usr/local/cmaps/" + fileName + ".cpt"
else:
filePath = GMTPath + "/" + fileName + ".cpt"
try:
f = open(filePath)
except:
print("file ", filePath, "not found")
return None
lines = f.readlines()
f.close()
colorModel = "RGB"
x, r, g, b = [], [], [], []
for l in lines:
ls = l.split()
if l[0] == "#":
if ls[-1] == "HSV":
colorModel = "HSV"
continue
else:
continue
if ls[0] == "B" or ls[0] == "F" or ls[0] == "N":
pass
else:
x.append(float(ls[0]))
r.append(float(ls[1]))
g.append(float(ls[2]))
b.append(float(ls[3]))
xtemp = float(ls[4])
rtemp = float(ls[5])
gtemp = float(ls[6])
btemp = float(ls[7])
x.append(xtemp)
r.append(rtemp)
g.append(gtemp)
b.append(btemp)
if frm is None:
frm = 0
if to is None:
to = len(x)
if frm is 'mid':
frm = len(x[::2])
if to is 'mid':
to = len(x[::2])-1
x = x[frm:to]
r = r[frm:to]
g = g[frm:to]
b = b[frm:to]
#nTable = len(r)
x = np.asanyarray(x, dtype=np.float)
r = np.asanyarray(r, dtype=np.float)
g = np.asanyarray(g, dtype=np.float)
b = np.asanyarray(b, dtype=np.float)
if colorModel == "HSV":
for i in range(r.shape[0]):
rr, gg, bb = colorsys.hsv_to_rgb(r[i] / 360., g[i], b[i])
r[i], g[i], b[i] = rr, gg, bb
if colorModel == "HSV":
for i in range(r.shape[0]):
rr, gg, bb = colorsys.hsv_to_rgb(r[i] / 360., g[i], b[i])
r[i], g[i], b[i] = rr, gg, bb
if colorModel == "RGB":
r, g, b = r / 255., g / 255., b / 255.
xNorm = (x - x[0]) / (x[-1] - x[0])
red = []
blue = []
green = []
for i in range(len(x)):
red.append([xNorm[i], r[i], r[i]])
green.append([xNorm[i], g[i], g[i]])
blue.append([xNorm[i], b[i], b[i]])
colorDict = {"red": red, "green": green, "blue": blue}
cmap = mpl.colors.LinearSegmentedColormap(fileName, colorDict, N=256, gamma=1.0)
return cmap