casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
sdimprocess_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_sdimprocess import sdimprocess
00018 class sdimprocess_cli_:
00019     __name__ = "sdimprocess"
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__ = (sdimprocess_cli_,)
00028        self.__doc__ = self.__call__.__doc__
00029 
00030        self.parameters={'infiles':None, 'mode':None, 'numpoly':None, 'beamsize':None, 'smoothsize':None, 'direction':None, 'masklist':None, 'tmax':None, 'tmin':None, 'outfile':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, mode=None, numpoly=None, beamsize=None, smoothsize=None, direction=None, masklist=None, tmax=None, tmin=None, outfile=None, overwrite=None,  async=None):
00047 
00048         """SD task: task for single-dish image processing
00049   Keyword arguments:
00050         infiles -- name of input SD (FITS or CASA) image
00051         mode -- processing mode
00052                 default: 'basket'
00053                 options: 'basket', 'press'
00054 
00055             >>>mode expandable parameter
00056                  direction -- scan direction in unit of degree
00057                      default: []
00058                      example: [0.0,90.0]
00059                  masklist -- mask width for Basket-Weaving on percentage
00060                      default: 1.0 (1.0% of map size)
00061                  numpoly -- order of polynomial fit in Presssed-out
00062                      default: 2
00063                  beamsize -- beam size 
00064                      default: 0.0
00065                      example: 10.0 (interpreted as '10arcsec'), '1arcmin'
00066                  smoothsize -- smoothing beam in Pressed-out
00067                      default: 2.0 (interpreted as 2.0 * beamsize)
00068                      example: '1arcmin' (set smoothsize directly)
00069 
00070         tmax -- maximum value used for process
00071                 default: 0.0 (no threshold in maximum)
00072                 example: 10.0 (mask data larger value than 10.0)
00073         tmin -- minimum value used for process
00074                 default: 0.0 (no threshold in minimum)
00075                 example: -10.0 (mask data smaller value than -10.0)
00076         outfile -- output CASA image name
00077                 default: '' (use default name)
00078                 example: 'output.im'
00079         overwrite -- overwrite option for outfile
00080                 default: False (not overwrite)
00081                 options: True, False
00082                 example: if True, existing file will be overwritten
00083 
00084 
00085         DESCRIPTION:
00086 
00087         Task sdimprocess is used to remove a scanning noise that appears 
00088         as a striped noise pattern along the scan direction in a raster 
00089         scan data. 
00090 
00091         By default, the scanning noise is removed by using the 
00092         'Basket-Weaving' method (Emerson & Grave 1988) that requires 
00093         multiple images that observed exactly the same area with different 
00094         scanning direction. If only one image is available, the 'Pressed-out' 
00095         method (Sofue & Reich 1979) can be used to remove the scanning 
00096         effect.
00097 
00098         For 'Basket-Weaving', scanning directions must have at least two 
00099         different values. Normally, the scanning direction should be 
00100         specified for each input image. Otherwise, specified scanning 
00101         directions will be used iteratively. The masklist is a width of 
00102         masking region in the Fourier plane. It is specified as a fraction 
00103         (percentage) of the image size. 
00104 
00105         For 'Pressed-out', the scanning direction must be unique. There are 
00106         two ways to specify a size of smoothing beam used for process. One 
00107         is to specify smoothing size directly. To do this, smoothsize should 
00108         be specified as string that consists of a numerical value and an unit 
00109         (e.g. '10.0arcsec'). A value of beamsize will be ignored in this case. 
00110         Another way to specify smoothing size is to set an observed beam size 
00111         and indicate smoothing size as a scale factor of the observed beam 
00112         size. In this case, the beamsize is interpreted as the observed beam 
00113         size, and the smoothsize is the scale factor. If the beamsize is 
00114         provided as float value, its unit is assumed to 'arcsec'. It is also 
00115         possible to set the beamsize as string consisting of the numerical 
00116         value and the unit. The smoothsize must be float value.
00117 
00118         The infiles only allows an image data (CASA or FITS), and not does
00119         not work with MS or Scantable. The direction is an angle with respect 
00120         to the horizontal direction, and its unit is degree. Any value may be 
00121         interpreted properly, but the value ranging from 0.0 to 180.0 will be 
00122         secure. The tmax and the tmin is used to specify a threshold that 
00123         defines a range of spectral values used for processing. The data point 
00124         that has the value larger than tmax or smaller than tmin will be 
00125         excluded from the processing. The default (0.0) is no threshold. 
00126         The outfile specifies an output CASA image name. If the outfile is 
00127         empty, the default name ('sdimprocess.out.im') will be used. 
00128   
00129         """
00130         if not hasattr(self, "__globals__") or self.__globals__ == None :
00131            self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals
00132         #casac = self.__globals__['casac']
00133         casalog = self.__globals__['casalog']
00134         #casalog = casac.casac.logsink()
00135         self.__globals__['__last_task'] = 'sdimprocess'
00136         self.__globals__['taskname'] = 'sdimprocess'
00137         ###
00138         self.__globals__['update_params'](func=self.__globals__['taskname'],printtext=False,ipython_globals=self.__globals__)
00139         ###
00140         ###
00141         #Handle globals or user over-ride of arguments
00142         #
00143         function_signature_defaults=dict(zip(self.__call__.func_code.co_varnames,self.__call__.func_defaults))
00144         useLocalDefaults = False
00145 
00146         for item in function_signature_defaults.iteritems():
00147                 key,val = item
00148                 keyVal = eval(key)
00149                 if (keyVal == None):
00150                         #user hasn't set it - use global/default
00151                         pass
00152                 else:
00153                         #user has set it - use over-ride
00154                         if (key != 'self') :
00155                            useLocalDefaults = True
00156 
00157         myparams = {}
00158         if useLocalDefaults :
00159            for item in function_signature_defaults.iteritems():
00160                key,val = item
00161                keyVal = eval(key)
00162                exec('myparams[key] = keyVal')
00163                self.parameters[key] = keyVal
00164                if (keyVal == None):
00165                    exec('myparams[key] = '+ key + ' = self.itsdefault(key)')
00166                    keyVal = eval(key)
00167                    if(type(keyVal) == dict) :
00168                       if len(keyVal) > 0 :
00169                          exec('myparams[key] = ' + key + ' = keyVal[len(keyVal)-1][\'value\']')
00170                       else :
00171                          exec('myparams[key] = ' + key + ' = {}')
00172 
00173         else :
00174             async = self.parameters['async']
00175             myparams['infiles'] = infiles = self.parameters['infiles']
00176             myparams['mode'] = mode = self.parameters['mode']
00177             myparams['numpoly'] = numpoly = self.parameters['numpoly']
00178             myparams['beamsize'] = beamsize = self.parameters['beamsize']
00179             myparams['smoothsize'] = smoothsize = self.parameters['smoothsize']
00180             myparams['direction'] = direction = self.parameters['direction']
00181             myparams['masklist'] = masklist = self.parameters['masklist']
00182             myparams['tmax'] = tmax = self.parameters['tmax']
00183             myparams['tmin'] = tmin = self.parameters['tmin']
00184             myparams['outfile'] = outfile = self.parameters['outfile']
00185             myparams['overwrite'] = overwrite = self.parameters['overwrite']
00186 
00187 
00188         result = None
00189 
00190 #
00191 #    The following is work around to avoid a bug with current python translation
00192 #
00193         mytmp = {}
00194 
00195         mytmp['infiles'] = infiles
00196         mytmp['mode'] = mode
00197         mytmp['numpoly'] = numpoly
00198         mytmp['beamsize'] = beamsize
00199         mytmp['smoothsize'] = smoothsize
00200         mytmp['direction'] = direction
00201         mytmp['masklist'] = masklist
00202         mytmp['tmax'] = tmax
00203         mytmp['tmin'] = tmin
00204         mytmp['outfile'] = outfile
00205         mytmp['overwrite'] = overwrite
00206         pathname='file:///'+os.environ.get('CASAPATH').split()[0]+'/share/xml/'
00207         trec = casac.casac.utils().torecord(pathname+'sdimprocess.xml')
00208 
00209         casalog.origin('sdimprocess')
00210         try :
00211           #if not trec.has_key('sdimprocess') or not casac.casac.utils().verify(mytmp, trec['sdimprocess']) :
00212             #return False
00213 
00214           casac.casac.utils().verify(mytmp, trec['sdimprocess'], True)
00215           scriptstr=['']
00216           saveinputs = self.__globals__['saveinputs']
00217           saveinputs('sdimprocess', 'sdimprocess.last', myparams, self.__globals__,scriptstr=scriptstr)
00218           if async :
00219             count = 0
00220             keybase =  time.strftime("%y%m%d.%H%M%S")
00221             key = keybase + "_" + str(count)
00222             while self.__async__.has_key(key) :
00223                count += 1
00224                key = keybase + "_" + str(count)
00225             result = tm.execute('sdimprocess', infiles, mode, numpoly, beamsize, smoothsize, direction, masklist, tmax, tmin, outfile, overwrite)
00226             print "Use: "
00227             print "      tm.retrieve(return_value) # to retrieve the status"
00228             print 
00229             self.rkey = key
00230             self.__async__[key] = result
00231           else :
00232               tname = 'sdimprocess'
00233               spaces = ' '*(18-len(tname))
00234               casalog.post('\n##########################################'+
00235                            '\n##### Begin Task: ' + tname + spaces + ' #####')
00236               casalog.post(scriptstr[1][1:]+'\n', 'INFO')
00237               result = sdimprocess(infiles, mode, numpoly, beamsize, smoothsize, direction, masklist, tmax, tmin, outfile, overwrite)
00238               casalog.post('##### End Task: ' + tname + '  ' + spaces + ' #####'+
00239                            '\n##########################################')
00240 
00241         except Exception, instance:
00242           if(self.__globals__.has_key('__rethrow_casa_exceptions') and self.__globals__['__rethrow_casa_exceptions']) :
00243              raise
00244           else :
00245              #print '**** Error **** ',instance
00246              tname = 'sdimprocess'
00247              casalog.post('An error occurred running task '+tname+'.', 'ERROR')
00248              pass
00249 
00250         gc.collect()
00251         return result
00252 #
00253 #
00254 #
00255     def paramgui(self, useGlobals=True, ipython_globals=None):
00256         """
00257         Opens a parameter GUI for this task.  If useGlobals is true, then any relevant global parameter settings are used.
00258         """
00259         import paramgui
00260         if not hasattr(self, "__globals__") or self.__globals__ == None :
00261            self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals
00262 
00263         if useGlobals:
00264             if ipython_globals == None:
00265                 myf=self.__globals__
00266             else:
00267                 myf=ipython_globals
00268 
00269             paramgui.setGlobals(myf)
00270         else:
00271             paramgui.setGlobals({})
00272 
00273         paramgui.runTask('sdimprocess', myf['_ip'])
00274         paramgui.setGlobals({})
00275 
00276 #
00277 #
00278 #
00279     def defaults(self, param=None, ipython_globals=None, paramvalue=None, subparam=None):
00280         if not hasattr(self, "__globals__") or self.__globals__ == None :
00281            self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals
00282         if ipython_globals == None:
00283             myf=self.__globals__
00284         else:
00285             myf=ipython_globals
00286 
00287         a = odict()
00288         a['infiles']  = ['']
00289         a['mode']  = ''
00290         a['tmax']  = 0.0
00291         a['tmin']  = 0.0
00292         a['outfile']  = ''
00293         a['overwrite']  = False
00294 
00295         a['async']=False
00296         a['mode'] = {
00297                     0:odict([{'value':'basket'}, {'direction':[]}, {'masklist':1.0}]), 
00298                     1:odict([{'value':'press'}, {'numpoly':2}, {'beamsize':0.0}, {'smoothsize':2.0}, {'direction':0.0}])}
00299 
00300 ### This function sets the default values but also will return the list of
00301 ### parameters or the default value of a given parameter
00302         if(param == None):
00303                 myf['__set_default_parameters'](a)
00304         elif(param == 'paramkeys'):
00305                 return a.keys()
00306         else:
00307             if(paramvalue==None and subparam==None):
00308                if(a.has_key(param)):
00309                   return a[param]
00310                else:
00311                   return self.itsdefault(param)
00312             else:
00313                retval=a[param]
00314                if(type(a[param])==dict):
00315                   for k in range(len(a[param])):
00316                      valornotval='value'
00317                      if(a[param][k].has_key('notvalue')):
00318                         valornotval='notvalue'
00319                      if((a[param][k][valornotval])==paramvalue):
00320                         retval=a[param][k].copy()
00321                         retval.pop(valornotval)
00322                         if(subparam != None):
00323                            if(retval.has_key(subparam)):
00324                               retval=retval[subparam]
00325                            else:
00326                               retval=self.itsdefault(subparam)
00327                      else:
00328                         retval=self.itsdefault(subparam)
00329                return retval
00330 
00331 
00332 #
00333 #
00334     def check_params(self, param=None, value=None, ipython_globals=None):
00335       if ipython_globals == None:
00336           myf=self.__globals__
00337       else:
00338           myf=ipython_globals
00339 #      print 'param:', param, 'value:', value
00340       try :
00341          if str(type(value)) != "<type 'instance'>" :
00342             value0 = value
00343             value = myf['cu'].expandparam(param, value)
00344             matchtype = False
00345             if(type(value) == numpy.ndarray):
00346                if(type(value) == type(value0)):
00347                   myf[param] = value.tolist()
00348                else:
00349                   #print 'value:', value, 'value0:', value0
00350                   #print 'type(value):', type(value), 'type(value0):', type(value0)
00351                   myf[param] = value0
00352                   if type(value0) != list :
00353                      matchtype = True
00354             else :
00355                myf[param] = value
00356             value = myf['cu'].verifyparam({param:value})
00357             if matchtype:
00358                value = False
00359       except Exception, instance:
00360          #ignore the exception and just return it unchecked
00361          myf[param] = value
00362       return value
00363 #
00364 #
00365     def description(self, key='sdimprocess', subkey=None):
00366         desc={'sdimprocess': 'SD task: task for single-dish image processing',
00367                'infiles': 'name of input SD image (FITS or CASA image)',
00368                'mode': 'processing mode ("press" or "basket")',
00369                'numpoly': 'order of polynomial fit',
00370                'beamsize': 'beam size',
00371                'smoothsize': 'size of smoothing beam',
00372                'direction': 'scan direction in unit of degree',
00373                'masklist': 'mask width (on percentage)',
00374                'tmax': 'maximum value used for process',
00375                'tmin': 'minimum value used for process',
00376                'outfile': 'output CASA image name',
00377                'overwrite': 'overwrite option for outfile',
00378 
00379                'async': 'If true the taskname must be started using sdimprocess(...)'
00380               }
00381 
00382 #
00383 # Set subfields defaults if needed
00384 #
00385 
00386         if(desc.has_key(key)) :
00387            return desc[key]
00388 
00389     def itsdefault(self, paramname) :
00390         a = {}
00391         a['infiles']  = ['']
00392         a['mode']  = ''
00393         a['numpoly']  = 2
00394         a['beamsize']  = 0.0
00395         a['smoothsize']  = 2.0
00396         a['direction']  = []
00397         a['masklist']  = 1.0
00398         a['tmax']  = 0.0
00399         a['tmin']  = 0.0
00400         a['outfile']  = ''
00401         a['overwrite']  = False
00402 
00403         #a = sys._getframe(len(inspect.stack())-1).f_globals
00404 
00405         if self.parameters['mode']  == 'basket':
00406             a['direction'] = []
00407             a['masklist'] = 1.0
00408 
00409         if self.parameters['mode']  == 'press':
00410             a['numpoly'] = 2
00411             a['beamsize'] = 0.0
00412             a['smoothsize'] = 2.0
00413             a['direction'] = 0.0
00414 
00415         if a.has_key(paramname) :
00416               return a[paramname]
00417 sdimprocess_cli = sdimprocess_cli_()