LAL  7.5.0.1-8083555
lal_path2cache.py
Go to the documentation of this file.
1 #
2 # Copyright (C) 2006 Kipp Cannon
3 #
4 # This program is free software; you can redistribute it and/or modify it
5 # under the terms of the GNU General Public License as published by the
6 # Free Software Foundation; either version 2 of the License, or (at your
7 # option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
12 # Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along
15 # with this program; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 
18 
19 #
20 # =============================================================================
21 #
22 # Preamble
23 #
24 # =============================================================================
25 #
26 
27 
28 import os.path
29 import sys
30 from optparse import OptionParser
31 
32 
33 from ligo import segments
34 
35 
36 from lal.utils import CacheEntry
37 
38 
39 from lal import git_version
40 
41 
42 __author__ = "Kipp Cannon <kipp.cannon@ligo.org>"
43 __version__ = "git id %s" % git_version.id
44 __date__ = git_version.date
45 
46 
47 #
48 # =============================================================================
49 #
50 # Command Line
51 #
52 # =============================================================================
53 #
54 
55 
57  parser = OptionParser(
58  #version = "Name: %%prog\n%s" % git_version.verbose_msg,
59  usage = "usage: %prog [options]\n\nExample:\n\tls *.xml | %prog"
60  )
61  parser.add_option("-a", "--include-all", action = "store_true", help = "Include all files in output. Unparseable file names are assigned empty metadata.")
62  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.")
63  parser.add_option("-i", "--input", metavar="filename", help="Read input from this file (default = stdin).")
64  parser.add_option("-o", "--output", metavar="filename", help="Write output to this file (default = stdout).")
65  parser.add_option("-v", "--verbose", action="store_true", help="Be verbose.")
66  return parser.parse_args()[0]
67 
68 
69 #
70 # =============================================================================
71 #
72 # Main
73 #
74 # =============================================================================
75 #
76 
77 
78 #
79 # Parse command line
80 #
81 
82 
83 options = parse_command_line()
84 
85 
86 #
87 # Open input and output streams
88 #
89 
90 
91 if options.input is not None:
92  src = open(options.input)
93 else:
94  src = sys.stdin
95 
96 if options.output is not None:
97  dst = open(options.output, "w")
98 else:
99  dst = sys.stdout
100 
101 #
102 # Other initializations
103 #
104 
105 
106 path_count = 0
107 seglists = segments.segmentlistdict()
108 
109 
110 #
111 # Filter input one line at a time
112 #
113 
114 
115 for line in src:
116  path, filename = os.path.split(line.strip())
117  url = "file://localhost%s" % os.path.abspath(os.path.join(path, filename))
118  try:
119  cache_entry = CacheEntry.from_T050017(url)
120  except ValueError as e:
121  if options.include_all:
122  cache_entry = CacheEntry(None, None, None, url)
123  elif options.force:
124  continue
125  else:
126  raise e
127  print(str(cache_entry), file=dst)
128  path_count += 1
129  if cache_entry.segment is not None:
130  seglists |= cache_entry.segmentlistdict.coalesce()
131 
132 
133 #
134 # Summary
135 #
136 
137 
138 if options.verbose:
139  print("Size of cache: %d URLs" % path_count, file=sys.stderr)
140  for instrument, seglist in list(seglists.items()):
141  ext = seglist.extent()
142  dur = abs(seglist)
143  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)
144  span = seglists.union(seglists)
145  ext = span.extent()
146  dur = abs(span)
147  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()