3 # Copyright (C) 2013 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.
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 from glue.ligolw
import ligolw
27 from glue.ligolw
import array
28 from glue.ligolw
import param
29 array.use_in(ligolw.LIGOLWContentHandler)
30 param.use_in(ligolw.LIGOLWContentHandler)
31 from glue.ligolw
import utils
32 from pylal
import series as lalseries
33 from pylal
import datatypes as laltypes
36 # A program to fit a PSD to a polynomial
41 # -# Fit a PSD to a polynomial with default settings (consider using gstlal_plot_psd to compare the results)
43 # $ gstlal_psd_polyfit --output smooth.xml.gz H1refpsd.xml.gz
49 # ### Command line options
51 # - `--median-window` [int]: Median window in sample points to apply running median for removing sharp features. Default 8. Setting it to 1 effectively disables this filter.
52 # - `--output` [filename]: Set the name of the LIGO light-weight XML file to output
53 # - `--poly-order` [int]: Set the order of the fitting polynomial. default 10
54 # - `--low-fit-freq` [float]: Set the low frequency at which to begin fitting in Hz. default 30
55 # - `--high-fit-freq` [float]: Set the high frequency at which to stop fitting in Hz. default 6500
58 parser = OptionParser(description = __doc__)
59 parser.add_option("--median-window", type =
int, default = 8, help="Median window in sample points to apply running median for removing sharp features. Default 8. Setting it to 1 effectively disables this filter.")
60 parser.add_option("--output", metavar = "filename", help = "Set the name of the LIGO light-weight XML file to output")
61 parser.add_option("--poly-order", type =
int, default = 10, help = "Set the order of the fitting polynomial. default 10")
62 parser.add_option("--low-fit-freq", type =
float, default = 30, help = "Set the low frequency at which to begin fitting in Hz. default 30")
63 parser.add_option("--high-fit-freq", type =
float, default = 6500, help = "Set the high frequency at which to stop fitting in Hz. default 6500")
65 options, filenames = parser.parse_args()
67 psds = lalseries.read_psd_xmldoc(utils.load_filename(filenames[0], verbose = True, contenthandler = ligolw.LIGOLWContentHandler))
69 # FIXME Don't assume all psds have same resolution
70 psd = psds.values()[0]
71 minsample = int(options.low_fit_freq / psd.deltaF)
72 maxsample =
int(options.high_fit_freq / psd.deltaF)
75 for ifo, psd in psds.items():
76 psd = reference_psd.
movingmedian(psd, options.median_window)
77 psd = reference_psd.polyfit(psd, minsample, maxsample, options.poly_order, True)
80 reference_psd.
write_psd(options.output, psds)