casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
task_browsetable.py
Go to the documentation of this file.
00001 import os
00002 import re
00003 import shutil
00004 import tempfile
00005 from taskinit import *
00006 
00007 def browsetable(tablename=None, mightedit=None,
00008                 sortlist=None, taql=None, skipcols=None):
00009     """ Browse a table (visibility data set, calibration table, or image):
00010 
00011     Brings up a GUI browser that can open and display any CASA table.
00012 
00013     Parameters:
00014         tablename -- Name of table directory on disk (MS, cal. table, image)
00015                      default: none; example: tablename='ngc5921.ms'
00016         mightedit -- If True disable the filtering options (below) and allow
00017                      editing the table.  Warning: the GUI appears to ignore
00018                      whether the table tool is opened read-only - just be
00019                      aware that you should not edit filtered tables unless
00020                      you know what you are doing.
00021         sortlist  -- List of columns to sort by.
00022                      default: [] (none)
00023         taql      -- TaQL query string for prefiltering the table.
00024                      default: '' (none); example: taql='ANTENNA2 < 6'
00025         skipcols  -- Columns to NOT display.
00026                      default: [] (none); example: skipcols='feed1, feed2'
00027     """
00028     #Python script
00029     try:
00030         casalog.origin('browsetable')
00031         if type(tablename) == str:
00032             t = tbtool()
00033             mightfilter = sortlist or taql or skipcols
00034             if os.path.exists(tablename):
00035                 t.open(tablename, nomodify=not mightedit)
00036             if not tablename or mightedit or not mightfilter:
00037                 t.browse()
00038             else:
00039                 colnames = t.colnames()
00040                 if type(skipcols) == str:
00041                     skipcols = re.split(r',?\s+', skipcols)
00042                 for c in skipcols:
00043                     if c.upper() in colnames:
00044                         colnames.remove(c.upper())
00045                 if type(sortlist) == list:
00046                     sortlist = ', '.join(sortlist)
00047                    
00048                 # ft.browse won't work unless ft is given a name.  This does NOT
00049                 # appear to cause more than about 116 kB to be written to disk,
00050                 # but it does mean that the directory needs to be removed
00051                 # afterwards.
00052                 tempdir = '/tmp'
00053                 if not os.path.isdir(tempdir):
00054                     tempdir = os.getcwd()
00055                 ftname = tempfile.mkdtemp(prefix=tablename + '_filtered_', dir=tempdir)
00056                 #print "ftname =", ftname
00057                 casalog.post('Using ' + ftname + ' as the filtered table.')
00058                
00059                 ft = t.query(taql, ftname, sortlist=sortlist.upper(),
00060                              columns=', '.join(colnames))
00061                 #print "ft.name() =", ft.name()
00062                 ft.browse()
00063                 ft.close()
00064                 #shutil.rmtree(ftname)  # Can't do until browser exits.
00065             t.close()  # Can be closed even if not open.
00066         else:
00067             raise Exception, 'Table ' + str(tablename) + ' not found - please check the name'
00068     except Exception, instance:
00069         print '*** Error ***', instance