LALPulsar  6.1.0.1-b72065a
make_frame_cache.py
Go to the documentation of this file.
1 """
2 Matt Pitkin - 13/11/06
3 code to take scan a directory of frame files and output it as a frame cache file between given
4 times
5 """
6 
7 from __future__ import print_function
8 
9 # import modules
10 import sys
11 import os
12 import getopt
13 
14 # program usage
15 
16 
17 def usage():
18  msg = """\
19 Usage: make_frame_cache [options]
20 
21  -h, --help display this message
22  -d, --frame-dir directory of frame file
23  -s, --gps-start-time start time of frames to output
24  -e, --gps-end-time end time fo frames to output
25  -o, --output-file file to output frame cache to
26 """
27  print(msg, file=sys.stderr)
28 
29 
30 # parse command line
31 shortop = "hd:s:e:o:"
32 longop = ["help", "frame-dir=", "gps-start-time=", "gps-end-time=", "output-file="]
33 
34 try:
35  opts, args = getopt.getopt(sys.argv[1:], shortop, longop)
36 except getopt.GetoptError:
37  usage()
38  sys.exit(1)
39 
40 
41 # process options
42 for o, a in opts:
43  if o in ("-h", "--help"):
44  usage()
45  sys.exit(1)
46  elif o in ("-d", "--frame-dir"):
47  frame_dir = a
48  elif o in ("-s", "--gps-start-time"):
49  start = int(a)
50  elif o in ("-e", "--gps-end-time"):
51  end = int(a)
52  elif o in ("-o", "--output-file"):
53  output = a
54  else:
55  print("Unknown option: {}".format(o), file=sys.stderr)
56  usage()
57  sys.exit(1)
58 
59 # get all files in frame dir
60 try:
61  files = os.listdir(frame_dir)
62  print(files[0], file=sys.stderr)
63 except Exception as e:
64  print("Problem listing directory {}".format(frame_dir), file=sys.stderr)
65  sys.exit(1)
66 
67 # open output file
68 try:
69  f = open(output, "w")
70 except Exception as e:
71  print("Can't open file {}".format(output), file=sys.stderr)
72  sys.exit(1)
73 
74 files.sort()
75 
76 # scan through directory and output frame files with given time range
77 i = 0
78 j = 0
79 while i < len(files):
80  # check if frame file
81  if ".gwf" in files[i]:
82  # get frame channel, time and duration
83  ifo = files[i][0]
84  frinfo = files[i].split("-")
85  channel = frinfo[1] # channel should be first field
86  time = int(frinfo[2]) # time should be the second field
87 
88  # find the - before the duration (last value)
89  index1 = files[i].rfind("-")
90  # find the . before gwf
91  index2 = files[i].rfind(".")
92 
93  duration = int(files[i][index1 + 1 : index2])
94 
95  # check if file is within time range and output
96  if time + duration > start and time <= end:
97  cache = (
98  ifo
99  + " "
100  + channel
101  + " "
102  + str(time)
103  + " "
104  + str(duration)
105  + " "
106  + "file://localhost"
107  + frame_dir
108  + "/"
109  + files[i]
110  + "\n"
111  )
112  f.write(cache)
113  j += 1
114 
115  i += 1
116 
117 f.close()
118 
119 # if no frame files were found then say so
120 if j == 0:
121  print("No frames files between {} and {}.".format(start, end), file=sys.stderr)
122  os.remove(output)
123 
124 sys.exit(0)