!--------------------------------------- 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 MATSQRT(PA,KN,ZSIGN) 2
#if defined (DOC) 
*
***s/r MATSQRT     - Calculate square root of an error covariance
*     .              matrix
*
*Author  : M. Buehner *ARMA/AES  February, 2000
*Revision:
*
*Arguments
*     .  PA(KN,KN)     :  on entry, the original matrix
*     .                   on exit,  the sqrt     matrix
*     .  KN            : order of the matrix
#endif
      IMPLICIT NONE
*
* Arguments
*
      INTEGER KN
      REAL*8 PA(KN,KN),ZSIGN
*
* Local variables
*
      INTEGER JI, J, INFO, IER, IWORK
      REAL*8 ZWORK(4*KN), ZRESULT(KN,KN), ZEIGENV2(KN,KN),
     +       ZEIGEN(KN,KN), ZEIGENV(KN)
*
      IWORK=4*KN
*
      WRITE(*,*)' MATSQRT-Sqrt matrix of a symmetric matrix'
      WRITE(*,*)' zsign= ',zsign
      WRITE(*,*)'  -----------------------------------------------'
*
*     1. Computation of eigenvalues and eigenvectors
*
      DO JI=1,KN
         DO J=1,KN
            ZEIGEN(JI,J)=PA(JI,J)
         END DO
      END DO
      CALL DSYEV('V','U',KN, ZEIGEN,KN, ZEIGENV,ZWORK, IWORK, INFO )
*
      WRITE(*,'(1x,"ORIGINAL EIGEN VALUES: ")') 
      WRITE(*,'(1x,10f7.3)') (ZEIGENV(JI),JI=1,KN) 
*
*     2.  Take SQRT of eigenvalues
*
      DO JI=1,KN
        DO J=1,KN
          ZEIGENV2(JI,J)= 0.0d0
        END DO
      END DO
      DO JI=1,KN
        ZEIGENV2(JI,JI)= ZEIGENV(JI)**(0.5d0*ZSIGN)
      END DO
*
      WRITE(*,'(1x,"SQRT OF ORIGINAL EIGEN VALUES: ")') 
      WRITE(*,'(1x,10f7.3)') (ZEIGENV2(JI,JI),JI=1,KN) 
*
      CALL DGEMM('N','N',KN,KN,KN,1.0d0,ZEIGEN,KN,ZEIGENV2,KN,
     +           0.0D0 ,ZRESULT,KN)

      CALL DGEMM('N','T',KN,KN,KN,1.0D0,ZRESULT,KN,ZEIGEN,KN,
     +           0.0d0,PA,KN)


*
*     4. Deallocate local arrays
*
      WRITE(*,*)'MATSQRT-----------Done--------------- '
      WRITE(*,*)' '

      RETURN
      END