gstlal  0.8.1
 All Classes Namespaces Files Functions Variables Pages
gstlal_plot_psd
1 #!/usr/bin/env python
2 #
3 # Copyright (C) 2012 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 sys
20 import matplotlib
21 matplotlib.use('Agg')
22 from matplotlib import pyplot
23 import numpy
24 from glue.ligolw import ligolw
25 from glue.ligolw import array
26 from glue.ligolw import param
27 array.use_in(ligolw.LIGOLWContentHandler)
28 param.use_in(ligolw.LIGOLWContentHandler)
29 from glue.ligolw import utils
30 from pylal import series as lalseries
31 from gstlal import reference_psd
32 
33 ## @file
34 # A program to plot reference psds; see gstlal_plot_psd for more info
35 #
36 # A program to plot a psd such as one generated by gstlal_reference_psd
37 #
38 # ### Usage:
39 #
40 # gstlal_plot_psd OUTPUT-NAME PSD-FILE-1 PSD-FILE-2
41 #
42 # e.g.,
43 #
44 # gstlal_plot_psd psd.png psd.xml.gz
45 #
46 
47 if len(sys.argv) < 3:
48  print "USAGE gstlal_plot_psd OUTPUT-NAME PSD-FILE-1 PSD-FILE-2 ..."
49  sys.exit()
50 
51 outname = sys.argv[1]
52 
53 minpsd = []
54 
55 for fname in sys.argv[2:]:
56  psds = lalseries.read_psd_xmldoc(utils.load_filename(fname, verbose = True, contenthandler = ligolw.LIGOLWContentHandler))
57  for k,v in psds.items():
58  # compute horizon up to 90% of nyquist to avoid roll off
59  h = reference_psd.horizon_distance(v, 1.4, 1.4, 8, 10, len(v.data) * v.deltaF * 0.90)
60  if len(sys.argv[2:]) > 1:
61  flabel = fname
62  else:
63  flabel = ""
64  minpsd.append(min(v.data))
65  pyplot.loglog(v.deltaF * numpy.arange(len(v.data)), v.data, label = '%s %s: %0.f Mpc' % (flabel, k, h), alpha=0.75)
66  pyplot.xlabel('Frequency (Hz)')
67  pyplot.ylabel('Strain^2/Hz')
68 pyplot.legend()
69 pyplot.grid()
70 pyplot.ylim([min(minpsd), min(minpsd)*1e10])
71 pyplot.savefig(outname)