Changeset 1650 for branches/alma/python


Ignore:
Timestamp:
10/21/09 13:54:00 (15 years ago)
Author:
Kana Sugimoto
Message:

New Development: Yes

JIRA Issue: Yes (CAS-1441)

Ready to Release: Yes

Interface Changes: Yes

What Interface Changed: A new method 'splitant' is created.

Note the method is only available from CASA.

Test Programs: sd.splitant('INPUT.ms',outprefix='outfile')

Put in Release Notes: Yes

Module(s): None

Description:

A new method 'splitant' is added. This method is to split a measurement set
containing multiple antenna by antenna and output them as scantables.
The list of scantable names is returned.
Note this method calls CASA table tools and only available from CASA.


File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/alma/python/asapmath.py

    r1634 r1650  
    901901        scannos:       list of scan number
    902902        calmode:       calibration mode
     903
    903904        verify:        verify calibration     
    904905    """
     
    909910    scal = scantable( stm.cwcal( ssub, calmode, antname ) )
    910911    return scal
     912
     913### Start mod: 2009.10.16 kana ###
     914def splitant(filename, outprefix='',overwrite=False):
     915    """
     916    Split Measurement set by antenna name, save data as a scantables,
     917    and return a list of filename.
     918    Notice this method can only be available from CASA.
     919    Prameter
     920       filename:    the name of Measurement set to be read.
     921       outprefix:   the prefix of output scantable name.
     922                    the names of output scantable will be
     923                    outprefix.antenna1, outprefix.antenna2, ....
     924       overwrite    If the file should be overwritten if it exists.
     925                    The default False is to return with warning
     926                    without writing the output. USE WITH CARE.
     927                   
     928    """
     929    # Import the table toolkit from CASA
     930    try:
     931        import casac
     932    except ImportError:
     933        if rcParams['verbose']:
     934            #print "failed to load casa"
     935            print_log()
     936            asaplog.push("failed to load casa")
     937            print_log('ERROR')
     938        else: raise
     939        return False
     940    try:
     941        tbtool = casac.homefinder.find_home_by_name('tableHome')
     942        tb = tbtool.create()
     943        tb2 = tbtool.create()
     944    except:
     945        if rcParams['verbose']:
     946            #print "failed to load a table tool:\n", e
     947            print_log()
     948            asaplog.push("failed to load table tool")
     949            print_log('ERROR')
     950        else: raise
     951        return False
     952    # Check the input filename
     953    if isinstance(filename, str):
     954        import os.path
     955        filename = os.path.expandvars(filename)
     956        filename = os.path.expanduser(filename)
     957        if not os.path.exists(filename):
     958            s = "File '%s' not found." % (filename)
     959            if rcParams['verbose']:
     960                print_log()
     961                asaplog.push(s)
     962                print_log('ERROR')
     963                return
     964            raise IOError(s)
     965        # check if input file is MS
     966        if not os.path.isdir(filename) \
     967               or not os.path.exists(filename+'/ANTENNA') \
     968               or not os.path.exists(filename+'/table.f1'):
     969            s = "File '%s' is not a Measurement set." % (filename)
     970            if rcParams['verbose']:
     971                print_log()
     972                asaplog.push(s)
     973                print_log('ERROR')
     974                return
     975            raise IOError(s)
     976    else:
     977        s = "The filename should be string. "
     978        if rcParams['verbose']:
     979            print_log()
     980            asaplog.push(s)
     981            print_log('ERROR')
     982            return
     983        raise TypeError(s)
     984    # Check out put file name
     985    outname=''
     986    if len(outprefix) > 0: prefix=outprefix+'.'
     987    # Now do the actual splitting.
     988    outfiles=[]
     989    tmpms="temp_antsplit.ms"
     990    if os.path.exists(tmpms):
     991        ans=raw_input('Temporal file '+tmpms+' exists. Delete it and continue? [y/N]: ')
     992        if ans.upper() == 'Y':
     993            os.system('rm -rf '+tmpms)
     994            asaplog.push('The file '+tmpms+' deleted.')
     995        else:
     996            asaplog.push('Exit without splitting.')
     997            return
     998    tb.open(tablename=filename+'/ANTENNA',nomodify=True)
     999    nant=tb.nrows()
     1000    antnames=tb.getcol('NAME',0,nant,1)
     1001    antpos=tb.getcol('POSITION',0,nant,1).transpose()
     1002    tb.close()
     1003    tb.open(tablename=filename,nomodify=True)
     1004    ant1=tb.getcol('ANTENNA1',0,-1,1)
     1005    for antid in set(ant1):
     1006        qstr="ANTENNA1 == "+str(antid)
     1007        stab = tb.queryC(qstr)
     1008        ctab = stab.copy(tmpms,deep=True)
     1009        stab.close()
     1010        ctab.close()
     1011        scan=scantable(tmpms,False)
     1012        outname=prefix+antnames[antid]+'.asap'
     1013        scan.save(outname,format='ASAP',overwrite=overwrite)
     1014        # Modify scantable header
     1015        tb2.open(tablename=outname,nomodify=False)
     1016        tb2.putkeyword(keyword='AntennaName',value=antnames[antid])
     1017        tb2.putkeyword(keyword='AntennaPosition',value=antpos[antid])
     1018        tb2.flush()
     1019        tb2.close()
     1020        del scan, ctab, stab
     1021        outfiles.append(outname)
     1022    tb.close()
     1023    del tb, tb2
     1024    os.system('rm -rf '+tmpms)
     1025    return outfiles
     1026### End mod ######################
Note: See TracChangeset for help on using the changeset viewer.