1 | import os
|
---|
2 |
|
---|
3 | def mask_and(a, b):
|
---|
4 | assert(len(a)==len(b))
|
---|
5 | return [ a[i] & b[i] for i in xrange(len(a)) ]
|
---|
6 |
|
---|
7 | def mask_or(a, b):
|
---|
8 | assert(len(a)==len(b))
|
---|
9 | return [ a[i] | b[i] for i in xrange(len(a)) ]
|
---|
10 |
|
---|
11 | def mask_not(a):
|
---|
12 | return [ not i for i in a ]
|
---|
13 |
|
---|
14 | def _n_bools(n, val):
|
---|
15 | return [ val for i in xrange(n) ]
|
---|
16 |
|
---|
17 | def _is_sequence_or_number(param, ptype=int):
|
---|
18 | import numpy
|
---|
19 | #if isinstance(param,tuple) or isinstance(param,list):
|
---|
20 | if type(param) in (tuple, list, numpy.ndarray):
|
---|
21 | if len(param) == 0: return True # empty list
|
---|
22 | out = True
|
---|
23 | for p in param:
|
---|
24 | out &= isinstance(p,ptype)
|
---|
25 | return out
|
---|
26 | elif isinstance(param, ptype):
|
---|
27 | return True
|
---|
28 | return False
|
---|
29 |
|
---|
30 | def _to_list(param, ptype=int):
|
---|
31 | if isinstance(param, ptype):
|
---|
32 | if ptype is str: return param.split()
|
---|
33 | else: return [param]
|
---|
34 | if _is_sequence_or_number(param, ptype):
|
---|
35 | return list(param)
|
---|
36 | return None
|
---|
37 |
|
---|
38 | def unique(x):
|
---|
39 | """
|
---|
40 | Return the unique values in a list
|
---|
41 | Parameters:
|
---|
42 | x: the list to reduce
|
---|
43 | Examples:
|
---|
44 | x = [1,2,3,3,4]
|
---|
45 | print unique(x)
|
---|
46 | [1,2,3,4]
|
---|
47 | """
|
---|
48 | return dict([ (val, 1) for val in x]).keys()
|
---|
49 |
|
---|
50 | def list_files(path=".",suffix="rpf"):
|
---|
51 | """
|
---|
52 | Return a list files readable by asap, such as rpf, sdfits, mbf, asap
|
---|
53 | Parameters:
|
---|
54 | path: The directory to list (default '.')
|
---|
55 | suffix: The file extension (default rpf)
|
---|
56 | Example:
|
---|
57 | files = list_files("data/","sdfits")
|
---|
58 | print files
|
---|
59 | ['data/2001-09-01_0332_P363.sdfits',
|
---|
60 | 'data/2003-04-04_131152_t0002.sdfits',
|
---|
61 | 'data/Sgr_86p262_best_SPC.sdfits']
|
---|
62 | """
|
---|
63 | if not os.path.isdir(path):
|
---|
64 | return None
|
---|
65 | valid = "ms rpf rpf.1 rpf.2 sdf sdfits mbf asap".split()
|
---|
66 | if not suffix in valid:
|
---|
67 | return None
|
---|
68 | files = [os.path.expanduser(os.path.expandvars(path+"/"+f)) for f in os.listdir(path)]
|
---|
69 | return filter(lambda x: x.endswith(suffix),files)
|
---|
70 |
|
---|
71 | def page(message):
|
---|
72 | """Run the input message through a pager. This is only done if
|
---|
73 | ``rcParams["verbose"]`` is set.
|
---|
74 | """
|
---|
75 | verbose = False
|
---|
76 | try:
|
---|
77 | from asap.parameters import rcParams
|
---|
78 | verbose = rcParams['verbose']
|
---|
79 | except:
|
---|
80 | pass
|
---|
81 | if verbose:
|
---|
82 | try:
|
---|
83 | from IPython.genutils import page as pager
|
---|
84 | except ImportError:
|
---|
85 | from pydoc import pager
|
---|
86 | pager(message)
|
---|
87 | return None
|
---|
88 | else:
|
---|
89 | return message
|
---|
90 |
|
---|
91 | def toggle_verbose():
|
---|
92 | from asap import rcParams
|
---|
93 | rcParams['verbose'] = not bool(rcParams['verbose'])
|
---|