!-------------------------------------- LICENCE BEGIN ------------------------------------
!Environment Canada - Atmospheric Science and Technology License/Disclaimer,
!                     version 3; Last Modified: May 7, 2008.
!This is free but copyrighted software; you can use/redistribute/modify it under the terms
!of the Environment Canada - Atmospheric Science and Technology License/Disclaimer
!version 3 or (at your option) any later version that should be found at:
!http://collaboration.cmc.ec.gc.ca/science/rpn.comm/license.html
!
!This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
!without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
!See the above mentioned License/Disclaimer for more details.
!You should have received a copy of the License/Disclaimer along with this software;
!if not, you can write to: EC-RPN COMM Group, 2121 TransCanada, suite 500, Dorval (Quebec),
!CANADA, H9P 1J3; or send e-mail to service.rpn@ec.gc.ca
!-------------------------------------- LICENCE END --------------------------------------
!

      subroutine endsim2(kdim,px) 2,6
      USE procs_topo,only : myid 
      implicit none
*
* Author : Simon Pellerin (Nov. 2001)
* Purpose: Send an end of simulation signal to the TLM-ADJ unit
*          after minimisation completion
* Revisions: M. Buehner ARMA/MSC, August 2002
*            . Send restart signal with LRESTART is TRUE
*            M. Buehner ARMA, May 2008
*            . Change call to spa2gd for NANALVAR=4
*            S. Pellerin August 2008
*            . Added calls to 'tmg_*' subroutines
*
      include 'prof_f.h'
#include "comlun.cdk"
* lrestart
#include "comvfiles.cdk"
* ltlmend
#include "comdim.cdk"
#include "comcva.cdk"

      integer kdim
      real*8 px(kdim)

c     Local variables
      integer :: ihdl,istat,prof_wrrec
      character (len=128) :: clprof

c     Opening Prof_File

      IF(myid /= 0) RETURN

      write(nulout,*)
     &     'ENDSIM: Sending an end of simulation signal...'

      IF(LRESTART) THEN
c Restart
        WRITE(NULOUT,*)'ENDSIM - SENDING EVN_ESRS SIGNAL :'
     &       ,EVN_ESRS
        clprof = trim(CEXC4DV) // '/dwgf.prof'
        IHDL = PROF_OPEN(clprof,'WRITE','FILE')
        ISTAT = PROF_PVAR(IHDL,EVN_ESRS,PRM_EVNT)
        call tmg_start(81,'PROF_R+W')
        istat = prof_wrrec(ihdl)
        istat = prof_close(ihdl)
        call tmg_stop(81)
        clprof = trim(CEXC4DV) // '/evnt.prof'
        IHDL = PROF_OPEN(clprof,'WRITE','FILE')
        WRITE(NULOUT,*)'ENDSIM - SENDING EVN_ESRS SIGNAL :'
     &       ,EVN_ESRS
        ISTAT = PROF_PVAR(IHDL,EVN_ESRS,PRM_EVNT)
        call tmg_start(81,'PROF_R+W')
        istat = prof_wrrec(ihdl)
        ISTAT = PROF_CLOSE(IHDL)
        call tmg_stop(81)
      ELSEif(ltlmend) then
c Ask TLM program to realise one more integration before closing the
c shop
        WRITE(NULOUT,*)'ENDSIM - SENDING EVN_TLME SIGNAL :'
     &       ,EVN_TLME
        call tmg_start(51,'VAR_CH') !
        call cain(kdim,px)
        if(nanalvar.eq.4) then
          CALL SPA2GD
        else
          CALL SPA2SP
          CALL SPGD
        endif
        call tmg_stop(51)
        call putdx2('E')
      else
c Ask TLM-ADJ program to close the shop
        WRITE(NULOUT,*)'ENDSIM - SENDING EVN_ESIM SIGNAL :'
     &       ,EVN_ESIM
        clprof = trim(CEXC4DV) // '/dwgf.prof'
        IHDL = PROF_OPEN(clprof,'WRITE','FILE')
        ISTAT = PROF_PVAR(IHDL,EVN_ESIM,PRM_EVNT)
        call tmg_start(81,'PROF_R+W')
        istat = prof_wrrec(ihdl)
        istat = prof_close(ihdl)
        call tmg_stop(81)
        clprof = trim(CEXC4DV) // '/evnt.prof'
        IHDL = PROF_OPEN(clprof,'WRITE','FILE')
        WRITE(NULOUT,*)'ENDSIM - SENDING EVN_ESIM SIGNAL :'
     &       ,EVN_ESIM
        ISTAT = PROF_PVAR(IHDL,EVN_ESIM,PRM_EVNT)
        call tmg_start(81,'PROF_R+W')
        istat = prof_wrrec(ihdl)
        ISTAT = PROF_CLOSE(IHDL)
        call tmg_stop(81)
      ENDIF

      end subroutine endsim2