Example #1
0
def spwchan_to_sets(vis, spw):
    """
    Returns the spw:chan selection string spw as a dict of sets of selected
    channels for vis, keyed by spectral window ID.

    Note that '' returns an empty set!  Use '*' to select everything!

    Example (16.ms has spws 0 and 1 with 16 chans each):
    >>> from update_spw import spwchan_to_sets
    >>> vis = casa['dirs']['data'] + '/regression/unittest/split/unordered_polspw.ms'
    >>> spwchan_to_sets(vis, '0:0')
    {0: set([0])}
    >>> selsets = spwchan_to_sets(vis, '1:1~3;5~9^2,9') # 9 is a bogus spw.
    >>> selsets
    {1: [1, 2, 3, 5, 7, 9]}
    >>> spwchan_to_sets(vis, '1:1~3;5~9^2,8')
    {1: set([1, 2, 3, 5, 7, 9]), 8: set([0])}
    >>> spwchan_to_sets(vis, '')
    {}
    """
    if not spw:  # ms.msseltoindex(vis, spw='')['channel'] returns a
        return {}  # different kind of empty array.  Skip it.

    # Currently distinguishing whether or not vis is a valid MS from whether it
    # just doesn't have all the channels in spw is a bit crude.  Sanjay is
    # working on adding some flexibility to ms.msseltoindex.
    if not os.path.isdir(vis):
        raise ValueError, str(vis) + ' is not a valid MS.'

    sets = {}
    try:
        scharr = ms.msseltoindex(vis, spw=spw)['channel']
        for scr in scharr:
            if not sets.has_key(scr[0]):
                sets[scr[0]] = set([])

            # scr[2] is the last selected channel.  Bump it up for range().
            scr[2] += 1
            sets[scr[0]].update(range(*scr[1:]))
    except:
        # spw includes channels that aren't in vis, so it needs to be trimmed
        # down to make ms.msseltoindex happy.
        allrec = ms.msseltoindex(vis, spw='*')
        #print "Trimming", spw
        spwd = spw_to_dict(spw, {}, False)
        for s in spwd:
            if s in allrec['spw']:
                endchan = allrec['channel'][s, 2]
                if not sets.has_key(s):
                    sets[s] = set([])
                if spwd[s] == '':
                    # We need to get the spw's # of channels without using
                    # ms.msseltoindex.
                    mytb = casac.table()
                    mytb.open(vis + '/SPECTRAL_WINDOW')
                    spwd[s] = range(mytb.getcell('NUM_CHAN', s))
                    mytb.close()
                sets[s].update([c for c in spwd[s] if c <= endchan])
    return sets
Example #2
0
def spwchan_to_sets(vis, spw):
    """
    Returns the spw:chan selection string spw as a dict of sets of selected
    channels for vis, keyed by spectral window ID.

    Note that '' returns an empty set!  Use '*' to select everything!

    Example (16.ms has spws 0 and 1 with 16 chans each):
    >>> from update_spw import spwchan_to_sets
    >>> vis = casa['dirs']['data'] + '/regression/unittest/split/unordered_polspw.ms'
    >>> spwchan_to_sets(vis, '0:0')
    {0: set([0])}
    >>> selsets = spwchan_to_sets(vis, '1:1~3;5~9^2,9') # 9 is a bogus spw.
    >>> selsets
    {1: [1, 2, 3, 5, 7, 9]}
    >>> spwchan_to_sets(vis, '1:1~3;5~9^2,8')
    {1: set([1, 2, 3, 5, 7, 9]), 8: set([0])}
    >>> spwchan_to_sets(vis, '')
    {}
    """
    if not spw:        # ms.msseltoindex(vis, spw='')['channel'] returns a
        return {}      # different kind of empty array.  Skip it.

    # Currently distinguishing whether or not vis is a valid MS from whether it
    # just doesn't have all the channels in spw is a bit crude.  Sanjay is
    # working on adding some flexibility to ms.msseltoindex.
    if not os.path.isdir(vis):
        raise ValueError, str(vis) + ' is not a valid MS.'
        
    sets = {}
    try:
        scharr = ms.msseltoindex(vis, spw=spw)['channel']
        for scr in scharr:
            if not sets.has_key(scr[0]):
                sets[scr[0]] = set([])

            # scr[2] is the last selected channel.  Bump it up for range().
            scr[2] += 1
            sets[scr[0]].update(range(*scr[1:]))
    except:
        # spw includes channels that aren't in vis, so it needs to be trimmed
        # down to make ms.msseltoindex happy.
        allrec = ms.msseltoindex(vis, spw='*')
        #print "Trimming", spw
        spwd = spw_to_dict(spw, {}, False)
        for s in spwd:
            if s in allrec['spw']:
                endchan = allrec['channel'][s, 2]
                if not sets.has_key(s):
                    sets[s] = set([])
                if spwd[s] == '':
                    # We need to get the spw's # of channels without using
                    # ms.msseltoindex.
                    mytb = casac.table()
                    mytb.open(vis + '/SPECTRAL_WINDOW')
                    spwd[s] = range(mytb.getcell('NUM_CHAN', s))
                    mytb.close()
                sets[s].update([c for c in spwd[s] if c <= endchan])
    return sets
Example #3
0
def spwchan_to_ranges(vis, spw):
    """
    Returns the spw:chan selection string spw as a dict of channel selection
    ranges for vis, keyed by spectral window ID.

    The ranges are stored as tuples of (start channel,
                                        end channel (inclusive!),
                                        step).

    Note that '' returns an empty set!  Use '*' to select everything!

    Example:
    >>> from update_spw import spwchan_to_ranges
    >>> selranges = spwchan_to_ranges('uid___A002_X1acc4e_X1e7.ms', '7:10~20^2;40~55')
    ValueError: spwchan_to_ranges() does not support multiple channel ranges per spw.
    >>> selranges = spwchan_to_ranges('uid___A002_X1acc4e_X1e7.ms', '0~1:1~3,5;7:10~20^2')
    >>> selranges
    {0: (1,  3,  1), 1: (1,  3,  1), 5: (10, 20,  2), 7: (10, 20,  2)}
    """
    selarr = ms.msseltoindex(vis, spw=spw)['channel']
    nspw = selarr.shape[0]
    selranges = {}
    for s in xrange(nspw):
        if selranges.has_key(selarr[s][0]):
            raise ValueError, 'spwchan_to_ranges() does not support multiple channel ranges per spw.'
        selranges[selarr[s][0]] = tuple(selarr[s][1:])
    return selranges
Example #4
0
def spwchan_to_ranges(vis, spw):
    """
    Returns the spw:chan selection string spw as a dict of channel selection
    ranges for vis, keyed by spectral window ID.

    The ranges are stored as tuples of (start channel,
                                        end channel (inclusive!),
                                        step).

    Note that '' returns an empty set!  Use '*' to select everything!

    Example:
    >>> from update_spw import spwchan_to_ranges
    >>> selranges = spwchan_to_ranges('uid___A002_X1acc4e_X1e7.ms', '7:10~20^2;40~55')
    ValueError: spwchan_to_ranges() does not support multiple channel ranges per spw.
    >>> selranges = spwchan_to_ranges('uid___A002_X1acc4e_X1e7.ms', '0~1:1~3,5;7:10~20^2')
    >>> selranges
    {0: (1,  3,  1), 1: (1,  3,  1), 5: (10, 20,  2), 7: (10, 20,  2)}
    """
    selarr = ms.msseltoindex(vis, spw=spw)['channel']
    nspw = selarr.shape[0]
    selranges = {}
    for s in xrange(nspw):
        if selranges.has_key(selarr[s][0]):
            raise ValueError, 'spwchan_to_ranges() does not support multiple channel ranges per spw.'
        selranges[selarr[s][0]] = tuple(selarr[s][1:])
    return selranges
Example #5
0
        except Exception, e:
            raise ValueError, "Exception %s parsing figfile" % e

        self.vis = vis
        self.title = vis
        self.tb = tbtool()

        # Convert '' to '*' for ms.msseltoindex.
        if not spw:
            spw = '*'
        if not field:
            field = '*'
        if not antenna:
            antenna = '*'
        self.selindices = ms.msseltoindex(vis, field=field, spw=spw,
                                          baseline=antenna,
                                          observation=str(observation))
        basequery = ""
        if observation:
            basequery += 'OBSERVATION_ID in %s' % self.selindices['obsids']
        if antenna != '*':
            if basequery:
                basequery += ' and '
            basequery += 'ANTENNA1 in [%s] and ANTENNA2 in [%s]' % (
                ','.join(map(str, self.selindices['antenna1'])),
                ','.join(map(str, self.selindices['antenna2'])))
        if array:
            if basequery:
                basequery += ' and '
            arrids = expand_tilde(array)
            basequery += 'ARRAY_ID in [%s]' % ','.join(map(str, arrids))