casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
task_listpartition.py
Go to the documentation of this file.
00001 import os
00002 import numpy as np
00003 import pprint
00004 from taskinit import *
00005 import partitionhelper as ph
00006 from parallel.parallel_task_helper import ParallelTaskHelper
00007 
00008 
00009 def listpartition(vis=None, createdict=None, listfile=None):
00010     
00011     """List information about an MMS data set in the logger:
00012 
00013        Keyword arguments:
00014        vis -- Name of multi-MS or normal MS.
00015                default: none. example: vis='uidA002.mms'
00016        createdict -- Create and return a dictionary with information about
00017                      the sub-MSs.
00018                default: False
00019        listfile -- save the output to a file
00020              default: ''. Example: listfile="mylist.txt"
00021              
00022         The task also returns a dictionary with scan summary information
00023         for each sub-MS. 
00024                       
00025 
00026        """
00027 
00028     casalog.origin('listpartition')
00029 
00030     mslocal = casac.ms()
00031     mslocal1 = casac.ms()
00032             
00033 
00034     try:
00035         if (type(vis) == str) & os.path.exists(vis):
00036             mslocal.open(thems=vis)
00037         else:
00038             raise Exception, \
00039                 'Visibility data set not found - please verify the name'
00040 
00041         # Check output filename existence 
00042         if listfile != '':
00043             if (type(listfile) == str) & os.path.exists(listfile):
00044                 raise Exception, 'Output file \'%s\' already exists'%listfile
00045             
00046             casalog.post('Will save output to \'%s\''%listfile)
00047             ffout = open(listfile, 'w')
00048             
00049                 
00050         # Is it a multi-MS?
00051         ismms = mslocal.ismultims()
00052         
00053         # List of MSs to process
00054         mslist = []
00055         
00056         # It is a multi-MS
00057         if ismms:
00058             casalog.post('This is a multi-MS')
00059             mslist = mslocal.getreferencedtables()
00060             mslist.sort()
00061             sname = 'Sub-MS'
00062         else:
00063             mslist.append(vis)
00064             sname = 'MS'
00065             
00066         # Close top MS
00067         mslocal.close()
00068         
00069         # Create lists for scan and spw dictionaries of each sub-MS
00070         msscanlist = []
00071         msspwlist = []
00072 
00073         # List with sizes in bytes per sub-MS
00074         sizelist = []
00075         
00076         # Loop through all sub-Mss
00077         for subms in mslist:
00078             mslocal1.open(subms)
00079             scans = mslocal1.getscansummary()
00080             msscanlist.append(scans)
00081             spws = mslocal1.getspectralwindowinfo()
00082             msspwlist.append(spws)
00083             mslocal1.close()
00084 
00085             # Get the data volume in bytes per sub-MS
00086             sizelist.append(ph.getDiskUsage(subms))
00087 
00088         # Get the information to list in output
00089         # Dictionary to return
00090         outdict = {}
00091 
00092         for ims in range(mslist.__len__()):   
00093             # Create temp dictionary for each sub-MS
00094             tempdict = {}
00095             msname = os.path.basename(mslist[ims])
00096             tempdict['MS'] = msname
00097             tempdict['size'] = sizelist[ims]
00098             
00099             # Get scan dictionary for this sub-MS
00100             scandict = msscanlist[ims]
00101             
00102             # Get spw dictionary for this sub-MS
00103             # NOTE: the keys of spwdict.keys() are NOT the spw Ids
00104             spwdict = msspwlist[ims]
00105             
00106             # The keys are the scan numbers
00107             scanlist = scandict.keys()
00108             
00109             # Get information per scan
00110             tempdict['scanId'] = {}
00111             for scan in scanlist:
00112                 newscandict = {}
00113                 subscanlist = scandict[scan].keys()
00114                 
00115                 # Get spws and nrows per sub-scan
00116                 nrows = 0
00117                 aspws = np.array([],dtype='int32')
00118                 for subscan in subscanlist:
00119                     nrows += scandict[scan][subscan]['nRow']
00120 
00121                     # Get the spws for each sub-scan
00122                     spwids = scandict[scan][subscan]['SpwIds']
00123                     aspws = np.append(aspws,spwids)
00124 
00125                 newscandict['nrows'] = nrows
00126 
00127                 # Sort spws  and remove duplicates
00128                 aspws.sort()
00129                 uniquespws = np.unique(aspws)
00130                 newscandict['spwIds'] = uniquespws
00131                                 
00132                 # Array to hold channels
00133                 charray = np.empty_like(uniquespws)
00134                 spwsize = np.size(uniquespws)
00135                 
00136 
00137                 # Now get the number of channels per spw
00138                 for ind in range(spwsize):
00139                     spwid = uniquespws[ind]
00140                     for sid in spwdict.keys():
00141                         if spwdict[sid]['SpectralWindowId'] == spwid:
00142                             nchans = spwdict[sid]['NumChan']
00143                             charray[ind] = nchans
00144                             continue
00145                     
00146                 newscandict['nchans'] = charray
00147                 tempdict['scanId'][int(scan)] = newscandict
00148                     
00149                 
00150             outdict[ims] = tempdict
00151 #            pprint.pprint(outdict)
00152 
00153 
00154         # Now loop through the dictionary to print the information
00155         if outdict.keys() == []:
00156             casalog.post('Error in processing dictionaries','ERROR')
00157         
00158         indices = outdict.keys()
00159         indices.sort()
00160             
00161         counter = 0
00162         for index in indices:
00163             
00164             # Get data
00165             MS = outdict[index]['MS']            
00166             SIZE = outdict[index]['size']
00167             SCAN = outdict[index]['scanId']
00168                         
00169             # Sort scans for more optimal printing
00170             # Print information per scan
00171             firstscan = True
00172             skeys = SCAN.keys()
00173             skeys.sort()
00174             for myscan in skeys:
00175                 SPW = outdict[index]['scanId'][myscan]['spwIds']
00176                 NCHAN = outdict[index]['scanId'][myscan]['nchans']
00177                 NROWS = outdict[index]['scanId'][myscan]['nrows']
00178                 
00179                 # Get maximum widths
00180                 smxw = getWidth(outdict, 'spw')
00181                 cmxw = getWidth(outdict, 'channel')
00182                 
00183                 # Create format
00184                 fhdr = '%-'+str(len(MS)+2)+'s' + '%-6s' + '%-'+str(smxw+2)+'s' + \
00185                         '%-'+str(cmxw+2)+'s' + '%-8s' + '%-6s'
00186     
00187                 
00188                 # Print header
00189                 text = ''
00190                 if counter == 0:
00191                     text = text + fhdr % (sname,'Scan','Spw','Nchan','Nrows','Size')  
00192                     text = text + '\n'                  
00193                 counter += 1
00194                 
00195                 # Print first scan
00196                 if firstscan:
00197                     text = text + fhdr % (MS, myscan, SPW, NCHAN, NROWS, SIZE)   
00198                 else:
00199                     text = text + fhdr % ('', myscan, SPW, NCHAN, NROWS, '')
00200                 
00201                 firstscan = False            
00202 
00203                 # Print to a file
00204                 if listfile != '':
00205                     print >> ffout, text
00206                 else:
00207                     # Print to the logger
00208                     casalog.post(text)
00209                                 
00210                 
00211         if listfile != '':    
00212             ffout.close()
00213                                         
00214      
00215         # Return the scan dictionary
00216         if createdict:
00217             return outdict
00218         
00219         return {}
00220             
00221     except Exception, instance:
00222 #        mslocal.close()
00223         print '*** Error ***', instance
00224     
00225 
00226            
00227 def getWidth(adict, par):
00228     
00229     width = 0
00230     for aa in adict.keys():
00231         scans = adict[aa]['scanId'].keys()
00232         for bb in scans:
00233             if par == 'spw':
00234                 spws = adict[aa]['scanId'][bb]['spwIds']
00235                 mystr = str(spws)
00236                 length = len(mystr)
00237                 if length > width:
00238                     width = length
00239                     
00240             elif par == 'channel':
00241                 chans = adict[aa]['scanId'][bb]['nchans']
00242                 mystr = str(chans)
00243                 length = len(mystr)
00244                 if length > width:
00245                     width = length
00246     if width < 5:
00247         width = 5
00248         
00249     return width
00250 
00251 
00252 
00253 
00254 
00255 
00256 
00257 
00258 
00259