casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
applycal_pg.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 string
00010 import time
00011 import inspect
00012 import gc
00013 import numpy
00014 from odict import odict
00015 from task_applycal import applycal
00016 from task_applycal import casalog
00017 
00018 class applycal_pg_:
00019     __name__ = "applycal"
00020 
00021     def __init__(self) :
00022        self.__bases__ = (applycal_pg_,)
00023        self.__doc__ = self.__call__.__doc__
00024 
00025 
00026     def __call__(self, vis=None, field=None, spw=None, intent=None, selectdata=None, timerange=None, uvrange=None, antenna=None, scan=None, observation=None, msselect=None, gaintable=None, gainfield=None, interp=None, spwmap=None, gaincurve=None, opacity=None, parang=None, calwt=None, applymode=None, flagbackup=None, async=None):
00027 
00028         """Apply calibrations solutions(s) to data
00029 
00030 
00031        Applycal reads the specified gain calibration tables, applies
00032        them to the (raw) data column (with the specified selection),
00033        and writes the calibrated results into the corrected column.
00034        This is done in one step, so all available calibration tables must
00035        be specified.  Included parang, gaincurve, and opacity are used in
00036        obtaining previous calibration tables.
00037 
00038        Applycal will overwrite existing corrected data, and will flag data
00039        for which there is no calibration available.
00040 
00041        All calibration tables (both temporal, frequency, polarization
00042        calibrations) are specified in the gaintable parameter.  The
00043        calibration values associated with a restricted list of fields
00044        can also be selected for each table in gainfield.
00045 
00046        See task accum for instructions on forming calibration
00047        incrementally.  See task split for copying out any portion of the data
00048        and selected columns to a new visibility file.
00049 
00050        Keyword arguments:
00051        vis -- Name of input visibility file
00052                default: < none>; example: vis='ngc5921.ms'
00053 
00054       --- Data Selection: the data to which the calibration will be applied
00055         (see help par.selectdata for more detailed information)
00056 
00057       field -- Select field id(s) or field name(s) to apply calibration.
00058                  [run listobs to obtain the list id's or names]
00059                default: ''=all fields
00060                If field's string is an integer >=0, it is assumed to be an index
00061                  otherwise, it is assumed to be a field name
00062                field='0~2'; field ids 0,1,2
00063                field='0,4,5~7'; field ids 0,4,5,6,7
00064                field='3C286,3C295'; fields named 3C286 and 3C295
00065                field = '3,4C*'; field id 3, all names starting with 4C
00066       spw -- Select spectral window/channels 
00067                type 'help par.selection' for more examples.
00068              spw='0~2,4'; spectral windows 0,1,2,4 (all channels)
00069              spw='<2';  spectral windows less than 2 (i.e. 0,1)
00070              spw='0:5~61'; spw 0, channels 5 to 61, INCLUSIVE
00071              spw='*:5~61'; all spw with channels 5 to 62
00072              spw='0,10,3:3~45'; spw 0,10 all channels, spw 3, channels 3 to 45.
00073              spw='0~2:2~6'; spw 0,1,2 with channels 2 through 6 in each.
00074              spw='0:0~10;15~60'; spectral window 0 with channels 0-10,15-60
00075                        NOTE ';' to separate channel selections
00076              spw='0:0~10^2,1:20~30^5'; spw 0, channels 0,2,4,6,8,10,
00077                    spw 1, channels 20,25,30
00078        intent -- Select observing intent
00079                  default: ''  (no selection by intent)
00080                  intent='*BANDPASS*'  (selects data labelled with
00081                                        BANDPASS intent)
00082        selectdata -- Other data selection parameters
00083               default: True
00084        timerange  -- Select data based on time range:
00085               default = '' (all); examples,
00086               timerange = 'YYYY/MM/DD/hh:mm:ss~YYYY/MM/DD/hh:mm:ss'
00087               Note: if YYYY/MM/DD is missing, date defaults to first day in
00088                     data set
00089               timerange='09:14:0~09:54:0' picks 40 min on first day
00090               timerange= '25:00:00~27:30:00' picks 1 hr to 3 hr 30min on next day
00091               timerange='09:44:00' data within one integration of time
00092               timerange='>10:24:00' data after this time
00093        uvrange -- Select data within uvrange (default units meters)
00094               default: '' (all); example:
00095               uvrange='0~1000kl'; uvrange from 0-1000 kilo-lambda
00096               uvrange='>4kl';uvranges greater than 4 kilolambda
00097        antenna -- Select data based on antenna/baseline
00098               default: '' (all)
00099               If antenna's string is an integer >=0, it is taken to be an index
00100                 otherwise, it is assumed to be an antenna name
00101               antenna='5&6'; baseline between antenna index 5 and index 6.
00102               antenna='VA05&VA06'; baseline between VLA antenna 5 and 6.
00103               antenna='5&6;7&8'; baseline 5-6 and 7-8
00104               antenna='5'; all baselines with antenna index 5
00105               antenna='05'; all baselines with antenna name 05--vla antenna 5.
00106               antenna='5,6,10'; all baselines with antennas 5,6 and 10
00107        scan -- Scan number range
00108        observation -- Select by observation ID(s).
00109                        default: '' = all
00110                        example: '0~3,6'
00111        msselect -- Optional complex data selection (ignore for now)
00112 
00113        --- Calibration files to apply
00114        gaintable -- Gain calibration table(s) to apply 
00115                default: '' (none);
00116                examples: gaintable='ngc5921.gcal'
00117                    gaintable=['n5921.ampcal','n5921.phcal','n5921.bpass']
00118                All gain table types: 'G', GSPLINE, 'T', 'B', 'BPOLY', 'D's'
00119                    can be applied.
00120        gainfield -- Select a subset of calibrators from each gaintable
00121                default:'' ==> all sources in table;
00122                'nearest' ==> nearest (on sky) available field in table
00123                otherwise, same syntax as field
00124                example: gainfield='0~3'
00125                         gainfield=['0~3','4~6']  (for multiple gaintables)
00126        interp -- Interpolation type (in time[,freq]) to use for each gaintable.
00127                 When frequency interpolation is relevant (B, Df, Xf),
00128                 separate time-dependent and freq-dependent interp
00129                 types with a comma (freq _after_ the comma).                
00130                 Specifications for frequency are ignored when the
00131                 calibration table has no channel-dependence.
00132                 Time-dependent interp options ending in 'PD' enable a
00133                 "phase delay" correction per spw for non-channel-dependent
00134                 calibration types.
00135                 default: '' --> 'linear,linear' for all gaintable(s)
00136                 example: interp='nearest'   (in time, freq-dep will be
00137                                              linear, if relevant)
00138                          interp='linear,cubic'  (linear in time, cubic
00139                                                  in freq)
00140                          interp=',spline'  (spline in freq; linear in
00141                                             time by default)
00142                          interp=['nearest,spline','linear']  (for multiple gaintables)
00143                 Options: Time: 'nearest', 'linear', 'nearestPD', 'linearPD'
00144                          Freq: 'nearest', 'linear', 'cubic', 'spline'
00145        spwmap -- Spectral windows combinations to form for gaintable(s)
00146                default: [] (apply solutions from each spw to that spw only)
00147                Example:  spwmap=[0,0,1,1] means apply the caltable solutions
00148                          from spw = 0 to the spw 0,1 and spw 1 to spw 2,3.
00149                          spwmap=[[0,0,1,1],[0,1,0,1]]  (for multiple gaintables)
00150                          
00151           Complicated example:
00152 
00153             gaintable=['tab1','tab2','tab3']
00154             gainfield='3C286'
00155             interp=['linear','nearest']
00156             spwmap=[[],[0,0,2]]
00157             
00158             This means: apply 3 cal tables, selecting only solutions for 3C286
00159             from tab1 (but all fields from tab2 and tab3, indicated by
00160             no gainfield entry for these files).  Linear interpolation
00161             (in time) will be used for 'tab1' and 'tab3' (default); 'tab2' will
00162             use nearest.  For the 'tab2', the calibration spws map
00163             will be mapped to the data spws according to 0->0, 0->1, 2->2.
00164             (I.e., for data spw=0 and 2, the spw mapping is one to one,
00165             but data spw 1 will be calibrated by solutions from spw 0.)  
00166            
00167        gaincurve -- Apply internal VLA/EVLA  antenna gain curve correction (True/False)
00168                default: False;
00169                Use gaincurve=True ONLY for VLA or EVLA data
00170        opacity -- Opacity correction to apply (nepers), per spw
00171                default: [] (no opacity correction for any spw)
00172                examples: 
00173                    A global value for all spws:
00174                      opacity=0.051 
00175                    Different values for spws 0,1,2:
00176                      opacity=[0.051, 0.055, 0.057] 
00177                    (if fewer opacity values than spws are specified,
00178                     then the last-specified value will be duplicated 
00179                     for all higher-numbered spws, e.g., in the above 
00180                     example, 0.057 will be used for spws 3 and higher, 
00181                     if they exist)
00182                Typical VLA values are: 5 GHz - 0.013, 8 GHz - 0.013
00183                15 GHz - 0.016, 23 GHz - 0.051, 43 GHz - 0.07
00184        parang -- If True, apply the parallactic angle correction.  FOR ANY
00185                POLARIZATION CALIBRATION AND IMAGING, parang = True
00186                default: False
00187        calwt -- Calibrate weights along with data for all
00188                relevant calibrations
00189                default: True; example: calwt=False
00190        applymode -- Calibration apply mode:
00191                ''='calflag' (default) calibrate data and apply flags from solutions
00192                'trial' report on flags from solutions, dataset entirely unchanged
00193                'flagonly' apply flags from solutions only, data not calibrated
00194                'calonly' calibrate data only (by unflagged solutions), flags from solutions NOT applied (use with extreme caution!)
00195        flagbackup -- Back up the state of the flags before applying calibration
00196                      default: True
00197        async -- Run task in a separate process 
00198                 default: False; example: async=True
00199 
00200 
00201 
00202         """
00203         a=inspect.stack()
00204         stacklevel=0
00205         for k in range(len(a)):
00206           if (string.find(a[k][1], 'ipython console') > 0) or (string.find(a[k][1], '<string>') >= 0):
00207                 stacklevel=k
00208                 break
00209         myf=sys._getframe(stacklevel).f_globals
00210         myf['__last_task'] = 'applycal'
00211         myf['taskname'] = 'applycal'
00212         ###
00213         myf['update_params'](func=myf['taskname'],printtext=False)
00214         ###
00215         ###
00216         #Handle globals or user over-ride of arguments
00217         #
00218         function_signature_defaults=dict(zip(self.__call__.func_code.co_varnames,self.__call__.func_defaults))
00219         useLocalDefaults = False
00220 
00221         for item in function_signature_defaults.iteritems():
00222                 key,val = item
00223                 keyVal = eval(key)
00224                 if (keyVal == None):
00225                         #user hasn't set it - use global/default
00226                         pass
00227                 else:
00228                         #user has set it - use over-ride
00229                         if (key != 'self') :
00230                            useLocalDefaults = True
00231                         #myf[key]=keyVal
00232 
00233         myparams = {}
00234         if useLocalDefaults :
00235            for item in function_signature_defaults.iteritems():
00236                key,val = item
00237                keyVal = eval(key)
00238                exec('myparams[key] = keyVal')
00239                if (keyVal == None):
00240                    exec('myparams[key] = '+ key + ' = self.itsdefault(key)')
00241                    keyVal = eval(key)
00242                    if(type(keyVal) == dict) :
00243                       exec('myparams[key] = ' + key + ' = keyVal[len(keyVal)-1][\'value\']')
00244 
00245         else :
00246             uselessvariable = None 
00247             myparams['vis'] = vis = myf['vis']
00248             myparams['field'] = field = myf['field']
00249             myparams['spw'] = spw = myf['spw']
00250             myparams['intent'] = intent = myf['intent']
00251             myparams['selectdata'] = selectdata = myf['selectdata']
00252             myparams['timerange'] = timerange = myf['timerange']
00253             myparams['uvrange'] = uvrange = myf['uvrange']
00254             myparams['antenna'] = antenna = myf['antenna']
00255             myparams['scan'] = scan = myf['scan']
00256             myparams['observation'] = observation = myf['observation']
00257             myparams['msselect'] = msselect = myf['msselect']
00258             myparams['gaintable'] = gaintable = myf['gaintable']
00259             myparams['gainfield'] = gainfield = myf['gainfield']
00260             myparams['interp'] = interp = myf['interp']
00261             myparams['spwmap'] = spwmap = myf['spwmap']
00262             myparams['gaincurve'] = gaincurve = myf['gaincurve']
00263             myparams['opacity'] = opacity = myf['opacity']
00264             myparams['parang'] = parang = myf['parang']
00265             myparams['calwt'] = calwt = myf['calwt']
00266             myparams['applymode'] = applymode = myf['applymode']
00267             myparams['flagbackup'] = flagbackup = myf['flagbackup']
00268 
00269         if type(gaintable)==str: gaintable=[gaintable]
00270         if type(gainfield)==str: gainfield=[gainfield]
00271         if type(interp)==str: interp=[interp]
00272         if type(spwmap)==int: spwmap=[spwmap]
00273         if type(opacity)==float: opacity=[opacity]
00274 
00275         result = None
00276 
00277 #
00278 #    The following is work around to avoid a bug with current python translation
00279 #
00280         mytmp = {}
00281 
00282         mytmp['vis'] = vis
00283         mytmp['field'] = field
00284         mytmp['spw'] = spw
00285         mytmp['intent'] = intent
00286         mytmp['selectdata'] = selectdata
00287         mytmp['timerange'] = timerange
00288         mytmp['uvrange'] = uvrange
00289         mytmp['antenna'] = antenna
00290         mytmp['scan'] = scan
00291         mytmp['observation'] = observation
00292         mytmp['msselect'] = msselect
00293         mytmp['gaintable'] = gaintable
00294         mytmp['gainfield'] = gainfield
00295         mytmp['interp'] = interp
00296         mytmp['spwmap'] = spwmap
00297         mytmp['gaincurve'] = gaincurve
00298         mytmp['opacity'] = opacity
00299         mytmp['parang'] = parang
00300         mytmp['calwt'] = calwt
00301         mytmp['applymode'] = applymode
00302         mytmp['flagbackup'] = flagbackup
00303         pathname='file:///'+os.environ.get('CASAPATH').split()[0]+'/share/xml/'
00304         trec = casac.utils().torecord(pathname+'applycal.xml')
00305 
00306         casalog.origin('applycal')
00307         if not trec.has_key('applycal') or not casac.utils().verify(mytmp, trec['applycal']) :
00308             return False
00309 
00310 
00311         try :
00312           casalog.post('')
00313           casalog.post('##########################################')
00314           casalog.post('##### Begin Task: applycal           #####')
00315           casalog.post('')
00316           result = applycal(vis, field, spw, intent, selectdata, timerange, uvrange, antenna, scan, observation, msselect, gaintable, gainfield, interp, spwmap, gaincurve, opacity, parang, calwt, applymode, flagbackup)
00317           casalog.post('')
00318           casalog.post('##### End Task: applycal           #####')
00319           casalog.post('##########################################')
00320 
00321 
00322 # saveinputs for individule engine has no use
00323 # saveinputs should alos be removed from casa_in_py.py
00324 #
00325 #
00326 #          saveinputs = myf['saveinputs']
00327 #          saveinputs('applycal', 'applycal.last', myparams)
00328 #
00329 #
00330         except Exception, instance:
00331           #print '**** Error **** ',instance
00332           pass
00333 
00334         gc.collect()
00335         return result
00336 #
00337 #
00338 ##
00339 #    def paramgui(self, useGlobals=True):
00340 #        """
00341 #        Opens a parameter GUI for this task.  If useGlobals is true, then any relevant global parameter settings are used.
00342 #        """
00343 #        import paramgui
00344 #
00345 #        a=inspect.stack()
00346 #        stacklevel=0
00347 #        for k in range(len(a)):
00348 #          if (string.find(a[k][1], 'ipython console') > 0) or (string.find(a[k][1], '<string>') >= 0):
00349 #            stacklevel=k
00350 #            break
00351 #        myf = sys._getframe(stacklevel).f_globals
00352 #
00353 #        if useGlobals:
00354 #            paramgui.setGlobals(myf)
00355 #        else:
00356 #            paramgui.setGlobals({})
00357 #
00358 #        paramgui.runTask('applycal', myf['_ip'])
00359 #        paramgui.setGlobals({})
00360 #
00361 #
00362 #
00363 #
00364     def defaults(self, param=None):
00365         a=inspect.stack()
00366         stacklevel=0
00367         for k in range(len(a)):
00368           if (string.find(a[k][1], 'ipython console') > 0) or (string.find(a[k][1], '<string>') >= 0):
00369                 stacklevel=k
00370                 break
00371         myf=sys._getframe(stacklevel).f_globals
00372         a = odict()
00373         a['vis']  = ''
00374         a['field']  = ''
00375         a['spw']  = ''
00376         a['intent']  = ''
00377         a['selectdata']  = True
00378         a['gaintable']  = ['']
00379         a['gainfield']  = ['']
00380         a['interp']  = ['']
00381         a['spwmap']  = []
00382         a['gaincurve']  = False
00383         a['opacity']  = []
00384         a['parang']  = False
00385         a['calwt']  = True
00386         a['applymode']  = ''
00387         a['flagbackup']  = True
00388 
00389         a['async']=False
00390         a['selectdata'] = {
00391                     0:odict([{'value':True}, {'timerange':''}, {'uvrange':''}, {'antenna':''}, {'scan':''}, {'observation':''}, {'msselect':''}]), 
00392                     1:{'value':False}}
00393 
00394 ### This function sets the default values but also will return the list of
00395 ### parameters or the default value of a given parameter
00396         if(param == None):
00397                 myf['__set_default_parameters'](a)
00398         elif(param == 'paramkeys'):
00399                 return a.keys()
00400         else:
00401                 if(a.has_key(param)):
00402                    #if(type(a[param]) == dict) :
00403                    #   return a[param][len(a[param])-1]['value']
00404                    #else :
00405                       return a[param]
00406 
00407 
00408 #
00409 #
00410     def check_params(self, param=None, value=None):
00411       a=inspect.stack() 
00412       stacklevel=0
00413       for k in range(len(a)):
00414         if (string.find(a[k][1], 'ipython console') > 0) or (string.find(a[k][1], '<string>') >= 0):
00415             stacklevel=k
00416             break
00417       myf=sys._getframe(stacklevel).f_globals
00418 
00419 #      print 'param:', param, 'value:', value
00420       try :
00421          if str(type(value)) != "<type 'instance'>" :
00422             value0 = value
00423             value = myf['cu'].expandparam(param, value)
00424             matchtype = False
00425             if(type(value) == numpy.ndarray):
00426                if(type(value) == type(value0)):
00427                   myf[param] = value.tolist()
00428                else:
00429                   #print 'value:', value, 'value0:', value0
00430                   #print 'type(value):', type(value), 'type(value0):', type(value0)
00431                   myf[param] = value0
00432                   if type(value0) != list :
00433                      matchtype = True
00434             else :
00435                myf[param] = value
00436             value = myf['cu'].verifyparam({param:value})
00437             if matchtype:
00438                value = False
00439       except Exception, instance:
00440          #ignore the exception and just return it unchecked
00441          myf[param] = value
00442       return value
00443 
00444 #
00445 #
00446     def description(self, key='applycal', subkey=None):
00447         desc={'applycal': 'Apply calibrations solutions(s) to data',
00448                'vis': 'Name of input visibility file',
00449                'field': 'Select field using field id(s) or field name(s)',
00450                'spw': 'Select spectral window/channels',
00451                'intent': 'Select observing intent',
00452                'selectdata': 'Other data selection parameters',
00453                'timerange': 'Select data based on time range',
00454                'uvrange': 'Select data within uvrange (default units meters)',
00455                'antenna': 'Select data based on antenna/baseline',
00456                'scan': 'Scan number range',
00457                'observation': 'Select by observation ID(s)',
00458                'msselect': 'Optional complex data selection (ignore for now)',
00459                'gaintable': 'Gain calibration table(s) to apply on the fly',
00460                'gainfield': 'Select a subset of calibrators from gaintable(s)',
00461                'interp': 'Interp type in time[,freq], per gaintable.  default=linear,linear',
00462                'spwmap': 'Spectral windows combinations to form for gaintables(s)',
00463                'gaincurve': 'Apply internal VLA antenna gain curve correction',
00464                'opacity': 'Opacity correction to apply (nepers), per spw',
00465                'parang': 'Apply parallactic angle correction',
00466                'calwt': 'Calibrate data weights from all relevant calibrations',
00467                'applymode': 'Calibration mode: ""="calflag","trial","flagonly", or "calonly"',
00468                'flagbackup': 'Automatically back up the state of flags before the run?',
00469 
00470                'async': 'If true the taskname must be started using applycal(...)'
00471               }
00472 
00473 #
00474 # Set subfields defaults if needed
00475 #
00476 
00477         if(desc.has_key(key)) :
00478            return desc[key]
00479 
00480     def itsdefault(self, paramname) :
00481         a = {}
00482         a['vis']  = ''
00483         a['field']  = ''
00484         a['spw']  = ''
00485         a['intent']  = ''
00486         a['selectdata']  = True
00487         a['timerange']  = ''
00488         a['uvrange']  = ''
00489         a['antenna']  = ''
00490         a['scan']  = ''
00491         a['observation']  = ''
00492         a['msselect']  = ''
00493         a['gaintable']  = ['']
00494         a['gainfield']  = ['']
00495         a['interp']  = ['']
00496         a['spwmap']  = []
00497         a['gaincurve']  = False
00498         a['opacity']  = []
00499         a['parang']  = False
00500         a['calwt']  = True
00501         a['applymode']  = ''
00502         a['flagbackup']  = True
00503 
00504         if a.has_key(paramname) :
00505               return a[paramname]
00506 applycal_pg = applycal_pg_()