def get_manually(gps_start_time, gps_end_time):
    db_location = os.environ['S6_SEGMENT_SERVER']
    segment_connection = segmentdb_utils.setup_database(db_location)
    engine = query_engine.LdbdQueryEngine(segment_connection)

    # 1. Get v1 science segments

    sql = "SELECT segment.start_time, segment.end_time "
    sql += "FROM segment_definer, segment "
    sql += "WHERE segment.segment_def_id = segment_definer.segment_def_id "
    sql += "AND   segment_definer.ifos = 'H1' "
    sql += "AND   segment.segment_def_cdb = segment_definer.creator_db "
    sql += "AND   segment_definer.name = 'DMT-SCIENCE' "
    sql += "AND   segment_definer.version = 1 "
    sql += "AND NOT (%s > segment.end_time OR segment.start_time > %s)" % (
        gps_start_time, gps_end_time)

    v1_science_segments = segmentlist(
        [segment(row[0], row[1]) for row in engine.query(sql)]).coalesce()

    # 2. Get v2 science summaries

    sql = "SELECT segment_summary.start_time, segment_summary.end_time "
    sql += "FROM segment_definer, segment_summary "
    sql += "WHERE segment_summary.segment_def_id = segment_definer.segment_def_id "
    sql += "AND   segment_definer.ifos = 'H1' "
    sql += "AND   segment_summary.segment_def_cdb = segment_definer.creator_db "
    sql += "AND   segment_definer.name = 'DMT-SCIENCE' "
    sql += "AND   segment_definer.version = 2 "
    sql += "AND NOT (%s > segment_summary.end_time OR segment_summary.start_time > %s)" % (
        gps_start_time, gps_end_time)

    v2_science_summaries = segmentlist(
        [segment(row[0], row[1]) for row in engine.query(sql)]).coalesce()

    # 1. Get v2 science segments

    sql = "SELECT segment.start_time, segment.end_time "
    sql += "FROM segment_definer, segment "
    sql += "WHERE segment.segment_def_id = segment_definer.segment_def_id "
    sql += "AND   segment_definer.ifos = 'H1' "
    sql += "AND   segment.segment_def_cdb = segment_definer.creator_db "
    sql += "AND   segment_definer.name = 'DMT-SCIENCE' "
    sql += "AND   segment_definer.version = 2 "
    sql += "AND NOT (%s > segment.end_time OR segment.start_time > %s)" % (
        gps_start_time, gps_end_time)

    v2_science_segments = segmentlist(
        [segment(row[0], row[1]) for row in engine.query(sql)]).coalesce()

    result = (v1_science_segments - v2_science_summaries) + v2_science_segments

    result.coalesce()

    result &= segmentlist([segment(gps_start_time, gps_end_time)])

    return result
Exemplo n.º 2
0
    def query_segdb(cls, flags, *args, **kwargs):
        """Query the inital LIGO segment database for a list of flags.

        Parameters
        ----------
        flags : `iterable`
            A list of flag names for which to query.
        *args
            Either, two `float`-like numbers indicating the
            GPS [start, stop) interval, or a `SegmentList`
            defining a number of summary segments.
        url : `str`, optional, default: ``'https://segments.ligo.org'``
            URL of the segment database.

        Returns
        -------
        flagdict : `DataQualityDict`
            An ordered `DataQualityDict` of (name, `DataQualityFlag`)
            pairs.
        """
        # given segmentlist
        if len(args) == 1 and isinstance(args[0], SegmentList):
            qsegs = args[0]
        elif len(args) == 1 and len(args[0]) == 2:
            qsegs = SegmentList(Segment(to_gps(args[0][0]),
                                        to_gps(args[0][1])))
        elif len(args) == 2:
            qsegs = SegmentList([Segment(to_gps(args[0]), to_gps(args[1]))])
        else:
            raise ValueError("DataQualityDict.query_segdb must be called with "
                             "a list of flag names, and either GPS start and "
                             "stop times, or a SegmentList of query segments")
        url = kwargs.pop('url', 'https://segments.ligo.org')
        if kwargs.pop('on_error', None) is not None:
            warnings.warn("DataQualityDict.query_segdb doesn't accept the "
                          "on_error keyword argument")
        if kwargs.keys():
            raise TypeError("DataQualityDict.query_segdb has no keyword "
                            "argument '%s'" % list(kwargs.keys()[0]))

        # process query
        from glue.segmentdb import (segmentdb_utils as segdb_utils,
                                    query_engine as segdb_engine)
        connection = segdb_utils.setup_database(url)
        engine = segdb_engine.LdbdQueryEngine(connection)
        segdefs = []
        for flag in flags:
            dqflag = DataQualityFlag(name=flag)
            ifo = dqflag.ifo
            name = dqflag.tag
            if dqflag.version is None:
                vers = '*'
            else:
                vers = dqflag.version
            for gpsstart, gpsend in qsegs:
                if float(gpsend) == +inf:
                    gpsend = to_gps('now').seconds
                gpsstart = float(gpsstart)
                if not gpsstart.is_integer():
                    raise ValueError("Segment database queries can only"
                                     "operate on integer GPS times")
                gpsend = float(gpsend)
                if not gpsend.is_integer():
                    raise ValueError("Segment database queries can only"
                                     "operate on integer GPS times")
                segdefs += segdb_utils.expand_version_number(
                    engine, (ifo, name, vers, gpsstart, gpsend, 0, 0))
        segs = segdb_utils.query_segments(engine, 'segment', segdefs)
        segsum = segdb_utils.query_segments(engine, 'segment_summary', segdefs)
        # build output
        out = cls()
        for definition, segments, summary in zip(segdefs, segs, segsum):
            # parse flag name
            flag = ':'.join(map(str, definition[:3]))
            name = flag.rsplit(':', 1)[0]
            # if versionless
            if flag.endswith('*'):
                flag = name
                key = name
            # if asked for versionless, but returned a version
            elif flag not in flags and name in flags:
                key = name
            # other
            else:
                key = flag
            # define flag
            if key not in out:
                out[key] = DataQualityFlag(name=flag)
            # add segments
            out[key].known.extend(summary)
            out[key].active.extend(segments)
        return out
def grab_segments(start, end, flag,\
                  segment_url='https://segdb.ligo.caltech.edu',\
                  segment_summary=False):
    """
    Returns a segmentlist containing the segments during which the given flag
    was active in the given period.

    Arguments:

      start : int
        GPS start time
      end : int
        GPS end time
      flag : string
        'IFO:NAME:VERSION' format string

    Keyword arguments:

      segment_url : string
        url of segment database to query, default https://segdb.ligo.caltech.edu
      segment_summary : [ True | False ]
        also return the glue.segments.segmentlist defining the valid span of the
        returned segments
  """

    # set times
    start = int(math.floor(start))
    end = int(math.ceil(end))

    # set query engine
    connection = segmentdb_utils.setup_database(segment_url)
    engine = query_engine.LdbdQueryEngine(connection)

    # format flag name
    if isinstance(flag, basestring):
        flags = flag.split(',')
    else:
        flags = flag

    segdefs = []
    for f in flags:
        spec = f.split(':')
        if len(spec) < 2 or len(spec) > 3:
            raise AttributeError, "Included segements must be of the form "+\
                                  "ifo:name:version or ifo:name:*"

        ifo = spec[0]
        name = spec[1]

        if len(spec) is 3 and spec[2] is not '*':
            version = int(spec[2])
            if version < 1:
                raise AttributeError, "Segment version numbers must be greater than zero"
        else:
            version = '*'

        # expand segment definer
        segdefs += segmentdb_utils.expand_version_number(engine, (ifo, name, version, \
                                                                start, end, 0, 0))

    # query database and return
    segs = segmentdb_utils.query_segments(engine, 'segment', segdefs)
    segs = [s.coalesce() for s in segs]
    if segment_summary:
        segsums = segmentdb_utils.query_segments(engine, 'segment_summary',
                                                 segdefs)
        #segsums = reduce(operator.or_, segsums).coalesce()
        segsums = [s.coalesce() for s in segsums]
        segsummap = [segments.segmentlist() for f in flags]
        for segdef, segsum in zip(segdefs, segsums):
            try:
                fidx = flags.index(':'.join(map(str, segdef[:3])))
            except ValueError:
                fidx = flags.index(':'.join(segdef[:2]))
            segsummap[fidx].extend(segsum)
        if flag == flags[0]:
            return segs[0], segsummap[0]
        else:
            return segs, segsummap
    if flag == flags[0]:
        return segs[0]
    else:
        return segs
Exemplo n.º 4
0
    def query(cls, flags, *args, **kwargs):
        """Query the segment database for a set of `DataQualityFlags`.

        Parameters
        ----------
        flags : `iterable`
            A list of flag names for which to query.
        *args
            Either, two `float`-like numbers indicating the
            GPS [start, stop) interval, or a
            :class:`~gwpy.segments.segments.SegmentList`
            defining a number of summary segments.
        url : `str`, optional, default: ``'https://segdb.ligo.caltech.edu'``
            URL of the segment database.

        Returns
        -------
        flagdict : `DataQualityDict
            An ordered `DataQualityDict` of (name, `DataQualityFlag`)
            pairs.
        """
        # given segmentlist
        if len(args) == 1 and isinstance(args[0], SegmentList):
            qsegs = args[0]
        elif len(args) == 1 and len(args[0]) == 2:
            qsegs = SegmentList(Segment(args[0]))
        elif len(args) == 2:
            qsegs = SegmentList([Segment(args)])
        else:
            raise ValueError("DataQualityDict.query must be called with a "
                             "flag name, and either GPS start and stop times, "
                             "or a SegmentList of query segments")
        url = kwargs.pop('url', 'https://segdb.ligo.caltech.edu')
        if kwargs.keys():
            raise TypeError("DataQualityDict.query has no keyword argument "
                            "'%s'" % kwargs.keys()[0])
        # parse flags
        if isinstance(flags, (str, unicode)):
            flags = flags.split(',')
        else:
            flags = flags
        # process query
        from glue.segmentdb import (segmentdb_utils as segdb_utils,
                                    query_engine as segdb_engine)
        connection = segdb_utils.setup_database(url)
        engine = segdb_engine.LdbdQueryEngine(connection)
        segdefs = []
        for flag in flags:
            dqflag = DataQualityFlag(name=flag)
            ifo = dqflag.ifo
            name = dqflag.tag
            if dqflag.version is None:
                vers = '*'
            else:
                vers = dqflag.version
            for gpsstart, gpsend in qsegs:
                gpsstart = float(gpsstart)
                if not gpsstart.is_integer():
                    raise ValueError("Segment database queries can only"
                                     "operate on integer GPS times")
                gpsend = float(gpsend)
                if not gpsend.is_integer():
                    raise ValueError("Segment database queries can only"
                                     "operate on integer GPS times")
                segdefs += segdb_utils.expand_version_number(
                    engine, (ifo, name, vers, gpsstart, gpsend, 0, 0))
        segs = segdb_utils.query_segments(engine, 'segment', segdefs)
        segsum = segdb_utils.query_segments(engine, 'segment_summary', segdefs)
        segs = [s.coalesce() for s in segs]
        segsum = [s.coalesce() for s in segsum]
        # build output
        out = cls()
        for definition, segments, summary in zip(segdefs, segs, segsum):
            # parse flag name
            flag = ':'.join(map(str, definition[:3]))
            name = flag.rsplit(':', 1)[0]
            # if versionless
            if flag.endswith('*'):
                flag = name
                key = name
            # if asked for versionless, but returned a version
            elif flag not in flags and name in flags:
                key = name
            # other
            else:
                key = flag
            # define flag
            if not key in out:
                out[key] = DataQualityFlag(name=flag)
            # add segments
            out[key].valid.extend(summary)
            out[key].active.extend(segments)
        return out