casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
sdsave_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_sdsave import sdsave
00018 class sdsave_cli_:
00019     __name__ = "sdsave"
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__ = (sdsave_cli_,)
00028        self.__doc__ = self.__call__.__doc__
00029 
00030        self.parameters={'infile':None, 'antenna':None, 'getpt':None, 'rowlist':None, 'scanlist':None, 'field':None, 'iflist':None, 'pollist':None, 'scanaverage':None, 'timeaverage':None, 'tweight':None, 'polaverage':None, 'pweight':None, 'restfreq':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, infile=None, antenna=None, getpt=None, rowlist=None, scanlist=None, field=None, iflist=None, pollist=None, scanaverage=None, timeaverage=None, tweight=None, polaverage=None, pweight=None, restfreq=None, outfile=None, outform=None, overwrite=None,  async=None):
00047 
00048         """ASAP SD task: save the sd spectra in various format
00049   Keyword arguments:
00050         infile -- name of input SD dataset
00051         antenna -- antenna name or id (only effective for MS input). 
00052         getpt -- fill DIRECTION column properly, or reuse POINTING table 
00053                  in original MS (only effective for MS input)
00054                  default: True
00055                  options: True (fill DIRECTION column properly)
00056                           False (reuse POINTING table in original MS)
00057         rowlist -- list of row numbers to process
00058                 default: [] (use all rows)
00059                 example: [0,2,4,6]
00060                 For expert users only!
00061                 this selection is applied first, and then followed by
00062                 the selection with scans, fields, ifs, and polarizations. 
00063         scanlist -- list of to process
00064                 default: [] (use all scans)
00065                 example: [21,22,23,24]
00066                 this selection is in addition to field, iflist, and pollist
00067         field -- selection string for selecting scans by name
00068                 default: '' (no name selection)
00069                 example: 'FLS3a*'
00070                 this selection is in addition to scanlist, iflist, and pollist
00071         iflist -- list of IF id numbers to select
00072                 default: [] (use all IFs)
00073                 example: [15]
00074                 this selection is in addition to scanlist, field, and pollist
00075         pollist -- list of polarization id numbers to select
00076                 default: [] (use all polarizations)
00077                 example: [1]
00078                 this selection is in addition to scanlist, field, and iflist
00079         scanaverage --  average integrations within scans
00080                 options: (bool) True,False
00081                 default: False
00082                 example: if True, average integrations before it is saved
00083         timeaverage -- average times for multiple scan cycles
00084                 options: (bool) True,False
00085                 default: False
00086                 >>>timeaverage expandable parameter
00087                  tweight -- weighting for time average
00088                          options: 'none'
00089                                   'var'   (1/var(spec) weighted)
00090                                   'tsys'  (1/Tsys**2 weighted)
00091                                   'tint'  (integration time weighted)
00092                                   'tintsys'  (Tint/Tsys**2)
00093                                   'median'  ( median averaging)
00094                          default: 'none'
00095 
00096         polaverage -- average polarizations
00097                 options: (bool) True,False
00098                 default: False
00099                 >>>polaverage expandable parameter
00100                  pweight -- weighting for polarization average
00101                          options: 'none'
00102                                   'var'  (1/var(spec) weighted)
00103                                   'tsys' (1/Tsys**2 weighted)
00104         restfreq -- rest frequencies of output data
00105                 Available types are a number, string, a list of numbers or
00106                 strings (see examples below), and list of dictionaries. 
00107                 The default unit of restfreq is Hz, if not specified.
00108                 A list can be used to set different rest frequencies to
00109                 each IF. the length of list input must be nIF. Dictionary 
00110                 input should be a pair of molecule name and frequency with 
00111                 keys of 'name' and 'value', respectively. The 'value's in the 
00112                 dictionary input follows the same manner as for single 
00113                 float or string input. 
00114                 default: '' (use current setting)
00115                 example: 4.6e10 (float value in Hz),
00116                          '46GHz' (string with unit),
00117                          ['345.8GHz', '347.0GHz', 356.7e9] (for each IF)
00118                          [{'name':'CO','value':345e9}] (a value with name)
00119         outfile -- name of output dataset
00120                 default: '' 
00121         outform -- output data format
00122                 default: 'ASAP'
00123                 Options: 'ASAP', 'MS2', 'SDFITS', 'ASCII'
00124         overwrite -- overwrite the output file if already exists
00125                 options: (bool) True,False
00126                 default: False
00127                 WARNING: if outform='ASCII', this parameter is ignored
00128 
00129 
00130         DESCRIPTION:
00131 
00132         Task sdsave writes the single dish data to a disk file in 
00133         specified format (ASAP, MS2, SDFITS, ASCII). It is possible to 
00134         save the subset of the data by selecting row numbers, scan numbers, 
00135         IF ids, polarization ids and field names. The ASAP (scantable) format 
00136         is recommended for further analysis using Sd tool. For further 
00137         imaging using imager, save the data to the Measurement Set (MS2).
00138 
00139         Note that setting getpt=False needs a lot of attention.
00140         If you set getpt=False, the task retrieves pointing direction from 
00141         MS's FIELD table, which might not be correct for single dish 
00142         observation, instead to check MS's POINTING table, which is the 
00143         default behavior of the task (getpt=True). To compensate this, 
00144         absolute path to MS's POINTING table is stored, and it will be used 
00145         for POINTING table when the data is converted back to MS format. 
00146         In general, getpt=False is faster especially for large data. However, 
00147         MS created from Scantable cannot have correct POINTING table if 
00148         original MS's POINTING table doesn't exist. Such situation will 
00149         happen when original MS is removed or renamed, or imported Scantable 
00150         is moved to other computer alone.
00151 
00152         WARNING for the GBT raw SDFITS format data as input:
00153         SDtasks are able to handle GBT raw SDFITS format data since the 
00154         data filler is available. However, the functionality is not well 
00155         tested yet, so that there may be unknown bugs.  
00156 
00157   
00158         """
00159         if not hasattr(self, "__globals__") or self.__globals__ == None :
00160            self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals
00161         #casac = self.__globals__['casac']
00162         casalog = self.__globals__['casalog']
00163         #casalog = casac.casac.logsink()
00164         self.__globals__['__last_task'] = 'sdsave'
00165         self.__globals__['taskname'] = 'sdsave'
00166         ###
00167         self.__globals__['update_params'](func=self.__globals__['taskname'],printtext=False,ipython_globals=self.__globals__)
00168         ###
00169         ###
00170         #Handle globals or user over-ride of arguments
00171         #
00172         function_signature_defaults=dict(zip(self.__call__.func_code.co_varnames,self.__call__.func_defaults))
00173         useLocalDefaults = False
00174 
00175         for item in function_signature_defaults.iteritems():
00176                 key,val = item
00177                 keyVal = eval(key)
00178                 if (keyVal == None):
00179                         #user hasn't set it - use global/default
00180                         pass
00181                 else:
00182                         #user has set it - use over-ride
00183                         if (key != 'self') :
00184                            useLocalDefaults = True
00185 
00186         myparams = {}
00187         if useLocalDefaults :
00188            for item in function_signature_defaults.iteritems():
00189                key,val = item
00190                keyVal = eval(key)
00191                exec('myparams[key] = keyVal')
00192                self.parameters[key] = keyVal
00193                if (keyVal == None):
00194                    exec('myparams[key] = '+ key + ' = self.itsdefault(key)')
00195                    keyVal = eval(key)
00196                    if(type(keyVal) == dict) :
00197                       if len(keyVal) > 0 :
00198                          exec('myparams[key] = ' + key + ' = keyVal[len(keyVal)-1][\'value\']')
00199                       else :
00200                          exec('myparams[key] = ' + key + ' = {}')
00201 
00202         else :
00203             async = self.parameters['async']
00204             myparams['infile'] = infile = self.parameters['infile']
00205             myparams['antenna'] = antenna = self.parameters['antenna']
00206             myparams['getpt'] = getpt = self.parameters['getpt']
00207             myparams['rowlist'] = rowlist = self.parameters['rowlist']
00208             myparams['scanlist'] = scanlist = self.parameters['scanlist']
00209             myparams['field'] = field = self.parameters['field']
00210             myparams['iflist'] = iflist = self.parameters['iflist']
00211             myparams['pollist'] = pollist = self.parameters['pollist']
00212             myparams['scanaverage'] = scanaverage = self.parameters['scanaverage']
00213             myparams['timeaverage'] = timeaverage = self.parameters['timeaverage']
00214             myparams['tweight'] = tweight = self.parameters['tweight']
00215             myparams['polaverage'] = polaverage = self.parameters['polaverage']
00216             myparams['pweight'] = pweight = self.parameters['pweight']
00217             myparams['restfreq'] = restfreq = self.parameters['restfreq']
00218             myparams['outfile'] = outfile = self.parameters['outfile']
00219             myparams['outform'] = outform = self.parameters['outform']
00220             myparams['overwrite'] = overwrite = self.parameters['overwrite']
00221 
00222         if type(rowlist)==int: rowlist=[rowlist]
00223         if type(scanlist)==int: scanlist=[scanlist]
00224         if type(iflist)==int: iflist=[iflist]
00225         if type(pollist)==int: pollist=[pollist]
00226 
00227         result = None
00228 
00229 #
00230 #    The following is work around to avoid a bug with current python translation
00231 #
00232         mytmp = {}
00233 
00234         mytmp['infile'] = infile
00235         mytmp['antenna'] = antenna
00236         mytmp['getpt'] = getpt
00237         mytmp['rowlist'] = rowlist
00238         mytmp['scanlist'] = scanlist
00239         mytmp['field'] = field
00240         mytmp['iflist'] = iflist
00241         mytmp['pollist'] = pollist
00242         mytmp['scanaverage'] = scanaverage
00243         mytmp['timeaverage'] = timeaverage
00244         mytmp['tweight'] = tweight
00245         mytmp['polaverage'] = polaverage
00246         mytmp['pweight'] = pweight
00247         mytmp['restfreq'] = restfreq
00248         mytmp['outfile'] = outfile
00249         mytmp['outform'] = outform
00250         mytmp['overwrite'] = overwrite
00251         pathname='file:///'+os.environ.get('CASAPATH').split()[0]+'/share/xml/'
00252         trec = casac.casac.utils().torecord(pathname+'sdsave.xml')
00253 
00254         casalog.origin('sdsave')
00255         try :
00256           #if not trec.has_key('sdsave') or not casac.casac.utils().verify(mytmp, trec['sdsave']) :
00257             #return False
00258 
00259           casac.casac.utils().verify(mytmp, trec['sdsave'], True)
00260           scriptstr=['']
00261           saveinputs = self.__globals__['saveinputs']
00262           saveinputs('sdsave', 'sdsave.last', myparams, self.__globals__,scriptstr=scriptstr)
00263           if async :
00264             count = 0
00265             keybase =  time.strftime("%y%m%d.%H%M%S")
00266             key = keybase + "_" + str(count)
00267             while self.__async__.has_key(key) :
00268                count += 1
00269                key = keybase + "_" + str(count)
00270             result = tm.execute('sdsave', infile, antenna, getpt, rowlist, scanlist, field, iflist, pollist, scanaverage, timeaverage, tweight, polaverage, pweight, restfreq, outfile, outform, overwrite)
00271             print "Use: "
00272             print "      tm.retrieve(return_value) # to retrieve the status"
00273             print 
00274             self.rkey = key
00275             self.__async__[key] = result
00276           else :
00277               tname = 'sdsave'
00278               spaces = ' '*(18-len(tname))
00279               casalog.post('\n##########################################'+
00280                            '\n##### Begin Task: ' + tname + spaces + ' #####')
00281               casalog.post(scriptstr[1][1:]+'\n', 'INFO')
00282               result = sdsave(infile, antenna, getpt, rowlist, scanlist, field, iflist, pollist, scanaverage, timeaverage, tweight, polaverage, pweight, restfreq, outfile, outform, overwrite)
00283               casalog.post('##### End Task: ' + tname + '  ' + spaces + ' #####'+
00284                            '\n##########################################')
00285 
00286         except Exception, instance:
00287           if(self.__globals__.has_key('__rethrow_casa_exceptions') and self.__globals__['__rethrow_casa_exceptions']) :
00288              raise
00289           else :
00290              #print '**** Error **** ',instance
00291              tname = 'sdsave'
00292              casalog.post('An error occurred running task '+tname+'.', 'ERROR')
00293              pass
00294 
00295         gc.collect()
00296         return result
00297 #
00298 #
00299 #
00300     def paramgui(self, useGlobals=True, ipython_globals=None):
00301         """
00302         Opens a parameter GUI for this task.  If useGlobals is true, then any relevant global parameter settings are used.
00303         """
00304         import paramgui
00305         if not hasattr(self, "__globals__") or self.__globals__ == None :
00306            self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals
00307 
00308         if useGlobals:
00309             if ipython_globals == None:
00310                 myf=self.__globals__
00311             else:
00312                 myf=ipython_globals
00313 
00314             paramgui.setGlobals(myf)
00315         else:
00316             paramgui.setGlobals({})
00317 
00318         paramgui.runTask('sdsave', myf['_ip'])
00319         paramgui.setGlobals({})
00320 
00321 #
00322 #
00323 #
00324     def defaults(self, param=None, ipython_globals=None, paramvalue=None, subparam=None):
00325         if not hasattr(self, "__globals__") or self.__globals__ == None :
00326            self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals
00327         if ipython_globals == None:
00328             myf=self.__globals__
00329         else:
00330             myf=ipython_globals
00331 
00332         a = odict()
00333         a['infile']  = ''
00334         a['antenna']  = 0
00335         a['getpt']  = True
00336         a['rowlist']  = []
00337         a['scanlist']  = []
00338         a['field']  = ''
00339         a['iflist']  = []
00340         a['pollist']  = []
00341         a['scanaverage']  = False
00342         a['timeaverage']  = False
00343         a['polaverage']  = False
00344         a['restfreq']  = ''
00345         a['outfile']  = ''
00346         a['outform']  = 'ASAP'
00347         a['overwrite']  = False
00348 
00349         a['async']=False
00350         a['timeaverage'] = {
00351                     0:{'value':False}, 
00352                     1:odict([{'value':True}, {'tweight':'none'}])}
00353         a['polaverage'] = {
00354                     0:{'value':False}, 
00355                     1:odict([{'value':True}, {'pweight':'none'}])}
00356 
00357 ### This function sets the default values but also will return the list of
00358 ### parameters or the default value of a given parameter
00359         if(param == None):
00360                 myf['__set_default_parameters'](a)
00361         elif(param == 'paramkeys'):
00362                 return a.keys()
00363         else:
00364             if(paramvalue==None and subparam==None):
00365                if(a.has_key(param)):
00366                   return a[param]
00367                else:
00368                   return self.itsdefault(param)
00369             else:
00370                retval=a[param]
00371                if(type(a[param])==dict):
00372                   for k in range(len(a[param])):
00373                      valornotval='value'
00374                      if(a[param][k].has_key('notvalue')):
00375                         valornotval='notvalue'
00376                      if((a[param][k][valornotval])==paramvalue):
00377                         retval=a[param][k].copy()
00378                         retval.pop(valornotval)
00379                         if(subparam != None):
00380                            if(retval.has_key(subparam)):
00381                               retval=retval[subparam]
00382                            else:
00383                               retval=self.itsdefault(subparam)
00384                      else:
00385                         retval=self.itsdefault(subparam)
00386                return retval
00387 
00388 
00389 #
00390 #
00391     def check_params(self, param=None, value=None, ipython_globals=None):
00392       if ipython_globals == None:
00393           myf=self.__globals__
00394       else:
00395           myf=ipython_globals
00396 #      print 'param:', param, 'value:', value
00397       try :
00398          if str(type(value)) != "<type 'instance'>" :
00399             value0 = value
00400             value = myf['cu'].expandparam(param, value)
00401             matchtype = False
00402             if(type(value) == numpy.ndarray):
00403                if(type(value) == type(value0)):
00404                   myf[param] = value.tolist()
00405                else:
00406                   #print 'value:', value, 'value0:', value0
00407                   #print 'type(value):', type(value), 'type(value0):', type(value0)
00408                   myf[param] = value0
00409                   if type(value0) != list :
00410                      matchtype = True
00411             else :
00412                myf[param] = value
00413             value = myf['cu'].verifyparam({param:value})
00414             if matchtype:
00415                value = False
00416       except Exception, instance:
00417          #ignore the exception and just return it unchecked
00418          myf[param] = value
00419       return value
00420 #
00421 #
00422     def description(self, key='sdsave', subkey=None):
00423         desc={'sdsave': 'ASAP SD task: save the sd spectra in various format',
00424                'infile': 'name of input SD dataset',
00425                'antenna': 'antenna name or id (only effective for MS input)',
00426                'getpt': 'fill DIRECTION column properly, or reuse POINTING table in original MS (only effective for MS input)',
00427                'rowlist': 'list of row numbers to process (e.g. [0,2,4,6])',
00428                'scanlist': 'list of scan to process (e.g. [20,21,22,23])',
00429                'field': 'string for selection by source name',
00430                'iflist': 'list of IF ids to select (e.g. [0,1])',
00431                'pollist': 'list of polarization ids to select (e.g. [0,1])',
00432                'scanaverage': 'average integs within scans (True,False) ',
00433                'timeaverage': 'average scans over time (True,False)',
00434                'tweight': 'weighting for time averaging',
00435                'polaverage': 'average over polarizations (True,False)',
00436                'pweight': '',
00437                'restfreq': 'rest frequency (default unit: Hz)',
00438                'outfile': 'output file name',
00439                'outform': 'output file format (ASCII,MS2,SDFITS,ASAP)',
00440                'overwrite': 'overwrite the output file if already exists',
00441 
00442                'async': 'If true the taskname must be started using sdsave(...)'
00443               }
00444 
00445 #
00446 # Set subfields defaults if needed
00447 #
00448 
00449         if(desc.has_key(key)) :
00450            return desc[key]
00451 
00452     def itsdefault(self, paramname) :
00453         a = {}
00454         a['infile']  = ''
00455         a['antenna']  = 0
00456         a['getpt']  = True
00457         a['rowlist']  = []
00458         a['scanlist']  = []
00459         a['field']  = ''
00460         a['iflist']  = []
00461         a['pollist']  = []
00462         a['scanaverage']  = False
00463         a['timeaverage']  = False
00464         a['tweight']  = 'none'
00465         a['polaverage']  = False
00466         a['pweight']  = 'none'
00467         a['restfreq']  = ''
00468         a['outfile']  = ''
00469         a['outform']  = 'ASAP'
00470         a['overwrite']  = False
00471 
00472         #a = sys._getframe(len(inspect.stack())-1).f_globals
00473 
00474         if self.parameters['timeaverage']  == True:
00475             a['tweight'] = 'none'
00476 
00477         if self.parameters['polaverage']  == True:
00478             a['pweight'] = 'none'
00479 
00480         if a.has_key(paramname) :
00481               return a[paramname]
00482 sdsave_cli = sdsave_cli_()