#!/usr/bin/env python
#
# Copyright (C) 2009-2011  Kipp Cannon, Chad Hanna, Drew Keppel
#
# 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.

## @file
# may be deprecated soon; do not use at this time

#
# =============================================================================
#
#                                   Preamble
#
# =============================================================================
#


import sys
from optparse import OptionParser


from glue import segments
from glue.ligolw import ligolw
from glue.ligolw import lsctables
from glue.ligolw import array
from glue.ligolw import param
array.use_in(ligolw.LIGOLWContentHandler)
param.use_in(ligolw.LIGOLWContentHandler)
lsctables.use_in(ligolw.LIGOLWContentHandler)
from glue.ligolw import utils
from glue.ligolw.utils import process as ligolw_process
from glue.ligolw.utils import search_summary as ligolw_search_summary


from gstlal import far
from gstlal.inspiral import gen_likelihood_control_doc

## @file gstlal_inspiral_reset_likelihood
# This program resets the trials table and segments from files containing the distribution statistics for gstlal_inspiral jobs; see gstlal_inspiral_reset_likelihood for help and usage

## @package gstlal_inspiral_reset_likelihood
# 


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


def parse_command_line():
	parser = OptionParser()
	parser.add_option("--marginalized-likelihood-file", metavar = "filename", help = "Set the name of the xml file containing the marginalized likelihood")
	parser.add_option("--verbose", action = "store_true", help = "Be verbose.")
	options, urls = parser.parse_args()
	return options, urls


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


#
# parse command line
#


options, urls = parse_command_line()


#
# loop over input documents
#


for url in urls:
	#
	# load input document
	#

	in_xmldoc = utils.load_url(url, verbose = options.verbose, contenthandler = ligolw.LIGOLWContentHandler)
	likelihood_data = far.LocalRankingData.from_xml(in_xmldoc)
	search_summary_row, = (row for row in lsctables.table.get_table(in_xmldoc, lsctables.SearchSummaryTable.tableName) if row.process_id == likelihood_data.distributions.process_id)
	ifos = search_summary_row.instruments
	# reset the clock to None
	likelihood_data.livetime_seg = segments.segment(None,None)
	# reset the trials table to 0
	for k in likelihood_data.trials_table:
		likelihood_data.trials_table[k].count = 0
		likelihood_data.trials_table[k].count_below_thresh = 0

	xmldoc = gen_likelihood_control_doc(likelihood_data, ifos)

	utils.write_filename(xmldoc, url, gz = url.endswith(".gz"), verbose = options.verbose)

# Reset the marginalized likelihood file if it exists too
if options.marginalized_likelihood_file is not None:
	
	marg, procid = far.RankingData.from_xml(utils.load_filename(options.marginalized_likelihood_file, contenthandler = ligolw.LIGOLWContentHandler, verbose = options.verbose))
	
	for k in marg.trials_table:
		marg.trials_table[k].count = 0
		marg.trials_table[k].count_below_thresh = 0
	
	marg.livetime_seg = segments.segment(None,None)

	xmldoc = ligolw.Document()
	node = xmldoc.appendChild(ligolw.LIGO_LW())
	node.appendChild(lsctables.New(lsctables.ProcessTable))
	node.appendChild(lsctables.New(lsctables.ProcessParamsTable))
	node.appendChild(lsctables.New(lsctables.SearchSummaryTable))
	process = ligolw_process.register_to_xmldoc(xmldoc, u"gstlal_inspiral_reset_likelihood", options.__dict__)
	search_summary = ligolw_search_summary.append_search_summary(xmldoc, process)
	search_summary.out_segment = marg.livetime_seg
	xmldoc.childNodes[-1].appendChild(marg.to_xml(process, search_summary))
	ligolw_process.set_process_end_time(process)
	outname = options.marginalized_likelihood_file
	utils.write_filename(xmldoc, outname, gz = outname.endswith(".gz"), verbose = options.verbose)
