#! /usr/bin/env python
#
# Copyright (C) 2011 Chad Hanna
#
# 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.

import os
import sys
import numpy
import copy
from optparse import OptionParser
from pylal import spawaveform
from glue.ligolw import ligolw
from glue.ligolw import lsctables
from glue.ligolw import utils
from glue.ligolw.utils import process as ligolw_process

def parse_command_line():
	parser = OptionParser()
	parser.add_option("--output", metavar = "file", default = ".", help = "Set the name of the output file")
	parser.add_option("--input", metavar = "file", default = ".", help = "Set the name of the input file")
	parser.add_option("--chi", action="append", help="values of chi to populate, give multiple times")
	parser.add_option("--approximant", help = "override approximant with this value")
	parser.add_option("-v", "--verbose", action = "store_true", help = "Be verbose.")
	options, filenames = parser.parse_args()

	return options, filenames

options, filenames = parse_command_line()

xmldoc=utils.load_filename(options.input, verbose = options.verbose)
sngl_inspiral_table=lsctables.table.get_table(xmldoc, lsctables.SnglInspiralTable.tableName)
process_params_table = lsctables.table.get_table(xmldoc, lsctables.ProcessParamsTable.tableName)
tmpltbank_process_ids = lsctables.table.get_table(xmldoc, lsctables.ProcessTable.tableName).get_ids_by_program("tmpltbank")
procrow = ligolw_process.register_to_xmldoc(xmldoc, "gstlal_add_spins_to_bank", options.__dict__)

if options.approximant is not None:
	for row in process_params_table:
		if row.process_id in tmpltbank_process_ids and row.param=='--approximant':
			row.value= options.approximant
			row.process_id == procrow.process_id
	
new_rows = []
for row in sngl_inspiral_table:
	for chi in options.chi:
		#FIXME populate the ending frequency
		row.chi=float(chi)
		new_rows.append(copy.deepcopy(row))

sngl_inspiral_table[:] = new_rows

utils.write_filename(xmldoc, options.output, verbose = options.verbose)
