casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
sdtpimaging_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_sdtpimaging import sdtpimaging
00018 class sdtpimaging_cli_:
00019     __name__ = "sdtpimaging"
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__ = (sdtpimaging_cli_,)
00028        self.__doc__ = self.__call__.__doc__
00029 
00030        self.parameters={'infile':None, 'calmode':None, 'masklist':None, 'blpoly':None, 'backup':None, 'flaglist':None, 'antenna':None, 'spw':None, 'stokes':None, 'createimage':None, 'outfile':None, 'imsize':None, 'cell':None, 'phasecenter':None, 'ephemsrcname':None, 'pointingcolumn':None, 'gridfunction':None, 'plotlevel':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, calmode=None, masklist=None, blpoly=None, backup=None, flaglist=None, antenna=None, spw=None, stokes=None, createimage=None, outfile=None, imsize=None, cell=None, phasecenter=None, ephemsrcname=None, pointingcolumn=None, gridfunction=None, plotlevel=None,  async=None):
00047 
00048         """SD task: do a simple calibration and imaging for total power data 
00049   Keyword arguments:
00050         infile -- name of input SD (MS) dataset
00051         calmode -- calibration mode (currently only baseline subtraction)
00052                 options: 'baseline','none'
00053                 default: 'none'
00054                 example: choose mode 'none' if you have
00055                          already calibrated and want to do
00056                          plotting nd/or imaging 
00057             >>> calmode='baseline' expandable parameters
00058                masklist -- mask in numbers of rows from each edge of each scan 
00059                            to be included for baseline fitting
00060                  default: none
00061                  example: [30,30] or [30] 
00062                           used first 30 rows and last 30 rows of each scan for 
00063                           the baseline 
00064                blpoly -- polynomial order for the baseline fit
00065                  default: 1
00066                backup -- set True to create backup for input data
00067                  default: True
00068         flaglist -- list of scan numbers to flag (ranges can be accepted)  
00069                 default: [] (use all scans)
00070                 example: [[0,3],80]
00071                          flag the scan range [0,3] = [0,1,2,3] and scan 80 
00072         antenna -- select data based on antenna name(s) or id(s) in string
00073                 default: '' (use all antennas)
00074                 example: '0,1', 'DV01'
00075                 WARNING: currently baseline subtraction properly 
00076                          only one of the antennas.
00077         spw -- spectral window id
00078                 default: 0
00079                 example: 1
00080         stokes -- select data based on stokes or polarization type 
00081                 default: '' (use all polarizations)
00082                 example: 'XX'
00083         createimage -- do imaging? 
00084                 default: False 
00085             >>> createimage=True expandable parameters
00086                outfile -- output image name
00087                  default: none
00088                  example: 'mySDimage.im'
00089                imsize -- x and y image size in pixels, symmetric for single 
00090                          value
00091                  default: [256,256]
00092                  example: imsize=200 (equivalent to [200,200])
00093                cell -- x and y cell size. default unit arcmin
00094                  default: '1.0arcmin'
00095                  example: cell=['0.2arcmin, 0.2arcmin']
00096                           cell='0.2arcmin' (equivalent to example above)
00097                phasecenter -- image phase center: direction measure or fieldid 
00098                  default: 0
00099                  example: 'J2000 13h44m00 -17d02m00', 'AZEL -123d48m29 15d41m41'
00100                ephemsrcname -- ephemeris source name to proper shifting to 
00101                                center on the moving source for imaging
00102                  default: ''
00103                           if the source name in the data matches one of the 
00104                           known solar objects by the system, this task 
00105                           automatically set the source name. 
00106                  example: 'moon' 
00107                pointingcolumn -- pointing data column to use
00108                  option: 'direction', 'target', 'pointing_offset', 
00109                          'source_offset', 'encoder' 
00110                  default: 'direction'
00111                gridfunction -- gridding function for imaging
00112                  options: 'BOX' (Box-car), 'SF' (Spheroidal), 
00113                           'PB' (Primary-beam), 'GAUSS' (Gaussian),
00114                           'GJINC' (Gaussian*Jinc)
00115                  default: 'BOX'
00116                  example: 'SF'
00117         plotlevel -- control for plotting of results
00118                 options: (int) 0=none, 1=some, 2=more, <0=hardcopy
00119                 default: 0 (no plotting)
00120                 example: plotlevel<0 as abs(plotlevel), e.g.
00121                          -1: hardcopy plot 
00122                              (will be named <infile>_scans.eps)
00123                           1: plot raw data, calibrated data 
00124                              (for calmode='baseline)
00125                              plot raw or if exist calibrated data 
00126                              (for calmode='none')
00127                           2: plot raw data, progressively display baseline 
00128                              fitting for each scan, and final calibrated data 
00129                              (for calmode='baseline')  
00130 
00131 
00132         DESCRIPTION:
00133 
00134         Task sdtpimaging performs data selection, calibration, and imaging for 
00135         single-dish totalpower raster scan data.  This is a still experimental 
00136         task made to work mostly for the data taken at the ALMA Testing 
00137         Facility (ATF) or OSF. Currently, this task directly accesses the 
00138         Measurement Set data only because of the data access efficiency. So it 
00139         differs from other single-dish tasks that mostly operate on the ASAP 
00140         scantable data format.  By setting calmode='none', one can run 
00141         sdtpimaging to plot the data (raw or calibrated, if exists) and 
00142         further imaging by setting createimage=True. 
00143         
00144         The calibration available at this moment is just a simple baseline 
00145         subtraction for each scan. The fitted regions set by masklist are the 
00146         common for all the scans. Selection of the antennas can be made by 
00147         setting antennaid(s) or antenna name(s) in string (e.g. '0', '0,1', 
00148         'DV01',etc.). 
00149 
00150         For baseline subtraction, it currently works properly for a single 
00151         antenna selection. So a separate sdtpimaging task needs to be ran for 
00152         each antenna. It currently assumes that the data has a single spw(=0) 
00153         and fieldid(=0). By setting flaglist, one can set flag by scan numbers 
00154         to be excluded from imaging. (Note: 'scan numbers' are determined from 
00155         state id and related to SUB_SCAN column in STATE subtable and not to 
00156         SCAN_NUMBER in MS.) By default, baseline subtraction stage overwrites 
00157         (FLOAT_)DATA column of input data. You can keep original data by 
00158         setting backup parameter to True. In this case, the task make a copy 
00159         of input data specified by infile parameter. Name of backup file is 
00160         <infile>.sdtpimaging.bak.<timestamp>.  
00161 
00162         The selection of polarizations can be made by specifying the 
00163         polarization name in stokes, such as 'XX' or 'YY' for linear 
00164         polarizations. For example, with createimage=True, stokes='XXYY' will 
00165         produces an image cube with each plane contains the image of one of
00166         the polarizations while stokes='' or stokes='I' will produces a 'total 
00167         intensity' or Stokes I image. 
00168 
00169         Among the imaging sub-parameters, ephemsrcname is used to set the name 
00170         of a moving source such as planets to produce a stationary image (can 
00171         be omitted), and pointingcolumn is used to specify which pointing data 
00172         column to use for imaging. Convolution kernel for imaging can be 
00173         specified by using gridfunction. Available options are 'Box' (Box-car),
00174         'SF' (Spheroidal), 'PB' (Primary-beam), 'GAUSS' (Gaussian), and 
00175         'GJINC' (Gaussian*Jinc), where Jinc(x) = J_1(pi*x/c)/(pi*x/c) with a 
00176         first order Bessel function J_1. Sub-parameters for convolution 
00177         functions cannot be specified in this task. To costomize your 
00178         convolution function, please do imaging using sdimaging task or 
00179         imager tool.
00180   
00181         """
00182         if not hasattr(self, "__globals__") or self.__globals__ == None :
00183            self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals
00184         #casac = self.__globals__['casac']
00185         casalog = self.__globals__['casalog']
00186         #casalog = casac.casac.logsink()
00187         self.__globals__['__last_task'] = 'sdtpimaging'
00188         self.__globals__['taskname'] = 'sdtpimaging'
00189         ###
00190         self.__globals__['update_params'](func=self.__globals__['taskname'],printtext=False,ipython_globals=self.__globals__)
00191         ###
00192         ###
00193         #Handle globals or user over-ride of arguments
00194         #
00195         function_signature_defaults=dict(zip(self.__call__.func_code.co_varnames,self.__call__.func_defaults))
00196         useLocalDefaults = False
00197 
00198         for item in function_signature_defaults.iteritems():
00199                 key,val = item
00200                 keyVal = eval(key)
00201                 if (keyVal == None):
00202                         #user hasn't set it - use global/default
00203                         pass
00204                 else:
00205                         #user has set it - use over-ride
00206                         if (key != 'self') :
00207                            useLocalDefaults = True
00208 
00209         myparams = {}
00210         if useLocalDefaults :
00211            for item in function_signature_defaults.iteritems():
00212                key,val = item
00213                keyVal = eval(key)
00214                exec('myparams[key] = keyVal')
00215                self.parameters[key] = keyVal
00216                if (keyVal == None):
00217                    exec('myparams[key] = '+ key + ' = self.itsdefault(key)')
00218                    keyVal = eval(key)
00219                    if(type(keyVal) == dict) :
00220                       if len(keyVal) > 0 :
00221                          exec('myparams[key] = ' + key + ' = keyVal[len(keyVal)-1][\'value\']')
00222                       else :
00223                          exec('myparams[key] = ' + key + ' = {}')
00224 
00225         else :
00226             async = self.parameters['async']
00227             myparams['infile'] = infile = self.parameters['infile']
00228             myparams['calmode'] = calmode = self.parameters['calmode']
00229             myparams['masklist'] = masklist = self.parameters['masklist']
00230             myparams['blpoly'] = blpoly = self.parameters['blpoly']
00231             myparams['backup'] = backup = self.parameters['backup']
00232             myparams['flaglist'] = flaglist = self.parameters['flaglist']
00233             myparams['antenna'] = antenna = self.parameters['antenna']
00234             myparams['spw'] = spw = self.parameters['spw']
00235             myparams['stokes'] = stokes = self.parameters['stokes']
00236             myparams['createimage'] = createimage = self.parameters['createimage']
00237             myparams['outfile'] = outfile = self.parameters['outfile']
00238             myparams['imsize'] = imsize = self.parameters['imsize']
00239             myparams['cell'] = cell = self.parameters['cell']
00240             myparams['phasecenter'] = phasecenter = self.parameters['phasecenter']
00241             myparams['ephemsrcname'] = ephemsrcname = self.parameters['ephemsrcname']
00242             myparams['pointingcolumn'] = pointingcolumn = self.parameters['pointingcolumn']
00243             myparams['gridfunction'] = gridfunction = self.parameters['gridfunction']
00244             myparams['plotlevel'] = plotlevel = self.parameters['plotlevel']
00245 
00246         if type(masklist)==int: masklist=[masklist]
00247         if type(flaglist)==int: flaglist=[flaglist]
00248         if type(imsize)==int: imsize=[imsize]
00249         if type(cell)==float: cell=[cell]
00250 
00251         result = None
00252 
00253 #
00254 #    The following is work around to avoid a bug with current python translation
00255 #
00256         mytmp = {}
00257 
00258         mytmp['infile'] = infile
00259         mytmp['calmode'] = calmode
00260         mytmp['masklist'] = masklist
00261         mytmp['blpoly'] = blpoly
00262         mytmp['backup'] = backup
00263         mytmp['flaglist'] = flaglist
00264         mytmp['antenna'] = antenna
00265         mytmp['spw'] = spw
00266         mytmp['stokes'] = stokes
00267         mytmp['createimage'] = createimage
00268         mytmp['outfile'] = outfile
00269         mytmp['imsize'] = imsize
00270         if type(cell) == str :
00271            mytmp['cell'] = casac.casac.qa.quantity(cell)
00272         else :
00273            mytmp['cell'] = cell
00274         mytmp['phasecenter'] = phasecenter
00275         mytmp['ephemsrcname'] = ephemsrcname
00276         mytmp['pointingcolumn'] = pointingcolumn
00277         mytmp['gridfunction'] = gridfunction
00278         mytmp['plotlevel'] = plotlevel
00279         pathname='file:///'+os.environ.get('CASAPATH').split()[0]+'/share/xml/'
00280         trec = casac.casac.utils().torecord(pathname+'sdtpimaging.xml')
00281 
00282         casalog.origin('sdtpimaging')
00283         try :
00284           #if not trec.has_key('sdtpimaging') or not casac.casac.utils().verify(mytmp, trec['sdtpimaging']) :
00285             #return False
00286 
00287           casac.casac.utils().verify(mytmp, trec['sdtpimaging'], True)
00288           scriptstr=['']
00289           saveinputs = self.__globals__['saveinputs']
00290           saveinputs('sdtpimaging', 'sdtpimaging.last', myparams, self.__globals__,scriptstr=scriptstr)
00291           if async :
00292             count = 0
00293             keybase =  time.strftime("%y%m%d.%H%M%S")
00294             key = keybase + "_" + str(count)
00295             while self.__async__.has_key(key) :
00296                count += 1
00297                key = keybase + "_" + str(count)
00298             result = tm.execute('sdtpimaging', infile, calmode, masklist, blpoly, backup, flaglist, antenna, spw, stokes, createimage, outfile, imsize, cell, phasecenter, ephemsrcname, pointingcolumn, gridfunction, plotlevel)
00299             print "Use: "
00300             print "      tm.retrieve(return_value) # to retrieve the status"
00301             print 
00302             self.rkey = key
00303             self.__async__[key] = result
00304           else :
00305               tname = 'sdtpimaging'
00306               spaces = ' '*(18-len(tname))
00307               casalog.post('\n##########################################'+
00308                            '\n##### Begin Task: ' + tname + spaces + ' #####')
00309               casalog.post(scriptstr[1][1:]+'\n', 'INFO')
00310               result = sdtpimaging(infile, calmode, masklist, blpoly, backup, flaglist, antenna, spw, stokes, createimage, outfile, imsize, cell, phasecenter, ephemsrcname, pointingcolumn, gridfunction, plotlevel)
00311               casalog.post('##### End Task: ' + tname + '  ' + spaces + ' #####'+
00312                            '\n##########################################')
00313 
00314         except Exception, instance:
00315           if(self.__globals__.has_key('__rethrow_casa_exceptions') and self.__globals__['__rethrow_casa_exceptions']) :
00316              raise
00317           else :
00318              #print '**** Error **** ',instance
00319              tname = 'sdtpimaging'
00320              casalog.post('An error occurred running task '+tname+'.', 'ERROR')
00321              pass
00322 
00323         gc.collect()
00324         return result
00325 #
00326 #
00327 #
00328     def paramgui(self, useGlobals=True, ipython_globals=None):
00329         """
00330         Opens a parameter GUI for this task.  If useGlobals is true, then any relevant global parameter settings are used.
00331         """
00332         import paramgui
00333         if not hasattr(self, "__globals__") or self.__globals__ == None :
00334            self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals
00335 
00336         if useGlobals:
00337             if ipython_globals == None:
00338                 myf=self.__globals__
00339             else:
00340                 myf=ipython_globals
00341 
00342             paramgui.setGlobals(myf)
00343         else:
00344             paramgui.setGlobals({})
00345 
00346         paramgui.runTask('sdtpimaging', myf['_ip'])
00347         paramgui.setGlobals({})
00348 
00349 #
00350 #
00351 #
00352     def defaults(self, param=None, ipython_globals=None, paramvalue=None, subparam=None):
00353         if not hasattr(self, "__globals__") or self.__globals__ == None :
00354            self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals
00355         if ipython_globals == None:
00356             myf=self.__globals__
00357         else:
00358             myf=ipython_globals
00359 
00360         a = odict()
00361         a['infile']  = ''
00362         a['calmode']  = 'none'
00363         a['flaglist']  = []
00364         a['antenna']  = ''
00365         a['spw']  = 0
00366         a['stokes']  = ''
00367         a['createimage']  = False
00368         a['plotlevel']  = 0
00369 
00370         a['async']=False
00371         a['calmode'] = {
00372                     0:{'value':'none'}, 
00373                     1:odict([{'value':'baseline'}, {'masklist':[]}, {'blpoly':1}, {'backup':True}])}
00374         a['createimage'] = {
00375                     0:{'value':False}, 
00376                     1:odict([{'value':True}, {'outfile':''}, {'imsize':[256, 256]}, {'cell':[1, 1]}, {'phasecenter':''}, {'ephemsrcname':''}, {'pointingcolumn':'direction'}, {'gridfunction':'BOX'}])}
00377 
00378 ### This function sets the default values but also will return the list of
00379 ### parameters or the default value of a given parameter
00380         if(param == None):
00381                 myf['__set_default_parameters'](a)
00382         elif(param == 'paramkeys'):
00383                 return a.keys()
00384         else:
00385             if(paramvalue==None and subparam==None):
00386                if(a.has_key(param)):
00387                   return a[param]
00388                else:
00389                   return self.itsdefault(param)
00390             else:
00391                retval=a[param]
00392                if(type(a[param])==dict):
00393                   for k in range(len(a[param])):
00394                      valornotval='value'
00395                      if(a[param][k].has_key('notvalue')):
00396                         valornotval='notvalue'
00397                      if((a[param][k][valornotval])==paramvalue):
00398                         retval=a[param][k].copy()
00399                         retval.pop(valornotval)
00400                         if(subparam != None):
00401                            if(retval.has_key(subparam)):
00402                               retval=retval[subparam]
00403                            else:
00404                               retval=self.itsdefault(subparam)
00405                      else:
00406                         retval=self.itsdefault(subparam)
00407                return retval
00408 
00409 
00410 #
00411 #
00412     def check_params(self, param=None, value=None, ipython_globals=None):
00413       if ipython_globals == None:
00414           myf=self.__globals__
00415       else:
00416           myf=ipython_globals
00417 #      print 'param:', param, 'value:', value
00418       try :
00419          if str(type(value)) != "<type 'instance'>" :
00420             value0 = value
00421             value = myf['cu'].expandparam(param, value)
00422             matchtype = False
00423             if(type(value) == numpy.ndarray):
00424                if(type(value) == type(value0)):
00425                   myf[param] = value.tolist()
00426                else:
00427                   #print 'value:', value, 'value0:', value0
00428                   #print 'type(value):', type(value), 'type(value0):', type(value0)
00429                   myf[param] = value0
00430                   if type(value0) != list :
00431                      matchtype = True
00432             else :
00433                myf[param] = value
00434             value = myf['cu'].verifyparam({param:value})
00435             if matchtype:
00436                value = False
00437       except Exception, instance:
00438          #ignore the exception and just return it unchecked
00439          myf[param] = value
00440       return value
00441 #
00442 #
00443     def description(self, key='sdtpimaging', subkey=None):
00444         desc={'sdtpimaging': 'SD task: do a simple calibration and imaging for total power data ',
00445                'infile': 'name of input SD dataset(only MS is allowed for this task)',
00446                'calmode': 'SD calibration mode (currently only support none or baseline subtraction )',
00447                'masklist': 'mask in data row numbers to be included for baseline fitting for each scan',
00448                'blpoly': 'polynomial order for the baseline fit',
00449                'backup': 'set True to create backup for input data',
00450                'flaglist': 'list of scan numbers to flag (e.g. [[1,3], 80])',
00451                'antenna': 'antenna name(s) or id(s)',
00452                'spw': 'spectral window id for imaging',
00453                'stokes': 'stokes or correlation name (e.g. "I", "XX")',
00454                'createimage': 'do imaging?',
00455                'outfile': 'output image name',
00456                'imsize': 'x and y image size in pixels, symmetric for single value',
00457                'cell': 'x and y cell size. default unit arcmin',
00458                'phasecenter': 'Image phase center: position or field index',
00459                'ephemsrcname': 'ephemeris source name',
00460                'pointingcolumn': 'pointing data column to use',
00461                'gridfunction': 'gridding function for imaging ("BOX","SF","PB")',
00462                'plotlevel': 'plot results (0=none,1+=some,<0=hardcopy)',
00463 
00464                'async': 'If true the taskname must be started using sdtpimaging(...)'
00465               }
00466 
00467 #
00468 # Set subfields defaults if needed
00469 #
00470 
00471         if(desc.has_key(key)) :
00472            return desc[key]
00473 
00474     def itsdefault(self, paramname) :
00475         a = {}
00476         a['infile']  = ''
00477         a['calmode']  = 'none'
00478         a['masklist']  = []
00479         a['blpoly']  = 1
00480         a['backup']  = True
00481         a['flaglist']  = []
00482         a['antenna']  = ''
00483         a['spw']  = 0
00484         a['stokes']  = ''
00485         a['createimage']  = False
00486         a['outfile']  = ''
00487         a['imsize']  = [256, 256]
00488         a['cell']  = ['1.0arcmin', '1.0arcmin']
00489         a['phasecenter']  = ''
00490         a['ephemsrcname']  = ''
00491         a['pointingcolumn']  = 'direction'
00492         a['gridfunction']  = 'BOX'
00493         a['plotlevel']  = 0
00494 
00495         #a = sys._getframe(len(inspect.stack())-1).f_globals
00496 
00497         if self.parameters['calmode']  == 'baseline':
00498             a['masklist'] = []
00499             a['blpoly'] = 1
00500             a['backup'] = True
00501 
00502         if self.parameters['createimage']  == True:
00503             a['outfile'] = ''
00504             a['imsize'] = [256, 256]
00505             a['cell'] = [1, 1]
00506             a['phasecenter'] = ''
00507             a['ephemsrcname'] = ''
00508             a['pointingcolumn'] = 'direction'
00509             a['gridfunction'] = 'BOX'
00510 
00511         if a.has_key(paramname) :
00512               return a[paramname]
00513 sdtpimaging_cli = sdtpimaging_cli_()