!********************************************************************
! Dolphine Ocean Model v1.0
! Date:	Nov 3, 2015
!********************************************************************

program main 
    
    call init
    call run 
    call finalize

    stop
end program


subroutine init()
    use driver 
    implicit real*8 (a-h,o-z)
    call init_comm
    call mpi_grid_gen
    call init_io
    call set_windmix
    call set_rundata
    call set_zkb
    call set_surfwinds
    call set_area 
    call set_ts_nudging 
    call set_lateral_mixing
    call set_extra_vertical_eddy_viscosity
    call init_solver
    call set_seasonal_data      
    call write_topo
    call eliminate_spurious_values

end subroutine 


subroutine run
    use driver
    implicit real*8 (a-h,o-z)

    if(myid.eq.0) print*,'mxit,itf,days',mxit,itf,days

    nopt=30
    it0=itf
    wopt=1./dble(nopt*itfday)
    timer=0.d0
    timer(10)=mpi_wtime()

    ! ------------------------------
    ! main time integration loop 100
    ! ------------------------------
    do itf=1,mxit
	timer(1)=timer(1)+mpi_wtime()-timer(10)
	timer(10)=mpi_wtime()

	days=itf/daodt
	n=days/360.+.0001
	tmp=days/360.
	! during year, n360 goes from 0 to 360
	n360=(tmp-n)*360.+.1
	nyr=n+1

	!call check_salinity_budget

	call mpi_barrier(m_cart,ierr)

	call fsglo

	call mpi_barrier(m_cart,ierr)

	call check_blowup

	call write_hist

	if (mod(itf,itfday).ne.0.and.itf.lt.mxit) continue 

	call restart

    enddo

    timer(1)=timer(1)+mpi_wtime()-timer(10)
    open(88,file='data/timer'//grd,form='unformatted')
    write(88)timer
    close(88)

end subroutine


subroutine finalize()
    call mpi_finalize(ierr)
end subroutine 
