#!/usr/bin/env python3
#
# Copyright (C) 2018 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 sys
from gstlal.stats import inspiral_extrinsics
import argparse

parser = argparse.ArgumentParser(description = "create dt dphi snr pdfs")
parser.add_argument("--cov-mat-h5", help = "hdf5 file that contains the covariance matrix")
parser.add_argument("--start", metavar='N', type=int, help='The first index to marginalize')
parser.add_argument("--stop", metavar='N', type=int, help='The last index to marginalize')
parser.add_argument("--output", metavar='name', type=str, help='The output file name', default = "inspiral_dtdphi_pdf.h5")
options = parser.parse_args()

if options.start is not None and options.stop is not None:
	if options.start % inspiral_extrinsics.TimePhaseSNR.numchunks or options.stop % inspiral_extrinsics.TimePhaseSNR.numchunks:
		raise ValueError("start and stop must be multiple of %d" % TimePhaseSNR.numchunks)
	IE = inspiral_extrinsics.TimePhaseSNR.from_hdf5(fname = options.cov_mat_h5, verbose = True, margstart = options.start, margstop = options.stop)
else:
	IE = inspiral_extrinsics.TimePhaseSNR.from_hdf5(fname = options.cov_mat_h5, verbose = True)

IE.to_hdf5(options.output)
