casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
fluxscale_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_fluxscale import fluxscale
00018 class fluxscale_cli_:
00019     __name__ = "fluxscale"
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__ = (fluxscale_cli_,)
00028        self.__doc__ = self.__call__.__doc__
00029 
00030        self.parameters={'vis':None, 'caltable':None, 'fluxtable':None, 'reference':None, 'transfer':None, 'listfile':None, 'append':None, 'refspwmap':None, 'incremental':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, vis=None, caltable=None, fluxtable=None, reference=None, transfer=None, listfile=None, append=None, refspwmap=None, incremental=None,  async=None):
00047 
00048         """Bootstrap the flux density scale from standard calibrators
00049 
00050        After running gaincal on standard flux density calibrators (with or
00051        without an image model), and other calibrators with unknown flux
00052        densities (assumed 1 Jy), fluxscale applies the constraint that
00053        net system gain was, in fact, independent of field, on average,
00054        and that field-dependent gains in the input caltable are solely
00055        a result of the unknown flux densities for the calibrators.
00056        Using time-averaged gain amplitudes, the ratio between 
00057        each ordinary calibrator and the flux density calibrator(s) is 
00058        formed for each antenna and polarization (that they have in
00059        common).  For incremetal=False(default), the median of 
00060        this ratio over antennas and polarizations yields a correction 
00061        factor that is applied to the ordinary calibrators' gains. For 
00062        incremental=True, only the correction factors are written out 
00063        to the output fluxtable.
00064 
00065        The square of the gain correction factor for each calibrator
00066        and spw is the presumed flux density of that calibrator, and is
00067        reported in the logger.  The errors reported with this value
00068        reflect the scatter in gain ratio over antennas and
00069        polarizations, divided by the square root of the number of 
00070        antennas and polarizations available.  If the flux densities 
00071        for multiple spws exist, fitted spectral index and (for nspw>2)
00072        curvature are also reported. The MODEL_DATA column
00073        is currently _not_ revised to reflect the flux densities
00074        derived by fluxscale.  Use setjy to set the MODEL_DATA column,
00075        if necessary.
00076 
00077        The constant gain constraint is usually a reasonable assumption
00078        for the electronic systems on typical antennas.  It is
00079        important that external time- and/or elevation-dependent
00080        effects are separately accounted for when solving for the gain
00081        solution supplied to fluxscale, e.g., gain curves, 
00082        opacity, etc.  The fluxscale results can also be degraded
00083        by poor pointing during the observation.
00084 
00085 
00086        Keyword arguments:
00087        vis -- Name of input visibility file
00088                default: none; example: vis='ngc5921.ms'
00089        caltable -- Name of input calibration table
00090                default: none; example: caltable='ngc5921.gcal'
00091                This cal table was obtained from task gaincal.
00092        fluxtable -- Name of output, flux-scaled calibration table
00093                default: none; example: fluxtable='ngc5921.gcal2'
00094                The gains in this table have been adjusted by the
00095                derived flux density each calibrator.  The MODEL_DATA
00096                column has NOT been updated for the flux density of the
00097                calibrator.  Use setjy to do this if it is a point source.
00098        reference -- Reference field name(s)
00099                The names of the fields with a known flux densities or
00100                   visibilties that have been placed in the MODEL column
00101                   by setjy or ft for a model not in the CASA system.
00102                The syntax is similar to field.  Hence field index or
00103                   names can be used.
00104                default: none; example: reference='1328+307'
00105        transfer -- Transfer field name(s)
00106                The names of the fields with unknown flux densities.
00107                   These should be point-like calibrator sources
00108                The syntax is similar to field.  Hence source index or
00109                  names can be used.
00110                default: '' = all sources in caltable that are not specified
00111                   as reference sources.  Do not include unknown target sources
00112                example: transfer='1445+099, 3C84'; transfer = '0,4'
00113 
00114                NOTE: All fields in reference and transfer must have solutions
00115                in the caltable.
00116 
00117        listfile -- Fit listfile name
00118                The list file contains the flux density, flux density error,
00119                  S/N, and number of solutions (all antennas and feeds) for each
00120                  spectral window.  NOTE: The nominal spectral window frequencies
00121                  will be included in the future.
00122                default: '' = no fit listfile will be created.
00123 
00124        append -- Append fluxscaled solutions to the fluxtable.
00125                default: False; (will overwrite if already existing)
00126                example: append=True
00127        refspwmap -- Vector of spectral windows enablings scaling across
00128                spectral windows
00129                default: [-1]==> none.
00130                Example with 4 spectral windows:
00131                if the reference fields were observed only in spw=1 & 3,
00132                and the transfer fields were observed in all 4 spws (0,1,2,3),
00133                specify refspwmap=[1,1,3,3].
00134                This will ensure that transfer fields observed in spws 0,1,2,3
00135                will be referenced to reference field solutions only in
00136                spw 1 or 3.
00137       
00138        incremental -- Create an incremental caltable containing only gain correction 
00139                factors ( flux density= 1/(gain correction factor)**2)
00140                default: False; (older behavior = create flux-scaled gain table)
00141                example: incremental=True (output a caltable containing flux scale factors.)
00142               
00143                NOTE: If you use the incremental option, note that BOTH this incremental 
00144                fluxscale table AND an amplitude vs. time table should be supplied in applycal.
00145 
00146  
00147         """
00148         if not hasattr(self, "__globals__") or self.__globals__ == None :
00149            self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals
00150         #casac = self.__globals__['casac']
00151         casalog = self.__globals__['casalog']
00152         #casalog = casac.casac.logsink()
00153         self.__globals__['__last_task'] = 'fluxscale'
00154         self.__globals__['taskname'] = 'fluxscale'
00155         ###
00156         self.__globals__['update_params'](func=self.__globals__['taskname'],printtext=False,ipython_globals=self.__globals__)
00157         ###
00158         ###
00159         #Handle globals or user over-ride of arguments
00160         #
00161         function_signature_defaults=dict(zip(self.__call__.func_code.co_varnames,self.__call__.func_defaults))
00162         useLocalDefaults = False
00163 
00164         for item in function_signature_defaults.iteritems():
00165                 key,val = item
00166                 keyVal = eval(key)
00167                 if (keyVal == None):
00168                         #user hasn't set it - use global/default
00169                         pass
00170                 else:
00171                         #user has set it - use over-ride
00172                         if (key != 'self') :
00173                            useLocalDefaults = True
00174 
00175         myparams = {}
00176         if useLocalDefaults :
00177            for item in function_signature_defaults.iteritems():
00178                key,val = item
00179                keyVal = eval(key)
00180                exec('myparams[key] = keyVal')
00181                self.parameters[key] = keyVal
00182                if (keyVal == None):
00183                    exec('myparams[key] = '+ key + ' = self.itsdefault(key)')
00184                    keyVal = eval(key)
00185                    if(type(keyVal) == dict) :
00186                       if len(keyVal) > 0 :
00187                          exec('myparams[key] = ' + key + ' = keyVal[len(keyVal)-1][\'value\']')
00188                       else :
00189                          exec('myparams[key] = ' + key + ' = {}')
00190 
00191         else :
00192             async = self.parameters['async']
00193             myparams['vis'] = vis = self.parameters['vis']
00194             myparams['caltable'] = caltable = self.parameters['caltable']
00195             myparams['fluxtable'] = fluxtable = self.parameters['fluxtable']
00196             myparams['reference'] = reference = self.parameters['reference']
00197             myparams['transfer'] = transfer = self.parameters['transfer']
00198             myparams['listfile'] = listfile = self.parameters['listfile']
00199             myparams['append'] = append = self.parameters['append']
00200             myparams['refspwmap'] = refspwmap = self.parameters['refspwmap']
00201             myparams['incremental'] = incremental = self.parameters['incremental']
00202 
00203         if type(reference)==str: reference=[reference]
00204         if type(transfer)==str: transfer=[transfer]
00205         if type(refspwmap)==int: refspwmap=[refspwmap]
00206 
00207         result = None
00208 
00209 #
00210 #    The following is work around to avoid a bug with current python translation
00211 #
00212         mytmp = {}
00213 
00214         mytmp['vis'] = vis
00215         mytmp['caltable'] = caltable
00216         mytmp['fluxtable'] = fluxtable
00217         mytmp['reference'] = reference
00218         mytmp['transfer'] = transfer
00219         mytmp['listfile'] = listfile
00220         mytmp['append'] = append
00221         mytmp['refspwmap'] = refspwmap
00222         mytmp['incremental'] = incremental
00223         pathname='file:///'+os.environ.get('CASAPATH').split()[0]+'/share/xml/'
00224         trec = casac.casac.utils().torecord(pathname+'fluxscale.xml')
00225 
00226         casalog.origin('fluxscale')
00227         try :
00228           #if not trec.has_key('fluxscale') or not casac.casac.utils().verify(mytmp, trec['fluxscale']) :
00229             #return False
00230 
00231           casac.casac.utils().verify(mytmp, trec['fluxscale'], True)
00232           scriptstr=['']
00233           saveinputs = self.__globals__['saveinputs']
00234           saveinputs('fluxscale', 'fluxscale.last', myparams, self.__globals__,scriptstr=scriptstr)
00235           if async :
00236             count = 0
00237             keybase =  time.strftime("%y%m%d.%H%M%S")
00238             key = keybase + "_" + str(count)
00239             while self.__async__.has_key(key) :
00240                count += 1
00241                key = keybase + "_" + str(count)
00242             result = tm.execute('fluxscale', vis, caltable, fluxtable, reference, transfer, listfile, append, refspwmap, incremental)
00243             print "Use: "
00244             print "      tm.retrieve(return_value) # to retrieve the status"
00245             print 
00246             self.rkey = key
00247             self.__async__[key] = result
00248           else :
00249               tname = 'fluxscale'
00250               spaces = ' '*(18-len(tname))
00251               casalog.post('\n##########################################'+
00252                            '\n##### Begin Task: ' + tname + spaces + ' #####')
00253               casalog.post(scriptstr[1][1:]+'\n', 'INFO')
00254               result = fluxscale(vis, caltable, fluxtable, reference, transfer, listfile, append, refspwmap, incremental)
00255               casalog.post('##### End Task: ' + tname + '  ' + spaces + ' #####'+
00256                            '\n##########################################')
00257 
00258         except Exception, instance:
00259           if(self.__globals__.has_key('__rethrow_casa_exceptions') and self.__globals__['__rethrow_casa_exceptions']) :
00260              raise
00261           else :
00262              #print '**** Error **** ',instance
00263              tname = 'fluxscale'
00264              casalog.post('An error occurred running task '+tname+'.', 'ERROR')
00265              pass
00266 
00267         gc.collect()
00268         return result
00269 #
00270 #
00271 #
00272     def paramgui(self, useGlobals=True, ipython_globals=None):
00273         """
00274         Opens a parameter GUI for this task.  If useGlobals is true, then any relevant global parameter settings are used.
00275         """
00276         import paramgui
00277         if not hasattr(self, "__globals__") or self.__globals__ == None :
00278            self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals
00279 
00280         if useGlobals:
00281             if ipython_globals == None:
00282                 myf=self.__globals__
00283             else:
00284                 myf=ipython_globals
00285 
00286             paramgui.setGlobals(myf)
00287         else:
00288             paramgui.setGlobals({})
00289 
00290         paramgui.runTask('fluxscale', myf['_ip'])
00291         paramgui.setGlobals({})
00292 
00293 #
00294 #
00295 #
00296     def defaults(self, param=None, ipython_globals=None, paramvalue=None, subparam=None):
00297         if not hasattr(self, "__globals__") or self.__globals__ == None :
00298            self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals
00299         if ipython_globals == None:
00300             myf=self.__globals__
00301         else:
00302             myf=ipython_globals
00303 
00304         a = odict()
00305         a['vis']  = ''
00306         a['caltable']  = ''
00307         a['fluxtable']  = ''
00308         a['reference']  = ['']
00309         a['transfer']  = ['']
00310         a['listfile']  = ''
00311         a['append']  = False
00312         a['refspwmap']  = [-1]
00313         a['incremental']  = False
00314 
00315         a['async']=False
00316 
00317 ### This function sets the default values but also will return the list of
00318 ### parameters or the default value of a given parameter
00319         if(param == None):
00320                 myf['__set_default_parameters'](a)
00321         elif(param == 'paramkeys'):
00322                 return a.keys()
00323         else:
00324             if(paramvalue==None and subparam==None):
00325                if(a.has_key(param)):
00326                   return a[param]
00327                else:
00328                   return self.itsdefault(param)
00329             else:
00330                retval=a[param]
00331                if(type(a[param])==dict):
00332                   for k in range(len(a[param])):
00333                      valornotval='value'
00334                      if(a[param][k].has_key('notvalue')):
00335                         valornotval='notvalue'
00336                      if((a[param][k][valornotval])==paramvalue):
00337                         retval=a[param][k].copy()
00338                         retval.pop(valornotval)
00339                         if(subparam != None):
00340                            if(retval.has_key(subparam)):
00341                               retval=retval[subparam]
00342                            else:
00343                               retval=self.itsdefault(subparam)
00344                      else:
00345                         retval=self.itsdefault(subparam)
00346                return retval
00347 
00348 
00349 #
00350 #
00351     def check_params(self, param=None, value=None, ipython_globals=None):
00352       if ipython_globals == None:
00353           myf=self.__globals__
00354       else:
00355           myf=ipython_globals
00356 #      print 'param:', param, 'value:', value
00357       try :
00358          if str(type(value)) != "<type 'instance'>" :
00359             value0 = value
00360             value = myf['cu'].expandparam(param, value)
00361             matchtype = False
00362             if(type(value) == numpy.ndarray):
00363                if(type(value) == type(value0)):
00364                   myf[param] = value.tolist()
00365                else:
00366                   #print 'value:', value, 'value0:', value0
00367                   #print 'type(value):', type(value), 'type(value0):', type(value0)
00368                   myf[param] = value0
00369                   if type(value0) != list :
00370                      matchtype = True
00371             else :
00372                myf[param] = value
00373             value = myf['cu'].verifyparam({param:value})
00374             if matchtype:
00375                value = False
00376       except Exception, instance:
00377          #ignore the exception and just return it unchecked
00378          myf[param] = value
00379       return value
00380 #
00381 #
00382     def description(self, key='fluxscale', subkey=None):
00383         desc={'fluxscale': 'Bootstrap the flux density scale from standard calibrators',
00384                'vis': 'Name of input visibility file (MS)',
00385                'caltable': 'Name of input calibration table',
00386                'fluxtable': 'Name of output, flux-scaled calibration table',
00387                'reference': 'Reference field name(s) (transfer flux scale FROM)',
00388                'transfer': 'Transfer field name(s) (transfer flux scale TO), \'\' -> all',
00389                'listfile': 'Name of listfile that contains the fit information.  Default is '' (no file).',
00390                'append': 'Append solutions?',
00391                'refspwmap': 'Scale across spectral window boundaries.  See help fluxscale',
00392                'incremental': 'incremental caltable',
00393 
00394                'async': 'If true the taskname must be started using fluxscale(...)'
00395               }
00396 
00397         if(desc.has_key(key)) :
00398            return desc[key]
00399 
00400     def itsdefault(self, paramname) :
00401         a = {}
00402         a['vis']  = ''
00403         a['caltable']  = ''
00404         a['fluxtable']  = ''
00405         a['reference']  = ['']
00406         a['transfer']  = ['']
00407         a['listfile']  = ''
00408         a['append']  = False
00409         a['refspwmap']  = [-1]
00410         a['incremental']  = False
00411 
00412         #a = sys._getframe(len(inspect.stack())-1).f_globals
00413 
00414         if a.has_key(paramname) :
00415               return a[paramname]
00416 fluxscale_cli = fluxscale_cli_()