

import os
import re
import subprocess
import datetime as dt

import time
import shutil

all_iata_codes = ["FSD","MSO","ORD","MKE","PHX","RDU","FAR","LIT","PIT","SDF","CYS","BOI","MSP","OMA","ABQ","LAS","DTW","BOS","OKC","MEM","BNA","ELP","DEN","IND","SFO","ATL","CMH","DFW","SAT","PHL","DSM","JAX","IAH","LAX","LGA","BHX","MSY","MIA","BWI","SLC","PHX","CVG","CLT","SMF","SEA","MCI","PDX","COS","SAN","AUS","DCA"]



# #takes 00:06:15

# #ran on 9/21
# #runs for all cities
# #---------------------------------------uhi_metrobuffer_generation.py
# some_command = 'srun /home/rhussain/miniconda3/envs/uhi_env/bin/python /home/rhussain/urban-heat-islands-phase2/phase2_0_uhi_metrobuffer_generation.py'
# # print(os.system("ls"))
# # https://cmdlinetips.com/2014/03/how-to-run-a-shell-command-from-python-and-get-the-output/
# out = subprocess.Popen(
# ['sbatch', '--job-name=phase2_0_uhi_metrobuffer_generation',
# '--output=logs/phase2_0_uhi_metrobuffer_generation.output',
# '--account=eros',
# '--partition=workq',
# '--nodes=1',
# '--ntasks=1',
# '--cpus-per-task=40',
# '--time=2-00:00:00',  # hpc limit is 2 days if u really need it
# '--mail-type=ALL',
# '--mail-user=rhussain@contractor.usgs.gov',
# '--wrap=' + some_command + '',
# ],
# stdout=subprocess.PIPE,
# stderr=subprocess.STDOUT)
# stdout, stderr = out.communicate()
# print(subprocess.list2cmdline(out.args))
# print(stdout)
# # https://stackoverflow.com/questions/26825729/extract-number-from-string-in-python/26825781
# # basically when u submit a slurm job,
# # it returns a string that has one number in it, which is the slurm job id
# # so we just use a regex to get the number that is in the string
# all_numbers = re.findall("\d+", str(stdout))
# print(all_numbers)
# job_id = all_numbers[0]
# dependency_string = '--dependency=afterok:' + job_id



#----------------------------------------------------------------------------------------------|


# #this one takes 03:04:45 per site
# #so here you can do the loop
# #then add the job dependency

# base_output_directory_332 = "/caldera/projects/usgs/eros/urban_heat_islands/Phase2/section_332/output"
# if os.path.exists(base_output_directory_332):
#    shutil.rmtree(base_output_directory_332)
#    time.sleep(10)
# if not os.path.exists(base_output_directory_332):
#     os.mkdir(base_output_directory_332)
# #use this one not the one above
# for i in range(52):
# #for i in range(4):
#     #step
#     # for i in range(52): 332X
#     #this step runs on all iata codes, you dont need to pass one in for each
#     some_command = 'srun /home/rhussain/miniconda3/envs/uhi_env/bin/python /home/rhussain/urban-heat-islands-phase2/phase2_2_landcover_raster_generation_add_section_332X.py --site_tuples_index '+str(i)
#     # print(os.system("ls"))
#     # https://cmdlinetips.com/2014/03/how-to-run-a-shell-command-from-python-and-get-the-output/
#     out = subprocess.Popen(
#     ['sbatch', '--job-name=urban_heat_islands_landcover_raster_generation_add_section_332X_'+str(i),
#     '--output=logs/phase2_2_landcover_raster_generation_add_section_332X_'+str(i)+'.output',
#     '--account=eros',
#     '--partition=workq',
#     '--nodes=1',
#     '--ntasks=1',
#     '--cpus-per-task=40',
#     '--time=2-00:00:00',  # hpc limit is 2 days if u really need it
#     '--mail-type=ALL',
#     '--mail-user=rhussain@contractor.usgs.gov',
#     '--wrap=' + some_command + '',
#     ],
#     stdout=subprocess.PIPE,
#     stderr=subprocess.STDOUT)
#     stdout, stderr = out.communicate()
#     print(subprocess.list2cmdline(out.args))
#     print(stdout)
#     # https://stackoverflow.com/questions/26825729/extract-number-from-string-in-python/26825781
#     # basically when u submit a slurm job,
#     # it returns a string that has one number in it, which is the slurm job id
#     # so we just use a regex to get the number that is in the string
#     all_numbers = re.findall("\d+", str(stdout))
#     print(all_numbers)
#     job_id = all_numbers[0]
#     dependency_string = '--dependency=afterok:' + job_id





#the reason we need it here is because
#slurm jobs might run on different days
#this ensures that the date a slurm job looks for will be the same
#instead of one running on monday, and the second running on tuesday looking for a tuesday date
processing_date = dt.datetime.today().strftime('%Y%m%d')



#------------------------------------------------------------------------------phase2_1_uhi_urbanbuffer_clip
#any script that uses clipped_output_directory needs this one to finish first
#Run time 00:01:46

#ran on 9/21
chosen_iata_phase1_codes = all_iata_codes

#for iata_code in ["ATL", "IAH", "PHX","MSP"]:
#for iata_code in ["MSP","IAH"]:


#MSP should be ready
#for iata_code in ["ATL","MSP","OKC"]:
#for iata_code in ["BHX", "MSY", "PIT"]:
#PHL failed
#for iata_code in ["PHL","OKC","ATL"]:
#for iata_code in ["MSY","PHL"]:
#for iata_code in ["SAT","IND","OMA","ORD","BWI"]:





#for iata_code in ["MSO","FSD"]:#already transferred





#"DCA","BWI","ORD","OMA","IND","SAT","BHX","MSY","PIT","MSP","ATL","IAH","PHX","OKC","DFW","JAX", "DTW","CMH"


#-------------------------

# remaining 
# {'LAS', 'SEA', 'MKE', 'FAR', 'DSM', 'DEN', 'LIT', 'BOI', 'BNA', 'PDX', 'MIA', 'SMF', 'COS', 'ELP', 'MEM', 'SAN', 'BOS', 'SLC', 'RDU', 'CLT', 'CVG', 'ABQ', 'LAX', 'LGA', 'AUS', 'MCI', 'SFO', 'CYS', 'PHL'}

#in transferred
#for iata_code in ["CLT","SFO","BOI","LAS", "SEA", "MKE", "FAR", "DSM"]:

#needs transfer
#remaining {'DEN', 'LIT', 'BNA', 'PDX', 'MIA', 'SMF', 'COS', 'ELP', 'MEM', 'SAN', 'BOS', 'SLC', 'RDU', 'CVG', 'ABQ', 'LAX', 'LGA', 'AUS', 'MCI', , 'CYS', 'PHL'}
#for iata_code in ["DEN", "LIT", "BNA", "PDX", "MIA", "SMF", "COS", "ELP", "MEM", "SAN", "BOS", "SLC", "RDU", 
#"CVG", "ABQ", "LAX", "LGA", "AUS", "MCI", "CYS", "PHL"]:

for iata_code in ["CVG"]:





# #------------------------------------------------------------------------------------phase2_21_uhi_stats_citycenbased_persistent
#     #Run time 00:00:31
#     #needs 354 to complete first
    
    workdir = "/caldera/projects/usgs/eros/urban_heat_islands/Phase1/" + iata_code + "/output_folder"
    iata_code = iata_code
    metrobuffer_shapefile_root = "/caldera/projects/usgs/eros/urban_heat_islands/Phase2/uhi_metrobuffer_generation"
    uhi_lc_root = "/caldera/projects/usgs/eros/urban_heat_islands/Phase2/section_332/output/" + iata_code
    section_354_directory = "/caldera/projects/usgs/eros/urban_heat_islands/Phase2/section_354/"+iata_code

    some_command = 'srun /home/rhussain/miniconda3/envs/uhi_env/bin/python /home/rhussain/urban-heat-islands-phase2/phase2_21_uhi_stats_citycenbased_persistent.py --workdir ' + str(workdir) + ' --iata_code ' +  str(iata_code) + ' --metrobuffer_shapefile_root ' + str(metrobuffer_shapefile_root) + ' --uhi_lc_root '+ str(uhi_lc_root) + ' --section_354_directory '+str(section_354_directory)
    print(some_command)

    # print(os.system("ls"))
    # https://cmdlinetips.com/2014/03/how-to-run-a-shell-command-from-python-and-get-the-output/
    
    out = subprocess.Popen(
    ['sbatch', '--job-name=phase2_21_uhi_stats_citycenbased_persistent'+iata_code,
    '--output=logs/phase2_21_uhi_stats_citycenbased_persistent'+iata_code+'.output',
    '--account=eros',
    '--partition=workq',
    '--nodes=1',
    '--ntasks=1',
    '--cpus-per-task=40',
    '--time=2-00:00:00',  # hpc limit is 2 days if u really need it
    '--mail-type=ALL',
    '--mail-user=rhussain@contractor.usgs.gov',
    '--wrap=' + some_command + '',
    ],
    stdout=subprocess.PIPE,
    stderr=subprocess.STDOUT)
    stdout, stderr = out.communicate()
    print(subprocess.list2cmdline(out.args))
    print(stdout)
    # https://stackoverflow.com/questions/26825729/extract-number-from-string-in-python/26825781
    # basically when u submit a slurm job,
    # it returns a string that has one number in it, which is the slurm job id
    # so we just use a regex to get the number that is in the string
    all_numbers = re.findall("\d+", str(stdout))
    print(all_numbers)
    job_id = all_numbers[0]
    dependency_string = '--dependency=afterok:' + job_id



