3 Plotter for gstlal_inspiral_calc_snr
5 from optparse
import OptionParser
10 from gstlal
import svd_bank_snr
11 from gstlal.plots
import snr as plotsnr
13 from lal.utils
import CacheEntry
15 def parse_command_line():
16 parser = OptionParser(description = __doc__)
18 parser.add_option(
"-o",
"--outdir", metavar =
"directory", help =
"The output directory for the SNR plot (require).")
19 parser.add_option(
"-i",
"--input", metavar =
"cache", help =
"The input cache containing SNR urls (require).")
20 parser.add_option(
"-f",
"--format", metavar =
"file_type", help =
"The format of the output plot (default = .png).")
21 parser.add_option(
"--center", metavar =
"gpsSeconds", type =
"float", help =
"Center the plot to --center (optional).")
22 parser.add_option(
"--width", metavar =
"inch", default = 8, type =
"int", help =
"The output width of the figure.")
23 parser.add_option(
"--span", metavar =
"seconds", type =
"float", help =
"The time span around --center (optional).")
24 parser.add_option(
"-v",
"--verbose", action =
"store_true", help =
"Be verbose.")
26 options, args = parser.parse_args()
28 if options.input is None:
29 raise ValueError(
"Missing --input.")
30 if options.outdir is None:
31 raise ValueError(
"Missing --outdir.")
32 if options.center is not None and options.span is None:
33 raise ValueError(
"--span should also be provided if --center is provided.")
34 if options.span is not None and options.center is None:
35 raise ValueError(
"--center should also be provided if --span is provided.")
39 options = parse_command_line()
41 suffix =
".png" if (options.format is None) else options.format
45 with open(options.input) as cache:
46 for line in cache.readlines():
47 entry = CacheEntry(line)
48 urls.append(entry.url)
49 if description is None and segment is None:
50 description = entry.description
51 segment =
"-" + str(entry.segment[0]) +
"-" + str(entry.segment[1] - entry.segment[0])
53 assert description + segment == entry.description +
"-" + str(entry.segment[0]) +
"-" + str(entry.segment[1] - entry.segment[0]),
"Cannot plot snrs with different segments or for different template."
55 # done checking file consistency, overwrite output name with customized segment
56 if options.center and options.span:
57 segment =
"-" + str(int(options.center - options.span)) +
"-" + str(int(options.span * 2))
60 autocorrelations_dict = {}
62 snr_dict, ac_dict = svd_bank_snr.read_xmldoc(svd_bank_snr.read_url(url, svd_bank_snr.SNRContentHandler, verbose = options.verbose))
63 SNRs_dict.update(snr_dict)
64 autocorrelations_dict.update(ac_dict)
65 snrs_groups = zip(*SNRs_dict.values())
66 acs_groups = zip(*autocorrelations_dict.values())
68 #=============================================================================================
72 #=============================================================================================
73 for snrs_group in snrs_groups:
74 figure = plotsnr.plot_snr(dict(zip(SNRs_dict.keys(), zip(snrs_group))), width = options.width, center = options.center, span = options.span, verbose = options.verbose)
75 if len(zip(*SNRs_dict.values())) == 1:
76 figure.savefig(os.path.join(options.outdir,
"%s-" %
"".join(sorted(SNRs_dict.keys())) + description + segment + suffix))
78 figure.savefig(os.path.join(options.outdir,
"%s-" %
"".join(sorted(SNRs_dict.keys())) + description.replace(
"ALL_ALL",
"_".join(snrs_group[0].name.split(
"_")[1:3])) + segment + suffix))
81 if len(snrs_groups) != len(acs_groups):
82 raise ValueError(
"The number of SNR time series and template autocorrelations does not matched.")
84 #=============================================================================================
86 # Plot SNRs with autocorrelation
88 #=============================================================================================
89 # This is only useful when SNRs are complex time series.
90 if numpy.iscomplexobj(SNRs_dict.values()[0][0].data.data):
92 for snrs_group, acs_group in zip(snrs_groups, acs_groups):
93 figure = plotsnr.
plot_snr_with_ac(dict(zip(SNRs_dict.keys(), zip(snrs_group))), dict(zip(autocorrelations_dict.keys(), zip(acs_group))), width = options.width, ref_trigger_time = options.center, verbose = options.verbose)
95 all_instruments = set(SNRs_dict) & set(autocorrelations_dict)
96 # just pick one, they must be the same length
97 if len(zip(*SNRs_dict.values())) == 1:
98 figure.savefig(os.path.join(options.outdir,
"%s-" %
"".join(sorted(all_instruments)) + description +
"_with_autocorrlation" + segment+ suffix))
100 figure.savefig(os.path.join(options.outdir,
"%s-" %
"".join(sorted(all_instruments)) + description.replace(
"ALL_ALL",
"_".join(snrs_group[0].name.split(
"_")[1:3])) +
"_with_autocorrlation" + segment + suffix))