!-------------------------------------- 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 STAGWINDS(KULOUT) 3,7
#if defined (DOC)
*     
***   s/r STAGWINDS  - Spectral interpolation of wind field to GEM's staggered grid
*     .                (option available in 4D-Var mode only)
*     
*     
*     Author  : P. Gauthier *ARMA/MSC  May, 2003
*     Revision:
*     Arguments:
*     .   KULOUT: logical unit used for printouts
*
#endif
      use modstag, only: level2_staggrid, nj_s
      IMPLICIT NONE
*     implicits
#include "comdim.cdk"
#include "comcst.cdk"
#include "comleg.cdk"
#include "comsp.cdk"
#include "comsp1.cdk"
#include "comgd0.cdk"
#include "comgd1.cdk"
*
*     Arguments
      integer :: kulout
                                !
                                ! Local variables
                                !
      real*8 :: dldeuxpi, dldellon, dltrans
      real*8 ::dlcostrans(0:ntrunc), dlsintrans(0:ntrunc),dla,dlb
      integer :: ila, ila0, jm, jk, jn
      !
      write(kulout,fmt='(/4x,A,/,8x,A)')
     S     'STAGWINDS- Staggering of the wind field by half a grid length'
     S     ,'(done directly on the spectral coefficients SPVOR and SPDIV)'
                                !
      level2_staggrid = .false.
      call transfer('SP01')     ! Set the spectral coefficients aside
                                !
                                !     - Preparing coefficients for translation 
                                !       by Dx/2 for complete wind field (u only for now)
      dldeuxpi = 2.d0*rpi
      dldellon = dldeuxpi/dble(ni)
      dltrans = dldellon/2.d0
      do jm = 0, ntrunc
         dlcostrans(jm) = cos(jm*dltrans)
         dlsintrans(jm) = sin(jm*dltrans)
      end do
*     !        
*     ! Modify spectral coefficients for translation in longitude
*     !        
      do jm = 0, ntrunc
         ila0 = nind(jm)
         do jn = jm, ntrunc
            ila = ila0 + (jn-jm)
            do jk = 1, 2*nflev
               dla = sp(ila,1,jk)*dlcostrans(jm)- sp(ila,2,jk)*dlsintrans(jm)
               dlb = sp(ila,1,jk)*dlsintrans(jm)+ sp(ila,2,jk)*dlcostrans(jm)
               sp(ila,1,jk) = dla
               sp(ila,2,jk) = dlb
            end do
         end do
      end do
                                !
      call spgd                 ! Transform to grid-point space
                                ! (winds staggered on U-grid while all other fields are left intact)
      call transfer ('GD01')
      call transfer ('ZSP0')
                                ! Restore original spectral coefficients
                                ! for vorticity and divergence only
      sp(:,:,1:2*nflev) = sp1(:,:,1:2*nflev)
                                !
      level2_staggrid = .TRUE.
      call spgd                 ! Spectral tranform (winds only) on staggered-grid in latitude
      level2_staggrid = .FALSE.
*     
*     Overwrite the meridional wind field component with the staggered wind field
*
      vt1(:,1:nflev,:) = 0.d0
      vt1(nibeg:niend,1:nflev,1:nj_s) 
     S     =  vt0(nibeg:niend,1:nflev,1:nj_s)
                                !
      call transfer ('GD10')    ! Put the modified grid-point model state into main
                                ! grid register (comgd0)
                                !
      END SUBROUTINE STAGWINDS