!-------------------------------------- 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 oda_sqrtB(da_v,na_vdim) 7,12
  use modstag, only : lstagwinds
  implicit none
  ! Dummies
  integer :: na_vdim ! dimension of the model state in error covariance space
  real*8, dimension(na_vdim) :: da_v ! model state in error covariance space
  !
  !Purpose:
  !Transforms model state from error covariance space to grid point space.
  !
  !Author  : S. Pellerin *ARMA/MRB January 2009
  !          Based on transformations involved in evaljo.ftn
  !          as written by P. Gauthier *ARMA/AES in June, 1993
  !
  !Revision:
  ! L. Fillion ARMA/MSC 14 Dec 2004
  !     .   Limited-Area Variational Analysis option: LAM4D.
  !     L. Fillion ARMA/EC May 2006: Upgrade to v10_0_0. - Tg not yet supported for LAM4D.
  !     L. Fillion ARMA/EC 14 Aug 2007: Upgrade to v10_0_3. - Tg not yet supported for LAM4D.
  !     L. Fillion ARMA/EC 1 May 2008: Upgrade to v_10_1_0 and include Shallow-Water option.
  !     L. Fillion ARMA/EC 8 Jan 2009: Upgrade to v_10_1_2
  !     L. Fillion ARMA/EC 5 May 2009: Upgrade to v_10_2_2
  !     L. Fillion ARMA/EC 13 Jul 2010: Introduce cv2gdgl for lcva_hemis .true.
  !
#include "comlun.cdk"
#include "comdim.cdk"
#include "comcva.cdk"
#include "comsv.cdk"
#include "comgrd_param.cdk"
  ! Local declarations

  call tmg_start(51,'VAR_CH') !
  CALL CAIN(na_vdim,da_v)
  !
  ! Conversion of analysis variables to spectral model state                    !
  if(grd_typ.eq.'GU') then
    if(nanalvar.eq.4) then
       CALL SPA2GD
    else
      if(lsw) then
        call spa2spsw
        call spgd
      else
        if(lcva_hemis) then
          call cv2gdgl
        else
          CALL SPA2SP
          call transfer('SP01')
          if(lstagwinds) then
            write(nulout,*) 'USING STAGWINDS!'
            call stagwinds(nulout) ! Winds are put directly on GEM's staggered grid
          else
            call spgd
          endif
        endif
      endif
    endif
  else if (grd_typ.eq.'LU') then
    call cv2gd
  endif
  !
  IF(NSVMODE.gt.0) then     !
     CALL SPA2SPSV           ! B matrix with Hessian singular vectors
  END IF
  !
  call tmg_stop (51)

end subroutine oda_sqrtB