29 Build a LAL cache from a list of LIGO LW XML files containing search
35 from optparse
import OptionParser
40 from ligo.lw
import ligolw
41 from ligo.lw
import lsctables
42 from ligo.lw
import utils
as ligolw_utils
43 from functools
import reduce
44 from lal
import git_version
47 __author__ =
"Kipp Cannon <kipp.cannon@ligo.org>"
48 __version__ =
"git id %s" % git_version.id
49 __date__ = git_version.date
62 parser = OptionParser(
64 usage =
"%prog [options] filenames ...",
65 description =
"Generates a LAL format cache file describing a collection of LIGO light-weight XML files. The cache is constructed by parsing the search_summary table in each file to extract the instruments and time each file spans. To allow long file lists to be processed, the filenames are interpreted as shell patterns (wildcard expansion is performed)."
67 parser.add_option(
"--description", metavar =
"string", help =
"Set all descriptions to this string. Use \"-\" for no description. If not given then the description will be extracted from the search summary rows, and if the search summary rows do not provide a unique description an error is raised.")
68 parser.add_option(
"--observatory", metavar =
"string", help =
"Set all observatories to this string. Use \"-\" for no observatory. If not given then the union of the instruments from the search summary rows will be used to construct an \"observatory\" string.")
69 parser.add_option(
"-v",
"--verbose", action =
"store_true", help =
"Be verbose.")
70 parser.add_option(
"-o",
"--output", metavar =
"filename", help =
"Write output to this file (default = stdout).")
71 parser.add_option(
"-p",
"--program", metavar =
"name", action =
"append", help =
"Obtain instruments, starts, durations, and descriptions from the search summary rows for this program (default = use all search summary rows). Can be given multiple times to select rows from more than one program.")
72 options, filenames = parser.parse_args()
75 options.output =
file(options.output,
"w")
77 options.output = sys.stdout
79 if options.program
is not None:
80 options.program = set(options.program)
85 filenames = [filename
for g
in filenames
for filename
in glob.glob(g)]
87 return options, filenames
101 Return True if name & attrs describe a search summary table or a
104 return lsctables.SearchSummaryTable.CheckProperties(name, attrs)
or lsctables.ProcessTable.CheckProperties(name, attrs)
110 ligolw.PartialLIGOLWContentHandler.__init__(self, xmldoc, element_filter)
125 for n, filename
in enumerate(filenames):
128 print(
"%d/%d:" % (n + 1, len(filenames)), end=
' ', file=sys.stderr)
129 xmldoc = ligolw_utils.load_filename(filename, verbose = options.verbose, contenthandler = ContentHandler)
130 searchsumm = lsctables.SearchSummaryTable.get_table(xmldoc)
133 if options.program
is not None:
134 process_table = lsctables.ProcessTable.get_table(xmldoc)
135 process_ids = reduce(
lambda a, b: a | b, list(map(process_table.get_ids_by_program, options.program)))
140 seglists = searchsumm.get_out_segmentlistdict(process_ids).coalesce()
142 raise ValueError(
"%s: no matching rows found in search summary table" % filename)
144 if options.program
is not None:
145 raise ValueError(
"%s: null value in ifos column in search_summary table" % filename)
146 raise ValueError(
"%s: null value in ifos column in search_summary table, try using --program" % filename)
149 observatory = (options.observatory
and options.observatory.strip())
or "+".join(sorted(seglists))
152 if options.description:
153 description = options.description
155 if process_ids
is None:
156 description = set(searchsumm.getColumnByName(
"comment"))
158 description = set(row.comment
for row
in searchsumm
if row.process_id
in process_ids)
159 if len(description) < 1:
160 raise ValueError(
"%s: no matching rows found in search summary table" % filename)
161 if len(description) > 1:
162 raise ValueError(
"%s: comments in matching rows of search summary table are not identical" % filename)
163 description = description.pop().strip()
or None
166 url =
"file://localhost" + os.path.abspath(filename)
169 print(str(
CacheEntry(observatory, description, seglists.extent_all(), url)), file=options.output)
A Python object representing one line in a LAL cache file.
def __init__(self, xmldoc)
def element_filter(name, attrs)
Return True if name & attrs describe a search summary table or a process table.