3 # Copyright (C) 2018 Kipp Cannon
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.
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.
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.
21 # =============================================================================
25 # =============================================================================
29 from optparse import OptionParser
33 from glue.ligolw import utils as ligolw_utils
34 from lal import series as lalseries
38 # =============================================================================
42 # =============================================================================
46 def parse_command_line():
47 parser = OptionParser(
50 parser.add_option("--prefix", metavar = "string", default = "psd_", help = "Prepend output files with this string (default = \"psd_\").")
51 parser.add_option("-v", "--verbose", action = "store_true", help = "Be verbose (optional).")
53 options, filenames = parser.parse_args()
55 if len(filenames) != 1:
56 raise ValueError("only file PSD file may be given")
58 return options, filenames
62 # =============================================================================
66 # =============================================================================
75 options, filenames = parse_command_line()
79 # load the PSD file iterate over instruments, writing ASD to ascii file
83 for filename in filenames:
84 for instrument, psd in lalseries.read_psd_xmldoc(ligolw_utils.load_filename(filename, verbose = options.verbose, contenthandler = lalseries.PSDContentHandler)).items():
85 filename = "%s%s.txt" % (options.prefix, instrument)
86 with open(filename, "w") as f:
88 print >>sys.stderr, "writing \"%s\" ..." % filename
89 for i, x in enumerate(psd.data.data):
90 print >>f, "%.16g %.16g" % (psd.f0 + i * psd.deltaF, x**.5)