コード例 #1
0
ファイル: channels.py プロジェクト: tjma12/gwsumm
def _new(channel, find_parent=True):
    """Create a new `~gwpy.detector.Channel` in the globalv cache.
    """
    # convert to Channel
    if isinstance(channel, Channel):
        new = channel
    else:
        new = Channel(channel)
    name = str(channel)
    type_ = new.type

    # work out what kind of channel it is
    parts = re_channel.findall(name)

    # match single raw channel for LIGO
    if (len(parts) == 1 and new.ifo in ('H1', 'L1')
            and not re.search(r'\.[a-z]+\Z', name)):
        new.url = '%s/channel/byname/%s' % (CIS_URL, str(new))

    # match single trend
    elif len(parts) == 1:
        # set default trend type based on mode
        if type_ is None and ':DMT-' in name:  # DMT is always m-trend
            new.type = 'm-trend'
        # match parameters from 'raw' version of this channel

    # match composite channel
    else:
        new.subchannels = parts
        new._ifo = "".join(set(p.ifo for p in map(Channel, parts) if p.ifo))

    if find_parent:
        _update_dependent(new)

    # store new channel and return
    globalv.CHANNELS.append(new)
    try:
        return get_channel(new)
    except RuntimeError as e:
        if 'maximum recursion depth' in str(e):
            raise RuntimeError("Recursion error while accessing channel "
                               "information for %s" % str(channel))
        raise
コード例 #2
0
ファイル: channels.py プロジェクト: berkowitze/gwsumm
def get_channel(channel, find_trend_source=True, timeout=5):
    """Define a new :class:`~gwpy.detector.channel.Channel`

    Parameters
    ----------
    channel : `str`
        name of new channel
    find_trend_source : `bool`, optional, default: `True`
        query for raw version of trend channel (trends not in CIS)
    timeout : `float`, optional, default: `5`
        number of seconds to wait before connection times out

    Returns
    -------
    Channel : :class:`~gwpy.detector.channel.Channel`
        new channel.
    """
    chans = re_channel.findall(str(channel))
    nchans = len(chans)
    if nchans > 1 or (nchans == 1 and chans[0] != str(channel)):
        name = str(channel)
        try:
            type_ = Channel.MATCH.match(name).groupdict()['type']
        except AttributeError:
            type_ = None
        # handle special characters in channel name
        rename = name
        for cchar in ['+', '*', '^', '|']:
            rename = rename.replace(cchar, '\%s' % cchar)
        found = globalv.CHANNELS.sieve(name=rename, exact_match=True)
    elif ',' in str(channel):
        name, type_ = str(channel).rsplit(',', 1)
        found = globalv.CHANNELS.sieve(name=name, type=type_, exact_match=True)
    else:
        type_ = isinstance(channel, Channel) and channel.type or None
        name = str(channel)
        found = globalv.CHANNELS.sieve(name=name, type=type_, exact_match=True)
    if len(found) == 1:
        return found[0]
    elif len(found) > 1:
        cstrings = set(['%s [%s, %s]' % (c.ndsname, c.sample_rate, c.unit)
                        for c in found])
        if len(cstrings) == 1:
            return found[0]
        else:
            raise ValueError("Ambiguous channel request '%s', multiple "
                             "existing channels recovered:\n    %s"
                         % (str(channel), '\n    '.join(cstrings)))
    else:
        matches = list(Channel.MATCH.finditer(name))
        # match single raw channel
        if len(matches) == 1 and not re.search('\.[a-z]+\Z', name):
            new = Channel(str(channel))
            new.url = '%s/channel/byname/%s' % (CIS_URL, str(new))
        # match single trend
        elif len(matches) == 1:
            # set default trend type based on mode
            if type_ is None and ':DMT-' in name:  # DMT is always m-trend
                type_ = 'm-trend'
            elif type_ is None and globalv.MODE == SUMMARY_MODE_GPS:
                type_ = 's-trend'
            elif type_ is None:
                type_ = 'm-trend'
            name += ',%s' % type_
            new = Channel(name)
            if find_trend_source:
                try:
                    source = get_channel(new.name.rsplit('.')[0])
                except ValueError:
                    pass
                else:
                    new.url = source.url
                    new.unit = source.unit
                    try:
                        new.bits = source.bits
                    except AttributeError:
                        pass
                    try:
                        new.filter = source.filter
                    except AttributeError:
                        pass
                    for param in filter(lambda x: x.endswith('_range') and
                                                  not hasattr(new, x),
                                        vars(source)):
                        setattr(new, param, getattr(source, param))
            # determine sample rate for trends
            if type_ == 'm-trend':
                new.sample_rate = 1/60.
            elif type_ == 's-trend':
                new.sample_rate = 1
        # match composite channel
        else:
            parts = get_channels([m.group() for m in matches])
            new = Channel(name)
            new.subchannels = parts
            new._ifo = "".join(set(p.ifo for p in parts if p.ifo))
        globalv.CHANNELS.append(new)
        try:
            return get_channel(new)
        except RuntimeError as e:
            if 'maximum recursion depth' in str(e):
                raise RuntimeError("Recursion error while accessing channel "
                                   "information for %s" % str(channel))
            else:
                raise
コード例 #3
0
def get_channel(channel, find_trend_source=True, timeout=5):
    """Define a new :class:`~gwpy.detector.channel.Channel`

    Parameters
    ----------
    channel : `str`
        name of new channel
    find_trend_source : `bool`, optional, default: `True`
        query for raw version of trend channel (trends not in CIS)
    timeout : `float`, optional, default: `5`
        number of seconds to wait before connection times out

    Returns
    -------
    Channel : :class:`~gwpy.detector.channel.Channel`
        new channel.
    """
    chans = re_channel.findall(str(channel))
    nchans = len(chans)
    if nchans > 1 or (nchans == 1 and chans[0] != str(channel)):
        name = str(channel)
        try:
            type_ = Channel.MATCH.match(name).groupdict()['type']
        except AttributeError:
            type_ = None
        # handle special characters in channel name
        rename = name
        for cchar in ['+', '*', '^', '|']:
            rename = rename.replace(cchar, '\%s' % cchar)
        found = globalv.CHANNELS.sieve(name=rename, exact_match=True)
    elif ',' in str(channel):
        name, type_ = str(channel).rsplit(',', 1)
        found = globalv.CHANNELS.sieve(name=name, type=type_, exact_match=True)
    else:
        type_ = isinstance(channel, Channel) and channel.type or None
        name = str(channel)
        found = globalv.CHANNELS.sieve(name=name, type=type_, exact_match=True)
    if len(found) == 1:
        return found[0]
    elif len(found) > 1:
        cstrings = set([
            '%s [%s, %s]' % (c.ndsname, c.sample_rate, c.unit) for c in found
        ])
        if len(cstrings) == 1:
            return found[0]
        else:
            raise ValueError("Ambiguous channel request '%s', multiple "
                             "existing channels recovered:\n    %s" %
                             (str(channel), '\n    '.join(cstrings)))
    else:
        matches = list(Channel.MATCH.finditer(name))
        # match single raw channel
        if len(matches) == 1 and not re.search('\.[a-z]+\Z', name):
            new = Channel(str(channel))
            new.url = '%s/channel/byname/%s' % (CIS_URL, str(new))
        # match single trend
        elif len(matches) == 1:
            # set default trend type based on mode
            if type_ is None and ':DMT-' in name:  # DMT is always m-trend
                type_ = 'm-trend'
            elif type_ is None and globalv.MODE == Mode.gps:
                type_ = 's-trend'
            elif type_ is None:
                type_ = 'm-trend'
            name += ',%s' % type_
            new = Channel(name)
            if find_trend_source:
                try:
                    source = get_channel(new.name.rsplit('.')[0])
                except ValueError:
                    pass
                else:
                    new.url = source.url
                    new.unit = source.unit
                    try:
                        new.bits = source.bits
                    except AttributeError:
                        pass
                    try:
                        new.filter = source.filter
                    except AttributeError:
                        pass
                    for param in filter(
                            lambda x: x.endswith('_range') and not hasattr(
                                new, x), vars(source)):
                        setattr(new, param, getattr(source, param))
            # determine sample rate for trends
            if type_ == 'm-trend':
                new.sample_rate = 1 / 60.
            elif type_ == 's-trend':
                new.sample_rate = 1
        # match composite channel
        else:
            parts = get_channels([m.group() for m in matches])
            new = Channel(name)
            new.subchannels = parts
            new._ifo = "".join(set(p.ifo for p in parts if p.ifo))
        globalv.CHANNELS.append(new)
        try:
            return get_channel(new)
        except RuntimeError as e:
            if 'maximum recursion depth' in str(e):
                raise RuntimeError("Recursion error while accessing channel "
                                   "information for %s" % str(channel))
            else:
                raise