casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
importevla_pg.py
Go to the documentation of this file.
00001 #
00002 # This file was generated using xslt from its XML file
00003 #
00004 # Copyright 2008, Associated Universities Inc., Washington DC
00005 #
00006 import sys
00007 import os
00008 from casac import *
00009 import string
00010 import time
00011 import inspect
00012 import gc
00013 import numpy
00014 from odict import odict
00015 from task_importevla import importevla
00016 from task_importevla import casalog
00017 
00018 class importevla_pg_:
00019     __name__ = "importevla"
00020 
00021     def __init__(self) :
00022        self.__bases__ = (importevla_pg_,)
00023        self.__doc__ = self.__call__.__doc__
00024 
00025 
00026     def __call__(self, asdm=None, vis=None, ocorr_mode=None, compression=None, asis=None, scans=None, verbose=None, overwrite=None, online=None, tbuff=None, flagzero=None, flagpol=None, shadow=None, tolerance=None, addantenna=None, applyflags=None, savecmds=None, outfile=None, flagbackup=None, async=None):
00027 
00028         """Convert an Science Data Model observation into a CASA Measurement Set
00029 Convert a Science Data Model (SDM) dataset into a CASA Measurement Set (MS).  
00030 Will place online flags and specified clip/shadow flags into FLAG_CMD table 
00031 and optionally apply to MS.
00032 
00033 Warning: This version is under development and is geared to handling EVLA 
00034 specific flag and system files, and is otherwise identical to importasdm.
00035                                           
00036         HISTORY: Task created v1.0 S.T. Myers 2010-03-11 (3.0.1)
00037                  Last updated v9.0 S.M. Castro 2012-03-13 (3.4) code+doc
00038 
00039 Keyword arguments:
00040 asdm         -- Name of input SDM file (directory)
00041                 default: none; 
00042                     Example: asdm='ExecBlock3'
00043 
00044 vis          -- Root ms or scantable name, note a .ms is NOT appended to name
00045                 default: none
00046 
00047 ocorr_mode   -- output data for correlation mode AUTO_ONLY 
00048                 (ao) or CROSS_ONLY (co) or CROSS_AND_AUTO (ca)
00049                 default: co (for EVLA)
00050 
00051 compression  -- produces comrpressed columns in the resulting measurement set.
00052                 default: False
00053 
00054 asis         -- creates verbatim copies of the ASDM tables in 
00055                 the output measurement set. The value given to
00056                 this option must be a list of table names separated
00057                 by space characters; the wildcard character '*' is 
00058                 allowed in table names.
00059 
00060 scans        -- processes the scans requested in this parameter (default is
00061                 all scans).  For simplest use provide a comma-separated list of 
00062                 scan ranges, e.g. scans='1~3,5,10~20'.
00063                 default: '' = all scans
00064 
00065                 NOTE: A scan specification tecnically consists of an ExecBlock 
00066                 index followed by the character ':' followed by a comma
00067                 separated list of scan indexes or scan index ranges. The EVLA
00068                 does not currently include more than one ExecBlock in a SDM
00069                 so this specification prefix is not needed.
00070 
00071                 By default all the scans are considered. 
00072 
00073 overwrite    -- Over write an existing MS
00074 
00075 verbose      -- produce log output as asdm2MS is being run
00076 
00077 EVLA-specific parameters:
00078 -------------------------
00079 
00080 online       -- create flagging commands for online flags. The commands will be saved to the FLAG_CMD 
00081                 sub-table of the MS. Optionally, it can also be saved to an ASCII file when savecmds is set
00082                 to True.
00083                 default: True
00084  
00085             >>> online expandable parameters
00086                  tbuff -- (float) time padding buffer (in seconds)
00087                  default: 0.0
00088 
00089                  NOTE: this time is in seconds. You should currently
00090                  set the value of tbuff to be 1.5x the correlator
00091                  integration time if greater than 1 second.  For
00092                  example, if the SDM has integrations of 3 seconds,
00093                  set tbuff=4.5.  Likewise, set tbuff=15.0 for 10-sec
00094                  integrations.
00095                                   
00096  
00097 flagzero     -- create flags to clip out visibilities with zero values. The command will be saved to the FLAG_CMD 
00098                 sub-table of the MS. Optionally, it can also be saved to an ASCII file when savecmds is set
00099                 to True.
00100                 default: True
00101  
00102             >>> flagzero expandable parameter(s) 
00103                  flagpol -- (boolean) also zero-clip on cross-hands (default=False)
00104  
00105 shadow       -- create flags for antennas that are shadowed. The command will be saved to the FLAG_CMD 
00106                 sub-table of the MS. Optionally, it can also be saved to an ASCII file when savecmds is set
00107                 to True.
00108                 default: True
00109  
00110             >>> shadow expandable parameter
00111                  tolerance -- Amount of shadowing allowed in meters.
00112                    default: 0.0
00113 
00114                  addantenna -- It can be either a file name with additional antenna names, positions 
00115                                and diameters, or a Python dictionary with the same information. 
00116                                You can use the flaghelper functions to create the dictionary from a file.  
00117                    default: ''
00118                    
00119                    To create a dictionary inside casapy.
00120                    > import flaghelper as fh
00121                    > antdic = fh.readAntennaList(antfile)
00122                    
00123                    Where antfile is a text file in disk that contains information such as:
00124                     name=VLA01
00125                     diameter=25.0
00126                     position=[-1601144.96146691, -5041998.01971858, 3554864.76811967]
00127                     name=VLA02
00128                     diameter=25.0
00129                     position=[-1601105.7664601889, -5042022.3917835914, 3554847.245159178]
00130                    
00131  
00132 applyflags   -- apply the online and specified flags to the MS
00133                 default: False
00134 
00135 
00136 savecmds   -- Save the flag commands to an ASCII file given by the parameter outfile. It will save the
00137               flag commands from online, flagzero and/or shadow if they are set to True. 
00138                 default: False
00139                     
00140             >>> savecmds expandable parameter
00141                 outfile      -- Filename where to save the flag commands.
00142                 default: ' ' --> by default it will save on a filename composed from the MS name.
00143                     Example: vis='evla.ms', the outfile will be 'evla_cmd.txt'. 
00144                     
00145                     NOTE: The file is open to save in append mode.
00146 
00147 
00148 flagbackup   -- Backup original flags in >ms<.flagversions 
00149                 default: True
00150 
00151        Examples:
00152 
00153 
00154         1)  Produces MS CLowTest_000.ms with autocorrelations.
00155             You will find the online, zero, and shadow flags in the FLAG_CMD table
00156             for later application.  Does not apply any flags.
00157                 
00158              importevla(asdm='CLowTest_000',ocorr_mode='ca')
00159                 
00160 
00161         2)  Produces MS CLowTest_000.ms without autocorrelations.  
00162                
00163              importevla(asdm='CLowTest_000')
00164                 
00165 
00166         3)  Will apply online flags and uses a more conservative 2sec buffer
00167                 before the start and after the end timeranges.
00168                 
00169              importevla(asdm='CLowTest_000',online=True,tbuff=2.0,applyflags=True)
00170                 
00171 
00172         4)  This will create the FLAG_CMD sub-table using online flags only,
00173             but will not apply them to the MS.
00174 
00175             importevla(asdm='CLowTest_000',online=True,flagzero=False,shadow=False)
00176 
00177 
00178         5)  This will write the online flags to the FLAG_CMD table. It will also save commands
00179             to clip zeros and to flag shadowed antennas to the table. The commands will be further 
00180             applied to the data and the APPLIED column of the FLAG_CMD will be updated to True.
00181 
00182             importevla(asdm='CLowTest_000',online=True,flagzero=True,shadow=True,applyflags=True)
00183 
00184 
00185         6)  Import only scans 1, 2, 3, 5, 7, 9, save the online, shadow and clip commands to an ASCII file and
00186             do not apply the flags. The commands will be saved to CLowTest_000_cmd.txt.
00187 
00188             importevla(asdm='CLowTest_000',scans='1~3,5,7,9',online=True,flagzero=True,shadow=True,
00189                                 applyflags=False, savecmds=True)
00190                 
00191                 You can use either flagdata or flagcmd to apply the flags later with the following commands:
00192                 
00193                 Apply all the flags in the file using flagdata
00194                 flagdata('CLowTest_000.ms', mode='list', inpfile='CLowTest_000_cmd.txt')
00195 
00196                 Select by reason on the file
00197                 flagdata('CLowTest_000.ms',mode='list', inpfile='CLowTest_000_cmd.txt', 
00198                            reason=['ANTENNA_NOT_POINTING','CORRELATOR_DATA_INVALID'])
00199                 
00200                 Apply all the flags in the file using flagcmd
00201                 flagcmd('CLowTest_000.ms',inpmode='list',inpfile='CLTest_000_cmd.txt',action='apply')
00202                                 
00203 
00204 HISTORY: Task last updated v9.0 S.M. Castro 2012-03-8 (3.4.0)
00205          Docs last updated v9.0 S.M. Castro 2012-03-13 (3.4.0)
00206 
00207   
00208         """
00209         a=inspect.stack()
00210         stacklevel=0
00211         for k in range(len(a)):
00212           if (string.find(a[k][1], 'ipython console') > 0) or (string.find(a[k][1], '<string>') >= 0):
00213                 stacklevel=k
00214                 break
00215         myf=sys._getframe(stacklevel).f_globals
00216         myf['__last_task'] = 'importevla'
00217         myf['taskname'] = 'importevla'
00218         ###
00219         myf['update_params'](func=myf['taskname'],printtext=False)
00220         ###
00221         ###
00222         #Handle globals or user over-ride of arguments
00223         #
00224         function_signature_defaults=dict(zip(self.__call__.func_code.co_varnames,self.__call__.func_defaults))
00225         useLocalDefaults = False
00226 
00227         for item in function_signature_defaults.iteritems():
00228                 key,val = item
00229                 keyVal = eval(key)
00230                 if (keyVal == None):
00231                         #user hasn't set it - use global/default
00232                         pass
00233                 else:
00234                         #user has set it - use over-ride
00235                         if (key != 'self') :
00236                            useLocalDefaults = True
00237                         #myf[key]=keyVal
00238 
00239         myparams = {}
00240         if useLocalDefaults :
00241            for item in function_signature_defaults.iteritems():
00242                key,val = item
00243                keyVal = eval(key)
00244                exec('myparams[key] = keyVal')
00245                if (keyVal == None):
00246                    exec('myparams[key] = '+ key + ' = self.itsdefault(key)')
00247                    keyVal = eval(key)
00248                    if(type(keyVal) == dict) :
00249                       exec('myparams[key] = ' + key + ' = keyVal[len(keyVal)-1][\'value\']')
00250 
00251         else :
00252             uselessvariable = None 
00253             myparams['asdm'] = asdm = myf['asdm']
00254             myparams['vis'] = vis = myf['vis']
00255             myparams['ocorr_mode'] = ocorr_mode = myf['ocorr_mode']
00256             myparams['compression'] = compression = myf['compression']
00257             myparams['asis'] = asis = myf['asis']
00258             myparams['scans'] = scans = myf['scans']
00259             myparams['verbose'] = verbose = myf['verbose']
00260             myparams['overwrite'] = overwrite = myf['overwrite']
00261             myparams['online'] = online = myf['online']
00262             myparams['tbuff'] = tbuff = myf['tbuff']
00263             myparams['flagzero'] = flagzero = myf['flagzero']
00264             myparams['flagpol'] = flagpol = myf['flagpol']
00265             myparams['shadow'] = shadow = myf['shadow']
00266             myparams['tolerance'] = tolerance = myf['tolerance']
00267             myparams['addantenna'] = addantenna = myf['addantenna']
00268             myparams['applyflags'] = applyflags = myf['applyflags']
00269             myparams['savecmds'] = savecmds = myf['savecmds']
00270             myparams['outfile'] = outfile = myf['outfile']
00271             myparams['flagbackup'] = flagbackup = myf['flagbackup']
00272 
00273 
00274         result = None
00275 
00276 #
00277 #    The following is work around to avoid a bug with current python translation
00278 #
00279         mytmp = {}
00280 
00281         mytmp['asdm'] = asdm
00282         mytmp['vis'] = vis
00283         mytmp['ocorr_mode'] = ocorr_mode
00284         mytmp['compression'] = compression
00285         mytmp['asis'] = asis
00286         mytmp['scans'] = scans
00287         mytmp['verbose'] = verbose
00288         mytmp['overwrite'] = overwrite
00289         mytmp['online'] = online
00290         mytmp['tbuff'] = tbuff
00291         mytmp['flagzero'] = flagzero
00292         mytmp['flagpol'] = flagpol
00293         mytmp['shadow'] = shadow
00294         mytmp['tolerance'] = tolerance
00295         mytmp['addantenna'] = addantenna
00296         mytmp['applyflags'] = applyflags
00297         mytmp['savecmds'] = savecmds
00298         mytmp['outfile'] = outfile
00299         mytmp['flagbackup'] = flagbackup
00300         pathname='file:///'+os.environ.get('CASAPATH').split()[0]+'/share/xml/'
00301         trec = casac.utils().torecord(pathname+'importevla.xml')
00302 
00303         casalog.origin('importevla')
00304         if not trec.has_key('importevla') or not casac.utils().verify(mytmp, trec['importevla']) :
00305             return False
00306 
00307 
00308         try :
00309           casalog.post('')
00310           casalog.post('##########################################')
00311           casalog.post('##### Begin Task: importevla           #####')
00312           casalog.post('')
00313           result = importevla(asdm, vis, ocorr_mode, compression, asis, scans, verbose, overwrite, online, tbuff, flagzero, flagpol, shadow, tolerance, addantenna, applyflags, savecmds, outfile, flagbackup)
00314           casalog.post('')
00315           casalog.post('##### End Task: importevla           #####')
00316           casalog.post('##########################################')
00317 
00318 
00319 # saveinputs for individule engine has no use
00320 # saveinputs should alos be removed from casa_in_py.py
00321 #
00322 #
00323 #          saveinputs = myf['saveinputs']
00324 #          saveinputs('importevla', 'importevla.last', myparams)
00325 #
00326 #
00327         except Exception, instance:
00328           #print '**** Error **** ',instance
00329           pass
00330 
00331         gc.collect()
00332         return result
00333 #
00334 #
00335 ##
00336 #    def paramgui(self, useGlobals=True):
00337 #        """
00338 #        Opens a parameter GUI for this task.  If useGlobals is true, then any relevant global parameter settings are used.
00339 #        """
00340 #        import paramgui
00341 #
00342 #        a=inspect.stack()
00343 #        stacklevel=0
00344 #        for k in range(len(a)):
00345 #          if (string.find(a[k][1], 'ipython console') > 0) or (string.find(a[k][1], '<string>') >= 0):
00346 #            stacklevel=k
00347 #            break
00348 #        myf = sys._getframe(stacklevel).f_globals
00349 #
00350 #        if useGlobals:
00351 #            paramgui.setGlobals(myf)
00352 #        else:
00353 #            paramgui.setGlobals({})
00354 #
00355 #        paramgui.runTask('importevla', myf['_ip'])
00356 #        paramgui.setGlobals({})
00357 #
00358 #
00359 #
00360 #
00361     def defaults(self, param=None):
00362         a=inspect.stack()
00363         stacklevel=0
00364         for k in range(len(a)):
00365           if (string.find(a[k][1], 'ipython console') > 0) or (string.find(a[k][1], '<string>') >= 0):
00366                 stacklevel=k
00367                 break
00368         myf=sys._getframe(stacklevel).f_globals
00369         a = odict()
00370         a['asdm']  = ''
00371         a['vis']  = ''
00372         a['ocorr_mode']  = 'co'
00373         a['compression']  = False
00374         a['asis']  = ''
00375         a['scans']  = ''
00376         a['verbose']  = False
00377         a['overwrite']  = False
00378         a['online']  = True
00379         a['flagzero']  = True
00380         a['shadow']  = True
00381         a['applyflags']  = False
00382         a['savecmds']  = False
00383         a['flagbackup']  = True
00384 
00385         a['async']=False
00386         a['online'] = {
00387                     0:odict([{'value':True}, {'tbuff':0.0}]), 
00388                     1:{'value':False}}
00389         a['flagzero'] = {
00390                     0:odict([{'value':True}, {'flagpol':True}]), 
00391                     1:{'value':False}}
00392         a['shadow'] = {
00393                     0:odict([{'value':True}, {'tolerance':0.0}, {'addantenna':''}]), 
00394                     1:{'value':False}}
00395         a['savecmds'] = {
00396                     0:{'value':False}, 
00397                     1:odict([{'value':True}, {'outfile':''}])}
00398 
00399 ### This function sets the default values but also will return the list of
00400 ### parameters or the default value of a given parameter
00401         if(param == None):
00402                 myf['__set_default_parameters'](a)
00403         elif(param == 'paramkeys'):
00404                 return a.keys()
00405         else:
00406                 if(a.has_key(param)):
00407                    #if(type(a[param]) == dict) :
00408                    #   return a[param][len(a[param])-1]['value']
00409                    #else :
00410                       return a[param]
00411 
00412 
00413 #
00414 #
00415     def check_params(self, param=None, value=None):
00416       a=inspect.stack() 
00417       stacklevel=0
00418       for k in range(len(a)):
00419         if (string.find(a[k][1], 'ipython console') > 0) or (string.find(a[k][1], '<string>') >= 0):
00420             stacklevel=k
00421             break
00422       myf=sys._getframe(stacklevel).f_globals
00423 
00424 #      print 'param:', param, 'value:', value
00425       try :
00426          if str(type(value)) != "<type 'instance'>" :
00427             value0 = value
00428             value = myf['cu'].expandparam(param, value)
00429             matchtype = False
00430             if(type(value) == numpy.ndarray):
00431                if(type(value) == type(value0)):
00432                   myf[param] = value.tolist()
00433                else:
00434                   #print 'value:', value, 'value0:', value0
00435                   #print 'type(value):', type(value), 'type(value0):', type(value0)
00436                   myf[param] = value0
00437                   if type(value0) != list :
00438                      matchtype = True
00439             else :
00440                myf[param] = value
00441             value = myf['cu'].verifyparam({param:value})
00442             if matchtype:
00443                value = False
00444       except Exception, instance:
00445          #ignore the exception and just return it unchecked
00446          myf[param] = value
00447       return value
00448 
00449 #
00450 #
00451     def description(self, key='importevla', subkey=None):
00452         desc={'importevla': 'Convert an Science Data Model observation into a CASA Measurement Set',
00453                'asdm': 'Name of input asdm directory (on disk)',
00454                'vis': 'Root name of the ms to be created. Note the .ms is NOT added ',
00455                'ocorr_mode': 'Fill correlation mode AUTO_ONLY (ao), CROSS_ONLY (co) or CROSS_AND_AUTO (ca)',
00456                'compression': 'Flag for turning on data compression',
00457                'asis': 'Create verbatim copies of these SDM tables in the MS.',
00458                'scans': 'List of scans to fill (default is all scans).',
00459                'verbose': 'Output lots of information while the filler is working',
00460                'overwrite': 'Over write an existing MS',
00461                'online': 'Create online flags',
00462                'tbuff': 'Time padding buffer (in seconds)',
00463                'flagzero': 'Create flag commands for zero points',
00464                'flagpol': 'Create flag commands for cross-hand correlations',
00465                'shadow': 'Create flag commands for shadowed data',
00466                'tolerance': 'Amount of shadow allowed (in meters)',
00467                'addantenna': 'File name or dictionary with additional antenna names, positions and diameters',
00468                'applyflags': 'Apply flag commands to MS',
00469                'savecmds': 'Save flag commands to an ASCII file',
00470                'outfile': 'Name of ASCII file to save flag commands',
00471                'flagbackup': 'Back up flag column before applying flags',
00472 
00473                'async': 'If true the taskname must be started using importevla(...)'
00474               }
00475 
00476 #
00477 # Set subfields defaults if needed
00478 #
00479 
00480         if(desc.has_key(key)) :
00481            return desc[key]
00482 
00483     def itsdefault(self, paramname) :
00484         a = {}
00485         a['asdm']  = ''
00486         a['vis']  = ''
00487         a['ocorr_mode']  = 'co'
00488         a['compression']  = False
00489         a['asis']  = ''
00490         a['scans']  = ''
00491         a['verbose']  = False
00492         a['overwrite']  = False
00493         a['online']  = True
00494         a['tbuff']  = 0.0
00495         a['flagzero']  = True
00496         a['flagpol']  = True
00497         a['shadow']  = True
00498         a['tolerance']  = 0.0
00499         a['addantenna']  = ''
00500         a['applyflags']  = False
00501         a['savecmds']  = False
00502         a['outfile']  = ''
00503         a['flagbackup']  = True
00504 
00505         if a.has_key(paramname) :
00506               return a[paramname]
00507 importevla_pg = importevla_pg_()