
# Copyright 2022 - Barcelona Supercomputing Center
# Author: Etienne Tourigny, Rodrigo Martín Posada
# MIT License
from datetime import date
import numpy as np
import yaml
from yaml.loader import SafeLoader
import argparse
import os
import sys  

if __name__ == '__main__':

### Read in YAML file 
    current_path = os.getcwd()
    parser=argparse.ArgumentParser()
    parser.add_argument("--model_name",type=str)
    parser.add_argument("--template_name",type=str)
    parser.add_argument("--file_name",type=str)
    parser.add_argument("--leg_length_sec",type=str)
    parser.add_argument("--cpl_freq_amip_sec",type=str)
    parser.add_argument("--StartYear",type=str)
    parser.add_argument("--StartMonth",type=str)
    parser.add_argument("--StartDay",type=str)
    parser.add_argument("--FixYear",type=str)
    parser.add_argument("--amip_interpolate",type=str)
    parser.add_argument("--co2_interpolate",type=str)
    args = parser.parse_args()
    ModelName = args.model_name 
    RunLengthSec = args.leg_length_sec
    TimeStepSec = args.cpl_freq_amip_sec
    StartYear = args.StartYear
    StartMonth = args.StartMonth
    StartDay = args.StartDay
    FixYear = args.FixYear
    AMIP_interpolate = args.amip_interpolate
    CO2_interpolate = args.co2_interpolate

    template_name = args.template_name
    file_name = args.file_name
    # TODO add check to create a test here 


    yamel_file_path = "/home/bsc32/bsc32013/scrath_23013/ec_earth/amip/python-amip-reader/tests/data/template_amip.yaml" #os.path.join(current_path,args.template_name)
    print(yamel_file_path)

    # if os.path.isfile(yamel_file_path) == False: 
    #     print("YAML file does not exist. Aborted", file=sys.stderr)
    #     raise NameError("YAML file does not exist.")
        
    # Open the template file and load the file 
    read_in_data = {}  
    with open(yamel_file_path) as file:
        read_in_data = yaml.load(file, Loader=SafeLoader)
        file.close()

## Modify the ReadInData based on the values passed by the RunExample 


# Modify the fields from template files 
    read_in_data['RunLengthSec'] = RunLengthSec
    read_in_data['TimeStepSec'] = TimeStepSec
    read_in_data['StartYear'] = StartYear
    read_in_data['StartMonth'] = StartMonth
    read_in_data['StartDay'] = StartDay
    read_in_data['FixYear'] = FixYear

    read_in_data['FileInputVars']['AMIP_sst_monthly']['interpolate'] = AMIP_interpolate
    read_in_data['FileInputVars']['AMIP_sic_monthly']['interpolate'] = AMIP_interpolate
    read_in_data['FileInputVars']['CO2_em_monthly']['interpolate'] = CO2_interpolate
    read_in_data['FileInputVars']['CO2_land_monthly']['interpolate'] = CO2_interpolate
    read_in_data['FileInputVars']['CO2_ocean_monthly']['interpolate'] = CO2_interpolate

    # Save it again
with open(file_name, 'w') as stream:
    yaml.dump(read_in_data, stream, sort_keys=False)








### Read in template file   
    #read_yaml(read_in_data)
    # RunLengthSec = read_in_data.get("RunLengthSec")
    # TimeStepSec = read_in_data.get("TimeStepSec")
    # StartYear = read_in_data.get("StartYear")
    # StartMonth = read_in_data.get("StartMonth")
    # StartDay = read_in_data.get("StartDay")
    # FixYear = read_in_data.get("FixYear")
    
    # Read in FileInputVars information
    # FileInputVars = read_in_data.get("FileInputVars")
    # FileInputVars_AMIP_sst_monthly = FileInputVars.get("AMIP_sst_monthly")
    # FileInputVars_AMIP_sst_monthly_id = FileInputVars_AMIP_sst_monthly.get("id")
    # FileInputVars_AMIP_sic_monthly= FileInputVars.get("AMIP_sic_monthly")
    # FileInputVars_CO2_em_monthly = FileInputVars.get("CO2_em_monthly")
    # FileInputVars_CO2_land_monthly = FileInputVars.get("CO2_land_monthly")


    # Read in OasisOutputVars information
    # OasisOutputVars = read_in_data.get("OasisOutputVars")
    # print(OasisOutputVars)
    # OasisOutputVars_AMIP_sst_monthly = OasisOutputVars.get("AMIP_sst_monthly")
    # OasisOutputVars_AMIP_sic= OasisOutputVars.get("AMIP_sic")
    # OasisOutputVars_AMIP_CO2_emis = OasisOutputVars.get("AMIP_CO2_emis")
    # OasisOutputVars_AMIP_CO2_land = OasisOutputVars.get("AMIP_CO2_land")
    # OasisOutputVars_AMIP_CO2_ocean = OasisOutputVars.get("AMIP_CO2_ocean")

    # print(FileInputVars_AMIP_sst_monthly_id)
    # LDebug = read_in_data.get("LDebug")

    # experiment_out_data = {}
    # experiment_out_data = {RunLengthSec,TimeStepSec,StartYear, StartMonth, FixYear,FileInputVars}



    # print(experiment_out_data)



    # with open(r'store_file.yaml', 'w') as file:
    #     documents = yaml.dump(experiment_out_data, file)


