def to_segwizard(segs, fobj, header=True, coltype=int): """Write the given `SegmentList` to the file object fobj Parameters ---------- segs : :class:`~gwpy.segments.segments.SegmentList` segmentlist to print fobj : `file`, `str` open file object, or file path, to write to header : `bool`, optional print header into the file, default: `True` coltype : `type`, optional numerical type in which to cast times before printing See Also -------- :mod:`glue.segmentsUtils` for definition of the segwizard format, and the to/from functions used in this GWpy module """ if isinstance(fobj, string_types): close = True fobj = open(fobj, 'w') else: close = False segmentsUtils.tosegwizard(fobj, segs, header=header, coltype=coltype) if close: fobj.close()
def check_segment_availability(grb_name, grb_time, query_start, query_end, offset, ifo, segmentName): ''' Searches +/- offset from GRB time to download the latest segment lists then extracts times and puts them into a txt file. ''' args = {'grb_name' : grb_name, 'query_start' : query_start, 'query_end' : query_end, 'ifo' : ifo, 'segmentName' : segmentName} cmd = "ligolw_segment_query --database --query-segments --include-segments '{segmentName}' --gps-start-time {query_start} --gps-end-time {query_end} > ./segments{ifo}_grb{grb_name}.xml".format(**args) print '>>',cmd print process = subprocess.Popen([cmd], shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) output,err = process.communicate() # try to open the file try: doc = utils.load_filename("segments{ifo}_grb{grb_name}.xml".format(**args), contenthandler = lsctables.use_in(ligolw.LIGOLWContentHandler)) except: raise IOError, "Error reading file: segments{ifo}_grb{grb_name}.xml".format(**args) # extract the segment list from segment:table and store in a txt file segs = table.get_table(doc, "segment") seglist = segments.segmentlist(segments.segment(s.start_time, s.end_time) for s in segs) segmentsUtils.tosegwizard(file("{ifo}-science_grb{grb_name}.txt".format(**args),'w'),seglist,header = True) print ">> %s segments +/-%ds from %ds found:"%(ifo,offset,grb_time) for s in segs: print "Start:",s.start_time,"End:",s.end_time,"Duration:",s.end_time-s.start_time print return
def to_segwizard(segs, fobj, header=True, coltype=int): """Write the given `SegmentList` to the file object fobj Parameters ---------- segs : :class:`~gwpy.segments.segments.SegmentList` segmentlist to print fobj : `file`, `str` open file object, or file path, to write to header : `bool`, optional print header into the file, default: `True` coltype : `type`, optional numerical type in which to cast times before printing See Also -------- glue.segmentsUtils for definition of the segwizard format, and the to/from functions used in this GWpy module """ from glue import segmentsUtils if isinstance(fobj, string_types): close = True fobj = open(fobj, 'w') else: close = False segmentsUtils.tosegwizard(fobj, segs, header=header, coltype=coltype) if close: fobj.close()
def test_tofromseqwizard(self): """ Check that the segwizard writing routine's output is parsed correctly. """ data = StringIO.StringIO() correct = segments.segmentlist([segments.segment(10, 100), segments.segment(110, 120), segments.segment(125, 130), segments.segment(0, 200)]) segmentsUtils.tosegwizard(data, correct) data.seek(0) self.assertEqual(correct, segmentsUtils.fromsegwizard(data, strict=True))
def test_tofromseqwizard(self): """ Check that the segwizard writing routine's output is parsed correctly. """ data = StringIO.StringIO() correct = segments.segmentlist([ segments.segment(10, 100), segments.segment(110, 120), segments.segment(125, 130), segments.segment(0, 200) ]) segmentsUtils.tosegwizard(data, correct) data.seek(0) self.assertEqual(correct, segmentsUtils.fromsegwizard(data, strict=True))
def check_segment_availability(grb_name, grb_time, query_start, query_end, offset, ifo, segmentName): ''' Searches +/- offset from GRB time to download the latest segment lists then extracts times and puts them into a txt file. ''' args = { 'grb_name': grb_name, 'query_start': query_start, 'query_end': query_end, 'ifo': ifo, 'segmentName': segmentName } cmd = "ligolw_segment_query --database --query-segments --include-segments '{segmentName}' --gps-start-time {query_start} --gps-end-time {query_end} > ./segments{ifo}_grb{grb_name}.xml".format( **args) print '>>', cmd print process = subprocess.Popen([cmd], shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) output, err = process.communicate() # try to open the file try: doc = utils.load_filename( "segments{ifo}_grb{grb_name}.xml".format(**args), contenthandler=lsctables.use_in(ligolw.LIGOLWContentHandler)) except: raise IOError, "Error reading file: segments{ifo}_grb{grb_name}.xml".format( **args) # extract the segment list from segment:table and store in a txt file segs = table.get_table(doc, "segment") seglist = segments.segmentlist( segments.segment(s.start_time, s.end_time) for s in segs) segmentsUtils.tosegwizard(file( "{ifo}-science_grb{grb_name}.txt".format(**args), 'w'), seglist, header=True) print ">> %s segments +/-%ds from %ds found:" % (ifo, offset, grb_time) for s in segs: print "Start:", s.start_time, "End:", s.end_time, "Duration:", s.end_time - s.start_time print return
def to_segwizard(segs, fobj): """Write the given `SegmentList` to the file object fobj Parameters ---------- segs : :class:`~gwpy.segments.segments.SegmentList` segmentlist to print fobj : `file`, `str` open file object, or file path, to write to See Also -------- :mod:`glue.segmentsUtils` for definition of the segwizard format, and the to/from functions used in this GWpy module """ if isinstance(fobj, basestring): close = True fobj = open(fobj, 'w') else: close = False segmentsUtils.tosegwizard(fobj, segs) if close: fobj.close()
grb_ifolist.sort() ifo_times = "".join(grb_ifolist) if offSourceSegment is None: print( "Warning: insufficient multi-IFO data to construct an off-source segment for GRB %s; skipping" % grb.event_number_grb, file=sys.stderr) continue elif opts.verbose: print("Sufficient off-source data has been found in", ifo_times, "time.") # write out the segment list to a segwizard file offsource_segfile = idirectory + "/offSourceSeg.txt" segmentsUtils.tosegwizard(open(offsource_segfile, "w"), segments.segmentlist([offSourceSegment])) onsource_segfile = idirectory + "/onSourceSeg.txt" segmentsUtils.tosegwizard(file(onsource_segfile, "w"), segments.segmentlist([onSourceSegment])) segLen = abs(onSourceSegment) bufferSegment = segments.segment( onSourceSegment[0]-opts.number_buffer_left*segLen,\ onSourceSegment[1]+opts.number_buffer_right*segLen) buffer_segfile = idirectory + "/bufferSeg.txt" segmentsUtils.tosegwizard(file(buffer_segfile, "w"), segments.segmentlist([bufferSegment])) if opts.verbose: print("on-source segment: ", onSourceSegment) print("off-source segment: ", offSourceSegment) ############################################################################
############################################################################## # Based on the start and end time, generate a list of epochs to # analyze. An entire hipe dag will be run for each of these epochs. search_epochs = segments.segmentlist() istart = opts.start_time while (istart < opts.end_time): iend = istart + opts.interval if iend > opts.end_time: iend = opts.end_time search_epochs.append(segments.segment(istart, iend)) istart += opts.interval # FIXME: the writing out of the segments should be done at the end so # that successfully generated dags, etc can be maintained from run to # run segmentsUtils.tosegwizard(file("multi_hipe_selectedsegs.txt", 'w'), search_epochs) ############################################################################## # Read in all the segment lists ifolist = [] segdict = {} if opts.h1_segments: tmplist = segmentsUtils.fromsegwizard(file(opts.h1_segments)).coalesce() segdict["H1"] = cleanlist(tmplist, minsciseg) ifolist.append("H1") if opts.h2_segments: tmplist = segmentsUtils.fromsegwizard(file(opts.h2_segments)).coalesce() segdict["H2"] = cleanlist(tmplist, minsciseg) ifolist.append("H2")
############################################################################## # Based on the start and end time, generate a list of epochs to # analyze. An entire hipe dag will be run for each of these epochs. search_epochs = segments.segmentlist() istart = opts.start_time while ( istart < opts.end_time ): iend = istart + opts.interval if iend > opts.end_time: iend = opts.end_time search_epochs.append(segments.segment(istart,iend)) istart += opts.interval # FIXME: the writing out of the segments should be done at the end so # that successfully generated dags, etc can be maintained from run to # run segmentsUtils.tosegwizard(file("multi_hipe_selectedsegs.txt",'w'),search_epochs) ############################################################################## # Read in all the segment lists ifolist = [] segdict = {} if opts.h1_segments: tmplist = segmentsUtils.fromsegwizard(file(opts.h1_segments)).coalesce() segdict["H1"] = cleanlist(tmplist, minsciseg) ifolist.append("H1") if opts.h2_segments: tmplist = segmentsUtils.fromsegwizard(file(opts.h2_segments)).coalesce() segdict["H2"] = cleanlist(tmplist, minsciseg) ifolist.append("H2")
############################################################################## # set up the segment including the off-source segment grb_ifolist.sort() ifo_times = "".join(grb_ifolist) if offSourceSegment is None: print >>sys.stderr, "Warning: insufficient multi-IFO data to construct an off-source segment for GRB %s; skipping" % grb.event_number_grb continue elif opts.verbose: print "Sufficient off-source data has been found in", ifo_times, "time." # write out the segment list to a segwizard file offsource_segfile = idirectory + "/offSourceSeg.txt" segmentsUtils.tosegwizard(open(offsource_segfile, "w"), segments.segmentlist([offSourceSegment])) onsource_segfile = idirectory + "/onSourceSeg.txt" segmentsUtils.tosegwizard(file(onsource_segfile, "w"), segments.segmentlist([onSourceSegment])) segLen = abs( onSourceSegment ) bufferSegment = segments.segment( onSourceSegment[0]-opts.number_buffer_left*segLen,\ onSourceSegment[1]+opts.number_buffer_right*segLen) buffer_segfile = idirectory + "/bufferSeg.txt" segmentsUtils.tosegwizard(file(buffer_segfile, "w"), segments.segmentlist([bufferSegment])) if opts.verbose: print "on-source segment: ", onSourceSegment print "off-source segment: ", offSourceSegment ############################################################################
""" Write a random segment list in segwizard format to stdout. """ import random import sys from glue import segments from glue import segmentsUtils def randomlist(n): def r(): return random.randint(1,5000) if n < 1: raise ValueError, "randomlist(n): n must be >= 1" x = r() l = segments.segmentlist([segments.segment(x, x + r())]) for i in range(n - 1): x = l[-1][1] + r() l.append(segments.segment(x, x + r())) return l segmentsUtils.tosegwizard(sys.stdout, randomlist(1000))