import copy
import sys
import time

from crossnetwork_processing_functions import get_default_temporal_resolutions_per_matrix, get_configuration_variables, check_GHOST_stage_temporary_directories, check_GHOST_stage_arguments_logs_submit
from process_GHOST_stage import ProcessGHOSTStage

###--------------------------------------------------------------------------------------------------###
#Get root slurm job ID, and active network/parameter to process --> passed from job submittal script
###--------------------------------------------------------------------------------------------------###
slurm_job_ID, network, parameter, stages_to_process, pipeline_start_time = sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4], sys.argv[5]
stages_to_process = stages_to_process.split(',')

###--------------------------------------------------------------------------------------------------###
#Get dictionaries holding run specific variables, parameter specific variables and network specific variables
###--------------------------------------------------------------------------------------------------###
run_details, parameter_details, network_details = get_configuration_variables(slurm_job_ID, network=network, parameter=parameter)

###--------------------------------------------------------------------------------------------------###
#Check/create/clean argument/log/submit directories of relevant defined GHOST stages
###--------------------------------------------------------------------------------------------------###
check_GHOST_stage_arguments_logs_submit(run_details, stages_to_process)

###--------------------------------------------------------------------------------------------------###
#Process pipeline
###--------------------------------------------------------------------------------------------------###

#add pipeline_start_time to run_details
run_details['pipeline_start_time'] = float(pipeline_start_time)

#open the active pipeline logfile
pipeline_logfile = open('{}/run/pipeline_logs/{}_{}.out'.format(run_details['process_root'],network,parameter),"a")

#initialise ProcessGHOSTStage class instance
process_inst = ProcessGHOSTStage(run_details, parameter_details, network_details, pipeline_logfile)

#STAGE S1
if 'S1' in stages_to_process:
    process_inst.S1()
#STAGE S2
if 'S2' in stages_to_process:
    process_inst.S2()
#STAGE S3
if 'S3' in stages_to_process:
    process_inst.S3()
#STAGE T1
if 'T1' in stages_to_process:
    process_inst.T1()         
#STAGE T2
if 'T2' in stages_to_process:
    #if valid_temporal_resolution_codes variable does not exist, assume all defined temporal resolutions to process are valid 
    if not hasattr(process_inst, 'valid_temporal_resolution_codes'):
        process_inst.valid_temporal_resolution_codes = copy.deepcopy(run_details['temporal_resolutions'])
    #if valid_regions variable does not exist, assume all defined regions to process are valid 
    if not hasattr(process_inst, 'valid_regions'):
        process_inst.valid_regions = copy.deepcopy(run_details['regions'])    
    process_inst.T2()
    #clean all temporary GHOST stage data directories
    check_GHOST_stage_temporary_directories(run_details, parameter_details, stage='all')
#STAGE T1
if 'P1' in stages_to_process:
    process_inst.P1()    
#STAGE E1
if 'E1' in stages_to_process:
    process_inst.E1()  
#STAGE M1
if 'M1' in stages_to_process:
    process_inst.M1()   
#STAGE C1
if 'C1' in stages_to_process:
    process_inst.C1()   
#STAGE Z1
if 'Z1' in stages_to_process:
    process_inst.Z1()   

#close pipeline logfile
pipeline_logfile.close() 