コード例 #1
0
    def Linecolor(self, num, cmap='jet'):
        '''
		Return is from Blue to Red

		num:
			(1) color name:
					in ['red', 'orange', 'yellow', 'green', 'cyan', 'blue', 'purple', 'pink', 'brown', 'black', 'white']
					return () of this color
			(2) int, number of colors:
					return () of colors
			(3) == 'png' : 
					open 'jizhipy_Plt_LineColor.png'
	
		cmap:
			Use which cmap to generate the linecolor
			(1) None=>'gist_rainbow_r' | Name of cmap (see plt_color.cmap(None))
			(2) <matplotlib.colors.LinearSegmentedColormap> (cmap instance)
			(3) cmap='my': use my cmap
		'''
        from jizhipy.Basic import Raise, IsType, ShellCmd, Path
        import numpy as np
        #----------------------------------------
        if (IsType.isstr(num)):
            num = num.lower()
            if ('png' in num):
                uname = ShellCmd('uname')[0].lower()
                figpath = Path.jizhipyPath(
                    'jizhipy_tool/jizhipy_Plt_Color_Linecolor.png')
                operate = 'eog ' if (uname == 'linux') else 'open '
                ShellCmd(operate + figpath)
                return
            #----------------------------------------
            if (num in self.mycolor.keys()): return self.mycolor[num]
            else: Raise(Exception, num + ' NOT in ' + str(self.mycolor.keys()))
        #----------------------------------------
        if (IsType.isstr(cmap) and cmap == 'my'):
            _jet_data_my = np.array([(255, 0, 0), (255, 128, 0), (200, 200, 0),
                                     (0, 200, 0), (0, 250, 250), (0, 0, 255),
                                     (160, 0, 255)]) / 255.
            r, g, b = _jet_data_my.T
            from scipy.interpolate import interp1d
            r, g, b = np.array([r, g, b]) * ratio
            x = np.linspace(0, 1, r.size)
            fr = interp1d(x, r)
            fg = interp1d(x, g)
            fb = interp1d(x, b)
            x = np.linspace(0, 1, num)
            r, g, b = fr(x), fg(x), fb(x)
            color = []
            for i in range(num):
                color += [(r[i], g[i], b[i])]
            return tuple(color)
        #----------------------------------------
        else:
            cmap = self.Cmap(cmap)
            color = [cmap(x)[:3] for x in np.linspace(0., 1, num)]
            for i in range(len(color)):
                color[i] = (color[i][0], color[i][1], color[i][2])
            return tuple(color)
コード例 #2
0
def PYTHONPATH(use=False, notuse=False):
    '''
	$PYTHONHOME
	$PYTHONPATH

	(1) use=False
			NOT append to sys.path
		notuse=False
			NOT remove from sys.path	

	(2) use=None
			append $PYTHONPATH to sys.path	
	(3) use=str | [str, str, ...]
			append it/them to sys.path	

	(4) notuse=None
			like =False, NOT remove from sys.path	
	(5) notuse=str | [str, str, ...]
			remove it/them from sys.path

	return:
		Updated sys.path
	'''
    import sys, os
    from jizhipy.Basic import IsType, ShellCmd
    #----------------------------------------
    if (use is True or use is False): use = []
    elif (use is None):
        use = ShellCmd('echo $PYTHONPATH')[0].split(':')
    elif (IsType.isstr(use)):
        use = [use]
    elif (IsType.istuple(use)):
        use = list(use)
    #----------------------------------------
    if (notuse is True or notuse is False or notuse is None): notuse = []
    elif (IsType.isstr(notuse)): notuse = [notuse]
    elif (IsType.istuple(notuse)): notuse = list(notuse)
    #----------------------------------------
    n = 0
    while (n < len(use)):
        if (use[n] == ''): use.pop(n)
        else:
            if (use[n][-1] == '/'):
                use[n] = use[n][:-1]
                n += 1
    if (len(use) > 0): use = [''] + use
    #----------------------------------------
    here = os.path.abspath('')
    notusehere = True if ('' in notuse) else False
    for i in range(len(notuse)):
        notuse[i] = os.path.abspath(os.path.expanduser(notuse[i]))
    if (notusehere): notuse.append('')
    #----------------------------------------
    sys.path = use + sys.path
    for i in range(len(notuse)):
        while (notuse[i] in sys.path):
            sys.path.remove(notuse[i])
    return sys.path[:]
コード例 #3
0
ファイル: PsGrep.py プロジェクト: jizhi/jizhipy
def PsGrep(*args):
    '''
	PsGrep('python wait.py') => ps | grep "python wait.py"

	Parameters
	----------
	*args:
		Can match many keys

	Returns
	----------
	return pid, pid is a list of str
	'''
    from jizhipy.Basic import ShellCmd
    value, _value, pid = [], [], []
    for key in args:
        _value += ShellCmd('ps | grep "' + key + '"')
    for v in _value:
        get = True
        for key in args:
            if (key not in v): get = False
        if (get and 'grep' not in v and v not in value):
            value.append(v)
    for v in value:
        w = v.split(' ')
        for p in w:
            try:
                pid.append(str(int(p)))
                break
            except:
                pass
    return pid
コード例 #4
0
    def FindDir(self, dirpath, exclude=[], name=''):
        '''
		Shell command: find dirpath -name name, plus+ exclude
		'''
        import os
        from jizhipy.Basic import IsType, ShellCmd
        if (not IsType.isstr(name)): name = ''
        name = EscPath(name, 'add')
        #--------------------------------------------------
        dirpath = EscPath(dirpath, 'add', True)
        if (name): walk = ShellCmd('find ' + dirpath + ' -name ' + name)
        else: walk = ShellCmd('find ' + dirpath)
        #--------------------------------------------------
        exctmp = []
        if (IsType.isstr(exclude)): exclude = [exclude]
        for i in range(len(exclude)):
            exclude[i] = EscPath(exclude[i], 'add')
            if ('*' not in exclude[i]):
                dirname = os.path.abspath(os.path.expanduser(exclude[i]))
                exctmp += ShellCmd('find ' + dirname)
            else:
                dirname, basename = os.path.split(exclude[i])
                dirname = os.path.abspath(os.path.expanduser(dirname))
                exctmp += ShellCmd('find ' + dirname + ' -name ' + basename)
        exclude = exctmp
        #--------------------------------------------------
        for i in range(len(exclude)):
            try:
                walk.remove(exclude[i])
            except:
                pass
        walk.remove(dirpath)
        dirs, files = [], []
        for i in range(len(walk)):
            if (os.path.isdir(walk[i])):
                dirs.append(walk[i][len(dirpath) + 1:])
            else:
                files.append(walk[i][len(dirpath) + 1:])
        #--------------------------------------------------
        return [dirpath, dirs, files]
コード例 #5
0
def SysMemory() : 
	'''
	Return the total physical memory (MB) of the computer
	1e8 float64 accounts for 800MB:
		array.dtype = np.float64
		array.size = 1e8
		array accounts for 800MB memory
	'''
	from jizhipy.Basic import ShellCmd
	uname = ShellCmd('uname')[0]
	if (uname == 'Linux') : 
		memory = ShellCmd('free -m')[1]
	elif (uname == 'Darwin') : 
		memory =ShellCmd('top -l 1 | head -n 10 | grep PhysMem')[0]
	else : Raise(Exception, uname+' not in (Linux, Darwin)')
	#--------------------------------------------------
	for i in range(len(memory)) : 
		if (memory[i] in '123456789') : 
			n1 = i
			break
	for i in range(n1+1, len(memory)) : 
		if (memory[i] not in '0123456789') : 
			n2 = i
			break
	totmem = int(memory[n1:n2]) # MB
	#--------------------------------------------------
	if (uname == 'Darwin') : 
		for i in range(len(memory)-1, 0, -1) : 
			if (memory[i] in '0123456789') : 
				n4 = i+1
				break
		for i in range(n4-1, 0, -1) : 
			if (memory[i] not in '0123456789') : 
				n3 = i+1
				break
		totmem += int(memory[n3:n4]) # MB
	#--------------------------------------------------
	return totmem
コード例 #6
0
def Ass2Txt( pos, path, fmt=None ) : 
	#'''
	#Read and write Chinese

	#pos: 
	#	str to judge what are the real texts
	#  For example:
	#	pos = [('0000,0000,0000,,', '\N{'), ('shad1}', '\n')]

	#path:
	#	Must be one
	#	If path is one file, handle it
	#	If path is directory, use fmt to judge which files will be handled

	#fmt:
	#	with or without '.' is OK: 'ass' | '.ass'
	#'''
	from jizhipy.Basic import ShellCmd, Path, IsType
	import chardet, codecs
	path = Path.AbsPath(path)
	if (IsType.isfile(path)) : path = [path]
	else : 
		indir = path
		if (fmt is None) : path = ShellCmd('ls '+indir)
		else : 
			fmt = str(fmt).lower()
			if (fmt[0] != '.') : fmt = '.' + fmt
			path = ShellCmd('ls '+indir+'*'+fmt)
			fmt = fmt.upper()
			path += ShellCmd('ls '+indir+'*'+fmt)
		for i in range(len(path)) : path[i] = indir+path[i]
	#---------------------------------------------
	outnamelist = []
	for i in range(len(path)) : 
		filename = path[i]
		try : a = open(filename).read()
		except : continue
		code = chardet.detect(a)['encoding']
		a = a.decode(code)
		txt = ''
		while ('\n' in a) : 
			n = a.index('\n')
			b = a[:n+1]
			for i in range(len(pos)) : 
				n1 = n2 = -1
				if (pos[i][0] in b) : 
					n1 = b.rfind(pos[i][0])+len(pos[i][0])
				if (pos[i][1] in b) : n2 = b.rfind(pos[i][1])
				if (n1<0 or n2<0) : break
				c = b[n1:n2]
				if (c[0]  == '\r') : c = c[1:]
				if (c[-1] == '\r') : c = c[:-1]
				txt += c + '\n'
			if (txt != '') : txt += '\n'
			a = a[n+1:]
		outname = filename + '.txt'
		b = codecs.open(outname, 'w', code)
		b.write(txt)
		b.close()
		outnamelist.append(outname)
	return outnamelist
コード例 #7
0
ファイル: PoolFor.py プロジェクト: jizhi/jizhipy
def NprocessCPU(Nprocess=None):
    '''
	return [Nprocess, NprocessTot, threads, cores, Ncpu, hyper, cpuinfo]
	'''
    from jizhipy.Basic import ShellCmd
    try:
        uname = ShellCmd('uname')[0]
        #--------------------------------------------------
        if (uname == 'Linux'):
            threads = ShellCmd('cat /proc/cpuinfo | grep siblings')[-1]
            cores = ShellCmd('cat /proc/cpuinfo | grep "cpu cores"')[-1]
        elif (uname == 'Darwin'):
            threads = ShellCmd('sysctl machdep.cpu.thread_count')[-1]
            cores = ShellCmd('sysctl machdep.cpu.core_count')[-1]
        threads = int(threads.split(':')[-1])
        cores = int(cores.split(':')[-1])
        #--------------------------------------------------
        if (uname == 'Linux'):
            NprocessTot = len(ShellCmd('cat /proc/cpuinfo | grep processor'))
        elif (uname == 'Darwin'):
            NprocessTot = threads
        Ncpu = int(NprocessTot / threads)
        hyper = False if (threads == cores) else True
        #--------------------------------------------------
        if (Nprocess is None): Nprocess = NprocessTot
        elif (type(Nprocess) == str):
            Nprocess = Nprocess.lower()
            Nprocess = float(Nprocess.split('*none')[0])
            Nprocess = int(Nprocess * NprocessTot)
        rest = NprocessTot / 8
        if (rest < 1): rest = 1
        if (Nprocess > NprocessTot - rest): Nprocess = NprocessTot - rest
        if (Nprocess < 1): Nprocess = 1
        #--------------------------------------------------
        strs = '' if (Ncpu == 1) else 's'
        cpuinfo = ('CPU INFO: %i cpu' + strs +
                   ', each cpu has %i cores %i threads (hyper=' + str(hyper) +
                   '), total %i processors') % (Ncpu, cores, threads,
                                                NprocessTot)
        return (Nprocess, NprocessTot, threads, cores, Ncpu, hyper, cpuinfo)
    except:
        return (3, 4, 2, 2, 1, True, '')
コード例 #8
0
def GSM(fwhm,
        freq,
        lon=None,
        lat=None,
        nside=None,
        coordsys='Galactic',
        gsmpath='gsm_parameter.out',
        save=False):
    '''
	Already -2.726

	fwhm:
		[degree]
		fwhm==None: don't smooth the map

	freq, dfreq:
		[MHz]
		Must be one/isnum, NOT list
		* freq: center frequency
		* dfreq: averaged from freq-dfreq/2 (include) to freq+dfreq/2 (include). For GSM(), dfreq effect is very small, so remove this argument

	lon, lat:
		[degree]
		lon: RA  | l
		lat: Dec | b
		Must can be broadcast
		Use lon, lat first, otherwise, use nside

	nside:
		Healpix full sky map instead of lon, lat

	coordsys:
		'Equatorial' | 'Galactic' | 'Ecliptic'

	gsmpath:
		str, where is the program "gsm_parameter.out"
		./gsm_parameter.out  freq  outpath  gsmdir

	save:
		True | False
		Save gsm_xxx.npy or not?

	Compile:
		gfortran -ffixed-line-length-none gsm_parameter.f -o gsm_parameter.out

	return:
		gsm, ndarray
	'''
    import os
    import healpy as hp
    import numpy as np
    from jizhipy.Transform import CoordTrans
    from jizhipy.Array import Asarray
    from jizhipy.Basic import Raise, Path, ShellCmd, IsType
    if (gsmpath is None): gsmpath = 'gsm_parameter.out'
    freq = Asarray(freq).flatten()
    if (freq.size != 1):
        Raise(Exception,
              'freq must isnum, but now freq.size=' + str(freq.size))
    freq = freq[0]
    gsmpath = Path.AbsPath(str(gsmpath))
    if (not Path.ExistsPath(gsmpath)):
        gsmpath = Path.jizhipyPath('jizhipy_tool/GSM/gsm_parameter.out')
        if (not Path.ExistsPath(gsmpath)):
            Raise(Exception, 'gsmpath="' + gsmpath + '" NOT exists')
    n = gsmpath.rfind('/')
    if (n < 0): gsmdir = ''
    else: gsmdir = gsmpath[:n + 1]

    if (lon is not None and lat is not None):
        if (np.sum((lat < -90) + (lat > 90)) > 0):
            Raise(Exception, 'lat out of [-90, 90] degree')
        nside = None
    else:
        islist = True
        try:
            nside = int(nside)
        except:
            nside = 512
    #--------------------------------------------------

    # list local "gsm_*.npy"
    gsmlist = ShellCmd('ls gsm_*.npy')
    ngsm = None
    for i in range(len(gsmlist)):
        try:
            f = np.sort([float(gsmlist[i][4:-4]), freq])
        except:
            continue
        r = (f[1] / f[0])**3
        if (r < 1.01):
            ngsm = i
            break
    if (ngsm is None):
        freqstr = ('%.2f' % freq)
        if (freqstr[-1] == '0'): freqstr = freqstr[:-1]
        if (freqstr[-1] == '0'): freqstr = freqstr[:-2]
        outname = 'gsm_' + freqstr
        os.system(gsmpath + ' ' + freqstr + ' ' + outname + '.dat ' + gsmdir)
        gsm = np.float32(np.loadtxt(outname + '.dat'))
        os.system('rm ' + outname + '.dat')
        if (save): np.save(outname + '.npy', gsm)
    else:
        gsm = np.float32(np.load(gsmlist[ngsm]))
        if (not save): os.system('rm ' + gsmlist[ngsm])
    if (Path.ExistsPath('qaz_cols.dat')): os.system('rm qaz_cols.dat')
    nsidegsm = hp.get_nside(gsm)
    #--------------------------------------------------

    coordsys = str(coordsys).lower()
    if (nside is None):
        if (fwhm is not None and fwhm > 1):
            fwhm = (fwhm**2 - 1)**0.5
            gsm = hp.smoothing(gsm, fwhm * np.pi / 180, verbose=False)
            gsm[gsm < 0] = 0
        islist = False if (IsType.isnum(lon + lat)) else True
        lon, lat = lon + lat * 0, lon * 0 + lat  # same shape
        lon %= 360
        lon, lat = CoordTrans.Celestial(lon * np.pi / 180, lat * np.pi / 180,
                                        coordsys, 'galactic')  # rad
        npix = hp.ang2pix(nsidegsm, np.pi / 2 - lat, lon)
        del lon, lat
        gsm = gsm[npix]
        del npix
    else:
        if (nside != nsidegsm): gsm = hp.ud_grade(gsm, nside)
        if (coordsys != 'galactic'):
            gsm = CoordTrans.CelestialHealpix(gsm, 'RING', 'galactic',
                                              coordsys)[0]
        if (fwhm is not None and fwhm > 1):
            fwhm = (fwhm**2 - 1)**0.5
            gsm = hp.smoothing(gsm, fwhm * np.pi / 180, verbose=False)
            gsm[gsm < 0] = 0
    gsm = np.float32(gsm)
    if (not islist): gsm = gsm[0]
    return gsm
コード例 #9
0
    def Property(self, path):
        '''
		return the property of the file/directory

		Parameters
		----------
		path:
			[str] | [list of str]

		Returns
		----------
		return property

		property:
			[dict] | [list of dict]
			property[i] = {'permision':'?', 'group':'?', 'size':'?', 'date':'?', 'path':path[i]}
		'''
        from jizhipy.Basic import IsType, ShellCmd, Time
        import os
        year = Time(0).split('/')[0]
        if (IsType.isstr(path)): islist, path = False, [path]
        else: islist = True
        prop, path = [], self.Abs(path)
        for i in range(len(path)):
            d = {
                'permision': '',
                'group': '',
                'size': '',
                'date': '',
                'path': path[i]
            }
            if (os.path.exists(path[i]) is False):
                prop.append(d)
                continue
            if (IsType.isfile(path[i])):
                p, ds = ShellCmd('ls -lh ' + path[i])[0], None
            else:
                if (path[i][-1] != '/'): path[i] += '/'
                n = path[i][:-1].rfind('/')
                dn, bn = path[i][:n], path[i][n + 1:-1]
                q = ShellCmd('ls -lh ' + dn + ' | grep ' + bn)
                for p in q:
                    if (p[-len(bn) - 1:] == ' ' + bn): break
                ds = ShellCmd('du -sh ' + path[i])[0].split('\t')[0]
            p = p.split(' ')
            while ('' in p):
                p.remove('')
            d['path'] = path[i]
            d['permision'] = p[0]
            d['group'] = p[2] + ':' + p[3]
            d['size'] = p[4] if (ds is None) else ds
            m, day = p[5], p[6]
            try:
                int(m)
            except:
                m = m[:-1]
            if (':' in p[7]): y, h = year, p[7] + ':00'
            else: y, h = p[7], '12:34:56'
            d['date'] = y + '/' + m + '/' + day + ' ' + h
            prop.append(d)
        if (not islist): prop = prop[0]
        return prop