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
23 from pylal.xlal
import datatypes
24 from pylal.xlal.datatypes
import lalunit
25 from optparse
import OptionParser
28 # gstlal_psd_xml_from_asd_txt
31 # A program to turn an ASCII PSD or ASD into an xml file suitable for consumption by various gstlal programs; see gstlal_psd_xml_from_asd_txt for details
36 # -# 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>.
38 # $ gstlal_psd_xml_from_asd_txt --instrument=V1 --output v1psd.xml.gz v1_early_asd.txt
41 # -# other things possible, please add some!
43 # ### Command line options
45 # + `--instrument` [string]: Instrument, e.g. H1
46 # + `--output` [filename]: Set the name of the LIGO light-weight XML file to output
47 # + `--df` [float] (Hz): Set the frequency resolution to interpolate to, default = 0.25
48 # + `--type` [ASD|PSD]: Input is ASD or PSD, default ASD
50 # specify the ASD or PSD text file as a file argument at the end
55 # - The PSD will be interpolated with with linear interpolation if needed. The user is highly encouraged to plot the result to see if it is satisfactory. See, e.g., gstlal_plot_psd
60 # | Reviewers | git hash | date |
61 # | ----------------------------------- | --------------------- | ------------- |
62 # | Florent, Duncan Me., Jolien, Kipp, Chad |abedb7c19ad3d5348c000bc5f9435d5d2e810e42 | 2014-04-29 |
66 parser = OptionParser(description = __doc__)
67 parser.add_option("--instrument", help="instrument, e.g. H1")
68 parser.add_option("--output", metavar = "filename", help = "Set the name of the LIGO light-weight XML file to output")
69 parser.add_option("--df", metavar = "
float", type = "
float", default = 0.25, help = "set the frequency resolution to interpolate to, default = 0.25")
70 parser.add_option("--type", metavar = "ASD|PSD", default = "ASD", help = "input is ASD or PSD, default ASD")
72 options, filenames = parser.parse_args()
74 data = numpy.loadtxt(filenames[0], comments = '
#')
76 if options.type ==
"ASD":
78 elif options.type ==
"PSD":
81 raise ValueError(
"--type must be ASD or PSD")
83 #FIXME hack to pad the series since it doesn't start at 0 :(
84 f_padded = numpy.concatenate((numpy.arange(0, f[0], options.df), f))
85 psd_padded = numpy.concatenate((numpy.ones(len(f_padded) - len(f)) * psd[0], psd))
87 uniformf = numpy.arange(0, f_padded.max(), options.df)
88 psdinterp = interp1d(f_padded, psd_padded)
90 psd = psdinterp(uniformf)
92 psdseries = datatypes.real8frequencyseries.REAL8FrequencySeries(deltaF=options.df, data=psd, sampleUnits=lalunit.LALUnit("s strain^2"), f0=0)
93 reference_psd.
write_psd(options.output, {options.instrument: psdseries})