program runoff_mapper

    use config_mod
    use runoff_mapper_mod
    use cplng

    implicit none

    integer      ::      RunLengthSec, TimeStepSec
    namelist /NAMRNFMAP/ RunLengthSec, TimeStepSec

    integer :: time
    logical :: LUNnotFree

    call cplng_init
    call cplng_config

    call setup_runoff_maps

    ! Read RunLengthSec,TimeStepSec from namelist
    inquire(NAMLUN,opened=LUNnotFree)
    if (LUNnotFree) call ERROR('Namelist LUN not free')
    open(NAMLUN,file=NamelistFileName,status='OLD')
    read(NAMLUN,nml=NAMRNFMAP)
    close(namlun)

    ! 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)
    do time=0,RunLengthSec-TimeStepSec,TimeStepSec

        ! Get runoff field from the atmosphere
        call cplng_exchange(time,cplng_stage_rcv_atm)

        ! Do the mapping
        call map_runoff

        ! Send mapped runoff+calving to the ocean
        call cplng_exchange(time,cplng_stage_snd_oce)

    enddo

    call cplng_finalize

end program runoff_mapper
