3 # Copyright (C) 2011 Chad Hanna
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.
20 from scipy.interpolate import interp1d
22 from gstlal import reference_psd
24 from optparse import OptionParser
26 ### A program to turn an ASCII PSD or ASD into an xml file suitable for
27 ### consumption by various gstlal programs
33 ### 1. An early V1 input ASD from <a href=http://www.lsc-group.phys.uwm.edu/cgit/gstlal/plain/gstlal/share/v1_early_asd.txt>here</a>.
35 ### .. code-block:: bash
37 ### gstlal_psd_xml_from_asd_txt --instrument=V1 --output v1psd.xml.gz v1_early_asd.txt
40 ### 2. Other things possible, please add some!
46 ### * The PSD will be interpolated with with linear interpolation if needed.
47 ### The user is highly encouraged to plot the result to see if it is satisfactory.
53 ### +-----------------------------------------+------------------------------------------+------------+
54 ### | Reviewers | Hash | Date |
55 ### +=========================================+==========================================+============+
56 ### | Florent, Duncan Me., Jolien, Kipp, Chad | e5b22699b049f7a35dff468febad471ca6c737f3 | 2014-04-29 |
57 ### +-----------------------------------------+------------------------------------------+------------+
60 parser = OptionParser(description = __doc__)
61 parser.add_option("--instrument", help="instrument, e.g. H1")
62 parser.add_option("--output", metavar = "filename", help = "Set the name of the LIGO light-weight XML file to output")
63 parser.add_option("--df", metavar = "float", type = "float", default = 0.25, help = "set the frequency resolution to interpolate to, default = 0.25")
64 parser.add_option("--type", metavar = "type", default = "ASD", help = "input is ASD or PSD, default ASD")
66 options, filenames = parser.parse_args()
68 data = numpy.loadtxt(filenames[0], comments = '#')
70 if options.type == "ASD":
72 elif options.type == "PSD":
75 raise ValueError("--type must be ASD or PSD")
77 #FIXME hack to pad the series since it doesn't start at 0 :(
78 f_padded = numpy.concatenate((numpy.arange(0, f[0], options.df), f))
79 psd_padded = numpy.concatenate((numpy.ones(len(f_padded) - len(f)) * psd[0], psd))
81 uniformf = numpy.arange(0, f_padded.max(), options.df)
82 psdinterp = interp1d(f_padded, psd_padded)
84 psd = psdinterp(uniformf)
86 psdseries = lal.CreateREAL8FrequencySeries("Sn(f)", 0, 0, options.df, lal.Unit("s strain^2"), len(psd))
87 psdseries.data.data = psd
88 reference_psd.write_psd(options.output, {options.instrument: psdseries})