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
Exemple #2
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']
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']