casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
sdcoadd_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_sdcoadd import sdcoadd
00018 class sdcoadd_cli_:
00019     __name__ = "sdcoadd"
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__ = (sdcoadd_cli_,)
00028        self.__doc__ = self.__call__.__doc__
00029 
00030        self.parameters={'infiles':None, 'antenna':None, 'fluxunit':None, 'telescopeparm':None, 'specunit':None, 'frame':None, 'doppler':None, 'scanaverage':None, 'timeaverage':None, 'tweight':None, 'polaverage':None, 'pweight':None, 'outfile':None, 'outform':None, 'overwrite':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, infiles=None, antenna=None, fluxunit=None, telescopeparm=None, specunit=None, frame=None, doppler=None, scanaverage=None, timeaverage=None, tweight=None, polaverage=None, pweight=None, outfile=None, outform=None, overwrite=None,  async=None):
00047 
00048         """ASAP SD task: coadd multiple scantables into one
00049   Keyword arguments:
00050         infiles -- list of names of input SD dataset
00051         antenna -- antenna name or id (only effective for MS input). 
00052         fluxunit -- units for line flux
00053                 options: 'K','Jy',''
00054                 default: '' (keep current fluxunit of the first data in the infiles)
00055 
00056             >>> fluxunit expandable parameter
00057                  telescopeparm -- the telescope characteristics
00058                          options: (str) name or (list) list of gain info
00059                          default: '' (none set)
00060                          example: if telescopeparm='', it tries to get the telescope
00061                                   name from the data.
00062                                   Full antenna parameters (diameter,ap.eff.) known
00063                                   to ASAP are
00064                                   'ATPKSMB', 'ATPKSHOH', 'ATMOPRA', 'DSS-43',
00065                                   'CEDUNA','HOBART'. For GBT, it fixes default fluxunit
00066                                   to 'K' first then convert to a new fluxunit.
00067                                   telescopeparm=[104.9,0.43] diameter(m), ap.eff.
00068                                   telescopeparm=[0.743] gain in Jy/K
00069                                   telescopeparm='FIX' to change default fluxunit
00070                                   see description below
00071 
00072         specunit -- units for spectral axis
00073                 options: (str) 'channel','km/s','GHz','MHz','kHz','Hz'
00074                 default: '' (=current)
00075                 example: this will be the units for masklist
00076         frame -- frequency frame for spectral axis
00077                 options: (str) 'LSRK','REST','TOPO','LSRD','BARY',
00078                          'GEO','GALACTO','LGROUP','CMB'
00079                 default: currently set frame in scantable
00080                 WARNING: frame='REST' not yet implemented
00081         doppler -- doppler mode
00082                 options: (str) 'RADIO','OPTICAL','Z','BETA','GAMMA'
00083                 default: currently set doppler in scantable
00084         scanaverage -- average integrations within scans
00085                 options: (bool) True,False
00086                 default: False
00087                 example: if True, this happens in read-in
00088                 For GBT, set False!
00089         timeaverage -- average times for multiple scan cycles
00090                 options: (bool) True,False
00091                 default: False
00092                 example: if True, this happens after calibration
00093 
00094             >>>timeaverage expandable parameter
00095                  tweight -- weighting for time average
00096                          options: 'none' 
00097                                   'var'   (1/var(spec) weighted)
00098                                   'tsys'  (1/Tsys**2 weighted)
00099                                   'tint'  (integration time weighted)
00100                                   'tintsys'  (Tint/Tsys**2)
00101                                   'median'  ( median averaging)
00102                          default: 'none'
00103 
00104         polaverage -- average polarizations
00105                 options: (bool) True,False
00106                 default: False
00107 
00108             >>>polaverage expandable parameter
00109                  pweight -- weighting for polarization average
00110                          options: 'none'
00111                                   'var'  (1/var(spec) weighted)
00112                                   'tsys' (1/Tsys**2 weighted)
00113                          default: 'none'
00114 
00115         outfile -- Name of output file
00116                 default: '' (scantable)
00117                 example:
00118         outform -- format of output file
00119                 options: 'ASCII','SDFITS','MS','ASAP'
00120                 default: 'ASAP'
00121                 example: the ASAP format is easiest for further sd
00122                          processing; use MS for CASA imaging.
00123                          If ASCII, then will append some stuff to
00124                          the outfile name
00125         overwrite -- overwrite the output file if already exists
00126                 options: (bool) True,False
00127                 default: False
00128                 WARNING: if outform='ASCII', this parameter is ignored 
00129 
00130 
00131 
00132         DESCRIPTION:
00133 
00134         Task sdcoadd performs co-add multiple single dish spectral data given by
00135         a list of spectral data file names in any of the following formats,
00136         ASAP, MS2,SDFITS, and ASCII.
00137         The units of line flux, the units of spectral axis, frame, and doppler
00138         are assumed to be those of the first one in the infiles if not
00139         specified.
00140         timaverage and polaverage are used to perform time
00141         and polarization averaging over scans on the co-added scantable before
00142         saving to a file on disk.
00143 
00144         WARNING for the GBT raw SDFITS format data as input:
00145         SDtasks are able to handle GBT raw SDFITS format data since the 
00146         data filler is available. However, the functionality is not well 
00147         tested yet, so that there may be unknown bugs.  
00148 
00149   
00150         """
00151         if not hasattr(self, "__globals__") or self.__globals__ == None :
00152            self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals
00153         #casac = self.__globals__['casac']
00154         casalog = self.__globals__['casalog']
00155         #casalog = casac.casac.logsink()
00156         self.__globals__['__last_task'] = 'sdcoadd'
00157         self.__globals__['taskname'] = 'sdcoadd'
00158         ###
00159         self.__globals__['update_params'](func=self.__globals__['taskname'],printtext=False,ipython_globals=self.__globals__)
00160         ###
00161         ###
00162         #Handle globals or user over-ride of arguments
00163         #
00164         function_signature_defaults=dict(zip(self.__call__.func_code.co_varnames,self.__call__.func_defaults))
00165         useLocalDefaults = False
00166 
00167         for item in function_signature_defaults.iteritems():
00168                 key,val = item
00169                 keyVal = eval(key)
00170                 if (keyVal == None):
00171                         #user hasn't set it - use global/default
00172                         pass
00173                 else:
00174                         #user has set it - use over-ride
00175                         if (key != 'self') :
00176                            useLocalDefaults = True
00177 
00178         myparams = {}
00179         if useLocalDefaults :
00180            for item in function_signature_defaults.iteritems():
00181                key,val = item
00182                keyVal = eval(key)
00183                exec('myparams[key] = keyVal')
00184                self.parameters[key] = keyVal
00185                if (keyVal == None):
00186                    exec('myparams[key] = '+ key + ' = self.itsdefault(key)')
00187                    keyVal = eval(key)
00188                    if(type(keyVal) == dict) :
00189                       if len(keyVal) > 0 :
00190                          exec('myparams[key] = ' + key + ' = keyVal[len(keyVal)-1][\'value\']')
00191                       else :
00192                          exec('myparams[key] = ' + key + ' = {}')
00193 
00194         else :
00195             async = self.parameters['async']
00196             myparams['infiles'] = infiles = self.parameters['infiles']
00197             myparams['antenna'] = antenna = self.parameters['antenna']
00198             myparams['fluxunit'] = fluxunit = self.parameters['fluxunit']
00199             myparams['telescopeparm'] = telescopeparm = self.parameters['telescopeparm']
00200             myparams['specunit'] = specunit = self.parameters['specunit']
00201             myparams['frame'] = frame = self.parameters['frame']
00202             myparams['doppler'] = doppler = self.parameters['doppler']
00203             myparams['scanaverage'] = scanaverage = self.parameters['scanaverage']
00204             myparams['timeaverage'] = timeaverage = self.parameters['timeaverage']
00205             myparams['tweight'] = tweight = self.parameters['tweight']
00206             myparams['polaverage'] = polaverage = self.parameters['polaverage']
00207             myparams['pweight'] = pweight = self.parameters['pweight']
00208             myparams['outfile'] = outfile = self.parameters['outfile']
00209             myparams['outform'] = outform = self.parameters['outform']
00210             myparams['overwrite'] = overwrite = self.parameters['overwrite']
00211 
00212         if type(infiles)==str: infiles=[infiles]
00213 
00214         result = None
00215 
00216 #
00217 #    The following is work around to avoid a bug with current python translation
00218 #
00219         mytmp = {}
00220 
00221         mytmp['infiles'] = infiles
00222         mytmp['antenna'] = antenna
00223         mytmp['fluxunit'] = fluxunit
00224         mytmp['telescopeparm'] = telescopeparm
00225         mytmp['specunit'] = specunit
00226         mytmp['frame'] = frame
00227         mytmp['doppler'] = doppler
00228         mytmp['scanaverage'] = scanaverage
00229         mytmp['timeaverage'] = timeaverage
00230         mytmp['tweight'] = tweight
00231         mytmp['polaverage'] = polaverage
00232         mytmp['pweight'] = pweight
00233         mytmp['outfile'] = outfile
00234         mytmp['outform'] = outform
00235         mytmp['overwrite'] = overwrite
00236         pathname='file:///'+os.environ.get('CASAPATH').split()[0]+'/share/xml/'
00237         trec = casac.casac.utils().torecord(pathname+'sdcoadd.xml')
00238 
00239         casalog.origin('sdcoadd')
00240         try :
00241           #if not trec.has_key('sdcoadd') or not casac.casac.utils().verify(mytmp, trec['sdcoadd']) :
00242             #return False
00243 
00244           casac.casac.utils().verify(mytmp, trec['sdcoadd'], True)
00245           scriptstr=['']
00246           saveinputs = self.__globals__['saveinputs']
00247           saveinputs('sdcoadd', 'sdcoadd.last', myparams, self.__globals__,scriptstr=scriptstr)
00248           if async :
00249             count = 0
00250             keybase =  time.strftime("%y%m%d.%H%M%S")
00251             key = keybase + "_" + str(count)
00252             while self.__async__.has_key(key) :
00253                count += 1
00254                key = keybase + "_" + str(count)
00255             result = tm.execute('sdcoadd', infiles, antenna, fluxunit, telescopeparm, specunit, frame, doppler, scanaverage, timeaverage, tweight, polaverage, pweight, outfile, outform, overwrite)
00256             print "Use: "
00257             print "      tm.retrieve(return_value) # to retrieve the status"
00258             print 
00259             self.rkey = key
00260             self.__async__[key] = result
00261           else :
00262               tname = 'sdcoadd'
00263               spaces = ' '*(18-len(tname))
00264               casalog.post('\n##########################################'+
00265                            '\n##### Begin Task: ' + tname + spaces + ' #####')
00266               casalog.post(scriptstr[1][1:]+'\n', 'INFO')
00267               result = sdcoadd(infiles, antenna, fluxunit, telescopeparm, specunit, frame, doppler, scanaverage, timeaverage, tweight, polaverage, pweight, outfile, outform, overwrite)
00268               casalog.post('##### End Task: ' + tname + '  ' + spaces + ' #####'+
00269                            '\n##########################################')
00270 
00271         except Exception, instance:
00272           if(self.__globals__.has_key('__rethrow_casa_exceptions') and self.__globals__['__rethrow_casa_exceptions']) :
00273              raise
00274           else :
00275              #print '**** Error **** ',instance
00276              tname = 'sdcoadd'
00277              casalog.post('An error occurred running task '+tname+'.', 'ERROR')
00278              pass
00279 
00280         gc.collect()
00281         return result
00282 #
00283 #
00284 #
00285     def paramgui(self, useGlobals=True, ipython_globals=None):
00286         """
00287         Opens a parameter GUI for this task.  If useGlobals is true, then any relevant global parameter settings are used.
00288         """
00289         import paramgui
00290         if not hasattr(self, "__globals__") or self.__globals__ == None :
00291            self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals
00292 
00293         if useGlobals:
00294             if ipython_globals == None:
00295                 myf=self.__globals__
00296             else:
00297                 myf=ipython_globals
00298 
00299             paramgui.setGlobals(myf)
00300         else:
00301             paramgui.setGlobals({})
00302 
00303         paramgui.runTask('sdcoadd', myf['_ip'])
00304         paramgui.setGlobals({})
00305 
00306 #
00307 #
00308 #
00309     def defaults(self, param=None, ipython_globals=None, paramvalue=None, subparam=None):
00310         if not hasattr(self, "__globals__") or self.__globals__ == None :
00311            self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals
00312         if ipython_globals == None:
00313             myf=self.__globals__
00314         else:
00315             myf=ipython_globals
00316 
00317         a = odict()
00318         a['infiles']  = ['']
00319         a['antenna']  = 0
00320         a['fluxunit']  = ''
00321         a['specunit']  = ''
00322         a['frame']  = ''
00323         a['doppler']  = ''
00324         a['scanaverage']  = False
00325         a['timeaverage']  = False
00326         a['polaverage']  = False
00327         a['outfile']  = ''
00328         a['outform']  = 'ASAP'
00329         a['overwrite']  = False
00330 
00331         a['async']=False
00332         a['fluxunit'] = {
00333                     0:{'value':''}, 
00334                     1:odict([{'value':'K'}, {'telescopeparm':''}]), 
00335                     2:odict([{'value':'k'}, {'telescopeparm':''}]), 
00336                     3:odict([{'value':'Jy'}, {'telescopeparm':''}]), 
00337                     4:odict([{'value':'jy'}, {'telescopeparm':''}])}
00338         a['timeaverage'] = {
00339                     0:{'value':False}, 
00340                     1:odict([{'value':True}, {'tweight':'none'}])}
00341         a['polaverage'] = {
00342                     0:{'value':False}, 
00343                     1:odict([{'value':True}, {'pweight':'none'}])}
00344 
00345 ### This function sets the default values but also will return the list of
00346 ### parameters or the default value of a given parameter
00347         if(param == None):
00348                 myf['__set_default_parameters'](a)
00349         elif(param == 'paramkeys'):
00350                 return a.keys()
00351         else:
00352             if(paramvalue==None and subparam==None):
00353                if(a.has_key(param)):
00354                   return a[param]
00355                else:
00356                   return self.itsdefault(param)
00357             else:
00358                retval=a[param]
00359                if(type(a[param])==dict):
00360                   for k in range(len(a[param])):
00361                      valornotval='value'
00362                      if(a[param][k].has_key('notvalue')):
00363                         valornotval='notvalue'
00364                      if((a[param][k][valornotval])==paramvalue):
00365                         retval=a[param][k].copy()
00366                         retval.pop(valornotval)
00367                         if(subparam != None):
00368                            if(retval.has_key(subparam)):
00369                               retval=retval[subparam]
00370                            else:
00371                               retval=self.itsdefault(subparam)
00372                      else:
00373                         retval=self.itsdefault(subparam)
00374                return retval
00375 
00376 
00377 #
00378 #
00379     def check_params(self, param=None, value=None, ipython_globals=None):
00380       if ipython_globals == None:
00381           myf=self.__globals__
00382       else:
00383           myf=ipython_globals
00384 #      print 'param:', param, 'value:', value
00385       try :
00386          if str(type(value)) != "<type 'instance'>" :
00387             value0 = value
00388             value = myf['cu'].expandparam(param, value)
00389             matchtype = False
00390             if(type(value) == numpy.ndarray):
00391                if(type(value) == type(value0)):
00392                   myf[param] = value.tolist()
00393                else:
00394                   #print 'value:', value, 'value0:', value0
00395                   #print 'type(value):', type(value), 'type(value0):', type(value0)
00396                   myf[param] = value0
00397                   if type(value0) != list :
00398                      matchtype = True
00399             else :
00400                myf[param] = value
00401             value = myf['cu'].verifyparam({param:value})
00402             if matchtype:
00403                value = False
00404       except Exception, instance:
00405          #ignore the exception and just return it unchecked
00406          myf[param] = value
00407       return value
00408 #
00409 #
00410     def description(self, key='sdcoadd', subkey=None):
00411         desc={'sdcoadd': 'ASAP SD task: coadd multiple scantables into one',
00412                'infiles': 'list of names of input SD dataset',
00413                'antenna': 'antenna name or id (only effective for MS input)',
00414                'fluxunit': 'units for line flux (K,Jy) (''=current)',
00415                'telescopeparm': 'param of telescope for flux conversion',
00416                'specunit': 'units for spectral axis (channel,km/s,GHz)',
00417                'frame': 'frequency reference frame, e.g. LSRK (''=current)',
00418                'doppler': 'doppler convention, e.g. RADIO (''=current)',
00419                'scanaverage': 'average integs within scans (True,False) ',
00420                'timeaverage': 'average scans over time (True,False)',
00421                'tweight': 'weighting for time averaging',
00422                'polaverage': 'average over polarizations (True,False)',
00423                'pweight': 'weighting for polarization averaging',
00424                'outfile': 'output file name',
00425                'outform': 'output file format (ASCII,MS,SDFITS,ASAP)',
00426                'overwrite': 'overwrite the output file if already exists',
00427 
00428                'async': 'If true the taskname must be started using sdcoadd(...)'
00429               }
00430 
00431 #
00432 # Set subfields defaults if needed
00433 #
00434 
00435         if(desc.has_key(key)) :
00436            return desc[key]
00437 
00438     def itsdefault(self, paramname) :
00439         a = {}
00440         a['infiles']  = ['']
00441         a['antenna']  = 0
00442         a['fluxunit']  = ''
00443         a['telescopeparm']  = ''
00444         a['specunit']  = ''
00445         a['frame']  = ''
00446         a['doppler']  = ''
00447         a['scanaverage']  = False
00448         a['timeaverage']  = False
00449         a['tweight']  = 'none'
00450         a['polaverage']  = False
00451         a['pweight']  = 'none'
00452         a['outfile']  = ''
00453         a['outform']  = 'ASAP'
00454         a['overwrite']  = False
00455 
00456         #a = sys._getframe(len(inspect.stack())-1).f_globals
00457 
00458         if self.parameters['fluxunit']  == 'K':
00459             a['telescopeparm'] = ''
00460 
00461         if self.parameters['fluxunit']  == 'k':
00462             a['telescopeparm'] = ''
00463 
00464         if self.parameters['fluxunit']  == 'Jy':
00465             a['telescopeparm'] = ''
00466 
00467         if self.parameters['fluxunit']  == 'jy':
00468             a['telescopeparm'] = ''
00469 
00470         if self.parameters['timeaverage']  == True:
00471             a['tweight'] = 'none'
00472 
00473         if self.parameters['polaverage']  == True:
00474             a['pweight'] = 'none'
00475 
00476         if a.has_key(paramname) :
00477               return a[paramname]
00478 sdcoadd_cli = sdcoadd_cli_()