Example #1
0
def sdsmooth(infile=None, datacolumn=None, antenna=None, 
              field=None, spw=None, timerange=None, scan=None, 
              pol=None, intent=None, reindex=None,
              kernel=None, kwidth=None,
              outfile=None, overwrite=None):

    casalog.origin('sdsmooth')

    try:
        if len(outfile) == 0:
            errmsg = 'outfile is empty.'
            raise_exception(errmsg)
        
        if (os.path.exists(outfile)) and (not overwrite):
            errmsg = outfile+' exists.'
            raise_exception(errmsg)

        sdms.open(infile)
        sdms.set_selection(spw=spw, field=field, 
                           antenna=antenna,
                           timerange=timerange, scan=scan,
                           polarization=pol, intent=intent,
                           reindex=reindex)
        sdms.smooth(type=kernel, width=kwidth, datacolumn=datacolumn, outfile=outfile)
        
        # Write to HISTORY of outfile MS
        param_names = sdsmooth.func_code.co_varnames[:sdsmooth.func_code.co_argcount]
        param_vals = [eval(p) for p in param_names]
        write_history(ms, outfile, 'sdsmooth', param_names,
                      param_vals, casalog)

    except Exception, instance:
        raise Exception, instance
Example #2
0
def mbdevtest(objname='aaa'):
    depth = [s[3] for s in inspect.stack()].index('<module>')
    mbglobals = sys._getframe(depth).f_globals
    taskname = sys._getframe().f_code.co_name
    logger.origin(taskname)

    mbglobals[objname] = 'spam!'
Example #3
0
def importmiriad(mirfile='', vis='', tsys=False, spw='all', vel='', linecal=False, wide='all', debug=0):

        """Convert a Miriad visibility file into a CASA MeasurementSet

        importmiriad(mirfile='ngc5921.uv', vis='ngc5921.ms',tsys=True)

 
        """

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

        mytmp['mirfile'] = mirfile
        mytmp['vis'] = vis
        mytmp['tsys'] = tsys
        mytmp['spw'] = spw
        mytmp['vel'] = vel
        mytmp['linecal'] = linecal
        mytmp['wide'] = wide
        mytmp['debug'] = debug
	pathname='file:///'+os.environ.get('CASAPATH').split()[0]+'/'+os.environ.get('CASAPATH').split()[1]+'/xml/'
        trec = casac.utils().torecord(pathname+'importmiriad.xml')

        casalog.origin('importmiriad')
        if trec.has_key('importmiriad') and casac.utils().verify(mytmp, trec['importmiriad']) :
	    result = task_importmiriad.importmiriad(mirfile, vis, tsys, spw, vel, linecal, wide, debug)

	else :
	  result = False
        return result
Example #4
0
def mbplotmap(raster='data', contour='fit', label=''):
    depth = [s[3] for s in inspect.stack()].index('<module>')
    mbglobals = sys._getframe(depth).f_globals
    taskname = sys._getframe().f_code.co_name
    logger.origin(taskname)

    mbmp = mbglobals['__mbmaps__'][label]
    mg_daz, mg_del = mbmp.getmeshgrid()

    ax = plt.gca()
    ax.set_xlim([np.min(mg_daz), np.max(mg_daz)])
    ax.set_ylim([np.min(mg_del), np.max(mg_del)])
    ax.set_xlabel('dAz (arcsec)')
    ax.set_ylabel('dEl (arcsec)')

    # raster map
    mp = mbmp[raster].data
    mp_raster = np.ma.array(mp, mask=np.isnan(mp))
    ax.pcolormesh(mg_daz, mg_del, mp_raster, cmap='inferno')

    # contour map (optional)
    try:
        mp_contour = mbmp[contour].data
        ax.contour(mg_daz, mg_del, mp_contour)
    except:
        logger.post('fit map is not contained', 'WARN')
Example #5
0
def mbundomap(label=''):
    depth = [s[3] for s in inspect.stack()].index('<module>')
    mbglobals = sys._getframe(depth).f_globals
    taskname = sys._getframe().f_code.co_name
    logger.origin(taskname)

    mbglobals['__mbmaps__'].undo(label)
Example #6
0
def hanningsmooth(
    vis=None,
    outputvis=None,
    keepmms=None,
    field=None,
    spw=None,
    scan=None,
    antenna=None,
    correlation=None,
    timerange=None,
    intent=None,
    array=None,
    uvrange=None,
    observation=None,
    feed=None,
    datacolumn=None,
):
    """Hanning smooth frequency channel data to remove Gibbs ringing

    """

    casalog.origin('hanningsmooth')

    # Initiate the helper class
    pdh = ParallelDataHelper("hanningsmooth", locals())

    # Validate input and output parameters
    try:
        pdh.setupIO()
    except Exception, instance:
        casalog.post('%s' % instance, 'ERROR')
        return False
Example #7
0
def msmoments(infile, moments, antenna, field, spw, includemask, excludemask,
              outfile, overwrite):

    retms = None
    casalog.origin('msmoments')

    # CAS-5410 Use private tools inside task scripts
    ms = gentools(['ms'])[0]

    try:
        if ((not overwrite) and (len(moments) == 1)):
            if (os.path.exists(outfile)):
                raise Exception(outfile + " exists.")
        ms.open(infile)
        retms = ms.moments(moments=moments,
                           antenna=antenna,
                           field=field,
                           spw=spw,
                           includemask=includemask,
                           excludemask=excludemask,
                           outfile=outfile,
                           overwrite=overwrite)
        ms.close()
        retms.close()
    except Exception, instance:
        ms.close()
        if retms: retms.close()
        print '*** Error ***', instance
        raise Exception, instance
Example #8
0
def plotprofilemap(imagename=None, figfile=None, overwrite=None, transparent=None,
                   pol=None, spectralaxis=None, restfreq=None, plotrange=None, title=None, 
                   linecolor=None, linestyle=None, linewidth=None,
                   separatepanel=None, plotmasked=None, maskedcolor=None, 
                   showaxislabel=None, showtick=None, showticklabel=None,
                   figsize=None, numpanels=None):
    casalog.origin('plotprofilemap')
    
    try:
        if len(figfile) > 0 and os.path.exists(figfile) and overwrite == False:
            raise RuntimeError('overwrite is False and output file exists: \'%s\''%(figfile))
    
        image = SpectralImage(imagename)
        npol = len(image.stokes)
        if pol < 0 or pol > npol - 1:
            raise RuntimeError('pol {pol} is out of range (Stokes axis {stokes})'.format(pol=pol,stokes=image.stokes))        

        parsed_size = parse_figsize(figsize)
        nx, ny = parse_numpanels(numpanels)
        if (not isinstance(restfreq, str)) or len(restfreq) == 0:
            restfreq = None 
        plot_profile_map(image, figfile, pol, spectralaxis, restfreq, title, linecolor, linestyle, linewidth,
                         separatepanel, plotmasked, maskedcolor,
                         showaxislabel, showtick, showticklabel, parsed_size,
                         nx, ny, transparent, plotrange)
    except Exception, e:
        casalog.post('Error: %s'%(str(e)), priority='SEVERE')
        import traceback
        casalog.post(traceback.format_exc(), priority='DEBUG')
        raise e
Example #9
0
def mblistmap():
    depth = [s[3] for s in inspect.stack()].index('<module>')
    mbglobals = sys._getframe(depth).f_globals
    taskname = sys._getframe().f_code.co_name
    logger.origin(taskname)

    mbmps = mbglobals['__mbmaps__']
    keys_history = sorted([key for key in mbmps.keys() if type(key) == int])
    keys_pin = sorted([key for key in mbmps.keys() if type(key) == str])
    textcases = lambda N: '{0} {1}'.format(N, ['map is', 'maps are'][N > 1])

    # list scan(s) in history
    logger.post('{0} stored as history:'.format(textcases(len(keys_history))))
    logger.post('')
    for key in keys_history:
        hd = mbmps[key]['data'].header
        logger.post('label: {0} {1}'.format(key, '(latest)' * (key == 0)))
        logger.post('- shape: {0} grid x {1} grid'.format(
            hd['NAXIS1'], hd['NAXIS2']))
        logger.post('- origin: {0}'.format(hd['ORIGIN']))
        logger.post('- taskflow: {0}'.format(hd['TASKFLOW']))
        logger.post('')

    # list pinned scan(s)
    logger.post('{0} pinned:'.format(textcases(len(keys_pin))))
    logger.post('')
    for key in keys_pin:
        hd = mbmps[key]['data'].header
        logger.post("label: '{0}'".format(key))
        logger.post('- shape: {0} grid x {1} grid'.format(
            hd['NAXIS1'], hd['NAXIS2']))
        logger.post('- origin: {0}'.format(hd['ORIGIN']))
        logger.post('- taskflow: {0}'.format(hd['TASKFLOW']))
        logger.post('')
Example #10
0
def mbconvertmaptoscan(label=''):
    depth = [s[3] for s in inspect.stack()].index('<module>')
    mbglobals = sys._getframe(depth).f_globals
    taskname = sys._getframe().f_code.co_name
    logger.origin(taskname)

    logger.post('not implemented yet!')
Example #11
0
def msuvbin(vis=None,
            field=None,
            spw=None,
            taql=None,
            outvis=None,
            phasecenter=None,
            nx=None,
            ny=None,
            cell=None,
            ncorr=None,
            nchan=None,
            fstart=None,
            fstep=None,
            wproject=None,
            memfrac=None):

    casalog.origin('msuvbin ')
    cmd = 'msuvbin '
    pnames = msuvbin.func_code.co_varnames[:msuvbin.func_code.co_argcount]
    for p in pnames:
        pval = eval(p)
        if (not ((pval == '') or (pval == None))):
            if (str(pval).find(' ') > 0):
                pval = '"' + str(pval) + '"'
            if (p == 'cell'):
                cmd = cmd + ' ' + 'cellx' + '=' + str(
                    pval) + ' ' + 'celly' + '=' + str(pval)
            else:
                cmd = cmd + ' ' + p + '=' + str(pval)

##############3
    print 'command ', cmd

    ############
    os.system(cmd)
Example #12
0
def visstat2(vis=None,
             axis=None,
             datacolumn=None,
             useflags=None,
             spw=None,
             field=None,
             selectdata=None,
             antenna=None,
             uvrange=None,
             timerange=None,
             correlation=None,
             scan=None,
             array=None,
             observation=None,
             timeaverage=None,
             timebin=None,
             timespan=None,
             maxuvwdistance=None,
             disableparallel=None,
             ddistart=None,
             taql=None,
             monolithic_processing=None,
             intent=None,
             reportingaxes=None):

    casalog.origin('visstat2')
    casalog.post(
        '"visstat2" has been renamed "visstat"; use of visstat2 is deprecated; please replace calls to visstat2 with calls to visstat',
        priority='INFO')

    return visstat(vis, axis, datacolumn, useflags, spw, field, selectdata,
                   antenna, uvrange, timerange, correlation, scan, array,
                   observation, timeaverage, timebin, timespan, maxuvwdistance,
                   disableparallel, ddistart, taql, monolithic_processing,
                   intent, reportingaxes)
Example #13
0
def mbopenfits(mbfits, mode='readonly', memmap=False):
    depth = [s[3] for s in inspect.stack()].index('<module>')
    mbglobals = sys._getframe(depth).f_globals
    taskname = sys._getframe().f_code.co_name
    logger.origin(taskname)

    mbfits = os.path.expanduser(mbfits)
    fitsname = os.path.basename(mbfits)
    logger.post('MBFITS: {0}'.format(fitsname))

    try:
        f = fits.open(mbfits, mode, memmap)
        logger.post('MBFITS is successfully opened')
    except:
        logger.post('an error occured', 'ERROR')

    mbsc = MBScan.fromfits(f)
    mbsc.recordtask(taskname)

    if '__mbfits__' in mbglobals.keys():
        mbglobals['__mbfits__'].close()
        del mbglobals['__mbfits__']

    mbglobals['__mbfits__'] = f
    mbglobals['__mbscans__'].append(mbsc)

    logger.post('MBFITS is now stored as __mbfits__')
    logger.post('MBScan is now stored as __mbscans__[0]')
Example #14
0
def mbmathscan(expression='SC0+SC1', label0='', label1=''):
    depth = [s[3] for s in inspect.stack()].index('<module>')
    mbglobals = sys._getframe(depth).f_globals
    taskname = sys._getframe().f_code.co_name
    logger.origin(taskname)

    mbsc_0 = mbglobals['__mbscans__'][0 if label0 == '' else label0]
    mbsc_1 = mbglobals['__mbscans__'][1 if label1 == '' else label1]
    sc_0 = mbsc_0['data'].data
    sc_1 = mbsc_1['data'].data

    # place holder
    expression = expression.lower()
    phlist_0 = ['l0', 'label0', 's0', 'sc0', 'scan0']
    phlist_1 = ['l1', 'label1', 's1', 'sc1', 'scan1']
    ph_0 = [ph for ph in phlist_0 if ph in expression][0]
    expression = expression.replace(ph_0, 'sc_0')
    try:
        ph_1 = [ph for ph in phlist_1 if ph in expression][0]
        expression = expression.replace(ph_1, 'sc_1')
    except:
        logger.post('second scan is not spacified', 'WARN')

    # calculation
    try:
        sc_new = eval(expression, mbglobals, locals())
    except:
        logger.post('an error occured', 'ERROR')

    mbsc_0['data'].data = sc_new
    mbsc_0.recordtask(taskname)
    mbglobals['__mbscans__'].append(mbsc_0)

    logger.post('calculation is successfully finished')
    logger.post('new MBScan is now stored as __mbscans__[0]')
Example #15
0
def plotprofilemap(imagename=None, figfile=None, overwrite=None, transparent=None,
                   pol=None, spectralaxis=None, restfreq=None, plotrange=None, title=None, 
                   linecolor=None, linestyle=None, linewidth=None,
                   separatepanel=None, plotmasked=None, maskedcolor=None, 
                   showaxislabel=None, showtick=None, showticklabel=None,
                   figsize=None, numpanels=None):
    casalog.origin('plotprofilemap')
    
    try:
        if len(figfile) > 0 and os.path.exists(figfile) and overwrite == False:
            raise RuntimeError('overwrite is False and output file exists: \'%s\''%(figfile))
    
        image = SpectralImage(imagename)
        npol = len(image.stokes)
        if pol < 0 or pol > npol - 1:
            raise RuntimeError('pol {pol} is out of range (Stokes axis {stokes})'.format(pol=pol,stokes=image.stokes))        

        parsed_size = parse_figsize(figsize)
        nx, ny = parse_numpanels(numpanels)
        if (not isinstance(restfreq, str)) or len(restfreq) == 0:
            restfreq = None 
        plot_profile_map(image, figfile, pol, spectralaxis, restfreq, title, linecolor, linestyle, linewidth,
                         separatepanel, plotmasked, maskedcolor,
                         showaxislabel, showtick, showticklabel, parsed_size,
                         nx, ny, transparent, plotrange)
    except Exception, e:
        casalog.post('Error: %s'%(str(e)), priority='SEVERE')
        import traceback
        casalog.post(traceback.format_exc(), priority='DEBUG')
        raise e
Example #16
0
def clearcal(
    vis=None,
    field=None,
    spw=None,
    intent=None,
    addmodel=None,
):

    casalog.origin('clearcal')

    # Do the trivial parallelization
    if ParallelTaskHelper.isParallelMS(vis):
        helper = ParallelTaskHelper('clearcal', locals())
        helper.go()
        return

    # Local versions of the tools
    tblocal = tbtool()
    cblocal = cbtool()
    mslocal = mstool()

    try:

        # we will initialize scr cols only if we don't create them
        doinit = False

        if (type(vis) == str) & os.path.exists(vis):
            tblocal.open(vis)
            doinit = tblocal.colnames().count('CORRECTED_DATA') > 0
            tblocal.close()

            # We ignore selection if creating the scratch columns
            if not doinit:
                casalog.post(
                    'Need to create scratch columns; ignoring selection.')

            cblocal.setvi(old=True, quiet=False)
            # Old VI for now
            cblocal.open(vis, addmodel=addmodel)
        else:
            raise Exception, \
                'Visibility data set not found - please verify the name'

        # If necessary (scr col not just created), initialize scr cols
        if doinit:
            cblocal.selectvis(field=field, spw=spw, intent=intent)
            cblocal.initcalset(1)
        cblocal.close()

        # Write history to the MS
        param_names = clearcal.func_code.co_varnames[:clearcal.func_code.
                                                     co_argcount]
        param_vals = [eval(p) for p in param_names]
        casalog.post('Updating the history in the output', 'DEBUG1')
        write_history(mslocal, vis, 'clearcal', param_names, param_vals,
                      casalog)

    except Exception, instance:

        print '*** Error ***', instance
Example #17
0
def mbfitmap(method='Gaussian2D', label=''):
    depth = [s[3] for s in inspect.stack()].index('<module>')
    mbglobals = sys._getframe(depth).f_globals
    taskname = sys._getframe().f_code.co_name
    logger.origin(taskname)

    mbmp = mbglobals['__mbmaps__'][label]

    mp_data = mbmp['data'].data
    mg_daz, mg_del = mbmp.getmeshgrid()

    if method == 'Gaussian2D':
        fitter = Gaussian2D()

    hd_fit, mp_fit = fitter(mg_daz, mg_del, mp_data)

    for i in range(len(hd_fit)):
        try:
            logger.post('{0:<8} {1:+.5f} / {2}'.format(*hd_fit.cards[i]))
        except:
            logger.post('{0:<8} {1} / {2}'.format(*hd_fit.cards[i]))

    hdu_fit = fits.PrimaryHDU(mp_fit, mbmp['data'].header)
    hdu_fit.header['EXTNAME'] = 'FIT'
    hdu_fit.header += hd_fit

    mbmp.append(hdu_fit)
    mbmp.recordtask(taskname)
    mbglobals['__mbmaps__'].append(mbmp)

    return dict(hdu_fit.header)
Example #18
0
def mbsavescan(fitsname, overwrite=False, label=''):
    depth = [s[3] for s in inspect.stack()].index('<module>')
    mbglobals = sys._getframe(depth).f_globals
    taskname = sys._getframe().f_code.co_name
    logger.origin(taskname)

    mbsc = mbglobals['__mbscans__'][label]
    mbsc.writeto(fitsname, clobber=overwrite)
Example #19
0
def mbclosefits():
    depth = [s[3] for s in inspect.stack()].index('<module>')
    mbglobals = sys._getframe(depth).f_globals
    taskname = sys._getframe().f_code.co_name
    logger.origin(taskname)

    if '__mbfits__' in mbglobals.keys():
        mbglobals['__mbfits__'].close()
        del mbglobals['__mbfits__']
Example #20
0
def clearcal(
    vis=None,
    field=None,
    spw=None,
    intent=None,
    addmodel=None,
    ):

    casalog.origin('clearcal')

    # Do the trivial parallelization
    if ParallelTaskHelper.isParallelMS(vis):
        helper = ParallelTaskHelper('clearcal', locals())
        helper.go()
        return

    # Local versions of the tools
    tblocal = tbtool()
    cblocal = cbtool()
    mslocal = mstool()

    try:

        # we will initialize scr cols only if we don't create them
        doinit = False

        if (type(vis) == str) & os.path.exists(vis):
            tblocal.open(vis)
            doinit = tblocal.colnames().count('CORRECTED_DATA') > 0
            tblocal.close()

            # We ignore selection if creating the scratch columns
            if not doinit:
                casalog.post('Need to create scratch columns; ignoring selection.'
                             )

            cblocal.open(vis, addmodel=addmodel)
        else:
            raise Exception, \
                'Visibility data set not found - please verify the name'

        # If necessary (scr col not just created), initialize scr cols
        if doinit:
            cblocal.selectvis(field=field, spw=spw, intent=intent)
            cblocal.initcalset(1)
        cblocal.close()

        # Write history to the MS
        param_names = clearcal.func_code.co_varnames[:clearcal.func_code.co_argcount]
        param_vals = [eval(p) for p in param_names]
        casalog.post('Updating the history in the output', 'DEBUG1')
        write_history(mslocal, vis, 'clearcal', param_names,
                      param_vals, casalog)
        
    except Exception, instance:

        print '*** Error ***', instance
Example #21
0
def sdfit(infile=None, datacolumn=None, antenna=None, field=None, spw=None,
           timerange=None, scan=None, pol=None, intent=None,
           timebin=None, timespan=None,
           polaverage=None, 
           fitfunc=None, fitmode=None, nfit=None, thresh=None, avg_limit=None,
           minwidth=None, edge=None, outfile=None, overwrite=None):
    casalog.origin('sdfit')

    try:
        if os.path.exists(outfile):
            if overwrite:
                os.system('rm -rf %s' % outfile)
            else:
                raise ValueError(outfile + ' exists.')
        if (fitmode not in  ['list', 'auto']):
            raise ValueError, "fitmode='%s' is not supported yet" % fitmode
        if (spw == ''): spw = '*'

        selection = ms.msseltoindex(vis=infile, spw=spw, field=field, 
                                    baseline=antenna, time=timerange, 
                                    scan=scan)

        sdms.open(infile)
        sdms.set_selection(spw=sdutil.get_spwids(selection), field=field, 
                           antenna=antenna, timerange=timerange, 
                           scan=scan, polarization=pol, intent=intent)

        tempfile = 'temp_sdfit_'+str(datetime.datetime.fromtimestamp(time.time())).replace('-','').replace(' ','').replace(':','')
        if os.path.exists(tempfile):
            tempfile += str(datetime.datetime.fromtimestamp(time.time())).replace('-','').replace(' ','').replace(':','')
            if os.path.exists(tempfile):
                raise Exception('temporary file ' + tempfile + ' exists...')
        tempoutfile = tempfile + '_temp_output_ms'
        if os.path.exists(tempoutfile):
            tempoutfile += str(datetime.datetime.fromtimestamp(time.time())).replace('-','').replace(' ','').replace(':','')
            if os.path.exists(tempoutfile):
                raise Exception('temporary ms file ' + tempoutfile + ' exists...')
        
        if fitmode=='auto': nfit = [-1]
        num_fit_str = str(',').join(map(str, nfit))

        sdms.fit_line(datacolumn=datacolumn, spw=spw, pol=pol, 
                      timebin=timebin, timespan=timespan,
                      polaverage=polaverage, 
                      fitfunc=fitfunc, nfit=num_fit_str,
                      linefinding=(fitmode=='auto'), threshold=thresh,
                      avg_limit=avg_limit, minwidth=minwidth, edge=edge,
                      tempfile=tempfile, tempoutfile=tempoutfile)

        if os.path.exists(tempfile):
            return get_results(tempfile, fitfunc, nfit, outfile)
        else:
            raise Exception('temporary file was unexpectedly not created.')

    except Exception, instance:
        raise Exception, instance
Example #22
0
def sdfit(infile=None, datacolumn=None, antenna=None, field=None, spw=None,
           timerange=None, scan=None, pol=None, intent=None,
           timeaverage=None, timebin=None, timespan=None,
           polaverage=None, polaveragemode=None,
           fitfunc=None, fitmode=None, nfit=None, thresh=None, avg_limit=None,
           minwidth=None, edge=None, outfile=None, overwrite=None):
    casalog.origin('sdfit')

    try:
        if os.path.exists(outfile):
            if overwrite:
                os.system('rm -rf %s' % outfile)
            else:
                raise ValueError(outfile + ' exists.')
        if (fitmode not in  ['list', 'auto']):
            raise ValueError, "fitmode='%s' is not supported yet" % fitmode
        if (spw == ''): spw = '*'

        selection = ms.msseltoindex(vis=infile, spw=spw, field=field, 
                                    baseline=antenna, time=timerange, 
                                    scan=scan)

        sdms.open(infile)
        sdms.set_selection(spw=sdutil.get_spwids(selection), field=field, 
                           antenna=antenna, timerange=timerange, 
                           scan=scan, polarization=pol, intent=intent)

        tempfile = 'temp_sdfit_'+str(datetime.datetime.fromtimestamp(time.time())).replace('-','').replace(' ','').replace(':','')
        if os.path.exists(tempfile):
            tempfile += str(datetime.datetime.fromtimestamp(time.time())).replace('-','').replace(' ','').replace(':','')
            if os.path.exists(tempfile):
                raise Exception('temporary file ' + tempfile + ' exists...')
        tempoutfile = tempfile + '_temp_output_ms'
        if os.path.exists(tempoutfile):
            tempoutfile += str(datetime.datetime.fromtimestamp(time.time())).replace('-','').replace(' ','').replace(':','')
            if os.path.exists(tempoutfile):
                raise Exception('temporary ms file ' + tempoutfile + ' exists...')
        
        if fitmode=='auto': nfit = [-1]
        num_fit_str = str(',').join(map(str, nfit))

        sdms.fit_line(datacolumn=datacolumn, spw=spw, pol=pol, 
                      timeaverage=timeaverage, timebin=timebin, timespan=timespan,
                      polaverage=polaverage, polaveragemode=polaveragemode,
                      fitfunc=fitfunc, nfit=num_fit_str,
                      linefinding=(fitmode=='auto'), threshold=thresh,
                      avg_limit=avg_limit, minwidth=minwidth, edge=edge,
                      tempfile=tempfile, tempoutfile=tempoutfile)

        if os.path.exists(tempfile):
            return get_results(tempfile, fitfunc, nfit, outfile)
        else:
            raise Exception('temporary file was unexpectedly not created.')

    except Exception, instance:
        raise Exception, instance
Example #23
0
def statwt2(vis, timebin, chanbin):
    casalog.origin('statwt2')
    try:
        myms = mstool()
        myms.open(vis, nomodify=False)
        myms.statwt2(timebin=timebin, chanbin=chanbin)
        return True
    except Exception, instance:
        casalog.post('*** Error ***' + str(instance), 'SEVERE')
        raise
Example #24
0
File: casa.py Project: UCL/purify
def purify_image(datatransform, imagename, imsize=(128, 128), overwrite=False,
        **kwargs):
    """ Creates an image using the Purify method

        Parameters:
            datatransform: DataTransform derived
                Iterator of purify data
            imagename: string
                Path to output image
            imsize: 2-tuple
                Width and height of the output image in pixels
            overwrite: bool
                Whether to overwrite existing image. Defaults to False.
            other arguments:
                See purify.SDMM
    """
    from os.path import exists, abspath
    from numpy import array
    from taskinit import gentools, casalog
    from . import SDMM

    # Input sanitizing
    if 'image_size' in kwargs:
        msg = 'Image size should be given using the imsize argument'
        raise ValueError(msg)

    imagename = abspath(imagename)
    if exists(imagename) and not overwrite:
        msg = "File %s already exist. It will not be overwritten.\n" \
                "Please delete the file or choose another filename for the" \
                " image."
        raise IOError(msg % imagename)

    casalog.post('Starting Purify task')
    scale = kwargs.pop('scale', 'default')
    sdmm = SDMM(image_size=imsize, **kwargs)


    # Contains images over all dimensions
    image = []
    for i, data in enumerate(datatransform):
        casalog.origin('Purifying plane %s' % str(i))
        channel = sdmm(data, scale=scale)
        image.append(channel)

    image = array(image, order='F')

    # Create image
    casalog.post('Creating CASA image')
    ia, = gentools(['ia'])
    ia.newimagefromarray(imagename, image.real, overwrite=overwrite)
Example #25
0
    def coreSolve(self, accumData, accumModel, accumWeight, accumFlag):
        ispw = self.ispw
        accumFlag = np.logical_not(accumFlag)
        accumWeight = 1.0 / accumWeight

        goodbl = (np.mean(np.mean(np.mean(accumFlag, axis=0), axis=1), axis=1)
                  > 0)
        casalog.origin("gaincalRIO::coreIO::coreSolve")
        if (self.debug):
            casalog.post("DEBUG: Good baselines: %d" % np.sum(goodbl))
        solver = rantsol(threshold=self.threshold,
                         minblperant=self.minblperant,
                         refant=self.refant,
                         Nant=self.Nant,
                         ant1=self.ant1[goodbl],
                         ant2=self.ant2[goodbl],
                         outlieralgorithm=self.outlieralgorithm,
                         dorobustmean=False,
                         calmode=self.calmode,
                         mingain=self.mingain,
                         minsnr=self.minsnr,
                         dosolintflag=self.dosolintflag,
                         debug=self.debug)

        if (self.debug):
            self.timesolve -= clock.time()
            casalog.post("DEBUG: start solve")

        self.getGains(solver, accumData, accumModel, accumWeight, accumFlag,
                      goodbl)
        if (self.debug):
            self.timesolve += clock.time()
            casalog.post("DEBUG: end solve")
            casalog.post("DEBUG: Time on solver- %.3f" % solver.timeOnSolver)
            casalog.post("DEBUG: Time on flagging- %.3f" %
                         solver.timeOnFlaging)

        self.nflaginit += solver.nflagsinit
        self.nflagnbl += solver.nflagsnbl
        self.gainsOld = np.copy(self.gains)
        self.snr = np.zeros(self.gains.shape)
        self.snr[self.antFlags] = np.abs(self.gains[self.antFlags]) / np.abs(
            self.gains_er[self.antFlags])
        totalSol = self.nCorr * self.gains.shape[1] * self.gains.shape[2]
        self.nflagfinal += np.sum((self.antFlags))
        self.ntotalsol += totalSol
        casalog.post("%d/%d : %d out of %d solutions flagged" %
                     (self.isol + 1, self.solintMap[ispw].shape[0],
                      totalSol - np.sum(self.antFlags), totalSol))
Example #26
0
def split(vis, outputvis, keepmms, field, spw, scan, antenna, correlation,
          timerange, intent, array, uvrange, observation, feed, datacolumn,
          keepflags, width, timebin, combine):
    """Create a visibility subset from an existing visibility set"""

    casalog.origin('split')

    # Initialize the helper class
    pdh = ParallelDataHelper("split", locals())

    # Validate input and output parameters
    try:
        pdh.setupIO()
    except Exception, instance:
        casalog.post('%s' % instance, 'ERROR')
        return False
Example #27
0
def mbplotscan(pixellist=[1, 2], label=''):
    depth = [s[3] for s in inspect.stack()].index('<module>')
    mbglobals = sys._getframe(depth).f_globals
    taskname = sys._getframe().f_code.co_name
    logger.origin(taskname)

    mbsc = mbglobals['__mbscans__'][label]
    mjd = mbsc['record'].data['MJD']

    ax = plt.gca()
    for pixel in pixellist:
        ts = mbsc['data'].data[:, pixel - 1]
        ax.plot(mjd, ts, label='Pixel No. {0}'.format(pixel))

    ax.set_xlim([mjd.min(), mjd.max()])
    ax.set_xlabel('MJD (day)')
    ax.legend()
Example #28
0
def tsdfit(infile=None, datacolumn=None, antenna=None, field=None, spw=None, timerange=None, scan=None, pol=None,
           fitfunc=None, fitmode=None, nfit=None, thresh=None, min_nchan=None, avg_limit=None, box_size=None,
           edge=None, outfile=None, overwrite=None):
    casalog.origin('tsdbaseline')

    try:
        if os.path.exists(outfile):
            if overwrite:
                os.system('rm -rf %s' % outfile)
            else:
                raise ValueError(outfile + ' exists.')
        if (fitmode != 'list'):
            raise ValueError, "fitmode='%s' is not supported yet" % fitmode
        if (spw == ''): spw = '*'

        selection = ms.msseltoindex(vis=infile, spw=spw, field=field, 
                                    baseline=str(antenna), time=timerange, 
                                    scan=scan)

        sdms.open(infile)
        sdms.set_selection(spw=sdutil.get_spwids(selection), field=field, 
                           antenna=str(antenna), timerange=timerange, 
                           scan=scan)

        tempfile = 'temp_tsdfit_'+str(datetime.datetime.fromtimestamp(time.time())).replace('-','').replace(' ','').replace(':','')
        if os.path.exists(tempfile):
            tempfile += str(datetime.datetime.fromtimestamp(time.time())).replace('-','').replace(' ','').replace(':','')
            if os.path.exists(tempfile):
                raise Exception('temporary file ' + tempfile + ' exists...')
        tempoutfile = tempfile + '_temp_output_ms'
        if os.path.exists(tempoutfile):
            tempoutfile += str(datetime.datetime.fromtimestamp(time.time())).replace('-','').replace(' ','').replace(':','')
            if os.path.exists(tempoutfile):
                raise Exception('temporary ms file ' + tempoutfile + ' exists...')

        sdms.fit_line(datacolumn=datacolumn, spw=spw, pol=pol, fitfunc=fitfunc,
                      nfit=str(nfit)[1:-1].replace(' ', ''),
                      tempfile=tempfile, tempoutfile=tempoutfile)

        if os.path.exists(tempfile):
            return get_results(tempfile, fitfunc, nfit, outfile)
        else:
            raise Exception('temporary file was unexpectedly not created.')

    except Exception, instance:
        raise Exception, instance
Example #29
0
def cvel2(
    vis,
    outputvis,
    keepmms,
    passall,    # hidden parameter for backwards compatibiliy
    field,
    spw,
    scan,
    antenna,
    correlation,
    timerange,
    intent,
    array,
    uvrange,
    observation,
    feed,
    datacolumn,
    mode,
    nchan,
    start,
    width,
    interpolation,
    phasecenter,
    restfreq,
    outframe,
    veltype,
    hanning,
    ):
    
    """ This task used the MSTransform framework. It needs to use the ParallelDataHelper
        class, implemented in parallel.parallel_data_helper.py. 
    """

    # Initialize the helper class  
    pdh = ParallelDataHelper("cvel2", locals()) 

    casalog.origin('cvel2')
        
    # Validate input and output parameters
    try:
        pdh.setupIO()
    except Exception, instance:
        casalog.post('%s'%instance,'ERROR')
        return False
Example #30
0
def uvsub(vis=None,reverse=False):

    """Subtract model from the corrected visibility data
    
        uvsub(vis='ngc5921.ms', reverse=false)
        
        This function subtracts model visibility data from corrected visibility
        data leaving the residuals in the corrected data column.  If the
        parameter 'reverse' is set true, the process is reversed.
        
        Keyword arguments:
        vis -- Name of input visibility file (MS)
                default: none; example: vis='ngc5921.ms'
        reverse -- Reverse the operation (add rather than subtract)
                default: false; example: reverse=true
        
        uvsub(vis='ngc5921.ms', reverse=false)
    
    """

    #Python script
    #
    try:
        ms = mstool()
        casalog.origin('uvsub')
        if ((type(vis)==str) & (os.path.exists(vis))):
            ms.open(thems=vis,nomodify=False)
        else:
            raise Exception, 'Visibility data set not found - please verify the name'
            return
        ms.uvsub(reverse)
        ms.close
        
        # Write history to MS
        try:
            param_names = uvsub.func_code.co_varnames[:uvsub.func_code.co_argcount]
            param_vals = [eval(p) for p in param_names]
            write_history(mstool(), vis, 'uvsub', param_names,
                          param_vals, casalog)
        except Exception, instance:
            casalog.post("*** Error \'%s\' updating HISTORY" % (instance),
                     'WARN')            

        return
Example #31
0
def mbinit(version='', status=''):
    depth = [s[3] for s in inspect.stack()].index('<module>')
    mbglobals = sys._getframe(depth).f_globals
    taskname = sys._getframe().f_code.co_name
    logger.origin(taskname)

    if not '__mbscans__' in mbglobals.keys():
        mbglobals['__mbscans__'] = ViparDB()

    if not '__mbmaps__' in mbglobals.keys():
        mbglobals['__mbmaps__'] = ViparDB()

    if not incasa:
        mbglobals['default'] = default
        mbglobals['go'] = go
        mbglobals['inp'] = inp

    logger.post('Vipar - version {0} ({1})'.format(version, status))
    logger.post('initial settings of Vipar finished')
Example #32
0
def mbremovenoise(method='PCA', pixels=[], fraction=0.9, label=''):
    depth = [s[3] for s in inspect.stack()].index('<module>')
    mbglobals = sys._getframe(depth).f_globals
    taskname = sys._getframe().f_code.co_name
    logger.origin(taskname)

    mbsc = mbglobals['__mbscans__'][label]
    sc_old = mbsc['data'].data

    if pixels == []:
        logger.post('pixels used for estimation: all')
        sc_target = sc_old
    else:
        logger.post('pixels used for estimation: {0}'.format(pixels))
        sc_target = sc_old[:, np.array(pixels) - 1]

    logger.post('method: {0}'.format(method))
    try:
        if method == 'PCA':
            fr_gain = np.ptp(sc_target, axis=0)  # effective gain
            pca = PCA(sc_target / fr_gain)
            sc_noise = pca.reconstruct(fraction) * fr_gain

        elif method in ['median', 'mean']:
            ts_noise = getattr(np, method)(sc_target, axis=1)
            sc_noise = np.tile(ts_noise, (sc_target.shape[1], 1)).T

        logger.post('correlated noise is successfully estimated')
    except:
        logger.post('an error occured', 'ERROR')

    if pixels == []:
        sc_new = sc_target - sc_noise
    else:
        sc_new = sc_old
        sc_new[:, np.array(pixels) - 1] = sc_target - sc_noise

    mbsc['data'].data = sc_new
    mbsc.recordtask(taskname)
    mbglobals['__mbscans__'].append(mbsc)

    logger.post('correlated noise is successfully subtracted from MBScan')
    logger.post('new MBScan is now stored as __mbscans__[0]')
Example #33
0
def msuvbin(vis=None, field=None, spw=None, taql=None, outvis=None, phasecenter=None, nx=None, ny=None, cell=None,
           ncorr=None, nchan=None, fstart=None, fstep=None, wproject=None, memfrac=None):
    
    casalog.origin('msuvbin ') 
    cmd='msuvbin '
    pnames=msuvbin.func_code.co_varnames[:msuvbin.func_code.co_argcount]
    for p in pnames:
        pval=eval(p)
        if(not ((pval == '') or (pval ==None))):
            if(str(pval).find(' ') > 0):
                pval='"'+str(pval)+'"'
            if(p=='cell'):
                cmd=cmd+' '+'cellx'+'='+str(pval)+' '+'celly'+'='+str(pval)
            else:
                cmd=cmd+' '+p+'='+str(pval)

##############3
    print 'command ', cmd

############
    os.system(cmd)
Example #34
0
def msmoments( infile, moments, antenna, field, spw, includemask, excludemask, outfile, overwrite):
    
    retms=None
    casalog.origin('msmoments')

    # CAS-5410 Use private tools inside task scripts
    ms = gentools(['ms'])[0]

    try:
        if ( (not overwrite) and (len(moments) == 1) ):
            if ( os.path.exists(outfile) ):
                raise Exception( outfile+" exists." ) 
        ms.open( infile ) 
        retms = ms.moments( moments=moments,antenna=antenna,field=field,spw=spw,includemask=includemask,excludemask=excludemask,outfile=outfile,overwrite=overwrite)
	ms.close()
        retms.close()
    except Exception, instance:
        ms.close()
        if retms: retms.close()
	print '*** Error ***',instance
	raise Exception, instance
def mbsubtractbaseline(method='median', label=''):
    depth = [s[3] for s in inspect.stack()].index('<module>')
    mbglobals = sys._getframe(depth).f_globals
    taskname = sys._getframe().f_code.co_name
    logger.origin(taskname)

    mbsc = mbglobals['__mbscans__'][label]
    sc_old = mbsc['data'].data

    logger.post('method: {0}'.format(method))
    try:
        fr_baseline = getattr(np, method)(sc_old, axis=0)
        sc_new = sc_old - fr_baseline
        logger.post('baseline is successfully estimated')
    except:
        logger.post('an error occured', 'ERROR')

    mbsc['data'].data = sc_new
    mbsc.recordtask(taskname)
    mbglobals['__mbscans__'].append(mbsc)

    logger.post('baseline is successfully subtracted from MBScan')
    logger.post('new MBScan is now stored as __mbscans__[0]')
Example #36
0
def delmod(vis=None, otf=None, field=None, scr=None):

    casalog.origin('delmod')

    # Do the trivial parallelization
    if ParallelTaskHelper.isParallelMS(vis):
        helper = ParallelTaskHelper('delmod', locals())
        helper.go()
        return

    #Python script
    try:

        # only if vis exists...
        if ((type(vis) == str) & (os.path.exists(vis))):
            # ... and we are asked to do something...
            # open without adding anything!
            _cb.open(vis, addcorr=False, addmodel=False)
            _cb.delmod(otf=otf, field=field, scr=scr)
            _cb.close()
        else:
            raise Exception, 'Visibility data set not found - please verify the name'

        # Write history to MS
        try:
            param_names = delmod.func_code.co_varnames[:delmod.func_code.
                                                       co_argcount]
            param_vals = [eval(p) for p in param_names]
            write_history(mstool(), vis, 'delmod', param_names, param_vals,
                          casalog)
        except Exception, instance:
            casalog.post("*** Error \'%s\' updating HISTORY" % (instance),
                         'WARN')

    except Exception, instance:
        print '*** Error ***', instance
Example #37
0
def plotuv(vis=None, field=None, antenna=None, spw=None, observation=None, array=None,
           maxnpts=None, colors=None, symb=None, ncycles=None, figfile=None):
    """
    Plots the uv coverage of vis in klambda.  ncycles of colors will be
    allocated to representative wavelengths.

    colors: a list of matplotlib color codes.
    symb: One of matplotlib's codes for plot symbols: .:,o^v<>s+xDd234hH|_
          default: ',':  The smallest points I could find.
    maxnpts: Save memory and/or screen space by plotting a maximum of maxnpts
             (or all of them if maxnpts < 1).  There is a very sharp
             slowdown if the plotter starts swapping.
    spw: spw selection string (ignores channel specifications for now).
    field: field selection string (for now, only 1 field will be plotted).
    antenna: antenna selection string (currently ignored).
    """
    casalog.origin('plotuv')
    try:
        uvplotinfo = UVPlotInfo(vis, spw, field, antenna, observation, array,
                                ncycles, colors, symb, figfile, maxnpts)
    except Exception, e:
        casalog.post("Error plotting the UVWs of %s:" % vis, 'SEVERE')
        casalog.post("%s" % e, 'SEVERE')
        return False
Example #38
0
def predictcomp(objname=None, standard=None, epoch=None,
                minfreq=None, maxfreq=None, nfreqs=None, prefix=None,
                antennalist=None, showplot=None, savefig=None, symb=None,
                include0amp=None, include0bl=None, blunit=None, bl0flux=None):
    """
    Writes a component list named clist to disk and returns a dict of
    {'clist': clist,
     'objname': objname,
     'angdiam': angular diameter in radians (if used in clist),
     'standard': standard,
     'epoch': epoch,
     'freqs': pl.array of frequencies, in GHz,
     'uvrange': pl.array of baseline lengths, in m,
     'amps':  pl.array of predicted visibility amplitudes, in Jy,
     'savedfig': False or, if made, the filename of a plot.}
    or False on error.

    objname: An object supported by standard.
    standard: A standard for calculating flux densities, as in setjy.
              Default: 'Butler-JPL-Horizons 2010'
    epoch: The epoch to use for the calculations.   Irrelevant for
           extrasolar standards.
    minfreq: The minimum frequency to use.
             Example: '342.0GHz'
    maxfreq: The maximum frequency to use.
             Default: minfreq
             Example: '346.0GHz'
             Example: '', anything <= 0, or None: use minfreq.
    nfreqs:  The number of frequencies to use.
             Default: 1 if minfreq == maxfreq,
                      2 otherwise.
    prefix: The component list will be saved to
              prefix + 'spw0_<objname>_<minfreq><epoch>.cl'
            Default: ''
    antennalist: An array configuration file as used by simdata.
                 If given, a plot of S vs. |u| will be made.
                 Default: '' (None, just make clist.)
    showplot: Whether or not to show the plot on screen.
              Subparameter of antennalist.
              Default: Necessarily False if antennalist is not specified.
                       True otherwise.
    savefig: Filename for saving a plot of S vs. |u|.
             Subparameter of antennalist.
             Default: False (necessarily if antennalist is not specified)
             Examples: True (save to prefix + '.png')
                       'myplot.png' (save to myplot.png) 
    symb: One of matplotlib's codes for plot symbols: .:,o^v<>s+xDd234hH|_
          default: '.'
    include0amp: Force the lower limit of the amplitude axis to 0.
                 Default: False
    include0bl: Force the lower limit of the baseline length axis to 0.
    blunit: Unit of the baseline length 
    bl0flux: show zero baseline flux
    """
    retval = False
    try:
        casalog.origin('predictcomp')
        # some parameter minimally required
        if objname=='':
          raise Exception, "Error, objname is undefined"
        if minfreq=='':
          raise Exception, "Error, minfreq is undefined" 
        minfreqq = qa.quantity(minfreq)
        minfreqHz = qa.convert(minfreqq, 'Hz')['value']
        try:
            maxfreqq = qa.quantity(maxfreq)
        except Exception, instance:
            maxfreqq = minfreqq
        frequnit = maxfreqq['unit']
        maxfreqHz = qa.convert(maxfreqq, 'Hz')['value']
        if maxfreqHz <= 0.0:
            maxfreqq = minfreqq
            maxfreqHz = minfreqHz
        if minfreqHz != maxfreqHz:
            if nfreqs < 2:
                nfreqs = 2
        else:
            nfreqs = 1
        freqs = pl.linspace(minfreqHz, maxfreqHz, nfreqs)

        myme = metool()
        mepoch = myme.epoch('UTC', epoch)
        #if not prefix:
            ## meanfreq = {'value': 0.5 * (minfreqHz + maxfreqHz),
            ##             'unit': frequnit}
            ## prefix = "%s%s_%.7g" % (objname, epoch.replace('/', '-'),
            ##                         minfreqq['value'])
            ## if minfreqHz != maxfreqHz:
            ##     prefix += "to" + maxfreq
            ## else:
            ##     prefix += minfreqq['unit']
            ## prefix += "_"
        #    prefix = ''
        
        #
        if not prefix:
          if not os.access("./",os.W_OK):
            casalog.post("No write access in the current directory, trying to write cl to /tmp...","WARN")
            prefix="/tmp/"
            if not os.access(prefix, os.W_OK):
              casalog.post("No write access to /tmp to write cl file", "SEVERE")
              return False
        else:
          prefixdir=os.path.dirname(prefix)
          if not os.path.exists(prefixdir):
            prefixdirs = prefixdir.split('/')
            if prefixdirs[0]=="":
              rootdir = "/" + prefixdirs[1]
            else:
              rootdir = "./"
            if os.access(rootdir,os.W_OK):
              os.makedirs(prefixdir) 
            else:
              casalog.post("No write access to "+rootdir+" to write cl file", "SEVERE")
              return False
       
        # Get clist
        myim = imtool()
        if hasattr(myim, 'predictcomp'):
            casalog.post('local im instance created', 'DEBUG1')
        else:
            casalog.post('Error creating a local im instance.', 'SEVERE')
            return False
        #print "FREQS=",freqs
        if standard=='Butler-JPL-Horizons 2012':
            clist = predictSolarObjectCompList(objname, mepoch, freqs.tolist(), prefix)
        else:
            clist = myim.predictcomp(objname, standard, mepoch, freqs.tolist(), prefix)
        #print "created componentlist =",clist
        if os.path.isdir(clist):
            # The spw0 is useless here, but it is added by FluxStandard for the sake of setjy.
            casalog.post('The component list was saved to ' + clist)
            
            retval = {'clist': clist,
                      'objname': objname,
                      'standard': standard,
                      'epoch': mepoch,
                      'freqs (GHz)': 1.0e-9 * freqs,
                      'antennalist': antennalist}
            mycl = cltool()
            mycl.open(clist)
            comp = mycl.getcomponent(0)
            zeroblf=comp['flux']['value']
            if standard=='Butler-JPL-Horizons 2012':
              f0=comp['spectrum']['frequency']['m0']['value']
            else:
              f0=retval['freqs (GHz)'][0]
            casalog.post("Zero baseline flux %s @ %sGHz " % (zeroblf, f0),'INFO')
            mycl.close(False)               # False prevents the stupid warning.
            for k in ('shape', 'spectrum'):
                retval[k] = comp[k]
            if antennalist:
                retval['spectrum']['bl0flux']={}
                retval['spectrum']['bl0flux']['value']=zeroblf[0]
                retval['spectrum']['bl0flux']['unit']='Jy'
                retval['savedfig'] = savefig
                if not bl0flux:
                  zeroblf=[0.0]
                retval.update(plotcomp(retval, showplot, wantdict=True, symb=symb,
                                       include0amp=include0amp, include0bl=include0bl, blunit=blunit, bl0flux=zeroblf[0]))
            else:
                retval['savedfig'] = None
        else:
            casalog.post("There was an error in making the component list.",
                         'SEVERE')
Example #39
0
def sdgaincal(infile=None, calmode=None, radius=None, smooth=None, 
              antenna=None, field=None, spw=None, scan=None, intent=None, 
              applytable=None, outfile='', overwrite=False): 
    
    casalog.origin('sdgaincal')
    
    # Calibrator tool
    (mycb,) = gentools(['cb'])

    try:
        # outfile must be specified
        if (outfile == '') or not isinstance(outfile, str):
            raise ValueError, "outfile is empty."
        
        # overwrite check
        if os.path.exists(outfile) and not overwrite:
            raise RuntimeError(outfile + ' exists.')
        
        # TODO: pre-application functionality is not supported yet
        if type(applytable) == types.ListType or \
          (isinstance(applytable, str) and len(applytable) > 0):
            msg = 'Pre-application of calibration solutions is not supported yet.'
            casalog.post(msg, priority='SEVERE')
            raise RuntimeError(msg)
        
        # open MS
        if isinstance(infile, str) and os.path.exists(infile):
            mycb.open(filename=infile, compress=False, addcorr=False, addmodel=False)
        else:
            raise RuntimeError, 'infile not found - please verify the name'
        
        # select data
        if isinstance(antenna, str) and len(antenna) > 0:
            baseline = '{ant}&&&'.format(ant=antenna)
        else:
            baseline = ''
        mycb.selectvis(spw=spw, scan=scan, field=field, intent=intent, baseline=baseline)
        
        # set solve
        if calmode == 'doublecircle':
            if radius is None:
                raise RuntimeError('radius must be specified.')
            elif not isinstance(radius, str):
                rcenter = '%sarcsec'%(radius)
            else:
                try:
                    # if radius is a string only consists of numeric value without unit, 
                    # it will succeed.
                    rcenter = '%sarcsec'%(float(radius))
                except:
                    # if the above fails, it may indicate that the string contains unit
                    rcenter = radius
            mycb.setsolve(type='SDGAIN_OTFD', table=outfile, radius=rcenter, smooth=smooth)
        else:
            raise RuntimeError('Unknown calibration mode: \'{mode}\''.format(mode=calmode))

        # solve
        mycb.solve()

        ## reporting calibration solution
        #reportsolvestats(mycb.activityrec());

    except Exception, e:
        import traceback
        casalog.post(traceback.format_exc(), priority='DEBUG')
        casalog.post(errmsg(e), priority='SEVERE')
        raise e
Example #40
0
def uvcontsub3(vis, fitspw, combine, fitorder, field, spw,
               scan, intent, correlation, observation):
    """Extract the line(s) of an MS."""
    retval = True
    casalog.origin('uvcontsub3')

    myms = mstool()
    mytb = tbtool()
    # This one is redundant - it is already checked at the XML level.
    if not ((type(vis) == str) and os.path.isdir(vis)):
        casalog.post('Visibility data set not found - please verify the name', 'SEVERE')
        return False

    outputvis = vis + '.contsub'
    if os.path.exists(outputvis):
        casalog.post("Output MS " + outputvis + " already exists - will not overwrite.", 'SEVERE')
        return False

    if combine and combine.lower() != 'spw':
        casalog.post("uvcontsub3 deliberately does not support combination by",
                     'SEVERE')
        casalog.post("anything except spw.", 'SEVERE')
        return False

    # MSStateGram is picky ('CALIBRATE_WVR.REFERENCE, OBSERVE_TARGET_ON_SOURCE'
    # doesn't work, but 'CALIBRATE_WVR.REFERENCE,OBSERVE_TARGET_ON_SOURCE'
    # does), and I don't want to mess with bison now.  A .upper() might be a
    # good idea too, but the MS def'n v.2 does not say whether OBS_MODE should
    # be case-insensitive.
    intent = intent.replace(', ', ',')

    if type(spw) == list:
        spw = ','.join([str(s) for s in spw])
    elif type(spw) == int:
        spw = str(spw)

    ## if ':' in spw:
    ##     casalog.post("uvcontsub3 does not yet support selection by channel for the output",
    ##                  'SEVERE')
    ##     casalog.post("Meanwhile, use split to select the desired channels", 'WARN')
    ##     return False

    if ';' in spw:
        casalog.post("uvcontsub3 does not yet support writing multiple channel groups per output spw",
                     'SEVERE')
        return False

    mytb.open(vis + '/SPECTRAL_WINDOW')
    allspw = '0~' + str(mytb.nrows() - 1)
    mytb.close()
    if 'spw' not in combine:
        spwmfitspw = subtract_spws(spw, fitspw)
        if spwmfitspw == 'UNKNOWN':
            spwmfitspw = subtract_spws(allspw, fitspw)
        if spwmfitspw:
            raise Exception, "combine must include 'spw' when the fit is being applied to spws outside fitspw."

    if type(correlation) == list:
        correlation = ', '.join(correlation)
    correlation = correlation.upper()

    mytb.open(vis, nomodify=True)
    if 'CORRECTED_DATA' in mytb.colnames():
        datacolumn = 'CORRECTED_DATA'
    else:
        # DON'T remind the user that split before uvcontsub wastes time -
        # scratch columns will eventually go away.
        datacolumn = 'DATA'
    mytb.close()

    myms.open(vis, nomodify=True)
    if not myms.contsub(outputms=outputvis,   fitspw=fitspw,
                        fitorder=fitorder,    combine=combine,
                        spw=spw,              unionspw=join_spws(fitspw, spw),
                        field=field,          scan=scan,
                        intent=intent,        correlation=correlation,
                        obs=str(observation), whichcol=datacolumn):
        myms.close()
        return False
    myms.close()

    # Write history to output MS, not the input ms.
    try:
        param_names = uvcontsub3.func_code.co_varnames[:uvcontsub3.func_code.co_argcount]
        param_vals = [eval(p) for p in param_names]   
        retval &= write_history(myms, outputvis, 'uvcontsub3', param_names, param_vals,
                                casalog)
    except Exception, instance:
        casalog.post("*** Error \'%s\' updating HISTORY" % (instance),
                     'WARN')
Example #41
0
def statwt(
    vis,
    dorms,
    byantenna,
    sepacs,
    fitspw,
    fitcorr,
    combine,
    timebin,
    minsamp,
    field,
    spw,
    antenna,
    timerange,
    scan,
    intent,
    array,
    correlation,
    obs,
    datacolumn,
):
    """
    Sets WEIGHT and SIGMA using the scatter of the visibilities.
    """
    casalog.origin("statwt")
    retval = True
    try:
        myms = mstool()
        mytb = tbtool()

        # parameter check for those not fully implemeted
        # (should be taken out once implemented)
        if byantenna:
            raise ValueError("byantenna=True is not supported yet")
        if fitcorr != "":
            raise ValueError("fitcorr is not supported yet")
        if timebin != "0s" and timebin != "-1s":
            raise ValueError("timebin is not supported yet")

        datacol = "DATA"
        mytb.open(vis)
        colnames = mytb.colnames()
        mytb.close()

        for datacol in ["CORRECTED_DATA", "DATA", "junk"]:
            if datacol in colnames:
                break
        if datacol == "junk":
            raise ValueError(vis + " does not have a data column")

        if datacolumn == "float_data":
            raise ValueError("float_data is not yet supported")

        if datacolumn == "corrected" and datacol == "DATA":  # no CORRECTED_DATA case (fall back to DATA)
            casalog.post("No %s column found, using %s column" % (datacolumn.upper() + "_DATA", datacol), "WARN")
            datacolumn = datacol
        else:
            if datacolumn == "corrected":
                datacolumn_name = datacolumn.upper() + "_DATA"
            else:
                datacolumn_name = datacolumn.upper()
            casalog.post("Using %s column to determine visibility scatter" % datacolumn_name)

        if ":" in spw:
            casalog.post("The channel selection part of spw will be ignored.", "WARN")

        if len(correlation) > 0:
            correlation = ""
            casalog.post("Correlation selection in statwt has been disabled as of CASA v4.5", "WARN")

        myms.open(vis, nomodify=False)
        retval = myms.statwt(
            dorms,
            byantenna,
            sepacs,
            fitspw,
            fitcorr,
            combine,
            timebin,
            minsamp,
            field,
            spw,
            antenna,
            timerange,
            scan,
            intent,
            array,
            correlation,
            obs,
            datacolumn,
        )
        myms.close()
    except Exception, e:
        casalog.post("Error setting WEIGHT and SIGMA for %s:" % vis, "SEVERE")
        casalog.post("%s" % e, "SEVERE")
        if False:  # Set True for debugging.
            for p in statwt.func_code.co_varnames[: statwt.func_code.co_argcount]:
                v = eval(p)
                print p, "=", v, ", type =", type(v)
        retval = False
def readJPLephem(fmfile):
    """
    Reads a JPL Horizons text file (see
    http://ssd.jpl.nasa.gov/horizons.cgi#top ) for a solar system object and
    returns various quantities in a dictionary.  The dict will be blank ({}) if
    there is a failure.
    """
    retdict = {}
    casalog.origin('readJPLephem')

    # Try opening fmfile now, because otherwise there's no point continuing.
    try:
        ephem = open(fmfile)
    except IOError:
        casalog.post("Could not open ephemeris file " + fmfile,
                     priority="SEVERE")
        return {}

    # Setup the regexps.

    # Headers (one time only things)
    
    # Dictionary of quantity label: regexp pattern pairs that will be searched
    # for once.  The matching quantity will go in retdict[label].  Only a
    # single quantity (group) will be retrieved per line.
    headers = {
        'NAME': {'pat': r'^Target body name:\s+\d*\s*(\w+)'},   # object name, w.o. number
        'ephtype': {'pat': r'\?s_type=1#top>\]\s*:\s+\*(\w+)'}, # e.g. OBSERVER
        'obsloc': {'pat': r'^Center-site name:\s+(\w+)'},        # e.g. GEOCENTRIC
        # Catch either an explicit mean radius or a solitary target radius.
        'meanrad': {'pat': r'(?:Mean radius \(km\)\s*=|^Target radii\s*:)\s*([0-9.]+)(?:\s*km)?\s*$',
                    'unit': 'km'},
        # Triaxial target radii
        'radii': {'pat': r'Target radii\s*:\s*([0-9.]+\s*x\s*[0-9.]+\s*x\s*[0-9.]+)\s*km.*Equator, meridian, pole',
        #'radii': {'pat': r'Target radii\s*:\s*([0-9.]+\s*x\s*[0-9.]+\s*x\s*[0-9.]+)\s*km.*Equator, meridian, pole|Target radii\s*:\s*([0-9.]+)\s*km\s*',
                  'unit': 'km'},
        'T_mean': {'pat': r'Mean Temperature \(K\)\s*=\s*([0-9.]+)',
                   'unit': 'K'},

         # Figure out the units later.
        'rot_per': {'pat': r'(?i)(?<!Inferred )\b(rot(ation(al)?|\.)?\s*per.*=\s*([-0-9.]+\s*[dhr]*|Synchronous))'},
        'orb_per': {'pat': r'Orbital period((, days)?\s*=\s*[-0-9.]+\s*[dhr](\s*\(?R\)?)?)'},

        # MeasComet does not read units for these! E-lon(deg),  Lat(deg),     Alt(km)
        'GeoLong': {'pat': r'^Center geodetic\s*: ([-+0-9.]+,\s*[-+0-9.]+,\s*[-+0-9.]+)'},
        'dMJD':    {'pat': r'^Step-size\s*:\s*(.+)'},

        #                     request method v  wday mth   mday  hh  mm  ss   yyyy
        'VS_CREATE': {'pat': r'^Ephemeris / \w+ \w+ (\w+\s+\d+\s+\d+:\d+:\d+\s+\d+)'}
        }
    for hk in headers:
        headers[hk]['pat'] = re.compile(headers[hk]['pat'])

    # Data ("the rows of the table")
    
    # need date, r (heliocentric distance), delta (geocentric distance), and phang (phase angle).
    # (Could use the "dot" time derivatives for Doppler shifting, but it's
    # likely unnecessary.)
    datapat = r'^\s*'

    stoppat = r'\$\$EOE$'  # Signifies the end of data.

    # Read fmfile into retdict.
    num_cols = 0
    in_data = False
    comp_mismatches = []
    print_datapat = False
    # define interpretation of invalid values ('n.a.')
    invalid=-999.
    for line in ephem:
        if in_data:
            if re.match(stoppat, line):
                break
            matchobj = re.search(datapat, line)
            if matchobj:
                gdict = matchobj.groupdict()
                for col in gdict:
                    if gdict[col]=='n.a.':
                        gdict[col]=invalid
                    retdict['data'][col]['data'].append(gdict[col])
                if len(gdict) < num_cols:
                    print "Partially mismatching line:"
                    print line
                    print "Found:"
                    print gdict
                    print_datapat = True
            else:
                print_datapat = True
                # Chomp trailing whitespace.
                comp_mismatches.append(re.sub(r'\s*$', '', line))
        elif re.match(r'^\s*' + cols['MJD']['header'] + r'\s+'
                      + cols['RA']['header'], line):
            # See what columns are present, and finish setting up datapat and
            # retdict.
            havecols = []
            # extract coordinate ref info

            m=re.match(r'(^\s*)(\S+)(\s+)('+cols['RA']['header']+')', line)
            coordref=m.group(4).split('(')[-1]
            cols['RA']['comment']+='('+coordref+')'
            cols['DEC']['comment']+='('+coordref+')'
            # Chomp trailing whitespace.
            myline = re.sub(r'\s*$', '', line)
            titleline = myline
            remaining_cols = cols.keys()
            found_col = True
            # This loop will terminate one way or another.
            while myline and remaining_cols and found_col:
                found_col = False
                #print "myline = '%s'" % myline
                #print "remaining_cols =", ', '.join(remaining_cols)
                for col in remaining_cols:
                    if re.match(r'^\s*' + cols[col]['header'], myline):
                        #print "Found", col
                        havecols.append(col)
                        remaining_cols.remove(col)
                        myline = re.sub(r'^\s*' + cols[col]['header'],
                                        '', myline)
                        found_col = True
                        break
            datapat += r'\s+'.join([cols[col]['pat'] for col in havecols])
            sdatapat = datapat
            casalog.post("Found columns: " + ', '.join(havecols))
            datapat = re.compile(datapat)
            retdict['data'] = {}
            for col in havecols:
                if not cols[col].get('unwanted'):
                    retdict['data'][col] = {'comment': cols[col]['comment'],
                                            'data':    []}
            num_cols = len(retdict['data'])
        elif re.match(r'^\$\$SOE\s*$', line):  # Start of ephemeris
            casalog.post("Starting to read data.", priority='INFO2')
            in_data = True
        else:
            #print "line =", line
            #print "looking for", 
            for hk in headers:
                if not retdict.has_key(hk):
                    matchobj = re.search(headers[hk]['pat'], line)
                    if matchobj:
                        if hk=='radii':
                            mobjs=matchobj.groups()
                            for gp in mobjs:
                                if gp!=None:
                                    retdict[hk] = gp
                                    break
                            break
                        else:
                            retdict[hk] = matchobj.group(1) # 0 is the whole line
                            break
    ephem.close()

    # If there were errors, provide debugging info.
    if comp_mismatches:
        print "Completely mismatching lines:"
        print "\n".join(comp_mismatches)
    if print_datapat:
        print "The apparent title line is:"
        print titleline
        print "datapat = r'%s'" % sdatapat

    # Convert numerical strings into actual numbers.
    try:
        retdict['earliest'] = datestr_to_epoch(retdict['data']['MJD']['data'][0])
        retdict['latest'] = datestr_to_epoch(retdict['data']['MJD']['data'][-1])
    except Exception, e:
        print "Error!"
        if retdict.has_key('data'):
            if retdict['data'].has_key('MJD'):
                if retdict['data']['MJD'].has_key('data'):
                    #print "retdict['data']['MJD']['data'] =", retdict['data']['MJD']['data']
                    print "retdict['data'] =", retdict['data']
                else:
                    print "retdict['data']['MJD'] has no 'data' key."
                    print "retdict['data']['MJD'].keys() =", retdict['data']['MJD'].keys()
            else:
                print "retdict['data'] has no 'MJD' key."
                print "retdict['data'].keys() =", retdict['data'].keys()
        else:
            print "retdict has no 'data' key."
        raise e
Example #43
0
def sdbaseline(infile=None, datacolumn=None, antenna=None, field=None, spw=None, timerange=None, scan=None, pol=None, intent=None, maskmode=None, thresh=None, avg_limit=None, minwidth=None, edge=None, blmode=None, dosubtract=None, blformat=None, bloutput=None, bltable=None, blfunc=None, order=None, npiece=None, applyfft=None, fftmethod=None, fftthresh=None, addwn=None, rejwn=None, clipthresh=None, clipniter=None, blparam=None, verbose=None, showprogress=None, minnrow=None, outfile=None, overwrite=None):

    casalog.origin('sdbaseline')
    try:
        if (outfile == '') or not isinstance(outfile, str):
            print("type=%s, value=%s" % (type(outfile), str(outfile)))
            raise ValueError, "outfile name is empty."
        if os.path.exists(outfile) and not overwrite:
            raise Exception(outfile + ' exists.')
        if (maskmode == 'interact'):
            raise ValueError, "maskmode='%s' is not supported yet" % maskmode
        if (blfunc == 'variable' and not os.path.exists(blparam)):
            raise ValueError, "input file '%s' does not exists" % blparam
        
        if (spw == ''): spw = '*'

        if (blmode == 'apply'):
            if not os.path.exists(bltable):
                raise ValueError, "file specified in bltable '%s' does not exist." % bltable

            sorttab_info = remove_sorted_table_keyword(infile)

            if overwrite and os.path.exists(outfile) and (infile != outfile):
                os.system('rm -rf %s' % outfile)

            selection = ms.msseltoindex(vis=infile, spw=spw, field=field, 
                                        baseline=antenna, time=timerange, 
                                        scan=scan)
            sdms.open(infile)
            sdms.set_selection(spw=sdutil.get_spwids(selection), field=field, 
                               antenna=antenna, timerange=timerange, 
                               scan=scan, polarization=pol, intent=intent)
            sdms.apply_baseline_table(bltable=bltable,
                                      datacolumn=datacolumn,
                                      spw=spw,
                                      outfile=outfile)
            sdms.close()
            
            restore_sorted_table_keyword(infile, sorttab_info)
            
        elif (blmode == 'fit'):

            if(blfunc == 'sinusoid'):
                addwn = sdutil.parse_wavenumber_param(addwn)
                rejwn = sdutil.parse_wavenumber_param(rejwn)
                check_fftthresh(fftthresh)

            blformat, bloutput = prepare_for_blformat_bloutput(infile, blformat, bloutput, overwrite)

            output_bloutput_text_header(blformat, bloutput,
                                        blfunc, maskmode,
                                        infile, outfile)
            
            if (blfunc == 'variable'):
                sorttab_info = remove_sorted_table_keyword(infile)
        
            if overwrite and os.path.exists(outfile) and (infile != outfile):
                os.system('rm -rf %s' % outfile)

            selection = ms.msseltoindex(vis=infile, spw=spw, field=field, 
                                        baseline=antenna, time=timerange, 
                                        scan=scan)
            sdms.open(infile)
            sdms.set_selection(spw=sdutil.get_spwids(selection),
                               field=field, antenna=antenna,
                               timerange=timerange, scan=scan,
                               polarization=pol, intent=intent)
            params, func = prepare_for_baselining(blfunc=blfunc,
                                                  datacolumn=datacolumn,
                                                  outfile=outfile,
                                                  bloutput=','.join(bloutput),
                                                  dosubtract=dosubtract,
                                                  spw=spw,
                                                  pol=pol,
                                                  linefinding=(maskmode=='auto'),
                                                  threshold=thresh,
                                                  avg_limit=avg_limit,
                                                  minwidth=minwidth,
                                                  edge=edge,
                                                  order=order,
                                                  npiece=npiece,
                                                  applyfft=applyfft,
                                                  fftmethod=fftmethod,
                                                  fftthresh=fftthresh,
                                                  addwn=addwn,
                                                  rejwn=rejwn,
                                                  clip_threshold_sigma=clipthresh,
                                                  num_fitting_max=clipniter+1,
                                                  blparam=blparam)
            func(**params)
            sdms.close()
            
            if (blfunc == 'variable'):
                restore_sorted_table_keyword(infile, sorttab_info)

    except Exception, instance:
        raise Exception, instance
Example #44
0
def tsdbaseline(infile=None, datacolumn=None, antenna=None, field=None, spw=None, timerange=None, scan=None, pol=None, maskmode=None, thresh=None, avg_limit=None, minwidth=None, edge=None, blmode=None, dosubtract=None, blformat=None, bloutput=None, bltable=None, blfunc=None, order=None, npiece=None, applyfft=None, fftmethod=None, fftthresh=None, addwn=None, rejwn=None, clipthresh=None, clipniter=None, blparam=None, verify=None, verbose=None, showprogress=None, minnrow=None, outfile=None, overwrite=None):

    casalog.origin('tsdbaseline')
    try:
        if (outfile == '') or not isinstance(outfile, str):
            print("type=%s, value=%s" % (type(outfile), str(outfile)))
            raise ValueError, "outfile name is empty."
        if os.path.exists(outfile) and not overwrite:
            raise Exception(outfile + ' exists.')
        if (maskmode == 'interact'):
            raise ValueError, "maskmode='%s' is not supported yet" % maskmode
        if (blfunc == 'variable' and not os.path.exists(blparam)):
            raise ValueError, "input file '%s' does not exists" % blparam
        
        if (spw == ''): spw = '*'

        if blmode == 'apply':
            if not os.path.exists(bltable):
                raise ValueError, "file specified in bltable '%s' does not exist." % bltable

            with sdutil.tbmanager(infile + '/DATA_DESCRIPTION') as tb:
                spw_ids = tb.getcol('SPECTRAL_WINDOW_ID')
            with sdutil.tbmanager(infile + '/ANTENNA') as tb:
                ant_ids = range(tb.nrows())
            with sdutil.tbmanager(infile + '/FEED') as tb:
                feed_ids = numpy.unique(tb.getcol('FEED_ID'))

            sorttab_info = remove_sorted_table_keyword(infile)

            if overwrite and os.path.exists(outfile):
                os.system('rm -rf %s' % outfile)

            selection = ms.msseltoindex(vis=infile, spw=spw, field=field, 
                                        baseline=str(antenna), time=timerange, 
                                        scan=scan)
            sdms.open(infile)
            sdms.set_selection(spw=sdutil.get_spwids(selection), field=field, 
                               antenna=str(antenna), timerange=timerange, 
                               scan=scan)
            sdms.apply_baseline_table(bltable=bltable,
                                      datacolumn=datacolumn,
                                      spw=spw,
                                      outfile=outfile)
            sdms.close()
            
            restore_sorted_table_keyword(infile, sorttab_info)
            
        elif blmode == 'fit':
            blout_exists = False
            if (isinstance(bloutput, str) and os.path.exists(bloutput)):
                blout_exists = True
            elif isinstance(bloutput, list):
                for blout in bloutput:
                    if os.path.exists(blout):
                        blout_exists = True
                        break
            if blout_exists and not overwrite:
                raise ValueError, "file(s) specified in bloutput exists."

            selection = ms.msseltoindex(vis=infile, spw=spw, field=field, 
                                        baseline=str(antenna), time=timerange, 
                                        scan=scan)

            if blfunc == 'variable':
                sorttab_info = remove_sorted_table_keyword(infile)
        
            sdms.open(infile)
            sdms.set_selection(spw=sdutil.get_spwids(selection), field=field, 
                               antenna=str(antenna), timerange=timerange, 
                               scan=scan)

            
            #if(outfile==''):
            #    outfile=infile


            

            

#            if isinstance(blformat, str):
#                if('' == blformat):
#                    #blformat=',,'
#                    bloutput=',,'
#                    print 'blformat'
#                if('text' == blformat):
#                    bloutput_tmp = infile + '_blparam.txt'
#                    bloutput = ',' + bloutput_tmp + ','
#                    if os.path.exists(bloutput):
#                        raise Exception(fname + ' exists.') 





            new_bloutput=[]


            if isinstance(blformat, str):
                blformat = [blformat]
        
            

            if isinstance(bloutput, str):
                bloutput = [bloutput] 




            if isinstance(blformat, list):
                if('' in blformat):
                    blformat = ',,'
                #elif(len(blformat) > 3 ):
                #    raise ValueError, "The maximum size of blformat must be three."
                elif(len(blformat) != len(bloutput)):
                    raise ValueError, "The size of blfomat must be the same as that of bloutput."
                elif [key for key, val in Counter(blformat).items() if val>1]:
                    raise ValueError, "Same multiple blformats are not allowed."
                else:
                    if('csv' in blformat):
                        if(''!= bloutput[blformat.index('csv')]):
                            fname=bloutput[blformat.index('csv')]
                            if not overwrite and os.path.exists(fname):
                                raise Exception(fname + ' exists.') 
                            new_bloutput.append(bloutput[blformat.index('csv')])
                        else:
                            fname= infile + '_blparam.csv'
                            if not overwrite and os.path.exists(fname):
                                raise Exception(fname + ' exists. ')######################################################
                            new_bloutput.append(infile + '_blparam.csv')
                    else:
                         new_bloutput.append('')

                    

                    

                    if('text' in blformat):
                        if(''!= bloutput[blformat.index('text')]):
                            new_bloutput.append(bloutput[blformat.index('text')])
                        
                            fname = bloutput[blformat.index('text')]

                            
                            if not overwrite and os.path.exists(fname):
                                raise Exception(fname + ' exists.')


                            f = open(fname, "w")

                            blf = blfunc.lower()
                            mm = maskmode.lower()
                            if blf == 'poly':
                                ftitles = ['Fit order']
                            elif blf == 'chebyshev':
                                ftitles = ['Fit order']
                            elif blf == 'cspline':
                                ftitles = ['nPiece']
                                #print 'task 2'
                            else: # sinusoid
                                ftitles = ['applyFFT', 'fftMethod', 'fftThresh', 'addWaveN', 'rejWaveN']
                            if mm == 'auto':
                                mtitles = ['Threshold', 'avg_limit', 'Edge']
                            elif mm == 'list':
                                mtitles = []
                            else: # interact
                                mtitles = []
                            ctitles = ['clipThresh', 'clipNIter']

                            #fkeys = getattr(self, '%s_keys'%(blfunc))
                            #mkeys = getattr(self, '%s_keys'%(maskmode))
                            
                            outfile2=''
                            if(outfile==''): 
                                outfile2 = infile
                            else:
                                outfile2 = outfile
                            

                            info = [['Source Table', infile],
                                    ['Output File', outfile2]]
                                    #['Function', blfunc],
                                    #['Fit order', order]] 
                            #for i in xrange(len(ftitles)):
                            #    info.append([ftitles[i],getattr(self,fkeys[i])])
                            #if blf != 'poly':
                            #    for i in xrange(len(ctitles)):
                            #        info.append([ctitles[i],clip_keys[i]])
                            info.append(['Mask mode', maskmode])
                            #for i in xrange(len(mtitles)):
                            #    info.append([mtitles[i],getattr(mkeys[i])])

                            separator = "#"*60 + "\n"

                            f.write(separator)
                            for i in xrange(len(info)):
                                f.write('%12s: %s\n'%tuple(info[i]))
                            f.write(separator)
                            f.close()

                        
                        
                        else:
                            #new_bloutput.append(infile + '_blparam.txt')
                            new_bloutput.append(infile + '_blparam.txt')
                            fname2  = infile + '_blparam.txt'
                            if not overwrite and os.path.exists(fname2):
                                raise Exception(fname2 + ' exists.')

                            f = open(fname2, "w")

                            blf = blfunc.lower()
                            mm = maskmode.lower()
                            if blf == 'poly':
                                ftitles = ['Fit order']
                            elif blf == 'chebyshev':
                                ftitles = ['Fit order']
                            elif blf == 'cspline':
                                ftitles = ['nPiece']
                            else: # sinusoid
                                ftitles = ['applyFFT', 'fftMethod', 'fftThresh', 'addWaveN', 'rejWaveN']
                            if mm == 'auto':
                                mtitles = ['Threshold', 'avg_limit', 'Edge']
                            elif mm == 'list':
                                mtitles = []
                            else: # interact
                                mtitles = []
                            ctitles = ['clipThresh', 'clipNIter']

                            #fkeys = getattr(self, '%s_keys'%(blfunc))
                            #mkeys = getattr(self, '%s_keys'%(maskmode))

                            outfile2=''
                            if(outfile==''):
                                outfile2 = infile

                            info = [['Source Table', infile],
                                    ['Output File', outfile]]
                                    #['Function', blfunc],
                                    #['Fit order', order]] 
                            #for i in xrange(len(ftitles)):
                            #    info.append([ftitles[i],getattr(self,fkeys[i])])
                            #if blf != 'poly':
                            #    for i in xrange(len(ctitles)):
                            #        info.append([ctitles[i],clip_keys[i]])
                            info.append(['Mask mode', maskmode])
                            #for i in xrange(len(mtitles)):
                            #    info.append([mtitles[i],getattr(mkeys[i])])

                            separator = "#"*60 + "\n"

                            f.write(separator)
                            for i in xrange(len(info)):
                                f.write('%12s: %s\n'%tuple(info[i]))
                            f.write(separator)
                            f.close()       
                    
                   
                    else:
                        new_bloutput.append('')



                    if('table' in blformat):
                        #print 'blformat->',type(blformat), blformat
                        #print "blformat.index('table')", blformat.index('table')
                        #print "bloutput[blformat.index('table')])", bloutput[blformat.index('table')]

                        if(''!= bloutput[blformat.index('table')]):
                            fname = bloutput[blformat.index('table')]
                            if not overwrite and os.path.exists(fname):
                                raise Exception(fname + ' exists.')#############################################

                            new_bloutput.append(bloutput[blformat.index('table')])
                       
                        else:
                            fname = infile + '_blparam.bltable'
                            if not overwrite and os.path.exists(fname):
                                raise Exception(fname + ' exists.')#############################################
                                print ''
                            new_bloutput.append(fname)



                    else:
                        new_bloutput.append('')

                    blformat = ",".join(blformat)
                    bloutput = ",".join(new_bloutput)

            
            #print 'task blformat',type(blformat), blformat
            #print 'task bloutput',type(bloutput), bloutput
                



            params, func = prepare_for_baselining(blfunc=blfunc,
                                                  datacolumn=datacolumn,
                                                  outfile=outfile,
                                                  bltable=bloutput, # remove this line once text/csv output becomes available (2015/7/1 WK)
                                                  blformat=blformat,
                                                  bloutput=bloutput,
                                                  dosubtract=dosubtract,
                                                  spw=spw,
                                                  pol=pol,
                                                  order=order,
                                                  npiece=npiece,
                                                  blparam=blparam,
                                                  clip_threshold_sigma=clipthresh,
                                                  num_fitting_max=clipniter+1,
                                                  linefinding=(maskmode=='auto'),
                                                  threshold=thresh,
                                                  avg_limit=avg_limit,
                                                  minwidth=minwidth,
                                                  edge=edge)
            if overwrite:
                if os.path.exists(outfile):
                    os.system('rm -rf %s' % outfile)
                for bloutfile in new_bloutput:
                    if os.path.exists(bloutfile):
                        os.system('rm -rf %s' % bloutfile)

            #print params
            if(blformat != ',,'):
                func(**params)
            
            if (blfunc == 'variable'):
                restore_sorted_table_keyword(infile, sorttab_info)

    except Exception, instance:
        raise Exception, instance
Example #45
0
def visstat(vis=None,
            axis=None,
            datacolumn=None,
            useflags=None,
            spw=None,
            field=None,
            selectdata=None,
            antenna=None,
            uvrange=None,
            timerange=None,
            correlation=None,
            scan=None,
            array=None,
            observation=None):

    casalog.origin('visstat')  

    mslocal = mstool()

    mslocal.open(vis)

    if axis in ['amp', 'amplitude', 'phase', 'imag', 'imaginary', 'real']:
        complex_type = axis
        col = datacolumn
    else:
        complex_type = ''
        col = axis

    if (not selectdata):
        antenna=''
        uvrange=''
        timerange=''
        correlation=''
        scan=''
        array=''
        observation = ''
        
    s = mslocal.statistics(column=col.upper(),
                      complex_value=complex_type,
                      useflags=useflags,
                      spw=spw,
                      field=field,
                      baseline=antenna,
                      uvrange=uvrange,
                      time=timerange,
                      correlation=correlation,
                      scan=scan,
                      array=array,
                      obs=str(observation))
    
    mslocal.close()

    for stats in s.keys():
        casalog.post(stats + " values --- ", "NORMAL")
        
        if s[stats]['npts'] > 0:
            casalog.post("         -- number of points [npts]:           " + str(int(round(s[stats]['npts']))), "NORMAL")
            casalog.post("         -- minimum value [min]:               " + str(s[stats]['min'  ]), "NORMAL")
            casalog.post("         -- maximum value [max]:               " + str(s[stats]['max'  ]), "NORMAL")
            casalog.post("         -- Sum of values [sum]:               " + str(s[stats]['sum'  ]), "NORMAL")
            casalog.post("         -- Sum of squared values [sumsq]:     " + str(s[stats]['sumsq']), "NORMAL")

        casalog.post(stats + " statistics --- ", "NORMAL")
        if s[stats]['npts'] > 0:
                casalog.post("        -- Mean of the values [mean]:                 " + str(s[stats]['mean']), "NORMAL")
                casalog.post("        -- Variance of the values [var]:              " + str(s[stats]['var']), "NORMAL")
                casalog.post("        -- Standard deviation of the values [stddev]: " + str(s[stats]['stddev']), "NORMAL")
                casalog.post("        -- Root mean square [rms]:                    " + str(s[stats]['rms']), "NORMAL")
                casalog.post("        -- Median of the pixel values [median]:       " + str(s[stats]['median']), "NORMAL")
                casalog.post("        -- Median of the deviations [medabsdevmed]:   " + str(s[stats]['medabsdevmed']), "NORMAL")
                casalog.post("        -- Quartile [quartile]:                       " + str(s[stats]['quartile']), "NORMAL")
        else:
            casalog.post(stats + " -- No valid points found", "WARN")

    return s
Example #46
0
def stack(coords, outfile, stampsize = 32, imagenames= [], method = 'mean',
        weighting = None, maxmaskradius=None, psfmode = 'point', primarybeam = None):
    """
   	 Performs stacking in the image domain.

         coords -- A coordList object of all target coordinates.
	 outfile -- Target name for stacked image.
         stampsize -- size of target image in pixels
         imagenames -- Name of images to extract flux from.
         method -- 'mean' or 'median', will determined how pixels are calculated
         weighting -- only for method 'mean', if set to None will use weights in coords.
         maxmaskradius -- allows blanking of centre pixels in weight calculation
         psfmode -- Allows application of filters to stacking, currently not supported.
         primarybeam -- only applies if weighting='pb'

         returns: Estimate of stacked flux assuming point source.
    """


    from ..interval import interval
    import os
    import shutil
    import numpy as np
    from taskinit import ia, casalog

    casalog.origin('stacker')
    casalog.post('#'*42,'INFO')
    casalog.post('#'*5 +  ' {0: <31}'.format("Begin Task: Stacker")+'#'*5, 'INFO')

    global skymap
    global data
    global oldimagenames

    if coords.coord_type == 'physical':
        coords = stacker.getPixelCoords(coords, imagenames)


# Important that len(coords) here is for the pixel coordinates, not physical!
    _allocate_buffers(coords.imagenames, stampsize, len(coords))

    ia.open(coords.imagenames[0])
    cs = ia.coordsys()
    outnchans = ia.boundingbox()['trc'][2]+1
    outnstokes = ia.boundingbox()['trc'][3]+1
    ia.done()



    for imagename in coords.imagenames:
            ia.open(imagename)
            if ia.shape()[2] != outnchans or ia.shape()[3] != outnstokes:
                    print('Channels/polarisations do not match in all images! You probably want to stacking do stacking on continuum data and not on spectral cube.')
                    return
            ia.done()

    _load_stack(coords, psfmode)

    if method == 'mean' and weighting == 'sigma2':
        coords = _calculate_sigma2_weights(coords, maxmaskradius)
    elif method == 'mean' and weighting == 'sigma':
        coords = _calculate_sigma_weights(coords, maxmaskradius)
    elif method == 'mean' and weighting == 'pb':
        coords = calculate_pb_weights(coords, primarybeam, imagenames)

    npos = len([c.weight for c in coords if c.weight > 1e-6])
    casalog.post('Number of stacking positions: {0}'.format(npos),
            priority='INFO')

    stacked_im  = _stack_stack(method, coords)


    _write_stacked_image(outfile, stacked_im,
                         coords.imagenames[0], stampsize)
    casalog.post('#'*5 +  ' {0: <31}'.format("End Task: stacker")+'#'*5)
    casalog.post('#'*42)
    return stacked_im[int(stampsize/2), int(stampsize/2),0,0]
Example #47
0
def run():
    
    # Set CASA_ENGINGE env. variable so that:
    # - taskinit does not initialize the viewer
    # - taskmanmager is not initialized
    os.environ['CASA_ENGINE']="YES"

    # Initialize MPI environment
    from mpi4casa.MPIEnvironment import MPIEnvironment

    # Initialize MPICommunicator singleton
    from mpi4casa.MPICommunicator import MPICommunicator
    communicator = MPICommunicator()

    # Wait to receive start service signal from MPI client processor
    start_service_signal_available = False
    while not start_service_signal_available:
        start_service_signal_available = communicator.control_service_request_probe()
        if start_service_signal_available:
            # Receive CASA global dictionary
            request = communicator.control_service_request_recv()
        else:
            time.sleep(MPIEnvironment.mpi_start_service_sleep_time)
      
    # Check if request is start or stop signal
    if request['signal'] == 'start':
    
        # Get CASA environment dictionary
        global casa
        casa = request['casa']
        global _casa_top_frame_
        _casa_top_frame_ = True
        
        # Re-set log file
        if request['logmode'] == 'separated' or request['logmode'] == 'redirect':
            casa['files']['logfile'] = '%s-server-%s-host-%s-pid-%s' % (casa['files']['logfile'],
                                                                       str(MPIEnvironment.mpi_processor_rank),
                                                                       str(MPIEnvironment.hostname),
                                                                       str(MPIEnvironment.mpi_pid))
    
        # Import logger, logfile is set at taskinit retrieving from the casa dict. from the stack
        from taskinit import casalog
        
        # Set log origin so that the processor origin is updated
        casalog_call_origin = "mpi4casapy"
        casalog.origin(casalog_call_origin)
        
        # If log mode is separated activate showconsole to have all logs sorted by time at the terminal
        if request['logmode'] == 'redirect':
            casalog.showconsole(True)
            
        # Install filter to remove MSSelectionNullSelection errors
        casalog.filter('NORMAL1')
        casalog.filterMsg('MSSelectionNullSelection')
        casalog.filterMsg('non-existent spw')
        
        # Post MPI welcome msg
        casalog.post(MPIEnvironment.mpi_info_msg,"INFO",casalog_call_origin)
    
        # Initialize MPICommandServer and start serving
        from mpi4casa.MPICommandServer import MPICommandServer
        server = MPICommandServer()
                        
        server.serve()
        
    else:
        
        MPIEnvironment.finalize_mpi_environment()
    
        
Example #48
0
def stack(coords, vis, outvis='', imagename='', cell='1arcsec', stampsize=32,
          primarybeam='guess', datacolumn='corrected', use_cuda = False):
    """
         Performs stacking in the uv domain.


         coords      -- A coordList object of all target coordinates.
         vis         -- Input uv data file.
         outvis      -- Output uv data file. Can be set to '' to not save
                        stacked visibilities.
         datacolumn  -- Either 'corrected' or 'data'. Which column stacking is
                        applied to.
         primarybeam -- How to calculated primary beam. Currently only two
                        options, 'guess' (using casa builtin model) or
                        'constant' (i.e. no correction)
         imagename   -- Optional argument to image stacked data.
         cell        -- pixel size for target image
         stampsize   -- size of target image in pixels

         returns: Estimate of stacked flux assuming point source.
    """
    import shutil
    import os
    try:
        from taskinit import casalog
    except ImportError:
        casalog = None

    if casalog is not None:
        casalog.origin('stacker')
        casalog.post('#'*42, 'INFO')
        casalog.post('#'*5 + ' {0: <31}'.format("Begin Task: Stacker")+'#'*5,
                     'INFO')
        casalog.post('Number of stacking positions: {0}'.format(len(coords)),
                     'INFO')

    if outvis != '':
        if not os.access(outvis, os.F_OK):
            shutil.copytree(vis, outvis)

    infiletype, infilename, infileoptions = stacker._checkfile(vis, datacolumn)
    if outvis != '':
        outfiletype, outfilename, outfileoptions =\
            stacker._checkfile(outvis, datacolumn)
    else:
        outfilename = ''
        outfiletype = stacker.FILE_TYPE_NONE
        outfileoptions = 0

    if casalog is not None:
        casalog.post('Input uv file: \'{0}\' of type {1}'.format(
            infilename, stacker.FILETYPENAME[infiletype]), 'INFO')
        if outvis != '':
            casalog.post('Output uv file: \'{0}\' of type {1}'.format(
                outfilename, stacker.FILETYPENAME[outfiletype]), 'INFO')
        else:
            _ = 'No output uv file given, will not write stacked visibility'
            casalog.post(_, 'INFO')

# primary beam
    if primarybeam == 'guess':
        primarybeam = stacker.pb.guesspb(vis)
    elif primarybeam in ['constant', 'none'] or primarybeam is None:
        primarybeam = stacker.pb.PrimaryBeamModel()

    pbtype, pbfile, pbnpars, pbpars = primarybeam.cdata()

    x = [p.x for p in coords]
    y = [p.y for p in coords]
    weight = [p.weight for p in coords]

    x = (c_double*len(x))(*x)
    y = (c_double*len(y))(*y)
    weight = (c_double*len(weight))(*weight)

    import time
    start = time.time()
    flux = c_stack(infiletype, c_char_p(infilename), infileoptions,
                   outfiletype, c_char_p(outfilename), outfileoptions,
                   pbtype, c_char_p(pbfile), pbpars, pbnpars,
                   x, y, weight, c_int(len(coords)), c_bool(use_cuda))
    stop = time.time()
#     print("Started stack at {}".format(start))
#     print("Finished stack at {}".format(stop))
    print("Time used to stack: {0}".format(stop-start))

    if imagename != '':
        import clean
        import clearcal
        clearcal.clearcal(vis=outvis)
        clean.clean(vis=outvis, imagename=imagename, field='0', mode='mfs',
                    cell=cell, imsize=stampsize, weighting='natural')

    if casalog is not None:
        casalog.post('#'*5 + ' {0: <31}'.format("End Task: stacker")+'#'*5)
        casalog.post('#'*42)
    return flux
Example #49
0
def importfitsidi(fitsidifile,vis,constobsid=None,scanreindexgap_s=None):
	"""Convert FITS-IDI visibility file into a CASA visibility file (MS).

	Keyword arguments:
	fitsidifile -- Name(s) of input FITS IDI file(s)
		default: None; example='3C273XC1.IDI' or ['3C273XC1.IDI1', '3C273XC1.IDI2']
	vis -- Name of output visibility file (MS)
		default: None; example: vis='3C273XC1.ms'
		
	constobsid -- If True a constant obs id == 0  of is given to all input files 
	        default = False (new obs id for each input file)

	scanreindexgap_s --  if > 0., a new scan is started whenever the gap between two
                integrations is > the given value (seconds) or when a new field starts
                default = 0. (no reindexing)
	"""

	#Python script
        retval = True
	try:
		casalog.origin('importfitsidi')
		casalog.post("")
                myms = mstool()
		mytb = tbtool()
		if(type(fitsidifile)==str):
			casalog.post('### Reading file '+fitsidifile, 'INFO')
			myms.fromfitsidi(vis,fitsidifile)
			myms.close()
		elif(type(fitsidifile)==list):
			clist = fitsidifile
			casalog.post('### Reading file '+clist[0], 'INFO')
			myms.fromfitsidi(vis,clist[0])
			myms.close()
			clist.pop(0)
			tname = '_importfitsidi_tmp_'+vis
			shutil.rmtree(tname, ignore_errors=True)
			for fidifile in clist:
				casalog.post('### Reading file '+fidifile, 'INFO')
				myms.fromfitsidi(tname,fidifile)
				myms.close()
				myms.open(vis, nomodify=False)
				myms.concatenate(msfile=tname, freqtol='', dirtol='')
				myms.close()
				shutil.rmtree(tname, ignore_errors=True)
		else:
                        raise Exception, 'Parameter fitsidifile should be of type str or list'			

		if (constobsid):
			mytb.open(vis+'/OBSERVATION', nomodify=False)
			nobs = mytb.nrows()
			cando = True
			if nobs>1:
				casalog.post('Trying to keep obsid constant == 0 for all input files', 'INFO')
				# check if all observations are from the same telescope; if not warn and leave as is
				tels = mytb.getcol('TELESCOPE_NAME')
				for i in range(1,nobs):
					if tels[i]!=tels[0]:
						cando = False

				if cando:
					# get min and max time and write them into the first row;
					casalog.post('Adjusting OBSERVATION table', 'INFO')
					timeranges = mytb.getcol('TIME_RANGE')
					ttr = timeranges.transpose()
					newmin = min(ttr[0])
					newmax = max(ttr[1])
					mytb.putcell('TIME_RANGE', 0, [newmin,newmax])
					# delete the other rows
					mytb.removerows(range(1,nobs))
				else:
					casalog.post('The input files stem from different telescopes. Need to give different obs id.', 'WARN')
			mytb.close()
			
			if cando:
				# give the same obs id == 0 to the entire output MS
				casalog.post('Setting observation ID of all integrations to 0', 'INFO')
				mytb.open(vis, nomodify=False)
				for i in xrange(0, mytb.nrows()):
					mytb.putcell('OBSERVATION_ID', i, 0)
				mytb.close()


		else: # don't want constant obs id
			if(type(fitsidifile)==list and len(fitsidifile)>1):
				casalog.post('Incrementing observation ID for each input file ...', 'INFO')
			
		if (scanreindexgap_s > 0.):
			# reindex the scan column
			mytb.open(vis, nomodify=False)
			times = mytb.getcol('TIME')
			fields = mytb.getcol('FIELD_ID')
			arrayids = mytb.getcol('ARRAY_ID')
			scannumbers = mytb.getcol('SCAN_NUMBER')

			timesorted = np.argsort(np.array(times)) 

			scannumber = 1
			prevtime = times[timesorted[0]]
			prevfield = fields[timesorted[0]]
			prevarrayid = arrayids[timesorted[0]]
			scannumbers[timesorted[0]] = scannumber

			for i in xrange(1,mytb.nrows()):
				ii = timesorted[i]
				timenow = times[ii]
				fieldnow = fields[ii]
				arrayidnow = arrayids[ii]
				if (timenow-prevtime > scanreindexgap_s) \
					    or (fieldnow != prevfield) \
					    or (arrayidnow != prevarrayid):
					scannumber += 1
					casalog.post("Starting new scan "+str(scannumber)+" at "+str(timenow)\
							     +", field "+str(fieldnow)+", array_id "+str(arrayidnow), 'INFO')
				scannumbers[ii] = scannumber
				prevtime = timenow
				prevfield = fieldnow
				prevarrayid = arrayidnow

			mytb.putcol('SCAN_NUMBER', scannumbers)	
			mytb.close()
		
	        # write history
                try:
                        param_names = importfitsidi.func_code.co_varnames[:importfitsidi.func_code.co_argcount]
                        param_vals = [eval(p) for p in param_names]   
                        retval &= write_history(myms, vis, 'importfitsidi', param_names,
                                                param_vals, casalog)

                except Exception, instance:
                        casalog.post("*** Error \'%s\' updating HISTORY" % (instance),
                                     'WARN')

	except Exception, instance: 
		print '*** Error ***',instance
		shutil.rmtree('_importfitsidi_tmp_'+vis, ignore_errors=True)
		raise Exception, instance
Example #50
0
def oldsplit(vis, outputvis, datacolumn, field, spw, width, antenna,
          timebin, timerange, scan, intent, array, uvrange,
          correlation, observation, combine, keepflags, keepmms):
    """Create a visibility subset from an existing visibility set:

    Keyword arguments:
    vis -- Name of input visibility file (MS)
            default: none; example: vis='ngc5921.ms'
    outputvis -- Name of output visibility file (MS)
                  default: none; example: outputvis='ngc5921_src.ms'
    datacolumn -- Which data column to split out
                  default='corrected'; example: datacolumn='data'
                  Options: 'data', 'corrected', 'model', 'all',
                  'float_data', 'lag_data', 'float_data,data', and
                  'lag_data,data'.
                  note: 'all' = whichever of the above that are present.
    field -- Field name
              default: field = '' means  use all sources
              field = 1 # will get field_id=1 (if you give it an
                          integer, it will retrieve the source with that index)
              field = '1328+307' specifies source '1328+307'.
                 Minimum match can be used, egs  field = '13*' will
                 retrieve '1328+307' if it is unique or exists.
                 Source names with imbedded blanks cannot be included.
    spw -- Spectral window index identifier
            default=-1 (all); example: spw=1
    antenna -- antenna names
               default '' (all),
               antenna = '3 & 7' gives one baseline with antennaid = 3,7.
    timebin -- Interval width for time averaging.
               default: '0s' or '-1s' (no averaging)
               example: timebin='30s'
    timerange -- Time range
                 default='' means all times.  examples:
                 timerange = 'YYYY/MM/DD/hh:mm:ss~YYYY/MM/DD/hh:mm:ss'
                 timerange='< YYYY/MM/DD/HH:MM:SS.sss'
                 timerange='> YYYY/MM/DD/HH:MM:SS.sss'
                 timerange='< ddd/HH:MM:SS.sss'
                 timerange='> ddd/HH:MM:SS.sss'
    scan -- Scan numbers to select.
            default '' (all).
    intent -- Scan intents to select.
            default '' (all).
    array -- (Sub)array IDs to select.     
             default '' (all).
    uvrange -- uv distance range to select.
               default '' (all).
    correlation -- Select correlations, e.g. 'rr, ll' or ['XY', 'YX'].
                   default '' (all).
    observation -- Select by observation ID(s).
                   default '' (all).
    combine -- Data descriptors that time averaging can ignore:
                  scan, and/or state
                  Default '' (none)
    keepflags -- Keep flagged data, if possible
                 Default True

    keepmms -- If the input is a multi-MS, make the output one, too. (experimental)
               Default: False
                 
    """

    casalog.origin('oldsplit')
    mylocals = locals()
    rval = True
    try:

        if (keepmms and ParallelTaskHelper.isParallelMS(vis)): 
            if (timebin!='0s' and timebin!='-1s'): 
                casalog.post('Averaging over time with keepmms=True may lead to results different\n'
                             +'  from those obtained with keepmms=False due to different binning.', 'WARN')
                            
            myms = mstool()
            myms.open(vis)
            mses = myms.getreferencedtables()
            myms.close() 
            mses.sort()

            nfail = 0
            if os.path.exists(outputvis):
                raise ValueError, "Output MS %s already exists - will not overwrite." % outputvis
            tempout = outputvis+str(time.time())
            os.mkdir(tempout)
            successfulmses = []
            mastersubms = ''
            masterptab = ''
            emptyptab = tempout+'/EMPTY_POINTING'
            nochangeinpointing = (str(antenna)+str(timerange)=='')

            if nochangeinpointing:    
                # resulting pointing table is the same for all
                #  -> replace by empty table if it is a link and won't be modified anyway
                #     and put back original into the master after split

                # find the master
                for m in mses:
                    theptab = m+'/POINTING'
                    if not os.path.islink(theptab):
                        #print "is master ", theptab
                        mastersubms = m
                        masterptab = m+'/POINTING'
                        # save time by not copying the POINTING table len(mses) times
                        myttb = tbtool()
                        myttb.open(masterptab)
                        tmpp = myttb.copy(newtablename=emptyptab, norows=True)
                        myttb.close()
                        del myttb
                        tmpp.close()
                        del tmpp
                        break

            mytb = tbtool()

            # prepare the input MMS for processing
            replaced = []
            outputviss = []
            theptabs = []
            
            for m in mses:

                # make sure the SORTED_TABLE keywords are disabled
                mytb.open(m, nomodify=False)
                if 'SORTED_TABLE' in mytb.keywordnames():
                    tobedel = mytb.getkeyword('SORTED_TABLE').split(' ')[1]
                    mytb.removekeyword('SORTED_TABLE')
                    os.system('rm -rf '+tobedel)
                mytb.close()

                # deal with the POINTING table
                theptab = m+'/POINTING'
                theptabs.append(theptab)

                if nochangeinpointing and os.path.islink(theptab):
                    #print "is link ", theptab
                    os.remove(theptab)
                    shutil.copytree(emptyptab, theptab)
                    replaced.append(True)
                else:
                    replaced.append(False)

                # run oldsplit
                outputviss.append(os.path.abspath(tempout+'/'+os.path.basename(m)))
            # end for

            # send off the jobs
            print 'Running split_core ... '
            helper = ParallelTaskHelper('oldsplit', mylocals)
            helper.override_arg('outputvis',outputviss)
            helper._consolidateOutput = False
            goretval = helper.go()

            for i in xrange(len(mses)):
                m = mses[i]

                # deal with the POINTING table
                if replaced[i]:
                    # restore link
                    shutil.rmtree(theptabs[i], ignore_errors=True)
                    os.symlink('../'+os.path.basename(mastersubms)+'/POINTING', theptabs[i])
                    # (link in target will be created my makeMMS)

                # accumulate list of successful splits
                if not goretval[m]:
                    nfail+=1
                else:
                    successfulmses.append(outputviss[i])

            if nfail>0: # there were unsuccessful splits
                if len(successfulmses)==0:
                    casalog.post('Split failed in all subMSs.', 'WARN')
                    rval=False
                else:
                    casalog.post('*** Summary: there were failures in '+str(nfail)+' SUBMSs:', 'WARN')
                    casalog.post('*** (these are harmless if they are caused by selection):', 'WARN')
                    for m in mses:
                        if not goretval[m]:
                            casalog.post(os.path.basename(m)+': '+str(goretval[m]), 'WARN')
                        else:
                            casalog.post(os.path.basename(m)+': '+str(goretval[m]), 'NORMAL') 

                    casalog.post('Will construct MMS from subMSs with successful selection ...', 'NORMAL')

                    if nochangeinpointing: # need to take care of POINTING table
                        # in case the master subms did not make it
                        if not (tempout+'/'+os.path.basename(mastersubms) in successfulmses):
                            # old master subms was not selected.
                            # copy the original masterptab into the new master
                            shutil.rmtree(successfulmses[0]+'/POINTING')
                            shutil.copytree(masterptab, successfulmses[0]+'/POINTING')
                    
            if rval: # construct new MMS from the output
                if(width==1 and str(field)+str(spw)+str(antenna)+str(timerange)+str(scan)+str(intent)\
                   +str(array)+str(uvrange)+str(correlation)+str(observation)==''):
                    ph.makeMMS(outputvis, successfulmses)
                else:
                    myms.open(successfulmses[0], nomodify=False)
                    auxfile = "split_aux_"+str(time.time())
                    for i in xrange(1,len(successfulmses)):
                        myms.virtconcatenate(successfulmses[i], auxfile, '1Hz', '10mas', True)
                    myms.close()
                    os.remove(auxfile)
                    ph.makeMMS(outputvis, successfulmses, True, ['POINTING']) 


            shutil.rmtree(tempout, ignore_errors=True)



        else: # do not output an MMS

            rval = split_core(vis, outputvis, datacolumn, field, spw, width, antenna,
                              timebin, timerange, scan, intent, array, uvrange,
                              correlation, observation, combine, keepflags)

    except Exception, instance:
            casalog.post("*** Error: %s" % (instance), 'SEVERE')
            rval = False
Example #51
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 #52
0
def ftw(vis='', field='', spw='', model='', nterms=1, reffreq='', wprojplanes=1, complist='', incremental=False, usescratch=False):

        """Insert a source model  a visibility set:

       A source model (souce.model image) or components list is converted into a
       model visibility that is inserted into the MODEL_DATA column. 
       Wprojection is included to deal with large FOV.
       If you do not need Wprojection than simply use the task ft.
       
       TASK created by R. J. van Weeren (adapted from ft-task)


               

       Keyword arguments:
       vis -- Name of input visibility file
              default: none; example: vis='ngc5921.ms'
       field -- Field name list
               default: '' ==> all
               NOTE: BUT, only one source can be specified in a multi-source vis.
               field = '1328+307'  specifies source '1328+307'
               field = '4' specified field with index 4
       spw -- Spw selection
               default: spw = '' (all spw)
       model -- Name of input model image
               default: '' ==> None;
               example: model='/usr/lib/casapy/data/nrao/VLA/CalModels/3C286_X.im'
               Note: The model visibilities are scaled from the model frequency
                     to the observed frequency of the data.
       nterms -- Number of terms used to model the sky frequency dependence
                 default: 1  ==> one model image is required
                 example : nterms=3  represents a 2nd order Taylor-polynomial in frequency
                           and should be used in conjuction with coefficient model images as 
		           model=['xxx.model.tt0','xxx.model.tt1', 'xxx.model.tt2']
             reffreq -- Reference-frequency about which this Taylor-expansion is defined.
	                default: '' ==> reads the reference frequency from the model image
                        example : reffreq = '1.5GHz'
       wprojplanes is the number of pre-computed w-planes used for
                   the W-Projection algorithm.  wprojplanes=1 disables
       complist -- Name of component list
               default: None; ; example: complist='test.cl'
               component lists are difficult to make.
       incremental -- Add model visibility to the existing model visibilties stored in the MS
               default: False; example: incremental=True
       usescratch  -- if True model visibilities will be stored in the scratch column 
                            MODEL_DATA; when false the model visibilities will be generated 
                            on the fly (this mode may save some disk space equivalent to 
			    the volume of the observed data). 
                            default: False; example usescratch=True


     



 
        """

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

        mytmp['vis'] = vis
        mytmp['field'] = field
        mytmp['spw'] = spw
        mytmp['model'] = model
        mytmp['nterms'] = nterms
        mytmp['reffreq'] = reffreq
        mytmp['wprojplanes'] = wprojplanes
        mytmp['complist'] = complist
        mytmp['incremental'] = incremental
        mytmp['usescratch'] = usescratch
	pathname='file:///'+os.environ.get('CASAPATH').split()[0]+'/xml/'
        trec = casac.utils().torecord(pathname+'ftw.xml')

        casalog.origin('ftw')
        if trec.has_key('ftw') and casac.utils().verify(mytmp, trec['ftw']) :
	    result = task_ftw.ftw(vis, field, spw, model, nterms, reffreq, wprojplanes, complist, incremental, usescratch)

	else :
	  result = False
        return result
Example #53
0
def tsdbaseline(infile=None, datacolumn=None, antenna=None, field=None, spw=None, timerange=None, scan=None, pol=None, intent=None, maskmode=None, thresh=None, avg_limit=None, minwidth=None, edge=None, blmode=None, dosubtract=None, blformat=None, bloutput=None, bltable=None, blfunc=None, order=None, npiece=None, 
applyfft=None, fftmethod=None, fftthresh=None, 
addwn=None, rejwn=None, clipthresh=None, clipniter=None, blparam=None, verbose=None, showprogress=None, minnrow=None, outfile=None, overwrite=None):

    casalog.origin('tsdbaseline')
    try:
        if (outfile == '') or not isinstance(outfile, str):
            print("type=%s, value=%s" % (type(outfile), str(outfile)))
            raise ValueError, "outfile name is empty."
        if os.path.exists(outfile) and not overwrite:
            raise Exception(outfile + ' exists.')
        if (maskmode == 'interact'):
            raise ValueError, "maskmode='%s' is not supported yet" % maskmode
        if (blfunc == 'variable' and not os.path.exists(blparam)):
            raise ValueError, "input file '%s' does not exists" % blparam
        
        if (spw == ''): spw = '*'

        if (blmode == 'apply'):
            if not os.path.exists(bltable):
                raise ValueError, "file specified in bltable '%s' does not exist." % bltable

            sorttab_info = remove_sorted_table_keyword(infile)

            if overwrite and os.path.exists(outfile) and (infile != outfile):
                os.system('rm -rf %s' % outfile)

            selection = ms.msseltoindex(vis=infile, spw=spw, field=field, 
                                        baseline=antenna, time=timerange, 
                                        scan=scan)
            sdms.open(infile)
            sdms.set_selection(spw=sdutil.get_spwids(selection), field=field, 
                               antenna=antenna, timerange=timerange, 
                               scan=scan, polarization=pol, intent=intent)
            sdms.apply_baseline_table(bltable=bltable,
                                      datacolumn=datacolumn,
                                      spw=spw,
                                      outfile=outfile)
            sdms.close()
            
            restore_sorted_table_keyword(infile, sorttab_info)
            
        elif (blmode == 'fit'):

            #if(blfunc=='sinusoid'):
            #    addwn=sdutil._parse_wn(addwn)
            #    rejwn=sdutil._parse_wn(rejwn)
      

            sep=['-','<=','<','>=','>']
            addwn_tmp=[]

            if(blfunc=='sinusoid'):
                if(type(addwn)==str):
                    for isep in sep:
                        if(addwn.find(isep) != -1 and isep=='-'):
                            for i in range(int(addwn.split('-')[1].strip())+1):
                                addwn_tmp.append(i)
                            for i in range(int(addwn.split('-')[0].strip())):
                                addwn_tmp.remove(i)
                        
                        if(addwn.find(isep) != -1 and isep=='<'):
                            if(addwn.split('<')[0].strip()=='' and type(int(addwn.split('<')[1].strip()))==int):
                                for i in  range(int(addwn.split('<')[1].strip())):
                                    addwn_tmp.append(i)
                                break
                            elif(addwn.split('<')[1].strip()=='' and type(int(addwn.split('<')[0].strip()))==int):
                                addwn_tmp=addwn_string_greaterthan(infile, addwn, spw, inequality='<')
                                break
                            elif(addwn.split('<=')[1].strip()=='' and type(int(addwn.split('<=')[0].strip()))==int):
                                addwn_tmp=addwn_string_greaterthan(infile, addwn, spw, inequality='<=')
                                break
                        if(addwn.find(isep) != -1 and isep=='>'):
                            if(addwn.split('>=')[0].strip()=='' and type(int(addwn.split('>=')[1].strip()))==int):
                                addwn_tmp=addwn_string_greaterthan(infile, addwn, spw, inequality='>=')
                                break
                            elif(addwn.split('>')[1].strip()=='' and type(int(addwn.split('>')[0].strip()))==int):
                                for i in  range(int(addwn.split('>')[0].strip())):
                                    addwn_tmp.append(i)
                                break
                            elif(addwn.split('>')[0].strip()=='' and type(int(addwn.split('>')[1].strip()))==int):
                                addwn_tmp=addwn_string_greaterthan(infile, addwn, spw, inequality='>')
                                break
                            
                #print addwn_tmp
                    addwn=addwn_tmp

            blformat, bloutput = prepare_for_blformat_bloutput(infile, blformat, bloutput, overwrite)

            output_bloutput_text_header(blformat, bloutput,
                                        blfunc, maskmode,
                                        infile, outfile)
            
            if (blfunc == 'variable'):
                sorttab_info = remove_sorted_table_keyword(infile)
        
            if overwrite and os.path.exists(outfile) and (infile != outfile):
                os.system('rm -rf %s' % outfile)

            selection = ms.msseltoindex(vis=infile, spw=spw, field=field, 
                                        baseline=antenna, time=timerange, 
                                        scan=scan)
            sdms.open(infile)
            sdms.set_selection(spw=sdutil.get_spwids(selection),
                               field=field, antenna=antenna,
                               timerange=timerange, scan=scan,
                               polarization=pol, intent=intent)
            params, func = prepare_for_baselining(blfunc=blfunc,
                                                  datacolumn=datacolumn,
                                                  outfile=outfile,
                                                  bloutput=','.join(bloutput),
                                                  dosubtract=dosubtract,
                                                  spw=spw,
                                                  pol=pol,
                                                  order=order,
                                                  npiece=npiece,
                                                  blparam=blparam,
                                                  clip_threshold_sigma=clipthresh,
                                                  num_fitting_max=clipniter+1,
                                                  linefinding=(maskmode=='auto'),
                                                  threshold=thresh,
                                                  avg_limit=avg_limit,
                                                  minwidth=minwidth,
                                                  edge=edge,
                                                  applyfft=applyfft,###############
                                                  fftmethod=fftmethod,############
                                                  fftthresh=fftthresh,############
                                                  addwn=addwn,###################
                                                  rejwn=rejwn)####################
            func(**params)
            sdms.close()
            
            if (blfunc == 'variable'):
                restore_sorted_table_keyword(infile, sorttab_info)

    except Exception, instance:
        raise Exception, instance