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