diff --git a/gstlal-inspiral/bin/Makefile.am b/gstlal-inspiral/bin/Makefile.am index c3bf68535a0103f75da856691fd5177b9778224e..d29a3bf7b3fa7336fee5b7ddbe9ae1c37d4469e8 100644 --- a/gstlal-inspiral/bin/Makefile.am +++ b/gstlal-inspiral/bin/Makefile.am @@ -38,6 +38,7 @@ dist_bin_SCRIPTS = \ gstlal_inspiral_plot_kernels \ gstlal_inspiral_plot_psd_horizon \ gstlal_inspiral_plot_sensitivity \ + gstlal_inspiral_plot_snr \ gstlal_inspiral_plotsummary \ gstlal_inspiral_plot_svd_bank \ gstlal_inspiral_rate_posterior \ diff --git a/gstlal-inspiral/bin/gstlal_inspiral_plot_snr b/gstlal-inspiral/bin/gstlal_inspiral_plot_snr new file mode 100755 index 0000000000000000000000000000000000000000..a41d0aef7af20fbe12c1af3c0451376f17ad1732 --- /dev/null +++ b/gstlal-inspiral/bin/gstlal_inspiral_plot_snr @@ -0,0 +1,33 @@ +#!/usr/bin/env python +""" +Plotter for gstlal_inspiral_calc_snr +""" +import sys +from optparse import OptionParser + +from gstlal import plotsnr + +def parse_command_line(): + parser = OptionParser(description = __doc__) + + parser.add_option("-o", "--output", metavar = "filename", help = "The output filename for the SNR plot (require).") + parser.add_option("-i", "--input", metavar = "filename", help = "A LIGO light-weight XML file containing SNR time series data (require).") + parser.add_option("--center", metavar = "gpsSeconds", type = "float", help = "Center the plot to --center (optional).") + parser.add_option("--span", metavar = "seconds", type = "float", help = "The time span around --center (optional).") + parser.add_option("-v", "--verbose", action = "store_true", help = "Be verbose.") + + options, args = parser.parse_args() + + missing_required_options = [] + if options.input is None: + missing_required_options.append("--input") + if options.output is None: + missing_required_options.append("--output") + if missing_required_options: + raise ValueError("Missing required option(s) %s" % ", ".join(missing_required_options)) + + return options + +options = parse_command_line() + +plotsnr.plot_snr(options.input, output = options.output, center = options.center, span = options.span, verbose = options.verbose) diff --git a/gstlal-inspiral/python/Makefile.am b/gstlal-inspiral/python/Makefile.am index 78e791c898ca92301b32284e969e5d2a784dffba..932f339434d21dc0135d068065d44885debd51ec 100644 --- a/gstlal-inspiral/python/Makefile.am +++ b/gstlal-inspiral/python/Makefile.am @@ -26,6 +26,7 @@ pkgpython_PYTHON = \ plotfar.py \ plotsegments.py \ plotsensitivity.py \ + plotsnr.py \ rate_estimation.py \ snglinspiraltable.py \ spawaveform.py \ diff --git a/gstlal-inspiral/python/plotsnr.py b/gstlal-inspiral/python/plotsnr.py new file mode 100644 index 0000000000000000000000000000000000000000..36c3ca2c953efd01f938582ad38676bf7fc14f54 --- /dev/null +++ b/gstlal-inspiral/python/plotsnr.py @@ -0,0 +1,59 @@ +""" +Plotting SNR from LIGO light-weight XML file +""" +import sys +import numpy + +import matplotlib +matplotlib.use("Agg") +from matplotlib import pyplot + +from gstlal import svd_bank_snr + +def plot_snr(filename, output = None, center = None, span = None, verbose = False): + xmldoc = svd_bank_snr.read_url(filename, contenthandler = svd_bank_snr.SNRContentHandler, verbose = verbose) + SNR_dict = svd_bank_snr.read_xmldoc(xmldoc) + start = None + mid = 0 + end = None + + if verbose: + sys.stderr.write("Ploting SNR ...\n") + + for instrument, SNR in SNR_dict.items(): + if verbose and center and span: + sys.stderr.write("Locating SNR at GPSTime: %f spanning %f s\n" % (center, span)) + if center and span: + start = find_nearest(SNR, center - span)[1] + mid = find_nearest(SNR, center)[1] + end = find_nearest(SNR, center + span)[1] + + gps_time = (SNR.epoch.gpsSeconds + SNR.epoch.gpsNanoSeconds * 10.**-9 + (numpy.arange(SNR.data.length) * SNR.deltaT)) + relative_gps_time = (gps_time - gps_time[mid])[start:end] + data = SNR.data.data[start:end] + + fig, ax = pyplot.subplots(nrows = 1, ncols = 1, figsize = [15,6]) + ax.plot(relative_gps_time, data) + ax.set_xlabel("GPS Time Since %f" % gps_time[mid]) + ax.set_ylabel("SNR") + + pyplot.tight_layout() + if output is None: + output = "SNR_%s_since_%d.svg" %(SNR.name, gps_time[mid]) + if verbose: + sys.stderr.write("%s --> Done\n" % output) + fig.savefig(output) + else: + if verbose: + sys.stderr.write("%s --> Done\n" % output) + fig.savefig(output) + pyplot.close() + +def find_nearest(snr_series, time): + gps_start = snr_series.epoch.gpsSeconds + snr_series.epoch.gpsNanoSeconds * 10.**-9 + gps = gps_start + numpy.arange(snr_series.data.length) * snr_series.deltaT + if time - gps[0] > 0 and time - gps[-1] <0: + index = abs(gps - time).argmin() + else: + raise ValueError("Invalid choice of center time %f." % time) + return (snr_series.data.data[index], index)