casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
find_autocorr.py
Go to the documentation of this file.
00001 from __future__ import with_statement
00002 from contextlib import contextmanager
00003 from glob import glob
00004 import os
00005 import shutil
00006 import tempfile
00007 
00008 """
00009 Version 2 (2009/12/7), prompted by Ger van Diepen's suggestion of using TaQL
00010 instead of tb.getcol().  Requires python >= 2.5.
00011 """
00012 
00013 def list_autocorr(mspat="*[-_.][Mm][Ss]"):
00014     """
00015     List MSes that include autocorrelations, and the flagging status of those
00016     autocorrelations.
00017 
00018     Status codes:
00019     N: The MS has unflagged autocorrelations.
00020     R: The MS has autocorrelations, but FLAG_ROW is True for all of them.
00021     F: The MS has non-rowflagged autocorrelations, but they are still flagged.
00022     """
00023     mses = glob(mspat)
00024 
00025     if mses:
00026         msdict = find_autocorr(mses)
00027         if msdict:
00028             print "F MS"
00029             print "- --"
00030             mses = msdict.keys()
00031             mses.sort()
00032             for m in mses:
00033                 print msdict[m], m
00034         else:
00035             print "No autocorrelations found."
00036     else:
00037         print "Nothing matching", mspat, "was found."
00038 
00039 
00040 @contextmanager
00041 def taql(query, tbinst=None, cols=None):
00042     if not tbinst:
00043         tbinst = tb
00044     tmpnam = tempfile.mkdtemp(suffix="_tb", dir='.')
00045     if cols:
00046         cols = cols.rstrip() + ' '
00047         restab = tbinst.query(query, tmpnam, columns=cols)
00048     else:
00049         restab = tbinst.query(query, tmpnam)
00050     try:
00051         yield restab
00052     finally:
00053         restab.close()
00054         shutil.rmtree(tmpnam)
00055 
00056 
00057 def find_autocorr(mses):
00058     """
00059     Check the list of MSes in mses for ones that include autocorrelations, and
00060     return the flagging status of those autocorrelations in a dictionary keyed
00061     by MS name.
00062 
00063     Status codes:
00064     N: The MS has unflagged autocorrelations.
00065     R: The MS has autocorrelations, but FLAG_ROW is True for all of them.
00066     F: The MS has non-rowflagged autocorrelations, but they are still flagged.
00067     """
00068     retval = {}
00069     for currms in mses:
00070         tb.open(currms)
00071         with taql('ANTENNA1==ANTENNA2', cols="FLAG_ROW, FLAG") as t1:
00072             if t1.nrows() > 0:
00073                 retval[currms] = 'N'
00074                 with taql('!FLAG_ROW', t1, cols="FLAG") as t2:
00075                     if t2.nrows() == 0:
00076                         retval[currms] = 'R'
00077                     else:
00078                         with taql('!all(FLAG)', t2) as t3:
00079                             if t3.nrows() == 0:
00080                                 retval[currms] = 'F'
00081         tb.close()
00082     return retval