#include "PROFILES_OPTIONS.h"
#ifdef ALLOW_COST
#include "COST_OPTIONS.h"
#endif

CBOP
C     !ROUTINE: PROFILES_COST_FINAL
C     !INTERFACE:
      SUBROUTINE PROFILES_COST_FINAL( ifc, optimcycle, myThid )

C     !DESCRIPTION:
C     *==========================================================*
C     | SUBROUTINE PROFILES_COST_FINAL
C     *==========================================================*

C     !USES:
      IMPLICIT NONE

C     == global variables ==
#include "EEPARAMS.h"
#include "SIZE.h"
#include "PARAMS.h"
#ifdef ALLOW_COST
# include "cost.h"
#endif
#include "PROFILES_SIZE.h"
#include "profiles.h"

C     !INPUT/OUTPUT PARAMETERS:
C     ifc        :: file unit for costfunction_profiles.XXXX
C     optimcycle :: cycle number of the off-line optimization.
C     myThid     :: my Thread Id number
      INTEGER ifc
      INTEGER optimcycle
      INTEGER myThid

#ifdef ALLOW_COST
C     ! FUNCTIONS:
      INTEGER  ILNBLNK
      EXTERNAL ILNBLNK

C     !LOCAL VARIABLES:
      INTEGER bi, bj, num_var
      INTEGER num_file
      _RL f_profiles(NFILESPROFMAX,NVARMAX)
      _RL f_profiles_mean(NVARMAX)
      _RL no_profiles(NFILESPROFMAX,NVARMAX)
      _RL no_profiles_mean(NVARMAX)

      CHARACTER*26 cfname
      INTEGER IL
      CHARACTER*(MAX_LEN_MBUF) msgBuf
CEOP

C     Initialise cost function variables zo zero
      DO num_var=1,NVARMAX
       DO num_file=1,NFILESPROFMAX
        f_profiles(num_file,num_var)= 0. _d 0
        no_profiles(num_file,num_var)= 0. _d 0
       ENDDO
       f_profiles_mean(num_var)= 0. _d 0
       no_profiles_mean(num_var)= 0. _d 0
      ENDDO

c--   Sum up all contributions.
      DO bj = myByLo(myThid), myByHi(myThid)
       DO bi = myBxLo(myThid), myBxHi(myThid)

        DO num_var=1,NVARMAX
         DO num_file=1,NFILESPROFMAX
          tile_fc(bi,bj) = tile_fc(bi,bj)
     &            + mult_profiles(num_file,num_var)
     &            *objf_profiles(num_file,num_var,bi,bj)
          f_profiles(num_file,num_var)=f_profiles(num_file,num_var)
     &            +objf_profiles(num_file,num_var,bi,bj)
          no_profiles(num_file,num_var)=no_profiles(num_file,num_var)
     &            +num_profiles(num_file,num_var,bi,bj)
         ENDDO
         tile_fc(bi,bj) = tile_fc(bi,bj)
     &           + mult_profiles_mean(num_var)
     &           *objf_profiles_mean(num_var,bi,bj)
         f_profiles_mean(num_var)=f_profiles_mean(num_var)
     &           +objf_profiles_mean(num_var,bi,bj)
         no_profiles_mean(num_var)=no_profiles_mean(num_var)
     &           +num_profiles_mean(num_var,bi,bj)
        ENDDO

C-   end bi,bj loops
       ENDDO
      ENDDO

c--   Do global summation for each part of the cost function
      DO num_var=1,NVARMAX
       DO num_file=1,NFILESPROFMAX
        _GLOBAL_SUM_RL(f_profiles(num_file,num_var), myThid )
        _GLOBAL_SUM_RL(no_profiles(num_file,num_var), myThid )
       ENDDO
       _GLOBAL_SUM_RL(f_profiles_mean(num_var), myThid )
       _GLOBAL_SUM_RL(no_profiles_mean(num_var), myThid )
      ENDDO

C     start printing to STDOUT
      DO num_file=1,NFILESPROFMAX
       DO num_var=1,NVARMAX
        IF ( no_profiles(num_file,num_var).GT.zeroRL ) THEN
         WRITE(msgBuf,'(A,1PE22.14,2I3,1X,1PE9.2)')
     &     ' --> f_profiles =',f_profiles(num_file,num_var),
     &      num_file, num_var, mult_profiles(num_file,num_var)

         CALL PRINT_MESSAGE( msgBuf, standardMessageUnit,
     &                       SQUEEZE_RIGHT, myThid )
        ENDIF
       ENDDO
      ENDDO
      DO num_var=1,NVARMAX
       IF ( no_profiles_mean(num_var).GT.zeroRL ) THEN
        WRITE(msgBuf,'(A,1PE22.14,I3,1X,1PE9.2)')
     &     ' --> f_profiles_mean =',f_profiles_mean(num_var),
     &      num_var, mult_profiles_mean(num_var)

        CALL PRINT_MESSAGE( msgBuf, standardMessageUnit,
     &                      SQUEEZE_RIGHT, myThid )
       ENDIF
      ENDDO

c--   Each process has calculated the global part for itself.

C     only master thread of master CPU open and write to file
      IF ( ifc .NE. -1 ) THEN

       WRITE(cfname,'(A,I4.4)') 'costfunction_profiles.',optimcycle
       OPEN(unit=ifc,file=cfname)
       WRITE(msgBuf,'(A,A)')
     &      'Writing profiles cost function info to ', cfname
       CALL PRINT_MESSAGE( msgBuf, standardMessageUnit,
     &                     SQUEEZE_RIGHT, myThid )

       DO num_file=1,NFILESPROFMAX
        DO num_var=1,NVARMAX
         IF ( no_profiles(num_file,num_var).GT.zeroRL ) THEN
          IL = ILNBLNK( profilesfiles(num_file) )
          IL = MAX(IL,30)
          WRITE(ifc,'(4A,1PE22.14,1PE22.14,1X,1PE9.2)')
     &         profilesfiles(num_file)(1:IL),' ',
     &         prof_names(num_file,num_var), ' = ',
     &         f_profiles(num_file,num_var),
     &         no_profiles(num_file,num_var),
     &         mult_profiles(num_file,num_var)
         ENDIF
        ENDDO
       ENDDO
       DO num_var=1,NVARMAX
        IF ( no_profiles_mean(num_var).GT.zeroRL ) THEN
         WRITE(ifc,'(3A,1PE22.14,1PE22.14,1X,1PE9.2)')
     &        'profile_mean ', prof_names(1,num_var), ' = ',
     &        f_profiles_mean(num_var), no_profiles_mean(num_var),
     &        mult_profiles_mean(num_var)
        ENDIF
       ENDDO
       CLOSE(ifc)

      ENDIF

#endif /* ALLOW_COST */

      RETURN
      END
