casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
simanalyze_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_simanalyze import simanalyze
00018 class simanalyze_cli_:
00019     __name__ = "simanalyze"
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__ = (simanalyze_cli_,)
00028        self.__doc__ = self.__call__.__doc__
00029 
00030        self.parameters={'project':None, 'image':None, 'imagename':None, 'vis':None, 'modelimage':None, 'cell':None, 'imsize':None, 'imdirection':None, 'niter':None, 'threshold':None, 'weighting':None, 'mask':None, 'outertaper':None, 'stokes':None, 'analyze':None, 'showuv':None, 'showpsf':None, 'showmodel':None, 'showconvolved':None, 'showclean':None, 'showresidual':None, 'showdifference':None, 'showfidelity':None, 'graphics':None, 'verbose':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, project=None, image=None, imagename=None, vis=None, modelimage=None, cell=None, imsize=None, imdirection=None, niter=None, threshold=None, weighting=None, mask=None, outertaper=None, stokes=None, analyze=None, showuv=None, showpsf=None, showmodel=None, showconvolved=None, showclean=None, showresidual=None, showdifference=None, showfidelity=None, graphics=None, verbose=None, overwrite=None,  async=None):
00047 
00048         """image and analyze simulated datasets
00049 
00050     This task is for imaging and analyzing datasets created with 
00051     simobserve or simalma.  New functionality is actively 
00052     being added, so if you have changed versions of CASA, check 
00053     the inputs carefully.
00054     More information and examples are availible at 
00055      http://casaguides.nrao.edu/index.php?title=Simulating_Observations_in_CASA
00056     Please contact CASA experts with any questions.
00057     -------------------------------
00058     project -- root filename for all output files.
00059                must be the same as used when simulating the observation.
00060                in particular $project/$project.skymodel will be required
00061                to compare output and input images.
00062     image -- invert and deconvolve the measurement set(s)
00063        * NOTE: interactive clean or more parameters than the subset visible
00064          here are available by simply running the clean task directly, 
00065          then returning to simanalyze to run "analyze" if desired.
00066        * NOTE: the channelization of the output image cube will be the 
00067          same as that in the simulated Measurement Set.
00068        * if graphics turned on, display the clean image and residual image
00069        * uses Cotton-Schwab clean for single fields and Mosaic gridding
00070          for multiple fields (with Clark PSF calculation in minor cycles).
00071     vis -- the simulated interferometric MS, or total-power one, or both
00072        * one can use '$project' to let the task automatically replace it to
00073          the project name, e.g., vis='$project.noisy.ms,$project.noisy.sd.ms'.
00074          However, note that if you created measurement set(s) using simobserve,
00075          they will have names including the configuration, e.g. 
00076          $project.alma_out20.noisy.ms
00077        * setting this paramter to "default" will find and attempt to image
00078          all measurement sets (interferometric and single dish) in the 
00079          project directory
00080     modelimage -- prior (e.g. SD) image to be used in clean
00081     cell -- cell size e.g '10arcsec'.  "" defaults to the skymodel cell
00082     imsize -- image size in spatial pixels (x,y)
00083        0 or -1 will use the model image size; example: imsize=[500,500]
00084     imdirection -- phase center for synthesized image.  default is to 
00085        center on the sky model.
00086     niter -- number of clean/deconvolution iterations, 0 for no cleaning
00087     threshold -- flux level to stop cleaning
00088     weighting -- weighting to apply to visibilities
00089        options: 'natural','uniform','briggs' (robust=0.5)
00090     mask -- Specification of cleanbox(es), mask image(s), primary beam
00091        coverage level, and/or region(s) to be used for CLEANing.
00092        CLEAN tends to perform better, and is less likely to diverge, if 
00093        the CLEAN component placement is limited by a mask to where real 
00094        emission is expected to be.  e.g. pixel ranges mask=[110,110,150,145],
00095        filename of mask image mask='myimage.mask', or a file with mask 
00096        regions --  see help for the clean task.
00097     outertaper -- apply additional uv outer taper of visibilities
00098     stokes -- Stokes parameters to image; 'I','IV','IQU','IQUV'
00099     -------------------------------
00100     analyze -- compute and display difference between model and output, 
00101          fidelity, etc. 
00102     showuv -- display uv coverage
00103     showpsf -- display synthesized (dirty) beam (ignored in single dish simulation)
00104     showmodel -- display sky model at original resolution
00105     showconvolved -- display sky model convolved with output beam 
00106     showclean -- display the synthesized image
00107     showresidual -- display the clean residual image (ignored in single dish simulation)
00108     showdifference -- display difference between output cleaned image and 
00109          input model sky image convolved with output clean beam
00110     showfidelity -- display fidelity image
00111          fidelity = abs(input) / max[ abs(input-output), 0.7*rms(output) ]
00112     
00113     graphics -- view plots on the screen, saved to file, both, or neither
00114     verbose -- print extra information to the logger and terminal
00115     overwrite -- overwrite existing files in the project subdirectory
00116 
00117     Note that the RMS is calculated in the lower quarter of the image.  
00118          This is likely not the best choice, so you are encouraged to 
00119          measure RMS yourself in an off-source region using the viewer.
00120 
00121 
00122     -------------------------------
00123     Output produced: (not all will always exist, depending on input parameters)
00124     To support different runs with different arrays, the names have the
00125     configuration name from antennalist appended.
00126     -------------------------------
00127     project.[cfg].skymodel.flat.regrid.conv = input sky regridded to match 
00128          the output image, and convolved with the output clean beam
00129 
00130     project.[cfg].image = synthesized image
00131     project.[cfg].flux.pbcoverage = primary beam correction for mosaic image
00132     project.[cfg].residual = residual image after cleaning
00133     project.[cfg].clean.last = parameter file of what parameters were used in
00134           the clean task
00135     project.[cfg].psf = synthesized (dirty) beam calculated from weighted uv 
00136           distribution
00137     project.[cfg].image.png = diagnostic figure of clean image and residual
00138             
00139     project.[cfg].fidelity = fidelity image
00140     project.[cfg].analysis.png = diagnostic figure of difference and fidelity
00141             
00142     project.[cfg].simanalyze.last = saved input parameters for simanalyze task
00143 
00144 
00145         """
00146         if not hasattr(self, "__globals__") or self.__globals__ == None :
00147            self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals
00148         #casac = self.__globals__['casac']
00149         casalog = self.__globals__['casalog']
00150         #casalog = casac.casac.logsink()
00151         self.__globals__['__last_task'] = 'simanalyze'
00152         self.__globals__['taskname'] = 'simanalyze'
00153         ###
00154         self.__globals__['update_params'](func=self.__globals__['taskname'],printtext=False,ipython_globals=self.__globals__)
00155         ###
00156         ###
00157         #Handle globals or user over-ride of arguments
00158         #
00159         function_signature_defaults=dict(zip(self.__call__.func_code.co_varnames,self.__call__.func_defaults))
00160         useLocalDefaults = False
00161 
00162         for item in function_signature_defaults.iteritems():
00163                 key,val = item
00164                 keyVal = eval(key)
00165                 if (keyVal == None):
00166                         #user hasn't set it - use global/default
00167                         pass
00168                 else:
00169                         #user has set it - use over-ride
00170                         if (key != 'self') :
00171                            useLocalDefaults = True
00172 
00173         myparams = {}
00174         if useLocalDefaults :
00175            for item in function_signature_defaults.iteritems():
00176                key,val = item
00177                keyVal = eval(key)
00178                exec('myparams[key] = keyVal')
00179                self.parameters[key] = keyVal
00180                if (keyVal == None):
00181                    exec('myparams[key] = '+ key + ' = self.itsdefault(key)')
00182                    keyVal = eval(key)
00183                    if(type(keyVal) == dict) :
00184                       if len(keyVal) > 0 :
00185                          exec('myparams[key] = ' + key + ' = keyVal[len(keyVal)-1][\'value\']')
00186                       else :
00187                          exec('myparams[key] = ' + key + ' = {}')
00188 
00189         else :
00190             async = self.parameters['async']
00191             myparams['project'] = project = self.parameters['project']
00192             myparams['image'] = image = self.parameters['image']
00193             myparams['imagename'] = imagename = self.parameters['imagename']
00194             myparams['vis'] = vis = self.parameters['vis']
00195             myparams['modelimage'] = modelimage = self.parameters['modelimage']
00196             myparams['cell'] = cell = self.parameters['cell']
00197             myparams['imsize'] = imsize = self.parameters['imsize']
00198             myparams['imdirection'] = imdirection = self.parameters['imdirection']
00199             myparams['niter'] = niter = self.parameters['niter']
00200             myparams['threshold'] = threshold = self.parameters['threshold']
00201             myparams['weighting'] = weighting = self.parameters['weighting']
00202             myparams['mask'] = mask = self.parameters['mask']
00203             myparams['outertaper'] = outertaper = self.parameters['outertaper']
00204             myparams['stokes'] = stokes = self.parameters['stokes']
00205             myparams['analyze'] = analyze = self.parameters['analyze']
00206             myparams['showuv'] = showuv = self.parameters['showuv']
00207             myparams['showpsf'] = showpsf = self.parameters['showpsf']
00208             myparams['showmodel'] = showmodel = self.parameters['showmodel']
00209             myparams['showconvolved'] = showconvolved = self.parameters['showconvolved']
00210             myparams['showclean'] = showclean = self.parameters['showclean']
00211             myparams['showresidual'] = showresidual = self.parameters['showresidual']
00212             myparams['showdifference'] = showdifference = self.parameters['showdifference']
00213             myparams['showfidelity'] = showfidelity = self.parameters['showfidelity']
00214             myparams['graphics'] = graphics = self.parameters['graphics']
00215             myparams['verbose'] = verbose = self.parameters['verbose']
00216             myparams['overwrite'] = overwrite = self.parameters['overwrite']
00217 
00218         if type(imsize)==int: imsize=[imsize]
00219         if type(outertaper)==str: outertaper=[outertaper]
00220 
00221         result = None
00222 
00223 #
00224 #    The following is work around to avoid a bug with current python translation
00225 #
00226         mytmp = {}
00227 
00228         mytmp['project'] = project
00229         mytmp['image'] = image
00230         mytmp['imagename'] = imagename
00231         mytmp['vis'] = vis
00232         mytmp['modelimage'] = modelimage
00233         mytmp['cell'] = cell
00234         mytmp['imsize'] = imsize
00235         mytmp['imdirection'] = imdirection
00236         mytmp['niter'] = niter
00237         mytmp['threshold'] = threshold
00238         mytmp['weighting'] = weighting
00239         mytmp['mask'] = mask
00240         mytmp['outertaper'] = outertaper
00241         mytmp['stokes'] = stokes
00242         mytmp['analyze'] = analyze
00243         mytmp['showuv'] = showuv
00244         mytmp['showpsf'] = showpsf
00245         mytmp['showmodel'] = showmodel
00246         mytmp['showconvolved'] = showconvolved
00247         mytmp['showclean'] = showclean
00248         mytmp['showresidual'] = showresidual
00249         mytmp['showdifference'] = showdifference
00250         mytmp['showfidelity'] = showfidelity
00251         mytmp['graphics'] = graphics
00252         mytmp['verbose'] = verbose
00253         mytmp['overwrite'] = overwrite
00254         pathname='file:///'+os.environ.get('CASAPATH').split()[0]+'/share/xml/'
00255         trec = casac.casac.utils().torecord(pathname+'simanalyze.xml')
00256 
00257         casalog.origin('simanalyze')
00258         try :
00259           #if not trec.has_key('simanalyze') or not casac.casac.utils().verify(mytmp, trec['simanalyze']) :
00260             #return False
00261 
00262           casac.casac.utils().verify(mytmp, trec['simanalyze'], True)
00263           scriptstr=['']
00264           saveinputs = self.__globals__['saveinputs']
00265           saveinputs('simanalyze', 'simanalyze.last', myparams, self.__globals__,scriptstr=scriptstr)
00266           if async :
00267             count = 0
00268             keybase =  time.strftime("%y%m%d.%H%M%S")
00269             key = keybase + "_" + str(count)
00270             while self.__async__.has_key(key) :
00271                count += 1
00272                key = keybase + "_" + str(count)
00273             result = tm.execute('simanalyze', project, image, imagename, vis, modelimage, cell, imsize, imdirection, niter, threshold, weighting, mask, outertaper, stokes, analyze, showuv, showpsf, showmodel, showconvolved, showclean, showresidual, showdifference, showfidelity, graphics, verbose, overwrite)
00274             print "Use: "
00275             print "      tm.retrieve(return_value) # to retrieve the status"
00276             print 
00277             self.rkey = key
00278             self.__async__[key] = result
00279           else :
00280               tname = 'simanalyze'
00281               spaces = ' '*(18-len(tname))
00282               casalog.post('\n##########################################'+
00283                            '\n##### Begin Task: ' + tname + spaces + ' #####')
00284               casalog.post(scriptstr[1][1:]+'\n', 'INFO')
00285               result = simanalyze(project, image, imagename, vis, modelimage, cell, imsize, imdirection, niter, threshold, weighting, mask, outertaper, stokes, analyze, showuv, showpsf, showmodel, showconvolved, showclean, showresidual, showdifference, showfidelity, graphics, verbose, overwrite)
00286               casalog.post('##### End Task: ' + tname + '  ' + spaces + ' #####'+
00287                            '\n##########################################')
00288 
00289         except Exception, instance:
00290           if(self.__globals__.has_key('__rethrow_casa_exceptions') and self.__globals__['__rethrow_casa_exceptions']) :
00291              raise
00292           else :
00293              #print '**** Error **** ',instance
00294              tname = 'simanalyze'
00295              casalog.post('An error occurred running task '+tname+'.', 'ERROR')
00296              pass
00297 
00298         gc.collect()
00299         return result
00300 #
00301 #
00302 #
00303     def paramgui(self, useGlobals=True, ipython_globals=None):
00304         """
00305         Opens a parameter GUI for this task.  If useGlobals is true, then any relevant global parameter settings are used.
00306         """
00307         import paramgui
00308         if not hasattr(self, "__globals__") or self.__globals__ == None :
00309            self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals
00310 
00311         if useGlobals:
00312             if ipython_globals == None:
00313                 myf=self.__globals__
00314             else:
00315                 myf=ipython_globals
00316 
00317             paramgui.setGlobals(myf)
00318         else:
00319             paramgui.setGlobals({})
00320 
00321         paramgui.runTask('simanalyze', myf['_ip'])
00322         paramgui.setGlobals({})
00323 
00324 #
00325 #
00326 #
00327     def defaults(self, param=None, ipython_globals=None, paramvalue=None, subparam=None):
00328         if not hasattr(self, "__globals__") or self.__globals__ == None :
00329            self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals
00330         if ipython_globals == None:
00331             myf=self.__globals__
00332         else:
00333             myf=ipython_globals
00334 
00335         a = odict()
00336         a['project']  = 'sim'
00337         a['image']  = True
00338         a['analyze']  = False
00339         a['graphics']  = 'both'
00340         a['verbose']  = False
00341         a['overwrite']  = True
00342 
00343         a['async']=False
00344         a['image'] = {
00345                     0:odict([{'value':True}, {'vis':'default'}, {'modelimage':''}, {'imsize':0}, {'imdirection':''}, {'cell':''}, {'niter':500}, {'threshold':'0.1mJy'}, {'weighting':'natural'}, {'mask':[]}, {'outertaper':[]}, {'stokes':'I'}]), 
00346                     1:odict([{'value':False}, {'imagename':'default'}])}
00347         a['analyze'] = {
00348                     0:{'value':False}, 
00349                     1:odict([{'value':True}, {'showuv':True}, {'showpsf':True}, {'showmodel':True}, {'showconvolved':False}, {'showclean':True}, {'showresidual':False}, {'showdifference':True}, {'showfidelity':True}])}
00350 
00351 ### This function sets the default values but also will return the list of
00352 ### parameters or the default value of a given parameter
00353         if(param == None):
00354                 myf['__set_default_parameters'](a)
00355         elif(param == 'paramkeys'):
00356                 return a.keys()
00357         else:
00358             if(paramvalue==None and subparam==None):
00359                if(a.has_key(param)):
00360                   return a[param]
00361                else:
00362                   return self.itsdefault(param)
00363             else:
00364                retval=a[param]
00365                if(type(a[param])==dict):
00366                   for k in range(len(a[param])):
00367                      valornotval='value'
00368                      if(a[param][k].has_key('notvalue')):
00369                         valornotval='notvalue'
00370                      if((a[param][k][valornotval])==paramvalue):
00371                         retval=a[param][k].copy()
00372                         retval.pop(valornotval)
00373                         if(subparam != None):
00374                            if(retval.has_key(subparam)):
00375                               retval=retval[subparam]
00376                            else:
00377                               retval=self.itsdefault(subparam)
00378                      else:
00379                         retval=self.itsdefault(subparam)
00380                return retval
00381 
00382 
00383 #
00384 #
00385     def check_params(self, param=None, value=None, ipython_globals=None):
00386       if ipython_globals == None:
00387           myf=self.__globals__
00388       else:
00389           myf=ipython_globals
00390 #      print 'param:', param, 'value:', value
00391       try :
00392          if str(type(value)) != "<type 'instance'>" :
00393             value0 = value
00394             value = myf['cu'].expandparam(param, value)
00395             matchtype = False
00396             if(type(value) == numpy.ndarray):
00397                if(type(value) == type(value0)):
00398                   myf[param] = value.tolist()
00399                else:
00400                   #print 'value:', value, 'value0:', value0
00401                   #print 'type(value):', type(value), 'type(value0):', type(value0)
00402                   myf[param] = value0
00403                   if type(value0) != list :
00404                      matchtype = True
00405             else :
00406                myf[param] = value
00407             value = myf['cu'].verifyparam({param:value})
00408             if matchtype:
00409                value = False
00410       except Exception, instance:
00411          #ignore the exception and just return it unchecked
00412          myf[param] = value
00413       return value
00414 #
00415 #
00416     def description(self, key='simanalyze', subkey=None):
00417         desc={'simanalyze': 'image and analyze simulated datasets',
00418                'project': 'root prefix for output file names',
00419                'image': '(re)image $project.*.ms to $project.image',
00420                'imagename': 'image to analyze (default = first $project/*.image found)',
00421                'vis': 'Measurement Set(s) to image',
00422                'modelimage': 'prior image to use in clean e.g. existing single dish image',
00423                'cell': 'cell size with units or "" to equal model',
00424                'imsize': 'output image size in pixels (x,y) or 0 to match model',
00425                'imdirection': 'set output image direction, (otherwise center on the model)',
00426                'niter': 'maximum number of iterations (0 for dirty image)',
00427                'threshold': 'flux level (+units) to stop cleaning',
00428                'weighting': 'weighting to apply to visibilities',
00429                'mask': 'Cleanbox(es), mask image(s), region(s), or a level',
00430                'outertaper': 'uv-taper on outer baselines in uv-plane',
00431                'stokes': 'Stokes params to image',
00432                'analyze': '(only first 6 selected outputs will be displayed)',
00433                'showuv': 'display uv coverage',
00434                'showpsf': 'display synthesized (dirty) beam (ignored in single dish simulation)',
00435                'showmodel': 'display sky model at original resolution',
00436                'showconvolved': 'display sky model convolved with output clean beam',
00437                'showclean': 'display the synthesized image',
00438                'showresidual': 'display the clean residual image (ignored in single dish simulation)',
00439                'showdifference': 'display difference between output cleaned image and input model sky image convolved with output clean beam',
00440                'showfidelity': 'display fidelity (see help)',
00441                'graphics': 'display graphics at each stage to [screen|file|both|none]',
00442                'verbose': '',
00443                'overwrite': 'overwrite files starting with $project',
00444 
00445                'async': 'If true the taskname must be started using simanalyze(...)'
00446               }
00447 
00448 #
00449 # Set subfields defaults if needed
00450 #
00451 
00452         if(desc.has_key(key)) :
00453            return desc[key]
00454 
00455     def itsdefault(self, paramname) :
00456         a = {}
00457         a['project']  = 'sim'
00458         a['image']  = True
00459         a['imagename']  = 'default'
00460         a['vis']  = 'default'
00461         a['modelimage']  = ''
00462         a['cell']  = ''
00463         a['imsize']  = [128, 128]
00464         a['imdirection']  = ''
00465         a['niter']  = 500
00466         a['threshold']  = '0.1mJy'
00467         a['weighting']  = 'natural'
00468         a['mask']  = []
00469         a['outertaper']  = ['']
00470         a['stokes']  = 'I'
00471         a['analyze']  = False
00472         a['showuv']  = True
00473         a['showpsf']  = True
00474         a['showmodel']  = True
00475         a['showconvolved']  = False
00476         a['showclean']  = True
00477         a['showresidual']  = False
00478         a['showdifference']  = True
00479         a['showfidelity']  = True
00480         a['graphics']  = 'both'
00481         a['verbose']  = False
00482         a['overwrite']  = True
00483 
00484         #a = sys._getframe(len(inspect.stack())-1).f_globals
00485 
00486         if self.parameters['image']  == True:
00487             a['vis'] = 'default'
00488             a['modelimage'] = ''
00489             a['imsize'] = 0
00490             a['imdirection'] = ''
00491             a['cell'] = ''
00492             a['niter'] = 500
00493             a['threshold'] = '0.1mJy'
00494             a['weighting'] = 'natural'
00495             a['mask'] = []
00496             a['outertaper'] = []
00497             a['stokes'] = 'I'
00498 
00499         if self.parameters['image']  == False:
00500             a['imagename'] = 'default'
00501 
00502         if self.parameters['analyze']  == True:
00503             a['showuv'] = True
00504             a['showpsf'] = True
00505             a['showmodel'] = True
00506             a['showconvolved'] = False
00507             a['showclean'] = True
00508             a['showresidual'] = False
00509             a['showdifference'] = True
00510             a['showfidelity'] = True
00511 
00512         if a.has_key(paramname) :
00513               return a[paramname]
00514 simanalyze_cli = simanalyze_cli_()