C $Header: /u/gcmpack/MITgcm/pkg/ecco/ecco_cost_init_fixed.F,v 1.2 2003/11/17 22:32:33 heimbach Exp $

#include "PACKAGES_CONFIG.h"
#include "AD_CONFIG.h"
#include "COST_CPPOPTIONS.h"

      subroutine ECCO_COST_INIT_FIXED( mythid )

c     ==================================================================
c     SUBROUTINE ecco_cost_init_fixed
c     ==================================================================
c
c     o Set contributions to the cost function and the cost function
c       itself to zero. The cost function and the individual contribu-
c       tions are defined in the header file "ecco_cost.h".
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     changed: Ralf Giering 18-Jan-2001
c
c              - move namelist reading to cost_readparms.F
c
c     ==================================================================
c     SUBROUTINE ecco_cost_init_fixed
c     ==================================================================

      implicit none

c     == global variables ==

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

#include "cal.h"
#include "ecco_cost.h"

c     == routine arguments ==

      integer mythid

c     == local variables ==

      integer tempDate1(4)
      integer tempDate2(4)

      logical exst

c     == external functions ==

      integer  cal_IntMonths
      external 
      integer  cal_IntDays
      external 

c     == end of interface ==

#ifdef ALLOW_CAL

c--   The number of monthly and daily averages generated by the
c--   current model integration.
      nmonsrec = cal_IntMonths( mythid )
      ndaysrec = cal_IntDays( mythid )

      _BEGIN_MASTER( myThid )

c--     Get the complete dates of the ...
c--     ... TMI data.
        call CAL_FULLDATE( tmistartdate1,   tmistartdate2,
     &                     tmistartdate,    mythid )
c--     ... SST data.
        call CAL_FULLDATE( sststartdate1,   sststartdate2,
     &                     sststartdate,    mythid )
c--     ... SSS data.
        call CAL_FULLDATE( sssstartdate1,   sssstartdate2,
     &                     sssstartdate,    mythid )     
c--     ... T/P data.
        call CAL_FULLDATE( topexstartdate1, topexstartdate2,
     &                     topexstartdate,  mythid )
c--     ... ERS data.
        call CAL_FULLDATE( ersstartdate1,   ersstartdate2,
     &                     ersstartdate,    mythid )
c--     ... SCAT data.
        call CAL_FULLDATE( scatstartdate1,  scatstartdate2,
     &                     scatxstartdate,  mythid )
        call CAL_FULLDATE( scatstartdate1,  scatstartdate2,   
     &                     scatystartdate,  mythid )
c--     ... ARGO data.
        call CAL_FULLDATE( argotstartdate1,   argotstartdate2,
     &                     argotstartdate,    mythid )
        call CAL_FULLDATE( argosstartdate1,   argotstartdate2,
     &                     argosstartdate,    mythid )

c--   Sea-ice cost function start and stop times in model seconds.

        call CAL_FULLDATE  ( costIceStart1 , costIceStart2,
     &                       tempDate1     , mythid )
        call CAL_TIMEPASSED( modelstartdate, tempDate1    ,
     &                       tempDate2     , mythid )
        call CAL_TOSECONDS ( tempDate2 , costIceStart , mythid )
        costIceStart = costIceStart + startTime

        call CAL_FULLDATE  ( costIceEnd1   , costIceEnd2  ,
     &                       tempDate1     , mythid )
        call CAL_TIMEPASSED( modelstartdate, tempDate1    ,
     &                       tempDate2     , mythid )
        call CAL_TOSECONDS ( tempDate2 , costIceEnd , mythid )
        costIceEnd = costIceEnd + startTime

      _END_MASTER( mythid )

#endif /* ALLOW_CAL */

c--   Get the weights that are to be used for the individual cost
c--   function contributions.
      call ECCO_COST_WEIGHTS( mythid )

c--   Initialise adjoint of monthly mean files calculated
c--   in cost_averagesfields (and their ad...).
cph(
cph   The following init. shoud not be applied if in the middle
cph   of a divided adjoint run
cph)
#ifndef ALLOW_TANGENTLINEAR_RUN
cph!!! and I think it needs to be seen by TAF
cph!!! for repeated TLM runs
cph!!!
      inquire( file='costfinal', exist=exst )
      if ( .NOT. exst) then
         call ECCO_COST_INIT_BARFILES( mythid )
      endif
#endif

c--   Summarize the cost function's setup.
      _BEGIN_MASTER( mythid )
      call ECCO_COST_SUMMARY( mythid )
      _END_MASTER( mythid )

      _BARRIER

      end