

import config

#import scripts_m2m_download as m2m
import scripts_m2m_download_new_modified as m2m

import phase1_st_stats_forHua_optimized_level3 as step1
import phase1_merge_batch_forHua as step2
import os
import time
import traceback


"""
notes from saeed
so the first date in the tileID are the date of Landsat data acquisition and the second date is the processing date of ARD
LE07_CU_011007_20180211_20200615_C01_V1
The bold one is what you need when trying to find tiles for a specific year
"""

if __name__ == '__main__':
    username = config.USERNAME
    password = config.PASSWORD

    #base_dir = "/caldera/projects/usgs/eros/urban_heat_islands/atlanta"
    #base_dir = "C:\\Users\\rhussain\\Desktop\\LSRD\\data\\atlanta_m2m"
    base_dir = "C:\\Users\\rhussain\\OneDrive - DOI\\Desktop\\pycharm_project_data\\urban-heat-islands\\atlanta_m2m"


    #iata_location_identifier = "ATL"
    #a_start_year_inclusive = 1984
    #a_end_year_inclusive = 1988
    #tile_hv_names_to_match = ["H23V13", "H23V14"]

    #iata_location_identifier = "ATL"
    #a_start_year_inclusive = 1984
    #a_end_year_inclusive = 2019
    #tile_hv_names_to_match = ["H23V13", "H23V14", "H24V13", "H24V14"]

    #iata_location_identifier = "IAH"
    #a_start_year_inclusive = 1984
    #a_end_year_inclusive = 2019
    #tile_hv_names_to_match = ["H17V17"]

    iata_location_identifier = "IAH"
    a_start_year_inclusive = 2018
    a_end_year_inclusive = 2019
    #tile_hv_names_to_match = ["H17V17","H17V16"]
    tile_hv_names_to_match = ["H17V16"]

    if not os.path.isdir(base_dir):
        os.mkdir(base_dir)

    succeeded = False
    while not succeeded:
        try:
            #use this to download data
            for a_tile_hv_name in tile_hv_names_to_match:
                some_h = int(a_tile_hv_name[1:3])
                some_v = int(a_tile_hv_name[4:6])
                destination_folder = base_dir+"/"+a_tile_hv_name
                if not os.path.isdir(destination_folder):
                    os.mkdir(destination_folder)
                for current_year in range(a_start_year_inclusive,a_end_year_inclusive+1):
                    acq_date = str(current_year)+"-1-1,"+str(current_year)+"-12-31"
                    #m2m times out if you use more than 1 thread fyi
                    ABC = None
                    m2m.download_files(directory=destination_folder, h=some_h, v=some_v, acq_date=acq_date, threads=1,
                                       username=username, password=password, dataset="ARD_TILE",
                                       products="ST", spacecraft="LANDSAT_4")
                    m2m.download_files(directory=destination_folder, h=some_h, v=some_v, acq_date=acq_date, threads=1,
                                       username=username, password=password, dataset="ARD_TILE",
                                       products="ST", spacecraft="LANDSAT_5")
                    m2m.download_files(directory=destination_folder, h=some_h, v=some_v, acq_date=acq_date, threads=1,
                                       username=username, password=password, dataset="ARD_TILE",
                                       products="ST", spacecraft="LANDSAT_7")
                    m2m.download_files(directory=destination_folder, h=some_h, v=some_v, acq_date=acq_date, threads=1,
                                       username=username, password=password, dataset="ARD_TILE",
                                       products="ST", spacecraft="LANDSAT_8")
                succeeded = True
        except Exception as e:
            print(str(e))
            traceback.print_exc()
            time.sleep(60*10)



    ABC = None
    print("here")
    #use this to process data
    #work_path = base_dir
    #HV_List = tile_hv_names_to_match
    #step1.st_stats_forHua(work_path,HV_List,iata_location_identifier,a_start_year_inclusive,a_end_year_inclusive)
    #step2.merge_batch_forHua(work_path,HV_List,a_start_year_inclusive,a_end_year_inclusive,iata_location_identifier)

