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