LAL 7.7.0.1-678514e
lal_path2cache.py
Go to the documentation of this file.
1##python
2#
3# Copyright (C) 2006 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
20#
21# =============================================================================
22#
23# Preamble
24#
25# =============================================================================
26#
27
28
29import os.path
30import sys
31from optparse import OptionParser
32
33
34import igwn_segments as segments
35
36
37from lal.utils import CacheEntry
38
39
40from lal import git_version
41
42
43__author__ = "Kipp Cannon <kipp.cannon@ligo.org>"
44__version__ = "git id %s" % git_version.id
45__date__ = git_version.date
46
47
48#
49# =============================================================================
50#
51# Command Line
52#
53# =============================================================================
54#
55
56
58 parser = OptionParser(
59 #version = "Name: %%prog\n%s" % git_version.verbose_msg,
60 usage = "usage: %prog [options]\n\nExample:\n\tls *.xml | %prog"
61 )
62 parser.add_option("-a", "--include-all", action = "store_true", help = "Include all files in output. Unparseable file names are assigned empty metadata.")
63 parser.add_option("-f", "--force", action = "store_true", help = "Ignore errors. Unparseable file names are removed from the output. This has no effect if --include-all is given.")
64 parser.add_option("-i", "--input", metavar="filename", help="Read input from this file (default = stdin).")
65 parser.add_option("-o", "--output", metavar="filename", help="Write output to this file (default = stdout).")
66 parser.add_option("-v", "--verbose", action="store_true", help="Be verbose.")
67 return parser.parse_args()[0]
68
69
70#
71# =============================================================================
72#
73# Main
74#
75# =============================================================================
76#
77
78
79#
80# Parse command line
81#
82
83
85
86
87#
88# Open input and output streams
89#
90
91
92if options.input is not None:
93 src = open(options.input)
94else:
95 src = sys.stdin
96
97if options.output is not None:
98 dst = open(options.output, "w")
99else:
100 dst = sys.stdout
101
102#
103# Other initializations
104#
105
106
107path_count = 0
108seglists = segments.segmentlistdict()
109
110
111#
112# Filter input one line at a time
113#
114
115
116for line in src:
117 path, filename = os.path.split(line.strip())
118 url = "file://localhost%s" % os.path.abspath(os.path.join(path, filename))
119 try:
120 cache_entry = CacheEntry.from_T050017(url)
121 except ValueError as e:
122 if options.include_all:
123 cache_entry = CacheEntry(None, None, None, url)
124 elif options.force:
125 continue
126 else:
127 raise e
128 print(str(cache_entry), file=dst)
129 path_count += 1
130 if cache_entry.segment is not None:
131 seglists |= cache_entry.segmentlistdict.coalesce()
132
133
134#
135# Summary
136#
137
138
139if options.verbose:
140 print("Size of cache: %d URLs" % path_count, file=sys.stderr)
141 for instrument, seglist in list(seglists.items()):
142 ext = seglist.extent()
143 dur = abs(seglist)
144 print("Interval spanned by %s: %s (%s s total, %.4g%% duty cycle)" % (instrument, str(ext), str(dur), 100.0 * float(dur) / float(abs(ext))), file=sys.stderr)
145 span = seglists.union(seglists)
146 ext = span.extent()
147 dur = abs(span)
148 print("Interval spanned by union: %s (%s s total, %.4g%% duty cycle)" % (str(ext), str(dur), 100.0 * float(dur) / float(abs(ext))), file=sys.stderr)
A Python object representing one line in a LAL cache file.
Definition: cache.py:150
def parse_command_line()