gstlal-inspiral  1.9.2
 All Classes Namespaces Files Functions
gstlal_svd_bank_calc_psd
1 #!/usr/bin/env python3
2 #
3 # Copyright (C) 2020 Leo Tsukada
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 """Reproduce the psd frequnecy arraies used for whitening a template bank from the given svd bank files and dump it to a xml file."""
19 
20 ### Usage
21 ### -----
22 ###
23 ### $ gstlal_svd_bank_calc_psd --write-psd ./reproduce_svd_bank_psd.xml.gz \\
24 ### --svd-bank-files H1-GSTLAL_SVD_BANK.xml.gz \\
25 ### --svd-bank-files L1-GSTLAL_SVD_BANK.xml.gz \\
26 ### --svd-bank-files V1-GSTLAL_SVD_BANK.xml.gz \\
27 ### --verbose
28 ###
29 ### Related programs
30 ### ----------------
31 ###
32 ### - :any:`gstlal_svd_bank`
33 ###
34 
35 #
36 # parse command line
37 #
38 
39 
40 from optparse import OptionParser
41 
42 from ligo.lw import ligolw
43 from ligo.lw import array as ligolw_array
44 from ligo.lw import param as ligolw_param
45 from ligo.lw import lsctables
46 from ligo.lw import utils as ligolw_utils
47 
48 from lal import series as lalseries
49 
50 from gstlal import svd_bank
51 
52 @ligolw_array.use_in
53 @ligolw_param.use_in
54 @lsctables.use_in
55 class LIGOLWContentHandler(ligolw.LIGOLWContentHandler):
56  pass
57 
58 def parse_command_line():
59  parser = OptionParser(description = __doc__)
60 
61  # add our own options
62  parser.add_option("--write-psd", metavar = "filename", help = "Write measured noise spectrum to this LIGO light-weight XML file (required).")
63  parser.add_option("--svd-bank-files", action="append", metavar = "filename", default=[], help = "Set svd bank files from which to make psd xml file. One can set this multiple times, e.g. --svd-bank-files H1-SVD-BANK...xml.gz --svd-bank-files L1-SVD-BANK...xml.gz --svd-bank-files V1-SVD-BANK...xml.gz.")
64  parser.add_option("-v", "--verbose", action = "store_true", help = "Be verbose (optional).")
65 
66  options, filenames = parser.parse_args()
67 
68  # check our own options
69  if options.write_psd is None:
70  raise ValueError("Must specify --write-psd")
71 
72  return options, filenames
73 
74 
75 #
76 # =============================================================================
77 #
78 # Main
79 #
80 # =============================================================================
81 #
82 
83 
84 options, filenames = parse_command_line()
85 
86 psds = {}
87 
88 
89 for svd_bank_path in options.svd_bank_files:
90  bank = svd_bank.read_banks(svd_bank_path, contenthandler = LIGOLWContentHandler, verbose = options.verbose)[0]
91  psds.update({bank.sngl_inspiral_table[0].ifo: bank.processed_psd})
92 
93 # Write to file
94 ligolw_utils.write_filename(lalseries.make_psd_xmldoc(psds), options.write_psd, verbose = options.verbose)