From 8dc84c8ade45b5d72a68ff4d5d9ede59907f95e1 Mon Sep 17 00:00:00 2001
From: Kipp Cannon <kipp.cannon@ligo.org>
Date: Thu, 19 Jul 2018 03:29:43 +0900
Subject: [PATCH] add gstlal_asd_txt_from_psd_xml

---
 gstlal/bin/Makefile.am                 |  1 +
 gstlal/bin/gstlal_asd_txt_from_psd_xml | 90 ++++++++++++++++++++++++++
 2 files changed, 91 insertions(+)
 create mode 100755 gstlal/bin/gstlal_asd_txt_from_psd_xml

diff --git a/gstlal/bin/Makefile.am b/gstlal/bin/Makefile.am
index b5c0eb7300..98dfe82ea4 100644
--- a/gstlal/bin/Makefile.am
+++ b/gstlal/bin/Makefile.am
@@ -1,4 +1,5 @@
 dist_bin_SCRIPTS = \
+	gstlal_asd_txt_from_psd_xml \
 	gstlal_fake_frames \
 	gstlal_fake_frames_pipe \
 	gstlal_launch \
diff --git a/gstlal/bin/gstlal_asd_txt_from_psd_xml b/gstlal/bin/gstlal_asd_txt_from_psd_xml
new file mode 100755
index 0000000000..eec1df3470
--- /dev/null
+++ b/gstlal/bin/gstlal_asd_txt_from_psd_xml
@@ -0,0 +1,90 @@
+#!/usr/bin/env python
+#
+# Copyright (C) 2018  Kipp Cannon
+#
+# This program is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by the
+# Free Software Foundation; either version 2 of the License, or (at your
+# option) any later version.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
+# Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+
+
+#
+# =============================================================================
+#
+#                                   Preamble
+#
+# =============================================================================
+#
+
+
+from optparse import OptionParser
+import sys
+
+
+from glue.ligolw import utils as ligolw_utils
+from lal import series as lalseries
+
+
+#
+# =============================================================================
+#
+#                                 Command Line
+#
+# =============================================================================
+#
+
+
+def parse_command_line():
+	parser = OptionParser(
+		description = ""
+	)
+	parser.add_option("--prefix", metavar = "string", default = "psd_", help = "Prepend output files with this string (default = \"psd_\").")
+	parser.add_option("-v", "--verbose", action = "store_true", help = "Be verbose (optional).")
+
+	options, filenames = parser.parse_args()
+
+	if len(filenames) != 1:
+		raise ValueError("only file PSD file may be given")
+
+	return options, filenames
+
+
+#
+# =============================================================================
+#
+#                                     Main
+#
+# =============================================================================
+#
+
+
+#
+# parse command line
+#
+
+
+options, filenames = parse_command_line()
+
+
+#
+# load the PSD file iterate over instruments, writing ASD to ascii file
+#
+
+
+for filename in filenames:
+	for instrument, psd in lalseries.read_psd_xmldoc(ligolw_utils.load_filename(filename, verbose = options.verbose, contenthandler = lalseries.PSDContentHandler)).items():
+		filename = "%s%s.txt" % (options.prefix, instrument)
+		with open(filename, "w") as f:
+			if options.verbose:
+				print >>sys.stderr, "writing \"%s\" ..." % filename
+			for i, x in enumerate(psd.data.data):
+				print >>f, "%.16g %.16g" % (psd.f0 + i * psd.deltaF, x**.5)
-- 
GitLab