barakuda_colmap.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. # My own colormaps...
  2. # Check http://matplotlib.org/examples/color/colormaps_reference.html !!!
  3. # Colors: http://www.pitt.edu/~nisg/cis/web/cgi/rgb.html
  4. # Last Updated: L. Brodeau, January 2013
  5. import sys
  6. import numpy as nmp
  7. # List of Barakuda home-made colormaps:
  8. list_barakuda = [ 'blk', 'land', 'land_dark', 'terre', 'cb1', 'eke', 'bathy', 'mld', 'tap1', 'tap2', 'jetblanc', 'amoc',
  9. 'sst1', 'sst2', 'sst3', 'ice', 'ice_on', 'blanc', 'rms',
  10. 'sigtr', 'bbr', 'bbr2', 'bbr0', 'bbr_cold', 'bbr_warm',
  11. 'cold0', 'warm0', 'graylb', 'graylb2', 'sigma', 'sigma0', 'mask', 'on0', 'on1', 'on2', 'on3' ]
  12. # There is also NCVIEW colormaps, and the defauly Matplotlib colormaps...
  13. l_debug = False
  14. def chose_colmap( cname, log_ctrl=0, exp_ctrl=0 ):
  15. # 1st is it a ncview colormap ?
  16. if cname[:7] == 'ncview_':
  17. M = ncview_cmap_to_array( cname )
  18. ColorMap = __build_colormap__(M, log_ctrl=log_ctrl, exp_ctrl=exp_ctrl)
  19. # Maybe a barakuda colormap ?
  20. elif cname in list_barakuda or ( cname[-2:] == '_r' and cname[:-2] in list_barakuda):
  21. if l_debug: print '\n *** Getting Barakuda colormap "'+cname+'" !'
  22. x = brkd_cmap(cname)
  23. ColorMap = x.clrmp(log_ctrl=log_ctrl, exp_ctrl=exp_ctrl)
  24. else:
  25. # Then it must be a Matplotlib colormap:
  26. if log_ctrl or exp_ctrl > 0: print 'WARNING: cannot use LOG or EXP colormap with Matplotlib colormaps...'
  27. from matplotlib.pylab import cm
  28. import matplotlib.pyplot as mp
  29. list = mp.colormaps()
  30. if cname in list:
  31. # Yes it is!
  32. if l_debug: print '\n *** Getting Matplotlib colormap "'+cname+'" !'
  33. fToCall = getattr(cm, cname)
  34. ColorMap = fToCall
  35. else:
  36. print 'ERROR: (chose_colmap of barakuda_colmap.py) do not know where to get colormap "'+cname+'" !'
  37. sys.exit(0)
  38. return ColorMap
  39. def ncview_cmap_to_array( cname ):
  40. #
  41. #########################################################################################
  42. #
  43. # Get the NCVIEW colormap in the C header file and return an array ready for a colormap
  44. # Author: L. Brodeau, 2017
  45. #
  46. # cname: "ncview_" + name of the NCVIEW colormap as in the NCVIEW header colormap files [string]
  47. # example : 'ncview_rainbow'
  48. #
  49. #
  50. # The environment variable 'DIR_NCVIEW_CMAP' must be set!
  51. # => path to the directory containing the NCVIEW header colormap files
  52. # => ex: colormap 'rainbow' is defined in header file 'colormaps_rainbow.h''
  53. #
  54. ##########################################################################################
  55. import os
  56. import re
  57. dir_ncview_cmap = os.getenv('DIR_NCVIEW_CMAP')
  58. if dir_ncview_cmap is None:
  59. print(" ERROR => the {} environement variable is not set".format('DIR_NCVIEW_CMAP'))
  60. sys.exit(0)
  61. if cname[:7] != 'ncview_' : print ' ERROR: a ncview colormap should begin with "ncview_" !'; sys.exit(0)
  62. ncview_name = cname[7:]
  63. cf_ncview_cmap = dir_ncview_cmap+'/colormaps_'+ncview_name+'.h'
  64. if not os.path.exists(cf_ncview_cmap):
  65. print 'ERROR: NCVIEW colormap '+cf_ncview_cmap+' not found!' ; sys.exit(0)
  66. if l_debug: print '\n *** Getting NCVIEW colormap "'+ncview_name+'" from file "'+cf_ncview_cmap+'"'
  67. f = open(cf_ncview_cmap, 'r')
  68. cread_lines = f.readlines()
  69. f.close()
  70. lstarted = False
  71. vec = []
  72. for ll in cread_lines:
  73. ll = re.sub(r'\s', '', ll)
  74. ll = re.sub(r'{', ',', ll)
  75. ll = re.sub(r'}', ',', ll)
  76. ls = re.split(',',ll)
  77. if lstarted:
  78. for ve in ls[:-1]: vec.append(float(ve)) ; # [:-1] is to ommit the ',' at the end
  79. if ls[0] == 'staticintcmap_'+ncview_name+'[]=':
  80. lstarted = True
  81. for ve in ls[1:-1]: vec.append(float(ve)) ; # [:-1] is to ommit the ',' at the end
  82. ctmp = []
  83. ii = 0
  84. while ii < len(vec):
  85. ctmp.append([vec[ii], vec[ii+1], vec[ii+2]])
  86. ii += 3
  87. MM = nmp.array(ctmp)/255.
  88. return MM
  89. # ===== local ======
  90. def __build_colormap__(MC, log_ctrl=0, exp_ctrl=0):
  91. import matplotlib.colors as mplc
  92. [ nc, n3 ] = nmp.shape(MC)
  93. # Make x vector :
  94. x =[]
  95. for i in range(nc): x.append(255.*float(i)/((nc-1)*255.0))
  96. x = nmp.array(x)
  97. if log_ctrl > 0: x = nmp.log(x + log_ctrl)
  98. if exp_ctrl > 0: x = nmp.exp(x * exp_ctrl)
  99. rr = x[nc-1] ; x = x/rr
  100. y =nmp.zeros(nc)
  101. for i in range(nc): y[i] = x[nc-1-i]
  102. x = 1 - y ; rr = x[nc-1] ; x = x/rr
  103. vred = [] ; vblue = [] ; vgreen = []
  104. for i in range(nc):
  105. vred.append ([x[i],MC[i,0],MC[i,0]])
  106. vgreen.append([x[i],MC[i,1],MC[i,1]])
  107. vblue.append ([x[i],MC[i,2],MC[i,2]])
  108. cdict = {'red':vred, 'green':vgreen, 'blue':vblue}
  109. my_cm = mplc.LinearSegmentedColormap('my_colormap',cdict,256)
  110. return my_cm
  111. #=======================================================================
  112. class brkd_cmap:
  113. def __init__(self, name):
  114. self.name = name
  115. def clrmp(self, log_ctrl=0, exp_ctrl=0):
  116. cname = self.name
  117. lrev = False
  118. if cname[-2:] == '_r':
  119. lrev = True
  120. cname = cname[:-2]
  121. if cname == 'blk':
  122. M = nmp.array( [
  123. [ 0. , 0., 0. ], # black
  124. [ 0. , 0., 0. ] # black
  125. ] )
  126. elif cname == 'land':
  127. M = nmp.array( [
  128. [ 0.75 , 0.75, 0.75 ],
  129. [ 0.75 , 0.75, 0.75 ]
  130. ] )
  131. elif cname == 'land_dark':
  132. M = nmp.array( [
  133. [ 0.35 , 0.35, 0.35 ],
  134. [ 0.35 , 0.35, 0.35 ]
  135. ] )
  136. elif cname == 'terre':
  137. M = nmp.array( [
  138. [ 156./255.,85./255.,54./255. ],
  139. [ 156./255.,85./255.,54./255. ],
  140. ] )
  141. elif cname == 'on0':
  142. M = nmp.array( [
  143. [ 1.,1.,1. ], # blanc
  144. [ 0.,138./255.,184./255. ], # bleu
  145. [ 0.,0.,0. ], # noir
  146. ] )
  147. elif cname == 'on1':
  148. M = nmp.array( [
  149. [ 1.,1.,1. ], # blanc
  150. [ 0.,138./255.,184./255. ], # bleu
  151. ] )
  152. elif cname == 'on2':
  153. M = nmp.array( [
  154. [ 0.,0.,0. ], # noir
  155. [ 0.,138./255.,184./255. ], # bleu
  156. [ 1.,1.,1. ], # blanc
  157. ] )
  158. elif cname == 'on3':
  159. M = nmp.array( [
  160. [ 0.,0.,0. ], # noir
  161. [ 0.,138./255.,184./255. ], # bleu
  162. [ 1.,1.,1. ], # blanc
  163. [ 1.,237./255.,0 ], # jaune
  164. ] )
  165. elif cname == 'cb1':
  166. M = nmp.array( [
  167. [ 255,255,204 ],
  168. [ 161,218,180 ],
  169. [ 65,182,196 ],
  170. [ 44,127,184 ],
  171. [ 37,52,148 ]
  172. ] ) / 255.
  173. elif cname == 'eke':
  174. M = nmp.array( [
  175. [ 0. , 0.0 , 0.2 ], # black
  176. [ 0.1 , 0.5 , 1.0 ], # blue
  177. [ 0.2 , 1.0 , 0.0 ], # green
  178. [ 1. , 1.0 , 0.0 ], # yellow
  179. [ 1. , 0.0 , 0.0 ], # red
  180. [0.2 , 0.27, 0.07 ] # brown
  181. ] )
  182. elif cname == 'bathy':
  183. M = nmp.array( [
  184. [ 0.0 , 0.0 , 0.4 ], # dark blue
  185. [ 0.1 , 0.5 , 1.0 ], # blue
  186. [ 0.2 , 1.0 , 0.0 ], # green
  187. [ 1. , 1.0 , 0.0 ], # yellow
  188. [ 1. , 0.0 , 0.0 ], # red
  189. [0.2 , 0.27, 0.07 ] # brown
  190. ] )
  191. elif cname == 'mld':
  192. M = nmp.array( [
  193. [ 1.0 , 1.0 , 1.0 ], # white
  194. [ 0.1 , 0.5 , 1.0 ], # light blue
  195. [ 0.13, 0.54, 0.13], # dark green
  196. [ 0.2 , 1.0 , 0.0 ], # light green
  197. [ 1.0 , 1.0 , 0.0 ], # yellow
  198. [ 1.0 , 0.0 , 0.0 ], # red
  199. [ 60./255.,0.,0. ] # uber dark redish brown
  200. ] )
  201. elif cname == 'tap1':
  202. M = nmp.array( [
  203. [232./255.,254./255.,255./255.], # light blue 1
  204. [ 0.1 , 0.5 , 1.0 ], # blue 1
  205. [ 1.0 , 1.0 , 0.0 ], # yellow
  206. [ 1.0 , 0.0 , 0.0 ], # red
  207. [ 60./255.,0.,0. ] # uber dark redish brown
  208. ] )
  209. elif cname == 'tap2':
  210. M = nmp.array( [
  211. [232./255.,254./255.,255./255.], # light blue 1
  212. #[23./255.,170./255.,1.], #light blue 2
  213. #[132./255.,207./255.,197./255.], # light blue 3
  214. ##[166./255.,204./255.,255./255.], # light blue 4
  215. #[ 0.1 , 0.5 , 1.0 ], # blue 1
  216. [32./255.,55./255.,145./255.], # blue 2
  217. #[255./255.,166./255.,198./255.], # pink
  218. [ 1.0 , 1.0 , 0.0 ], # yellow
  219. [ 1.0 , 0.0 , 0.0 ], # red
  220. [ 112./255.,4./255.,4./255. ] # dark redish brown
  221. ] )
  222. elif cname == 'jetblanc':
  223. M = nmp.array( [
  224. [ 0.6 , 0.0 , 0.8 ], # violet
  225. [ 0.0 , 0.0 , 0.4 ], # dark blue
  226. [ 0.1 , 0.5 , 1.0 ], # light blue
  227. [ 1.0 , 1.0 , 1.0 ], # white
  228. [ 1.0 , 1.0 , 0.0 ], # yellow
  229. [ 1.0 , 0.0 , 0.0 ], # red
  230. [ 0.2 , 0.3 , 0.1 ] # dark redish brown
  231. ] )
  232. elif cname == 'amoc':
  233. M = nmp.array( [
  234. [ 0.4 , 0.0 , 0.6 ], # violet
  235. [ 1.0 , 1.0 , 1.0 ], # white
  236. [ 1.0 , 1.0 , 1.0 ], # white
  237. [0.68 , 0.98, 0.98], # light blue
  238. [ 0.0 , 0.0 , 0.95], # dark blue
  239. [ 0.2 , 1.0 , 0.0 ], # green
  240. [ 1.0 , 1.0 , 0.0 ], # yellow
  241. [ 1.0 , 0.0 , 0.0 ], # red
  242. [ 0.2 , 0.3 , 0.1 ] # dark read
  243. ] )
  244. elif cname == 'sst1':
  245. M = nmp.array( [
  246. [ 1.0 , 1.0 , 1.0 ], # white
  247. [ 0.4 , 0.0 , 0.6 ], # violet
  248. [ 0. , 0.2 , 0.99], # dark blue
  249. [0.68 , 0.98, 0.98], # light blue
  250. [ 0.13, 0.54, 0.13], # dark green
  251. [ 1.0 , 1.0 , 0.0 ], # yellow
  252. [ 1.0 , 0.0 , 0.0 ], # red
  253. [ 0.2 , 0.3 , 0.1 ] # dark read
  254. ] )
  255. elif cname == 'sst2':
  256. M = nmp.array( [
  257. [ 1.0 , 1.0 , 1.0 ], # white
  258. [ 0.4 , 0.0 , 0.6 ], # violet
  259. [ 0.0 , 0.0 , 0.95], # dark blue
  260. [0.68 , 0.98, 0.98], # light blue
  261. [46./255., 203./255., 35./255.], # green
  262. [ 1.0 , 1.0 , 0.0 ], # yellow
  263. [ 1.0 , 0.0 , 0.0 ], # red
  264. [ 0.2 , 0.3 , 0.1 ] # dark read
  265. ] )
  266. elif cname == 'sst3':
  267. M = nmp.array( [
  268. [ 0.4 , 0.0 , 0.6 ], # violet
  269. [ 0. , 0.2 , 0.99], # dark blue
  270. [0.68 , 0.98, 0.98], # light blue
  271. [ 0.13, 0.54, 0.13], # dark green
  272. [ 1.0 , 1.0 , 0.0 ], # yellow
  273. [ 1.0 , 0.0 , 0.0 ], # red
  274. [ 0.2 , 0.3 , 0.1 ] # dark read
  275. ] )
  276. elif cname == 'ice':
  277. M = nmp.array( [
  278. [ 0. , 0. , 0.3 ], # dark blue
  279. [ 0.6 , 0.6 , 0.8 ], # light grey
  280. [ 0.95 , 0.95 , 0.95 ], # white
  281. [ 1.0 , 1.0 , 1.0 ] # white
  282. ] )
  283. elif cname == 'ice_on':
  284. M = nmp.array( [
  285. [ 0.,0.,0. ], # noir (has to match with coldest color of "on3" !
  286. [ 25./255. , 102./255. , 114./255. ],
  287. [ 1.0 , 1.0 , 1.0 ] # white
  288. ] )
  289. # [ 0.6 , 0.6 , 0.8 ], # light grey
  290. elif cname == 'blanc':
  291. M = nmp.array( [
  292. [ 1.0 , 1.0 , 1.0 ], # white
  293. [ 1.0 , 1.0 , 1.0 ] # white
  294. ] )
  295. elif cname == 'rms':
  296. M = nmp.array( [
  297. [ 1.0 , 1.0 , 1.0 ],
  298. [ 0.1 , 0.5 , 1.0 ],
  299. [ 0.2 , 1.0 , 0.0 ],
  300. [ 1.0 , 1.0 , 0.0 ],
  301. [ 1.0 , 0.0 , 0.0 ],
  302. [ 0.2 , 0.3 , 0.1 ]
  303. ] )
  304. elif cname == 'sigtr':
  305. M = nmp.array( [
  306. [ 1.0 , 1.0 , 1.0 ], # white
  307. [ 0.0 , 0.8 , 1.0 ], #light blue
  308. [ 0.1 , 0.5 , 1.0 ], #light blue
  309. [ 0.0 , 0.0 , 0.4 ], # blue
  310. [ 0.0 , 0.4 , 0.0 ], # dark green
  311. [ 0.1 , 1.0 , 0.0 ], # green
  312. [ 0.4 , 1.0 , 0.0 ], # vert pomme
  313. [ 1.0 , 1.0 , 0.0 ], # yellow
  314. [ 1.0 , 0.4 , 0.0 ], # orange
  315. [ 1.0 , 0.0 , 0.0 ], # red
  316. [ 0.6 , 0.0 , 0.0 ], # red
  317. [ 0.2 , 0.3 , 0.1 ] # dark red
  318. ] )
  319. elif cname == 'bbr':
  320. M = nmp.array( [
  321. [ 0. , 0. , 0.2 ],
  322. [ 0. , 0. , 1. ],
  323. [ 1. , 1. , 1. ],
  324. [ 1. , 1. , 1. ],
  325. [ 1. , 0. , 0. ],
  326. [ 0.6 , 0. , 0. ]
  327. ] )
  328. elif cname == 'bbr2':
  329. M = nmp.array( [
  330. [ 0. , 1. , 1. ], # cyan
  331. [ 0. , 0. , 1. ],
  332. [ 1. , 1. , 1. ],
  333. [ 1. , 1. , 1. ],
  334. [ 1. , 0. , 0. ],
  335. [ 1. , 1. , 0. ] # jaune
  336. ] )
  337. elif cname == 'bbr0':
  338. M = nmp.array( [
  339. [ 0. , 1. , 1. ], # cyan
  340. [ 0. , 0. , 1. ],
  341. [ 1. , 1. , 1. ],
  342. [ 1. , 0. , 0. ],
  343. [ 1. , 1. , 0. ] # jaune
  344. ] )
  345. elif cname == 'bbr_cold':
  346. M = nmp.array( [
  347. [ 0. , 1. , 1. ], # cyan
  348. [ 0. , 0. , 1. ],
  349. [ 19./255. , 7./255. , 129./255 ], # dark blue
  350. [ .1 , .1 , .9 ],
  351. [ 1. , 1. , 1. ],
  352. [ 1. , 1. , 1. ],
  353. [ 0.7 , 0. , 0. ] # Dark red
  354. ] )
  355. elif cname == 'bbr_warm':
  356. M = nmp.array( [
  357. [ 19./255. , 7./255. , 129./255 ], # dark blue
  358. [ 1. , 1. , 1. ],
  359. [ 1. , 1. , 1. ],
  360. [ 0.9 , 0.1 , 0.1 ],
  361. [ 0.7 , 0. , 0. ], # Dark red
  362. [ 1. , 1. , 0. ], # jaune
  363. ] )
  364. elif cname == 'cold0':
  365. M = nmp.array( [
  366. [ 177./255. , 250./255. , 122./255. ], # greenish
  367. [ 0. , 1. , 1. ], # cyan
  368. [ 7./255. , 11./255. , 122./255. ], # dark blue
  369. [ 0. , 0. , 1. ], # true blue
  370. [ 177./255. , 189./255. , 250./255. ], # light blue
  371. [ 1. , 1. , 1. ],
  372. ] )
  373. elif cname == 'warm0':
  374. M = nmp.array( [
  375. [ 1. , 1. , 1. ],
  376. [ 255./255. , 254./255. , 198./255. ], # very light yellow
  377. [ 1. , 1. , 0. ], # yellow
  378. [ 244./255. , 78./255. , 255./255. ], # pink
  379. [ 1. , 0. , 0. ], # true red
  380. [ 139./255. , 5./255. , 5./255. ] # dark red
  381. ] )
  382. elif cname == 'graylb':
  383. M = nmp.array( [
  384. [ 1. , 1. , 1. ],
  385. [ 0.1 , 0.1 , 0.1 ]
  386. ] )
  387. elif cname == 'graylb2':
  388. M = nmp.array( [
  389. [ 0.6 , 0.6 , 0.6 ],
  390. [ 1. , 1. , 1. ]
  391. ] )
  392. elif cname == 'sigma':
  393. M = nmp.array( [
  394. [ 1.0 , 1.0 , 1.0 ], # white
  395. [ 1.0 , 0.0 , 0.0 ], # red
  396. [ 1.0 , 1.0 , 0.0 ], # yellow
  397. [ 0.2 , 1.0 , 0.0 ], # green
  398. [ 0.1 , 0.5 , 1.0 ], # light blue
  399. [ 0.0 , 0.0 , 0.4 ], # dark blue
  400. [ 0.6 , 0.0 , 0.8 ] # violet
  401. ] )
  402. elif cname == 'sigma0':
  403. M = nmp.array( [
  404. [ 0.2 , 0.3 , 0.1 ], # dark redish brown
  405. [ 1.0 , 0.0 , 0.0 ], # red
  406. [ 1.0 , 1.0 , 0.0 ], # yellow
  407. [ 0.2 , 1.0 , 0.0 ], # green
  408. [ 0.1 , 0.5 , 1.0 ], # light blue
  409. [ 0.0 , 0.0 , 0.4 ], # dark blue
  410. [ 0.6 , 0.0 , 0.8 ], # violet
  411. [ 1.0 , 1.0 , 1.0 ] # white
  412. ] )
  413. elif cname == 'mask':
  414. M = nmp.array( [
  415. [ 0.5 , 0.5 , 0.5 ], # gray
  416. [ 0.5 , 0.5 , 0.5 ] # gray
  417. ] )
  418. else:
  419. print 'ERROR: (''barakuda_colmap.py) => unknown "barakuda" colormap: '+cname
  420. sys.exit(0)
  421. if lrev:
  422. # reverse colormap:
  423. my_cmap = __build_colormap__(M[::-1,:], log_ctrl=log_ctrl, exp_ctrl=exp_ctrl)
  424. else:
  425. my_cmap = __build_colormap__(M, log_ctrl=log_ctrl, exp_ctrl=exp_ctrl)
  426. return my_cmap
  427. #=======================================================================