# plot_em_energy.py¶

plot_em_energy plots the electromagnetic-wave energy and luminosity as measured from a given detector as a function of time. plot_em_energy uses $$\Phi_2$$.

#!/usr/bin/env python3
# PYTHON_ARGCOMPLETE_OK

# Copyright (C) 2021-2022 Gabriele Bozzola
#
# This program is free software; you can redistribute it and/or modify it under
# 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, see <https://www.gnu.org/licenses/>.

import logging

import matplotlib.pyplot as plt

from kuibit import argparse_helper as kah
from kuibit.simdir import SimDir
from kuibit.visualize_matplotlib import (
get_figname,
save_from_dir_filename_ext,
set_axis_limits_from_args,
setup_matplotlib,
)

if __name__ == "__main__":

desc = f"""\
{kah.get_program_name()} plots, for a given detector, the electromagnetic
luminosity and cumulative energy as a function of time as computed from Phi2.
"""

parser = kah.init_argparse(desc)

"--detector-num",
type=int,
required=True,
help="Number of the spherical surface over which to read Phi2.",
)

args = kah.get_args(parser)
setup_matplotlib(rc_par_file=args.mpl_rc_file)

logger = logging.getLogger(__name__)

if args.verbose:
logging.basicConfig(format="%(asctime)s - %(message)s")
logger.setLevel(logging.DEBUG)

figname = get_figname(args, default=f"em_energy_det{args.detector_num}")
logger.debug(f"Using figname {figname}")

with SimDir(
pickle_file=args.pickle_file,
) as sim:

logger.debug("Prepared SimDir")

logger.debug("Computing energy")
logger.debug("Computed energy")

logger.debug("Computing power")
logger.debug("Computed power")

logger.debug("Plotting")

fig, (ax1, ax2) = plt.subplots(2, sharex=True)

# We set E = 0 at t - r = 0
ax2.set_xlabel(r"Time - Detector distance $(t - r)$")
ax1.set_ylabel(r"$dE\slash dt (t)$")
ax2.set_ylabel(r"$E^{<t}(t)$")

f"Det {args.detector_num}", anchor="SW", offset=0.005
)
add_text_to_corner(rf"$r = {radius:.3f}$", anchor="NE", offset=0.005)

set_axis_limits_from_args(args)
logger.debug("Plotted")

logger.debug("Saving")
save_from_dir_filename_ext(
args.outdir,
figname,
args.fig_extension,
tikz_clean_figure=args.tikz_clean_figure,
)
logger.debug("DONE")