!-------------------------------------- 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 dynstep_ad -  ADJ of dynstep_tl 
*
#include "model_macros_f.h"
*

      subroutine dynstep_ad 1,35
*
      implicit none
*
*author
*     M.Tanguay
*
*revision
* v2_10 - Tanguay M.        - initial MPI version
* v2_21 - Tanguay M.        - ADJ of HO option 
*                           - ADJ of vertical sponge layer 
* v2_31 - Tanguay M.        - adapt ADJ to diffusion in gem_run 
* v3_03 - Tanguay M.        - Adjoint Lam configuration 
* v3_30 - Tanguay M.        - Adapt TL/AD to Lam_ctebcs_L
* v3_31 - Tanguay M.        - Control BC
* v3_31 - Tanguay M.        - Introduce time extrapolation
*
*object
*     ADJ of dynstep_tl 
*
*arguments
*     none
*
*implicits
#include "glb_ld.cdk"
#include "lun.cdk"
#include "orh.cdk"
#include "init.cdk"
#include "lctl.cdk"
#include "rstr.cdk"
#include "schm.cdk"
#include "lam.cdk"
#include "v4dg.cdk"
#include "v4dg_bc.cdk"
#include "step.cdk"
*
      integer itraj
      logical first_L, secon_L
**
*     ---------------------------------------------------------------
      if (Lun_debug_L) write(Lun_out,1000)
*
*     first_L is TRUE  for the first timestep
*           or the first timestep after digital filter initialisation
*
      first_L = (Lctl_step.eq.1).or.(Rstri_idon_L .and. 
     $           Lctl_step.eq.(Init_dfnp+1)/2)
*
*     secon_L is TRUE  for the second timestep
*            or the second timestep after digital filter initialisation
*
      secon_L = (Lctl_step.eq.2).or.(Rstri_idon_L .and. 
     $           Lctl_step.eq.(Init_dfnp+1)/2 + 1)
*
*     Orh_crank_L is TRUE for a time step using C-N procedure
*
      Orh_crank_L= first_L .or. ( secon_L .and. ( Schm_xwvt3 .gt. 0 )) 
     $                     .or. ( mod(Lctl_step-1,Schm_modcn) .eq. 0 )
*
*     ----------------
*     START TRAJECTORY
*     ----------------
      Orh_icn = Schm_itcn
      if (.not.Orh_crank_L) Orh_icn = 1
*
      call tmg_start0 (90, 'BCS_AD' )
*     Recover TRAJ NESTING fields at current time step
*     ------------------------------------------------
      if ( G_lam .and. .not.Lam_ctebcs_L ) then
*
          call tmg_start0 (91, 'BC0_AD' )
          V4dg_rwnest = 0
          call v4d_rwnest ()
          call tmg_stop0 (91)
*
      endif
      call tmg_stop0 (90)
*
*     --------------
*     END TRAJECTORY
*     --------------
*
      call tmg_start0 (90, 'BCS_AD' )
      if (G_lam) then
*
*        Recall TRAJ predictives variables AFTER nesting and blending
*        ------------------------------------------------------------
         call tmg_start0 (92, 'BC4_AD' )
         call v4d_rwtraj (14)
         call tmg_stop0 (92)
*
*        ADJ of
*        Set VMM dependent variables
*        ---------------------------
         call tmg_start0 (93, 'BC5_AD' )
         call v4d_set_vmm_dep_ad
         call tmg_stop0 (93)
*
*        ADJ of
*        Perform Horizontal blending within the Hblen region
*        ---------------------------------------------------
         call tmg_start0 (94, 'BC3_AD' )
         call v4d_nest_blen_ad
         call tmg_stop0 (94)
*
*        ADJ of
*        Impose Lateral BCs within Pil regions
*        -------------------------------------
         call tmg_start0 (95, 'BC2_AD' )
         call v4d_nest_bcs_ad
         call tmg_stop0 (95)
*
      endif
      call tmg_stop0 (90)
*
*     ADJ of
*     ------------------------------------------------------------
*     When the timestep is completed, rename all the
*     variables at time level t1 -> t0 and rename all the
*     variables at time level t0 -> t1 for the next timestep
*     ------------------------------------------------------------
      if (Schm_modcn.eq.Step_total) call t02tw_ad( )
      call t22t0_ad( )
      call t02t1_ad( )
      call t12t2_ad( )
*
*     ADJ of
*     ------------------------------------------------------------
*     Perform the timestep
*     ------------------------------------------------------------
      call tstpdyn_ad( Schm_itraj )
*
*     ADJ of
*     ------------------------------------------------------------
*     Compute 3D winds at time t0-dt/2 (th)
*     ------------------------------------------------------------
*
      if ( Orh_crank_L ) then
*
*        ---------------------------------------
*        ADJ of
*        DUMMY TIME STEPS AND WIND INTERPOLATION
*        ---------------------------------------
*
         if (Lun_debug_L) write(Lun_out,1005) Schm_itcn-1
*
         do Orh_icn = Schm_itcn-1,1,-1
*
*           ADJ of
*           Compute the average of the t1 and t0 winds
*           ------------------------------------------
            call wndth_ad( )
*
*           ADJ of
*           Perform dummy timestep
*           ----------------------
*
            itraj = Schm_itraj
            if( Orh_icn.eq.1 .and. first_L ) itraj = max( 5, Schm_itraj )
*
            call tstpdyn_ad( itraj)
*
         end do
         if (Lun_debug_L) write(Lun_out,1006)
*
*           ADJ of
*           First guess for winds and positions at th
*           -----------------------------------------
            call wdpers_ad( )
*
      else
*
*        ------------------
*        ADJ of
*        WIND EXTRAPOLATION
*        ------------------
*
*        ADJ of
*        Perform extrapolation
*        ---------------------
         call extrwnd_ad( )
*
      endif
*
      call tmg_start0 (90, 'BCS_AD' )
      if ( G_lam .and. .not.Lam_ctebcs_L ) then
*
          if (V4dg_bc_variant.eq.0) then
*
*             ----------------------------------------
*             Zero NESTING fields at current time step
*             ----------------------------------------
              call v4d_zeronest
*
          else
*
              call tmg_start0 (96, 'BC1_AD' )
              call v4d_bc_2_nest_ad (Lctl_step)
              call tmg_stop0 (96)
*
          endif
*
      endif
      call tmg_stop0 (90)
*
      if ( first_L) then
         call pospers_ad( )
      endif
*
*     ===================
*     END OF ONE TIMESTEP
*     ===================
*
 1000 format(
     +/,'ADJ of CONTROL OF DYNAMICAL STEP: (S/R DYNSTEP_AD)',
     +/,'=================================================='/)
 1005 format(
     $3X,'ADJ of ##### Crank-Nicholson iterations: ===> PERFORMING',I3,
     $   ' timestep(s) #####',/)
 1006 format(
     $/3X,'ADJ of ##### Crank-Nicholson iterations: ===> DONE... #####',/)
*
      return
      end