#!python
# Copyright (C) 2015 Alex Nitz
#
# 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 3 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.

"""Merge hdf psd files"""

import argparse
import logging

import numpy

import pycbc.types
from pycbc.io import HFile

logger = logging.getLogger(__name__)

parser = argparse.ArgumentParser(description=__doc__)
pycbc.add_common_pycbc_options(parser)
parser.add_argument("--psd-files", nargs="+")
parser.add_argument("--output-file", required=True)

args = parser.parse_args()
pycbc.init_logging(args.verbose)

outf = HFile(args.output_file, "w")
inc = {}
start, end = {}, {}
for psd_file in args.psd_files:
    f = HFile(psd_file, "r")
    ifo = next(iter(f.keys()))
    if ifo not in inc:
        inc[ifo] = 0
        start[ifo], end[ifo] = [], []

    for rkey in f[f"{ifo}/psds"]:
        int_key = int(rkey)
        rkey = f"{ifo}/psds/{rkey}"
        psd = pycbc.types.load_frequencyseries(psd_file, group=rkey)

        key = ifo + "/psds/" + str(inc[ifo])
        outf.create_dataset(
            key, data=psd, compression="gzip", compression_opts=9, shuffle=True
        )

        s = f[f"{ifo}/start_time"][int_key]
        e = f[f"{ifo}/end_time"][int_key]

        outf[key].attrs["epoch"] = int(psd.epoch)
        outf[key].attrs["delta_f"] = float(psd.delta_f)
        start[ifo].append(s)
        end[ifo].append(e)

        inc[ifo] += 1

for ifo in start:
    outf[ifo + "/start_time"] = numpy.array(start[ifo], dtype=numpy.uint32)
    outf[ifo + "/end_time"] = numpy.array(end[ifo], dtype=numpy.uint32)

outf.attrs["low_frequency_cutoff"] = f.attrs["low_frequency_cutoff"]
outf.attrs["dynamic_range_factor"] = pycbc.DYN_RANGE_FAC
logger.info("Done!")
