https://github.com/gwastro/pycbc
Raw File
Tip revision: 76e049255ae10e242fb205375513a8e4ab38af55 authored by Alex Nitz on 23 February 2018, 09:30:48 UTC
Set version for 1.9.2 release (#2083)
Tip revision: 76e0492
pycbc_merge_psds
#!/usr/bin/env 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 logging, argparse, numpy, h5py, pycbc.types
from pycbc.version import git_verbose_msg as version

parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--version', action='version', version=version)
parser.add_argument('--verbose', action="store_true")
parser.add_argument('--psd-files', nargs='+')
parser.add_argument("--output-file", required=True)

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

outf = h5py.File(args.output_file, 'w')
inc = {}
start, end = {}, {}
for psd_file in args.psd_files:
    f = h5py.File(psd_file, 'r')
    ifo = 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!')
back to top