#!/usr/bin/env python
#
# Routine generator
# It generates a bash script to run all the downloads at once
#
# Barcerlona Supercomputing Center - Centro Nacional de Supercomputacion
#        Earth Sciences Department - Computational Earth Sciences
#     
#     Authors: Ivan Jose Cernicharo Ortiz (ivan.cernicharo@bsc.es)
#              Jesus Pena-Izquierdo (jesus.pena@bsc.es)
#

import os
import sys

if __name__ == '__main__':
    # freq = [
    #     'daily',
    #     'monthly_mean'
    # ]
    freq = ['monthly_mean']

    lvl = [
    'PLVL',
    'SLVL'
    ]

    months = [
    'january',
    'february',
    'march',
    'april',
    'may',
    'june',
    'july',
    'august',
    'september',
    'october',
    'november',
    'december'
    ]

    path = '/data/cds_archives/reanalysis/era5'

    product_type = [] # Empty, you can define it if you want

    format = [
    'grib',
    'netcdf'
    ]

    item = 'era5_downloader.py'

    file_name = '00_RUN_' + item.replace('.py','.sh')
    f = open(file_name,'w')
    string = 'rm -rf ' + path + '*\n'
    f.write(string)
    string = 'python3 era5_folder_generator.py ' + path + '\n'
    f.write(string)
    bash_logs = 'Bash_LOGs/era5'
    string = 'mkdir -p ' + bash_logs + '\n'
    f.write(string)

    if product_type == []:
        for fr in freq:
            for lv in lvl:
                for m in months:
                    for fm in format:
                        command = "nohup python3 "
                        command += " " + item 
                        command += " " + fr 
                        command += " " + lv
                        command += " " + m
                        command += " " + path
                        command += " " + fm + " &>"
                        command += " " + bash_logs + "/LOG_out_cds"
                        command += "_" + item.replace('.py','') 
                        command += "_" + fr 
                        command += "_" + lv 
                        command += "_" + m 
                        command += "_" + fm + '.log &\n'
                        f.write(command)
    else:
        for fr in freq:
            for lv in lvl:
                for m in months:
                    for pt in product_type:
                        for fm in format:
                            command = "nohup python3 "
                            command += " " + item 
                            command += " " + fr 
                            command += " " + lv
                            command += " " + m
                            command += " " + path
                            command += " " + fm 
                            command += " " + pt + " &>"
                            command += " " + bash_logs + "/LOG_out_cds"
                            command += "_" + item.replace('.py','') 
                            command += "_" + fr 
                            command += "_" + lv 
                            command += "_" + m 
                            command += "_" + fm
                            command += "_" + pt + '.log &\n'
                            f.write(command)

    f.close()      