#!/usr/bin/python3.13 -s
# 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 logging, argparse, numpy, pycbc.types
from pycbc.io import HFile

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 = tuple(f.keys())[0]
    if ifo not in inc:
        inc[ifo] = 0
        start[ifo], end[ifo] = [], []
    
    for rkey in f['%s/psds' % ifo].keys():
        int_key = int(rkey)
        rkey = '%s/psds/%s' % (ifo, 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['%s/start_time' % ifo][int_key]
        e = f['%s/end_time' % ifo][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
logging.info('Done!')
