"""Repeat timing of FRB 20180916B from CHIME/FRB Catalog 2, and what a plasma lens would need to fake it.

Input: r3-catalog2-rows.csv (the 142 FRB 20180916B rows of chimefrbcat2.csv, release 20251231).
Arrival times are topocentric UTC at 400.1953125 MHz (column mjd_400). Rows with excluded_flag = 1
were taken during non-nominal telescope operation and are dropped. One event per TNS name.

Units: days for arrival times, seconds for intervals, MHz and GHz as named. Requires numpy and scipy.
"""
import csv
import json
import math
from pathlib import Path

import numpy as np
from scipy.optimize import minimize
from scipy.special import gammaln

HERE = Path(__file__).resolve().parent
SECONDS_PER_DAY = 86400.0
SIDEREAL_DAY = 0.99726957
CHIME_LONGITUDE_DEG = -119.6237  # CHIME at DRAO; used only for the hour angle of each arrival
PUBLISHED_PERIOD_DAYS = 16.35    # CHIME/FRB 2020, arXiv:2001.10275v5
PUBLISHED_PERIOD_ERROR = 0.15
PUBLISHED_PHASE_ZERO_MJD = 58369.18
PUBLISHED_HALF_WINDOW_DAYS = 2.7
DISPERSION_DELAY_COEFFICIENT_S = 4.149e-3   # t_DM0 = 4.149 ms DM / nu_GHz^2 (Cordes et al. 2017, Eq. 14)
LENS_STRENGTH_COEFFICIENT = 3430.0          # alpha = 3430 DM D_kpc / (nu_GHz a_AU)^2 (Eq. 3)
FREE_FREE_COEFFICIENT = 3.4e-7              # tau_ff = 3.4e-7 nu^-2.1 T4^-1.32 EM (Eq. 26)
AU_PER_PARSEC = 206264.806


def load_events(path=HERE / 'r3-catalog2-rows.csv'):
    rows = [row for row in csv.DictReader(path.open()) if row['excluded_flag'] == '0']
    groups = {}
    for row in rows:
        groups.setdefault(row['tns_name'], []).append(row)
    events = sorted(groups.values(), key=lambda group: min(float(row['mjd_400']) for row in group))
    arrivals = np.array([min(float(row['mjd_400']) for row in group) for group in events])
    sub_bursts = []
    for group in events:
        times = sorted(float(row['mjd_400']) for row in group)
        sub_bursts += [(time - times[0]) * SECONDS_PER_DAY for time in times[1:]]
    first = events[0][0]
    return {'arrivals_mjd': arrivals, 'sub_burst_offsets_s': np.array(sub_bursts), 'rows_used': len(rows),
            'ra_deg': float(first['ra']), 'dec_deg': float(first['dec'])}


def hour_angles(arrivals_mjd, ra_deg):
    """Approximate hour angle in hours, from the GMST series of the US Naval Observatory (about 0.1 s per century)."""
    days = arrivals_mjd + 2400000.5 - 2451545.0
    gmst_hours = (18.697374558 + 24.06570982441908 * days) % 24.0
    local = (gmst_hours + CHIME_LONGITUDE_DEG / 15.0) % 24.0
    return ((local - ra_deg / 15.0 + 12.0) % 24.0) - 12.0


def weibull_negative_log_likelihood(parameters, gaps):
    """Weibull interval density of Oppermann, Yu and Pen (arXiv:1705.04881v2, Eq. 2) with rate r and shape k."""
    log_k, log_r = parameters
    k, r = math.exp(log_k), math.exp(log_r)
    scale = gaps * r * math.exp(gammaln(1.0 + 1.0 / k))
    return -np.sum(math.log(k) - np.log(gaps) + k * np.log(scale) - scale ** k)


def fit_weibull(gaps):
    start = [math.log(0.5), math.log(1.0 / gaps.mean())]
    result = minimize(weibull_negative_log_likelihood, start, args=(gaps,), method='Nelder-Mead', options={'xatol': 1e-8, 'fatol': 1e-8})
    return math.exp(result.x[0]), math.exp(result.x[1])


def rayleigh_power(arrivals_mjd, period_days):
    phases = 2 * np.pi * (arrivals_mjd / period_days)
    return 2.0 / arrivals_mjd.size * (np.cos(phases).sum() ** 2 + np.sin(phases).sum() ** 2)


def period_scan(arrivals_mjd, lowest=15.5, highest=17.5):
    span = arrivals_mjd.max() - arrivals_mjd.min()
    frequency_step = 0.05 / span   # one twentieth of a cycle across the data span
    frequencies = np.arange(1 / highest, 1 / lowest, frequency_step)
    powers = np.array([rayleigh_power(arrivals_mjd, 1 / frequency) for frequency in frequencies])
    best = int(powers.argmax())
    return {'span_days': round(float(span), 2), 'frequency_step_per_day': float(f'{frequency_step:.4g}'),
            'trial_periods': int(frequencies.size), 'best_period_days': round(float(1 / frequencies[best]), 3),
            'best_rayleigh_power': round(float(powers[best]), 2),
            'rayleigh_power_at_published_period': round(float(rayleigh_power(arrivals_mjd, PUBLISHED_PERIOD_DAYS)), 2)}


def phase_window_fraction(arrivals_mjd, period_days):
    """Fraction of events within the published +-2.7 d of the activity centre (phase 0.5 from MJD 58369.18)."""
    centre = PUBLISHED_PHASE_ZERO_MJD + 0.5 * period_days
    offset = ((arrivals_mjd - centre + 0.5 * period_days) % period_days) - 0.5 * period_days
    return float((np.abs(offset) <= PUBLISHED_HALF_WINDOW_DAYS).mean()), offset


def windowed_poisson_gaps(arrivals_mjd, window_hours, trials, seed):
    """A source bursting at a constant rate, seen only near upper transit and only inside the activity window.

    The window is an idealisation of CHIME's exposure; the real exposure varies from day to day.
    """
    generator = np.random.default_rng(seed)
    start, stop = arrivals_mjd.min(), arrivals_mjd.max()
    shapes = []
    for _ in range(trials):
        days = np.arange(math.floor(start), math.ceil(stop) + 1)
        transit = days + ((0.0 - hour_angles(days, 29.503) / 24.0) % SIDEREAL_DAY)   # nearest upper transit after midnight
        centre = PUBLISHED_PHASE_ZERO_MJD + 0.5 * PUBLISHED_PERIOD_DAYS
        active = np.abs(((transit - centre + 0.5 * PUBLISHED_PERIOD_DAYS) % PUBLISHED_PERIOD_DAYS) - 0.5 * PUBLISHED_PERIOD_DAYS) <= PUBLISHED_HALF_WINDOW_DAYS
        windows = transit[active & (transit >= start) & (transit <= stop)]
        exposure_days = windows.size * window_hours / 24.0
        rate = arrivals_mjd.size / exposure_days
        counts = generator.poisson(rate * window_hours / 24.0, size=windows.size)
        times = np.sort(np.concatenate([centre + (generator.random(count) - 0.5) * window_hours / 24.0 for centre, count in zip(windows, counts)]))
        gaps = np.diff(times) * SECONDS_PER_DAY
        gaps = gaps[gaps > 0]
        if gaps.size > 10:
            shapes.append(fit_weibull(gaps)[0])
    shapes = np.array(shapes)
    return {'window_hours': window_hours, 'trials': int(shapes.size), 'seed': seed,
            'median_weibull_shape': round(float(np.median(shapes)), 3),
            'central_90_percent_shape': [round(float(np.percentile(shapes, 5)), 3), round(float(np.percentile(shapes, 95)), 3)]}


def lens_needed_for_echo(delay_s, frequency_ghz, effective_distance_kpc, temperature_4=1.0):
    """Smallest absorption a single Gaussian lens can have while giving a gain-one echo at this delay.

    The longest delays come from images near u^2 = 1/2, where the gain is one and the delay is
    t_DM0 alpha / (2e) (Cordes et al. 2017, Eqs. 3, 14 and 15). Geometric optics needs the lens to be
    much wider than the Fresnel scale; we take a = 3 r_F. Free-free absorption follows their Eq. 26.
    """
    # delay = t_DM0 alpha / 2e  ->  DM^2 D / a^2 = delay 2e nu^4 / (4.149e-3 * 3430)
    product = delay_s * 2 * math.e * frequency_ghz ** 4 / (DISPERSION_DELAY_COEFFICIENT_S * LENS_STRENGTH_COEFFICIENT)
    wavelength_m = 0.299792458 / frequency_ghz
    fresnel_au = math.sqrt(wavelength_m * effective_distance_kpc * 3.0857e19 / (2 * math.pi)) / 1.495979e11
    size_au = 3.0 * fresnel_au
    dispersion = size_au * math.sqrt(product / effective_distance_kpc)
    density = dispersion / (size_au / AU_PER_PARSEC)
    emission = dispersion ** 2 / (size_au / AU_PER_PARSEC)
    optical_depth = FREE_FREE_COEFFICIENT * frequency_ghz ** -2.1 * temperature_4 ** -1.32 * emission
    strength = LENS_STRENGTH_COEFFICIENT * dispersion * effective_distance_kpc / (frequency_ghz * size_au) ** 2
    return {'delay_s': float(f'{delay_s:.4g}'), 'effective_distance_kpc': effective_distance_kpc, 'lens_size_au': float(f'{size_au:.3g}'),
            'lens_dm_pc_cm3': float(f'{dispersion:.3g}'), 'electron_density_cm3': float(f'{density:.3g}'),
            'emission_measure_pc_cm6': float(f'{emission:.3g}'), 'free_free_optical_depth_600MHz': float(f'{optical_depth:.3g}'),
            'lens_strength_alpha': float(f'{strength:.3g}'), 'source_offset_lens_radii': float(f'{0.707 * (1 + strength * math.exp(-0.5)):.3g}')}


def run(seed=20261114):
    events = load_events()
    arrivals = events['arrivals_mjd']
    gaps = np.diff(arrivals) * SECONDS_PER_DAY
    angles = hour_angles(arrivals, events['ra_deg'])
    shape, rate = fit_weibull(gaps)
    in_window, offsets = phase_window_fraction(arrivals, PUBLISHED_PERIOD_DAYS)
    scan = period_scan(arrivals)
    best_in_window, _ = phase_window_fraction(arrivals, scan['best_period_days'])
    drift_cycles = (arrivals.max() - PUBLISHED_PHASE_ZERO_MJD) / PUBLISHED_PERIOD_DAYS * PUBLISHED_PERIOD_ERROR / PUBLISHED_PERIOD_DAYS
    window_hours = 2 * float(np.percentile(np.abs(angles), 95)) * SIDEREAL_DAY
    return {
        'input': {'file': 'r3-catalog2-rows.csv', 'rows_used': events['rows_used'], 'events': int(arrivals.size),
                  'first_mjd': round(float(arrivals.min()), 5), 'last_mjd': round(float(arrivals.max()), 5),
                  'sub_burst_offsets': int(events['sub_burst_offsets_s'].size)},
        'intervals': {'shortest_gap_s': round(float(gaps.min()), 1), 'median_gap_s': round(float(np.median(gaps)), 0), 'longest_gap_days': round(float(gaps.max() / SECONDS_PER_DAY), 1),
                      'gaps_under_one_hour': int((gaps < 3600).sum()), 'gaps_one_hour_to_one_day': int(((gaps >= 3600) & (gaps < SECONDS_PER_DAY)).sum()),
                      'gaps_over_one_day': int((gaps >= SECONDS_PER_DAY).sum()),
                      'sub_burst_offsets_ms': [round(float(np.min(events['sub_burst_offsets_s'])) * 1e3, 3), round(float(np.max(events['sub_burst_offsets_s'])) * 1e3, 2)]},
        'hour_angle': {'median_abs_hours': round(float(np.median(np.abs(angles))), 3), 'abs_95th_percentile_hours': round(float(np.percentile(np.abs(angles), 95)), 3),
                       'events_beyond_three_hours': int((np.abs(angles) > 3).sum())},
        'weibull_fit_all_gaps': {'shape_k': round(shape, 3), 'rate_per_day': round(rate * SECONDS_PER_DAY, 3)},
        'windowed_poisson_check': windowed_poisson_gaps(arrivals, window_hours, 200, seed),
        'periodicity': {'published_period_days': PUBLISHED_PERIOD_DAYS, 'published_phase_zero_mjd': PUBLISHED_PHASE_ZERO_MJD,
                        'fraction_within_published_window_at_16_35_d': round(in_window, 3),
                        'phase_uncertainty_cycles_at_last_event': round(float(drift_cycles), 2),
                        **scan, 'fraction_within_window_at_best_period': round(best_in_window, 3)},
        'lens_requirements': [lens_needed_for_echo(delay, 0.6, distance) for delay in (float(gaps.min()), float(np.median(gaps)), SECONDS_PER_DAY, PUBLISHED_PERIOD_DAYS * SECONDS_PER_DAY) for distance in (0.001, 1.0)],
    }


if __name__ == '__main__':
    print(json.dumps(run(), indent=1))
