!-------------------------------------- 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_tl -  TLM of dynstep 
*
#include "model_macros_f.h"
*

      subroutine dynstep_tl 1,33
*
      implicit none
*
*author
*     M.Tanguay
*
*revision
* v2_10 - Tanguay M.        - initial MPI version
* v2_21 - Tanguay M.        - 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
*     TLM of dynstep
*
*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 )
*
      itraj = Schm_itraj
*
      if ( first_L) then
         call pospers_tl( )
         itraj = max( 5, Schm_itraj )
      endif
*
*     ------------------------------------------------------------
*     Compute 3D winds at time t0-dt/2 (th)
*     ------------------------------------------------------------
*
      call tmg_start0 (80, 'BCS_TL' )
      if ( G_lam .and. .not.Lam_ctebcs_L ) then
*
*         ------------------------------------------------
*         Recover TRAJ NESTING fields at current time step
*         ------------------------------------------------
          V4dg_rwnest = 0
          call tmg_start0 (81, 'BC0_TL' )
          call v4d_rwnest ()
          call tmg_stop0 (81)
*
          if (V4dg_bc_variant.eq.0) then
*
*             ----------------------------------------
*             Zero NESTING fields at current time step
*             ----------------------------------------
              call v4d_zeronest
*
          else
*
              call tmg_start0 (82, 'BC1_TL' )
              call v4d_bc_2_nest (Lctl_step)
              call tmg_stop0 (82)
*
          endif
*
      endif
      call tmg_stop0 (80)
*
      if ( Orh_crank_L ) then
*
*        ---------------------------------------
*        DUMMY TIME STEPS AND WIND INTERPOLATION
*        ---------------------------------------
*
*        First guess for winds and positions at th
*        -----------------------------------------
         call wdpers_tl( )
*
         if (Lun_debug_L) write(Lun_out,1005) Schm_itcn-1
         do Orh_icn = 1,Schm_itcn-1
*
*	    Perform dummy timestep
*           ----------------------
            call tstpdyn_tl( itraj)
*
*	    Compute the average of the t1 and t0 winds
*           ------------------------------------------  
            call wndth_tl( )
*
            itraj = Schm_itraj
*
         end do
         if (Lun_debug_L) write(Lun_out,1006)
*
         Orh_icn=Schm_itcn
*     
      else
*
*        ------------------
*        WIND EXTRAPOLATION
*        ------------------
*
*        Perform extrapolation
*        ---------------------
         call extrwnd_tl( )
*
         Orh_icn = 1
*
      endif
*
*     ------------------------------------------------------------
*     Perform the timestep
*     ------------------------------------------------------------
      call tstpdyn_tl ( Schm_itraj )
*
*     ------------------------------------------------------------
*     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
*     ------------------------------------------------------------
      call t12t2_tl( )
      call t02t1_tl( )
      call t22t0_tl( )
      if (Schm_modcn.eq.Step_total) call t02tw_tl( )
*
      call tmg_start0 (80, 'BCS_TL' )
      if (G_lam) then
*
*        -------------------------------------
*        Impose Lateral BCs within Pil regions
*        -------------------------------------
*
         call tmg_start0 (83, 'BC2_TL' )
         call v4d_nest_bcs
         call tmg_stop0 (83)
*
*        ---------------------------------------------------
*        Perform Horizontal blending within the Hblen region
*        ---------------------------------------------------
         call tmg_start0 (84, 'BC3_TL' )
         call v4d_nest_blen
         call tmg_stop0 (84)
*
*        Recall TRAJ predictives variables AFTER nesting and blending
*        ------------------------------------------------------------
         call tmg_start0 (85, 'BC4_TL' )
         call v4d_rwtraj (14)
         call tmg_stop0 (85)
*
*        Set VMM dependent variables
*        ---------------------------
         call tmg_start0 (86, 'BC5_TL' )
         call v4d_set_vmm_dep_tl
         call tmg_stop0 (86)
*
      endif
      call tmg_stop0 (80)
*
*     ===================
*     END OF ONE TIMESTEP
*     ===================
*
 1000 format(
     +/,'TLM of CONTROL OF DYNAMICAL STEP: (S/R DYNSTEP_TL)',
     +/,'=================================================='/)
 1005 format(
     $/3X,'TLM of ##### Crank-Nicholson iterations: ===> PERFORMING',I3,
     $    ' timestep(s) #####'/)
 1006 format(
     $/3X,'TLM of ##### Crank-Nicholson iterations: ===> DONE... #####'/)
*
      return
      end