#!/usr/bin/env python

# Copyright (C) 2014 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.

import argparse
import copy
import logging
import os
import sys
import time

import numpy

import pycbc
import pycbc.inject
import pycbc.opt
from pycbc import DYN_RANGE_FAC, events, fft, psd, scheme, strain, vetoes, waveform
from pycbc.filter import MatchedFilterControl, qtransform
from pycbc.pool import BroadcastPool as Pool
from pycbc.types import complex64, float32, zeros
from pycbc.vetoes.sgchisq import SingleDetSGChisq

logger = logging.getLogger(__name__)

last_progress_update = -1.0


def update_progress(p, u, n):
    """Updates a file 'progress.txt' with a value 0 .. 1.0 when enough
    (filtering) progress was made"""
    global last_progress_update
    if p > last_progress_update + u:
        with open(n, "w") as f:
            f.write(f"{p:.4f}")
        last_progress_update = p


tstart = time.time()

parser = argparse.ArgumentParser(
    usage="", description="Find single detector gravitational-wave triggers."
)

pycbc.add_common_pycbc_options(parser)
parser.add_argument(
    "--update-progress",
    help="updates a file 'progress.txt' with a value 0 .. "
    "1.0 when this amount of (filtering) progress was made",
    type=float,
    default=0,
)
parser.add_argument(
    "--update-progress-file",
    help="name of the file to write the amount of (filtering) progress to",
    type=str,
    default="progress.txt",
)
parser.add_argument("--output", type=str, help="FIXME: ADD")
parser.add_argument("--bank-file", type=str, help="FIXME: ADD")
parser.add_argument(
    "--snr-threshold", help="SNR threshold for trigger generation", type=float
)
parser.add_argument(
    "--newsnr-threshold",
    type=float,
    metavar="THRESHOLD",
    help="Cut triggers with NewSNR less than THRESHOLD",
)
parser.add_argument(
    "--low-frequency-cutoff",
    type=float,
    help="The low frequency cutoff to use for filtering (Hz)",
)
parser.add_argument(
    "--enable-bank-start-frequency",
    action="store_true",
    help="Read the starting frequency of template waveforms from the template bank.",
)
parser.add_argument(
    "--max-template-length",
    type=float,
    help="The maximum length of a template is seconds. The "
    "starting frequency of the template is modified to "
    "ensure the proper length",
)
parser.add_argument(
    "--enable-q-transform",
    action="store_true",
    help="compute the q-transform for each segment of a "
    "given analysis run. (default = False)",
)
# add approximant arg
pycbc.waveform.bank.add_approximant_arg(parser)
parser.add_argument(
    "--order",
    type=int,
    help="The integer half-PN order at which to generate"
    " the approximant. Default is -1 which indicates to use"
    " approximant defined default.",
    default=-1,
    choices=numpy.arange(-1, 9, 1),
)
taper_choices = ["start", "end", "startend"]
parser.add_argument(
    "--taper-template",
    choices=taper_choices,
    help="For time-domain approximants, taper the start and/or"
    " end of the waveform before FFTing.",
)
parser.add_argument(
    "--cluster-function",
    choices=["findchirp", "symmetric"],
    help="How to cluster together triggers within a window. "
    "'findchirp' uses a forward sliding window; 'symmetric' "
    "will compare each window to the one before and after, keeping "
    "only a local maximum.",
    default="findchirp",
)
parser.add_argument(
    "--cluster-window",
    type=float,
    default=0,
    help="Length of clustering window in seconds. Set to 0 to disable clustering.",
)
parser.add_argument("--chisq-threshold", type=float, default=0, help="FIXME: ADD")
parser.add_argument("--chisq-delta", type=float, default=0, help="FIXME: ADD")
parser.add_argument(
    "--downsample-factor",
    type=int,
    help="Factor that determines the interval between the "
    "initial SNR sampling. If not set (or 1) no sparse sample "
    "is created, and the standard full SNR is calculated.",
    default=1,
)
parser.add_argument(
    "--upsample-threshold",
    type=float,
    help="The fraction of the SNR threshold to check the sparse SNR sample.",
)
parser.add_argument(
    "--upsample-method",
    choices=["pruned_fft"],
    help="The method to find the SNR points between the sparse SNR sample.",
    default="pruned_fft",
)
parser.add_argument(
    "--user-tag",
    type=str,
    metavar="TAG",
    help="""
                    This is used to identify FULL_DATA jobs for
                    compatibility with pipedown post-processing.
                    Option will be removed when no longer needed.""",
)
parser.add_argument(
    "--keep-loudest-log-chirp-window",
    type=float,
    help="Keep loudest triggers within ln chirp mass window",
)
parser.add_argument(
    "--keep-loudest-interval",
    type=float,
    help="Window in seconds to maximize triggers over bank",
)
parser.add_argument(
    "--keep-loudest-num",
    type=int,
    help="Number of triggers to keep from each maximization interval",
)
parser.add_argument(
    "--keep-loudest-stat",
    default="newsnr",
    choices=events.ranking.sngls_ranking_function_dict.keys(),
    help="Statistic used to determine loudest to keep",
)
parser.add_argument(
    "--finalize-events-template-rate",
    default=None,
    type=int,
    metavar="NUM TEMPLATES",
    help="After NUM TEMPLATES perform the various clustering "
    "and rejection tests that would be performed at the "
    "end of this job. Default is to only do those things "
    "at the end of the job. This can help control memory "
    "usage if a lot of triggers that would be rejected "
    "are being retained. A suggested value for this is "
    "512, but a good number may depend on other settings "
    "and your specific use-case.",
)
parser.add_argument("--gpu-callback-method", default="none")
parser.add_argument(
    "--use-compressed-waveforms",
    action="store_true",
    default=False,
    help="Use compressed waveforms from the bank file (if available).",
)
parser.add_argument(
    "--waveform-decompression-method",
    action="store",
    default=None,
    help="Method to be used decompress waveforms from the bank file.",
)
parser.add_argument(
    "--checkpoint-interval",
    type=int,
    help="Save results to checkpoint file every X seconds. "
    "Default is no checkpointing.",
)
parser.add_argument(
    "--require-valid-checkpoint",
    default=False,
    action="store_true",
    help="If the checkpoint file is invalid, raise an error. "
    "Default is to ignore invalid checkpoint files and to "
    "delete the broken file.",
)
parser.add_argument(
    "--checkpoint-exit-maxtime",
    type=int,
    help="Checkpoint and exit if X seconds of execution"
    " time is exceeded. Default is no checkpointing.",
)
parser.add_argument(
    "--checkpoint-exit-code",
    type=int,
    default=77,
    help="Exit code returned if exiting after a checkpoint",
)
parser.add_argument(
    "--multiprocessing-nprocesses",
    type=int,
    help="Parallelize over multiple processes, note this is "
    "separate from threading using the proc. scheme. "
    "Used in conjunction with the option"
    "--finalize-events-template-rate which should be set"
    "to a multiple of the number of processes.",
)

# Add options groups
psd.insert_psd_option_group(parser)
strain.insert_strain_option_group(parser)
strain.StrainSegments.insert_segment_option_group(parser)
scheme.insert_processing_option_group(parser)
fft.insert_fft_option_group(parser)
pycbc.opt.insert_optimization_option_group(parser)
pycbc.inject.insert_injfilterrejector_option_group(parser)
vetoes.SingleDetPowerChisq.insert_option_group(parser)
vetoes.SingleDetBankVeto.insert_option_group(parser)
vetoes.SingleDetAutoChisq.insert_option_group(parser)
SingleDetSGChisq.insert_option_group(parser)
opt = parser.parse_args()

# Check that the values returned for the options make sense
psd.verify_psd_options(opt, parser)
strain.verify_strain_options(opt, parser)
strain.StrainSegments.verify_segment_options(opt, parser)
scheme.verify_processing_options(opt, parser)
fft.verify_fft_options(opt, parser)
pycbc.opt.verify_optimization_options(opt, parser)

pycbc.init_logging(opt.verbose)

fft.from_cli(opt)
inj_filter_rejector = pycbc.inject.InjFilterRejector.from_cli(opt)
ctx = scheme.from_cli(opt)

gwstrain = strain.from_cli(
    opt, dyn_range_fac=DYN_RANGE_FAC, inj_filter_rejector=inj_filter_rejector
)

strain_segments = strain.StrainSegments.from_cli(opt, gwstrain)


def template_triggers(t_num):
    """Get the triggers for a specific template"""
    template = None
    tparam = None
    out_vals_all = []
    for s_num, stilde in enumerate(segments):
        # Filter check checks the 'inj_filter_rejector' options to
        # determine whether
        # to filter this template/segment if injections are present.
        if not inj_filter_rejector.template_segment_checker(bank, t_num, stilde):
            continue
        if template is None:
            template = bank[t_num]
            tparam = template.params

        if opt.update_progress:
            update_progress(
                (t_num + (s_num / float(len(segments)))) / len(bank),
                opt.update_progress,
                opt.update_progress_file,
            )
        logger.info(
            "Filtering template %d/%d segment %d/%d",
            t_num + 1,
            len(bank),
            s_num + 1,
            len(segments),
        )

        sigmasq = template.sigmasq(stilde.psd)
        snr, norm, corr, idx, snrv = matched_filter.matched_filter_and_cluster(
            s_num, sigmasq, cluster_window, epoch=stilde._epoch
        )
        if not len(idx):
            continue

        out_vals = out_vals_ref.copy()
        out_vals["bank_chisq"], out_vals["bank_chisq_dof"] = bank_chisq.values(
            template, stilde.psd, stilde, snrv, norm, idx + stilde.analyze.start
        )

        out_vals["chisq"], out_vals["chisq_dof"] = power_chisq.values(
            corr, snrv, norm, stilde.psd, idx + stilde.analyze.start, template
        )

        out_vals["sg_chisq"] = sg_chisq.values(
            stilde,
            template,
            stilde.psd,
            snrv,
            norm,
            out_vals["chisq"],
            out_vals["chisq_dof"],
            idx + stilde.analyze.start,
        )

        out_vals["cont_chisq"], _ = autochisq.values(
            snr,
            idx + stilde.analyze.start,
            template,
            stilde.psd,
            norm,
            stilde=stilde,
            low_frequency_cutoff=flow,
        )

        idx += stilde.cumulative_index

        out_vals["time_index"] = idx
        out_vals["snr"] = snrv * norm
        out_vals["sigmasq"] = numpy.zeros(len(snrv), dtype=float32) + sigmasq
        if opt.psdvar_short_segment is not None:
            out_vals["psd_var_val"] = pycbc.psd.find_trigger_value(
                psd_var, out_vals["time_index"], opt.gps_start_time, opt.sample_rate
            )

        out_vals_all.append(copy.deepcopy(out_vals))
    return out_vals_all, tparam


with ctx:
    if opt.fft_backends == "fftw":
        # The following FFTW specific options needed to wait until
        # we were inside the scheme context.

        # Import system wisdom.
        if opt.fftw_import_system_wisdom:
            fft.fftw.import_sys_wisdom()

        # Read specified user-provided wisdom files
        if opt.fftw_input_float_wisdom_file is not None:
            fft.fftw.import_single_wisdom_from_filename(
                opt.fftw_input_float_wisdom_file
            )

        if opt.fftw_input_double_wisdom_file is not None:
            fft.fftw.import_double_wisdom_from_filename(
                opt.fftw_input_double_wisdom_file
            )

    flow = opt.low_frequency_cutoff
    flen = strain_segments.freq_len
    tlen = strain_segments.time_len
    delta_f = strain_segments.delta_f

    logger.info("Making frequency-domain data segments")
    segments = strain_segments.fourier_segments()
    psd.associate_psds_to_segments(
        opt,
        segments,
        gwstrain,
        flen,
        delta_f,
        flow,
        dyn_range_factor=DYN_RANGE_FAC,
        precision="single",
    )

    # storage for values and types to be passed to event manager
    out_types = {
        "time_index": int,
        "snr": complex64,
        "chisq": float32,
        "chisq_dof": int,
        "bank_chisq": float32,
        "bank_chisq_dof": int,
        "cont_chisq": float32,
        "psd_var_val": float32,
        "sigmasq": float32,
    }
    out_types.update(SingleDetSGChisq.returns)
    out_vals_ref = dict.fromkeys(out_types)
    names = sorted(out_vals_ref.keys())

    if len(strain_segments.segment_slices) == 0:
        logger.info("--filter-inj-only specified and no injections in analysis time")
        event_mgr = events.EventManager(
            opt,
            names,
            [out_types[n] for n in names],
            psd=None,
            gating_info=gwstrain.gating_info,
        )
        event_mgr.finalize_template_events()
        event_mgr.write_events(opt.output)
        logger.info("Finished")
        sys.exit(0)

    # FIXME: Maybe we should use the PSD corresponding to each trigger
    if opt.psdvar_segment is not None:
        logger.info("Calculating PSD variation")
        psd_var = pycbc.psd.calc_filt_psd_variation(
            gwstrain,
            opt.psdvar_segment,
            opt.psdvar_short_segment,
            opt.psdvar_long_segment,
            opt.psdvar_psd_duration,
            opt.psdvar_psd_stride,
            opt.psd_estimation,
            opt.psdvar_low_freq,
            opt.psdvar_high_freq,
        )

    if opt.enable_q_transform:
        logger.info("Performing q-transform on analysis segments")
        q_trans = qtransform.inspiral_qtransform_generator(segments)

    else:
        q_trans = {}

    if opt.checkpoint_interval:
        checkpoint_file = opt.output + ".checkpoint"

    checkpoint_exists = False
    tnum_start = 0
    if opt.checkpoint_interval and os.path.isfile(checkpoint_file):
        try:
            tnum_start, event_mgr = events.EventManager.restore_state(checkpoint_file)
            checkpoint_exists = True
        except Exception as e:
            if opt.require_valid_checkpoint:
                logger.info("Failed to load checkpoint file")
                raise (e)

            logger.info("Failed to load checkpoint file, starting anew")
            logger.info(e)

    if not checkpoint_exists:
        event_mgr = events.EventManager(
            opt,
            names,
            [out_types[n] for n in names],
            psd=segments[0].psd,
            gating_info=gwstrain.gating_info,
            q_trans=q_trans,
        )

    template_mem = zeros(tlen, dtype=complex64)
    cluster_window = int(opt.cluster_window * gwstrain.sample_rate)

    use_cluster = opt.cluster_window != 0.0

    ncores = ctx.num_threads if hasattr(ctx, "num_threads") else 1
    if opt.multiprocessing_nprocesses:
        ncores *= opt.multiprocessing_nprocesses

    matched_filter = MatchedFilterControl(
        opt.low_frequency_cutoff,
        None,
        opt.snr_threshold,
        tlen,
        delta_f,
        complex64,
        segments,
        template_mem,
        use_cluster,
        downsample_factor=opt.downsample_factor,
        upsample_threshold=opt.upsample_threshold,
        upsample_method=opt.upsample_method,
        gpu_callback_method=opt.gpu_callback_method,
        cluster_function=opt.cluster_function,
    )

    bank_chisq = vetoes.SingleDetBankVeto.from_cli(opt)

    power_chisq = vetoes.SingleDetPowerChisq.from_cli(opt)

    autochisq = vetoes.SingleDetAutoChisq.from_cli(opt)

    logger.info("Overwhitening frequency-domain data segments")
    for seg in segments:
        seg /= seg.psd

    logger.info("Read in template bank")
    bank = waveform.FilterBank(
        opt.bank_file,
        flen,
        delta_f,
        low_frequency_cutoff=None if opt.enable_bank_start_frequency else flow,
        dtype=complex64,
        phase_order=opt.order,
        taper=opt.taper_template,
        approximant=opt.approximant,
        out=template_mem,
        max_template_length=opt.max_template_length,
        enable_compressed_waveforms=opt.use_compressed_waveforms,
        waveform_decompression_method=opt.waveform_decompression_method
        if opt.use_compressed_waveforms
        else None,
    )

    sg_chisq = SingleDetSGChisq.from_cli(opt, bank)

    logger.info("Full template bank size: %s", len(bank))
    bank.template_thinning(inj_filter_rejector)
    logger.info("Template bank size after thinning: %s", len(bank))

    tsetup = time.time() - tstart
    tcheckpoint = time.time()

    tanalyze = list(range(tnum_start, len(bank)))
    n = opt.finalize_events_template_rate
    n = 1 if n is None else n
    tchunks = [tanalyze[i : i + n] for i in range(0, len(tanalyze), n)]

    mmap = map
    if opt.multiprocessing_nprocesses:
        mmap = Pool(opt.multiprocessing_nprocesses).map

    for tchunk in tchunks:
        data = list(mmap(template_triggers, tchunk))

        for elem in data:
            out_vals_all, tparam = elem
            if len(out_vals_all) > 0:
                event_mgr.new_template(tmplt=tparam)

                for edata in out_vals_all:
                    event_mgr.add_template_events(names, [edata[n] for n in names])

                event_mgr.cluster_template_events("time_index", "snr", cluster_window)
                event_mgr.finalize_template_events()

        if opt.finalize_events_template_rate is not None:
            event_mgr.consolidate_events(opt, gwstrain=gwstrain)

        if opt.checkpoint_interval and (
            time.time() - tcheckpoint > opt.checkpoint_interval
        ):
            event_mgr.save_state(max(tchunk), opt.output + ".checkpoint")
            tcheckpoint = time.time()

        if opt.checkpoint_exit_maxtime and (
            time.time() - tstart > opt.checkpoint_exit_maxtime
        ):
            event_mgr.save_state(max(tchunk), opt.output + ".checkpoint")
            sys.exit(opt.checkpoint_exit_code)

event_mgr.consolidate_events(opt, gwstrain=gwstrain)
event_mgr.finalize_events()
logger.info("Outputting %s triggers", str(len(event_mgr.events)))

tstop = time.time()
run_time = tstop - tstart
event_mgr.save_performance(ncores, len(segments), len(bank), run_time, tsetup)

logger.info("Writing out triggers")
event_mgr.write_events(opt.output)

if opt.fft_backends == "fftw":
    if opt.fftw_output_float_wisdom_file:
        fft.fftw.export_single_wisdom_to_filename(opt.fftw_output_float_wisdom_file)

    if opt.fftw_output_double_wisdom_file:
        fft.fftw.export_double_wisdom_to_filename(opt.fftw_output_double_wisdom_file)

logger.info("Finished")
