!-------------------------------------- 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 --------------------------------------
***s/r v4d_ctrlgrd - Control gradient of cost function 
*
#include "model_macros_f.h"
*

      subroutine v4d_ctrlgrd ()  2,3
*
      use v4d_prof, only: Pr_doobs, Pr_nevent 
*
      implicit none
*
*author N.Ek / M.Tanguay 
*
*revision
* v3_00 - N.Ek / M.Tanguay  - initial MPI version
* v3_01 - M.Tanguay         - add V4dg_sgvc_L 
* v3_11 - Tanguay M.        - No profiles when Pr_nevent=EVN_TLME
*                           - Read TRAJ for conv. moved to v4d_rwconv0
*                           - ADJ of digital filter
*
*object
*  see id section
*	
*arguments
*  none
*
*implicits
#include "v4dg.cdk"
#include "lctl.cdk"
#include "tr3d.cdk"
#include <prof_f.h>
#include "init.cdk"
#include "rstr.cdk"
*
      logical time0_L, time_L
*
      if(V4dg_conf/100.ne.1.or.V4dg_sgvc_L.or.Pr_nevent.eq.EVN_TLME) return
*
*     ----------------------------------------
*     Sensitivity analysis or Twin experiments
*     ----------------------------------------
      if(.not.V4dg_4dvar_L) then
*
*       Verify if INITIAL TIME requested
*       --------------------------------
        time0_L = Lctl_step.eq.0.and.V4dg_inv.ne.1
*
*       Verify if .NOT.INITIAL time requested
*       -------------------------------------
        time_L = Lctl_step.ne.0.and.mod(Lctl_step,V4dg_stepob).eq.0
        if(Init_balgm_L.and..not.Rstri_idon_L.and.Lctl_step.ge.(Init_dfnp-1)/2) time_L = .false.
*
        if(.not.(time0_L.or.time_L)) return
*
*       Evaluate gradient forcing
*       (NOTE: TRAJ is recovered in GRDSTEP)
*       ------------------------------------
        if(V4dg_ad_L) call v4d_grdstep ()
*
*     -----------------------------
*     4D-Var + exchange with 3D-Var
*     -----------------------------
      elseif(V4dg_4dvar_L) then
*
*       Verify if INITIAL TIME requested
*       --------------------------------
        time0_L = Lctl_step.eq.0.and.Pr_doobs(0).gt.0
*
*       Verify if .NOT.INITIAL time requested
*       -------------------------------------
        time_L = Lctl_step.ne.0.and.Pr_doobs(Lctl_step).gt.0
        if(Init_balgm_L.and..not.Rstri_idon_L.and.Lctl_step.ge.(Init_dfnp-1)/2) time_L = .false.
*
        if(.not.(time0_L.or.time_L)) return
*
*       Adjoint of
*       Interpolate model states at obs. locations and
*       output the result to be read by 3D-Var 
*       ----------------------------------------------
        call tmg_start ( 54,'GRDJOK' )
        if(V4dg_ad_L) call v4d_grdjok() 
        call tmg_stop ( 54 )
*
      endif
*
      return
      end