"""
configure and run the download of network data
"""

# import python modules
import argparse

# import all the download scripts
import CNEMC_download
import WMO_WDCGG_download
import BJMEMC_download
import US_EPA_CASTNET_download
import US_EPA_AirNow_DOS_download
import EEA_AQ_eReporting_download
import CAPMoN_download
import EANET_download
import CANADA_NAPS_download
import CHILE_SINCA_download
import US_NADP_AMNet_download
import US_NADP_AMoN_download
import MEXICO_CDMX_download
import JAPAN_NIES_download
import MITECO_download
import UK_AIR_download
import NOAA_ISD_download
import US_EPA_AQS_download
import AERONETv3_download
import EEA_AQ_eReporting_download
import EBAS_download




if __name__ == "__main__":

    # add arguments
    #parser = argparse.ArgumentParser(description="Script that runs network downloads")
    #parser.add_argument("--mode", type=str, help="Enter 'all' for entire time period or 'nrt' for most recent file", default='nrt')
    #args = parser.parse_args()

    # modes: 'all': download all the available files
    #        'nrt': download most recent file
    mode = 'nrt'
    print("Mode: {}".format(mode))

    # ghost version
    version = '1.6'
    print("Ghost version: {}".format(version))

    # maximum number of downloads attempts
    n_max_tries = 3

    # maximum time one download attempt should take per network
    download_params = {'US_EPA_AQS':                {'max_time_dl': 5},
                        'US_EPA_AirNow_DOS':        {'max_time_dl': 10},
                        'US_EPA_CASTNET':           {'max_time_dl': 10},
                        'EEA_AQ_eReporting':        {'max_time_dl': 5},
                        'AERONETv3':                {'max_time_dl': 1},
                        'BJMEMC':                   {'max_time_dl': 3},
                        'CNEMC':                    {'max_time_dl': 3},
                        'CANADA_NAPS':              {'max_time_dl': 5},
                        'CAPMoN':                   {'max_time_dl': 5},
                        'US_NADP_AMNet':            {'max_time_dl': 10},
                        'US_NADP_AMoN':             {'max_time_dl': 7},
                        'MEXICO_CDMX':              {'max_time_dl': 10},
                        'NOAA_ISD':                 {'max_time_dl': 15},
                        'MITECO':                   {'max_time_dl': 10},
                        'EANET':                    {'max_time_dl': 5},
                        'CHILE_SINCA':              {'max_time_dl': 30},
                        'EEA_AQ_eReporting':        {'max_time_dl': 20},
                        'JAPAN_NIES':               {'max_time_dl': 60},
                        'WMO_WDCGG':                {'max_time_dl': 60},
                        'UK_AIR':                   {'max_time_dl': 60}}


    # download data
    dl_data = False

    # download meta data too?
    dl_metadata = True

    # networks you want to download
    networks = [US_EPA_AQS_download]

    # download all networks
    #networks = ['all']
    if networks == 'all':
        networks = [US_EPA_AirNow_DOS_download, 
                    US_EPA_CASTNET_download, 
                    BJMEMC_download, 
                    CNEMC_download, 
                    WMO_WDCGG_download, 
                    CAPMoN_download, 
                    EANET_download, 
                    CANADA_NAPS_download, 
                    CHILE_SINCA_download, 
                    US_NADP_AMNet_download, 
                    US_NADP_AMoN_download, 
                    MEXICO_CDMX_download, 
                    JAPAN_NIES_download, 
                    MITECO_download, 
                    UK_AIR_download, 
                    NOAA_ISD_download, 
                    US_EPA_AQS_download, 
                    AERONETv3_download, 
                    EEA_AQ_eReporting_download, 
                    EBAS_download]
            


    # run the desired networks
    for network in networks:

        network_name = str(network).split('/')[-1].split('_download')[0]
        max_time_per_dl = download_params[network_name]['max_time_dl']

        if dl_data == True:
            print("Started downloading data of {}".format(network_name))
            network.download_data(mode, version, n_max_tries, max_time_per_dl)

        if dl_metadata == True:
            print("Started downloading metadata of {}".format(network_name))
            network.download_metadata(n_max_tries, max_time_per_dl)
