#!/usr/bin/env python3
#
# Copyright (C) 2017 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
#
# =============================================================================
#


import math
import matplotlib
matplotlib.rcParams.update({
	"font.size": 10.0,
	"axes.titlesize": 10.0,
	"axes.labelsize": 10.0,
	"xtick.labelsize": 8.0,
	"ytick.labelsize": 8.0,
	"legend.fontsize": 8.0,
	"figure.dpi": 600,
	"savefig.dpi": 600,
	"text.usetex": True
})
import numpy
from optparse import OptionParser
import sys


from ligo import segments
from gstlal import far
from gstlal.plots import far as plotfar


#
# =============================================================================
#
#                                 Command Line
#
# =============================================================================
#


def parse_command_line():
	parser = OptionParser()
	parser.add_option("--max-snr", metavar = "snr", type = "float", default = 200.0, help = "Set the maximum SNR for (chi^2, snr) plots (default = 200.0).")
	parser.add_option("--output-format", metavar = "extension", default = ".png", help = "Select output format by choosen the filename extension (default = \".png\").")
	parser.add_option("-v", "--verbose", action = "store_true", help = "Be verbose.")
	options, filenames = parser.parse_args()

	if options.output_format not in (".png", ".pdf", ".svg"):
		raise ValueError("invalid --output-format \"%s\"" % options.output_format)

	return options, filenames


#
# =============================================================================
#
#                                     Main
#
# =============================================================================
#


#
# command line
#


options, filenames = parse_command_line()


#
# load input
#


rankingstat = far.RankingStat(instruments = frozenset(("H1",)), min_instruments = 1)


#
# place a grid of impulses into the arrays and smooth
#


for x in 1.5**numpy.arange(4, 13):
	for y in 3.**numpy.arange(-8, 0):
		rankingstat.background_rates["H1_snr_chi"][x, y] = 1e8
rankingstat.finish()


#
# plots
#


if rankingstat is not None:
	fig = plotfar.plot_snr_chi_pdf(rankingstat, "H1", "background_pdf", options.max_snr)
	plotname = "coinc_params_binning%s" % options.output_format
	if options.verbose:
		print >>sys.stderr, "writing %s" % plotname
	fig.savefig(plotname)


# ranking statistic PDFs and CCDFs
#fig = plotfar.plot_likelihood_ratio_pdf(rankingstatpdf, (options.min_log_lambda, options.max_log_lambda), "Noise", which = "noise")
#plotname = "coinc_params_binning%s" % options.output_format
#if options.verbose:
#	print >>sys.stderr, "writing %s" % plotname
#fig.savefig(plotname)
