gstlal  0.8.1
 All Classes Namespaces Files Functions Variables Pages
gstlal_psd_xml_from_asd_txt
1 #!/usr/bin/env python
2 #
3 # Copyright (C) 2011 Chad Hanna
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 import numpy
20 from scipy.interpolate import interp1d
21 import sys
22 from gstlal import reference_psd
23 from pylal.xlal import datatypes
24 from pylal.xlal.datatypes import lalunit
25 from optparse import OptionParser
26 
27 ## @file
28 # gstlal_psd_xml_from_asd_txt
29 #
30 #
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
32 #
33 #
34 # ### Usage
35 #
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>.
37 #
38 # $ gstlal_psd_xml_from_asd_txt --instrument=V1 --output v1psd.xml.gz v1_early_asd.txt
39 #
40 #
41 # -# other things possible, please add some!
42 #
43 # ### Command line options
44 #
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
49 #
50 # specify the ASD or PSD text file as a file argument at the end
51 #
52 #
53 # ### Notes
54 #
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
56 #
57 #
58 # ### Review status
59 #
60 # | Reviewers | git hash | date |
61 # | ----------------------------------- | --------------------- | ------------- |
62 # | Florent, Duncan Me., Jolien, Kipp, Chad |abedb7c19ad3d5348c000bc5f9435d5d2e810e42 | 2014-04-29 |
63 #
64 #
65 
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")
71 
72 options, filenames = parser.parse_args()
73 
74 data = numpy.loadtxt(filenames[0], comments = '#')
75 f = data[:,0]
76 if options.type == "ASD":
77  psd = data[:,1]**2
78 elif options.type == "PSD":
79  psd = data[:,1]
80 else:
81  raise ValueError("--type must be ASD or PSD")
82 
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))
86 
87 uniformf = numpy.arange(0, f_padded.max(), options.df)
88 psdinterp = interp1d(f_padded, psd_padded)
89 
90 psd = psdinterp(uniformf)
91 
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})