gstlal  1.4.1
gstlal_asd_txt_from_psd_xml
1 #!/usr/bin/env python
2 #
3 # Copyright (C) 2018 Kipp Cannon
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 
20 #
21 # =============================================================================
22 #
23 # Preamble
24 #
25 # =============================================================================
26 #
27 
28 
29 from optparse import OptionParser
30 import sys
31 
32 
33 from glue.ligolw import utils as ligolw_utils
34 from lal import series as lalseries
35 
36 
37 #
38 # =============================================================================
39 #
40 # Command Line
41 #
42 # =============================================================================
43 #
44 
45 
46 def parse_command_line():
47  parser = OptionParser(
48  description = ""
49  )
50  parser.add_option("--prefix", metavar = "string", default = "psd_", help = "Prepend output files with this string (default = \"psd_\").")
51  parser.add_option("-v", "--verbose", action = "store_true", help = "Be verbose (optional).")
52 
53  options, filenames = parser.parse_args()
54 
55  if len(filenames) != 1:
56  raise ValueError("only file PSD file may be given")
57 
58  return options, filenames
59 
60 
61 #
62 # =============================================================================
63 #
64 # Main
65 #
66 # =============================================================================
67 #
68 
69 
70 #
71 # parse command line
72 #
73 
74 
75 options, filenames = parse_command_line()
76 
77 
78 #
79 # load the PSD file iterate over instruments, writing ASD to ascii file
80 #
81 
82 
83 for filename in filenames:
84  for instrument, psd in lalseries.read_psd_xmldoc(ligolw_utils.load_filename(filename, verbose = options.verbose, contenthandler = lalseries.PSDContentHandler)).items():
85  filename = "%s%s.txt" % (options.prefix, instrument)
86  with open(filename, "w") as f:
87  if options.verbose:
88  print >>sys.stderr, "writing \"%s\" ..." % filename
89  for i, x in enumerate(psd.data.data):
90  print >>f, "%.16g %.16g" % (psd.f0 + i * psd.deltaF, x**.5)