#!/usr/bin/env python3
# Copyright 2022 - Barcelona Supercomputing Center
# Authors: Etienne Tourigny, Rodrigo Martín Posada
# MIT License

from DataCoupler import DataCoupler
import argparse
import sys 

# Constants
NAMELIST_FILE_NAME = 'namelist.amip'
NAMELIST_SECTION = 'NAMAMIP'
LOG_FILE_NAME = 'amip.log'
CPLNG_COMP_NAME = 'AMIPFORC'
YAML_INPUT_FILE_NAME = "namelist.amip_exp1.yaml"

if __name__ == '__main__':
    # receive the yaml experiment input file is it exists , otherwise pass 
    try:
        parser=argparse.ArgumentParser()
        parser.add_argument("--yaml_experiment_input_file",type=str,default=YAML_INPUT_FILE_NAME)
        args = parser.parse_args()
        YAML_INPUT_FILE_NAME = args.yaml_experiment_input_file 
    except:
        print("NOTE: Could note read in the input yaml file : {}".format(YAML_INPUT_FILE_NAME), file=sys.stderr)
    else:
        print("NOTE: input yaml file is : {}".format(YAML_INPUT_FILE_NAME), file=sys.stderr)

    # Setup
    amip = DataCoupler(NAMELIST_FILE_NAME, NAMELIST_SECTION, YAML_INPUT_FILE_NAME, LOG_FILE_NAME)
    amip.setup_data_coupler()
    amip.setup_oasis(CPLNG_COMP_NAME, write_grid=True, write_grid_mask=True,
                     write_grid_corners= True, write_grid_areas = True)
    # Time loop
    amip.run_time_loop()
    # Finalize
    amip.finalise_data_coupler()

