#
# Make the posterior plot here since we need to make it the child of every sql
# node in the DAG
#
if use_ile_postproc:
    pos_plot_job_type, pos_plot_job_name = dagutils.write_posterior_plot_sub(
        tag="pos_plot", log_dir=opts.log_directory)
    pos_plot_job_type.write_sub_file()
    pos_plot_node = pipeline.CondorDAGNode(pos_plot_job_type)
    pos_plot_node.set_pre_script(dagutils.which("coalesce.sh"))
    pos_plot_node.set_category("PLOT")
    pos_plot_node.set_priority(JOB_PRIORITIES["PLOT"])
    ppdag.add_node(pos_plot_node)

sql_job_type, sql_job_name = dagutils.write_result_coalescence_sub(
    tag="coalesce", log_dir=opts.log_directory)
sql_job_type.write_sub_file()

# TODO: Mass index table
#for i, (m1, m2) in enumerate([(tmplt.mass1, tmplt.mass2) for tmplt in tmplt_bnk]):
for i, tmplt in enumerate(tmplt_bnk):
    mass_grouping = "MASS_SET_%d" % i

    ile_node = pipeline.CondorDAGNode(ile_job_type)
    ile_node.set_priority(JOB_PRIORITIES["ILE"])
    ile_node.add_macro("macromass1", tmplt.mass1)
    ile_node.add_macro("macromass2", tmplt.mass2)
    if opts.write_eff_lambda:
        ile_node.add_macro("macroefflambda", tmplt.psi0)
    if opts.write_deff_lambda:
        ile_node.add_macro("macrodefflambda", tmplt.psi3)
    ppdag.add_node(bpp_plot_node)

#
# Make the posterior plot here since we need to make it the child of every sql
# node in the DAG
#
if use_ile_postproc:
    pos_plot_job_type, pos_plot_job_name = dagutils.write_posterior_plot_sub(tag="pos_plot", log_dir=opts.log_directory)
    pos_plot_job_type.write_sub_file()
    pos_plot_node = pipeline.CondorDAGNode(pos_plot_job_type)
    pos_plot_node.set_pre_script(dagutils.which("coalesce.sh"))
    pos_plot_node.set_category("PLOT")
    pos_plot_node.set_priority(JOB_PRIORITIES["PLOT"])
    ppdag.add_node(pos_plot_node)

sql_job_type, sql_job_name = dagutils.write_result_coalescence_sub(tag="coalesce", log_dir=opts.log_directory)
sql_job_type.write_sub_file()

# TODO: Mass index table
# FIXME: Expanding intrinsic set will require modification here
for i, (m1, m2) in enumerate([(tmplt.mass1, tmplt.mass2) for tmplt in tmplt_bnk]):
    mass_grouping = "MASS_SET_%d" % i

    ile_node = pipeline.CondorDAGNode(ile_job_type)
    ile_node.set_priority(JOB_PRIORITIES["ILE"])
    ile_node.add_macro("macromass1", m1)
    ile_node.add_macro("macromass2", m2)
    if use_bayespe_postproc:
        # If we're using the Bayesian PE post processing script, dump the data
        ile_node.set_post_script(dagutils.which("process_ile_output"))
        ile_node.add_post_script_arg("--output ILE_%s.txt" % mass_grouping)