Example #1
0
def default(taskname=None):
    """ reset given task to its default values :

    taskname -- Name of task


    """

    try:
        myf=stack_frame_find( )
        if taskname==None: taskname=myf['taskname']
        myf['taskname']=taskname
        if type(taskname)!=str:
            taskname=taskname.__name__
            myf['taskname']=taskname

        ###Check if task exists by checking if task_defaults is defined
        if ( not myf.has_key(taskname) and
             str(type(myf[taskname])) != "<type 'instance'>" and
             not hasattr(myf[taskname],"defaults") ):
            raise TypeError, "task %s is not defined " %taskname
        eval(myf['taskname']+'.defaults()')

        casalog.origin('default')
        taskstring=str(taskname).split()[0]
        casalog.post(' #######  Setting values to default for task: '+taskstring+'  #######')


    except TypeError, e:
        print "default --error: ", e
Example #2
0
def default(taskname=None):
    """ reset given task to its default values :

    taskname -- Name of task


    """

    try:
        myf=stack_frame_find( )
        if taskname==None: taskname=myf['taskname']
        myf['taskname']=taskname
        if type(taskname)!=str:
            taskname=taskname.__name__
            myf['taskname']=taskname

        ###Check if task exists by checking if task_defaults is defined
        if ( not myf.has_key(taskname) and
             str(type(myf[taskname])) != "<type 'instance'>" and
             not hasattr(myf[taskname],"defaults") ):
            raise TypeError, "task %s is not defined " %taskname
        eval(myf['taskname']+'.defaults()')

        casalog.origin('default')
        taskstring=str(taskname).split()[0]
        casalog.post(' #######  Setting values to default for task: '+taskstring+'  #######')


    except TypeError, e:
        print "default --error: ", e
Example #3
0
def __set_default_parameters(b):
    myf=stack_frame_find( )
    a=b
    elkey=a.keys()
    for k in range(len(a)):
        if (type(a[elkey[k]]) != dict):
            myf[elkey[k]]=a[elkey[k]]
        elif (type(a[elkey[k]]) == dict and len(a[elkey[k]])==0):
            myf[elkey[k]]=a[elkey[k]]
        else:
            subdict=a[elkey[k]]
            ##clear out variables of other options if they exist
            for j in range(1,len(subdict)):
                subkey=subdict[j].keys()
                for kk in range(len(subkey)):
                    if((subkey[kk] != 'value') & (subkey[kk] != 'notvalue') ):
                        if(myf.has_key(subkey[kk])):
                            del myf[subkey[kk]]
            ###
            if(subdict[0].has_key('notvalue')):
                myf[elkey[k]]=subdict[0]['notvalue']
            else:
                myf[elkey[k]]=subdict[0]['value']
            subkey=subdict[0].keys()
            for j in range(0, len(subkey)):
                if((subkey[j] != 'value') & (subkey[j] != 'notvalue')):
                    myf[subkey[j]]=subdict[0][subkey[j]]
Example #4
0
def __set_default_parameters(b):
    myf=stack_frame_find( )
    a=b
    elkey=a.keys()
    for k in range(len(a)):
        if (type(a[elkey[k]]) != dict):
            myf[elkey[k]]=a[elkey[k]]
        elif (type(a[elkey[k]]) == dict and len(a[elkey[k]])==0):
            myf[elkey[k]]=a[elkey[k]]
        else:
            subdict=a[elkey[k]]
            ##clear out variables of other options if they exist
            for j in range(1,len(subdict)):
                subkey=subdict[j].keys()
                for kk in range(len(subkey)):
                    if((subkey[kk] != 'value') & (subkey[kk] != 'notvalue') ):
                        if(myf.has_key(subkey[kk])):
                            del myf[subkey[kk]]
            ###
            if(subdict[0].has_key('notvalue')):
                myf[elkey[k]]=subdict[0]['notvalue']
            else:
                myf[elkey[k]]=subdict[0]['value']
            subkey=subdict[0].keys()
            for j in range(0, len(subkey)):
                if((subkey[j] != 'value') & (subkey[j] != 'notvalue')):
                    myf[subkey[j]]=subdict[0][subkey[j]]
Example #5
0
def pointcal_defaults(param=None):
    myf = stack_frame_find()
    a = odict()
    a['vis'] = ''
    a['model'] = ''
    a['caltable'] = ''
    a['field'] = ''
    a['spw'] = ''
    a['selectdata'] = {
        1: {
            'value': False
        },
        0:
        odict([{
            'value': True
        }, {
            'timerange': ''
        }, {
            'uvrange': ''
        }, {
            'antenna': ''
        }, {
            'scan': ''
        }, {
            'msselect': ''
        }])
    }
    a['solint'] = 0.0
    if (param == None):
        myf['__set_default_parameters'](a)
    elif (param == 'paramkeys'):
        return a.keys()
    else:
        if (a.has_key(param)):
            return a[param]
Example #6
0
def filecatalog():
    """ Open the File Catalog GUI:

	"""
    myf = stack_frame_find()
    myf['__last_task'] = 'filecatalog'
    ###
    #Handle globals or user over-ride of arguments
    #Python script
    if (os.uname()[0] == 'Darwin'):
        from Carbon.Launch import LSFindApplicationForInfo
        from Carbon.CoreFoundation import kCFURLPOSIXPathStyle
        kLSUnknownCreator = '\x00\x00\x00\x00'
        fsRef, cfURL = LSFindApplicationForInfo(kLSUnknownCreator, None,
                                                "casafilecatalog.app")
        filecatalog_path = os.path.join(fsRef.as_pathname(), 'Contents',
                                        'MacOS', 'casafilecatalog')
        fcpid = os.spawnvp(os.P_NOWAIT, filecatalog_path, [filecatalog_path])
        #logpid=os.system('open -a casalogger.app casa.log')
    elif (os.uname()[0] == 'Linux'):
        fcpid = os.spawnlp(os.P_NOWAIT, 'casafilecatalog', 'casafilecatalog')
    else:
        print 'Unrecognized OS: No filecatalog available'

    myf['fcpid'] = fcpid
Example #7
0
def tput(taskname=None, outfile=''):
	myf=stack_frame_find( )
	if taskname == None: taskname = myf['taskname']
	if type(taskname) != str:
		taskname=taskname.__name__
	myf['taskname'] = taskname
	outfile = myf['taskname']+'.last'
	saveinputs(taskname, outfile)
 def setUp(self):
     self.g = stack_frame_find()
     if self.g.has_key('__rethrow_casa_exceptions'):
         self.rethrow_backup = self.g['__rethrow_casa_exceptions']
     else:
         self.rethrow_backup = None
     self.g['__rethrow_casa_exceptions'] = True
     super(failureTestCase, self).setUp()
Example #9
0
def tput(taskname=None, outfile=''):
	myf=stack_frame_find( )
	if taskname == None: taskname = myf['taskname']
	if type(taskname) != str:
		taskname=taskname.__name__
	myf['taskname'] = taskname
	outfile = myf['taskname']+'.last'
	saveinputs(taskname, outfile)
Example #10
0
def inp(taskname=None, page=False):
    """
    Function to browse input parameters of a given task
    taskname: name of task of interest
    page: use paging if True, useful if list of parameters is longer than terminal height
    """
    try:
        ####paging contributed by user Ramiro Hernandez
        if (page):
            #########################
            class TemporaryRedirect(object):
                def __init__(self, stdout=None, stderr=None):
                    self._stdout = stdout or sys.stdout
                    self._stderr = stderr or sys.stderr

                def __enter__(self):
                    self.old_stdout, self.old_stderr = sys.stdout, sys.stderr
                    self.old_stdout.flush()
                    self.old_stderr.flush()
                    sys.stdout, sys.stderr = self._stdout, self._stderr

                def __exit__(self, exc_type, exc_value, traceback):
                    self._stdout.flush()
                    self._stderr.flush()
                    sys.stdout = self.old_stdout
                    sys.stderr = self.old_stderr

            #######################end class
            tempfile = "__temp_input.casa"
            temporal = open(tempfile, 'w')

            with TemporaryRedirect(stdout=temporal):
                inp(taskname, False)
            temporal.close()
            os.system('more ' + tempfile)
            os.system('rm ' + tempfile)
            return
        ####
        myf = stack_frame_find()
        if ((taskname == None) and (not myf.has_key('taskname'))):
            print 'No task name defined for inputs display'
            return
        if taskname == None: taskname = myf['taskname']
        myf['taskname'] = taskname
        if type(taskname) != str:
            taskname = taskname.__name__
            myf['taskname'] = taskname

        try:
            parameter_checktype(['taskname'], taskname, str)
        except TypeError, e:
            print "inp -- TypeError: ", e
            return
        except ValueError, e:
            print "inp -- OptionError: ", e
            return
Example #11
0
def tget_defaults(param=None):
    myf = stack_frame_find()
    a = odict()
    a['task'] = ''
    a['savefile'] = ''
    if (param == None):
        myf['__set_default_parameters'](a)
    elif (param == 'paramkeys'):
        return a.keys()
    else:
        if (a.has_key(param)):
            return a[param]
Example #12
0
def tget_defaults(param=None):
       myf=stack_frame_find( )
       a=odict()
       a['task']=''
       a['savefile']=''
       if(param == None):
               myf['__set_default_parameters'](a)
       elif(param == 'paramkeys'):
               return a.keys()
       else:
               if(a.has_key(param)):
                       return a[param]
Example #13
0
def taskparamgui(useGlobals=True):
    """
        Show a parameter-setting GUI for all available tasks.
    """
    import paramgui

    if useGlobals:
        paramgui.setGlobals(stack_frame_find( ))
    else:
        paramgui.setGlobals({})

    paramgui.runAll(_ip)
    paramgui.setGlobals({})
Example #14
0
def taskparamgui(useGlobals=True):
    """
        Show a parameter-setting GUI for all available tasks.
    """
    import paramgui

    if useGlobals:
        paramgui.setGlobals(stack_frame_find( ))
    else:
        paramgui.setGlobals({})

    paramgui.runAll(_ip)
    paramgui.setGlobals({})
Example #15
0
def inp(taskname=None, page=False):
    """
    Function to browse input parameters of a given task
    taskname: name of task of interest
    page: use paging if True, useful if list of parameters is longer than terminal height
    """
    try:
        ####paging contributed by user Ramiro Hernandez
        if(page):
            #########################
            class TemporaryRedirect(object):
                def __init__(self, stdout=None, stderr=None):
                    self._stdout = stdout or sys.stdout
                    self._stderr = stderr or sys.stderr
                def __enter__(self):
                    self.old_stdout, self.old_stderr = sys.stdout, sys.stderr
                    self.old_stdout.flush(); self.old_stderr.flush()
                    sys.stdout, sys.stderr = self._stdout, self._stderr
                def __exit__(self, exc_type, exc_value, traceback):
                    self._stdout.flush(); self._stderr.flush()
                    sys.stdout = self.old_stdout
                    sys.stderr = self.old_stderr
            #######################end class
            tempfile="__temp_input.casa"
            temporal = open(tempfile, 'w')    

            with TemporaryRedirect(stdout=temporal):
                inp(taskname, False)
            temporal.close()
            os.system('more '+tempfile)
            os.system('rm '+tempfile)
            return
        ####
        myf=stack_frame_find( )
        if((taskname==None) and (not myf.has_key('taskname'))):
            print 'No task name defined for inputs display'
            return
        if taskname==None: taskname=myf['taskname']
        myf['taskname']=taskname
        if type(taskname)!=str:
            taskname=taskname.__name__
            myf['taskname']=taskname

        try:
            parameter_checktype(['taskname'],taskname,str)
        except TypeError, e:
            print "inp -- TypeError: ", e
            return
        except ValueError, e:
            print "inp -- OptionError: ", e
            return
Example #16
0
    def defaults(self,
                 param=None,
                 ipython_globals=None,
                 paramvalue=None,
                 subparam=None):
        if not hasattr(self, "__globals__") or self.__globals__ == None:
            self.__globals__ = stack_frame_find()
        if ipython_globals == None:
            myf = self.__globals__
        else:
            myf = ipython_globals

        a = odict()
        a['vis'] = ''
        a['spw'] = ''
        a['field'] = ''
        a['secsour'] = ''
        a['stddevfact'] = ''

        ### This function sets the default values but also will return the list of
        ### parameters or the default value of a given parameter
        if (param == None):
            myf['__set_default_parameters'](a)
        elif (param == 'paramkeys'):
            return a.keys()
        else:
            if (paramvalue == None and subparam == None):
                if (a.has_key(param)):
                    return a[param]
                else:
                    return self.itsdefault(param)
            else:
                retval = a[param]
                if (type(a[param]) == dict):
                    for k in range(len(a[param])):
                        valornotval = 'value'
                        if (a[param][k].has_key('notvalue')):
                            valornotval = 'notvalue'
                        if ((a[param][k][valornotval]) == paramvalue):
                            retval = a[param][k].copy()
                            retval.pop(valornotval)
                            if (subparam != None):
                                if (retval.has_key(subparam)):
                                    retval = retval[subparam]
                                else:
                                    retval = self.itsdefault(subparam)
                        else:
                            retval = self.itsdefault(subparam)
                return retval
Example #17
0
def asap_init():
    """ Initialize ASAP....: """
    myf = stack_frame_find()
    casapath = os.environ['CASAPATH']
    print('*** Loading ATNF ASAP Package...')
    import asap as sd
    #print('*** ... ASAP (%s rev#%s) import complete ***' % (sd.__version__,sd.__revision__))
    print('*** ... ASAP (rev#%s) import complete ***' % (sd.__revision__))
    os.environ['CASAPATH'] = casapath
    from sdimaging_cli import sdimaging_cli as sdimaging
    from sdfixscan_cli import sdfixscan_cli as sdfixscan
    from sdaverageold_cli import sdaverageold_cli as sdaverageold
    from sdbaselineold_cli import sdbaselineold_cli as sdbaselineold
    from sdbaseline2old_cli import sdbaseline2old_cli as sdbaseline2old
    from sdcalold_cli import sdcalold_cli as sdcalold
    from sdcal2old_cli import sdcal2old_cli as sdcal2old
    from sdcoaddold_cli import sdcoaddold_cli as sdcoaddold
    from sdfitold_cli import sdfitold_cli as sdfitold
    from sdflagold_cli import sdflagold_cli as sdflagold
    from sdflagmanagerold_cli import sdflagmanagerold_cli as sdflagmanagerold
    from sdgridold_cli import sdgridold_cli as sdgridold
    from sdlistold_cli import sdlistold_cli as sdlistold
    from sdmathold_cli import sdmathold_cli as sdmathold
    from sdplotold_cli import sdplotold_cli as sdplotold
    from sdreduceold_cli import sdreduceold_cli as sdreduceold
    from sdsaveold_cli import sdsaveold_cli as sdsaveold
    from sdscaleold_cli import sdscaleold_cli as sdscaleold
    from sdstatold_cli import sdstatold_cli as sdstatold
    myf['sd'] = sd
    myf['sdimaging'] = sdimaging
    myf['sdfixscan'] = sdfixscan
    myf['sdaverageold'] = sdaverageold
    myf['sdbaselineold'] = sdbaselineold
    myf['sdbaseline2old'] = sdbaseline2old
    myf['sdcalold'] = sdcalold
    myf['sdcal2old'] = sdcal2old
    myf['sdcoaddold'] = sdcoaddold
    myf['sdfitold'] = sdfitold
    myf['sdflagold'] = sdflagold
    myf['sdflagmanagerold'] = sdflagmanagerold
    myf['sdgridold'] = sdgridold
    myf['sdlistold'] = sdlistold
    myf['sdmathold'] = sdmathold
    myf['sdplotold'] = sdplotold
    myf['sdreduceold'] = sdreduceold
    myf['sdsaveold'] = sdsaveold
    myf['sdscaleold'] = sdscaleold
    myf['sdstatold'] = sdstatold
Example #18
0
def asap_init():
    """ Initialize ASAP....: """
    myf=stack_frame_find( )
    casapath=os.environ['CASAPATH']
    print('*** Loading ATNF ASAP Package...')
    import asap as sd
    #print('*** ... ASAP (%s rev#%s) import complete ***' % (sd.__version__,sd.__revision__))
    print('*** ... ASAP (rev#%s) import complete ***' % (sd.__revision__))
    os.environ['CASAPATH']=casapath
    from sdimaging_cli import sdimaging_cli as sdimaging
    from sdimprocess_cli import sdimprocess_cli as sdimprocess
    from sdaverageold_cli import sdaverageold_cli as sdaverageold
    from sdbaselineold_cli import sdbaselineold_cli as sdbaselineold
    from sdbaseline2old_cli import sdbaseline2old_cli as sdbaseline2old
    from sdcalold_cli import sdcalold_cli as sdcalold
    from sdcal2old_cli import sdcal2old_cli as sdcal2old
    from sdcoaddold_cli import sdcoaddold_cli as sdcoaddold
    from sdfitold_cli import sdfitold_cli as sdfitold
    from sdflagold_cli import sdflagold_cli as sdflagold
    from sdflagmanagerold_cli import sdflagmanagerold_cli as sdflagmanagerold
    from sdgridold_cli import sdgridold_cli as sdgridold
    from sdlistold_cli import sdlistold_cli as sdlistold
    from sdmathold_cli import sdmathold_cli as sdmathold
    from sdplotold_cli import sdplotold_cli as sdplotold
    from sdreduceold_cli import sdreduceold_cli as sdreduceold
    from sdsaveold_cli import sdsaveold_cli as sdsaveold
    from sdscaleold_cli import sdscaleold_cli as sdscaleold
    from sdstatold_cli import sdstatold_cli as sdstatold
    myf['sd']=sd
    myf['sdimaging']=sdimaging
    myf['sdimprocess']=sdimprocess
    myf['sdaverageold']=sdaverageold
    myf['sdbaselineold']=sdbaselineold
    myf['sdbaseline2old']=sdbaseline2old
    myf['sdcalold']=sdcalold
    myf['sdcal2old']=sdcal2old
    myf['sdcoaddold']=sdcoaddold
    myf['sdfitold']=sdfitold
    myf['sdflagold']=sdflagold
    myf['sdflagmanagerold']=sdflagmanagerold
    myf['sdgridold']=sdgridold
    myf['sdlistold']=sdlistold
    myf['sdmathold']=sdmathold
    myf['sdplotold']=sdplotold
    myf['sdreduceold']=sdreduceold
    myf['sdsaveold']=sdsaveold
    myf['sdscaleold']=sdscaleold
    myf['sdstatold']=sdstatold
Example #19
0
def go(taskname=None):
    """ Execute taskname: """
    myf=stack_frame_find( )
    if taskname==None: taskname=myf['taskname']
    oldtaskname=taskname
    if(myf.has_key('taskname')):
        oldtaskname=myf['taskname']
    #myf['taskname']=taskname
    if type(taskname)!=str:
        taskname=taskname.__name__
        myf['taskname']=taskname
    try:
        parameter_checktype(['taskname'],[taskname],str)
    except TypeError, e:
        print "go -- TypeError: ",e
        return
Example #20
0
    def paramgui(self, useGlobals=True, ipython_globals=None):
        """
        Opens a parameter GUI for this task.  If useGlobals is true, then any relevant global parameter settings are used.
        """
        import paramgui
        if not hasattr(self, "__globals__") or self.__globals__ == None:
            self.__globals__ = stack_frame_find()

        if useGlobals:
            if ipython_globals == None:
                myf = self.__globals__
            else:
                myf = ipython_globals

            paramgui.setGlobals(myf)
        else:
            paramgui.setGlobals({})

        paramgui.runTask('polconvert', myf['_ip'])
        paramgui.setGlobals({})
Example #21
0
def filecatalog():
	""" Open the File Catalog GUI:

	"""
        myf=stack_frame_find( )
        myf['__last_task']='filecatalog'
        ###
        #Handle globals or user over-ride of arguments
	#Python script
	if (os.uname()[0]=='Darwin'):
        	from Carbon.Launch import LSFindApplicationForInfo
        	from Carbon.CoreFoundation import kCFURLPOSIXPathStyle
        	kLSUnknownCreator = '\x00\x00\x00\x00'
        	fsRef, cfURL = LSFindApplicationForInfo(kLSUnknownCreator, None, "casafilecatalog.app")
        	filecatalog_path = os.path.join(fsRef.as_pathname(), 'Contents', 'MacOS', 'casafilecatalog')
		fcpid=os.spawnvp(os.P_NOWAIT, filecatalog_path, [filecatalog_path])
        	#logpid=os.system('open -a casalogger.app casa.log')
	elif (os.uname()[0]=='Linux'):
        	fcpid=os.spawnlp(os.P_NOWAIT,'casafilecatalog','casafilecatalog')
	else:
        	print 'Unrecognized OS: No filecatalog available'

	myf['fcpid']=fcpid
Example #22
0
def importvla(archivefiles, vis, bandname, frequencytol, project, starttime,
              stoptime, applytsys, autocorr, antnamescheme, keepblanks,
              evlabands):
    i = 0
    overwrite = True
    ok = True
    try:
        casalog.origin('importvla')
        if ((type(vis) != str) | (vis == '') | (os.path.exists(vis))):
            raise Exception, 'Need valid visibility file name (bad name or already exists)'
        if (os.path.exists(vis)):
            raise Exception, 'Visibility file already exists - remove or rename'
        for archivefile in archivefiles:
            if i > 0: overwrite = False
            myf = stack_frame_find()
            vftask = myf['vftask']
            vlafiller = vftask.fill
            if ((type(archivefile) == str) & (os.path.exists(archivefile))):
                vlafiller(msname=vis,
                          inputfile=archivefile,
                          overwrite=overwrite,
                          bandname=bandname,
                          freqtol=frequencytol,
                          project=project,
                          start=starttime,
                          stop=stoptime,
                          applytsys=applytsys,
                          keepautocorr=autocorr,
                          antnamescheme=antnamescheme,
                          keepblanks=keepblanks,
                          evlabands=evlabands)
                i += 1
            else:
                raise Exception, 'Archive file not found - please verify the name'
    except Exception, instance:
        print '*** Error importing %s to %s:' % (archivefiles, vis)
        raise Exception, instance
Example #23
0
def pointcal_defaults(param=None):
        myf=stack_frame_find( )
	a=odict()
        a['vis']=''
        a['model']=''
        a['caltable']=''
	a['field']=''
	a['spw']=''
        a['selectdata']={1:{'value':False},
			 0:odict([{'value':True},
				  {'timerange':''},
				  {'uvrange':''},
				  {'antenna':''},
				  {'scan':''},
				  {'msselect':''}])
                         }
        a['solint']=0.0
	if(param == None):
		myf['__set_default_parameters'](a)
	elif(param == 'paramkeys'):
		return a.keys()			    
        else:
		if(a.has_key(param)):
			return a[param]
Example #24
0
def pointcal(vis=None,model=None,caltable=None,
	     field=None,spw=None,
	     selectdata=None,timerange=None,uvrange=None,antenna=None,scan=None,msselect=None,
	     solint=None):
	"""Solve for pointing error calibration:

	This program is under development.  Please do not use.

	Keyword arguments:
	vis -- Name of input visibility file (MS)
		default: <unset>; example: vis='ngc5921.ms'
	model -- Name of input model (component list or image)
		default: <unset>; example: model='ngc5921.im'
	caltable -- Name of output Pointing calibration table
		default: <unset>; example: caltable='ngc5921.gcal'

	--- Data Selection (see help par.selectdata for more detailed information)

       field -- Select field using field id(s) or field name(s).
                  [run listobs to obtain the list id's or names]
               default: ''=all fields
               If field string is a non-negative integer, it is assumed a field index
                 otherwise, it is assumed a field name
               field='0~2'; field ids 0,1,2
               field='0,4,5~7'; field ids 0,4,5,6,7
               field='3C286,3C295'; field named 3C286 adn 3C295
               field = '3,4C*'; field id 3, all names starting with 4C
       spw -- Select spectral window/channels
               default: ''=all spectral windows and channels
               spw='0~2,4'; spectral windows 0,1,2,4 (all channels)
               spw='<2';  spectral windows less than 2 (i.e. 0,1)
               spw='0:5~61'; spw 0, channels 5 to 61
               spw='0,10,3:3~45'; spw 0,10 all channels, spw 3, channels 3 to 45.
               spw='0~2:2~6'; spw 0,1,2 with channels 2 through 6 in each.
               spw='0:0~10;15~60'; spectral window 0 with channels 0-10,15-60
               spw='0:0~10,1:20~30,2:1;2;3'; spw 0, channels 0-10,
                        spw 1, channels 20-30, and spw 2, channels, 1,2 and 3

       selectdata -- Other data selection parameters
               default: True
       timerange  -- Select data based on time range:
               default = '' (all); examples,
               timerange = 'YYYY/MM/DD/hh:mm:ss~YYYY/MM/DD/hh:mm:ss'
               Note: if YYYY/MM/DD is missing dat defaults to first day in data set
               timerange='09:14:0~09:54:0' picks 40 min on first day
               timerange= '25:00:00~27:30:00' picks 1 hr to 3 hr 30min on next day
               timerange='09:44:00' data within one integration of time
               timerange='>10:24:00' data after this time
       uvrange -- Select data within uvrange (default units kilo-lambda)
               default: '' (all); example:
               uvrange='0~1000'; uvrange from 0-1000 kilo-lamgda
               uvrange='>4';uvranges greater than 4 kilo lambda
       antenna -- Select data based on antenna/baseline
               default: '' (all)
               If antenna string is a non-negative integer, it is assumed an antenna index
                 otherwise, it is assumed as an antenna name
               antenna='5&6'; baseline between antenna index 5 and index 6.
               antenna='VA05&VA06'; baseline between VLA antenna 5 and 6.
               antenna='5&6;7&8'; baseline 5-6 and 7-8
               antenna='5'; all baselines with antenna 5
               antenna='5,6,10'; all baselines with antennas 5 and 6
       scan -- Scan number range - New, under developement
       msselect -- Optional complex data selection (ignore for now)

	solint --  Solution interval (sec)
		default: 0.0 (scan based); example: solint=60.

	"""
        myf=stack_frame_find( )
        myf['taskname']='pointcal'
	###fill unfilled parameters with defaults
	myf['update_params'](func=myf['taskname'], printtext=False)
	####local params 
        cb = myf['cb']
	selectantenna=myf['selectantenna']
        ###
        #Handle globals or user over-ride of arguments
        #
        function_signature_defaults=dict(zip(pointcal.func_code.co_varnames,pointcal.func_defaults))
        for item in function_signature_defaults.iteritems():
                key,val = item
                keyVal = eval(key)
                if (keyVal == None):
                        #user hasn't set it - use global/default
                        pass
                else:
			myf[key]=keyVal
                        #user has set it - use over-ride

        vis=myf['vis']
	model=myf['model']
        caltable=myf['caltable']
        field=myf['field']
        spw=myf['spw']
        selectdata=myf['selectdata']
	if (selectdata):
		timerange=myf['timerange']
		uvrange=myf['uvrange']
		antenna=myf['antenna']
		scan=myf['scan']
		msselect=myf['msselect']
	else:
		timerange=''
		uvrange=''
		antenna=''
		scan=''
		msselect=''
        solint=myf['solint']

        #Add type/menu/range error checking here
        arg_names=['vis','model','caltable',
		   'field','spw',
		   'selectdata','timerange','uvrange','antenna','scan','msselect',
		   'solint']
        arg_values=[vis,model,caltable,
		    field,spw,
		    selectdata,timerange,uvrange,antenna,scan,msselect,
		    solint]
        arg_types=[str,str,str,
		   str,str,
		   bool,str,str,str,str,str,
		   float]
        try:
                parameter_checktype(arg_names,arg_values,arg_types)
        except TypeError, e:
                print "pointcal -- TypeError: ", e
		return
Example #25
0
def update_params(func, printtext=True, ipython_globals=None):
    from odict import odict

    if ipython_globals == None:
        myf=stack_frame_find( )
    else:
        myf=ipython_globals

    ### set task to the one being called
    myf['taskname']=func
    obj=myf[func]

    if ( str(type(obj)) == "<type 'instance'>" and
         hasattr(obj,"check_params") ):
        hascheck = True
    else:
        hascheck = False

    noerror=True
    ###check if task has defined a task_check_params function

    if (hascheck):
	has_othertasks = myf.has_key('task_location')
	if(has_othertasks) :
	   has_task = myf['task_location'].has_key(myf['taskname'])
	   if (has_task) :
		pathname=myf['task_location'][myf['taskname']]
	   else :
	        pathname = os.environ.get('CASAPATH').split()[0]+'/'+os.environ.get('CASAPATH').split()[1]+'/xml'
                if not os.path.exists(pathname) :
                    pathname = os.environ.get('CASAPATH').split()[0]+'/xml'
                    if not os.path.exists(pathname) :
                        pathname = os.environ.get('CASAPATH').split()[0]+'/Resources/xml'
                
	else :
	   pathname = os.environ.get('CASAPATH').split()[0]+'/'+os.environ.get('CASAPATH').split()[1]+'/xml'
           if not os.path.exists(pathname) :
               pathname = os.environ.get('CASAPATH').split()[0]+'/xml'
               if not os.path.exists(pathname) :
                   pathname = os.environ.get('CASAPATH').split()[0]+'/Resources/xml'
                   if not os.path.exists(pathname) :
                       sys.exit("ERROR: casapy.py update_params() can not locate xml file for task %s" % (taskname))
                       
        xmlfile=pathname+'/'+myf['taskname']+'.xml'
        if(os.path.exists(xmlfile)) :
            cu.setconstraints('file://'+xmlfile);
        else:
            #
            # SRankin: quick fix for CAS-5381 - needs review.
            # The task is not a CASA internal task.  Extract the path from task_location.
            # This may not be robust.  I have not tracked down all the code that could update task_location.
            task_path=task_location[taskname]
            xmlfile=task_path+'/'+myf['taskname']+'.xml'
            if(os.path.exists(xmlfile)) :
                cu.setconstraints('file://'+xmlfile);

    a=myf[myf['taskname']].defaults("paramkeys",myf)
    itsdef=myf[myf['taskname']].defaults
    itsparams=myf[myf['taskname']].parameters
    params=a
    #print 'itsparams:', itsparams
    for k in range(len(params)):
        paramval = obj.defaults(params[k], myf)

        notdict=True
        ###if a dictionary with key 0, 1 etc then need to peel-open
        ###parameters
        if(type(paramval)==dict):
            if(paramval.has_key(0)):
                notdict=False
        if(myf.has_key(params[k])):
            itsparams.update({params[k]:myf[params[k]]})
        else:
            itsparams.update({params[k]:obj.itsdefault(params[k])})
        if (notdict ):
            if(not myf.has_key(params[k])):
                myf.update({params[k]:paramval})
                itsparams.update({params[k]:paramval})
            if(printtext):
                #print 'params:', params[k], '; myf[params]:', myf[params[k]]
                if(hascheck):
                    noerror = obj.check_params(params[k],myf[params[k]],myf)
                # RI this doesn't work with numpy arrays anymore.  Noone seems
                # interested, so I'll be the red hen and try to fix it.
                
                #print 'params:', params[k], '; noerror:', noerror, '; myf[params]:', myf[params[k]]
                myfparamsk=myf[params[k]]
                if(type(myf[params[k]])==np.ndarray):
                    myfparamsk=myfparamsk.tolist()
                #if(myf[params[k]]==paramval):
                if(myfparamsk==paramval):
                    print_params_col(params[k],myf[params[k]],obj.description(params[k]), 'ndpdef', 'black',noerror)
                else:
                    print_params_col(params[k],myf[params[k]],obj.description(params[k]), 'ndpnondef', 'black', noerror)
		itsparams[params[k]] = myf[params[k]]
        else:
            subdict=odict(paramval)
            ##printtext is False....called most probably to set
            ##undefined params..no harm in doing it anyways
            if(not printtext):
                ##locate which dictionary is user selected
                userdict={}
                subkeyupdated={}
                for somekey in paramval:
                    somedict=dict(paramval[somekey])
                    subkeyupdated.update(dict.fromkeys(somedict, False))
                    if(somedict.has_key('value') and myf.has_key(params[k])):
                        if(somedict['value']==myf[params[k]]):
                            userdict=somedict
                    elif(somedict.has_key('notvalue') and myf.has_key(params[k])):
                        if(somedict['notvalue']!=myf[params[k]]):
                            userdict=somedict
                ###The behaviour is to use the task.defaults
                ### for all non set parameters and parameters that
                ### have no meaning for this selection
                for j in range(len(subdict)):
                    subkey=subdict[j].keys()
                   
                    for kk in range(len(subkey)):
                        
                        if( (subkey[kk] != 'value') & (subkey[kk] != 'notvalue') ):
                            #if user selecteddict
                            #does not have the key
                            ##put default
                            if(userdict.has_key(subkey[kk])):
                                if(myf.has_key(subkey[kk])):
                                    itsparams.update({subkey[kk]:myf[subkey[kk]]})
                                else:
                                    itsparams.update({subkey[kk]:userdict[subkey[kk]]})
                                subkeyupdated[subkey[kk]]=True
                            elif((not subkeyupdated[subkey[kk]])):
                                itsparams.update({subkey[kk]:itsdef(params[k], None, itsparams[params[k]], subkey[kk])})
                                subkeyupdated[subkey[kk]]=True
            ### need to do default when user has not set val
            if(not myf.has_key(params[k])):
                if(paramval[0].has_key('notvalue')):
                    itsparams.update({params[k]:paramval[0]['notvalue']})
                    myf.update({params[k]:paramval[0]['notvalue']})
                else:
                    itsparams.update({params[k]:paramval[0]['value']})
                    myf.update({params[k]:paramval[0]['value']})
            userval=myf[params[k]]
            choice=0
            notchoice=-1
            valuekey='value'
            for j in range(len(subdict)):
                if(subdict[j].has_key('notvalue')):
                    valuekey='notvalue'
                    if(subdict[j]['notvalue'] != userval):
                        notchoice=j;
                        break
                else:
                    if(subdict[j]['value']==userval):
                        choice=j
                        notchoice=j
                        break
            subkey=subdict[choice].keys()
            if(hascheck):
                noerror=obj.check_params(params[k],userval,myf)
            if(printtext):
                if(myf[params[k]]==paramval[0][valuekey]):
                    print_params_col(params[k],myf[params[k]],obj.description(params[k]),'dpdef','black', noerror)
                else:
                    print_params_col(params[k],myf[params[k]],obj.description(params[k]),'dpnondef','black', noerror)
		itsparams[params[k]] = myf[params[k]]
            for j in range(len(subkey)):
                if((subkey[j] != valuekey) & (notchoice > -1)):
                    ###put default if not there
                    if(not myf.has_key(subkey[j])):
                        myf.update({subkey[j]:subdict[choice][subkey[j]]})
                    paramval=subdict[choice][subkey[j]]
                    if (j==(len(subkey)-1)):
                        # last subparameter - need to add an extra line to allow cut/pasting
                        comment='last'
                    else:
                        comment='blue'
                    if(hascheck):
                        noerror = obj.check_params(subkey[j],myf[subkey[j]],myf)
                    if(printtext):
                        if(myf[subkey[j]]==paramval):
                            print_params_col(subkey[j],myf[subkey[j]],obj.description(subkey[j],userval),'spdef',comment, noerror)
                        else:
                            print_params_col(subkey[j],myf[subkey[j]],obj.description(subkey[j],userval),'spnondef',comment, noerror)
		        itsparams[params[k]] = myf[params[k]]                    
Example #26
0
    def defaults(self,
                 param=None,
                 ipython_globals=None,
                 paramvalue=None,
                 subparam=None):
        if not hasattr(self, "__globals__") or self.__globals__ == None:
            self.__globals__ = stack_frame_find()
        if ipython_globals == None:
            myf = self.__globals__
        else:
            myf = ipython_globals

        a = odict()
        a['IDI'] = ''
        a['OUTPUTIDI'] = ''
        a['DiFXinput'] = ''
        a['DiFXcalc'] = ''
        a['doIF'] = []
        a['linAntIdx'] = [1]
        a['Range'] = []
        a['ALMAant'] = ''
        a['spw'] = -1
        a['calAPP'] = ''
        a['calAPPTime'] = [0., 5.]
        a['APPrefant'] = ''
        a['gains'] = [["NONE"]]
        a['interpolation'] = []
        a['gainmode'] = []
        a['XYavgTime'] = 0.0
        a['dterms'] = ["NONE"]
        a['amp_norm'] = 0.01
        a['XYadd'] = {}
        a['XYdel'] = {}
        a['XYratio'] = {}
        a['usePcal'] = []
        a['swapXY'] = [False]
        a['swapRL'] = True
        a['feedRotation'] = []
        a['correctParangle'] = False
        a['IDI_conjugated'] = False
        a['plotIF'] = -1
        a['plotRange'] = []
        a['plotAnt'] = -1
        a['excludeAnts'] = []
        a['excludeBaselines'] = []
        a['doSolve'] = -1
        a['solint'] = [1, 1]
        a['doTest'] = True
        a['npix'] = 50
        a['solveAmp'] = True
        a['solveMethod'] = 'gradient'
        a['calstokes'] = [1., 0., 0., 0.]
        a['calfield'] = -1

        ### This function sets the default values but also will return the list of
        ### parameters or the default value of a given parameter
        if (param == None):
            myf['__set_default_parameters'](a)
        elif (param == 'paramkeys'):
            return a.keys()
        else:
            if (paramvalue == None and subparam == None):
                if (a.has_key(param)):
                    return a[param]
                else:
                    return self.itsdefault(param)
            else:
                retval = a[param]
                if (type(a[param]) == dict):
                    for k in range(len(a[param])):
                        valornotval = 'value'
                        if (a[param][k].has_key('notvalue')):
                            valornotval = 'notvalue'
                        if ((a[param][k][valornotval]) == paramvalue):
                            retval = a[param][k].copy()
                            retval.pop(valornotval)
                            if (subparam != None):
                                if (retval.has_key(subparam)):
                                    retval = retval[subparam]
                                else:
                                    retval = self.itsdefault(subparam)
                        else:
                            retval = self.itsdefault(subparam)
                return retval
Example #27
0
def saveinputs(taskname=None, outfile='', myparams=None, ipython_globals=None, scriptstr=['']):
    #parameter_printvalues(arg_names,arg_values,arg_types)
    """ Save current input values to file on disk for a specified task:

    taskname -- Name of task
        default: <unset>; example: taskname='bandpass'
        <Options: type tasklist() for the complete list>
    outfile -- Output file for the task inputs
        default: taskname.saved; example: outfile=taskname.orion

    """

    try:
        if ipython_globals == None:
            myf=stack_frame_find( )
        else:
            myf=ipython_globals

        if taskname==None: taskname=myf['taskname']
        myf['taskname']=taskname
        if type(taskname)!=str:
            taskname=taskname.__name__
            myf['taskname']=taskname

        parameter_checktype(['taskname','outfile'],[taskname,outfile],[str,str])

        ###Check if task exists by checking if task_defaults is defined
        obj = False
        if ( not myf.has_key(taskname) and
             str(type(myf[taskname])) != "<type 'instance'>" and
             not hasattr(myf[taskname],"defaults") ):
            raise TypeError, "task %s is not defined " %taskname
        else:
            obj = myf[taskname]

        if taskname==None: taskname=myf['taskname']
        myf['taskname']=taskname
        if outfile=='': outfile=taskname+'.saved'
        if(myf.has_key('__multibackup') and myf['__multibackup']):
            backupoldfile(outfile)
        
        ##make sure unfolded parameters get their default values
        myf['update_params'](func=myf['taskname'], printtext=False, ipython_globals=myf)
        ###
        do_save_inputs = False
        outpathdir = os.path.realpath(os.path.dirname(outfile))
        outpathfile = outpathdir + os.path.sep + os.path.basename(outfile)
        if outpathfile not in casa['state']['unwritable'] and outpathdir not in casa['state']['unwritable']:
            try:
                taskparameterfile=open(outfile,'w')
                print >>taskparameterfile, '%-15s    = "%s"'%('taskname', taskname)
                do_save_inputs = True
            except:
                print "********************************************************************************"
                print "Warning: no write permission for %s, cannot save task" % outfile
                if os.path.isfile(outfile):
                    print "         inputs in %s..." % outpathfile
                    casa['state']['unwritable'].add(outpathfile)
                elif not os.path.isdir(outfile):
                    print "         inputs in dir %s..." % outpathdir
                    casa['state']['unwritable'].add(outpathdir)
                else:
                    print "         inputs because given file (%s) is a dir..." % outpathfile
                print "********************************************************************************"
        f=zip(myf[taskname].__call__.func_code.co_varnames[1:],myf[taskname].__call__.func_defaults)
        scriptstring='#'+str(taskname)+'('
	if myparams == None :
		myparams = {}
        l=0
        for j in range(len(f)):
            k=f[j][0]
	    if not myparams.has_key(k) and k != 'self' :
		    myparams[k] = myf[taskname].parameters[k]
            if(k != 'self' and type(myparams[k])==str):
                if ( myparams[k].count( '"' ) < 1 ):
                    # if the string doesn't contain double quotes then
                    # use double quotes around it in the parameter file.
                    if do_save_inputs:
                        print >>taskparameterfile, '%-15s    =  "%s"'%(k, myparams[k])
                    scriptstring=scriptstring+k+'="'+myparams[k]+'",'
                else:
                    # use single quotes.
                    if do_save_inputs:
                        print >>taskparameterfile, "%-15s    =  '%s'"%(k, myparams[k])
                    scriptstring=scriptstring+k+"='"+myparams[k]+"',"
            else :
                if ( j != 0 or k != "self" or
                     str(type(myf[taskname])) != "<type 'instance'>" ) :
                    if do_save_inputs:
                        print >>taskparameterfile, '%-15s    =  %s'%(k, myparams[k])
                    scriptstring=scriptstring+k+'='+str(myparams[k])+','

            ###Now delete varianle from global user space because
            ###the following applies: "It would be nice if one
            ### could tell the system to NOT recall
            ### previous non-default settings sometimes."
            if(not myf['casaglobals'] and myf.has_key(k)):
                del myf[k]
            l=l+1
            if l%5==0:
                scriptstring=scriptstring+'\n        '
        scriptstring=scriptstring.rstrip()
        scriptstring=scriptstring.rstrip('\n')
        scriptstring=scriptstring.rstrip(',')
        scriptstring=scriptstring+')'        
        scriptstr.append(scriptstring)
        scriptstring=scriptstring.replace('        ', '')
        scriptstring=scriptstring.replace('\n', '')
        if do_save_inputs:
            print >>taskparameterfile,scriptstring
            taskparameterfile.close()
    except TypeError, e:
        print "saveinputs --error: ", e
Example #28
0
def update_params(func, printtext=True, ipython_globals=None):
    from odict import odict

    if ipython_globals == None:
        myf=stack_frame_find( )
    else:
        myf=ipython_globals

    ### set task to the one being called
    myf['taskname']=func
    obj=myf[func]

    if ( str(type(obj)) == "<type 'instance'>" and
         hasattr(obj,"check_params") ):
        hascheck = True
    else:
        hascheck = False

    noerror=True
    ###check if task has defined a task_check_params function

    if (hascheck):
	has_othertasks = myf.has_key('task_location')
	if(has_othertasks) :
	   has_task = myf['task_location'].has_key(myf['taskname'])
	   if (has_task) :
		pathname=myf['task_location'][myf['taskname']]
	   else :
	        pathname = os.environ.get('CASAPATH').split()[0]+'/'+os.environ.get('CASAPATH').split()[1]+'/xml'
                if not os.path.exists(pathname) :
                    pathname = os.environ.get('CASAPATH').split()[0]+'/xml'
                    if not os.path.exists(pathname) :
                        pathname = os.environ.get('CASAPATH').split()[0]+'/Resources/xml'
                
	else :
	   pathname = os.environ.get('CASAPATH').split()[0]+'/'+os.environ.get('CASAPATH').split()[1]+'/xml'
           if not os.path.exists(pathname) :
               pathname = os.environ.get('CASAPATH').split()[0]+'/xml'
               if not os.path.exists(pathname) :
                   pathname = os.environ.get('CASAPATH').split()[0]+'/Resources/xml'
                   if not os.path.exists(pathname) :
                       sys.exit("ERROR: casapy.py update_params() can not locate xml file for task %s" % (taskname))
                       
        xmlfile=pathname+'/'+myf['taskname']+'.xml'
        if(os.path.exists(xmlfile)) :
            cu.setconstraints('file://'+xmlfile);
        else:
            #
            # SRankin: quick fix for CAS-5381 - needs review.
            # The task is not a CASA internal task.  Extract the path from task_location.
            # This may not be robust.  I have not tracked down all the code that could update task_location.
            task_path=task_location[taskname]
            xmlfile=task_path+'/'+myf['taskname']+'.xml'
            if(os.path.exists(xmlfile)) :
                cu.setconstraints('file://'+xmlfile);

    a=myf[myf['taskname']].defaults("paramkeys",myf)
    itsdef=myf[myf['taskname']].defaults
    itsparams=myf[myf['taskname']].parameters
    params=a
    #print 'itsparams:', itsparams
    for k in range(len(params)):
        paramval = obj.defaults(params[k], myf)

        notdict=True
        ###if a dictionary with key 0, 1 etc then need to peel-open
        ###parameters
        if(type(paramval)==dict):
            if(paramval.has_key(0)):
                notdict=False
        if(myf.has_key(params[k])):
            itsparams.update({params[k]:myf[params[k]]})
        else:
            itsparams.update({params[k]:obj.itsdefault(params[k])})
        if (notdict ):
            if(not myf.has_key(params[k])):
                myf.update({params[k]:paramval})
                itsparams.update({params[k]:paramval})
            if(printtext):
                #print 'params:', params[k], '; myf[params]:', myf[params[k]]
                if(hascheck):
                    noerror = obj.check_params(params[k],myf[params[k]],myf)
                # RI this doesn't work with numpy arrays anymore.  Noone seems
                # interested, so I'll be the red hen and try to fix it.
                
                #print 'params:', params[k], '; noerror:', noerror, '; myf[params]:', myf[params[k]]
                myfparamsk=myf[params[k]]
                if(type(myf[params[k]])==np.ndarray):
                    myfparamsk=myfparamsk.tolist()
                #if(myf[params[k]]==paramval):
                if(myfparamsk==paramval):
                    print_params_col(params[k],myf[params[k]],obj.description(params[k]), 'ndpdef', 'black',noerror)
                else:
                    print_params_col(params[k],myf[params[k]],obj.description(params[k]), 'ndpnondef', 'black', noerror)
		itsparams[params[k]] = myf[params[k]]
        else:
            subdict=odict(paramval)
            ##printtext is False....called most probably to set
            ##undefined params..no harm in doing it anyways
            if(not printtext):
                ##locate which dictionary is user selected
                userdict={}
                subkeyupdated={}
                for somekey in paramval:
                    somedict=dict(paramval[somekey])
                    subkeyupdated.update(dict.fromkeys(somedict, False))
                    if(somedict.has_key('value') and myf.has_key(params[k])):
                        if(somedict['value']==myf[params[k]]):
                            userdict=somedict
                    elif(somedict.has_key('notvalue') and myf.has_key(params[k])):
                        if(somedict['notvalue']!=myf[params[k]]):
                            userdict=somedict
                ###The behaviour is to use the task.defaults
                ### for all non set parameters and parameters that
                ### have no meaning for this selection
                for j in range(len(subdict)):
                    subkey=subdict[j].keys()
                   
                    for kk in range(len(subkey)):
                        
                        if( (subkey[kk] != 'value') & (subkey[kk] != 'notvalue') ):
                            #if user selecteddict
                            #does not have the key
                            ##put default
                            if(userdict.has_key(subkey[kk])):
                                if(myf.has_key(subkey[kk])):
                                    itsparams.update({subkey[kk]:myf[subkey[kk]]})
                                else:
                                    itsparams.update({subkey[kk]:userdict[subkey[kk]]})
                                subkeyupdated[subkey[kk]]=True
                            elif((not subkeyupdated[subkey[kk]])):
                                itsparams.update({subkey[kk]:itsdef(params[k], None, itsparams[params[k]], subkey[kk])})
                                subkeyupdated[subkey[kk]]=True
            ### need to do default when user has not set val
            if(not myf.has_key(params[k])):
                if(paramval[0].has_key('notvalue')):
                    itsparams.update({params[k]:paramval[0]['notvalue']})
                    myf.update({params[k]:paramval[0]['notvalue']})
                else:
                    itsparams.update({params[k]:paramval[0]['value']})
                    myf.update({params[k]:paramval[0]['value']})
            userval=myf[params[k]]
            choice=0
            notchoice=-1
            valuekey='value'
            for j in range(len(subdict)):
                if(subdict[j].has_key('notvalue')):
                    valuekey='notvalue'
                    if(subdict[j]['notvalue'] != userval):
                        notchoice=j;
                        break
                else:
                    if(subdict[j]['value']==userval):
                        choice=j
                        notchoice=j
                        break
            subkey=subdict[choice].keys()
            if(hascheck):
                noerror=obj.check_params(params[k],userval,myf)
            if(printtext):
                if(myf[params[k]]==paramval[0][valuekey]):
                    print_params_col(params[k],myf[params[k]],obj.description(params[k]),'dpdef','black', noerror)
                else:
                    print_params_col(params[k],myf[params[k]],obj.description(params[k]),'dpnondef','black', noerror)
		itsparams[params[k]] = myf[params[k]]
            for j in range(len(subkey)):
                if((subkey[j] != valuekey) & (notchoice > -1)):
                    ###put default if not there
                    if(not myf.has_key(subkey[j])):
                        myf.update({subkey[j]:subdict[choice][subkey[j]]})
                    paramval=subdict[choice][subkey[j]]
                    if (j==(len(subkey)-1)):
                        # last subparameter - need to add an extra line to allow cut/pasting
                        comment='last'
                    else:
                        comment='blue'
                    if(hascheck):
                        noerror = obj.check_params(subkey[j],myf[subkey[j]],myf)
                    if(printtext):
                        if(myf[subkey[j]]==paramval):
                            print_params_col(subkey[j],myf[subkey[j]],obj.description(subkey[j],userval),'spdef',comment, noerror)
                        else:
                            print_params_col(subkey[j],myf[subkey[j]],obj.description(subkey[j],userval),'spnondef',comment, noerror)
		        itsparams[params[k]] = myf[params[k]]                    
Example #29
0
def importarchive(
    archivefiles=None,
    vis=None,
    bandname=None,
    freqtol=None,
    ):
    """Convert VLA archive file(s) to a CASA visibility data set (MS):

....Keyword arguments:
....archivefiles -- Name of input VLA archive file(s)
........default: <unset>
........example: archivefiles=['AP314_A950519.xp1','AP314_A950519.xp2']
....vis -- Name of output visibility file (MS)
........default: <unset>; example: vis='NGC7538.ms'
....bandname -- VLA Frequency band
........default: <unset> - all bands; example: bandname='K'
........<Options: '4'=48-96 MHz,'P'=298-345 MHz,'L'=1.15-1.75 GHz,
........'C'=4.2-5.1 GHz,'X'=6.8-9.6 GHz,'U'=13.5-16.3 GHz,
........'K'=20.8-25.8 GHz,'Q'=38-51 GHz>
....#projectname -- Observing project name
....#....default: <unset>; example='AP314'
....freqtol -- Tolerance in frequency shift in naming spectral windows
........default: channel width of current spectral window in Hz
........example: 150000.0
....

...."""

    myf = stack_frame_find( )
    vftask = myf['vftask']
    vlafiller = vftask.fill
#    fg = myf['fg']
    aflocal = casac.agentflagger()
        # ##
        # Handle globals or user over-ride of arguments
    function_signature_defaults = \
        dict(zip(importarchive.func_code.co_varnames,
             importarchive.func_defaults))
    for item in function_signature_defaults.iteritems():
        (key, val) = item
        keyVal = eval(key)
        if keyVal == None:
                        # user hasn't set it - use global/default
            pass
        else:
                        # user has set it - use over-ride
            myf[key] = keyVal

    archivefiles = myf['archivefiles']
    vis = myf['vis']
    bandname = myf['bandname']
    freqtol = myf['freqtol']
        #
        # ##

    # Add type/menu/range error checking here
    if type(archivefiles) == str:
        archivefiles = [archivefiles]
    arg_names = ['archivefiles', 'vis', 'bandname', 'freqtol']
    arg_values = [archivefiles, vis, bandname, freqtol]
    arg_types = [list, str, str, float]
    try:
        parameter_checktype(arg_names, arg_values, arg_types)
        parameter_checkmenu('bandname', bandname, [
            '4',
            'P',
            'L',
            'C',
            'X',
            'U',
            'K',
            'Q',
            ])
    except TypeError, e:
        print 'importarchive -- TypeError: ', e
        return
Example #30
0
    def __call__( self, raster=None, contour=None, zoom=None, axes=None, out=None ):
        """ Old parameters:
               infile=None,displaytype=None,channel=None,zoom=None,outfile=None,
               outscale=None,outdpi=None,outformat=None,outlandscape=None,gui=None
        The imview task will display images in raster, contour, vector or
        marker form.  Images can be blinked, and movies are available
        for spectral-line image cubes.  For measurement sets, many
        display and editing options are available.

        examples of usage:

        imview
        imview "myimage.im"
        imview "myrestorefile.rstr"
        
        imview "myimage.im", "contour"

        imview "'myimage1.im' - 2 * 'myimage2.im'", "lel"
    
        Executing imview( ) will bring up a display panel
        window, which can be resized.  If no data file was specified,
        a Load Data window will also appear.  Click on the desired data
        file and choose the display type; the rendered data should appear
        on the display panel.

        A Data Display Options window will also appear.  It has drop-down
        subsections for related options, most of which are self-explanatory.
      
        The state of the imview task -- loaded data and related display
        options -- can be saved in a 'restore' file for later use.
        You can provide the restore filename on the command line or
        select it from the Load Data window.

        See the cookbook for more details on using the imview task.
    
        Keyword arguments:
        infile -- Name of file to visualize
            default: ''
            example: infile='ngc5921.image'
            If no infile is specified the Load Data window
            will appear for selecting data.
        displaytype -- (optional): method of rendering data
            visually (raster, contour, vector or marker).  
            You can also set this parameter to 'lel' and
            provide an lel expression for infile (advanced).
            default: 'raster'
            example: displaytype='contour'

        Note: the filetype parameter is optional; typing of
                data files is now inferred.
                example:  imview infile='my.im'
            implies:  imview infile='my.im', filetype='raster'
        the filetype is still used to load contours, etc.


        """
        myf=stack_frame_find( )
        vi = myf['vi'] if myf.has_key('vi') else None
        ving = myf['ving'] if myf.has_key('ving') else None

        casalog.origin('imview')
        
        if (type(out) == str and len(out) != 0) or \
               (type(out) == dict and len(out) != 0) :
            gui = False
            (out_file, out_format, out_scale, out_dpi, out_orientation) = self.__extract_outputinfo( out )
        else:
            gui = True

        if gui and self.local_vi is None or \
           not gui and self.local_ving is None:
            try:
                ## vi/ving might not be defined in taskinit if
                ## loading directly from python via casa.py...
                vwr = vi if gui else ving

                if type(vwr) == type(None) or type(vwr.cwd( )) != str:
                    vwr = viewertool.viewertool( gui, True, (type(myf) == dict and myf.has_key('casa') and type(myf['casa']) == type(os)) )
            except:
                vwr = None

            if gui:
                self.local_vi = vwr
            else:
                self.local_ving = vwr
        else:
            vwr = self.local_vi if gui else self.local_ving

        if type(vwr) == type(None):
            casalog.post( "failed to find a viewertool...", 'SEVERE')
            raise Exception, "failed to find a viewertool..."

        self.__pushd( vwr, os.path.abspath(os.curdir) )

        if (raster is None or len(raster) == 0) and \
           (contour is None or len(contour) == 0) :
            panel = self.__panel(vwr)
        else:
            panel = self.__load_files( "raster", vwr, None, raster )
            panel = self.__load_files( "contour", vwr, panel, contour )
            
        self.__set_axes( vwr, panel, axes )
        self.__zoom( vwr, panel, zoom )
        self.__process_colorwedges( vwr, panel )
        #vwr.unfreeze( panel )

        if not gui:
            vwr.output(out,scale=out_scale,dpi=out_dpi,format=out_format,orientation=out_orientation,panel=panel)
            vwr.close(panel)

        self.__popd( vwr )

        return None
Example #31
0
#
# User defined tasks setup.
# Generated from buildmytask.
#

import sys
from casa_stack_manip import stack_frame_find

if sys.path[1] != '/home/marti/WORKAREA/ARC_TOOLS/PolConvert/LaunchPad/trunk':
    sys.path.insert(
        1, '/home/marti/WORKAREA/ARC_TOOLS/PolConvert/LaunchPad/trunk')
from odict import odict
if not globals().has_key('mytasks'):
    mytasks = odict()

mytasks[
    'polconvert'] = '\n\nVersion 1.7.3\n\nConverts VLBI visibilities from mixed-polarization basis (i.e., linear-to-circular) into circular basis. Works with single VLBI stations as well as with phased arrays (i.e., phased ALMA).\n\n'

if not globals().has_key('task_location'):
    task_location = odict()

task_location[
    'polconvert'] = '/home/marti/WORKAREA/ARC_TOOLS/PolConvert/LaunchPad/trunk'
myglobals = stack_frame_find()
tasksum = myglobals['tasksum']
for key in mytasks.keys():
    tasksum[key] = mytasks[key]

from polconvert_cli import polconvert_cli as polconvert
Example #32
0
    def __launch( self ):

        ## if we've already launched the viewer
        if type(self.__state['launched']) == bool and self.__state['launched'] == True:
            return

        if dbus_connection( ) == None:
            raise Exception, "dbus is not available; cannot script the viewer"

        myf=stack_frame_find( )
        self.__rgm = myf['casac'].regionmanager()

        viewer_path = None
        if type(myf) == dict and myf.has_key('casa') and type(myf['casa']) == dict :
            if myf['casa'].has_key('helpers') \
               and type(myf['casa']['helpers']) == dict and myf['casa']['helpers'].has_key('viewer'):
                viewer_path = myf['casa']['helpers']['viewer']   #### set in casapy.py
                if len(os.path.dirname(viewer_path)) == 0:
                    for dir in os.getenv('PATH').split(':') :
                        dd = dir + os.sep + viewer_path
                        if os.path.exists(dd) and os.access(dd,os.X_OK) :
                            viewer_path = dd
                            break
                args = [ viewer_path, "--casapy" ]
            else:
                for exe in ['casaviewer']:
                    for dir in os.getenv('PATH').split(':') :
                        dd = dir + os.sep + exe
                        if os.path.exists(dd) and os.access(dd,os.X_OK) :
                            viewer_path = dd
                            break
                    if viewer_path is not None:
                        break
                args = [ viewer_path ]

            if myf['casa'].has_key('state') and myf['casa']['state'].has_key('init_version'):
                if myf['casa']['state']['init_version'] <= 0:
                    args += [ '--daemon' ]
            else:
                args += [ '--daemon' ]


        if viewer_path == None or not os.access(viewer_path,os.X_OK):
            raise RuntimeError("cannot find casa viewer executable")

        if self.__state['gui']:
            args += [ '--server=' + self.__state['dbus name'] ]
        else:
            args += [ '--nogui=' + self.__state['dbus name'] ]

        if type(myf) == dict and myf.has_key('casa') and type(myf['casa']) == dict and myf['casa'].has_key('files') \
                and type(myf['casa']['files']) == dict and myf['casa']['files'].has_key('logfile'):
            args += [ '--casalogfile=' + myf['casa']['files']['logfile'] ]

        if  type(myf) == dict and myf.has_key('casa') and type(myf['casa']) == dict and myf['casa'].has_key('flags') \
                 and type(myf['casa']['flags']) == dict and myf['casa']['flags'].has_key('--rcdir'):
            args += [ "--rcdir=" + myf['casa']['flags']['--rcdir'] ]

	if (os.uname()[0]=='Darwin'):
            if myf['casa']['state']['init_version'] > 0:
                from casa_system import procmgr
                procmgr.create(self.__state['dbus name'],args)
            else:
		vwrpid=os.spawnvp( os.P_NOWAIT, viewer_path, args )
	elif (os.uname()[0]=='Linux'):
            if myf['casa']['state']['init_version'] > 0:
                from casa_system import procmgr
                procmgr.create(self.__state['dbus name'],args)
            else:
		vwrpid=os.spawnlp( os.P_NOWAIT, viewer_path, *args )
	else:
        	raise Exception,'unrecognized operating system'

        self.__state['launched'] = True
Example #33
0
import string
import inspect
import sys
import os
import shutil
from casa_stack_manip import stack_frame_find

myf=stack_frame_find( )
casalog=myf['casalog']

#
# Utils
#
def note(message, priority="INFO", origin="regression_utility", ntime=None, postcli='F'):
    #if not ntime:  #if (ntime==None):
    #    ntime=time.asctime()
    #print ntime, priority, origin, message
    if postcli: print message
    casalog.postLocally(message, priority, origin)
###
def info(message):
    #note(message,origin='regression_utility')
    #print message
    casalog.postLocally(message, priority="INFO", origin="regression_utility")

def fail(message=""):
    casalog.postLocally(message, priority="SEVERE", origin='regression_utility')
    #print message
    raise RuntimeError, message

###
Example #34
0
	def __call__(self, infile=None,displaytype=None,channel=None,zoom=None,outfile=None,outscale=None,outdpi=None,outformat=None,outlandscape=None,gui=None):
		""" The viewer will display images in raster, contour, vector or
		marker form.  Images can be blinked, and movies are available
		for spectral-line image cubes.  For measurement sets, many
		display and editing options are available.

		examples of usage:

		viewer
		viewer "myimage.im"
		viewer "mymeasurementset.ms"
		viewer "myrestorefile.rstr"
		
		viewer "myimage.im", "contour"

		viewer "'myimage1.im' - 2 * 'myimage2.im'", "lel"
	
		The viewer can be run outside of casapy by typing <casaviewer>.

		Executing viewer <viewer> will bring up a display panel
		window, which can be resized.  If no data file was specified,
		a Load Data window will also appear.  Click on the desired data
		file and choose the display type; the rendered data should appear
		on the display panel.

		A Data Display Options window will also appear.  It has drop-down
		subsections for related	options, most of which are self-explanatory.
	  
		The state of the viewer -- loaded data and related display
		options -- can be saved in a 'restore' file for later use.
		You can provide the restore filename on the command line or
		select it from the Load Data window.

		See the cookbook for more details on using the viewer.
	
		Keyword arguments:
		infile -- Name of file to visualize
			default: ''
			example: infile='ngc5921.image'
			If no infile is specified the Load Data window
			will appear for selecting data.
		displaytype -- (optional): method of rendering data
			visually (raster, contour, vector or marker).  
			You can also set this parameter to 'lel' and
			provide an lel expression for infile (advanced).
			default: 'raster'
			example: displaytype='contour'

		Note: there is no longer a filetype parameter; typing of
		data files is now done automatically.
	        	example:  viewer infile='my.ms'
			obsolete: viewer infile='my.ms', filetype='ms'


		"""
                myf=stack_frame_find( )
                vi = myf['vi'] if myf.has_key('vi') else None
                ving = myf['ving'] if myf.has_key('ving') else None

		#Python script
		try:
			vwr = vi
			if type(gui) == bool and gui == False:
				vwr = ving

			if type(vwr.cwd( )) != str:
				vwr = None
		except:
			vwr = None

		if type(vwr) == type(None):
			need_gui = True
			if type(gui) == bool and gui == False:
				need_gui = False

			if need_gui :
				if self.local_vi is not None:
					vwr = self.local_vi
				else:
					vwr = viewertool.viewertool( True, True, (type(myf) == dict and myf.has_key('casa') and type(myf['casa']) == type(os)) )
					self.local_vi = vwr
			else:
				if self.local_ving is not None:
					vwr = self.local_ving
				else:
					vwr = viewertool.viewertool( False, True, (type(myf) == dict and myf.has_key('casa') and type(myf['casa']) == type(os)) )
					self.local_ving = vwr

		if type(vwr) != type(None) :
			##
			## (1) save current *viewer*server* path
			## (2) have viewer() task follow casapy/python's cwd
			try:
				old_path = vwr.cwd( )
			except:
				raise Exception, "viewer() failed to get the current working directory [" + str(sys.exc_info()[0]) + ": " + str(sys.exc_info()[1]) + "]"

			try:
				vwr.cwd(os.path.abspath(os.curdir))
			except:
				raise Exception, "viewer() failed to change to the new working directory (" + os.path.abspath(os.curdir) + ") [" + str(sys.exc_info()[0]) + ": " + str(sys.exc_info()[1]) + "]"

			panel = vwr.panel("viewer")
			data = None
			if type(infile) == str and len(infile) > 0 :
				if type(displaytype) == str:
					data = vwr.load( infile, displaytype, panel=panel )
				else:
					data = vwr.load( infile, panel=panel )

				if type(channel) == int and channel > 0 :
					vwr.channel(channel,panel=panel)
				if type(zoom) == int and zoom != 1 :
					vwr.zoom(zoom,panel=panel)
				if type(outfile) == str and len(outfile) > 0 :
					scale=1.0
					if type(outscale) == float :
						scale=outscale
					dpi=300
					if type(outdpi) == int :
						dpi=outdpi
					format="jpg"
					if type(outformat) == str :
						format=outformat
					orientation="portrait"
					if type(outlandscape) == bool and outlandscape :
						orientation="landscape"
					vwr.output(outfile,scale=scale,dpi=dpi,format=format,orientation=orientation,panel=panel)
			else:
				vwr.popup( 'open', panel=panel )


			# it makes no sense to leave a panel open with no way of interacting with it
			if type(gui) == bool and not gui:
				vwr.close(panel)

			## (3) restore original path
			try:
				vwr.cwd(old_path)
			except:
				raise Exception, "viewer() failed to restore the old working directory (" + old_path + ") [" + str(sys.exc_info()[0]) + ": " + str(sys.exc_info()[1]) + "]"

		else:
			viewer_path = myf['casa']['helpers']['viewer']   #### set in casapy.py
			args = [ viewer_path ]

			if type(infile) == str:
				if type(displaytype) == str:
					args += [ infile, displaytype ]
				else:
					args += [ infile ]

			if (os.uname()[0]=='Darwin'):
				vwrpid=os.spawnvp( os.P_NOWAIT, viewer_path, args )
			elif (os.uname()[0]=='Linux'):
				vwrpid=os.spawnlp( os.P_NOWAIT, viewer_path, *args )
			else:
				print 'Unrecognized OS: No viewer available'

		return None
Example #35
0
def pointcal(vis=None,
             model=None,
             caltable=None,
             field=None,
             spw=None,
             selectdata=None,
             timerange=None,
             uvrange=None,
             antenna=None,
             scan=None,
             msselect=None,
             solint=None):
    """Solve for pointing error calibration:

	This program is under development.  Please do not use.

	Keyword arguments:
	vis -- Name of input visibility file (MS)
		default: <unset>; example: vis='ngc5921.ms'
	model -- Name of input model (component list or image)
		default: <unset>; example: model='ngc5921.im'
	caltable -- Name of output Pointing calibration table
		default: <unset>; example: caltable='ngc5921.gcal'

	--- Data Selection (see help par.selectdata for more detailed information)

       field -- Select field using field id(s) or field name(s).
                  [run listobs to obtain the list id's or names]
               default: ''=all fields
               If field string is a non-negative integer, it is assumed a field index
                 otherwise, it is assumed a field name
               field='0~2'; field ids 0,1,2
               field='0,4,5~7'; field ids 0,4,5,6,7
               field='3C286,3C295'; field named 3C286 adn 3C295
               field = '3,4C*'; field id 3, all names starting with 4C
       spw -- Select spectral window/channels
               default: ''=all spectral windows and channels
               spw='0~2,4'; spectral windows 0,1,2,4 (all channels)
               spw='<2';  spectral windows less than 2 (i.e. 0,1)
               spw='0:5~61'; spw 0, channels 5 to 61
               spw='0,10,3:3~45'; spw 0,10 all channels, spw 3, channels 3 to 45.
               spw='0~2:2~6'; spw 0,1,2 with channels 2 through 6 in each.
               spw='0:0~10;15~60'; spectral window 0 with channels 0-10,15-60
               spw='0:0~10,1:20~30,2:1;2;3'; spw 0, channels 0-10,
                        spw 1, channels 20-30, and spw 2, channels, 1,2 and 3

       selectdata -- Other data selection parameters
               default: True
       timerange  -- Select data based on time range:
               default = '' (all); examples,
               timerange = 'YYYY/MM/DD/hh:mm:ss~YYYY/MM/DD/hh:mm:ss'
               Note: if YYYY/MM/DD is missing dat defaults to first day in data set
               timerange='09:14:0~09:54:0' picks 40 min on first day
               timerange= '25:00:00~27:30:00' picks 1 hr to 3 hr 30min on next day
               timerange='09:44:00' data within one integration of time
               timerange='>10:24:00' data after this time
       uvrange -- Select data within uvrange (default units kilo-lambda)
               default: '' (all); example:
               uvrange='0~1000'; uvrange from 0-1000 kilo-lamgda
               uvrange='>4';uvranges greater than 4 kilo lambda
       antenna -- Select data based on antenna/baseline
               default: '' (all)
               If antenna string is a non-negative integer, it is assumed an antenna index
                 otherwise, it is assumed as an antenna name
               antenna='5&6'; baseline between antenna index 5 and index 6.
               antenna='VA05&VA06'; baseline between VLA antenna 5 and 6.
               antenna='5&6;7&8'; baseline 5-6 and 7-8
               antenna='5'; all baselines with antenna 5
               antenna='5,6,10'; all baselines with antennas 5 and 6
       scan -- Scan number range - New, under developement
       msselect -- Optional complex data selection (ignore for now)

	solint --  Solution interval (sec)
		default: 0.0 (scan based); example: solint=60.

	"""
    myf = stack_frame_find()
    myf['taskname'] = 'pointcal'
    ###fill unfilled parameters with defaults
    myf['update_params'](func=myf['taskname'], printtext=False)
    ####local params
    cb = myf['cb']
    selectantenna = myf['selectantenna']
    ###
    #Handle globals or user over-ride of arguments
    #
    function_signature_defaults = dict(
        zip(pointcal.func_code.co_varnames, pointcal.func_defaults))
    for item in function_signature_defaults.iteritems():
        key, val = item
        keyVal = eval(key)
        if (keyVal == None):
            #user hasn't set it - use global/default
            pass
        else:
            myf[key] = keyVal
        #user has set it - use over-ride

    vis = myf['vis']
    model = myf['model']
    caltable = myf['caltable']
    field = myf['field']
    spw = myf['spw']
    selectdata = myf['selectdata']
    if (selectdata):
        timerange = myf['timerange']
        uvrange = myf['uvrange']
        antenna = myf['antenna']
        scan = myf['scan']
        msselect = myf['msselect']
    else:
        timerange = ''
        uvrange = ''
        antenna = ''
        scan = ''
        msselect = ''
    solint = myf['solint']

    #Add type/menu/range error checking here
    arg_names = [
        'vis', 'model', 'caltable', 'field', 'spw', 'selectdata', 'timerange',
        'uvrange', 'antenna', 'scan', 'msselect', 'solint'
    ]
    arg_values = [
        vis, model, caltable, field, spw, selectdata, timerange, uvrange,
        antenna, scan, msselect, solint
    ]
    arg_types = [str, str, str, str, str, bool, str, str, str, str, str, float]
    try:
        parameter_checktype(arg_names, arg_values, arg_types)
    except TypeError, e:
        print "pointcal -- TypeError: ", e
        return
Example #36
0
def csvclean(vis, imagename,field, spw, advise, mode, nchan, width, imsize, cell, phasecenter, niter, weighting, restoringbeam, interactive):

    """ This task does an invert of the visibilities and deconvolve in the
	    image plane. It does not do a uvdata subtraction (aka Cotton-Schwab
		major cycle) of model visibility as in clean. - For ALMA Commissioning
    
         vis -- Name of input visibility file
               default: none; example: vis='ngc5921.ms'    
    
	     imagename -- Name of output CASA image. (only the prefix)
                   default: none; example: imagename='m2'
                   output images are:
                 m2.image; cleaned and restored image
                        With or without primary beam correction
                 m2dirty.image; dirty image
                 m2psf.image; point-spread function (dirty beam)
                 m2.model; image of clean components
                 m2.mask; image containing clean regions, when interative=True

    
         field -- Select fields in MS.  Use field id(s) or field name(s).
                    ['go listobs' to obtain the list id's or names]
                default: ''= all fields
                If field string is a non-negative integer, it is assumed to
                    be a field index otherwise, it is assumed to be a 
                    field name
                field='0~2'; field ids 0,1,2
                field='0,4,5~7'; field ids 0,4,5,6,7
                field='3C286,3C295'; field named 3C286 and 3C295
                field = '3,4C*'; field id 3, all names starting with 4C
    
         spw --Select spectral window/channels
                NOTE: This selects the data passed as the INPUT to mode
                default: ''=all spectral windows and channels
                  spw='0~2,4'; spectral windows 0,1,2,4 (all channels)
                  spw='0:5~61'; spw 0, channels 5 to 61
                  spw='<2';   spectral windows less than 2 (i.e. 0,1)
                  spw='0,10,3:3~45'; spw 0,10 all channels, spw 3, 
                                     channels 3 to 45.
                  spw='0~2:2~6'; spw 0,1,2 with channels 2 through 6 in each.
                  spw='0:0~10;15~60'; spectral window 0 with channels 
                                      0-10,15-60
                  spw='0:0~10,1:20~30,2:1;2;3'; spw 0, channels 0-10,
                        spw 1, channels 20-30, and spw 2, channels, 1,2 and 3

        imsize -- Image pixel size (x,y).  DOES NOT HAVE TO BE A POWER OF 2
                   default = [256,256]; example: imsize=[350,350]
                   imsize = 500 is equivalent to [500,500]
                   Avoid odd-numbered imsize.
    
        cell -- Cell size (x,y)
                   default= '1.0arcsec';
                   example: cell=['0.5arcsec,'0.5arcsec'] or
                   cell=['1arcmin', '1arcmin']
                   cell = '1arcsec' is equivalent to ['1arcsec','1arcsec']
                   NOTE:cell = 2.0 => ['2arcsec', '2arcsec']

        phasecenter -- direction measure  or fieldid for the mosaic center
                   default: '' => first field selected ; example: phasecenter=6
                   or phasecenter='J2000 19h30m00 -40d00m00'
    
        niter -- Maximum number iterations,
                   if niter=0, then no CLEANing is done ("invert" only)
                   default: 500; example: niter=5000
    
        weighting -- Weighting to apply to visibilities:
                   default='natural'; example: weighting='uniform';
                   Options: 'natural','uniform','briggs', 
                           'superuniform','briggsabs','radial'
    
        restoringbeam -- Output Gaussian restoring beam for CLEAN image
                   [bmaj, bmin, bpa] elliptical Gaussian restoring beam
                   default units are in arc-seconds for bmaj,bmin, degrees
                   for bpa default: restoringbeam=[]; Use PSF calculated
                   from dirty beam. 
                   example: restoringbeam=['10arcsec'] or restorinbeam='10arcsec', circular Gaussian.
                            FWHM 10 arcseconds example:
                            restoringbeam=['10.0','5.0','45.0deg'] 10"x5" 
                            at 45 degrees
        
	    interactive -- Create a mask interactively or not.
        		   default=False; example: interactive=True
        		   The viewer will open with the image displayed. Select the
        		   region for the mask and double click in the middle of it.
            
    """
    

    #Python script    
    
    try:

        casalog.origin('csvclean')
        ms = casac.ms()
        

        parsummary = 'vis="'+str(vis)+'", imagename="'+str(imagename)+'", '
        parsummary += 'field="'+str(field)+'", spw="'+str(spw)+'", '
        parsummary = 'cell="'+str(cell)+'",'
        parsummary = 'phasecenter='+str(phasecenter)+','
        parsummary += 'imsize='+str(imsize)+', niter='+str(niter)+', '
        parsummary += 'weighting="'+str(weighting)+'", '
        parsummary += 'restoringbeam="'+str(restoringbeam)+'", '
        parsummary += 'interactive='+str(interactive)+''
        casalog.post(parsummary,'INFO')    
        
#        if (not (type(vis)==str) & (os.path.exists(vis))):
#            raise Exception, 'Visibility data set not found - please verify the name'
        if ((type(vis)==str) & (os.path.exists(vis))):
            ms.open(vis)
        else:
            raise Exception, 'Visibility data set not found - please verify the name'
        if(not advise):
            if (imagename == ""):
                #            ms.close()
                raise Exception, "Must provide output image name in parameter imagename."            
        
            if os.path.exists(imagename):
                #            ms.close()
                raise Exception, "Output image %s already exists - will not overwrite." % imagename
           
        if (field == ''):
        	field = '*'
        	
        if (spw == ''):
        	spw = '*'

        if ((type(imsize)==int)):
            imsize=[imsize,imsize]
    
        if ((len(imsize)==1)): 
            imsize=[imsize[0],imsize[0]]
        
        nx = imsize[0]
        ny = imsize[1]
      
        if ((type(cell)==int) | (type(cell)==float) | (type(cell)==str)):
            cell=[cell,cell]
               
        if ((len(cell)==1)):
            cell=[cell[0],cell[0]]

        cellx=cell[0]
        celly=cell[1]
        if((type(cell[0])==int) or (type(cell[0])==float)):
            cellx=qa.quantity(cell[0], 'arcsec')
            celly=qa.quantity(cell[1], 'arcsec')

        if(type(phasecenter)==str):
            ### blank means take field[0]
            if (phasecenter==''):
                fieldoo=field
                if(fieldoo==''):
                    fieldoo='0'
                phasecenter=int(ms.msseltoindex(vis,field=fieldoo)['field'][0])
            else:
                tmppc=phasecenter
                try:
                    if(len(ms.msseltoindex(vis, field=phasecenter)['field']) > 0):
                        tmppc = int(ms.msseltoindex(vis,
                                                    field=phasecenter)['field'][0])
                    ##succesful must be string like '0' or 'NGC*'
                except Exception, instance:
                    #failed must be a string type J2000 18h00m00 10d00m00
                    tmppc = phasecenter
                phasecenter = tmppc
                

        if restoringbeam == [''] or len(restoringbeam) == 0:
        	# calculate from fit below
            bmaj = ''
            bmin = ''
            bpa = ''
        else:        	
	        if (type(restoringbeam)==str):
	            restoringbeam=[restoringbeam,restoringbeam,'0deg']
	        if (type(restoringbeam)==list and (len(restoringbeam)==1)):
	            restoringbeam=[restoringbeam[0],restoringbeam[0],'0deg']
	        if (type(restoringbeam)==list and (len(restoringbeam)==2)):
	            restoringbeam=[restoringbeam[0],restoringbeam[1],'0deg']
	        if (type(restoringbeam)==list and (len(restoringbeam)==2)):
	            restoringbeam=[restoringbeam[0],restoringbeam[1],restoringbeam[2]]
	
	        if(qa.quantity(restoringbeam[0])['unit'] == ''):
	        	restoringbeam[0]=restoringbeam[0]+'arcsec'
	        if(qa.quantity(restoringbeam[1])['unit'] == ''):
	        	restoringbeam[1]=restoringbeam[1]+'arcsec'
	        if(qa.quantity(restoringbeam[2])['unit'] == ''):
	        	restoringbeam[2]=restoringbeam[2]+'deg'
	        
	        bmaj = restoringbeam[0]
	        bmin = restoringbeam[1]
	        bpa = restoringbeam[2]
                   
        # Create output names based on imagename parameter
        dirtyim = imagename+'dirty.image'
        psfim = imagename+'psf.image'
        modelname = imagename+'.model'
        imname = imagename+'.image'

        # Make sure all tables and images are closed
#        ms.close()
        
        # Add scratch columns if they don't exist
        #tb.open(vis)
        #hasit = tb.colnames().count('CORRECTED_DATA')>0
        #tb.close()
        #if not hasit:
        #	cb.open(vis)
        #	cb.close()
        		
        # make the dirty image and psf

        im.open(vis, usescratch=True)
        im.selectvis(spw=spw, field=field)
        spwsel=ms.msseltoindex(vis=vis, spw=spw)['spw']
        ch=ms.msseltoindex(vis=vis, spw=spw)['channel']
        if(nchan < 1):
            nchan=0
            for k in range(len(spwsel)):
                nchan += ch[k,2]-ch[k,1]+1
            nchan=nchan/width
            if(nchan < 1):
                nchan=1
        if(advise):
            tb.open(vis+'/SPECTRAL_WINDOW')
            allreffreq=tb.getcol('REF_FREQUENCY')
            reffreq=0.0
            if(len(allreffreq) > 1):
                reffreq=0.0;
                for f in  allreffreq:
                    reffreq+=f
                reffreq=reffreq/float(len(allreffreq))
            else:
                reffreq=allreffreq[0]
            tb.done()
            tb.open(vis+'/ANTENNA')
            diams=tb.getcol('DISH_DIAMETER')
            diam=np.min(diams)
            tb.done()
            fov=qa.quantity(3.0e8/reffreq/diam, 'rad')
            adv=im.advise(fieldofview=fov)
            cellx=qa.tos(adv[2], prec=4)
            celly=cellx
            myf = stack_frame_find( )
            myf['cell']=[cellx,cellx]
            myf['imsize']=[adv[1], adv[1]]
            nx=ny=adv[1]
            myf['advise']=False
            return
        redopsf=True
        redokounter=0
        immode='mfs'
        start=ch[0,1]
        if(mode=='cube'):
            immode='channel'
            if(width >1):
                start=start+ width/2
        while(redopsf):
            im.defineimage(nx=nx, ny=ny, cellx=cellx, celly=celly, phasecenter=phasecenter, spw=spwsel.tolist(), mode=immode, start=start, step=width, nchan=nchan)
            im.weight(weighting)
            try:
                im.makeimage(type='corrected', image=dirtyim)
                im.makeimage(type='psf', image=psfim)
                ###make an empty model
                im.make(modelname)
                if((redokounter==2) and (np.min(nx,ny) > 25)):
                    #pdb.set_trace()
                    ia.open(psfim)
                    csys=ia.coordsys()
                    rg.setcoordinates(csys=csys.torecord())
                    shp=ia.shape()
                    blc=['10pix', '10pix', '0pix', '0pix']
                    trc=[str(shp[0]-10)+'pix',str(shp[1]-10)+'pix',
                         str(shp[2]-1)+'pix', str(shp[3]-1)+'pix']
                    reg=rg.wbox(blc=blc, trc=trc)
                    ia.set(pixels=0, region=rg.complement(reg))
                    ia.done()
                #im.done()

                # Calculate bpa, bmin, bmaj if not given
                if restoringbeam == [''] or len(restoringbeam) == 0:
                    cx = nx/2
                    cy = ny/2
                    box = ''

                    #if (nx > 100 and ny > 100):
                    #    rrg = [cx-10, cy-10, cx+10, cy+10]
                    #    box = '%s,%s,%s,%s'%(rrg[0],rrg[1],rrg[2],rrg[3])
                    #ia.open(psfim)
                    #shp=ia.shape()
                    #coords = ia.fitcomponents(box=box)
                    #ia.close()
                    #if(coords['converged'] == True):
                coords=im.fitpsf(psfim)
                if(coords[0]):
                    bmaj=coords[1]
                    bmin=coords[2]
                    bpa=coords[3]
                    redopsf=False
                else:
                    redopsf=True
                    nx=nx+1
                    ny=ny+1
                    redokounter += 1 
                    if(redokounter==3):
                        casalog.post('Failed to find a decent psf','SEVERE')
                        return False
                    else:
                        casalog.post('Trying new image with 1 extra pixel','WARN')
                        
            except :
            	redopsf=True
                nx=nx+1
                ny=ny+1
                redokounter += 1
                if(redokounter==3):
                    casalog.post('Failed to find a decent psf','SEVERE')
                    im.done()
                    return False
                else:
                    casalog.post('Trying new image with 1 extra pixel','WARN')
        im.done()            
        parsummary = 'restoringbeam values = [\'%s\',\'%s\',\'%s\']'%(qa.tos(bmaj),qa.tos(bmin),qa.tos(bpa))
        casalog.post(parsummary,'INFO')
        
        # Make a mask
        maskname=''
        if(interactive):
            maskname=imagename+'.mask'
            if(os.path.exists(maskname)):
                ia.open(dirtyim)
                csys=ia.coordsys()
                shp=ia.shape()
                ia.done()
                ia.open(maskname)
                ia.regrid(outfile='__tmpmask__', shape=shp, csys=csys.torecord(), axes=[0,1], asvelocity=False)
                ia.remove(True)
                ia.done()
                shutil.move('__tmpmask__', maskname)
            im.drawmask(dirtyim, maskname)

        # use deconvolver to do image plane deconvolution
        # using a mask image as the mask
        dc.open(dirty=dirtyim, psf=psfim)
        # NOTE: use the parameter mask which can be an empty
        #       string if no mask
        dc.clean(niter=niter, model=modelname, mask=maskname)
        
        # create the restored image
        if restoringbeam == [''] or len(restoringbeam) == 0:
            dc.restore(model=modelname, image=imname, bmaj=bmaj, bmin=bmin, bpa=bpa)
        else:
            dc.restore(model=modelname, image=imname, bmaj=restoringbeam[0], bmin=restoringbeam[1], bpa=restoringbeam[2])  
			
        dc.done()  
        return True
Example #37
0
    def __launch( self ):

        ## if we've already launched the viewer
        if type(self.__state['launched']) == bool and self.__state['launched'] == True:
            return

        if dbus_connection( ) == None:
            raise Exception, "dbus is not available; cannot script the viewer"

        myf=stack_frame_find( )
        self.__rgm = __casac__.regionmanager.regionmanager( )

        viewer_path = None
        if type(myf) == dict and myf.has_key('casa') and type(myf['casa']) == dict :
            if myf['casa'].has_key('helpers') \
               and type(myf['casa']['helpers']) == dict and myf['casa']['helpers'].has_key('viewer'):
                viewer_path = myf['casa']['helpers']['viewer']   #### set in casapy.py
                if len(os.path.dirname(viewer_path)) == 0:
                    for dir in os.getenv('PATH').split(':') :
                        dd = dir + os.sep + viewer_path
                        if os.path.exists(dd) and os.access(dd,os.X_OK) :
                            viewer_path = dd
                            break
                args = [ viewer_path, "--casapy" ]
            else:
                for exe in ['casaviewer']:
                    for dir in os.getenv('PATH').split(':') :
                        dd = dir + os.sep + exe
                        if os.path.exists(dd) and os.access(dd,os.X_OK) :
                            viewer_path = dd
                            break
                    if viewer_path is not None:
                        break
                args = [ viewer_path ]

            if myf['casa'].has_key('state') and myf['casa']['state'].has_key('init_version'):
                if myf['casa']['state']['init_version'] <= 0:
                    args += [ '--daemon' ]
            else:
                args += [ '--daemon' ]


        if viewer_path == None or not os.access(viewer_path,os.X_OK):
            raise RuntimeError("cannot find casa viewer executable")

        if self.__state['gui']:
            args += [ '--server=' + self.__state['dbus name'] ]
        else:
            args += [ '--nogui=' + self.__state['dbus name'] ]

        if type(myf) == dict and myf.has_key('casa') and type(myf['casa']) == dict and myf['casa'].has_key('files') \
                and type(myf['casa']['files']) == dict and myf['casa']['files'].has_key('logfile'):
            args += [ '--casalogfile=' + myf['casa']['files']['logfile'] ]

        if  type(myf) == dict and myf.has_key('casa') and type(myf['casa']) == dict and myf['casa'].has_key('flags') \
                 and type(myf['casa']['flags']) == dict and myf['casa']['flags'].has_key('--rcdir'):
            args += [ "--rcdir=" + myf['casa']['flags']['--rcdir'] ]

	if (os.uname()[0]=='Darwin'):
            if myf['casa']['state']['init_version'] > 0:
                from casa_system import procmgr
                procmgr.create(self.__state['dbus name'],args,procmgr.output_option.DISCARD)
            else:
		vwrpid=os.spawnvp( os.P_NOWAIT, viewer_path, args )
	elif (os.uname()[0]=='Linux'):
            if myf['casa']['state']['init_version'] > 0:
                from casa_system import procmgr
                procmgr.create(self.__state['dbus name'],args,procmgr.output_option.DISCARD)
            else:
		vwrpid=os.spawnlp( os.P_NOWAIT, viewer_path, *args )
	else:
        	raise Exception,'unrecognized operating system'

        self.__state['launched'] = True
Example #38
0
def importarchive(
    archivefiles=None,
    vis=None,
    bandname=None,
    freqtol=None,
):
    """Convert VLA archive file(s) to a CASA visibility data set (MS):

....Keyword arguments:
....archivefiles -- Name of input VLA archive file(s)
........default: <unset>
........example: archivefiles=['AP314_A950519.xp1','AP314_A950519.xp2']
....vis -- Name of output visibility file (MS)
........default: <unset>; example: vis='NGC7538.ms'
....bandname -- VLA Frequency band
........default: <unset> - all bands; example: bandname='K'
........<Options: '4'=48-96 MHz,'P'=298-345 MHz,'L'=1.15-1.75 GHz,
........'C'=4.2-5.1 GHz,'X'=6.8-9.6 GHz,'U'=13.5-16.3 GHz,
........'K'=20.8-25.8 GHz,'Q'=38-51 GHz>
....#projectname -- Observing project name
....#....default: <unset>; example='AP314'
....freqtol -- Tolerance in frequency shift in naming spectral windows
........default: channel width of current spectral window in Hz
........example: 150000.0
....

...."""

    myf = stack_frame_find()
    vftask = myf['vftask']
    vlafiller = vftask.fill
    #    fg = myf['fg']
    aflocal = casac.agentflagger()
    # ##
    # Handle globals or user over-ride of arguments
    function_signature_defaults = \
        dict(zip(importarchive.func_code.co_varnames,
             importarchive.func_defaults))
    for item in function_signature_defaults.iteritems():
        (key, val) = item
        keyVal = eval(key)
        if keyVal == None:
            # user hasn't set it - use global/default
            pass
        else:
            # user has set it - use over-ride
            myf[key] = keyVal

    archivefiles = myf['archivefiles']
    vis = myf['vis']
    bandname = myf['bandname']
    freqtol = myf['freqtol']
    #
    # ##

    # Add type/menu/range error checking here
    if type(archivefiles) == str:
        archivefiles = [archivefiles]
    arg_names = ['archivefiles', 'vis', 'bandname', 'freqtol']
    arg_values = [archivefiles, vis, bandname, freqtol]
    arg_types = [list, str, str, float]
    try:
        parameter_checktype(arg_names, arg_values, arg_types)
        parameter_checkmenu('bandname', bandname, [
            '4',
            'P',
            'L',
            'C',
            'X',
            'U',
            'K',
            'Q',
        ])
    except TypeError, e:
        print 'importarchive -- TypeError: ', e
        return
Example #39
0
import shutil
import unittest
import os
import sys
from tasks import flagdata, flagmanager
from taskinit import aftool, tbtool
from __main__ import default
import exceptions
import __builtin__
from parallel.parallel_task_helper import ParallelTaskHelper
from casa_stack_manip import stack_frame_find
import flaghelper as fh

# to rethrow exception 
import inspect
g = stack_frame_find( )
g['__rethrow_casa_exceptions'] = True

# Path for data
datapath = os.environ.get('CASAPATH').split()[0] + "/data/regression/unittest/flagdata/"

# Pick up alternative data directory to run tests on MMSs
testmms = False
if os.environ.has_key('TEST_DATADIR'):   
    DATADIR = str(os.environ.get('TEST_DATADIR'))+'/flagdata/'
    if os.path.isdir(DATADIR):
        testmms = True
        datapath = DATADIR

print 'flagmanager tests will use data from '+datapath         
Example #40
0
def tget(task=None, savefile=''):
	""" Get last input values from file on disk for a specified task: 

	task -- Name of task
		default: <unset>; example: task='bandpass'
		<Options: type tasklist() for the complete list>
	savefile -- Output file for the task inputs
		default: task.last then task.saved
		example: savefile=task.orion

	"""
        try:
		myf=stack_frame_find( )
		if task==None :
			if myf.has_key('task') :
				task=myf['task']
			else : 
				if myf.has_key('taskname') : task=myf['taskname']
		myf['taskname']=task
		myf['task']=task
                if type(task)!=str:
                        task=task.__name__
		       	myf['task']=task
                        myf['taskname']=task
                parameter_checktype(['task','savefile'],[task,savefile],[str,str])
                parameter_checkmenu('task',task,tasksum.keys())

                ###Check if task exists by checking if task_defaults is defined
		if ( not myf.has_key(task) and 
		     str(type(myf[task])) != "<type 'instance'>" and 
		     not hasattr(myf[task],"defaults") ):
		          raise TypeError, "task %s is not defined " %task

                if task==None: task=myf['task']
                myf['task']=task
                #f=zip(myf[taskname].func_code.co_varnames,myf[taskname].func_defaults)
                #for j in range(len(f)):
                #        k=f[j][0]
		#	print 'k is ',k
		#	stst = myf[k]
		#	if ( type(stst) == str ):
		#		sex = k+"='"+stst+"'"
		#	else:
		#		sex = k+"="+str(stst)
		#	exec(sex)
		if savefile=='':
			savefile=task+'.last'
			try:
				taskparameterfile=open(savefile,'r')
			except:
				savefile=task+'.saved'
				try:
					taskparameterfile=open(savefile,'r')
				except:
					print "Sorry - no task.last or .saved"
					return
		
			taskparameterfile.close()
		execfile(savefile)
		# Put the task parameters back into the global namespace
                f=zip(myf[task].__call__.func_code.co_varnames[1:],myf[task].__call__.func_defaults)
                missing_ks = []
                for j in f:
                        k = j[0]
			if k != 'self':
                                try:
                                        myf[k] = eval(k)
                                except NameError:
                                        missing_ks.append(k)
                if missing_ks:
                        print "Did not find a saved value for",
                        if len(missing_ks) > 1:
                                print ', '.join(missing_ks[:-1]),
                                print 'or', missing_ks[-1]
                        else:
                                print missing_ks[0]
                        print "The set of task parameters has probably changed"
                        print "since", savefile, "was written."
		print "Restored parameters from file "+savefile
        except TypeError, e:
                print "tget --error: ", e
Example #41
0
	def __call__(self, infile=None,displaytype=None,channel=None,zoom=None,outfile=None,outscale=None,outdpi=None,outformat=None,outlandscape=None,gui=None):
		""" The msview will display measurement sets in raster form
		Many display and editing options are available.

		examples of usage:

		msview
		msview "mymeasurementset.ms"
		msview "myrestorefile.rstr"
		
		Executing the msview task will bring up a display panel
		window, which can be resized.  If no data file was specified,
		a Load Data window will also appear.  Click on the desired data
		file and choose the display type; the rendered data should appear
		on the display panel.

		A Data Display Options window will also appear.  It has drop-down
		subsections for related	options, most of which are self-explanatory.
	  
		The state of the msview task -- loaded data and related display
		options -- can be saved in a 'restore' file for later use.
		You can provide the restore filename on the command line or
		select it from the Load Data window.

		See the cookbook for more details on using the msview task.
	
		Keyword arguments:
		infile -- Name of file to visualize
			default: ''
			example: infile='my.ms'
			If no infile is specified the Load Data window
			will appear for selecting data.
		displaytype -- (optional): method of rendering data
			visually (raster, contour, vector or marker).  
			You can also set this parameter to 'lel' and
			provide an lel expression for infile (advanced).
			default: 'raster'

		Note: there is no longer a filetype parameter; typing of
		data files is now done automatically.
	        	example:  msview infile='my.ms'
			obsolete: msview infile='my.ms', filetype='ms'


		"""
                myf=stack_frame_find( )
                vi = myf['vi'] if myf.has_key('vi') else None
                ving = myf['ving'] if myf.has_key('ving') else None

		#Python script
		try:
			## vi might not be defined in taskinit if loading
			## directly from python via casa.py...
			vwr = vi
			if type(gui) == bool and gui == False:
				vwr = ving

			if type(vwr.cwd( )) != str:
				vwr = None
		except:
			vwr = None

		if type(vwr) == type(None):
			need_gui = True
			if type(gui) == bool and gui == False:
				need_gui = False

			if need_gui :
				if self.local_vi is not None:
					vwr = self.local_vi
				else:
					vwr = viewertool.viewertool( True, True, (type(myf) == dict and myf.has_key('casa') and type(myf['casa']) == type(os)) )
					self.local_vi = vwr
			else:
				if self.local_ving is not None:
					vwr = self.local_ving
				else:
					vwr = viewertool.viewertool( False, True, (type(myf) == dict and myf.has_key('casa') and type(myf['casa']) == type(os)) )
					self.local_ving = vwr

		if type(vwr) != type(None) :
			##
			## (1) save current *viewer*server* path
			## (2) have viewer() task follow casapy/python's cwd
			try:
				old_path = vwr.cwd( )
			except:
				raise Exception, "msview() failed to get the current working directory"

			try:
				vwr.cwd(os.path.abspath(os.curdir))
			except:
				raise Exception, "msview() failed to change to the new working directory"
				
			data = None
			if type(infile) == str and len(infile) > 0 :
				info = vwr.fileinfo(infile);
				if info['type'] != 'ms' :
					if info['type'] == 'image' :
						raise Exception, "msview() only displays images, try 'imview()'..."
					elif info['type'] == 'nonexistent' :
						raise Exception, "ms (" + infile + ") could not be found..."
					else :
						raise Exception, "unknow error..."

				panel = vwr.panel("viewer")
				if type(displaytype) == str:
					data = vwr.load( infile, displaytype, panel=panel )
				else:
					data = vwr.load( infile, panel=panel )

				if type(channel) == int and channel > 0 :
					vwr.channel(channel,panel=panel)
				if type(zoom) == int and zoom != 1 :
					vwr.zoom(zoom,panel=panel)
				if type(outfile) == str and len(outfile) > 0 :
					scale=1.0
					if type(outscale) == float :
						scale=outscale
					dpi=300
					if type(outdpi) == int :
						dpi=outdpi
					format="jpg"
					if type(outformat) == str :
						format=outformat
					orientation="portrait"
					if type(outlandscape) == bool and outlandscape :
						orientation="landscape"
					vwr.output(outfile,scale=scale,dpi=dpi,format=format,orientation=orientation,panel=panel)
			else:
				panel = vwr.panel("viewer")
				vwr.popup( 'open', panel=panel )


			# it makes no sense to leave a panel open with no way of interacting with it
			if type(gui) == bool and not gui:
				vwr.close(panel)

			## (3) restore original path
			try:
				vwr.cwd(old_path)
			except:
				raise Exception, "msview() failed to restore the old working directory"

		else:
			viewer_path = myf['casa']['helpers']['viewer']   #### set in casapy.py
			args = [ viewer_path ]

			if type(infile) == str:
				if type(displaytype) == str:
					args += [ infile, displaytype ]
				else:
					args += [ infile ]

			if (os.uname()[0]=='Darwin'):
				vwrpid=os.spawnvp( os.P_NOWAIT, viewer_path, args )
			elif (os.uname()[0]=='Linux'):
				vwrpid=os.spawnlp( os.P_NOWAIT, viewer_path, *args )
			else:
				print 'Unrecognized OS: No msview available'

		return None
Example #42
0
    def __call__(
        self,
        vis=None,
        spw=None,
        field=None,
        secsour=None,
        stddevfact=None,
    ):
        """Find line free channels to use in continuum imaging.

        Detailed Description:

        Find spectral channels free of spectral line emission to aid in the processes of continuum imaging.
        
        Arguments :
                vis: name of input visibility file
                   Default Value: 

                spw: Spectral window 
                   Default Value: 

                field: Field Name
                   Default Value: 

                secsour: File with positions and properties of Secondary Sources
                   Default Value: 

                stddevfact: Standard deviation factor 
                   Default Value: 

        Returns: void

        Example :


            Task to find channels free of molecular emission lines to provide users with line free channels to use in continuum only imaging.

            Outputs proposed line free channels to [field]_SPW_[spw]_LineFreeChans.txt

            Intended to be used within the ALMA data directory structure, as downloaded from the ALMA Archive.

            vis -- Name of fully calibrated input visibility file, within the 'calibrated' directory of an ALMA Archive product
               default: none; example: vis='ngc5921.ms'

            field -- Select field in which to find lines.  Use field name(s) *ONLY*.
                  
            spw -- Select a *single* spectral window in which to find lines

            secsour -- User defined file for extracting spectra a position
                    "sourceX     RA[hh:mm:ss.000]    Dec[dd:mm:ss.000]   Bmaj*   Bmin*   BPA*"
                    " *fitted 2D Gaussian major, minor axis and position angle."

            stddevfact --  Standard deviation factor for sigma clipping.
              default = 1.5

        
        """
        if not hasattr(self, "__globals__") or self.__globals__ == None:
            self.__globals__ = stack_frame_find()
        #casac = self.__globals__['casac']
        casalog = self.__globals__['casalog']
        casa = self.__globals__['casa']
        #casalog = casac.casac.logsink()
        self.__globals__['__last_task'] = 'lumberjack'
        self.__globals__['taskname'] = 'lumberjack'
        ###
        self.__globals__['update_params'](func=self.__globals__['taskname'],
                                          printtext=False,
                                          ipython_globals=self.__globals__)
        ###
        ###
        #Handle globals or user over-ride of arguments
        #
        if type(self.__call__.func_defaults) is NoneType:
            function_signature_defaults = {}
        else:
            function_signature_defaults = dict(
                zip(self.__call__.func_code.co_varnames[1:],
                    self.__call__.func_defaults))
        useLocalDefaults = False

        for item in function_signature_defaults.iteritems():
            key, val = item
            keyVal = eval(key)
            if (keyVal == None):
                #user hasn't set it - use global/default
                pass
            else:
                #user has set it - use over-ride
                if (key != 'self'):
                    useLocalDefaults = True

        myparams = {}
        if useLocalDefaults:
            for item in function_signature_defaults.iteritems():
                key, val = item
                keyVal = eval(key)
                exec('myparams[key] = keyVal')
                self.parameters[key] = keyVal
                if (keyVal == None):
                    exec('myparams[key] = ' + key + ' = self.itsdefault(key)')
                    keyVal = eval(key)
                    if (type(keyVal) == dict):
                        if len(keyVal) > 0:
                            exec('myparams[key] = ' + key +
                                 ' = keyVal[len(keyVal)-1][\'value\']')
                        else:
                            exec('myparams[key] = ' + key + ' = {}')

        else:
            print ''

            myparams['vis'] = vis = self.parameters['vis']
            myparams['spw'] = spw = self.parameters['spw']
            myparams['field'] = field = self.parameters['field']
            myparams['secsour'] = secsour = self.parameters['secsour']
            myparams['stddevfact'] = stddevfact = self.parameters['stddevfact']

        result = None

        #
        #    The following is work around to avoid a bug with current python translation
        #
        mytmp = {}

        mytmp['vis'] = vis
        mytmp['spw'] = spw
        mytmp['field'] = field
        mytmp['secsour'] = secsour
        mytmp['stddevfact'] = stddevfact
        pathname = "file:///raid1/scratch/aavison/LJ_2020/"
        trec = casac.casac.utils().torecord(pathname + 'lumberjack.xml')

        casalog.origin('lumberjack')
        try:
            #if not trec.has_key('lumberjack') or not casac.casac.utils().verify(mytmp, trec['lumberjack']) :
            #return False

            casac.casac.utils().verify(mytmp, trec['lumberjack'], True)
            scriptstr = ['']
            saveinputs = self.__globals__['saveinputs']

            # Save .last file for this task execution. MPI servers don't write it (CASR-329).
            from mpi4casa.MPIEnvironment import MPIEnvironment
            do_full_logging = MPIEnvironment.is_mpi_disabled_or_client()
            if type(self.__call__.func_defaults) is NoneType:
                saveinputs = ''
            else:
                saveinputs('lumberjack',
                           'lumberjack.last',
                           myparams,
                           self.__globals__,
                           scriptstr=scriptstr,
                           do_save_inputs=do_full_logging)

            tname = 'lumberjack'
            spaces = ' ' * (18 - len(tname))
            casalog.post('\n##########################################' +
                         '\n##### Begin Task: ' + tname + spaces + ' #####')
            # Don't do telemetry from MPI servers (CASR-329)
            if do_full_logging and casa['state']['telemetry-enabled']:
                #casalog.poststat('Begin Task: ' + tname)
                task_starttime = str(datetime.datetime.now())
            if type(self.__call__.func_defaults) is NoneType:
                casalog.post(scriptstr[0] + '\n', 'INFO')
            else:
                casalog.post(scriptstr[1][1:] + '\n', 'INFO')

            # Effective call to the task as defined in gcwrap/python/scripts/task_*
            result = lumberjack(vis, spw, field, secsour, stddevfact)

            if do_full_logging and casa['state']['telemetry-enabled']:
                task_endtime = str(datetime.datetime.now())
                casalog.poststat('Task ' + tname + ' complete. Start time: ' +
                                 task_starttime + ' End time: ' + task_endtime)
            casalog.post('##### End Task: ' + tname + '  ' + spaces +
                         ' #####' +
                         '\n##########################################')

        except Exception, instance:
            if (self.__globals__.has_key('__rethrow_casa_exceptions')
                    and self.__globals__['__rethrow_casa_exceptions']):
                raise
            else:
                #print '**** Error **** ',instance
                tname = 'lumberjack'
                casalog.post('An error occurred running task ' + tname + '.',
                             'ERROR')
                pass
Example #43
0
def simanalyze(
    project=None,
    image=None,
    # if image==False:
    imagename=None, 
    skymodel=None,
    # else:
    vis=None, modelimage=None, imsize=None, imdirection=None, cell=None,
    interactive=None, niter=None, threshold=None,
    weighting=None, mask=None, outertaper=None, pbcor=None, stokes=None,
    featherimage=None,
    # endif
    analyze=None,
    showuv=None, showpsf=None, showmodel=None,
    showconvolved=None, showclean=None, showresidual=None, showdifference=None,
    showfidelity=None,
    graphics=None,
    verbose=None,
    overwrite=None,
    dryrun=False,
    logfile=None
    ):

#def simanalyze(project='sim', image=True, imagename='default', skymodel='', vis='default', modelimage='', imsize=[128, 128], imdirection='', cell='', interactive=False, niter=0, threshold='0.1mJy', weighting='natural', mask=[], outertaper=[''], stokes='I', featherimage='', analyze=False, showuv=True, showpsf=True, showmodel=True, showconvolved=False, showclean=True, showresidual=False, showdifference=True, showfidelity=True, graphics='both', verbose=False, overwrite=True, dryrun=False):


    # Collect a list of parameter values to save inputs
    in_params =  locals()

    import re
    import glob

    casalog.origin('simanalyze')
    if verbose: casalog.filter(level="DEBUG2")

    myf = stack_frame_find( )
    
    # create the utility object:    
    myutil = simutil()
    if logfile: 
        myutil.reportfile=logfile
        myutil.openreport()
    if verbose: myutil.verbose = True
    msg = myutil.msg
    from simutil import is_array_type

    # put output in directory called "project"
    fileroot = project
    if not os.path.exists(fileroot):
        msg(fileroot+" directory doesn't exist - the task expects to find results from creating the datasets there, like the skymodel.",priority="error")
        # msg should raise an exception for priority=error


    saveinputs = myf['saveinputs']
    saveinputs('simanalyze',fileroot+"/"+project+".simanalyze.last")
#               myparams=in_params)

    if (not image) and (not analyze):
        casalog.post("No operation to be done. Exiting from task.", "WARN")
        return True

    grscreen = False
    grfile = False
    if graphics == "both":
        grscreen = True
        grfile = True
    if graphics == "screen":
        grscreen = True
    if graphics == "file":
        grfile = True
    
    try:

        # Predefined parameters 
        pbcoeff = 1.13 ##  PB defined as pbcoeff*lambda/d


        # handle '$project' in modelimage
        modelimage = modelimage.replace('$project',project)
        featherimage = featherimage.replace('$project',project)

        #=========================
        # things we need: model_cell, model_direction if user doesn't specify - 
        # so find those first, and get information using util.modifymodel
        # with skymodel=newmodel


        # we need to parse either the mslist or imagename (if image=False) 
        # first, so that we can pick the appropriate skymodel, 
        # if there are several.
        skymodel_searchstring="NOT SPECIFIED"

        if not (image or dryrun):
            user_imagename=imagename
            if user_imagename=="default" or len(user_imagename)<=0:
                images= glob.glob(fileroot+"/*image")
                if len(images)<1:
                    msg("can't find any image in project directory",priority="error")
                    return False
                if len(images)>1:
                    msg("found multiple images in project directory",priority="warn")
                    msg("using  "+images[0],priority="warn")
                imagename=images[0]
            # trim .image suffix:
            imagename= imagename.replace(".image","")
            
            # if the user hasn't specified a sky model image, we can try to 
            # see if their imagename contains something like the project and 
            # configuration, as it would if simobserve created it.
            user_skymodel=skymodel
            if not os.path.exists(user_skymodel):
                if os.path.exists(fileroot+"/"+user_skymodel):
                    user_skymodel=fileroot+"/"+user_skymodel
                elif len(user_skymodel)>0:
                    raise Exception,"Can't find your specified skymodel "+user_skymodel
            # try to strip a searchable identifier
            tmpstring=user_skymodel.split("/")[-1]
            skymodel_searchstring=tmpstring.replace(".image","")
            


        if image:
            # check for default measurement sets:
            default_mslist = glob.glob(fileroot+"/*ms")
            n_default=len(default_mslist)
            # is the user requesting this ms?
            default_requested=[]
            for i in range(n_default): default_requested.append(False)

            # parse ms parameter and check for existance;
            # initial ms list
            if vis=="default" or len(vis)==0:
                mslist0=default_mslist
            else:
                mslist0 = vis.split(',')
            # verified found ms list
            mslist = []
            mstype = []
            
            mstoimage=[]
            tpmstoimage=None

            for ms0 in mslist0:
                if not len(ms0): continue
                ms1 = ms0.replace('$project',project)

                # MSes in fileroot/ have priority
                if os.path.exists(fileroot+"/"+ms1):
                    ms1 = fileroot + "/" + ms1

                if os.path.exists(ms1)or dryrun:
                    mslist.append(ms1)

                    # mark as requested
                    if default_mslist.count(ms1):
                        i=default_mslist.index(ms1)
                        default_requested[i]=True

                    # check if noisy in name
                    if re.search('noisy.',ms1):
                        ms1_raw=str.join("",re.split('noisy.',ms1))
                        if default_mslist.count(ms1_raw):
                            i=default_mslist.index(ms1_raw)
                            default_requested[i]=True
                    else: # not noisy
                        if ms1.endswith(".sd.ms"):
                            ms1_noisy=re.split('.sd.ms',ms1)[0]+'.noisy.sd.ms'
                        else:
                            ms1_noisy=re.split('.ms',ms1)[0]+'.noisy.ms'
                        if default_mslist.count(ms1_noisy):
                            i=default_mslist.index(ms1_noisy)
                            default_requested[i]=True
                            if vis == "default": continue
                            msg("You requested "+ms1+" but there is a noisy version of the ms in your project directory - if your intent is to model noisy data you may want to check inputs",priority="warn")

                    # check if the ms is tp data or not.
                    if dryrun:
                        # HACK
                        mstype.append('INT')
                        mstoimage.append(ms1)

                    elif myutil.ismstp(ms1,halt=False):
                        mstype.append('TP')
                        tpmstoimage = ms1
                        # XXX TODO more than one TP ms will not be handled
                        # correctly
                        msg("Found a total power measurement set, %s." % ms1,origin='simanalyze')
                    else:
                        mstype.append('INT')
                        mstoimage.append(ms1)
                        msg("Found a synthesis measurement set, %s." % ms1,origin='simanalyze')
                else:          
                    msg("measurement set "+ms1+" not found -- removing from imaging list")
            
            # check default mslist for unrequested ms:
            for i in range(n_default):
                if not default_requested[i]:
                    msg("Project directory contains "+default_mslist[i]+" but you have not requested to include it in your simulated image.")


            if not mstoimage  and len(tpmstoimage) == 0:
                raise Exception,"No MS found to image"

            # now try to parse the mslist for an identifier string that 
            # we can use to find the right skymodel if there are several
            if len(mstoimage) == 0 and len(tpmstoimage) > 0:
                tmpstring = tpmstoimage.split("/")[-1]
            else:
                tmpstring=(mstoimage[0]).split("/")[-1]
            skymodel_searchstring=tmpstring.replace(".ms","")



            # more than one to image?
            if len(mstoimage) > 1:
                msg("Multiple interferometric ms found:",priority="info",origin='simanalyze')
                for i in range(len(mstoimage)):
                    msg(" "+mstoimage[i],priority="info",origin='simanalyze')
                msg(" will be concated and simultaneously deconvolved; if something else is desired, please specify vis, or image manually and use image=F",priority="info",origin='simanalyze')
                concatms=project+"/"+project+".concat.ms"
                from concat import concat
                weights = get_concatweights(mstoimage)
                msg(" concat("+str(mstoimage)+",concatvis='"+concatms+"',visweightscale="+str(weights)+")",origin='simanalyze')
                if not dryrun:
                    concat(mstoimage,concatvis=concatms,visweightscale=weights)
                    mstoimage=[concatms]



        #========================================================
        # now we can search for skymodel, and if there are several, 
        # pick the one that is closest to either the imagename, 
        # or the first MS if there are several MS to image.

        components_only=False

        # first look for skymodel, if not then compskymodel
        skymodels=glob.glob(fileroot+"/"+project+"*.skymodel")+glob.glob(fileroot+"/"+project+"*.newmodel")
        nmodels=len(skymodels)
        skymodel_index=0
        if nmodels>1:
            msg("Found %i sky model images:" % nmodels,origin='simanalyze')
            # use the skymodel_searchstring to try to pick the right one
            # print them out for the user while we're at it.
            for i in range(nmodels):
                msg("   "+skymodels[i])
                if skymodels[i].count(skymodel_searchstring)>0:
                    skymodel_index=i
            msg("Using skymodel "+skymodels[skymodel_index],origin='simanalyze')
        if nmodels>=1:
            skymodel=skymodels[skymodel_index]
        else:
            skymodel=""
        
        if os.path.exists(skymodel):
            msg("Sky model image "+skymodel+" found.",origin='simanalyze')
        else:
            skymodels=glob.glob(fileroot+"/"+project+"*.compskymodel")
            nmodels=len(skymodels)
            if nmodels>1:
                msg("Found %i sky model images:" % nmodels,origin='simanalyze')
                for ff in skymodels:
                    msg("   "+ff)
                msg("Using "+skymodels[0],origin='simanalyze')
            if nmodels>=1:
                skymodel=skymodels[0]
            else:
                skymodel=""

            if os.path.exists(skymodel):
                msg("Sky model image "+skymodel+" found.",origin='simanalyze')
                components_only=True
            elif not dryrun:
                msg("Can't find a model image in your project directory, named skymodel or compskymodel - output image will be created, but comparison with the input model is not possible.",priority="warn",origin='simanalyze')
                analyze=False

        modelflat = skymodel+".flat"

        if os.path.exists(skymodel):
            if not (os.path.exists(modelflat) or dryrun):
                myutil.flatimage(skymodel,verbose=verbose)
            
            # modifymodel just collects info if skymodel==newmodel
            (model_refdir,model_cell,model_size,
             model_nchan,model_center,model_width,
             model_stokes) = myutil.modifymodel(skymodel,skymodel,
                                              "","","","","",-1,
                                              flatimage=False)
            
            cell_asec=qa.convert(model_cell[0],'arcsec')['value']


        #####################################################################
        # clean if desired, use noisy image for further calculation if present
        # todo suggest a cell size from psf?
        #####################################################################
        if image:

            # make sure cell is defined
            if is_array_type(cell):
                if len(cell) > 0:
                    cell0 = cell[0]
                else:
                    cell0 = ""
            else:
                cell0 = cell
            if len(cell0)<=0:
                cell = model_cell
            if is_array_type(cell):
                if len(cell) == 1:
                    cell = [cell[0],cell[0]]
            else:
                cell = [cell,cell]
            
            # cells are positive by convention
            cell = [qa.abs(cell[0]),qa.abs(cell[1])]
            
            # and imsize
            if is_array_type(imsize):
                if len(imsize) > 0:
                    imsize0 = imsize[0]
                    if len(imsize) > 1:
                        imsize1 = imsize[1]
                    else:
                        imsize1 = imsize0
                else:
                    imsize0 = -1
            else:
                imsize0 = imsize
            if imsize0 <= 0:
                imsize = [int(pl.ceil(qa.convert(qa.div(model_size[0],cell[0]),"")['value'])),
                          int(pl.ceil(qa.convert(qa.div(model_size[1],cell[1]),"")['value']))]
            else:
                imsize=[imsize0,imsize1]


            if len(mstoimage) == 0:
                if tpmstoimage:
                    sd_only = True
                else:
                    msg("no measurement sets found to image",priority="error",origin='simanalyze')
            else:
                sd_only = False
                # get some quantities from the interferometric ms
                # TODO use something like aU.baselineStats for this, and the 90% baseline
                maxbase=0.
                if len(mstoimage)>1 and dryrun:
                    msg("imaging multiple ms not possible in dryrun mode",priority="warn",origin="simanalyze")
                # TODO make work better for multiple MS
                for msfile in mstoimage:
                    if os.path.exists(msfile):
                        tb.open(msfile)
                        rawdata = tb.getcol("UVW")
                        tb.done()
                        maxbase = max([max(rawdata[0,]),max(rawdata[1,])])  # in m
                        psfsize = 0.3/qa.convert(qa.quantity(model_center),'GHz')['value']/maxbase*3600.*180/pl.pi # lambda/b converted to arcsec
                        minimsize = 8* int(psfsize/cell_asec)
                    elif dryrun:
                        minimsize = min(imsize)
                        psfsize = qa.mul(cell[0],3) # HACK
                    else:
                        raise Exception,mstoimage+" not found."

                if imsize[0] < minimsize:
                    msg("The number of image pixel in x-axis, %d, is small to cover 8 x PSF. Setting x pixel number, %d." % (imsize[0], minimsize), priority='warn',origin='simanalyze')
                    imsize[0] = minimsize
                if imsize[1] < minimsize:
                    msg("The number of image pixel in y-axis, %d, is small to cover 8 x PSF. Setting y pixel number, %d" % (imsize[1], minimsize), priority='warn',origin='simanalyze')
                    imsize[1] = minimsize

            tpimage=None
            # Do single dish imaging first if tpmstoimage exists.
            if tpmstoimage and os.path.exists(tpmstoimage):
                msg('creating image from ms: '+tpmstoimage,origin='simanalyze')
                #if len(mstoimage):
                #    tpimage = project + '.sd.image'
                #else:
                #    tpimage = project + '.image'
                tpimage = project + '.sd.image'
                tpimage = fileroot + "/" + tpimage

                if len(mstoimage):
                    if len(modelimage) and tpimage != modelimage and \
                           tpimage != fileroot+"/"+modelimage:
                        msg("modelimage parameter set to "+modelimage+" but also creating a new total power image "+tpimage,priority="warn",origin='simanalyze')
                        msg("assuming you know what you want, and using modelimage="+modelimage+" in deconvolution",priority="warn",origin='simanalyze')
                    elif len(featherimage) and tpimage != featherimage and \
                           tpimage != fileroot+"/"+featherimage:
                        msg("featherimage parameter set to "+featherimage+" but also creating a new total power image "+tpimage,priority="warn",origin='simanalyze')
                        msg("assuming you know what you want, and using featherimage="+featherimage+" in feather",priority="warn",origin='simanalyze')
                
                # Get PB size of TP Antenna
                # !! aveant will only be set if modifymodel or setpointings and in 
                # any case it will the the aveant of the INTERFM array - we want the SD
                if os.path.exists(tpmstoimage):
                    # antenna diameter
                    tb.open(tpmstoimage+"/ANTENNA")
                    diams = tb.getcol("DISH_DIAMETER")
                    tb.close()
                    aveant = pl.mean(diams)
                    # theoretical antenna beam size
                    import sdbeamutil
                    pb_asec = sdbeamutil.primaryBeamArcsec(qa.tos(qa.convert(qa.quantity(model_center),'GHz')),aveant,(0.75 if aveant==12.0 else 0.0),10.0)
                elif dryrun:
                    aveant = 12.0
                    pb_asec = pbcoeff*0.29979/qa.convert(qa.quantity(model_center),'GHz')['value']/aveant*3600.*180/pl.pi
                else:
                    raise Exception, tpmstoimage+" not found."

                # default PSF from PB of antenna
                imbeam = {'major': qa.quantity(pb_asec,'arcsec'),
                          'minor': qa.quantity(pb_asec,'arcsec'),
                          'positionangle': qa.quantity(0.0,'deg')}

                # Common imaging parameters
                sdim_param = dict(infiles=[tpmstoimage], overwrite=overwrite,
                                  phasecenter=model_refdir, mode='channel',
                                  nchan=model_nchan, start=0, width=1)
                
                if True: #SF gridding
                    msg("Generating TP image using 'SF' kernel.",origin='simanalyze')
                    beamsamp = 9
                    sfcell_asec = pb_asec/beamsamp
                    sfcell = qa.tos(qa.quantity(sfcell_asec, "arcsec"))
                    cell_asec = [qa.convert(cell[0],"arcsec")['value'],
                                 qa.convert(cell[1],"arcsec")['value']]
                    if cell_asec[0] > sfcell_asec or \
                           cell_asec[1] > sfcell_asec:
                        # imregrid() may not work properly for regrid of
                        # small to large cell
                        msg("The requested cell size is too large to invoke SF gridding. Please set cell size <= %f arcsec or grid TP MS '%s' manually" % (sfcell_asec, tpmstoimage),priority="error",origin='simanalyze')

                    sfsupport = 6
                    temp_out = tpimage+"0"
                    temp_cell = [sfcell, sfcell]
                    # too small - is imsize too small to start with?
                    # needs to cover all pointings.
                    temp_imsize = [int(pl.ceil(cell_asec[0]/sfcell_asec*imsize[0])),
                                   int(pl.ceil(cell_asec[1]/sfcell_asec*imsize[1]))]
                    msg("Using predefined algorithm to define grid parameters.",origin='simanalyze')
                    msg("SF gridding summary",origin='simanalyze')
                    msg("- Antenna primary beam: %f arcsec" % pb_asec,origin='simanalyze')
                    msg("- Image pixels per antenna PB (predefined): %f" % beamsamp,origin='simanalyze')
                    msg("- Cell size (arcsec): [%s, %s]" % (temp_cell[0], temp_cell[1]),origin='simanalyze')
                    msg("- Imsize to cover final TP image area: [%d, %d] (type: %s)" % (temp_imsize[0], temp_imsize[1], type(temp_imsize[0])),origin='simanalyze')
                    msg("- convolution support: %d" % sfsupport,origin='simanalyze')
                    # kernel specific imaging parameters
                    sdim_param['gridfunction'] = 'SF'
                    sdim_param['convsupport'] = sfsupport
                    sdim_param['outfile'] = temp_out
                    sdim_param['imsize'] = temp_imsize
                    sdim_param['cell'] = temp_cell

                    msg(get_taskstr('sdimaging', sdim_param), priority="info")
                    if not dryrun:
                        sdimaging(**sdim_param)
                        if not os.path.exists(temp_out):
                            raise RuntimeError, "TP imaging failed."

                        # Scale image by convolved beam / antenna primary beam
                        ia.open(temp_out)
                        imbeam = ia.restoringbeam()
                        ia.close()
                        beam_area_ratio = qa.getvalue(qa.convert(imbeam['major'], "arcsec")) \
                                          * qa.getvalue(qa.convert(imbeam['minor'], "arcsec")) \
                                          / pb_asec**2
                        msg("Scaling TP image intensity by %f." % (beam_area_ratio),origin='simanalyze')
                        temp_in = temp_out
                        temp_out = temp_out + ".scaled"
                        immath(imagename=temp_in, mode='evalexpr', expr="IM0*%f" % (beam_area_ratio),
                               outfile=temp_out)
                        if not os.path.exists(temp_out):
                            raise RuntimeError, "TP image scaling failed."
                        
                    # Regrid TP image to final resolution
                    msg("Regridding TP image to final resolution",origin='simanalyze')
                    msg("- cell size (arecsec): [%s, %s]" % (cell[0], cell[1]),origin='simanalyze')
                    msg("- imsize: [%d, %d]" % (imsize[0], imsize[1]),origin='simanalyze')
                    if not dryrun:
                        ia.open(temp_out)
                        newcsys = ia.coordsys()
                        ia.close()
                        dir_idx = newcsys.findcoordinate("direction")['world']
                        newcsys.setreferencepixel([imsize[0]/2., imsize[1]/2.],
                                                  type="direction")
                        incr = newcsys.increment(type='direction')['numeric']
                        newincr = [incr[0]*cell_asec[0]/sfcell_asec,
                                   incr[1]*cell_asec[1]/sfcell_asec,]
                        newcsys.setincrement(newincr, type="direction")
                        #
                        sdtemplate = imregrid(imagename=temp_out, template="get")
                        sdtemplate['csys'] = newcsys.torecord()
                        for idx in range(len(dir_idx)):
                            sdtemplate['shap'][ dir_idx[idx] ] = imsize[idx]
                        imregrid(imagename=temp_out, interpolation="cubic",
                             template=sdtemplate, output=tpimage,
                             overwrite=overwrite)
                        del newcsys, sdtemplate, incr, newincr, dir_idx
                        del temp_out, temp_cell, temp_imsize, sfcell_asec, cell_asec
                else: #PB grid
                    msg("Generating TP image using 'PB' kernel.",origin='simanalyze')
                    # Final TP cell and image size.
                    # imsize and cell are already int and quantum arrays
                    sdimsize = imsize
                    sdcell = [qa.tos(cell[0]), qa.tos(cell[1])]
                    ### TODO: need to set phasecenter properly based on imdirection
                    # kernel specific imaging parameters
                    sdim_param['gridfunction'] = 'PB'
                    sdim_param['outfile'] = tpimage
                    sdim_param['imsize'] = sdimsize
                    sdim_param['cell'] = sdcell
                    
                    msg(get_taskstr('sdimaging', sdim_param), priority="info")
                    if not dryrun:
                        sdimaging(**sdim_param)
                    del sdimsize, sdcell
                    # TODO: Define PSF of image here
                    # for now use default 

                # get image beam size form TP image
                if os.path.exists(tpimage):
                    ia.open(tpimage)
                    beam = ia.restoringbeam()
                    ia.close()

                if sd_only:
                    bmarea = beam['major']['value']*beam['minor']['value']*1.1331 #arcsec2
                    bmarea = bmarea/(cell[0]['value']*cell[1]['value']) # bm area in pix
                else: del beam
                #del beam

                msg('generation of total power image '+tpimage+' complete.',origin='simanalyze')
                # update TP ms name the for following steps
                sdmsfile = tpmstoimage
                sd_any = True
                
                imagename = re.split('.image$',tpimage)[0]
                # End of single dish imaging part

        outflat_current = False
        convsky_current = False

        if image and len(mstoimage) > 0:

            # for reruns
            foo=mstoimage[0]
            foo=foo.replace(".ms","")
            foo=foo.replace(project,"")
            foo=foo.replace("/","")
            project=project+foo

            imagename = fileroot + "/" + project

            # get nfld, sourcefieldlist, from (interfm) ms if it was not just created
            # TODO make work better for multiple mstoimage (figures below)
            if os.path.exists(mstoimage[0]):
                tb.open(mstoimage[0]+"/SOURCE")
                code = tb.getcol("CODE")
                sourcefieldlist = pl.where(code=='OBJ')[0]
                nfld = len(sourcefieldlist)
                tb.done()
            elif dryrun:
                nfld=1 # HACK
            msfile = mstoimage[0]

            # set cleanmode automatically (for interfm)
            if nfld == 1:
                cleanmode = "csclean"
            else:
                cleanmode = "mosaic"




            # clean insists on using an existing model if its present
            if os.path.exists(imagename+".image"): shutil.rmtree(imagename+".image")
            if os.path.exists(imagename+".model"): shutil.rmtree(imagename+".model")

            # An image in fileroot/ has priority
            if len(modelimage) > 0 and os.path.exists(fileroot+"/"+modelimage):
                modelimage = fileroot + "/" + modelimage
                msg("Found modelimage, %s." % modelimage,origin='simanalyze')

            # in simdata we use imdirection instead of model_refdir
            if not myutil.isdirection(imdirection,halt=False):
                imdirection=model_refdir
        
            myutil.imclean(mstoimage,imagename,
                         cleanmode,cell,imsize,imdirection,
                         interactive,niter,threshold,weighting,
                         outertaper,pbcor,stokes, #sourcefieldlist=sourcefieldlist,
                         modelimage=modelimage,mask=mask,dryrun=dryrun)


            # create imagename.flat and imagename.residual.flat:
            if not dryrun:
                myutil.flatimage(imagename+".image",verbose=verbose)
                myutil.flatimage(imagename+".residual",verbose=verbose)
            outflat_current = True

            # feather
            if featherimage:
                if not os.path.exists(featherimage):
                    raise Exception,"Could not find featherimage "+featherimage
            else:
                featherimage=""
                if tpimage:
                    # if you set modelimage, then it won't force tpimage into 
                    # featherimage.  this could be hard to explain 
                    # to the user.
                    if os.path.exists(tpimage) and not os.path.exists(modelimage):
                        featherimage=tpimage
                    

            if os.path.exists(featherimage):
                msg("feathering the interfermetric image "+imagename+".image with "+featherimage,origin='simanalyze',priority="info")
                from feather import feather 
                # TODO call with params?
                msg("feather('"+imagename+".feather.image','"+imagename+".image','"+featherimage+"')",priority="info")
                if not dryrun:
                    feather(imagename+".feather.image",imagename+".image",featherimage)
                    # copy residual flat image
                    shutil.copytree(imagename+".residual.flat",imagename+".feather.residual.flat")
                    imagename=imagename+".feather"
                    # but replace combined flat image
                    myutil.flatimage(imagename+".image",verbose=verbose)



            if verbose:
                msg(" ")
            msg("done inverting and cleaning",origin='simanalyze')
            if not is_array_type(cell):
                cell = [cell,cell]
            if len(cell) <= 1:
                cell = [qa.quantity(cell[0]),qa.quantity(cell[0])]
            else:
                cell = [qa.quantity(cell[0]),qa.quantity(cell[1])]
            cell = [qa.abs(cell[0]),qa.abs(cell[0])]

            # get beam from output clean image
            if verbose: msg("getting beam from "+imagename+".image",origin='simanalyze')
            if os.path.exists(imagename+".image"):
                ia.open(imagename+".image")
                beam = ia.restoringbeam()
                ia.close()
                # model has units of Jy/pix - calculate beam area from clean image
                # (even if we are not plotting graphics)
                bmarea = beam['major']['value']*beam['minor']['value']*1.1331 #arcsec2
                bmarea = bmarea/(cell[0]['value']*cell[1]['value']) # bm area in pix
                msg("synthesized beam area in output pixels = %f" % bmarea,origin='simanalyze')









        if image:
            # show model, convolved model, clean image, and residual
            if grfile:
                file = fileroot + "/" + project + ".image.png"
            else:
                file = ""
        else:
            mslist=[]

        if dryrun:
            grscreen=False
            grfile=False
            analyze=False

        if image and len(mstoimage) > 0:
            if grscreen or grfile:
                myutil.newfig(multi=[2,2,1],show=grscreen)

                # create regridded and convolved sky model image
                myutil.convimage(modelflat,imagename+".image.flat")
                convsky_current = True # don't remake this for analysis in this run

                disprange = []  # passing empty list causes return of disprange

                # original sky regridded to output pixels but not convolved with beam
                discard = myutil.statim(modelflat+".regrid",disprange=disprange,showstats=False)
                myutil.nextfig()

                # convolved sky model - units of Jy/bm
                disprange = []
                discard = myutil.statim(modelflat+".regrid.conv",disprange=disprange)
                myutil.nextfig()

                # clean image - also in Jy/beam
                # although because of DC offset, better to reset disprange
                disprange = []
                discard = myutil.statim(imagename+".image.flat",disprange=disprange)
                myutil.nextfig()

                if len(mstoimage) > 0:
                    myutil.nextfig()

                    # clean residual image - Jy/bm
                    discard = myutil.statim(imagename+".residual.flat",disprange=disprange)
                myutil.endfig(show=grscreen,filename=file)




        #####################################################################
        # analysis

        if analyze:

            if not os.path.exists(imagename+".image"):
                if os.path.exists(fileroot+"/"+imagename+".image"):
                    imagename=fileroot+"/"+imagename
                else:
                    msg("Can't find a simulated image - expecting "+imagename,priority="error")
                    return False

            # we should have skymodel.flat created above 

            if not image:
                if not os.path.exists(imagename+".image"):
                    msg("you must image before analyzing.",priority="error")
                    return False

                # get beam from output clean image
                if verbose: msg("getting beam from "+imagename+".image",origin="analysis")
                ia.open(imagename+".image")
                beam = ia.restoringbeam()
                ia.close()
                # model has units of Jy/pix - calculate beam area from clean image
                cell = myutil.cellsize(imagename+".image")
                cell= [ qa.convert(cell[0],'arcsec'),
                        qa.convert(cell[1],'arcsec') ]
                # (even if we are not plotting graphics)
                bmarea = beam['major']['value']*beam['minor']['value']*1.1331 #arcsec2
                bmarea = bmarea/(cell[0]['value']*cell[1]['value']) # bm area in pix
                msg("synthesized beam area in output pixels = %f" % bmarea)


            # flat output:?  if the user manually cleaned, this may not exist
            outflat = imagename + ".image.flat"
            if (not outflat_current) or (not os.path.exists(outflat)):
                # create imagename.flat and imagename.residual.flat
                myutil.flatimage(imagename+".image",verbose=verbose)
                if os.path.exists(imagename+".residual"):
                    myutil.flatimage(imagename+".residual",verbose=verbose)
                else:
                    if showresidual:
                        msg(imagename+".residual not found -- residual will not be plotted",priority="warn")
                    showresidual = False
                outflat_current = True

            # regridded and convolved input:?
            if not convsky_current:
                myutil.convimage(modelflat,imagename+".image.flat")
                convsky_current = True

            # now should have all the flat, convolved etc even if didn't run "image" 

            # make difference image.
            # immath does Jy/bm if image but only if ia.setbrightnessunit("Jy/beam") in convimage()
            convolved = modelflat + ".regrid.conv"
            difference = imagename + '.diff'
            diff_ia = ia.imagecalc(difference, "'%s' - '%s'" % (convolved, outflat), overwrite=True)
            diff_ia.setbrightnessunit("Jy/beam")

            # get rms of difference image for fidelity calculation
            #ia.open(difference)
            diffstats = diff_ia.statistics(robust=True, verbose=False,list=False)
            diff_ia.close()
            del diff_ia
            maxdiff = diffstats['medabsdevmed']
            if maxdiff != maxdiff: maxdiff = 0.
            if type(maxdiff) != type(0.):
                if maxdiff.__len__() > 0:
                    maxdiff = maxdiff[0]
                else:
                    maxdiff = 0.
            # Make fidelity image.
            absdiff = imagename + '.absdiff'
            calc_ia = ia.imagecalc(absdiff, "max(abs('%s'), %f)" % (difference,
                                                          maxdiff/pl.sqrt(2.0)), overwrite=True)
            calc_ia.close()
            fidelityim = imagename + '.fidelity'
            calc_ia = ia.imagecalc(fidelityim, "abs('%s') / '%s'" % (convolved, absdiff), overwrite=True)
            calc_ia.close()
            msg("fidelity image calculated",origin="analysis")

            # scalar fidelity
            absconv = imagename + '.absconv'
            calc_ia = ia.imagecalc(absconv, "abs('%s')" % convolved, overwrite=True)
            if ia.isopen(): ia.close() #probably not necessary
            calc_ia.close()
            del calc_ia

            ia.open(absconv)
            modelstats = ia.statistics(robust=True, verbose=False,list=False)
            maxmodel = modelstats['max']
            if maxmodel != maxmodel: maxmodel = 0.
            if type(maxmodel) != type(0.):
                if maxmodel.__len__() > 0:
                    maxmodel = maxmodel[0]
                else:
                    maxmodel = 0.
            ia.close()
            scalarfidel = maxmodel/maxdiff
            msg("fidelity range (max model / rms difference) = "+str(scalarfidel),origin="analysis")


            # now, what does the user want to actually display?

            # need MS for showuv and showpsf
            if not image:
                msfile = fileroot + "/" + project + ".ms"
            elif sd_only:
                # imaged and single dish only
                msfile = tpmstoimage
            # psf is not available for SD only sim
            if os.path.exists(msfile) and myutil.ismstp(msfile,halt=False):
                if showpsf: msg("single dish simulation -- psf will not be plotted",priority='warn')
                showpsf = False
            if (not image) and (not os.path.exists(msfile)):
                if showpsf or showuv:
                    msg("No image is generated in this run. Default MS, '%s', does not exist -- uv and psf will not be plotted" % msfile,priority='warn')
                showpsf = False
                showuv = False
            

            # if the order in the task input changes, change it here too
            figs = [showuv,showpsf,showmodel,showconvolved,showclean,showresidual,showdifference,showfidelity]
            nfig = figs.count(True)
            if nfig > 6:
                msg("only displaying first 6 selected panels in graphic output",priority="warn")
            if nfig <= 0:
                return True
            if nfig < 4:
                multi = [1,nfig,1]
            else:
                if nfig == 4:
                    multi = [2,2,1]
                else:
                    multi = [2,3,1]

            if grfile:
                file = fileroot + "/" + project + ".analysis.png"
            else:
                file = ""

            if grscreen or grfile:
                myutil.newfig(multi=multi,show=grscreen)

                # if order in task parameters changes, change here too
                if showuv:
# TODO loop over all ms - show all UV including zero
                    if len(mslist)>1:
                        msg("Using only "+msfile+" for uv plot",priority="warn",origin='simanalyze')
                    tb.open(msfile)
                    rawdata = tb.getcol("UVW")
                    tb.done()
                    pl.box()
                    maxbase = max([max(rawdata[0,]),max(rawdata[1,])])  # in m
                    klam_m = 300/qa.convert(model_center,'GHz')['value']
                    pl.plot(rawdata[0,]/klam_m,rawdata[1,]/klam_m,'b,')
                    pl.plot(-rawdata[0,]/klam_m,-rawdata[1,]/klam_m,'b,')
                    ax = pl.gca()
                    ax.yaxis.LABELPAD = -4
                    pl.xlabel('u[klambda]',fontsize='x-small')
                    pl.ylabel('v[klambda]',fontsize='x-small')
                    pl.axis('equal')
                    # Add zero-spacing (single dish) if not yet plotted
# TODO make this a check over all ms
#                    if predict_sd and not myutil.ismstp(msfile,halt=False):
#                        pl.plot([0.],[0.],'r,')
                    myutil.nextfig()

                if showpsf:
                    if image:
                        psfim = imagename + ".psf"
                    else:
                        psfim = project + ".quick.psf"
                        if not os.path.exists(psfim):
                            if len(mslist)>1:
                                msg("Using only "+msfile+" for psf generation",priority="warn")
                            im.open(msfile)
                            # TODO spectral parms
                            im.defineimage(cellx=qa.tos(model_cell[0]),nx=max([minimsize,128]))
                            if os.path.exists(psfim):
                                shutil.rmtree(psfim)
                            im.approximatepsf(psf=psfim)
                            # beam is set above (even in "analyze" only)
                            # note that if image, beam has fields 'major' whereas if not, it
                            # has fields like 'bmaj'.
                            # beam=im.fitpsf(psf=psfim)
                            im.done()
                    ia.open(psfim)
                    beamcs = ia.coordsys()
                    beam_array = ia.getchunk(axes=[beamcs.findcoordinate("spectral")['pixel'][0],beamcs.findcoordinate("stokes")['pixel'][0]],dropdeg=True)
                    nn = beam_array.shape
                    xextent = nn[0]*cell_asec*0.5
                    xextent = [xextent,-xextent]
                    yextent = nn[1]*cell_asec*0.5
                    yextent = [-yextent,yextent]
                    flipped_array = beam_array.transpose()
                    ttrans_array = flipped_array.tolist()
                    ttrans_array.reverse()
                    pl.imshow(ttrans_array,interpolation='bilinear',cmap=pl.cm.jet,extent=xextent+yextent,origin="bottom")
                    psfim.replace(project+"/","")
                    pl.title(psfim,fontsize="x-small")
                    b = qa.convert(beam['major'],'arcsec')['value']
                    pl.xlim([-3*b,3*b])
                    pl.ylim([-3*b,3*b])
                    ax = pl.gca()
                    pl.text(0.05,0.95,"bmaj=%7.1e\nbmin=%7.1e" % (beam['major']['value'],beam['minor']['value']),transform = ax.transAxes,bbox=dict(facecolor='white', alpha=0.7),size="x-small",verticalalignment="top")
                    ia.close()
                    myutil.nextfig()

                disprange = []  # first plot will define range
                if showmodel:
                    discard = myutil.statim(modelflat+".regrid",incell=cell,disprange=disprange,showstats=False)
                    myutil.nextfig()
                    disprange = []

                if showconvolved:
                    discard = myutil.statim(modelflat+".regrid.conv")
                    # if disprange gets set here, it'll be Jy/bm
                    myutil.nextfig()

                if showclean:
                    # own scaling because of DC/zero spacing offset
                    discard = myutil.statim(imagename+".image.flat")
                    myutil.nextfig()

                if showresidual:
                    # it gets its own scaling
                    discard = myutil.statim(imagename+".residual.flat")
                    myutil.nextfig()

                if showdifference:
                    # it gets its own scaling.
                    discard = myutil.statim(imagename+".diff")
                    myutil.nextfig()

                if showfidelity:
                    # it gets its own scaling.
                    discard = myutil.statim(imagename+".fidelity",showstats=False)
                    myutil.nextfig()

                myutil.endfig(show=grscreen,filename=file)

            sim_min,sim_max,sim_rms,sim_units = myutil.statim(imagename+".image.flat",plot=False)
            # if not displaying still print stats:
            # 20100505 ia.stats changed to return Jy/bm:
            msg('Simulation rms: '+str(sim_rms/bmarea)+" Jy/pix = "+
                str(sim_rms)+" Jy/bm",origin="analysis")
            msg('Simulation max: '+str(sim_max/bmarea)+" Jy/pix = "+
                str(sim_max)+" Jy/bm",origin="analysis")
            #msg('Simulation rms: '+str(sim_rms)+" Jy/pix = "+
            #    str(sim_rms*bmarea)+" Jy/bm",origin="analysis")
            #msg('Simulation max: '+str(sim_max)+" Jy/pix = "+
            #    str(sim_max*bmarea)+" Jy/bm",origin="analysis")
            msg('Beam bmaj: '+str(beam['major']['value'])+' bmin: '+str(beam['minor']['value'])+' bpa: '+str(beam['positionangle']['value']),origin="analysis")



        # cleanup - delete newmodel, newmodel.flat etc
# flat kept by user request CAS-5509
#        if os.path.exists(imagename+".image.flat"):
#            shutil.rmtree(imagename+".image.flat")
        if os.path.exists(imagename+".residual.flat"):
            shutil.rmtree(imagename+".residual.flat")
        # .flux.pbcoverage is nessesary for feather.
        #if os.path.exists(imagename+".flux.pbcoverage"):
        #    shutil.rmtree(imagename+".flux.pbcoverage")
        absdiff = imagename + '.absdiff'
        if os.path.exists(absdiff):
            shutil.rmtree(absdiff)
        absconv = imagename + '.absconv'
        if os.path.exists(absconv):
            shutil.rmtree(absconv)
#        if os.path.exists(imagename+".diff"):
#            shutil.rmtree(imagename+".diff")
        if os.path.exists(imagename+".quick.psf") and os.path.exists(imagename+".psf"):
            shutil.rmtree(imagename+".quick.psf")

        finalize_tools()
        if myutil.isreport():
            myutil.closereport()


    except TypeError, e:
        finalize_tools()
        #msg("simanalyze -- TypeError: %s" % e,priority="error")
        casalog.post("simanalyze -- TypeError: %s" % e, priority="ERROR")
        raise TypeError, e
        return
Example #44
0
def plotms(vis=None,
           gridrows=None,
           gridcols=None,
           rowindex=None,
           colindex=None,
           plotindex=None,
           xaxis=None,
           xdatacolumn=None,
           yaxis=None,
           ydatacolumn=None,
           yaxislocation=None,
           selectdata=None,
           field=None,
           spw=None,
           timerange=None,
           uvrange=None,
           antenna=None,
           scan=None,
           correlation=None,
           array=None,
           observation=None,
           intent=None,
           feed=None,
           msselect=None,
           averagedata=None,
           avgchannel=None,
           avgtime=None,
           avgscan=None,
           avgfield=None,
           avgbaseline=None,
           avgantenna=None,
           avgspw=None,
           scalar=None,
           transform=None,
           freqframe=None,
           restfreq=None,
           veldef=None,
           shift=None,
           extendflag=None,
           extcorr=None,
           extchannel=None,
           iteraxis=None,
           xselfscale=None,
           yselfscale=None,
           xsharedaxis=None,
           ysharedaxis=None,
           customsymbol=None,
           symbolshape=None,
           symbolsize=None,
           symbolcolor=None,
           symbolfill=None,
           symboloutline=None,
           coloraxis=None,
           customflaggedsymbol=None,
           flaggedsymbolshape=None,
           flaggedsymbolsize=None,
           flaggedsymbolcolor=None,
           flaggedsymbolfill=None,
           flaggedsymboloutline=None,
           plotrange=None,
           title=None,
           titlefont=None,
           xlabel=None,
           xaxisfont=None,
           ylabel=None,
           yaxisfont=None,
           showmajorgrid=None,
           majorwidth=None,
           majorstyle=None,
           majorcolor=None,
           showminorgrid=None,
           minorwidth=None,
           minorstyle=None,
           minorcolor=None,
           showlegend=None,
           legendposition=None,
           plotfile=None,
           expformat=None,
           exprange=None,
           highres=None,
           dpi=None,
           width=None,
           height=None,
           overwrite=None,
           showgui=None,
           clearplots=None,
           callib=None,
           headeritems=None):

    # we'll add these later
    #           extspw=None, extantenna=None,
    #           exttime=None, extscans=None, extfield=None,
    """
    
            Task for plotting and interacting with visibility data.  A variety
        of axes choices (including data column) along with MS selection and
        averaging options are provided for data selection.  Flag extension
        parameters are also available for flagging operations in the plotter.
        
            All of the provided parameters can also be set using the GUI once
        the application has been launched.  Additional and more specific
        operations are available through the GUI and/or through the plotms
        tool (pm).
        

    Keyword arguments:
    vis -- input visibility dataset
           default: ''
    
    gridrows -- Number of subplot rows.
                    default: 1
    gridcols -- Number of subplot columns.
                    default: 1 
    rowindex -- Row location of the subplot (0-based).
                    default: 0
    colindex -- Column location of the subplot (0-based).
                    default: 0          
    plotindex -- Index to address a subplot (0-based).
                    default: 0            
    xaxis, yaxis -- what to plot on the two axes
                    default: '' (uses PlotMS defaults/current set).
        &gt;&gt;&gt; xaxis, yaxis expandable parameters
        xdatacolumn, 
        ydatacolumn -- which data column to use for data axes
                       default: '' (uses PlotMS default/current set).
        yaxislocation -- whether the data should be plotted using the left or right y-axis
                       default: '' (uses PlotMS default).
    iteraxis -- what axis to iterate on when doing iteration plots
                default: ''
              &gt;&gt;&gt; xsharedaxis, ysharedaxis, xselfscale, yselfscale expandable parameters 
        xselfscale -- If true, iterated plots should share a common x-axis label per column.
                       default: False.
        yselfscale -- If true, iterated plots should share a common y-axis label per row.
                       default: False.
        xsharedaxis -- use a common x-axis for vertically aligned plots (must also set xselfscale=True)
                        default: False.
        ysharedaxis -- use a common y-axis for horizontally aligned plots (must also set yselfscale=True)
                        default: False.
    selectdata -- data selection parameters flag
                  (see help par.selectdata for more detailed information)
                  default: False
      &gt;&gt;&gt; selectdata expandable parameters
        field -- select using field ID(s) or field name(s)
                 default: '' (all).
        spw -- select using spectral window/channels
               default: '' (all)
        timerange -- select using time range
                     default: '' (all).
        uvrange -- select using uvrange
                   default: '' (all).
        antenna -- select using antenna/baseline
                   default: '' (all).
        scan -- select using scan number
                default: '' (all).
        correlation -- select using correlations
                       default: '' (all).
        array -- select using (sub)-array range
                 default: '' (all).
        observation -- select by observation ID(s).
                 default: '' (all).
        intent -- select observing intent  
                  default: ''  (no selection by intent)  
                  intent='*BANDPASS*'  (selects data labelled with  
                                        BANDPASS intent)
        feed   -- select feed ID
                  default: '' (all)
                  feed='1~2'
        msselect -- TaQL selection expression
                    default: '' (all).
    
    averagedata -- data averaging parameters flag
                   default: False.
      &gt;&gt;&gt; averagedata expandable parameters
        avgchannel -- average over channel?  either blank for none, or a value
                      in channels.
                      default: '' (none).
        avgtime -- average over time?  either blank for none, or a value in
                   seconds.
                   default: '' (none).
        avgscan -- average over scans?  only valid if time averaging is turned
                   on.
                   default: False.
        avgfield -- average over fields?  only valid if time averaging is
                    turned on.
                    default: False.
        avgbaseline -- average over all baselines?  mutually exclusive with
                       avgantenna.
                       default: False.
        avgantenna -- average by per-antenna?  mutually exclusive with
                      avgbaseline.
                      default: False.
        avgspw -- average over all spectral windows?
                  default: False.
    
    extendflag -- have flagging extend to other data points?
                  default: False.
      &gt;&gt;&gt; extendflag expandable parameters
        extcorr -- extend flags based on correlation?  blank = none.
                          default: ''.
        extchannel -- extend flags based on channel?
                      default: False.
        extspw -- extend flags based on spw?
                  default: False.
        extantenna -- extend flags based on antenna?  should be either blank,
                      'all' for all baselines, or an antenna-based value.
                      default: ''.
        exttime -- extend flags based on time (within scans)?
                   default: False.
        extscans -- extend flags based on scans?  only valid if time extension
                    is turned on.
                    default: False.
        extfield -- extend flags based on field?  only valid if time extension
                    is turned on.
                    default: False.
        showgui -- Whether or not to display the plotting GUI
                  default: True; example showgui=False

    coloraxis -- which axis to use for colorizing
                     default: ''  (ignored - same as colorizing off)              
    
    title  -- title along top of plot (called "canvas" in some places)
    titlefont -- plot title font size
                 default: 0 (autosize depending on grid)
    exprange -- Export all iteration plots ('all') or only the current one.
                    default: '' (only export the current iteration plot)
    xlabel, ylabel -- text to label horiz. and vert. axes, with formatting (%% and so on)
    xaxisfont, yaxisfont -- int for axis font size
    
    showlegend -- show a legend on the plot
                    default: False
    legendposition -- position for the legend.  Legends can be interior or exterior to the plot
                    Interior legends can be located in the upper right, lower right, upper left, or lower left.
                    Exterior legends can be located on the right, left, top, or bottom.
                    default: 'upperright'
    clearplots -- clear existing plots so that the new ones coming in can replace them.                 
    callib -- calibration library string, list of strings, or filename for on-the-fly calibration
    headeritems -- string of comma-separated page header items keywords

    """
    # Check if DISPLAY environment variable is set.
    if os.getenv('DISPLAY') == None:
        casalog.post(
            'ERROR: DISPLAY environment variable is not set!  Cannot open plotms.',
            'SEVERE')
        return False

    if plotfile:
        if not os.path.dirname(plotfile):
            # CAS-7148: Use dir that user cd'ed to in casapy session
            # instead of dir that
            plotfile = os.path.join(os.getcwd(), plotfile)
        if (os.path.exists(plotfile) and not overwrite):
            casalog.post(
                "Plot file " + plotfile +
                " exists and overwrite is false, cannot write the file",
                "SEVERE")
            return False

    try:
        # Check synonyms
        # format is:  synonym['new_term'] = 'existing_term', with
        # the existing term being what's coded in PlotMSConstants.h  (case insensitive)
        # CAS-8532 match capitalization in axis names in GUI
        synonyms = {}
        synonyms['Scan'] = 'scan'
        synonyms['Field'] = 'field'
        synonyms['Time'] = 'time'
        synonyms['timeinterval'] = synonyms['timeint'] = synonyms[
            'time_interval'] = synonyms['Interval'] = 'interval'
        synonyms['Spw'] = 'spw'
        synonyms['chan'] = synonyms['Channel'] = 'channel'
        synonyms['freq'] = synonyms['Frequency'] = 'frequency'
        synonyms['vel'] = synonyms['Velocity'] = 'velocity'
        synonyms['correlation'] = synonyms['Corr'] = 'corr'
        synonyms['ant1'] = synonyms['Antenna1'] = 'antenna1'
        synonyms['ant2'] = synonyms['Antenna2'] = 'antenna2'
        synonyms['Baseline'] = 'baseline'
        synonyms['Row'] = 'row'
        synonyms['Observation'] = 'observation'
        synonyms['Intent'] = 'intent'
        synonyms['Feed1'] = 'feed1'
        synonyms['Feed2'] = 'feed2'
        synonyms['amplitude'] = synonyms['Amp'] = 'amp'
        synonyms['Phase'] = 'phase'
        synonyms['Real'] = 'real'
        synonyms['imaginary'] = synonyms['Imag'] = 'imag'
        synonyms['weight'] = synonyms['Wt'] = synonyms['Weight'] = 'wt'
        synonyms['wtamp'] = synonyms['Wt*Amp'] = 'wtamp'
        synonyms['weightspectrum'] = synonyms['WtSp'] = synonyms[
            'WeightSpectrum'] = 'wtsp'
        synonyms['Sigma'] = 'sigma'
        synonyms['sigmaspectrum'] = synonyms['SigmaSpectrum'] = synonyms[
            'SigmaSp'] = 'sigmasp'
        synonyms['Flag'] = 'flag'
        synonyms['FlagRow'] = 'flagrow'
        synonyms['UVdist'] = 'uvdist'
        synonyms['uvdistl'] = synonyms['uvdist_l'] = synonyms[
            'UVwave'] = 'uvwave'
        synonyms['U'] = 'u'
        synonyms['V'] = 'v'
        synonyms['W'] = 'w'
        synonyms['Uwave'] = 'uwave'
        synonyms['Vwave'] = 'vwave'
        synonyms['Wwave'] = 'wwave'
        synonyms['Azimuth'] = 'azimuth'
        synonyms['Elevation'] = 'elevation'
        synonyms['hourang'] = synonyms['HourAngle'] = 'hourangle'
        synonyms['parang'] = synonyms['parallacticangle'] = synonyms[
            'ParAngle'] = 'parangle'
        synonyms['ant'] = synonyms['Antenna'] = 'antenna'
        synonyms['Ant-Azimuth'] = 'ant-azimuth'
        synonyms['Ant-Elevation'] = 'ant-elevation'
        synonyms['ant-parallacticangle'] = synonyms['ant-parang'] = synonyms[
            'Ant-ParAngle'] = 'ant-parangle'
        synonyms['gamp'] = synonyms['gainamp'] = synonyms[
            'GainAmp'] = 'Gain Amplitude'
        synonyms['gphase'] = synonyms['gainphase'] = synonyms[
            'GainPhase'] = 'Gain Phase'
        synonyms['greal'] = synonyms['gainreal'] = synonyms[
            'GainReal'] = 'Gain Real'
        synonyms['gimag'] = synonyms['gainimag'] = synonyms[
            'GainImag'] = 'Gain Imag'
        synonyms['del'] = synonyms['delay'] = synonyms['Delay'] = 'delay'
        synonyms['swp'] = synonyms['swpower'] = synonyms[
            'switchedpower'] = synonyms['SwPower'] = synonyms[
                'spgain'] = 'swpower'
        synonyms['tsys'] = synonyms['Tsys'] = synonyms['TSYS'] = 'tsys'
        synonyms['opac'] = synonyms['opacity'] = synonyms['Opac'] = 'opac'
        synonyms['snr'] = synonyms['SNR'] = 'SNR'
        synonyms['radialvelocity'] = synonyms[
            'Radial Velocity'] = 'Radial Velocity [km/s]'
        synonyms['rho'] = synonyms['Distance'] = 'Distance (rho) [km]'

        if (synonyms.has_key(xaxis)): xaxis = synonyms[xaxis]
        if type(yaxis) is str:
            if (synonyms.has_key(yaxis)): yaxis = synonyms[yaxis]
        elif type(yaxis) is list:
            for index, axis in enumerate(yaxis):
                if (synonyms.has_key(axis)): yaxis[index] = synonyms[axis]
        if type(coloraxis) is str:
            if (synonyms.has_key(coloraxis)): coloraxis = synonyms[coloraxis]

        # synonyms for data columns (only one, so just hardcode it)
        if (xdatacolumn == 'cor' or xdatacolumn == 'corr'):
            xdatacolumn = 'corrected'
        if (ydatacolumn == 'cor' or ydatacolumn == 'corr'):
            ydatacolumn = 'corrected'

        vis = vis.strip()
        if len(vis) > 0:
            vis = os.path.abspath(vis)
            if not os.path.exists(vis):
                casalog.post('\n'.join(['Input file not found:', vis]),
                             "SEVERE")
                return False

        if not plotindex:
            plotindex = 0
        if plotindex < 0:
            casalog.post("A negative plotindex is not valid.", "SEVERE")
            return False
        if clearplots and plotindex > 0:
            casalog.post(
                "A nonzero plotindex is not valid when clearing plots.",
                "SEVERE")
            return False

        # start plotms with the procmgr, use logfile
        myframe = stack_frame_find()
        if myframe['casa']['state']['init_version'] > 0:
            from casa_system import procmgr
            pmsrun = procmgr.running("plotms")
            pms = procmgr.fetch("plotms")
            try:
                if pmsrun and not pms.is_alive():  # crash!
                    pms.stop()
                    pmsrun = False
            except AttributeError:  # fetch failed: pms=None
                pass
            if not pmsrun:
                # start (or restart)
                plotmsApp = 'casaplotms'
                for dir in os.getenv('PATH').split(':'):
                    dd = dir + os.sep + plotmsApp
                    if os.path.exists(dd) and os.access(dd, os.X_OK):
                        plotmsApp = dd
                        break
                try:
                    logfile = myframe['casa']['files']['logfile']
                except KeyError:
                    logfile = ""
                procmgr.create("plotms", [
                    plotmsApp, "--nogui", "--nopopups", "--casapy",
                    "--logfilename=" + logfile
                ])
                if procmgr.running("plotms"):
                    # connect future calls to this plotms
                    procpid = procmgr.fetch('plotms').pid
                    pm.setPlotmsPid(procpid)

        # Determine whether this is going to be a scripting client or
        # a full GUI supporting user interaction.  This must be done
        # before other properties are set because it affects the
        # constructor of plotms.
        pm.setShowGui(showgui)

        if pm.isDrawing() and clearplots:
            casalog.post(
                "Plotms is running in GUI mode and cannot be run again until the current drawing completes.",
                "SEVERE")
            return False

        #Clear any existing plots.
        if clearplots:
            pm.clearPlots()

        gridChange = False
        if gridrows > 0 or gridcols > 0:
            gridChange = True
            if not gridrows:
                gridrows = 1
            if not gridcols:
                gridcols = 1

        if gridChange:
            pm.setGridSize(gridrows, gridcols)
        pm.setPlotMSFilename(vis, False, plotindex)

        if type(yaxis) is tuple:
            yaxis = yaxis[0]

        if not yaxis or type(yaxis) is str:
            if not yaxislocation or not type(yaxislocation) is str:
                yaxislocation = 'left'
            if not ydatacolumn or not type(ydatacolumn) is str:
                ydatacolumn = ''
            if not yaxis:
                yaxis = ''

            pm.setPlotAxes(xaxis, yaxis, xdatacolumn, ydatacolumn,
                           yaxislocation, False, plotindex, 0)

        else:
            yAxisCount = len(yaxis)

            yDataCount = 0
            if ydatacolumn != ['']:
                yDataCount = len(ydatacolumn)
            yLocationCount = 0
            if yaxislocation != ['']:
                yLocationCount = len(yaxislocation)
            '''Make sure all the y-axis values are unique.'''
            uniqueY = True
            for i in range(0, yAxisCount):
                yDataColumnI = ''
                if i < yDataCount:
                    yDataColumnI = ydatacolumn[i]
                for j in range(0, i):
                    if yaxis[j] == yaxis[i]:
                        yDataColumnJ = ''
                        if j < yDataCount:
                            yDataColumnJ = ydatacolumn[j]
                        if yDataColumnI == yDataColumnJ:
                            uniqueY = False
                            break
                if not uniqueY:
                    break
            if (uniqueY):
                for i in range(0, yAxisCount):
                    yDataColumn = ''
                    if i < yDataCount:
                        yDataColumn = ydatacolumn[i]
                    yAxisLocation = 'left'
                    if i < yLocationCount:
                        yAxisLocation = yaxislocation[i]
                    pm.setPlotAxes(xaxis, yaxis[i], xdatacolumn, yDataColumn,
                                   yAxisLocation, False, plotindex, i)
            else:
                raise Exception, 'Please remove duplicate y-axes.'

        # Set selection
        if (selectdata and os.path.exists(vis)):
            pm.setPlotMSSelection(field, spw, timerange, uvrange,
                                  antenna, scan, correlation, array,
                                  str(observation), intent, feed, msselect,
                                  False, plotindex)
        else:
            pm.setPlotMSSelection('', '', '', '', '', '', '', '', '', '', '',
                                  '', False, plotindex)

        # Set averaging
        if not averagedata:
            avgchannel = avgtime = ''
            avgscan = avgfield = avgbaseline = avgantenna = avgspw = False
            scalar = False
        if avgbaseline and avgantenna:
            casalog.post(
                'Averaging over baselines is mutually exclusive with per-antenna averaging.',
                "SEVERE")
            return False
        if avgchannel and (float(avgchannel) < 0.0):
            casalog.post('Cannot average negative number of channels',
                         "SEVERE")
            return False
        try:
            if avgtime and (float(avgtime) < 0.0):
                casalog.post('Cannot average negative time value', "SEVERE")
                return False
        except ValueError:
            casalog.post(
                'avgtime value must be numerical string in seconds (no units)',
                "SEVERE")
            return False
        pm.setPlotMSAveraging(avgchannel, avgtime, avgscan, avgfield,
                              avgbaseline, avgantenna, avgspw, scalar, False,
                              plotindex)
        # Set transformations
        if not transform:
            freqframe = ''
            restfreq = ''
            veldef = 'RADIO'
            shift = [0.0, 0.0]

        pm.setPlotMSTransformations(freqframe, veldef, restfreq, shift[0],
                                    shift[1], False, plotindex)

        # Set calibration: None, string (filename), dictionary
        useCallib = False
        callibString = ''
        if isinstance(callib, str):
            # Determine if filename or string of params
            if '=' in callib:
                useCallib = True
                callibString = callib
            else:
                callibFile = callib.strip()
                if len(callibFile) > 0:
                    callibFile = os.path.abspath(callib)
                    if os.path.exists(callibFile):
                        useCallib = True
                        callibString = callibFile
                    else:
                        casalog.post("Callib file does not exist")
                        raise RuntimeError("Callib file does not exist")
        elif isinstance(callib, list):
            if len(callib[0]
                   ) > 0:  # no param is a list (default in plotms.xml?)
                useCallib = True
                callibString = ",".join(callib)
        pm.setPlotMSCalibration(useCallib, callibString, False, plotindex)

        # Set flag extension
        # for now, some options here are not available:
        # pm.setFlagExtension(extendflag, extcorrelation, extchannel, extspw, extantenna, exttime, extscans, extfield)
        extcorrstr = ''
        if extcorr:
            extcorrstr = 'all'
        pm.setFlagExtension(extendflag, extcorrstr, extchannel)

        # Export range
        if not exprange or exprange == "":
            exprange = 'current'
        pm.setExportRange(exprange)
        if not dpi:
            dpi = -1
        if not width:
            width = -1
        if not height:
            height = -1

        # Set stuff that informs the plot on additional axes
        #  (iteration, colorization, etc.)
        # (Iteration)
        if not iteraxis:
            iteraxis = ""
        if iteraxis == "":
            xselfscale = yselfscale = False
            xsharedaxis = ysharedaxis = False
        if not rowindex:
            rowindex = 0
        if not colindex:
            colindex = 0
        if not xselfscale:
            xselfscale = False
        if not yselfscale:
            yselfscale = False
        if not xsharedaxis:
            xsharedaxis = False
        if not ysharedaxis:
            ysharedaxis = False
        if not xselfscale and xsharedaxis:
            casalog.post(
                "Plots cannot share an x-axis unless they use the same x-axis scale.",
                "ERROR")
            return False
        if not yselfscale and ysharedaxis:
            casalog.post(
                "Plots cannot share a y-axis unless they use the same y-axis scale.",
                "ERROR")
            return False
        pm.setPlotMSIterate(iteraxis, rowindex, colindex, xselfscale,
                            yselfscale, xsharedaxis, ysharedaxis, False,
                            plotindex)

        # (Colorization)
        if coloraxis:
            pm.setColorAxis(coloraxis, False, plotindex)

        # Set custom symbol
        # Make the custom symbol into a list if it is not already.
        if type(customsymbol) is bool and customsymbol:
            customSymbolValue = customsymbol
            customsymbol = [customSymbolValue]

        if type(symbolshape) is str:
            symbolValue = symbolshape
            symbolshape = [symbolValue]

        if type(symbolsize) is int:
            symbolValue = symbolsize
            symbolsize = [symbolValue]

        if type(symbolcolor) is str:
            symbolValue = symbolcolor
            symbolcolor = [symbolValue]

        if type(symbolfill) is str:
            symbolValue = symbolfill
            symbolfill = [symbolValue]

        if type(symboloutline) is bool:
            symbolValue = symboloutline
            symboloutline = [symbolValue]

        if type(customsymbol) is list:
            customSymbolCount = len(customsymbol)
            for i in range(0, customSymbolCount):

                if i >= len(symbolshape) or not symbolshape[i]:
                    symbolShapeI = 'autoscaling'
                else:
                    symbolShapeI = symbolshape[i]
                symbolShape = symbolShapeI

                if customsymbol[i]:
                    if i >= len(symbolsize) or not symbolsize[i]:
                        symbolSizeI = 2
                    else:
                        symbolSizeI = symbolsize[i]
                    symbolSize = symbolSizeI

                    if i >= len(symbolcolor) or not symbolcolor[i]:
                        symbolColorI = '0000ff'
                    else:
                        symbolColorI = symbolcolor[i]
                    symbolColor = symbolColorI

                    if i >= len(symbolfill) or not symbolfill[i]:
                        symbolFillI = 'fill'
                    else:
                        symbolFillI = symbolfill[i]
                    symbolFill = symbolFillI

                    if type(symboloutline) is bool:
                        symbolOutlineI = symboloutline
                    elif type(symboloutline) is list:
                        if i >= len(symboloutline) or not symboloutline[i]:
                            symbolOutlineI = False
                        else:
                            symbolOutlineI = symboloutline[i]
                    else:
                        symbolOutlineI = False
                    symbolOutline = symbolOutlineI

                else:
                    symbolSize = 2
                    symbolColor = '0000ff'
                    symbolFill = 'fill'
                    symbolOutline = False
                pm.setSymbol(symbolShape, symbolSize, symbolColor, symbolFill,
                             symbolOutline, False, plotindex, i)

        # Set custom flagged symbol
        if type(customflaggedsymbol) is bool and customflaggedsymbol:
            customSymbolValue = customflaggedsymbol
            customflaggedsymbol = [customSymbolValue]

        if type(flaggedsymbolshape) is str:
            symbolValue = flaggedsymbolshape
            flaggedsymbolshape = [symbolValue]

        if type(flaggedsymbolsize) is int:
            symbolValue = flaggedsymbolsize
            flaggedsymbolsize = [symbolValue]

        if type(flaggedsymbolcolor) is str:
            symbolValue = flaggedsymbolcolor
            flaggedsymbolcolor = [symbolValue]

        if type(flaggedsymbolfill) is str:
            symbolValue = flaggedsymbolfill
            flaggedsymbolfill = [symbolValue]

        if type(flaggedsymboloutline) is bool:
            symbolValue = flaggedsymboloutline
            flaggedsymboloutline = [symbolValue]

        if type(customflaggedsymbol) is list:
            customSymbolCount = len(customflaggedsymbol)
            for i in range(0, customSymbolCount):
                if i >= len(flaggedsymbolshape) or not flaggedsymbolshape[i]:
                    flaggedSymbolShapeI = 'nosymbol'
                else:
                    flaggedSymbolShapeI = flaggedsymbolshape[i]
                flaggedSymbolShape = flaggedSymbolShapeI

                if customflaggedsymbol[i]:

                    if i >= len(flaggedsymbolsize) or not flaggedsymbolsize[i]:
                        flaggedSymbolSizeI = 2
                    else:
                        flaggedSymbolSizeI = flaggedsymbolsize[i]
                    flaggedSymbolSize = flaggedSymbolSizeI

                    if i >= len(
                            flaggedsymbolcolor) or not flaggedsymbolcolor[i]:
                        flaggedSymbolColorI = 'ff0000'
                    else:
                        flaggedSymbolColorI = flaggedsymbolcolor[i]
                    flaggedSymbolColor = flaggedSymbolColorI

                    if i >= len(flaggedsymbolfill) or not flaggedsymbolfill[i]:
                        flaggedSymbolFillI = 'fill'
                    else:
                        flaggedSymbolFillI = flaggedsymbolfill[i]
                    flaggedSymbolFill = flaggedSymbolFillI

                    if type(flaggedsymboloutline) is bool:
                        flaggedSymbolOutlineI = flaggedsymboloutline
                    elif type(flaggedsymboloutline) is list:
                        if i >= len(flaggedsymboloutline
                                    ) or not flaggedsymboloutline[i]:
                            flaggedSymbolOutlineI = False
                        else:
                            flaggedSymbolOutlineI = flaggedsymboloutline[i]
                    else:
                        flaggedSymbolOutlineI = False
                    flaggedSymbolOutline = flaggedSymbolOutlineI
                else:
                    flaggedSymbolSize = 2
                    flaggedSymbolColor = 'ff0000'
                    flaggedSymbolFill = 'fill'
                    flaggedSymbolOutline = False
                pm.setFlaggedSymbol(flaggedSymbolShape, flaggedSymbolSize,
                                    flaggedSymbolColor, flaggedSymbolFill,
                                    flaggedSymbolOutline, False, plotindex, i)

        #Determine if there should be a legend.
        if not showlegend:
            showlegend = False
        if not legendposition:
            legendposition = 'upperRight'
        pm.setLegend(showlegend, legendposition, False, plotindex)

        # Set various user-directed appearance parameters
        pm.setTitle(title, False, plotindex)
        pm.setTitleFont(titlefont, False, plotindex)
        pm.setXAxisLabel(xlabel, False, plotindex)
        pm.setXAxisFont(xaxisfont, False, plotindex)
        pm.setYAxisLabel(ylabel, False, plotindex)
        pm.setYAxisFont(yaxisfont, False, plotindex)
        pm.setGridParams(showmajorgrid, majorwidth, majorstyle, majorcolor,
                         showminorgrid, minorwidth, minorstyle, minorcolor,
                         False, plotindex)

        #Plot range
        if (len(plotrange) != 4):
            if (len(plotrange) == 0):
                plotrange = [0.0, 0.0, 0.0, 0.0]
            else:
                raise Exception, 'plotrange parameter has incorrect number of elements.'

        xrange = plotrange[1] - plotrange[0]
        yrange = plotrange[3] - plotrange[2]

        pm.setXRange((xrange <= 0.), plotrange[0], plotrange[1], False,
                     plotindex)
        pm.setYRange((yrange <= 0.), plotrange[2], plotrange[3], False,
                     plotindex)

        # Page Header Items
        # Python keywords for specifying header items are defined in CAS-8082,
        # Erik's comment dated 9-jun-2016
        # Python / C++ header items keywords map
        # format is header_cpp_kw['python_keyword'] = 'c++_keyword', where
        # the c++ keyword is what's coded in PlotMSPageHeaderParam.h
        header_cpp_kw = {}
        header_cpp_kw['filename'] = 'filename'
        header_cpp_kw['ycolumn'] = 'y_columns'
        header_cpp_kw['obsdate'] = 'obs_start_date'
        header_cpp_kw['obstime'] = 'obs_start_time'
        header_cpp_kw['observer'] = 'obs_observer'
        header_cpp_kw['projid'] = 'obs_project'
        header_cpp_kw['telescope'] = 'obs_telescope_name'
        header_cpp_kw['targname'] = 'target_name'
        header_cpp_kw['targdir'] = 'target_direction'

        if type(headeritems) is str:
            cpp_headeritems = []
            for headeritem_word in headeritems.split(','):
                py_headeritem = headeritem_word.strip()
                if py_headeritem == "":
                    continue
                if py_headeritem in header_cpp_kw:
                    ccp_headeritem = header_cpp_kw[py_headeritem]
                    cpp_headeritems.append(ccp_headeritem)
                else:
                    casalog.post(
                        "Ignoring invalid page header item: " + py_headeritem,
                        "WARN")

            pm.setPlotMSPageHeaderItems(','.join(cpp_headeritems), False,
                                        plotindex)

        # Update
        plotUpdated = pm.update()
        if not plotUpdated:
            casalog.post("There was a problem updating the plot.")
        else:
            # write file if requested
            if (plotfile != ""):
                casalog.post("Plot file " + plotfile, 'NORMAL')
                time.sleep(0.5)
                if (pm.isDrawing()):
                    casalog.post(
                        "Will wait until drawing of the plot has completed before exporting it",
                        'NORMAL')
                    while (pm.isDrawing()):
                        time.sleep(1.0)
                casalog.post("Exporting the plot.", 'NORMAL')
                casalog.post("Calling pm.save", 'NORMAL')
                plotUpdated = pm.save(plotfile, expformat, highres, dpi, width,
                                      height)

    except Exception, instance:
        plotUpdated = False
        print "Exception during plotms task: ", instance
Example #45
0
import sys
import os
import string
from locatescript import copydata
from locatescript import locatescript
from casa_stack_manip import stack_frame_find

gl = stack_frame_find()


# Short description
def description():
    return "Test of a complete ALMA SV data analysis (2 ASDMs from Sept 2011)"


pass_on = {
    "asdm_dataset_name": "uid___A002_X2a5c2f_X54",
    "asdm_dataset2_name": "uid___A002_X2a5c2f_X220",
    "tsys_table": 'cal-tsys_X54.fdm',
    "tsys_table2": 'cal-tsys_X220.fdm',
    "mask1": 'M100cont-orig.mask',
    "mask2": 'M100line-orig.mask',
    "mask3": 'test-M100line-orig.mask'
}


def data():
    ### return the data files that are needed by the regression script
    return [
        pass_on["asdm_dataset_name"], pass_on["asdm_dataset2_name"],
        pass_on["tsys_table"], pass_on["tsys_table2"], pass_on["mask1"],
Example #46
0
def tget(task=None, savefile=''):
    """ Get last input values from file on disk for a specified task: 

	task -- Name of task
		default: <unset>; example: task='bandpass'
		<Options: type tasklist() for the complete list>
	savefile -- Output file for the task inputs
		default: task.last then task.saved
		example: savefile=task.orion

	"""
    try:
        myf = stack_frame_find()
        if task == None:
            if myf.has_key('task'):
                task = myf['task']
            else:
                if myf.has_key('taskname'): task = myf['taskname']
        myf['taskname'] = task
        myf['task'] = task
        if type(task) != str:
            task = task.__name__
            myf['task'] = task
            myf['taskname'] = task
        parameter_checktype(['task', 'savefile'], [task, savefile], [str, str])
        parameter_checkmenu('task', task, tasksum.keys())

        ###Check if task exists by checking if task_defaults is defined
        if (not myf.has_key(task)
                and str(type(myf[task])) != "<type 'instance'>"
                and not hasattr(myf[task], "defaults")):
            raise TypeError, "task %s is not defined " % task

        if task == None: task = myf['task']
        myf['task'] = task
        #f=zip(myf[taskname].func_code.co_varnames,myf[taskname].func_defaults)
        #for j in range(len(f)):
        #        k=f[j][0]
        #	print 'k is ',k
        #	stst = myf[k]
        #	if ( type(stst) == str ):
        #		sex = k+"='"+stst+"'"
        #	else:
        #		sex = k+"="+str(stst)
        #	exec(sex)
        if savefile == '':
            savefile = task + '.last'
            try:
                taskparameterfile = open(savefile, 'r')
            except:
                savefile = task + '.saved'
                try:
                    taskparameterfile = open(savefile, 'r')
                except:
                    print "Sorry - no task.last or .saved"
                    return

            taskparameterfile.close()
        execfile(savefile)
        # Put the task parameters back into the global namespace
        f = zip(myf[task].__call__.func_code.co_varnames[1:],
                myf[task].__call__.func_defaults)
        missing_ks = []
        for j in f:
            k = j[0]
            if k != 'self':
                try:
                    myf[k] = eval(k)
                except NameError:
                    missing_ks.append(k)
        if missing_ks:
            print "Did not find a saved value for",
            if len(missing_ks) > 1:
                print ', '.join(missing_ks[:-1]),
                print 'or', missing_ks[-1]
            else:
                print missing_ks[0]
            print "The set of task parameters has probably changed"
            print "since", savefile, "was written."
        print "Restored parameters from file " + savefile
    except TypeError, e:
        print "tget --error: ", e
Example #47
0
    def __call__(
        self,
        IDI=None,
        OUTPUTIDI=None,
        DiFXinput=None,
        DiFXcalc=None,
        doIF=None,
        linAntIdx=None,
        Range=None,
        ALMAant=None,
        spw=None,
        calAPP=None,
        calAPPTime=None,
        APPrefant=None,
        gains=None,
        interpolation=None,
        gainmode=None,
        XYavgTime=None,
        dterms=None,
        amp_norm=None,
        XYadd=None,
        XYdel=None,
        XYratio=None,
        usePcal=None,
        swapXY=None,
        swapRL=None,
        feedRotation=None,
        correctParangle=None,
        IDI_conjugated=None,
        plotIF=None,
        plotRange=None,
        plotAnt=None,
        excludeAnts=None,
        excludeBaselines=None,
        doSolve=None,
        solint=None,
        doTest=None,
        npix=None,
        solveAmp=None,
        solveMethod=None,
        calstokes=None,
        calfield=None,
    ):
        """\n\nVersion 1.8.2 -- Converts VLBI visibilities polarization basis.

        Detailed Description:
\n\nVersion 1.8.2 -- Converts VLBI visibilities from mixed-polarization (linear-circular) into circular basis. Works with single VLBI stations as well as with calibrated phased arrays (i.e., phased ALMA).\n\n
        Arguments :
                IDI: Input FITS-IDI file with VLBI visibilities. It can also be a direcotry containing SWIN files from DiFX.
                   Default Value: 

                OUTPUTIDI: Output FITS-IDI file (or SWIN directory). If equal to IDI, the file(s) will be overwritten
                   Default Value: 

                DiFXinput: If SWIN files are being converted, this must be the *.input file used by DiFX.
                   Default Value: 

                DiFXcalc: If SWIN files are being converted, this must be the *.calc file used by DiFX. This is optional, but if it is not provided, the cross-polarization gain estimates may be incorrect if doSolve>0.
                   Default Value: 

                doIF: List of IFs to convert. Default means all.
                   Default Value: []

                linAntIdx: List of indices of the linear-polarization antennas in the IDI file (lowest index starts with 1)
                   Default Value: [1]

                Range: Time range to convert (integer list; AIPS format). Default means all data
                   Default Value: []

                ALMAant: If ALMA has been used, this is the antenna table from the MS with the intra-ALMA visibilities.
                   Default Value: 

                spw: Spectral window in ALMAvis that contains the VLBI band. If negative, the program will derive it automatically.
                   Default Value: -1

                calAPP: If ALMA has been used, this is the combined ASDM_CALAPPPHASE table from the ASDM. The list of measurement sets can also be given (so the table is concatenated from all of them).
                   Default Value: 

                calAPPTime: Time shift and time tolerance (in sec) for the CALAPPPHASE table obtained from the ASDM.
                   Default Value: [0.,5.]

                APPrefant: If not empty, re-reference the TelCal phases, assuming that the X-Y phase-difference table provided in \'gains\' (see keyword below) uses APPrefant as the reference antenna. Notice that the name of the gain table with the X-Y phase differences has to contain the string \'.XY0\'.
                   Default Value: 

                gains: Gain tables to pre-calibrate the linear-pol VLBI stations (one list of gains per linear-pol station).
                   Default Value: [["NONE"]]

                interpolation:  Interpolation type to use (one per calibration table). Tells whether to apply linear or nearest interpolation. Default is to use linear for all tables.
                   Default Value: []

                gainmode:  Mode of gain calibration to impose (one per calibration table). Default is \'T\' for all tables, unless either the string \'XY0\', \'bandpass\' or \'Gxyamp\' appears in the table name. The gain types can be either \'G\' (i.e., split gains per polarization) or \'T\' (i.e., combine polarizations).
                   Default Value: []

                XYavgTime:  Re-compute the G-mode gains by adding a time smoothing of X-Y differences to the T-mode gains. Default is NOT to do this (i.e., use the G-mode gains as given). If positive, use a running time average with this size (in seconds).
                   Default Value: 0.0

                dterms: D-term tables to pre-calibrate the linear-pol VLBI stations (one table per linear-pol station).
                   Default Value: ["NONE"]

                amp_norm: If positive, normalize the amplitude correction to the X-Y average, and save the scaling factor (vs time) in an external (ASCII) file (ANTAB format, assuming a DPFU=amp_norm). If zero, or negative, apply the amplitude correction as is.
                   Default Value: 0.01

                XYadd: Add manually a phase between X and Y before conversion (in deg.). Either a list with one value per linear-pol station OR a list of lists (i.e., one value per IF for each antenna) OR a list of lists of lists (one value per channel, for each IF, for each linear-polarization antenna).
                   Default Value: {}

                XYdel: Add manually a multiband delay between X and Y before conversion (in deg./chanel). One value per linear-pol station.
                   Default Value: {}

                XYratio: Add manually an amplitude ratio between X and Y before conversion (R=X/Y). Follows the same format as XYadd. If a negative value is given for an antenna, the X/Y ratio will be estimated from its autocorrelations (the spectrum for antenna i will be computed using a running-median filter of width equal to -1/XYratio[i] of the IF bandwidth). If 0.0 is given for an antenna, the ratio will be estimated from the phasecal amplitudes (as long as usePcal is True).
                   Default Value: {}

                usePcal: List of booleans (one boolean per linear-polarization station). If True, use the X-Y difference of phasecal tones as an estimate of the X-Y cross-polarization phase. Default means to NOT use the phasecals.
                   Default Value: []

                swapXY: Swap X-Y before conversion. One boolean per linear-pol VLBI station.
                   Default Value: [False]

                swapRL: Swap R-L of the OTHER antenna(s) when plotting the fringes.
                   Default Value: True

                feedRotation: Rotation angle of the feed (one value per antenna, in degrees). Default means zero degrees (so that X is truly horizontal for the linear-pol. antennas). These angles are used in the gain-solver step.
                   Default Value: []

                correctParangle: If True, the correction for parallactic angle is applied to the converted antenna(s).
                   Default Value: False

                IDI_conjugated: Assume a swap in the baseline defintion (i.e., conjugation) of the FITS-IDI file. This has NO effect on SWIN files. 
                   Default Value: False

                plotIF: IF index(es) to plot. Default means to NOT plot. An empty list, [], means to plot ALL IFs being converted (but do not forget to set plotRange and plotAnt!).
                   Default Value: -1

                plotRange: Time range to plot (integer list; AIPS format). Default means to NOT plot
                   Default Value: []

                plotAnt: Index of the other antenna in the baseline to plot. Default means to NOT plot.
                   Default Value: -1

                excludeAnts: List of antennas (i.e., list of antenna codenames) to NOT use in the cross-polarization gain estimates.
                   Default Value: []

                excludeBaselines: List of baselines (i.e., a list of lists of two antenna codenames) to NOT use in the cross-polarization gain estimates.
                   Default Value: []

                doSolve: If negative, do not estimate the cross-polarization gains. If positive or zero, estimate the gains using a Global Cross-Pol Fringe Fitting (GCPFF). The gains are fitted with an error function (Chi Square) defined as:\n\n sum( doSolve*(RR/LL-1)^2 + (RL^2 + LR^2) ),\n\n so that doSolve=0 minimizes the cross-hand polarizations (so it assumes a small linear polarization of the source), whereas doSolve>>1 assumes a negligible Stokes V.
                   Default Value: -1

                solint: If solint[0] null or negative, solve the cross-polarization phase plus a multi-band delay (MBD). If not, solve in bandpass mode by averaging solint[0] channels per solution.\n Divide the solution time range (per scan) in solint[1] chunks (i.e., in solint[1] subscans). I.e., if solint[1]==1, the fringes are fully averaged in time for each scan (but corrected for the scan fringe rates) before the GPLFF condition is computed. solint[2] is the minimum time jump (in seconds) to split the data into different scans (default: 100 seconds).
                   Default Value: [1,1]

                doTest: If true, only compute (and eventually plot), the data, but leave OUTPUTIDI untouched.
                   Default Value: True

                npix: Number of pixels for the fringe plots (and fringe search).
                   Default Value: 50

                solveAmp: if the cross-polarization gains are being estimated, solve also for the X/Y amplitude ratios.
                   Default Value: True

                solveMethod: Method for the minimization of the Chi squared in the GCPFF. Can be \'gradient\', \'Levenberg-Marquardt\' or \'COBYLA\'.
                   Default Value: gradient

                calstokes: Stokes parameters, [I,Q,U,V] of the calibrator (of course, this is only used if doSolve is not negative). The total intensity is not needed in the calibration (i.e., calstokes[0] can be left to 1.0, so that the other parameters will correspond to fractional polarization). 
                   Default Value: [1.,0.,0.,0.]

                calfield: If not negative, field ID of the calibrator (useful if a time range covering several scans is being used in the GCPFF). If negative, use all data in the time range, regardless of the field ID.
                   Default Value: -1

        Returns: bool

        Example :


For more information about the internals of PolConvert, please read:

Marti-Vidal et al. 2016, Astronomy and Astrophysics, 587, 143 

PROCEDURE:

If a VLBI antenna used linear-feed receivers, PolConvert must first
estimate the X-Y cross gain for that antenna (phase, amplitude, 
and multi-band delay) before the final conversion. Use the plotting
option of PolConvert to plot a selected scan of a given baseline and 
that scan will be used to estimate the cross-polarization gains. 
PolConvert returns a list with the amplitude and phase cross-gains 
for all the antennas, as long as it is run in plotting mode. The user 
can then set these gain lists to XYadd and XYratio for a second run 
of PolConvert.

Given an FITS-IDI file (or set of SWIN DiFX files) with phased-ALMA 
observations, the user must first calibrate completely (i.e., in full 
polarization) the corresponding ALMA measurement set. It is important 
to create it using asis='CALAPPPHASE' in the importasdm task. 

If more than one asdm was created in the observations, the user must 
concatenate all the CALAPPPHASE tables of each asdm, for PolConvert to 
work properly. This can be done with the following commands (we assume 
here that MSLIST is a python list with the names of the measurement sets 
for each asdm):

for i,myms in enumerate(MSLIST):
  if i==0:
    os.system('cp -rf %s/ASDM_CALAPPPHASE ./CALAPPPHASE.tab'%myms)
  else:
    tb.open('%s/ASDM_CALAPPPHASE'%myms)
    tb.copyrows('./CALAPPPHASE.tab')
    tb.close()

These lines will create the table './CALAPPPHASE.tab', to be used by 
PolConvert (i.e., the table specified in the 'calAPP' keyword).

PolConvert can also do this for you, if you set calAPP = MSLIST. But
check carefully the information that it will print, regarding the 
time coverage of both the CALAPPPHASE table and the MSs.

Let us assume that the calibration tables are named 'gain.tb' and 
'bandpass.tb' (there can be many others, for delay, XY-phase, etc.) and 
the D-term table is called 'dterms.tb'. Then, with this assumption:

- If ALMA is the only station with linear receivers (let's say it is 
station number 1 in the FITS-IDI file), the call to PolConvert should be 
done with the following keyword values:

- linAntIdx = [1]

- Range = []    # i.e., all data will be converted

- ALMAvis = 'TheALMAvisibilities.ms' 
 
- spw = 0  # it may be a good idea to split first the science spw, 
           # before concatenating and calibrating the ms

- calAPP = './CALAPPPHASE.tab'

- calAPPTime = [0., 5.0]   # The CALAPP entries sometime start and end
                           # with time lags in between. The time tolerance
                           # of 5 seconds should avoid problems related
                           # to this.

- gains = [['gain.tb','bandpass.tb']]
- dterms = ['dterms.tb']

- doTest = False # to actually APPLY the changes, not only compute them!


###################
   
     SPECIAL CASE 1:

If there was a second antenna with linear-polarization receivers, 
it can also be converted, but has to be listed after ALMA. Let us assume
that this antenna has id=4 in the FITS-IDI file. Then:

- linAntIdx = [1,4]   # i.e., ALMA plus the other linear-pol. station.

- gains = [ ['gain.tb','bandpass.tb'] , ['NONE'] ]

     # Notice that 'NONE' can be used to tell PolConvert not to apply
     # any calibration to antenna #4 before conversion.

- dterms = [ 'dterms.dt' , 'NONE']


###################
   
     SPECIAL CASE 2:

If the user wants to check the conversion before applying it, PolConvert
can plot the fringes for a given IF, baseline and time range. Let us 
assume we want to plot the baseline to antenna 2 (i.e., baseline 1-2) in 
the time range 0-07:30:00 to 0-07:31:00 (AIPS format). Then:

- doTest = True  # i.e., do NOT write on the FITS-IDI file!

- plotIF = 1  # i.e., first IF in FITS-IDI file.

- plotRange = [0,7,30,0,0,7,31,0]

- Range = [0,7,30,0,0,7,31,0]  # i.e., to not waste resources computing
                               # things that we will not save nor plot.

- plotAnt = 2  


###################
   
     SPECIAL CASE 2:

For the two linear-pol antennas, use the pcal tones to correct the 
phase difference between X and Y. In addition to this, for the first 
antenna, the X/Y relative amplitude is estimated from the phasecal 
tones. For the second antenna, the X/Y amplitude spectrum is 
estimated from the autocorrelations, using a running median
filter of 51 channels (if the number of channels per IF is 510):


- usePcal = [True,True]
- XYratio = [-10., 0.0]


Notice that, if the GCPFF algorithm is used to solve for the X/Y gains,
these will be stored in the 'XYratio' and 'XYadd' keys of the returned
dictionary, whereas the a-priori gains computed from the pcals and the
autocorrelations will be stored as complex arrays in 'aPrioriXYGain'.



###################
   
     OTHER SPECIAL CASES (NOT FULLY TESTED):

1.- If two antennas have linear-pol receivers (i.e., ALMA plus another one)
and the second one was correlated with the pol. channels swapped, then:

- swapXY = [False, True]

If it was ALMA the antenna with swapped pol. channels, then:

- swapXY = [True, False]



2.- If the second antenna with linear-pol receivers had an offset of, say,
65 degrees between X and Y, this offset can be corrected before conversion:

- XYadd = [0.0, 65.]

  If there are 4 IFs and the X-Y phases for the second antenna differ among
  IFs, we can set them in the following way:

- XYadd = [[0.0, 0.0, 0.0, 0.0], [65., 30., 25., 10.]]


NOTICE THAT POLCONVERT CAN ESTIMATE THE XYADD AND XYRATIO, FROM 
THE SCAN THAT YOU ASK IT TO PLOT. IF IT IS A SCAN OF A STRONG CALIBRATOR,
POLCONVERT CAN ESTIMATE ALL THESE QUANTITIES FOR YOUR VLBI STATIONS WITH
LINEAR FEEDS.








#########################################
#########################################
##### A TYPICAL ALMA DATA REDUCTION #####

1.- Import all ASDMs into measurement sets. 
    BEWARE THAT YOU SET asis='CalAppPhase'

2.- Find out the spw that matches the VLBI frequency setup. Split it for
    each measurement set.

3.- Concatenate the splitted measurement sets. 

4.- Concatenate the 'ASDM_CALAPPPHASE' tables of the measurement sets
    into a new CALAPPPHASE TABLE (see help above).

5.- Calibrate the concatenated measurement set (full-polarization 
    calibration) using standard ALMA procedures.

6.- Execute polconvert, feeding it with the calibration tables and 
    the CALAPPPHASE table, in mode "doTest = True", and applying it
    only to a short (say, 1-2 minutes) scan of a strong source 
    (e.g., the phase calibrator). Select a given IF and antenna 
    to plot (it's better to select a short VLBI baseline to ALMA).

7.- The program will plot the fringes and print an estimate of the 
    extra X/Y phase that should be added to ALMA. This number should 
    be small, as long as the calibration is OK. If a large number 
    is found, and you see large cross-hand polarizations, add this 
    extra X-Y phase to polconvert, via the keyword "XYadd"

8.- Re-execute polconvert in test mode. Check whether the conversion 
    is satisfactory.

9.- Once satisfied with the conversion of the selected calibrator scan, 
    execute polconvert over the shole dataset with "doTest = False".

10.- It may be a good idea to do extra sanity checks, like the 
    possible dependence of XYadd with IF and/or its eventual time 
    evolution. All these effects should have been properly corrected 
    if the measurement set calibration was successful. Different 
    XYadd phases can be added to different IFs by converting each IF
    separately.


# END OF POLCONVERT DOCUMENTATION
#######################################



        """
        if not hasattr(self, "__globals__") or self.__globals__ == None:
            self.__globals__ = stack_frame_find()
        #casac = self.__globals__['casac']
        casalog = self.__globals__['casalog']
        casa = self.__globals__['casa']
        #casalog = casac.casac.logsink()
        self.__globals__['__last_task'] = 'polconvert'
        self.__globals__['taskname'] = 'polconvert'
        ###
        self.__globals__['update_params'](func=self.__globals__['taskname'],
                                          printtext=False,
                                          ipython_globals=self.__globals__)
        ###
        ###
        #Handle globals or user over-ride of arguments
        #
        if type(self.__call__.func_defaults) is NoneType:
            function_signature_defaults = {}
        else:
            function_signature_defaults = dict(
                zip(self.__call__.func_code.co_varnames[1:],
                    self.__call__.func_defaults))
        useLocalDefaults = False

        for item in function_signature_defaults.iteritems():
            key, val = item
            keyVal = eval(key)
            if (keyVal == None):
                #user hasn't set it - use global/default
                pass
            else:
                #user has set it - use over-ride
                if (key != 'self'):
                    useLocalDefaults = True

        myparams = {}
        if useLocalDefaults:
            for item in function_signature_defaults.iteritems():
                key, val = item
                keyVal = eval(key)
                exec('myparams[key] = keyVal')
                self.parameters[key] = keyVal
                if (keyVal == None):
                    exec('myparams[key] = ' + key + ' = self.itsdefault(key)')
                    keyVal = eval(key)
                    if (type(keyVal) == dict):
                        if len(keyVal) > 0:
                            exec('myparams[key] = ' + key +
                                 ' = keyVal[len(keyVal)-1][\'value\']')
                        else:
                            exec('myparams[key] = ' + key + ' = {}')

        else:
            print ''

            myparams['IDI'] = IDI = self.parameters['IDI']
            myparams['OUTPUTIDI'] = OUTPUTIDI = self.parameters['OUTPUTIDI']
            myparams['DiFXinput'] = DiFXinput = self.parameters['DiFXinput']
            myparams['DiFXcalc'] = DiFXcalc = self.parameters['DiFXcalc']
            myparams['doIF'] = doIF = self.parameters['doIF']
            myparams['linAntIdx'] = linAntIdx = self.parameters['linAntIdx']
            myparams['Range'] = Range = self.parameters['Range']
            myparams['ALMAant'] = ALMAant = self.parameters['ALMAant']
            myparams['spw'] = spw = self.parameters['spw']
            myparams['calAPP'] = calAPP = self.parameters['calAPP']
            myparams['calAPPTime'] = calAPPTime = self.parameters['calAPPTime']
            myparams['APPrefant'] = APPrefant = self.parameters['APPrefant']
            myparams['gains'] = gains = self.parameters['gains']
            myparams['interpolation'] = interpolation = self.parameters[
                'interpolation']
            myparams['gainmode'] = gainmode = self.parameters['gainmode']
            myparams['XYavgTime'] = XYavgTime = self.parameters['XYavgTime']
            myparams['dterms'] = dterms = self.parameters['dterms']
            myparams['amp_norm'] = amp_norm = self.parameters['amp_norm']
            myparams['XYadd'] = XYadd = self.parameters['XYadd']
            myparams['XYdel'] = XYdel = self.parameters['XYdel']
            myparams['XYratio'] = XYratio = self.parameters['XYratio']
            myparams['usePcal'] = usePcal = self.parameters['usePcal']
            myparams['swapXY'] = swapXY = self.parameters['swapXY']
            myparams['swapRL'] = swapRL = self.parameters['swapRL']
            myparams['feedRotation'] = feedRotation = self.parameters[
                'feedRotation']
            myparams['correctParangle'] = correctParangle = self.parameters[
                'correctParangle']
            myparams['IDI_conjugated'] = IDI_conjugated = self.parameters[
                'IDI_conjugated']
            myparams['plotIF'] = plotIF = self.parameters['plotIF']
            myparams['plotRange'] = plotRange = self.parameters['plotRange']
            myparams['plotAnt'] = plotAnt = self.parameters['plotAnt']
            myparams['excludeAnts'] = excludeAnts = self.parameters[
                'excludeAnts']
            myparams['excludeBaselines'] = excludeBaselines = self.parameters[
                'excludeBaselines']
            myparams['doSolve'] = doSolve = self.parameters['doSolve']
            myparams['solint'] = solint = self.parameters['solint']
            myparams['doTest'] = doTest = self.parameters['doTest']
            myparams['npix'] = npix = self.parameters['npix']
            myparams['solveAmp'] = solveAmp = self.parameters['solveAmp']
            myparams['solveMethod'] = solveMethod = self.parameters[
                'solveMethod']
            myparams['calstokes'] = calstokes = self.parameters['calstokes']
            myparams['calfield'] = calfield = self.parameters['calfield']

        result = None

        #
        #    The following is work around to avoid a bug with current python translation
        #
        mytmp = {}

        mytmp['IDI'] = IDI
        mytmp['OUTPUTIDI'] = OUTPUTIDI
        mytmp['DiFXinput'] = DiFXinput
        mytmp['DiFXcalc'] = DiFXcalc
        mytmp['doIF'] = doIF
        mytmp['linAntIdx'] = linAntIdx
        mytmp['Range'] = Range
        mytmp['ALMAant'] = ALMAant
        mytmp['spw'] = spw
        mytmp['calAPP'] = calAPP
        mytmp['calAPPTime'] = calAPPTime
        mytmp['APPrefant'] = APPrefant
        mytmp['gains'] = gains
        mytmp['interpolation'] = interpolation
        mytmp['gainmode'] = gainmode
        mytmp['XYavgTime'] = XYavgTime
        mytmp['dterms'] = dterms
        mytmp['amp_norm'] = amp_norm
        mytmp['XYadd'] = XYadd
        mytmp['XYdel'] = XYdel
        mytmp['XYratio'] = XYratio
        mytmp['usePcal'] = usePcal
        mytmp['swapXY'] = swapXY
        mytmp['swapRL'] = swapRL
        mytmp['feedRotation'] = feedRotation
        mytmp['correctParangle'] = correctParangle
        mytmp['IDI_conjugated'] = IDI_conjugated
        mytmp['plotIF'] = plotIF
        mytmp['plotRange'] = plotRange
        mytmp['plotAnt'] = plotAnt
        mytmp['excludeAnts'] = excludeAnts
        mytmp['excludeBaselines'] = excludeBaselines
        mytmp['doSolve'] = doSolve
        mytmp['solint'] = solint
        mytmp['doTest'] = doTest
        mytmp['npix'] = npix
        mytmp['solveAmp'] = solveAmp
        mytmp['solveMethod'] = solveMethod
        mytmp['calstokes'] = calstokes
        mytmp['calfield'] = calfield
        pathname = "file:///home/marti/WORKAREA/LAUNCHPAD/PolConvert/polconvertsd/"
        trec = casac.casac.utils().torecord(pathname + 'polconvert.xml')

        casalog.origin('polconvert')
        try:
            #if not trec.has_key('polconvert') or not casac.casac.utils().verify(mytmp, trec['polconvert']) :
            #return False

            casac.casac.utils().verify(mytmp, trec['polconvert'], True)
            scriptstr = ['']
            saveinputs = self.__globals__['saveinputs']

            # Save .last file for this task execution. MPI servers don't write it (CASR-329).
            from mpi4casa.MPIEnvironment import MPIEnvironment
            do_full_logging = MPIEnvironment.is_mpi_disabled_or_client()
            if type(self.__call__.func_defaults) is NoneType:
                saveinputs = ''
            else:
                saveinputs('polconvert',
                           'polconvert.last',
                           myparams,
                           self.__globals__,
                           scriptstr=scriptstr,
                           do_save_inputs=do_full_logging)

            tname = 'polconvert'
            spaces = ' ' * (18 - len(tname))
            casalog.post('\n##########################################' +
                         '\n##### Begin Task: ' + tname + spaces + ' #####')
            # Don't do telemetry from MPI servers (CASR-329)
            if do_full_logging and casa['state']['telemetry-enabled']:
                #casalog.poststat('Begin Task: ' + tname)
                task_starttime = str(datetime.datetime.now())
            if type(self.__call__.func_defaults) is NoneType:
                casalog.post(scriptstr[0] + '\n', 'INFO')
            else:
                casalog.post(scriptstr[1][1:] + '\n', 'INFO')

            # Effective call to the task as defined in gcwrap/python/scripts/task_*
            result = polconvert(
                IDI, OUTPUTIDI, DiFXinput, DiFXcalc, doIF, linAntIdx, Range,
                ALMAant, spw, calAPP, calAPPTime, APPrefant, gains,
                interpolation, gainmode, XYavgTime, dterms, amp_norm, XYadd,
                XYdel, XYratio, usePcal, swapXY, swapRL, feedRotation,
                correctParangle, IDI_conjugated, plotIF, plotRange, plotAnt,
                excludeAnts, excludeBaselines, doSolve, solint, doTest, npix,
                solveAmp, solveMethod, calstokes, calfield)

            if do_full_logging and casa['state']['telemetry-enabled']:
                task_endtime = str(datetime.datetime.now())
                casalog.poststat('Task ' + tname + ' complete. Start time: ' +
                                 task_starttime + ' End time: ' + task_endtime)
            casalog.post('##### End Task: ' + tname + '  ' + spaces +
                         ' #####' +
                         '\n##########################################')

        except Exception, instance:
            if (self.__globals__.has_key('__rethrow_casa_exceptions')
                    and self.__globals__['__rethrow_casa_exceptions']):
                raise
            else:
                #print '**** Error **** ',instance
                tname = 'polconvert'
                casalog.post('An error occurred running task ' + tname + '.',
                             'ERROR')
                pass
Example #48
0
def saveinputs(taskname=None, outfile='', myparams=None, ipython_globals=None, scriptstr=['']):
    #parameter_printvalues(arg_names,arg_values,arg_types)
    """ Save current input values to file on disk for a specified task:

    taskname -- Name of task
        default: <unset>; example: taskname='bandpass'
        <Options: type tasklist() for the complete list>
    outfile -- Output file for the task inputs
        default: taskname.saved; example: outfile=taskname.orion

    """

    try:
        if ipython_globals == None:
            myf=stack_frame_find( )
        else:
            myf=ipython_globals

        if taskname==None: taskname=myf['taskname']
        myf['taskname']=taskname
        if type(taskname)!=str:
            taskname=taskname.__name__
            myf['taskname']=taskname

        parameter_checktype(['taskname','outfile'],[taskname,outfile],[str,str])

        ###Check if task exists by checking if task_defaults is defined
        obj = False
        if ( not myf.has_key(taskname) and
             str(type(myf[taskname])) != "<type 'instance'>" and
             not hasattr(myf[taskname],"defaults") ):
            raise TypeError, "task %s is not defined " %taskname
        else:
            obj = myf[taskname]

        if taskname==None: taskname=myf['taskname']
        myf['taskname']=taskname
        if outfile=='': outfile=taskname+'.saved'
        if(myf.has_key('__multibackup') and myf['__multibackup']):
            backupoldfile(outfile)
        
        ##make sure unfolded parameters get their default values
        myf['update_params'](func=myf['taskname'], printtext=False, ipython_globals=myf)
        ###
        do_save_inputs = False
        outpathdir = os.path.realpath(os.path.dirname(outfile))
        outpathfile = outpathdir + os.path.sep + os.path.basename(outfile)
        if outpathfile not in casa['state']['unwritable'] and outpathdir not in casa['state']['unwritable']:
            try:
                taskparameterfile=open(outfile,'w')
                print >>taskparameterfile, '%-15s    = "%s"'%('taskname', taskname)
                do_save_inputs = True
            except:
                print "********************************************************************************"
                print "Warning: no write permission for %s, cannot save task" % outfile
                if os.path.isfile(outfile):
                    print "         inputs in %s..." % outpathfile
                    casa['state']['unwritable'].add(outpathfile)
                elif not os.path.isdir(outfile):
                    print "         inputs in dir %s..." % outpathdir
                    casa['state']['unwritable'].add(outpathdir)
                else:
                    print "         inputs because given file (%s) is a dir..." % outpathfile
                print "********************************************************************************"
        f=zip(myf[taskname].__call__.func_code.co_varnames[1:],myf[taskname].__call__.func_defaults)
        scriptstring='#'+str(taskname)+'('
	if myparams == None :
		myparams = {}
        l=0
        for j in range(len(f)):
            k=f[j][0]
	    if not myparams.has_key(k) and k != 'self' :
		    myparams[k] = myf[taskname].parameters[k]
            if(k != 'self' and type(myparams[k])==str):
                if ( myparams[k].count( '"' ) < 1 ):
                    # if the string doesn't contain double quotes then
                    # use double quotes around it in the parameter file.
                    if do_save_inputs:
                        print >>taskparameterfile, '%-15s    =  "%s"'%(k, myparams[k])
                    scriptstring=scriptstring+k+'="'+myparams[k]+'",'
                else:
                    # use single quotes.
                    if do_save_inputs:
                        print >>taskparameterfile, "%-15s    =  '%s'"%(k, myparams[k])
                    scriptstring=scriptstring+k+"='"+myparams[k]+"',"
            else :
                if ( j != 0 or k != "self" or
                     str(type(myf[taskname])) != "<type 'instance'>" ) :
                    if do_save_inputs:
                        print >>taskparameterfile, '%-15s    =  %s'%(k, myparams[k])
                    scriptstring=scriptstring+k+'='+str(myparams[k])+','

            ###Now delete varianle from global user space because
            ###the following applies: "It would be nice if one
            ### could tell the system to NOT recall
            ### previous non-default settings sometimes."
            if(not myf['casaglobals'] and myf.has_key(k)):
                del myf[k]
            l=l+1
            if l%5==0:
                scriptstring=scriptstring+'\n        '
        scriptstring=scriptstring.rstrip()
        scriptstring=scriptstring.rstrip('\n')
        scriptstring=scriptstring.rstrip(',')
        scriptstring=scriptstring+')'        
        scriptstr.append(scriptstring)
        scriptstring=scriptstring.replace('        ', '')
        scriptstring=scriptstring.replace('\n', '')
        if do_save_inputs:
            print >>taskparameterfile,scriptstring
            taskparameterfile.close()
    except TypeError, e:
        print "saveinputs --error: ", e