barakuda_colmap.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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', '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 ):
  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)
  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)
  24. else:
  25. # Then it must be a Matplotlib colormap:
  26. if log_ctrl > 0: print 'WARNING: cannot use LOG 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):
  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. rr = x[nc-1] ; x = x/rr
  99. y =nmp.zeros(nc)
  100. for i in range(nc): y[i] = x[nc-1-i]
  101. x = 1 - y ; rr = x[nc-1] ; x = x/rr
  102. vred = [] ; vblue = [] ; vgreen = []
  103. for i in range(nc):
  104. vred.append ([x[i],MC[i,0],MC[i,0]])
  105. vgreen.append([x[i],MC[i,1],MC[i,1]])
  106. vblue.append ([x[i],MC[i,2],MC[i,2]])
  107. cdict = {'red':vred, 'green':vgreen, 'blue':vblue}
  108. my_cm = mplc.LinearSegmentedColormap('my_colormap',cdict,256)
  109. return my_cm
  110. #=======================================================================
  111. class brkd_cmap:
  112. def __init__(self, name):
  113. self.name = name
  114. def clrmp(self, log_ctrl=0):
  115. cname = self.name
  116. lrev = False
  117. if cname[-2:] == '_r':
  118. lrev = True
  119. cname = cname[:-2]
  120. if cname == 'blk':
  121. M = nmp.array( [
  122. [ 0. , 0., 0. ], # black
  123. [ 0. , 0., 0. ] # black
  124. ] )
  125. elif cname == 'land':
  126. M = nmp.array( [
  127. [ 0.75 , 0.75, 0.75 ],
  128. [ 0.75 , 0.75, 0.75 ]
  129. ] )
  130. elif cname == 'land_dark':
  131. M = nmp.array( [
  132. [ 0.35 , 0.35, 0.35 ],
  133. [ 0.35 , 0.35, 0.35 ]
  134. ] )
  135. elif cname == 'terre':
  136. M = nmp.array( [
  137. [ 156./255.,85./255.,54./255. ],
  138. [ 156./255.,85./255.,54./255. ],
  139. ] )
  140. elif cname == 'on0':
  141. M = nmp.array( [
  142. [ 1.,1.,1. ], # blanc
  143. [ 0.,138./255.,184./255. ], # bleu
  144. [ 0.,0.,0. ], # noir
  145. ] )
  146. elif cname == 'on1':
  147. M = nmp.array( [
  148. [ 1.,1.,1. ], # blanc
  149. [ 0.,138./255.,184./255. ], # bleu
  150. ] )
  151. elif cname == 'on2':
  152. M = nmp.array( [
  153. [ 0.,0.,0. ], # noir
  154. [ 0.,138./255.,184./255. ], # bleu
  155. [ 1.,1.,1. ], # blanc
  156. ] )
  157. elif cname == 'on3':
  158. M = nmp.array( [
  159. [ 0.,0.,0. ], # noir
  160. [ 0.,138./255.,184./255. ], # bleu
  161. [ 1.,1.,1. ], # blanc
  162. [ 1.,237./255.,0 ], # jaune
  163. ] )
  164. elif cname == 'cb1':
  165. M = nmp.array( [
  166. [ 255,255,204 ],
  167. [ 161,218,180 ],
  168. [ 65,182,196 ],
  169. [ 44,127,184 ],
  170. [ 37,52,148 ]
  171. ] ) / 255.
  172. elif cname == 'eke':
  173. M = nmp.array( [
  174. [ 0. , 0.0 , 0.2 ], # black
  175. [ 0.1 , 0.5 , 1.0 ], # blue
  176. [ 0.2 , 1.0 , 0.0 ], # green
  177. [ 1. , 1.0 , 0.0 ], # yellow
  178. [ 1. , 0.0 , 0.0 ], # red
  179. [0.2 , 0.27, 0.07 ] # brown
  180. ] )
  181. elif cname == 'bathy':
  182. M = nmp.array( [
  183. [ 0.0 , 0.0 , 0.4 ], # dark blue
  184. [ 0.1 , 0.5 , 1.0 ], # blue
  185. [ 0.2 , 1.0 , 0.0 ], # green
  186. [ 1. , 1.0 , 0.0 ], # yellow
  187. [ 1. , 0.0 , 0.0 ], # red
  188. [0.2 , 0.27, 0.07 ] # brown
  189. ] )
  190. elif cname == 'mld':
  191. M = nmp.array( [
  192. [ 1.0 , 1.0 , 1.0 ], # white
  193. [ 0.1 , 0.5 , 1.0 ], # light blue
  194. [ 0.13, 0.54, 0.13], # dark green
  195. [ 0.2 , 1.0 , 0.0 ], # light green
  196. [ 1.0 , 1.0 , 0.0 ], # yellow
  197. [ 1.0 , 0.0 , 0.0 ], # red
  198. [ 60./255.,0.,0. ] # uber dark redish brown
  199. ] )
  200. elif cname == 'tap1':
  201. M = nmp.array( [
  202. [232./255.,254./255.,255./255.], # light blue 1
  203. [ 0.1 , 0.5 , 1.0 ], # blue 1
  204. [ 1.0 , 1.0 , 0.0 ], # yellow
  205. [ 1.0 , 0.0 , 0.0 ], # red
  206. [ 60./255.,0.,0. ] # uber dark redish brown
  207. ] )
  208. elif cname == 'tap2':
  209. M = nmp.array( [
  210. [232./255.,254./255.,255./255.], # light blue 1
  211. #[23./255.,170./255.,1.], #light blue 2
  212. #[132./255.,207./255.,197./255.], # light blue 3
  213. ##[166./255.,204./255.,255./255.], # light blue 4
  214. #[ 0.1 , 0.5 , 1.0 ], # blue 1
  215. [32./255.,55./255.,145./255.], # blue 2
  216. #[255./255.,166./255.,198./255.], # pink
  217. [ 1.0 , 1.0 , 0.0 ], # yellow
  218. [ 1.0 , 0.0 , 0.0 ], # red
  219. [ 112./255.,4./255.,4./255. ] # dark redish brown
  220. ] )
  221. elif cname == 'jetblanc':
  222. M = nmp.array( [
  223. [ 0.6 , 0.0 , 0.8 ], # violet
  224. [ 0.0 , 0.0 , 0.4 ], # dark blue
  225. [ 0.1 , 0.5 , 1.0 ], # light blue
  226. [ 1.0 , 1.0 , 1.0 ], # white
  227. [ 1.0 , 1.0 , 0.0 ], # yellow
  228. [ 1.0 , 0.0 , 0.0 ], # red
  229. [ 0.2 , 0.3 , 0.1 ] # dark redish brown
  230. ] )
  231. elif cname == 'amoc':
  232. M = nmp.array( [
  233. [ 0.4 , 0.0 , 0.6 ], # violet
  234. [ 1.0 , 1.0 , 1.0 ], # white
  235. [ 1.0 , 1.0 , 1.0 ], # white
  236. [0.68 , 0.98, 0.98], # light blue
  237. [ 0.0 , 0.0 , 0.95], # dark blue
  238. [ 0.2 , 1.0 , 0.0 ], # green
  239. [ 1.0 , 1.0 , 0.0 ], # yellow
  240. [ 1.0 , 0.0 , 0.0 ], # red
  241. [ 0.2 , 0.3 , 0.1 ] # dark read
  242. ] )
  243. elif cname == 'sst1':
  244. M = nmp.array( [
  245. [ 1.0 , 1.0 , 1.0 ], # white
  246. [ 0.4 , 0.0 , 0.6 ], # violet
  247. [ 0. , 0.2 , 0.99], # dark blue
  248. [0.68 , 0.98, 0.98], # light blue
  249. [ 0.13, 0.54, 0.13], # dark green
  250. [ 1.0 , 1.0 , 0.0 ], # yellow
  251. [ 1.0 , 0.0 , 0.0 ], # red
  252. [ 0.2 , 0.3 , 0.1 ] # dark read
  253. ] )
  254. elif cname == 'sst2':
  255. M = nmp.array( [
  256. [ 1.0 , 1.0 , 1.0 ], # white
  257. [ 0.4 , 0.0 , 0.6 ], # violet
  258. [ 0.0 , 0.0 , 0.95], # dark blue
  259. [0.68 , 0.98, 0.98], # light blue
  260. [46./255., 203./255., 35./255.], # green
  261. [ 1.0 , 1.0 , 0.0 ], # yellow
  262. [ 1.0 , 0.0 , 0.0 ], # red
  263. [ 0.2 , 0.3 , 0.1 ] # dark read
  264. ] )
  265. elif cname == 'sst3':
  266. M = nmp.array( [
  267. [ 0.4 , 0.0 , 0.6 ], # violet
  268. [ 0. , 0.2 , 0.99], # dark blue
  269. [0.68 , 0.98, 0.98], # light blue
  270. [ 0.13, 0.54, 0.13], # dark green
  271. [ 1.0 , 1.0 , 0.0 ], # yellow
  272. [ 1.0 , 0.0 , 0.0 ], # red
  273. [ 0.2 , 0.3 , 0.1 ] # dark read
  274. ] )
  275. elif cname == 'ice':
  276. M = nmp.array( [
  277. [ 0. , 0. , 0.3 ], # dark blue
  278. [ 0.6 , 0.6 , 0.8 ], # light grey
  279. [ 0.95 , 0.95 , 0.95 ], # white
  280. [ 1.0 , 1.0 , 1.0 ] # white
  281. ] )
  282. elif cname == 'blanc':
  283. M = nmp.array( [
  284. [ 1.0 , 1.0 , 1.0 ], # white
  285. [ 1.0 , 1.0 , 1.0 ] # white
  286. ] )
  287. elif cname == 'rms':
  288. M = nmp.array( [
  289. [ 1.0 , 1.0 , 1.0 ],
  290. [ 0.1 , 0.5 , 1.0 ],
  291. [ 0.2 , 1.0 , 0.0 ],
  292. [ 1.0 , 1.0 , 0.0 ],
  293. [ 1.0 , 0.0 , 0.0 ],
  294. [ 0.2 , 0.3 , 0.1 ]
  295. ] )
  296. elif cname == 'sigtr':
  297. M = nmp.array( [
  298. [ 1.0 , 1.0 , 1.0 ], # white
  299. [ 0.0 , 0.8 , 1.0 ], #light blue
  300. [ 0.1 , 0.5 , 1.0 ], #light blue
  301. [ 0.0 , 0.0 , 0.4 ], # blue
  302. [ 0.0 , 0.4 , 0.0 ], # dark green
  303. [ 0.1 , 1.0 , 0.0 ], # green
  304. [ 0.4 , 1.0 , 0.0 ], # vert pomme
  305. [ 1.0 , 1.0 , 0.0 ], # yellow
  306. [ 1.0 , 0.4 , 0.0 ], # orange
  307. [ 1.0 , 0.0 , 0.0 ], # red
  308. [ 0.6 , 0.0 , 0.0 ], # red
  309. [ 0.2 , 0.3 , 0.1 ] # dark red
  310. ] )
  311. elif cname == 'bbr':
  312. M = nmp.array( [
  313. [ 0. , 0. , 0.2 ],
  314. [ 0. , 0. , 1. ],
  315. [ 1. , 1. , 1. ],
  316. [ 1. , 1. , 1. ],
  317. [ 1. , 0. , 0. ],
  318. [ 0.6 , 0. , 0. ]
  319. ] )
  320. elif cname == 'bbr2':
  321. M = nmp.array( [
  322. [ 0. , 1. , 1. ], # cyan
  323. [ 0. , 0. , 1. ],
  324. [ 1. , 1. , 1. ],
  325. [ 1. , 1. , 1. ],
  326. [ 1. , 0. , 0. ],
  327. [ 1. , 1. , 0. ] # jaune
  328. ] )
  329. elif cname == 'bbr0':
  330. M = nmp.array( [
  331. [ 0. , 1. , 1. ], # cyan
  332. [ 0. , 0. , 1. ],
  333. [ 1. , 1. , 1. ],
  334. [ 1. , 0. , 0. ],
  335. [ 1. , 1. , 0. ] # jaune
  336. ] )
  337. elif cname == 'bbr_cold':
  338. M = nmp.array( [
  339. [ 0. , 1. , 1. ], # cyan
  340. [ 0. , 0. , 1. ],
  341. [ 19./255. , 7./255. , 129./255 ], # dark blue
  342. [ .1 , .1 , .9 ],
  343. [ 1. , 1. , 1. ],
  344. [ 1. , 1. , 1. ],
  345. [ 0.7 , 0. , 0. ] # Dark red
  346. ] )
  347. elif cname == 'bbr_warm':
  348. M = nmp.array( [
  349. [ 19./255. , 7./255. , 129./255 ], # dark blue
  350. [ 1. , 1. , 1. ],
  351. [ 1. , 1. , 1. ],
  352. [ 0.9 , 0.1 , 0.1 ],
  353. [ 0.7 , 0. , 0. ], # Dark red
  354. [ 1. , 1. , 0. ], # jaune
  355. ] )
  356. elif cname == 'cold0':
  357. M = nmp.array( [
  358. [ 177./255. , 250./255. , 122./255. ], # greenish
  359. [ 0. , 1. , 1. ], # cyan
  360. [ 7./255. , 11./255. , 122./255. ], # dark blue
  361. [ 0. , 0. , 1. ], # true blue
  362. [ 177./255. , 189./255. , 250./255. ], # light blue
  363. [ 1. , 1. , 1. ],
  364. ] )
  365. elif cname == 'warm0':
  366. M = nmp.array( [
  367. [ 1. , 1. , 1. ],
  368. [ 255./255. , 254./255. , 198./255. ], # very light yellow
  369. [ 1. , 1. , 0. ], # yellow
  370. [ 244./255. , 78./255. , 255./255. ], # pink
  371. [ 1. , 0. , 0. ], # true red
  372. [ 139./255. , 5./255. , 5./255. ] # dark red
  373. ] )
  374. elif cname == 'graylb':
  375. M = nmp.array( [
  376. [ 1. , 1. , 1. ],
  377. [ 0.1 , 0.1 , 0.1 ]
  378. ] )
  379. elif cname == 'graylb2':
  380. M = nmp.array( [
  381. [ 0.6 , 0.6 , 0.6 ],
  382. [ 1. , 1. , 1. ]
  383. ] )
  384. elif cname == 'sigma':
  385. M = nmp.array( [
  386. [ 1.0 , 1.0 , 1.0 ], # white
  387. [ 1.0 , 0.0 , 0.0 ], # red
  388. [ 1.0 , 1.0 , 0.0 ], # yellow
  389. [ 0.2 , 1.0 , 0.0 ], # green
  390. [ 0.1 , 0.5 , 1.0 ], # light blue
  391. [ 0.0 , 0.0 , 0.4 ], # dark blue
  392. [ 0.6 , 0.0 , 0.8 ] # violet
  393. ] )
  394. elif cname == 'sigma0':
  395. M = nmp.array( [
  396. [ 0.2 , 0.3 , 0.1 ], # dark redish brown
  397. [ 1.0 , 0.0 , 0.0 ], # red
  398. [ 1.0 , 1.0 , 0.0 ], # yellow
  399. [ 0.2 , 1.0 , 0.0 ], # green
  400. [ 0.1 , 0.5 , 1.0 ], # light blue
  401. [ 0.0 , 0.0 , 0.4 ], # dark blue
  402. [ 0.6 , 0.0 , 0.8 ], # violet
  403. [ 1.0 , 1.0 , 1.0 ] # white
  404. ] )
  405. elif cname == 'mask':
  406. M = nmp.array( [
  407. [ 0.5 , 0.5 , 0.5 ], # gray
  408. [ 0.5 , 0.5 , 0.5 ] # gray
  409. ] )
  410. else:
  411. print 'ERROR: (''barakuda_colmap.py) => unknown "barakuda" colormap: '+cname
  412. sys.exit(0)
  413. if lrev:
  414. # reverse colormap:
  415. my_cmap = __build_colormap__(M[::-1,:], log_ctrl=log_ctrl)
  416. else:
  417. my_cmap = __build_colormap__(M, log_ctrl=log_ctrl)
  418. return my_cmap
  419. #=======================================================================