#!/usr/bin/env python3
import pyoasis
from pyoasis import OASIS  # pylint: disable=no-name-in-module
import numpy as np

from amip_forcing_mod import AMIPForcing

# Constants
CPLNG_COMP_NAME = 'AMIPFORC'

if __name__ == '__main__':
    # Setup
    amip = AMIPForcing()
    grid_ny, grid_nx = amip.AMIP_sst.shape

    # OASIS init
    comp = pyoasis.Component(CPLNG_COMP_NAME)
    partition = pyoasis.SerialPartition(grid_nx*grid_ny)
    AMIP_sst_var = pyoasis.Var("AMIP_sst", partition, OASIS.OUT)
    AMIP_sic_var = pyoasis.Var("AMIP_sic", partition, OASIS.OUT)
    comp.enddef()

    amip.setup_amip_forcing(amip.StartYear, amip.StartMonth, amip.StartDay)

    # Time loop
    # Note that according to OASIS convention, the first time step is at time=0
    # and the last one is at runlength-timestep (see OASIS documentation)
    for time in range(0, amip.RunLengthSec, amip.TimeStepSec):
       # Currently nothing (no signal) is received from the atmosphere.
       # This could potentially lead to problems in the case of very long
       # runs with high resolution, in that case a buffer (Oasis?) will hold
       # hold a large amount of data from the AMIP_READER that haven't been
       # fetched by the atmopshere model yet.
       #
       # cplng_exchange(time,cplng_stage_rcv_atm) # Got from Fortran code

        amip.update_sst_and_sic()

        AMIP_sst_var.put(time, amip.AMIP_sst)
        AMIP_sic_var.put(time, amip.AMIP_sic)

    amip.finalise_amip_forcing()

    # del comp
