casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
importvla_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_importvla import importvla
00018 class importvla_cli_:
00019     __name__ = "importvla"
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__ = (importvla_cli_,)
00028        self.__doc__ = self.__call__.__doc__
00029 
00030        self.parameters={'archivefiles':None, 'vis':None, 'bandname':None, 'frequencytol':None, 'project':None, 'starttime':None, 'stoptime':None, 'applytsys':None, 'autocorr':None, 'antnamescheme':None, 'keepblanks':None, 'evlabands':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, archivefiles=None, vis=None, bandname=None, frequencytol=None, project=None, starttime=None, stoptime=None, applytsys=None, autocorr=None, antnamescheme=None, keepblanks=None, evlabands=None,  async=None):
00047 
00048         """Import VLA archive file(s) to a measurement set
00049 
00050       Imports an arbitrary number of VLA archive-format data sets into
00051       a casa measurement set.  If more than one band is present, they
00052       will be put in the same measurement set but in a separate spectral
00053       window.  The task will handle old style and new style VLA (after
00054       July 2007) archive data and apply the tsys to the data and to
00055       the weights.
00056 
00057       Keyword arguments:
00058       archivefiles -- Name of input VLA archive file(s)
00059               default: none.  Must be supplied
00060               example: archivefiles = 'AP314_A959519.xp1'
00061               example: archivefiles=['AP314_A950519.xp1','AP314_A950519.xp2']
00062       vis -- Name of output visibility file
00063               default: none.  Must be supplied.
00064               example: vis='NGC7538.ms'
00065               Will not over-write existing ms of same name.
00066               A backup flag-file version 'Original' will be made in
00067                 vis.flagversions.  See help flagmanager
00068       bandname -- VLA Frequency band
00069               default: => '' = all bands
00070               example: bandname='K'
00071               Options: '4'=48-96 MHz,'P'=298-345 MHz,'L'=1.15-1.75 GHz,
00072               'C'=4.2-5.1 GHz,'X'=6.8-9.6 GHz,'U'=13.5-16.3 GHz,
00073               'K'=20.8-25.8 GHz,'Q'=38-51 GHz
00074       frequencytol -- Tolerance in frequency shift in making spectral windows
00075               default: => 150000 (Hz).  For Doppler shifted data, <10000 Hz may
00076               may produce too many unnecessary spectral windows.
00077               example: frequencytol = 1500000.0 (units = Hz)
00078       project -- Project name to import from archive files:
00079               default: '' => all projects in file
00080               example: project='AL519'
00081               project = 'al519' or AL519 will work.  Do not include
00082               leading zeros; project = 'AL0519' will not work.
00083       starttime -- Time after which data will be considered for importing
00084               default: '' => all:  Date must be included.
00085               syntax: starttime = '2003/1/31/05:05:23'
00086       stoptime --  Time before which data will be considered for importing
00087               default: '' => all:  Date must be included.
00088               syntax: stoptime = '2003/1/31/08:05:23'
00089       applytsys -- Apply data scaling and weight scaling by nominal
00090               sensitivity (~Tsys)
00091               default: True.  Strongly recommended
00092       autocorr --  import autocorrelations to ms
00093               default:  => False (no autocorrelations)
00094       antnamescheme -- 'old' or 'new' antenna names.
00095               default => 'new' gives antnenna names
00096                 'VA04' or 'EA13 for VLA telescopse 04 and 13 (EVLA)
00097                 'old' gives names '04' or '13'
00098       keepblanks -- Should sources with blank names be filled into the data base
00099               default => false.  Do not fill
00100               These scans are tipping scans (as of June 1, 2009) and should not
00101               be filled in the visibility data set.
00102       evlabands -- Use the EVLA's center frequency and bandwidths for frequencies
00103               specified via wavelength or band. 
00104               default => True. 
00105       async --  Run asynchronously
00106               default = False; do not run asychronously
00107       
00108         """
00109         if not hasattr(self, "__globals__") or self.__globals__ == None :
00110            self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals
00111         #casac = self.__globals__['casac']
00112         casalog = self.__globals__['casalog']
00113         #casalog = casac.casac.logsink()
00114         self.__globals__['__last_task'] = 'importvla'
00115         self.__globals__['taskname'] = 'importvla'
00116         ###
00117         self.__globals__['update_params'](func=self.__globals__['taskname'],printtext=False,ipython_globals=self.__globals__)
00118         ###
00119         ###
00120         #Handle globals or user over-ride of arguments
00121         #
00122         function_signature_defaults=dict(zip(self.__call__.func_code.co_varnames,self.__call__.func_defaults))
00123         useLocalDefaults = False
00124 
00125         for item in function_signature_defaults.iteritems():
00126                 key,val = item
00127                 keyVal = eval(key)
00128                 if (keyVal == None):
00129                         #user hasn't set it - use global/default
00130                         pass
00131                 else:
00132                         #user has set it - use over-ride
00133                         if (key != 'self') :
00134                            useLocalDefaults = True
00135 
00136         myparams = {}
00137         if useLocalDefaults :
00138            for item in function_signature_defaults.iteritems():
00139                key,val = item
00140                keyVal = eval(key)
00141                exec('myparams[key] = keyVal')
00142                self.parameters[key] = keyVal
00143                if (keyVal == None):
00144                    exec('myparams[key] = '+ key + ' = self.itsdefault(key)')
00145                    keyVal = eval(key)
00146                    if(type(keyVal) == dict) :
00147                       if len(keyVal) > 0 :
00148                          exec('myparams[key] = ' + key + ' = keyVal[len(keyVal)-1][\'value\']')
00149                       else :
00150                          exec('myparams[key] = ' + key + ' = {}')
00151 
00152         else :
00153             async = self.parameters['async']
00154             myparams['archivefiles'] = archivefiles = self.parameters['archivefiles']
00155             myparams['vis'] = vis = self.parameters['vis']
00156             myparams['bandname'] = bandname = self.parameters['bandname']
00157             myparams['frequencytol'] = frequencytol = self.parameters['frequencytol']
00158             myparams['project'] = project = self.parameters['project']
00159             myparams['starttime'] = starttime = self.parameters['starttime']
00160             myparams['stoptime'] = stoptime = self.parameters['stoptime']
00161             myparams['applytsys'] = applytsys = self.parameters['applytsys']
00162             myparams['autocorr'] = autocorr = self.parameters['autocorr']
00163             myparams['antnamescheme'] = antnamescheme = self.parameters['antnamescheme']
00164             myparams['keepblanks'] = keepblanks = self.parameters['keepblanks']
00165             myparams['evlabands'] = evlabands = self.parameters['evlabands']
00166 
00167         if type(archivefiles)==str: archivefiles=[archivefiles]
00168 
00169         result = None
00170 
00171 #
00172 #    The following is work around to avoid a bug with current python translation
00173 #
00174         mytmp = {}
00175 
00176         mytmp['archivefiles'] = archivefiles
00177         mytmp['vis'] = vis
00178         mytmp['bandname'] = bandname
00179         if type(frequencytol) == str :
00180            mytmp['frequencytol'] = casac.casac.qa.quantity(frequencytol)
00181         else :
00182            mytmp['frequencytol'] = frequencytol
00183         mytmp['project'] = project
00184         mytmp['starttime'] = starttime
00185         mytmp['stoptime'] = stoptime
00186         mytmp['applytsys'] = applytsys
00187         mytmp['autocorr'] = autocorr
00188         mytmp['antnamescheme'] = antnamescheme
00189         mytmp['keepblanks'] = keepblanks
00190         mytmp['evlabands'] = evlabands
00191         pathname='file:///'+os.environ.get('CASAPATH').split()[0]+'/share/xml/'
00192         trec = casac.casac.utils().torecord(pathname+'importvla.xml')
00193 
00194         casalog.origin('importvla')
00195         try :
00196           #if not trec.has_key('importvla') or not casac.casac.utils().verify(mytmp, trec['importvla']) :
00197             #return False
00198 
00199           casac.casac.utils().verify(mytmp, trec['importvla'], True)
00200           scriptstr=['']
00201           saveinputs = self.__globals__['saveinputs']
00202           saveinputs('importvla', 'importvla.last', myparams, self.__globals__,scriptstr=scriptstr)
00203           if async :
00204             count = 0
00205             keybase =  time.strftime("%y%m%d.%H%M%S")
00206             key = keybase + "_" + str(count)
00207             while self.__async__.has_key(key) :
00208                count += 1
00209                key = keybase + "_" + str(count)
00210             result = tm.execute('importvla', archivefiles, vis, bandname, frequencytol, project, starttime, stoptime, applytsys, autocorr, antnamescheme, keepblanks, evlabands)
00211             print "Use: "
00212             print "      tm.retrieve(return_value) # to retrieve the status"
00213             print 
00214             self.rkey = key
00215             self.__async__[key] = result
00216           else :
00217               tname = 'importvla'
00218               spaces = ' '*(18-len(tname))
00219               casalog.post('\n##########################################'+
00220                            '\n##### Begin Task: ' + tname + spaces + ' #####')
00221               casalog.post(scriptstr[1][1:]+'\n', 'INFO')
00222               result = importvla(archivefiles, vis, bandname, frequencytol, project, starttime, stoptime, applytsys, autocorr, antnamescheme, keepblanks, evlabands)
00223               casalog.post('##### End Task: ' + tname + '  ' + spaces + ' #####'+
00224                            '\n##########################################')
00225 
00226         except Exception, instance:
00227           if(self.__globals__.has_key('__rethrow_casa_exceptions') and self.__globals__['__rethrow_casa_exceptions']) :
00228              raise
00229           else :
00230              #print '**** Error **** ',instance
00231              tname = 'importvla'
00232              casalog.post('An error occurred running task '+tname+'.', 'ERROR')
00233              pass
00234 
00235         gc.collect()
00236         return result
00237 #
00238 #
00239 #
00240     def paramgui(self, useGlobals=True, ipython_globals=None):
00241         """
00242         Opens a parameter GUI for this task.  If useGlobals is true, then any relevant global parameter settings are used.
00243         """
00244         import paramgui
00245         if not hasattr(self, "__globals__") or self.__globals__ == None :
00246            self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals
00247 
00248         if useGlobals:
00249             if ipython_globals == None:
00250                 myf=self.__globals__
00251             else:
00252                 myf=ipython_globals
00253 
00254             paramgui.setGlobals(myf)
00255         else:
00256             paramgui.setGlobals({})
00257 
00258         paramgui.runTask('importvla', myf['_ip'])
00259         paramgui.setGlobals({})
00260 
00261 #
00262 #
00263 #
00264     def defaults(self, param=None, ipython_globals=None, paramvalue=None, subparam=None):
00265         if not hasattr(self, "__globals__") or self.__globals__ == None :
00266            self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals
00267         if ipython_globals == None:
00268             myf=self.__globals__
00269         else:
00270             myf=ipython_globals
00271 
00272         a = odict()
00273         a['archivefiles']  = ['']
00274         a['vis']  = ''
00275         a['bandname']  = ''
00276         a['frequencytol']  = '150000.0Hz'
00277         a['project']  = ''
00278         a['starttime']  = ''
00279         a['stoptime']  = ''
00280         a['applytsys']  = True
00281         a['autocorr']  = False
00282         a['antnamescheme']  = 'new'
00283         a['keepblanks']  = False
00284         a['evlabands']  = False
00285 
00286         a['async']=False
00287 
00288 ### This function sets the default values but also will return the list of
00289 ### parameters or the default value of a given parameter
00290         if(param == None):
00291                 myf['__set_default_parameters'](a)
00292         elif(param == 'paramkeys'):
00293                 return a.keys()
00294         else:
00295             if(paramvalue==None and subparam==None):
00296                if(a.has_key(param)):
00297                   return a[param]
00298                else:
00299                   return self.itsdefault(param)
00300             else:
00301                retval=a[param]
00302                if(type(a[param])==dict):
00303                   for k in range(len(a[param])):
00304                      valornotval='value'
00305                      if(a[param][k].has_key('notvalue')):
00306                         valornotval='notvalue'
00307                      if((a[param][k][valornotval])==paramvalue):
00308                         retval=a[param][k].copy()
00309                         retval.pop(valornotval)
00310                         if(subparam != None):
00311                            if(retval.has_key(subparam)):
00312                               retval=retval[subparam]
00313                            else:
00314                               retval=self.itsdefault(subparam)
00315                      else:
00316                         retval=self.itsdefault(subparam)
00317                return retval
00318 
00319 
00320 #
00321 #
00322     def check_params(self, param=None, value=None, ipython_globals=None):
00323       if ipython_globals == None:
00324           myf=self.__globals__
00325       else:
00326           myf=ipython_globals
00327 #      print 'param:', param, 'value:', value
00328       try :
00329          if str(type(value)) != "<type 'instance'>" :
00330             value0 = value
00331             value = myf['cu'].expandparam(param, value)
00332             matchtype = False
00333             if(type(value) == numpy.ndarray):
00334                if(type(value) == type(value0)):
00335                   myf[param] = value.tolist()
00336                else:
00337                   #print 'value:', value, 'value0:', value0
00338                   #print 'type(value):', type(value), 'type(value0):', type(value0)
00339                   myf[param] = value0
00340                   if type(value0) != list :
00341                      matchtype = True
00342             else :
00343                myf[param] = value
00344             value = myf['cu'].verifyparam({param:value})
00345             if matchtype:
00346                value = False
00347       except Exception, instance:
00348          #ignore the exception and just return it unchecked
00349          myf[param] = value
00350       return value
00351 #
00352 #
00353     def description(self, key='importvla', subkey=None):
00354         desc={'importvla': 'Import VLA archive file(s) to a measurement set',
00355                'archivefiles': 'Name of input VLA archive file(s)',
00356                'vis': 'Name of output visibility file',
00357                'bandname': 'VLA frequency band name:\'\'=>obtain all bands in the archive file',
00358                'frequencytol': 'Frequency shift to define a unique spectra window (Hz)',
00359                'project': 'Project name: \'\' => all projects in files',
00360                'starttime': 'start time to search for data',
00361                'stoptime': 'end time to search for data',
00362                'applytsys': 'apply nominal sensistivity scaling to data and weights',
00363                'autocorr': 'import autocorrelations to ms, if set to True',
00364                'antnamescheme': '\'old\' or \'new\'; \'VA04\' or \'04\' for VLA ant 4',
00365                'keepblanks': 'Fill scans with blank (empty) source names (e.g. tipping scans)',
00366                'evlabands': 'Use updated eVLA frequencies and bandwidths for bands and wavelengths',
00367 
00368                'async': 'If true the taskname must be started using importvla(...)'
00369               }
00370 
00371         if(desc.has_key(key)) :
00372            return desc[key]
00373 
00374     def itsdefault(self, paramname) :
00375         a = {}
00376         a['archivefiles']  = ['']
00377         a['vis']  = ''
00378         a['bandname']  = ''
00379         a['frequencytol']  = '150000.0Hz'
00380         a['project']  = ''
00381         a['starttime']  = ''
00382         a['stoptime']  = ''
00383         a['applytsys']  = True
00384         a['autocorr']  = False
00385         a['antnamescheme']  = 'new'
00386         a['keepblanks']  = False
00387         a['evlabands']  = False
00388 
00389         #a = sys._getframe(len(inspect.stack())-1).f_globals
00390 
00391         if a.has_key(paramname) :
00392               return a[paramname]
00393 importvla_cli = importvla_cli_()