gstlal-inspiral  0.4.2
 All Classes Namespaces Files Functions Variables Pages
gstlal_iir_bank
1 #!/usr/bin/env python
2 #
3 # Copyright (C) 2011-2012 Shaun Hooper
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
20 # Compute an IIR bank for inspiral waveforms
21 #
22 # ### Command line interface
23 #
24 # + `--flow` [Hz] (float): Set the template low-frequency cut-off (default = 40.0).
25 # + `--sampleRate` [Hz] (float): Set the sample rate of the IIR template bank (optional).
26 # + `--padding` [pad] (float): Fractional amount to pad time slices (default = 1.1).
27 # + `--epsilon` [pad] (float): Second order correction factor (default = 0.02).
28 # + `--reference-psd` [filename]: Load the spectrum from this LIGO light-weight XML file (required).
29 # + `--template-bank` [filename]: Set the name of the LIGO light-weight XML file from which to load the template bank (required).
30 # + `--instrument` [ifo]: Set the instrument.
31 # + `--output` [filename]: Set the filename in which to save the template bank (required).
32 # + `--verbose`: Be verbose.
33 # + `--downsample`: Choose if you want to downsample IIR bank (recommended).
34 
35 
36 import sys
37 import scipy
38 import numpy
39 from optparse import OptionParser
40 
41 parser = OptionParser(description = __doc__)
42 parser.add_option("--flow", metavar = "Hz", type = "float", default = 40.0, help = "Set the template low-frequency cut-off (default = 40.0).")
43 parser.add_option("--sampleRate", metavar = "Hz", type = "float", help = "Set the sample rate of the IIR template bank (optional).")
44 parser.add_option("--padding", metavar = "pad", type = "float", default = 1.1, help = "Fractional amount to pad time slices.")
45 parser.add_option("--epsilon", metavar = "pad", type = "float", default = 0.02, help = "Second order correction factor.")
46 # FIXME add this when we know how to (by changing the parameters accoring to a function or optimizing on the fly or something)
47 #parser.add_option("--iir-tolerance", metavar = "match", type = "float", default = 0.99, help = "Set the SVD reconstruction tolerance (default = 0.99).")
48 parser.add_option("--reference-psd", metavar = "filename", help = "load the spectrum from this LIGO light-weight XML file (required).")
49 parser.add_option("--template-bank", metavar = "filename", help = "Set the name of the LIGO light-weight XML file from which to load the template bank (required).")
50 #FIXME figure out how to encode instrument info
51 parser.add_option("--instrument", metavar = "ifo", help = "Set the instrument")
52 
53 parser.add_option("--output", metavar = "filename", help = "Set the filename in which to save the template bank (required).")
54 parser.add_option("-v", "--verbose", action = "store_true", help = "Be verbose (optional).")
55 parser.add_option("--downsample", action = "store_true", help = "Choose if you want to downsample IIR bank (recommended)")
56 
57 options, filenames = parser.parse_args()
58 
59 required_options = ("template_bank", "output")
60 
61 missing_options = [option for option in required_options if getattr(options, option) is None]
62 if missing_options:
63  raise ValueError, "missing required option(s) %s" % ", ".join("--%s" % option.replace("_", "-") for option in sorted(missing_options))
64 
65 
66 from glue.ligolw import ligolw
67 from glue.ligolw import array
68 from glue.ligolw import param
69 array.use_in(ligolw.LIGOLWContentHandler)
70 param.use_in(ligolw.LIGOLWContentHandler)
71 from glue.ligolw import utils
72 from gstlal import cbc_template_iir
73 from pylal.series import read_psd_xmldoc
74 from glue.ligolw import utils, lsctables
75 
76 # read bank file
77 bank_xmldoc = utils.load_filename(options.template_bank, gz=options.template_bank.endswith('.gz'))
78 sngl_inspiral_table = lsctables.table.get_table(bank_xmldoc, lsctables.SnglInspiralTable.tableName)
79 fFinal = max(sngl_inspiral_table.getColumnByName("f_final"))
80 
81 # read psd file
82 if options.reference_psd:
83  ALLpsd = read_psd_xmldoc(utils.load_filename(options.reference_psd, verbose=options.verbose, contenthandler=ligolw.LIGOLWContentHandler))
84  bank_sngl_table = lsctables.table.get_table( bank_xmldoc,lsctables.SnglInspiralTable.tableName )
85  psd = ALLpsd[bank_sngl_table[0].ifo]
86  # smooth and create an interp object
87  psd = cbc_template_iir.smooth_and_interp(psd)
88 else:
89  psd = None
90 
91 
92 # get the iir coefficients
93 A, B, D, snrs = cbc_template_iir.makeiirbank(bank_xmldoc, sampleRate = 4096, psd_interp = psd, verbose=options.verbose, padding=options.padding, flower=options.flow, downsample = options.downsample, output_to_xml = True, epsilon = options.epsilon)
94 
95 utils.write_filename(bank_xmldoc, options.output, gz=options.output.endswith('.gz'))
96 
97 #import pylab
98 #pylab.hist(snrs)
99 #pylab.show()
100