C $Header: /u/gcmpack/MITgcm/pkg/ecco/ecco_cost_driver.F,v 1.3 2010/11/22 23:48:33 jmc Exp $
C $Name:  $

#include "COST_CPPOPTIONS.h"

      subroutine ECCO_COST_DRIVER(  myTime, myIter, mythid  )

c     ==================================================================
c     SUBROUTINE ecco_cost_driver
c     ==================================================================
c
c     o Compute all ecco cost function contributions.
c
c     ==================================================================
c     SUBROUTINE ecco_cost_driver
c     ==================================================================

      implicit none

c     == global variables ==

#include "EEPARAMS.h"
#include "SIZE.h"
#include "PARAMS.h"

c     == routine arguments ==
      integer mythid
      integer myiter
      _RL     mytime

#ifdef ALLOW_ECCO_EVOLUTION

#ifdef ALLOW_AUTODIFF_TAMC
# ifdef ALLOW_SSH_COST_CONTRIBUTION
#  ifdef ALLOW_PROFILES
#   include "profiles.h"
#  endif
# endif
# ifdef ALLOW_MEAN_HFLUX_COST_CONTRIBUTION
#  include "ecco_cost.h"
# endif
#endif

#ifdef ALLOW_COST_ATLANTIC
c--   Compute meridional heat transport
#ifdef ALLOW_DEBUG
      IF (debugMode) CALL DEBUG_CALL('cost_atlantic',myThid)
#endif
      call TIMER_START('cost_atlantic      [ECCO SPIN-DOWN]', mythid)
      call COST_ATLANTIC(  mytime, myiter,mythid )
      call TIMER_STOP ('cost_atlantic      [ECCO SPIN-DOWN]', mythid)
#endif

c--   Compute the cost function contribution of the boundary forcing,
c--   i.e. heat flux, salt flux, zonal and meridional wind stress.
#ifdef ALLOW_DEBUG
      IF (debugMode) CALL DEBUG_CALL('cost_forcing',myThid)
#endif
      call TIMER_START('cost_forcing       [ECCO SPIN-DOWN]', mythid)
      call COST_FORCING( myiter, mytime, mythid )
      call TIMER_STOP ('cost_forcing       [ECCO SPIN-DOWN]', mythid)
cph(
c--   Compute cost function contribution of wind stress observations.
#ifdef ALLOW_MEAN_HFLUX_COST_CONTRIBUTION
      call COST_MEAN_HEATFLUX( myiter, mytime, mythid )
# ifdef ALLOW_AUTODIFF_TAMC
CADJ STORE objf_hfluxmm = tapelev_init, key=1
# endif
#endif

c--   Compute cost function contribution of wind stress observations.
#ifdef ALLOW_MEAN_SFLUX_COST_CONTRIBUTION
      call COST_MEAN_SALTFLUX( myiter, mytime, mythid )
# ifdef ALLOW_AUTODIFF_TAMC
CADJ STORE objf_sfluxmm = tapelev_init, key=1
# endif
#endif
cph)

c--   Compute cost function contribution of SSH.
#ifdef ALLOW_SSH_COST_CONTRIBUTION
# ifdef ALLOW_DEBUG
      IF (debugMode) CALL DEBUG_CALL('cost_ssh',myThid)
# endif
# if (defined(ALLOW_SSHV4_COST))
      call TIMER_START('cost_sshv4         [ECCO SPIN-DOWN]', mythid)
      call COST_SSHV4( myiter, mytime, mythid )
      call TIMER_STOP ('cost_sshv4         [ECCO SPIN-DOWN]', mythid)
# elif (defined(ALLOW_NEW_SSH_COST))
      call TIMER_START('cost_ssh_new       [ECCO SPIN-DOWN]', mythid)
      call COST_SSH_NEW( myiter, mytime, mythid )
      call TIMER_STOP ('cost_ssh_new       [ECCO SPIN-DOWN]', mythid)
 else
      call TIMER_START('cost_ssh           [ECCO SPIN-DOWN]', mythid)
      call COST_SSH( myiter, mytime, mythid )
      call TIMER_STOP ('cost_ssh           [ECCO SPIN-DOWN]', mythid)
# endif
# ifdef ALLOW_AUTODIFF_TAMC
#  ifdef ALLOW_PROFILES
CADJ STORE prof_etan_mean = tapelev_init, key=1
#  endif
# endif
#endif

#ifdef ALLOW_BP_COST_CONTRIBUTION
c--   Compute bottom pressure cost
      call TIMER_START('cost_bp    [ECCO SPIN-DOWN]', mythid)
      call COST_BP( myiter, mytime, mythid )
      call TIMER_STOP ('cost_bp    [ECCO SPIN-DOWN]', mythid)
#endif

c--   Compute cost function contribution of Temperature and Salinity.
#ifdef ALLOW_DEBUG
      IF (debugMode) CALL DEBUG_CALL('cost_hyd',myThid)
#endif
      call TIMER_START('cost_hyd           [ECCO SPIN-DOWN]', mythid)
      call COST_HYD( myiter, mytime, mythid )
      call TIMER_STOP ('cost_hyd           [ECCO SPIN-DOWN]', mythid)

#ifdef ALLOW_SEAICE
#ifdef ALLOW_DEBUG
      IF (debugMode) CALL DEBUG_CALL('seaice_cost_driver',myThid)
#endif
      IF ( useSeaice) THEN
      call TIMER_START('seaice_cost_driver [ECCO SPIN-DOWN]', mythid)
      call SEAICE_COST_DRIVER( myiter, mytime, mythid )
      call TIMER_STOP ('seaice_cost_driver [ECCO SPIN-DOWN]', mythid)
      ENDIF
#endif

#ifdef ALLOW_OBCS_COST_CONTRIBUTION
#ifdef ALLOW_DEBUG
      IF (debugMode) CALL DEBUG_CALL('cost_obcs',myThid)
#endif
      call TIMER_START('cost_obcs          [ECCO SPIN-DOWN]', mythid)
      call COST_OBCS( myiter, mytime, mythid )
      call TIMER_STOP ('cost_obcs          [ECCO SPIN-DOWN]', mythid)
#endif

#ifdef ALLOW_CURMTR_COST_CONTRIBUTION
#ifdef ALLOW_DEBUG
      IF (debugMode) CALL DEBUG_CALL('cost_curmtr',myThid)
#endif
      call TIMER_START('cost_curmtr        [ECCO SPIN-DOWN]', mythid)
      call COST_CURMTR( myiter, mytime, mythid )
      call TIMER_STOP ('cost_curmtr        [ECCO SPIN-DOWN]', mythid)
#endif

c--   Compute cost function contribution of drifter velocities.
#ifdef ALLOW_DRIFTER_COST_CONTRIBUTION
#ifndef ALLOW_AUTODIFF_WHTAPEIO
#ifdef ALLOW_DEBUG
      IF (debugMode) CALL DEBUG_CALL('cost_drifter',myThid)
#endif
      call TIMER_START('cost_drifter         [ECCO SPIN-DOWN]', mythid)
      call COST_DRIFTER( myiter, mytime, mythid )
      call TIMER_STOP ('cost_drifter         [ECCO SPIN-DOWN]', mythid)
#endif
#endif

c--   Compute cost function contribution of wind stress observations.
#if (defined (ALLOW_SCAT_COST_CONTRIBUTION)  
     defined (ALLOW_DAILYSCAT_COST_CONTRIBUTION) )
#ifdef ALLOW_DEBUG
      IF (debugMode) CALL DEBUG_CALL('cost_scat',myThid)
#endif
      call TIMER_START('cost_scat            [ECCO SPIN-DOWN]', mythid)
      call COST_SCAT( myiter, mytime, mythid )
      call TIMER_STOP ('cost_scat            [ECCO SPIN-DOWN]', mythid)
#endif

c--   Compute cost function contribution of drift between the first
c     and the last year.
#ifdef ALLOW_DRIFT_COST_CONTRIBUTION
#ifdef ALLOW_DEBUG
      IF (debugMode) CALL DEBUG_CALL('cost_drift',myThid)
#endif
      call TIMER_START('cost_drift         [ECCO SPIN-DOWN]', mythid)
      call COST_DRIFT( myiter, mytime, mythid )
      call TIMER_STOP ('cost_drift         [ECCO SPIN-DOWN]', mythid)
#endif
#ifdef ALLOW_DRIFTW_COST_CONTRIBUTION
#ifdef ALLOW_DEBUG
      IF (debugMode) CALL DEBUG_CALL('cost_driftw',myThid)
#endif
      call TIMER_START('cost_driftw        [ECCO SPIN-DOWN]', mythid)
      call COST_DRIFTW( myiter, mytime, mythid )
      call TIMER_STOP ('cost_driftw        [ECCO SPIN-DOWN]', mythid)
#endif
      _BARRIER

c--   Compute initial vs. final T/S deviation
#ifdef ALLOW_COST_INI_FIN
      call TIMER_START('cost_ini_fin       [ECCO SPIN-DOWN]', mythid)
      call COST_THETA_INI_FIN( myiter, mytime, mythid )
      call COST_SALT_INI_FIN( myiter, mytime, mythid )
      call TIMER_STOP ('cost_ini_fin       [ECCO SPIN-DOWN]', mythid)
#endif
      _BARRIER

c--   Internal Parameter controls cost terms:
      call TIMER_START('cost_internal_params  [ECCO SPIN-DOWN]', mythid)
      call COST_INTERNAL_PARAMS( myiter, mytime, mythid )
      call TIMER_STOP ('cost_internal_params  [ECCO SPIN-DOWN]', mythid)
      _BARRIER

c--   Compute user defined cost function contributions (elaborate)
      call TIMER_START('cost_gencost_all    [ECCO SPIN-DOWN]', mythid)
      call COST_GENCOST_ALL( myiter, mytime, mythid )
      call TIMER_STOP ('cost_gencost_all    [ECCO SPIN-DOWN]', mythid)

c--   Compute user defined cost function contributions (basic)
      call TIMER_START('cost_usercost_all    [ECCO SPIN-DOWN]', mythid)
      call COST_USERCOST_ALL( myiter, mytime, mythid )
      call TIMER_STOP ('cost_usercost_all    [ECCO SPIN-DOWN]', mythid)

#endif /* ALLOW_ECCO_EVOLUTION */

      return
      end