from __future__ import print_function
import numpy as np
import sys,os

from WEAVEify_routines.step_02_common_routines.dictionary_functions import  \
CHECK_AND_INITIALIZE_KEY_VALUE,CHECK_IF_KEY_PRESENT

from WEAVEify_routines.step_02_common_routines.directory_functions import \
MAKE_DIRECTORY

def WEAVEify_function_calls_main(**kwargs):
    inp_dict_script = locals()["kwargs"]

    from default_parameters_WEAVEify import step_a_initialize_weavefy_parameter,\
    step_b_user_input_command_line,step_d_initialize_dependent_variables,\
    step_c_user_input_script, step_e_reorganize_input_for_weaveify


    from WEAVEify_routines.step_03_instrumental_broadening.step_03_lsf_effect import \
    step_03_lsf_effect_main

    from WEAVEify_routines.step_04_wavelength_resampling.wavelength_resampling_routines import \
    wavelength_resampling_routines_main

    from WEAVEify_routines.step_05_continuum_placement_from_template.continuum_placement_routines \
    import continuum_placement_routines_main

    from WEAVEify_routines.step_06_noise_addition.noise_addition_routines import \
    noise_addition_routines_main

    from WEAVEify_routines.step_07_read_write_mock_spectra.input_output_all_format \
    import save_optical_depths_all_format,save_flux_fields_all_format

    from WEAVEify_routines.step_01_weave_mock_utils.util_03_plot_spectra.make_quick_plots \
    import make_quick_plots_main

    from WEAVEify_routines.step_02_common_routines.echo_functions import \
    process_completion_print_function


    inp_dict        = step_a_initialize_weavefy_parameter()
    inp_dict        = step_c_user_input_script(inp_dict,inp_dict_script)
    inp_dict        = step_b_user_input_command_line(inp_dict)
    inp_dict        = step_d_initialize_dependent_variables(inp_dict)
    inp_dict        = step_e_reorganize_input_for_weaveify(inp_dict)

    process_completion_print_function(inp_dict,"Starting WEAVEify code")

    flag_mpi = inp_dict["flag_mpi"]
    out_path = inp_dict["out_path"]

    if inp_dict["rank"] == 0: MAKE_DIRECTORY(out_path) # Make output directory where data would be saved

    inp_dict = step_03_lsf_effect_main(inp_dict)
    inp_dict = wavelength_resampling_routines_main(inp_dict)

    print_statement = "Wavelength Resampling and LSF Convolution completed"
    process_completion_print_function(inp_dict,print_statement)

    inp_dict = continuum_placement_routines_main(inp_dict)
    inp_dict = noise_addition_routines_main(inp_dict)
    print_statement = "Continuum Placement and Noise Addition completed"
    process_completion_print_function(inp_dict,print_statement)
    inp_dict = save_flux_fields_all_format(inp_dict)



    flag_make_quick_plot = inp_dict["flag_make_quick_plot"]

    if flag_make_quick_plot.upper() == "Y":
        inp_dict  = make_quick_plots_main(inp_dict)
        process_completion_print_function(inp_dict,"Quick plot creation completed")
        process_completion_print_function(inp_dict,"Plot Location : " + inp_dict["out_path"] + inp_dict["out_file_plot"])

    process_completion_print_function(inp_dict,"Mock making code all processes completed")


    return inp_dict
