C $Header: /u/gcmpack/MITgcm/pkg/ecco/cost_hyd.F,v 1.42 2015/11/24 21:26:31 gforget Exp $
C $Name:  $

#include "ECCO_OPTIONS.h"
#ifdef ALLOW_SEAICE
# include "SEAICE_OPTIONS.h"
#endif

      subroutine COST_HYD( myiter, mytime, mythid )

c     ==================================================================
c     SUBROUTINE cost_hyd
c     ==================================================================
c
c     o Evaluate cost function contributions of temperature, salt, and
c       sea surface temperature contributions.
c
c     started: Christian Eckert eckert@mit.edu 30-Jun-1999
c
c     changed: Christian Eckert eckert@mit.edu 25-Feb-2000
c
c              - Restructured the code in order to create a package
c                for the MITgcmUV.
c
c     ==================================================================
c     SUBROUTINE cost_hyd
c     ==================================================================

      implicit none

c     == global variables ==
#ifdef ECCO_CTRL_DEPRECATED
#include "EEPARAMS.h"
#include "SIZE.h"
#include "PARAMS.h"
#include "GRID.h"

#include "cal.h"
#include "CTRL_SIZE.h"
#include "ctrl.h"
#include "ctrl_dummy.h"
#include "ecco_cost.h"
#ifdef ALLOW_SEAICE
# include "SEAICE_COST.h"
#endif
#endif

c     == routine arguments ==

      integer myiter
      _RL     mytime
      integer mythid

#ifdef ECCO_CTRL_DEPRECATED
c     == local variables ==
      character*(max_len_mbuf) msgbuf

      integer nnzbar
      integer nnzobs
      integer nrecloc
      integer nmonsloc
      _RL spminloc
      _RL spmaxloc
      _RL spzeroloc
      _RL localperiod

      logical dodimensionalcost2D, dodimensionalcost3D

c     == end of interface ==

      dodimensionalcost2D=.NOT.ctrlSmoothCorrel2D
      dodimensionalcost3D=.NOT.ctrlSmoothCorrel3D

      IF (using_cost_sst) THEN
#if (defined (ALLOW_SST_COST_CONTRIBUTION))
      write(msgbuf,'(a)') 'ph-cost call cost_sst'
      call PRINT_MESSAGE( msgbuf, standardmessageunit,
     &     SQUEEZE_RIGHT , mythid)
      nnzbar = nnztbar
      nnzobs = 1
      spminloc = -1.8
      spmaxloc = 40.
      spzeroloc = 0.
      localperiod = 0.
      nmonsloc=nmonsrec
      call COST_GENERIC(
     &     nnzbar, tbarfile, xx_tbar_mean_dummy,
     &     nnzobs, sstdatfile, ssterrfile, mult_sst,
     &     nmonsloc, nmonsloc, sststartdate, localperiod,
     &     'C', spminloc, spmaxloc, spzeroloc,
     &     no_preproc, no_preproc_c, no_preproc_i, no_preproc_r, 
     &     no_posproc, no_posproc_c, no_posproc_i, no_posproc_r,
     &     0, sstdatfile, objf_sst, num_sst,
     &     myiter, mytime, mythid )
c
#elif (defined (ALLOW_DAILYSST_COST_CONTRIBUTION))
c
      write(msgbuf,'(a)') 'ph-cost call cost_sst'
      call PRINT_MESSAGE( msgbuf, standardmessageunit,
     &     SQUEEZE_RIGHT , mythid)
      nnzbar = 1
      nnzobs = 1
      nrecloc = ndaysrec
      spminloc = -2.
      spmaxloc = 40.
      spzeroloc = 0.
      localperiod = 86400.
      nmonsloc=nmonsrec
      call COST_GENERIC(
     &     nnzbar, sstbarfile, xx_sstbar_mean_dummy,
     &     nnzobs, sstdatfile, ssterrfile, mult_sst,
     &     nrecloc, nmonsloc, sststartdate, localperiod,
     &     'C', spminloc, spmaxloc, spzeroloc,
     &     no_preproc, no_preproc_c, no_preproc_i, no_preproc_r,
     &     no_posproc, no_posproc_c, no_posproc_i, no_posproc_r,
     &     0, sstdatfile, objf_sst, num_sst,
     &     myiter, mytime, mythid )
cph      call cost_sst   ( myiter, mytime, mythid )
#endif
      ENDIF

#ifdef ALLOW_ARGO_THETA_COST_CONTRIBUTION
      write(msgbuf,'(a)') 'ph-cost call cost_argo_theta'
      call PRINT_MESSAGE( msgbuf, standardmessageunit,
     &     SQUEEZE_RIGHT , mythid)
      nnzbar = Nr
      nnzobs = Nr
      spminloc = -1.8
      spmaxloc = 40.
      spzeroloc = 0.
      localperiod = 0.
      nmonsloc=nmonsrec
      call COST_GENERIC(
     &     nnzbar, tbarfile, xx_tbar_mean_dummy,
     &     nnzobs, argotfile, temperrfile, mult_argot,
     &     nmonsloc, nmonsloc, argotstartdate, localperiod,
     &     'C', spminloc, spmaxloc, spzeroloc,
     &     no_preproc, no_preproc_c, no_preproc_i, no_preproc_r, 
     &     no_posproc, no_posproc_c, no_posproc_i, no_posproc_r,
     &     0, argotfile, objf_argot, num_argot,
     &     myiter, mytime, mythid )
#endif

#ifdef ALLOW_CTDT_COST_CONTRIBUTION
      write(msgbuf,'(a)') 'ph-cost call cost_ctdt'
      call PRINT_MESSAGE( msgbuf, standardmessageunit,
     &     SQUEEZE_RIGHT , mythid)
      nnzbar = Nr
      nnzobs = Nr
      spminloc = -1.8
      spmaxloc = 40.
      spzeroloc = 0.
      localperiod = 0.
      nmonsloc=nmonsrec
      call COST_GENERIC(
     &     nnzbar, tbarfile, xx_tbar_mean_dummy,
     &     nnzobs, ctdtfile, temperrfile, mult_ctdt,
     &     nmonsloc, nmonsloc, ctdtstartdate, localperiod,
     &     'C', spminloc, spmaxloc, spzeroloc,
     &     no_preproc, no_preproc_c, no_preproc_i, no_preproc_r, 
     &     no_posproc, no_posproc_c, no_posproc_i, no_posproc_r,
     &     0, ctdtfile, objf_ctdt, num_ctdt,
     &     myiter, mytime, mythid )
#endif

#ifdef ALLOW_XBT_COST_CONTRIBUTION
      write(msgbuf,'(a)') 'ph-cost call cost_xbt'
      call PRINT_MESSAGE( msgbuf, standardmessageunit,
     &     SQUEEZE_RIGHT , mythid)
      nnzbar = Nr
      nnzobs = Nr
      spminloc = -1.8
      spmaxloc = 40.
      spzeroloc = 0.
      localperiod = 0.
      nmonsloc=nmonsrec
      call COST_GENERIC(
     &     nnzbar, tbarfile, xx_tbar_mean_dummy,
     &     nnzobs, xbtfile, temperrfile, mult_xbt,
     &     nmonsloc, nmonsloc, xbtstartdate, localperiod,
     &     'C', spminloc, spmaxloc, spzeroloc,
     &     no_preproc, no_preproc_c, no_preproc_i, no_preproc_r, 
     &     no_posproc, no_posproc_c, no_posproc_i, no_posproc_r,
     &     0, xbtfile, objf_xbt, num_xbt,
     &     myiter, mytime, mythid )
#endif

      IF (using_cost_sst) THEN
#ifdef ALLOW_TMI_SST_COST_CONTRIBUTION
      write(msgbuf,'(a)') 'ph-cost call cost_tmi'
      call PRINT_MESSAGE( msgbuf, standardmessageunit,
     &     SQUEEZE_RIGHT , mythid)
      nnzbar = nnztbar
      nnzobs = 1
      spminloc = -1.8
      spmaxloc = 40.
      spzeroloc = 0.
      localperiod = 0.
      nmonsloc=nmonsrec
      call COST_GENERIC(
     &     nnzbar, tbarfile, xx_tbar_mean_dummy,
     &     nnzobs, tmidatfile, ssterrfile, mult_tmi,
     &     nmonsloc, nmonsloc, tmistartdate, localperiod,
     &     'C', spminloc, spmaxloc, spzeroloc,
     &     no_preproc, no_preproc_c, no_preproc_i, no_preproc_r,
     &     no_posproc, no_posproc_c, no_posproc_i, no_posproc_r,
     &     0, tmidatfile, objf_tmi, num_tmi,
     &     myiter, mytime, mythid )
cph      call cost_tmi( myiter, mytime, mythid )
#endif
      ENDIF

#ifdef ALLOW_SSS_COST_CONTRIBUTION
      write(msgbuf,'(a)') 'ph-cost call cost_sss'
      call PRINT_MESSAGE( msgbuf, standardmessageunit,
     &     SQUEEZE_RIGHT , mythid)
      nnzbar = nnzsbar
      nnzobs = 1
      spminloc = 20.
      spmaxloc = 40.
      spzeroloc = 0.
      localperiod = 0.
      nmonsloc=nmonsrec
      call COST_GENERIC(
     &     nnzbar, sbarfile, xx_sbar_mean_dummy,
     &     nnzobs, sssdatfile, ssterrfile, mult_sss,
     &     nmonsloc, nmonsloc, sssstartdate, localperiod,
     &     'C', spminloc, spmaxloc, spzeroloc,
     &     no_preproc, no_preproc_c, no_preproc_i, no_preproc_r,
     &     no_posproc, no_posproc_c, no_posproc_i, no_posproc_r,
     &     0, sssdatfile, objf_sss, num_sss,
     &     myiter, mytime, mythid )
cph      call cost_sss   ( myiter, mytime, mythid )
#endif

#ifdef ALLOW_CTDS_COST_CONTRIBUTION
      write(msgbuf,'(a)') 'ph-cost call cost_ctds'
      call PRINT_MESSAGE( msgbuf, standardmessageunit,
     &     SQUEEZE_RIGHT , mythid)
      nnzbar = Nr
      nnzobs = Nr
      spminloc = 20.
      spmaxloc = 40.
      spzeroloc = 0.
      localperiod = 0.
      nmonsloc=nmonsrec
      call COST_GENERIC(
     &     nnzbar, sbarfile, xx_sbar_mean_dummy,
     &     nnzobs, ctdsfile, salterrfile, mult_ctds,
     &     nmonsloc, nmonsloc, ctdsstartdate, localperiod,
     &     'C', spminloc, spmaxloc, spzeroloc,
     &     no_preproc, no_preproc_c, no_preproc_i, no_preproc_r, 
     &     no_posproc, no_posproc_c, no_posproc_i, no_posproc_r,
     &     0, ctdsfile, objf_ctds, num_ctds,
     &     myiter, mytime, mythid )
#endif

#ifdef ALLOW_ARGO_SALT_COST_CONTRIBUTION
      write(msgbuf,'(a)') 'ph-cost call cost_argo_salt'
      call PRINT_MESSAGE( msgbuf, standardmessageunit,
     &     SQUEEZE_RIGHT , mythid)
      nnzbar = Nr
      nnzobs = Nr
      spminloc = 20.
      spmaxloc = 40.
      spzeroloc = 0.
      localperiod = 0.
      nmonsloc=nmonsrec
      call COST_GENERIC(
     &     nnzbar, sbarfile, xx_sbar_mean_dummy,
     &     nnzobs, argosfile, salterrfile, mult_argos,
     &     nmonsloc, nmonsloc, argosstartdate, localperiod,
     &     'C', spminloc, spmaxloc, spzeroloc,
     &     no_preproc, no_preproc_c, no_preproc_i, no_preproc_r, 
     &     no_posproc, no_posproc_c, no_posproc_i, no_posproc_r,
     &     0, argosfile, objf_argos, num_argos,
     &     myiter, mytime, mythid )
#endif

#ifdef ALLOW_THETA0_COST_CONTRIBUTION
      if (.NOT.ctrlUseGen) then
      write(msgbuf,'(a)') 'ph-cost call cost_theta0'
      call PRINT_MESSAGE( msgbuf, standardmessageunit,
     &     SQUEEZE_RIGHT , mythid)
      call CTRL_COST_GEN3D ( xx_theta_file,
     &     xx_theta_dummy, wthetaLev, dodimensionalcost3D,
     &     num_temp0, objf_temp0, maskC, myThid )
      endif
#endif

#ifdef ALLOW_SALT0_COST_CONTRIBUTION
      if (.NOT.ctrlUseGen) then
      write(msgbuf,'(a)') 'ph-cost call cost_salt0'
      call PRINT_MESSAGE( msgbuf, standardmessageunit,
     &     SQUEEZE_RIGHT , mythid)
      call CTRL_COST_GEN3D ( xx_salt_file,
     &     xx_salt_dummy, wsaltLev, dodimensionalcost3D,
     &     num_salt0, objf_salt0, maskC, myThid )
      endif
#endif

#ifdef ALLOW_THETA_COST_CONTRIBUTION
      write(msgbuf,'(a)') 'ph-cost call cost_theta'
      call PRINT_MESSAGE( msgbuf, standardmessageunit,
     &     SQUEEZE_RIGHT , mythid)
      nnzbar = Nr
      nnzobs = Nr
      spminloc = -1.8
      spmaxloc = 40.
      spzeroloc = 0.
      localperiod = 0.
      nmonsloc=nmonsrec
      call COST_GENERIC(
     &     nnzbar, tbarfile, xx_tbar_mean_dummy,
     &     nnzobs, tdatfile, temperrfile, mult_temp,
     &     nmonsloc, nmonsloc, modelstartdate, localperiod,
     &     'C', spminloc, spmaxloc, spzeroloc,
     &     clim_preproc, no_preproc_c, clim_preproc_i, no_preproc_r,
     &     no_posproc, no_posproc_c, no_posproc_i, no_posproc_r,
     &     0, tdatfile, objf_temp, num_temp,
     &     myiter, mytime, mythid )
#endif

#ifdef ALLOW_SALT_COST_CONTRIBUTION
      write(msgbuf,'(a)') 'ph-cost call cost_salt'
      call PRINT_MESSAGE( msgbuf, standardmessageunit,
     &     SQUEEZE_RIGHT , mythid)
      nnzbar = Nr
      nnzobs = Nr
      spminloc = 25.
      spmaxloc = 40.
      spzeroloc = 0.
      localperiod = 0.
      nmonsloc=nmonsrec
      call COST_GENERIC(
     &     nnzbar, sbarfile, xx_sbar_mean_dummy,
     &     nnzobs, sdatfile, salterrfile, mult_salt,
     &     nmonsloc, nmonsloc, modelstartdate, localperiod,
     &     'C', spminloc, spmaxloc, spzeroloc,
     &     clim_preproc, no_preproc_c, clim_preproc_i, no_preproc_r,
     &     no_posproc, no_posproc_c, no_posproc_i, no_posproc_r,
     &     0, sdatfile, objf_salt, num_salt,
     &     myiter, mytime, mythid )
#endif

c#ifdef ALLOW_SIGMAR_COST_CONTRIBUTION
c      write(msgbuf,'(a)') 'ph-cost call cost_sigmar'
c      call print_message( msgbuf, standardmessageunit,
c     &     SQUEEZE_RIGHT , mythid)
c      call cost_sigmar  ( myiter, mytime, mythid )
c#endif

c#ifdef ALLOW_TRANSPORT_COST_CONTRIBUTION
c      write(msgbuf,'(a)') 'ph-cost call cost_gen_transport'
c      call print_message( msgbuf, standardmessageunit,
c     &     SQUEEZE_RIGHT , mythid)
c      call cost_gen_transport  ( myiter, mytime, mythid )
c#endif

#ifdef ALLOW_ETAN0_COST_CONTRIBUTION
      write(msgbuf,'(a)') 'ph-cost call cost_etan0'
      call PRINT_MESSAGE( msgbuf, standardmessageunit,
     &     SQUEEZE_RIGHT , mythid)
      call CTRL_COST_GEN2D (
     &     1,1,xx_etan_file,xx_etan_dummy,zeroRL,
     &     wetan, dodimensionalcost2D, num_etan0, objf_etan0,
#ifdef ECCO_CTRL_DEPRECATED
     &     zeroRL, num_zero_mean, objf_zero_mean,
     &     objf_zero_smoo, zeroRL, zeroRL,
#endif /* ECCO_CTRL_DEPRECATED */
     &     maskC, myThid )
#endif

#ifdef ALLOW_UVEL0_COST_CONTRIBUTION
      write(msgbuf,'(a)') 'ph-cost call cost_uvel0'
      call PRINT_MESSAGE( msgbuf, standardmessageunit,
     &     SQUEEZE_RIGHT , mythid)
      call CTRL_COST_GEN3D (
     &     xx_uvel_file,xx_uvel_dummy, wuvel3d, dodimensionalcost3D,
     &     num_uvel0, objf_uvel0, maskW, myThid )
#endif
#ifdef ALLOW_VVEL0_COST_CONTRIBUTION
      write(msgbuf,'(a)') 'ph-cost call cost_vvel0'
      call PRINT_MESSAGE( msgbuf, standardmessageunit,
     &     SQUEEZE_RIGHT , mythid)
      call CTRL_COST_GEN3D (
     &     xx_vvel_file,xx_vvel_dummy, wvvel3d, dodimensionalcost3D,
     &     num_vvel0, objf_vvel0, maskS, myThid )
#endif

#ifdef ALLOW_SEAICE_COST_SMR_AREA
# ifdef ALLOW_SEAICE_COST_AREASST
cph this block out of date;
cph similar call moved to seaice_cost_driver and
cph and extended using I. Fenty SST, SSS extensions

      write(msgbuf,'(a)') 'ph-cost call seaice_cost_areasst'
      call PRINT_MESSAGE( msgbuf, standardmessageunit,
     &     SQUEEZE_RIGHT , mythid)
      nnzbar = 1
      nnzobs = 1
      nrecloc = ndaysrec
      spminloc = 0.
      spmaxloc = 1.
      spzeroloc = -9999.
      localperiod = 86400.
      call SEAICE_COST_AREASST(
     &     nnzbar, smrareabarfile, smrareabar, xx_smrareabar_mean_dummy,
     &     nnzobs, smrareadatfile, smrareadat, mult_smrarea,
     &     nrecloc, smrareastartdate, localperiod,
     &     maskC, wsmrarea,
     &     spminloc, spmaxloc, spzeroloc,
     &     objf_smrarea, num_smrarea,
     &     myiter, mytime, mythid )

      write(msgbuf,'(a,1P2E18.10)')
     & 'ph-cost seaice_cost_areasst ',
     & objf_smrarea(1,1), num_smrarea(1,1)
      call PRINT_MESSAGE( msgbuf, standardmessageunit,
     &     SQUEEZE_RIGHT , mythid)

# endif /* ALLOW_SEAICE_COST_AREASST */
#endif

#endif /* ECCO_CTRL_DEPRECATED */

      end