casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
utils.py
Go to the documentation of this file.
00001 import os
00002 
00003 def mask_and(a, b):
00004     assert(len(a)==len(b))
00005     return [ a[i] & b[i] for i in xrange(len(a)) ]
00006 
00007 def mask_or(a, b):
00008     assert(len(a)==len(b))
00009     return [ a[i] | b[i] for i in xrange(len(a)) ]
00010 
00011 def mask_not(a):
00012     return [ not i for i in a ]
00013 
00014 def _n_bools(n, val):
00015     return [ val for i in xrange(n) ]
00016 
00017 def _is_sequence_or_number(param, ptype=int):
00018     if isinstance(param,tuple) or isinstance(param,list):
00019         if len(param) == 0: return True # empty list
00020         out = True
00021         for p in param:
00022             out &= isinstance(p,ptype)
00023         return out
00024     elif isinstance(param, ptype):
00025         return True
00026     return False
00027 
00028 def _to_list(param, ptype=int):
00029     if isinstance(param, ptype):
00030         if ptype is str: return param.split()
00031         else: return [param]
00032     if _is_sequence_or_number(param, ptype):
00033         return param
00034     return None
00035 
00036 def unique(x):
00037     """
00038     Return the unique values in a list
00039     Parameters:
00040         x:      the list to reduce
00041     Examples:
00042         x = [1,2,3,3,4]
00043         print unique(x)
00044         [1,2,3,4]
00045     """
00046     return dict([ (val, 1) for val in x]).keys()
00047 
00048 def list_files(path=".",suffix="rpf"):
00049     """
00050     Return a list files readable by asap, such as rpf, sdfits, mbf, asap
00051     Parameters:
00052         path:     The directory to list (default '.')
00053         suffix:   The file extension (default rpf)
00054     Example:
00055         files = list_files("data/","sdfits")
00056         print files
00057         ['data/2001-09-01_0332_P363.sdfits',
00058         'data/2003-04-04_131152_t0002.sdfits',
00059         'data/Sgr_86p262_best_SPC.sdfits']
00060     """
00061     if not os.path.isdir(path):
00062         return None
00063     valid = "ms rpf rpf.1 rpf.2 sdf sdfits mbf asap".split()
00064     if not suffix in valid:
00065         return None
00066     files = [os.path.expanduser(os.path.expandvars(path+"/"+f)) for f in os.listdir(path)]
00067     return filter(lambda x: x.endswith(suffix),files)
00068 
00069 def page(message):
00070     """Run the input message through a pager. This is only done if
00071     ``rcParams["verbose"]`` is set.
00072     """
00073     verbose = False
00074     try:
00075         from asap.parameters import rcParams
00076         verbose = rcParams['verbose']
00077     except:
00078         pass
00079     if verbose:
00080         try:
00081             from IPython.genutils import page as pager
00082         except ImportError:
00083             from pydoc import pager
00084         pager(message)
00085         return None
00086     else:
00087         return message
00088 
00089 def toggle_verbose():
00090     from asap import rcParams
00091     rcParams['verbose'] = not bool(rcParams['verbose'])