forked from hhuangwx/cmaps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmaps.template
57 lines (47 loc) · 1.96 KB
/
cmaps.template
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import re
from glob import glob
import numpy as np
from ._version import __version__
from .colormap import Colormap
CMAPSFILE_DIR = os.path.join(
os.path.dirname(os.path.abspath(__file__)), 'colormaps')
try:
USER_CMAPFILE_DIR = os.environ['CMAP_DIR']
except KeyError:
USER_CMAPFILE_DIR = None
class Cmaps(object):
"""colormaps"""
def __init__(self, ):
self._parse_cmaps()
self.__version__ = __version__
def _coltbl(self, cmap_file):
pattern = re.compile(r'(\d\.?\d*)\s+(\d\.?\d*)\s+(\d\.?\d*).*')
with open(cmap_file) as cmap:
cmap_buff = cmap.read()
cmap_buff = re.compile('ncolors.*\n').sub('', cmap_buff)
if re.search(r'\s*\d\.\d*', cmap_buff):
return np.asarray(pattern.findall(cmap_buff), 'f4')
else:
return np.asarray(pattern.findall(cmap_buff), 'u1') / 255.
def _parse_cmaps(self):
if CMAPSFILE_DIR is not None and os.path.exists(CMAPSFILE_DIR):
cmapsflist = sorted(glob(os.path.join(CMAPSFILE_DIR,"ncar_ncl","*.rgb")))
if USER_CMAPFILE_DIR is not None and os.path.exists(USER_CMAPFILE_DIR):
cmapsflist += sorted(glob(os.path.join(USER_CMAPFILE_DIR, '*.rgb')))
for cmap_file in cmapsflist:
cname = os.path.basename(cmap_file).split('.rgb')[0]
# start with the number will result illegal attribute
if cname[0].isdigit() or cname.startswith('_'):
cname = 'C' + cname
if '-' in cname:
cname = cname.replace('-', '_')
if '+' in cname:
cname = cname.replace('+', '_')
cmap = Colormap._get_cmap(self,cname,cmap_file)
setattr(self, cname, cmap)
cname = cname + '_r'
cmap = Colormap._get_cmap(self,cname,cmap_file,reverse=True)
setattr(self, cname, cmap)