#! /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.

## @file
# A legacy program to make spin aligned banks from an existing non spinning bank; DO NOT USE; instead consider lalapp_cbc_sbank or pycbc_geom_aligned_bank


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("--spin1z", action="append", help="values of spin1z to populate, give multiple times: convention body 1 is the lighter body")
	parser.add_option("--spin2z", action="append", help="values of spin2z to populate, give multiple times: convention body 1 is the lighter body")
	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 spin1z in options.spin1z:
		for spin2z in options.spin2z:
			#FIXME populate the ending frequency
			# swap masses to obey mass 1 is the lighter body 
			if row.mass1 > row.mass2:
				row.mass1, row.mass2 = row.mass2, row.mass1
			row.spin1z=float(spin1z)
			row.spin2z=float(spin2z)
			new_rows.append(copy.deepcopy(row))

sngl_inspiral_table[:] = new_rows

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