SUBROUTINE control
  !   Control the run

  use basic
  use prec_const
  IMPLICIT NONE

  CALL cpu_time(start)
  !________________________________________________________________________________
  !              1.   Prologue
  !                   1.1     Initialize the parallel environment
  IF (my_id .EQ. 1) WRITE(*,*) 'Initialize MPI...'
  CALL ppinit
  IF (my_id .EQ. 1) WRITE(*,'(a/)') '...MPI initialized'


  CALL daytim('Start at ')

  !                   1.2     Define data specific to run
  IF (my_id .EQ. 1) WRITE(*,*) 'Load basic data...'
  CALL basic_data
  IF (my_id .EQ. 1) WRITE(*,'(a/)') '...basic data loaded.'

  !                   1.3   Read input parameters from input file
  IF (my_id .EQ. 1) WRITE(*,*) 'Read input parameters...'
  CALL readinputs
  IF (my_id .EQ. 1) WRITE(*,'(a/)') '...input parameters read'

  !                   1.4     Set auxiliary values (allocate arrays, set grid, ...)
  IF (my_id .EQ. 1) WRITE(*,*) 'Calculate auxval...'
  CALL auxval
  IF (my_id .EQ. 1) WRITE(*,'(a/)') '...auxval calculated'

  !                   1.5     Initial conditions
  IF (my_id .EQ. 1) WRITE(*,*) 'Create initial state...'
  CALL inital
  IF (my_id .EQ. 1) WRITE(*,'(a/)') '...initial state created'

  !                   1.6     Initial diagnostics
  IF (my_id .EQ. 1) WRITE(*,*) 'Initial diagnostics...'
  CALL diagnose(0)
  IF (my_id .EQ. 1) WRITE(*,'(a/)') '...initial diagnostics done'

  CALL FLUSH(stdout)
  !________________________________________________________________________________

  IF (my_id .EQ. 1) WRITE(*,*) 'Time integration loop..'
  !________________________________________________________________________________
  !              2.   Main loop
  DO
CALL cpu_time(t0_step) ! Measuring time

     step  = step  + 1
     cstep = cstep + 1

     CALL stepon

     time  = time  + dt

     CALL tesend
     IF( nlend ) EXIT ! exit do loop

     CALL cpu_time(t0_diag) ! Measuring time
     CALL diagnose(step)
     CALL cpu_time(t1_diag); tc_diag = tc_diag + (t1_diag - t0_diag)


    CALL cpu_time(t1_step); tc_step = tc_step + (t1_step - t0_step)
  END DO
  IF (my_id .EQ. 1) WRITE(*,'(a/)') '...time integration done'
  !________________________________________________________________________________
  !              9.   Epilogue

  CALL diagnose(-1)
  CALL endrun

  IF (my_id .EQ. 1) CALL daytim('Done at ')

  CALL ppexit

END SUBROUTINE control
