#!/usr/bin/env python3
#
# Copyright (C) 2020 Leo Tsukada
#
# 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.
"""Reproduce the psd frequnecy arraies used for whitening a template bank from the given svd bank files and dump it to a xml file."""

### Usage
### -----
###
###	$ gstlal_svd_bank_calc_psd --write-psd ./reproduce_svd_bank_psd.xml.gz \\
###	--svd-bank-files H1-GSTLAL_SVD_BANK.xml.gz \\
###	--svd-bank-files L1-GSTLAL_SVD_BANK.xml.gz \\
###	--svd-bank-files V1-GSTLAL_SVD_BANK.xml.gz \\
###	--verbose
###
### Related programs
### ----------------
###
### - :any:`gstlal_svd_bank`
###  

#
# parse command line
#


from optparse import OptionParser

from ligo.lw import ligolw
from ligo.lw import array as ligolw_array
from ligo.lw import param as ligolw_param
from ligo.lw import lsctables
from ligo.lw import utils as ligolw_utils

from lal import series as lalseries

from gstlal import svd_bank

@ligolw_array.use_in
@ligolw_param.use_in
@lsctables.use_in
class LIGOLWContentHandler(ligolw.LIGOLWContentHandler):
	pass

def parse_command_line():
	parser = OptionParser(description = __doc__)

	# add our own options
	parser.add_option("--write-psd", metavar = "filename", help = "Write measured noise spectrum to this LIGO light-weight XML file (required).")
	parser.add_option("--svd-bank-files", action="append", metavar = "filename", default=[], help = "Set svd bank files from which to make psd xml file. One can set this multiple times, e.g. --svd-bank-files H1-SVD-BANK...xml.gz --svd-bank-files L1-SVD-BANK...xml.gz --svd-bank-files V1-SVD-BANK...xml.gz.")
	parser.add_option("-v", "--verbose", action = "store_true", help = "Be verbose (optional).")

	options, filenames = parser.parse_args()

	# check our own options
	if options.write_psd is None:
		raise ValueError("Must specify --write-psd")

	return options, filenames


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


options, filenames = parse_command_line()

psds = {}


for svd_bank_path in options.svd_bank_files:
	bank = svd_bank.read_banks(svd_bank_path, contenthandler = LIGOLWContentHandler, verbose = options.verbose)[0]
	psds.update({bank.sngl_inspiral_table[0].ifo: bank.processed_psd})

# Write to file
ligolw_utils.write_filename(lalseries.make_psd_xmldoc(psds), options.write_psd, verbose = options.verbose)
