gstlal  1.4.1
gstlal_ligo_data_find_check
Go to the documentation of this file.
1 #!/usr/bin/env python
2 #
3 # Copyright (C) 2013 Kipp Cannon
4 #
5 # This program is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by the
7 # Free Software Foundation; either version 2 of the License, or (at your
8 # option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13 # Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 
19 ## @file
20 # A program to check for gaps in frame cache files according to segment lists
21 
22 #
23 # =============================================================================
24 #
25 # Preamble
26 #
27 # =============================================================================
28 #
29 
30 
31 from optparse import OptionParser
32 import sqlite3
33 import sys
34 
35 
36 from lal.utils import CacheEntry
37 
38 
39 from glue.ligolw import dbtables
40 from glue.ligolw import utils as ligolw_utils
41 from glue.ligolw.utils import segments as ligolw_segments
42 
43 
44 #
45 # =============================================================================
46 #
47 # Command Line
48 #
49 # =============================================================================
50 #
51 
52 
53 def parse_command_line():
54  parser = OptionParser(
55  )
56  parser.add_option("-s", "--segments-file", metavar = "filename", help = "Load segment lists from this .xml file (required).")
57  parser.add_option("-n", "--segments-name", metavar = "name", help = "Load this segment list from the file (required).")
58  parser.add_option("-v", "--verbose", action = "store_true", help = "Be verbose (optional).")
59 
60  options, filenames = parser.parse_args()
61 
62  required = ("segments_file", "segments_name")
63  missing = [opt for opt in required if getattr(options, opt) is None]
64  if missing:
65  raise ValueError("missing options: %s" % ", ".join("--%s" % opt.replace("_", "-") for opt in missing))
66 
67  return options, filenames or [None]
68 
69 
70 #
71 # =============================================================================
72 #
73 # Main
74 #
75 # =============================================================================
76 #
77 
78 
79 #
80 # parse command line
81 #
82 
83 
84 options, filenames = parse_command_line()
85 
86 
87 #
88 # load segments and subtract time spanned by cache files
89 #
90 
91 
92 if options.segments_file.lower().endswith(".sqlite"):
93  if options.verbose:
94  print >>sys.stderr, "reading %s ..." % options.segments_file
95  seglists = ligolw_segments.segmenttable_get_by_name(dbtables.get_xml(sqlite3.connect(options.segments_file)), options.segments_name).coalesce()
96 else:
97  seglists = ligolw_segments.segmenttable_get_by_name(ligolw_utils.load_filename(options.segments_file, contenthandler = ligolw_segments.LIGOLWContentHandler, verbose = options.verbose), options.segments_name).coalesce()
98 
99 
100 for filename in filenames:
101  if options.verbose:
102  print >>sys.stderr, "loading %s ..." % (filename or "stdin")
103  for line in open(filename) if filename else sys.stdin:
104  seglists -= CacheEntry(line).segmentlistdict
105 
106 
107 #
108 # check for remainders
109 #
110 
111 
112 if any(seglists.values()):
113  print >>sys.stderr, "gaps found in cache. total missing time: %s" % str(abs(seglists))
114  sys.exit(1)
115 if options.verbose:
116  print >>sys.stderr, "cache is complete"