Exemplo n.º 1
0
def main():
    # parse commandline
    opts = parse_commandline()

    ## get the segments on which triggers are retrieved
    # case 1: segment file is given   
    if opts.segment_file is not None:
      # read segment file
      seg_list = KW_veto_utils.read_segfile(opts.segment_file)
    # case 2: start and end GPS time are given
    else:
      seg_list = segmentlist([segment(opts.gps_start_time,opts.gps_end_time)])
    
    try:
      dbname, GW_working_filename, GWconnection, GWcursor, GWnum =\
         get_trigs(opts.trigger_files, seg_list, opts.min_thresh, name_tag = opts.name_tag, scratch_dir=opts.scratch_dir, verbose=opts.verbose)

      # save/display the result
      outname = os.path.splitext(dbname)[0]+opts.out_format
      KW_veto_utils.save_db(GWcursor,"GWtrigs",outname,GW_working_filename,
                      order_by=opts.order_by,verbose=opts.verbose)
   
      # close the connection to the database
      GWconnection.close()
    finally:
      # erase temporal database
      if globals().has_key('GW_working_filename'):
        db = globals()["GW_working_filename"]
        if opts.verbose:
          print >> sys.stderr, "removing temporary workspace '%s'..." % db
        os.remove(db)
def main():
    ## parse the command line
    opts = parse_commandline()

    ## get the segments on which triggers are retrieved
    # case 1: segment file is given
    if opts.segment_file is not None:
        # read segment file
        if opts.segment_file.endswith(".txt"):
            seg_list = utils.read_segfile(opts.segment_file)
        elif opts.segment_file.endswith(".xml") or opts.segment_file.endswith(
                ".xml.gz"):
            seg_list = utils.read_segfile_xml(opts.segment_file, opts.verbose)
    # case 2: start and end GPS time are given
    else:
        seg_list = segmentlist(
            [segment(opts.gps_start_time, opts.gps_end_time)])

    ## loop over each channels and get KW triggers
    for chan in opts.channel_name:
        # wrap in try/finally clause so that the code can erase temporary database
        # when it encounters an error and had to stop in the middle
        try:
            ## get triggers
            dbname, KW_working_filename, KWconnection, KWcursor, full_chan_name = \
                get_trigs(chan,seg_list,opts.min_thresh,trigs_loc=opts.KW_location,name_tag=opts.name_tag,scratch_dir=opts.scratch_dir,verbose=opts.verbose)

            # save/display the result
            outname = dbname.replace(chan, full_chan_name).replace(
                ".db", opts.out_format)
            utils.save_db(KWcursor,
                          "KWtrigs",
                          outname,
                          KW_working_filename,
                          order_by=opts.order_by,
                          verbose=opts.verbose)

            # close the connection to database
            KWconnection.close()

        finally:
            # erase temporal database
            if globals().has_key('KW_working_filename'):
                db = globals()['KW_working_filename']
                if opts.verbose:
                    print >> sys.stderr, "removing temporary workspace '%s'..." % db
                os.remove(db)
Exemplo n.º 3
0
def main():
    ## parse the command line
    opts = parse_commandline()
    
    ## get the segments on which triggers are retrieved
    # case 1: segment file is given   
    if opts.segment_file is not None:
      # read segment file
      if opts.segment_file.endswith(".txt"):
        seg_list = utils.read_segfile(opts.segment_file)
      elif opts.segment_file.endswith(".xml") or opts.segment_file.endswith(".xml.gz"):
        seg_list = utils.read_segfile_xml(opts.segment_file,opts.verbose)
    # case 2: start and end GPS time are given
    else:
      seg_list = segmentlist([segment(opts.gps_start_time,opts.gps_end_time)])
   
    ## loop over each channels and get KW triggers
    for chan in opts.channel_name:      
      # wrap in try/finally clause so that the code can erase temporary database
      # when it encounters an error and had to stop in the middle
      try:
        ## get triggers
        dbname, KW_working_filename, KWconnection, KWcursor, full_chan_name = \
            get_trigs(chan,seg_list,opts.min_thresh,trigs_loc=opts.KW_location,name_tag=opts.name_tag,scratch_dir=opts.scratch_dir,verbose=opts.verbose)
    
        # save/display the result
        outname = dbname.replace(chan, full_chan_name).replace(".db", opts.out_format)
        utils.save_db(KWcursor, "KWtrigs", outname, KW_working_filename,
                      order_by=opts.order_by, verbose=opts.verbose)
     
        # close the connection to database
        KWconnection.close()
           
      finally:
        # erase temporal database
        if globals().has_key('KW_working_filename'):
          db = globals()['KW_working_filename']
          if opts.verbose:
            print >> sys.stderr, "removing temporary workspace '%s'..." % db
          os.remove(db)
Exemplo n.º 4
0
def main():
    # parse commandline
    opts = parse_commandline()

    ## get the segments on which triggers are retrieved
    # case 1: segment file is given
    if opts.segment_file is not None:
        # read segment file
        seg_list = KW_veto_utils.read_segfile(opts.segment_file)
    # case 2: start and end GPS time are given
    else:
        seg_list = segmentlist(
            [segment(opts.gps_start_time, opts.gps_end_time)])

    try:
        dbname, GW_working_filename, GWconnection, GWcursor, GWnum =\
           get_trigs(opts.trigger_files, seg_list, opts.min_thresh, name_tag = opts.name_tag, scratch_dir=opts.scratch_dir, verbose=opts.verbose)

        # save/display the result
        outname = os.path.splitext(dbname)[0] + opts.out_format
        KW_veto_utils.save_db(GWcursor,
                              "GWtrigs",
                              outname,
                              GW_working_filename,
                              order_by=opts.order_by,
                              verbose=opts.verbose)

        # close the connection to the database
        GWconnection.close()
    finally:
        # erase temporal database
        if globals().has_key('GW_working_filename'):
            db = globals()["GW_working_filename"]
            if opts.verbose:
                print >> sys.stderr, "removing temporary workspace '%s'..." % db
            os.remove(db)
Exemplo n.º 5
0
def get_trigs(trigger_files,
              segs,
              min_thresh,
              name_tag=None,
              scratch_dir=".",
              verbose=True):
    """
    prepare the SQL database and retrieve the triggers    
    """
    # initialize SQLite database
    start_time = segs[0][0]
    end_time = segs[-1][1]
    prefix = "%d_%d_%d_GWtrigs" % (start_time, end_time, min_thresh)
    if name_tag != None:
        prefix = name_tag + "_" + prefix
    dbname = prefix + ".db"
    # if the name already exists, rename the old one to avoid collisions
    KW_veto_utils.rename(dbname)

    global GW_working_filename  # so that it can be erased when error occurs
    GW_working_filename = KW_veto_utils.get_connection_filename(\
                         dbname,tmp_path=scratch_dir,verbose=verbose)

    GWconnection = sqlite3.connect(GW_working_filename)
    GWcursor = GWconnection.cursor()

    GWcursor.execute('create table GWtrigs (GPSTime double, SNR double)')

    ## find out file format and read in the data
    # tracker tracks 1) number of triggers retrieved and 2) if any triggers outside of specified segments
    tracker = {'counter': 0, 'outside': False}
    for t in trigger_files:
        ext = os.path.splitext(t)[-1]
        if ext == '.txt' or ext == '.dat':
            if verbose:
                print >> sys.stderr, "getting triggers from txt/dat file..."
            GWcursor = get_trigs_txt(GWcursor, t, segs, min_thresh, tracker,
                                     verbose)
        elif ext == '.xml':
            if verbose:
                print >> sys.stderr, "getting triggers from xml file..."
            GWcursor = get_trigs_xml(GWcursor, t, segs, min_thresh, tracker,
                                     verbose)
        else:
            print >> sys.stderr, """
        Error: unrecognized file format: please see --help and modify the 
               file to a supported format
        """
            sys.exit(1)

    GWconnection.commit()

    if verbose: print >> sys.stderr, "times and SNR for triggers retrieved!"

    # if some triggers were outside of given segments, warn the user
    if tracker['outside']:
        print >> sys.stderr, """
      Warning: Some of the triggers are outside of the segment list.
               Unless intentional (using DQ flags etc.), make sure you
               are using the right segment list.
               Ignoring...
      """

    # check how many triggers remained after cuts
    if tracker['counter'] == 0:
        print >> sys.stderr, """
      Error : No triggers remained after cut. 
              Please check trigger files and segments.
      """
        sys.exit(1)

    if verbose:
        print >> sys.stderr, "%d triggers are retrieved." % tracker['counter']

    return dbname, GW_working_filename, GWconnection, GWcursor, tracker[
        'counter']
Exemplo n.º 6
0
def get_trigs(channel, segs, min_thresh, trigs_loc=None,name_tag=None,\
              scratch_dir=".",verbose=True):
    """
    Get time and KW significance of KW triggers for a particular channel that
    occured in the specified segments and above specified KW significance
    threshold. 
    ifo has to be one of H1, H2, L1, V1.
    """
    if verbose: print >> sys.stderr, "getting data for %s..."%channel
  
    ## initialize SQLite database
    start_time = segs[0][0]
    end_time = segs[-1][1]
    duration = end_time - start_time
    prefix = "%s-%d-%d-KWtrigs"%(channel, start_time, duration)
    if name_tag != None:
      prefix = name_tag + "_" + prefix
    dbname = prefix+".db"
    # if the name already exists, rename the old one to avoid collision
    utils.rename(dbname)

    global KW_working_filename # so that it can be erased when error occurs
    KW_working_filename = utils.get_connection_filename(\
                         dbname,tmp_path=scratch_dir,verbose=verbose)

    KWconnection = sqlite3.connect(KW_working_filename)
    KWcursor = KWconnection.cursor()

    ## create a table for retrieved triggers
    KWcursor.execute('create table KWtrigs (GPSTime double, KWSignificance double, frequency int)')

    ## determine the KW trigger file we need
    ifo = channel.split("_")[0].upper()

    # Virgo case
    if trigs_loc is None and ifo == "V1":
      trigs_loc = "/archive/home/mabizoua/public_html/KW/"
    # LIGO case
    elif trigs_loc is None and ifo in ("H0","H1","H2","L0","L1"):
        trigs_loc = "/archive/home/lindy/public_html/triggers/s6/"
    # for S5, first two letters were not ifo but 's5'
    elif trigs_loc is None and ifo == "S5":   
      trigs_loc = "/archive/home/lindy/public_html/triggers/s5/"
    elif trigs_loc is None:
      print >> sys.stderr, "Error: channel name %s is unsupported. See --help for name format."%channel
      sys.exit(1)

    # sanity check
    if not os.path.exists(trigs_loc):
      print >> sys.stderr, "Error: KW daily dump %s not found."%trigs_loc

    ## select daily dump directories in the folder
    daily_dump_dirs = \
            [d for d in os.listdir(trigs_loc) if \
             re.match(r"(?P<start>\d{9,10}?)_(?P<end>\d{9,10}?)",d) != None \
             and len(d) < 22]
    # sort by time
    daily_dump_dirs.sort()

    # get the necessary daily dump folder
    analyze_dumps = [d for d in daily_dump_dirs \
                    if int(d.split("_")[0]) < end_time and \
                       int(d.split("_")[1]) > start_time]

    # for S5, some channels are not in certain dumps: do a little trick to keep
    # the name
    full_channel_name = ""
    for dump in analyze_dumps:
      trigs_file, tmp_name = \
        find_file_from_channel(channel,os.path.join(trigs_loc,dump))
      if full_channel_name == "" and tmp_name != None:
        full_channel_name = tmp_name
      if trigs_file != None:
        if verbose: print "retreiving data from %s..."%trigs_file

        for line in  open(trigs_file):
          # get central time and KW significance
          trig = line.split()
          t = float(trig[2]) 
          s = float(trig[7])
          f = float(trig[3])

          # check if KW trig is in the given segment and if its significance 
          # is above the minimum specified
          # FIXME: check inf/nan just in case
          if t in segs and s > min_thresh:
            # insert into the database
            # micro second for GPS time is accurate enough
            KWcursor.execute("insert into KWtrigs values (?, ?, ?)", ("%.3f"%t, "%.2f"%s,"%d"%f))

    if full_channel_name == "": # means there is no KW trigger
      full_channel_name = channel # better than nothing...

    # commit the insertions to the database
    KWconnection.commit()

    return dbname, KW_working_filename, KWconnection, KWcursor, full_channel_name
Exemplo n.º 7
0
def get_trigs(trigger_files,segs,min_thresh,name_tag=None,scratch_dir=".",verbose=True):
    """
    prepare the SQL database and retrieve the triggers    
    """
    # initialize SQLite database
    start_time = segs[0][0]
    end_time = segs[-1][1]
    prefix = "%d_%d_%d_GWtrigs"%(start_time,end_time,min_thresh)
    if name_tag != None:
      prefix = name_tag + "_" + prefix
    dbname = prefix+".db"
    # if the name already exists, rename the old one to avoid collisions
    KW_veto_utils.rename(dbname)
 
    global GW_working_filename # so that it can be erased when error occurs
    GW_working_filename = KW_veto_utils.get_connection_filename(\
                         dbname,tmp_path=scratch_dir,verbose=verbose)
   
    GWconnection = sqlite3.connect(GW_working_filename)
    GWcursor = GWconnection.cursor()

    GWcursor.execute('create table GWtrigs (GPSTime double, SNR double)')

    ## find out file format and read in the data
    # tracker tracks 1) number of triggers retrieved and 2) if any triggers outside of specified segments
    tracker = {'counter': 0,'outside': False}
    for t in trigger_files:
      ext=os.path.splitext(t)[-1]
      if ext == '.txt' or ext == '.dat':
        if verbose: print >> sys.stderr, "getting triggers from txt/dat file..."
        GWcursor = get_trigs_txt(GWcursor,t,segs,min_thresh,tracker,verbose)
      elif ext == '.xml':
        if verbose: print >> sys.stderr, "getting triggers from xml file..."
        GWcursor = get_trigs_xml(GWcursor,t,segs,min_thresh,tracker,verbose)
      else:
        print >> sys.stderr, """
        Error: unrecognized file format: please see --help and modify the 
               file to a supported format
        """
        sys.exit(1)
        
    GWconnection.commit()

    if verbose: print >> sys.stderr, "times and SNR for triggers retrieved!"

    # if some triggers were outside of given segments, warn the user
    if tracker['outside']:
      print >> sys.stderr, """
      Warning: Some of the triggers are outside of the segment list.
               Unless intentional (using DQ flags etc.), make sure you
               are using the right segment list.
               Ignoring...
      """

    # check how many triggers remained after cuts
    if tracker['counter'] == 0:
      print >> sys.stderr, """
      Error : No triggers remained after cut. 
              Please check trigger files and segments.
      """
      sys.exit(1)   
        
    if verbose:
      print >> sys.stderr, "%d triggers are retrieved."%tracker['counter']
 
    return dbname, GW_working_filename, GWconnection, GWcursor, tracker['counter']