gstlal-inspiral  0.4.2
 All Classes Namespaces Files Functions Variables Pages
gstlal_inspiral_iir_bank_pipe
1 #!/usr/bin/env python
2 #
3 # Copyright (C) 2011-2012 Shaun Hooper, Chad Hanna
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 gstlal_inspiral_iir_bank_pipe
20 # A program to make an HTCondor DAG to create banks of IIR filters for an online CBC analysis
21 #
22 # ### Command line interface
23 # + `--flow` [Hz] (float): Set the template low-frequency cut-off (default = 40.0).
24 # + `--sampleRate` [Hz] (float): Set the sample rate of the IIR template bank (optional).
25 # + `--padding` [pad] (float): Fractional amount to pad time slices (default = 1.1).
26 # + `--epsilon` [pad] (float): Second order correction factor (default = 0.02).
27 # + `--reference-psd` [file]: Set the name of the reference psd file, required.
28 # + `--bank-cache` [file]: Set the name of the bank cache, required.
29 # + `--output-name`: Set the base name of the output, required.
30 # + `--instrument`: Set the name of the instrument, required.
31 # + `--verbose`: Be verbose.
32 # + `--downsample`: Choose if you want to downsample IIR bank (recommended).
33 
34 """
35 This program makes a dag to generate iir banks
36 """
37 
38 __author__ = 'Chad Hanna <channa@caltech.edu>, Shaun Hooper <shaun.hooper@uwa.edu.au>'
39 
40 ##############################################################################
41 # import standard modules and append the lalapps prefix to the python path
42 import sys, os, copy, math
43 import subprocess, socket, tempfile
44 
45 ##############################################################################
46 # import the modules we need to build the pipeline
47 from glue import iterutils
48 from glue import pipeline
49 from glue import lal
50 from glue.ligolw import lsctables
51 from glue import segments
52 from optparse import OptionParser
53 
54 def which(prog):
55  which = subprocess.Popen(['which',prog], stdout=subprocess.PIPE)
56  out = which.stdout.read().strip()
57  if not out:
58  print >>sys.stderr, "ERROR: could not find %s in your path, have you built the proper software and source the proper env. scripts?" % (prog,prog)
59  raise ValueError
60  return out
61 
62 def log_path():
63  host = socket.getfqdn()
64  #FIXME add more hosts as you need them
65  if 'caltech.edu' in host: return '/usr1/' + os.environ['USER']
66  if 'phys.uwm.edu' in host: return '/localscratch/' + os.environ['USER']
67  if 'aei.uni-hannover.de' in host: return '/local/user/' + os.environ['USER']
68  if 'phy.syr.edu' in host: return '/usr1/' + os.environ['USER']
69 
70 
71 class bank_DAG(pipeline.CondorDAG):
72 
73  def __init__(self, name, logpath = log_path()):
74  self.basename = name
75  tempfile.tempdir = logpath
76  tempfile.template = self.basename + '.dag.log.'
77  logfile = tempfile.mktemp()
78  fh = open( logfile, "w" )
79  fh.close()
80  pipeline.CondorDAG.__init__(self,logfile)
81  self.set_dag_file(self.basename)
82  self.jobsDict = {}
83  self.node_id = 0
84  self.output_cache = []
85 
86  def add_node(self, node):
87  node.set_retry(3)
88  self.node_id += 1
89  node.add_macro("macroid", self.node_id)
90  pipeline.CondorDAG.add_node(self, node)
91 
92  def write_cache(self):
93  out = self.basename + ".cache"
94  f = open(out,"w")
95  for c in self.output_cache:
96  f.write(str(c)+"\n")
97  f.close()
98 
99 class gstlal_iir_bank_job(pipeline.CondorDAGJob):
100  """
101  A gstlal_iir_bank job
102  """
103  def __init__(self, executable=which('gstlal_iir_bank'), tag_base='gstlal_iir_bank'):
104  """
105  """
106  self.__prog__ = 'gstlal_iir_bank'
107  self.__executable = executable
108  self.__universe = 'vanilla'
109  pipeline.CondorDAGJob.__init__(self,self.__universe,self.__executable)
110  self.add_condor_cmd('getenv','True')
111  self.add_condor_cmd('requirements', 'Memory > 1999') #FIXME is this enough?
112  self.tag_base = tag_base
113  self.add_condor_cmd('environment',"KMP_LIBRARY=serial;MKL_SERIAL=yes")
114  self.set_sub_file(tag_base+'.sub')
115  self.set_stdout_file('logs/'+tag_base+'-$(macroid)-$(process).out')
116  self.set_stderr_file('logs/'+tag_base+'-$(macroid)-$(process).err')
117 
118 
119 class gstlal_iir_bank_node(pipeline.CondorDAGNode):
120  """
121  """
122  def __init__(self, job, dag, template_bank, ifo, flow, reference_psd, sampleRate, padding, downsample, epsilon=0.02, p_node=[]):
123 
124  pipeline.CondorDAGNode.__init__(self,job)
125  self.add_var_opt("template-bank", template_bank)
126  self.add_var_opt("instrument", ifo)
127  self.add_var_opt("flow", flow)
128  self.add_var_opt("reference-psd", reference_psd)
129  self.add_var_opt("sampleRate",sampleRate)
130  self.add_var_opt("padding",padding)
131  if downsample: self.add_var_opt("downsample","")
132  self.add_var_opt("epsilon", epsilon)
133  iir_bank_name_path = os.path.split(template_bank)
134  iir_bank_name = iir_bank_name_path[0] + "/iir_" + iir_bank_name_path[1]
135  self.add_var_opt("output", iir_bank_name)
136  dag.output_cache.append(lal.CacheEntry(ifo, "-", segments.segment(0, 999999999), "file://localhost%s" % (iir_bank_name,)))
137  for p in p_node:
138  self.add_parent(p)
139  dag.add_node(self)
140 
141 def parse_command_line():
142  parser = OptionParser()
143  parser.add_option("--flow", metavar = "Hz", type = "float", default = 40.0, help = "Set the template low-frequency cut-off (default = 40.0).")
144  parser.add_option("--sampleRate", metavar = "Hz", type = "float", help = "Set the sample rate of the IIR template bank (optional).")
145  parser.add_option("--padding", metavar = "pad", type = "float", default = 1.1, help = "Fractional amount to pad time slices.")
146  parser.add_option("--epsilon", metavar = "pad", type = "float", default = 0.02, help = "Second order correction factor.")
147  parser.add_option("--reference-psd", metavar = "file", help = "Set the name of the reference psd file, required")
148  parser.add_option("--bank-cache", metavar = "file", help = "Set the name of the bank cache, required")
149  parser.add_option("--output-name", help = "set the base name of the output, required")
150  parser.add_option("--instrument", help = "set the name of the instrument, required")
151  parser.add_option("-v", "--verbose", action = "store_true", help = "Be verbose.")
152  parser.add_option("--downsample", action = "store_true", help = "Choose if you want to downsample IIR bank (recommended)")
153  options, filenames = parser.parse_args()
154 
155  return options, filenames
156 
157 options, filenames = parse_command_line()
158 
159 # get input arguments
160 ifo = options.instrument
161 ref_psd = options.reference_psd
162 input_cache = options.bank_cache
163 psdname = ref_psd.split('.')[0]
164 
165 try: os.mkdir("logs")
166 except: pass
167 dag = bank_DAG(options.output_name)
168 
169 iirJob = gstlal_iir_bank_job(tag_base=psdname + "_gstlal_iir_bank")
170 iirNode = {}
171 
172 
173 for f in [lal.CacheEntry(line).path for line in open(input_cache)]:
174  print f
175  iirNode[f] = gstlal_iir_bank_node(iirJob, dag, f, ifo, options.flow, ref_psd, options.sampleRate, options.padding, options.downsample, options.epsilon)
176 
177 dag.write_sub_files()
178 dag.write_dag()
179 dag.write_script()
180 dag.write_cache()
181 
182 
183