def query_segments_db(db_location, gps_start, gps_end, spec): """ Query db_location to get segments between (gps_start, gps_end), with definer spec. """ engine = LdbdQueryEngine(segmentdb_utils.setup_database(db_location)) ifo, definer, version = spec.split(":") definer_args = [[ifo, definer, int(version), gps_start, gps_end, 0, 0]] result = segmentdb_utils.query_segments(engine, "segment", definer_args) return segmentlist(result[0])
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
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
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 dump_flags(ifos=None, segment_url=None, match=None, unmatch=None,\ latest=False): """ Returns the list of all flags defined in the database. Keyword rguments: ifo : [ str | list ] list of ifos to query, or str for single ifo segment_url : str url of segment database, defaults to contents of S6_SEGMENT_SERVER environment variable match : [ str | regular pattern ] regular expression to search against returned flag names, e.g, 'UPV' unmatch : [ str | regular pattern ] regular expression to negatively search against returned flag names """ if isinstance(ifos, str): ifos = [ifos] # get url if not segment_url: segment_url = os.getenv('S6_SEGMENT_SERVER') # open connection to LDBD(W)Server myClient = segmentdb_utils.setup_database(segment_url) reply = StringIO(myClient.query(squery)) xmldoc, digest = utils.load_fileobj(reply) seg_def_table = table.get_table(xmldoc, lsctables.SegmentDefTable.tableName) # sort table by ifo, name and version seg_def_table.sort(key=lambda flag: (flag.ifos[0], flag.name, \ flag.version), reverse=True) flags = lsctables.New(type(seg_def_table)) for row in seg_def_table: # test re match if match and not re.search(match, row.name): continue # test re unmatch if unmatch and re.search(unmatch, row.name): continue # only append latest versions of multiple flags flatest=True if latest: # get all flags with same ifo and name vflags = [f for f in flags if row.name==f.name and\ row.get_ifos()==f.get_ifos()] # if later version in list, move on for f in vflags: if f.version>=row.version: flatest=False break if not flatest: continue # append those flags matching ifos requirement for ifo in ifos: if ifo in row.get_ifos(): flags.append(row) break return flags
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
def query(cls, flags, *args, **kwargs): """Query the segment database URL as given for the listed `DataQualityFlag` names Parameters ---------- flag : str The name of the flag 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, defaults to segdb.ligo.caltech.edu Returns ------- flag : `DataQualityFlag` A new `DataQualityFlag`, with the `valid` and `active` lists filled appropriately. """ # parse arguments if len(args) == 1: qsegs = args[0] elif len(args) == 2: qsegs = [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(): TypeError("DataQualityDict.query has no keyword argument '%s'" % kwargs.keys()[0]) # parse flags if isinstance(flags, basestring): 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: ifo, name, version = parse_flag_name(flag) if not version: version = '*' for gpsstart, gpsend in qsegs: gpsstart = int(float(gpsstart)) gpsend = int(ceil(float(gpsend))) segdefs += segdb_utils.expand_version_number( engine, (ifo, name, version, 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])) if flag.endswith('*'): flag = flag.rsplit(':', 1)[0] # define flag if not flag in out: out[flag] = DataQualityFlag(name=flag) # add segments out[flag].valid.extend(summary) out[flag].active.extend(segments) return out
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(map(to_gps, args[0]))) elif len(args) == 2: qsegs = SegmentList([Segment(map(to_gps, args))]) 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'" % 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: if gpsend == PosInfinity or 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 dump_flags(ifos=None, segment_url=None, match=None, unmatch=None,\ latest=False): """ Returns the list of all flags defined in the database. Keyword rguments: ifo : [ str | list ] list of ifos to query, or str for single ifo segment_url : str url of segment database, defaults to contents of S6_SEGMENT_SERVER environment variable match : [ str | regular pattern ] regular expression to search against returned flag names, e.g, 'UPV' unmatch : [ str | regular pattern ] regular expression to negatively search against returned flag names """ if isinstance(ifos, str): ifos = [ifos] # get url if not segment_url: segment_url = os.getenv('S6_SEGMENT_SERVER') # open connection to LDBD(W)Server myClient = segmentdb_utils.setup_database(segment_url) reply = StringIO(myClient.query(squery)) xmldoc, digest = utils.load_fileobj(reply) seg_def_table = lsctables.SegmentDefTable.get_table(xmldoc) # sort table by ifo, name and version seg_def_table.sort(key=lambda flag: (flag.ifos[0], flag.name, \ flag.version), reverse=True) flags = lsctables.New(type(seg_def_table)) for row in seg_def_table: # test re match if match and not re.search(match, row.name): continue # test re unmatch if unmatch and re.search(unmatch, row.name): continue # only append latest versions of multiple flags flatest = True if latest: # get all flags with same ifo and name vflags = [f for f in flags if row.name==f.name and\ row.get_ifos()==f.get_ifos()] # if later version in list, move on for f in vflags: if f.version >= row.version: flatest = False break if not flatest: continue # append those flags matching ifos requirement for ifo in ifos: if ifo in row.get_ifos(): flags.append(row) break return flags
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
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