!-------------------------------------- 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 endsim 8,1
      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
*            S. Pellerin, ARMA, August 2008
*               - Added calls to 'tmg_*' subroutines
*
      include 'prof_f.h'
#include "comlun.cdk"
#include "comvfiles.cdk"
c     Local variables
      integer :: ihdl,istat,prof_wrrec
      character (len=128) :: clprof

      IF(myid /= 0) RETURN 
c     Opening Prof_File

      write(nulout,*)
     &     'ENDSIM: Sending an end of simulation signal...'
      clprof = trim(CEXC4DV) // '/dwgf.prof'
      IHDL = PROF_OPEN(clprof,'WRITE','FILE')
      IF(LRESTART) THEN
        ISTAT = PROF_PVAR(IHDL,EVN_ESRS,PRM_EVNT)
      ELSE
        ISTAT = PROF_PVAR(IHDL,EVN_ESIM,PRM_EVNT)
      ENDIF
      call tmg_start(81,'PROF_R+W')
      istat = prof_wrrec(ihdl)
      call tmg_stop(81)
      istat = prof_close(ihdl)

      clprof = trim(CEXC4DV) // '/evnt.prof'
      IHDL = PROF_OPEN(clprof,'WRITE','FILE')
      IF(LRESTART) THEN
        WRITE(NULOUT,*)'ENDSIM - SENDING EVN_ESRS SIGNAL :'
     &       ,EVN_ESRS
        ISTAT = PROF_PVAR(IHDL,EVN_ESRS,PRM_EVNT)
      ELSE
        WRITE(NULOUT,*)'ENDSIM - SENDING EVN_ESIM SIGNAL :'
     &       ,EVN_ESIM
        ISTAT = PROF_PVAR(IHDL,EVN_ESIM,PRM_EVNT)
      ENDIF
      call tmg_start(81,'PROF_R+W')
      istat = prof_wrrec(ihdl)
      call tmg_stop(81)
      ISTAT = PROF_CLOSE(IHDL)

      end subroutine endsim