!-------------------------------------- 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) 3
#if defined (DOC)
*
***s/r MATSQRT     - Calculate square root of an error covariance
*     .              matrix
*
*
*Author  : M. Buehner *ARMA/AES  February, 2000
*Revision:
*           C. Charette ARMA/SMC jan. 2005
*            - Replaced write(6,..) statements by write(nulout,...)
*           Bin He   *ARMA/MRB    NOv. 2011
*            - replaced MXMA with DGEMUL of ESSL  .
*
*Arguments
*     .  PA(KN,KN)     :  on entry, the original matrix
*     .                   on exit,  the sqrt     matrix
*     .  KN            : order of the matrix
#endif
      IMPLICIT NONE
*
* Global variables
*
#include "comlun.cdk"
#include "dgemul.h"
* Arguments
*
      INTEGER KN
      REAL*8 PA(KN,KN)
      REAL*8 ZSIGN
*
* Local variables
*
      INTEGER JI, J, INFO, IER, IWORK
      REAL*8 ZWORK(1), ZRESULT(KN,KN), ZEIGENV2(KN,KN),
     +       ZEIGEN(KN,KN), ZEIGENV(KN)
      POINTER(PXEIG  ,ZEIGEN  )
      POINTER(PXEIGV ,ZEIGENV )
      POINTER(PXEIGV2,ZEIGENV2)
      POINTER(PXWRK,ZWORK)
      POINTER(PXRES,ZRESULT)
*
*     0.  Memory allocation and miscellaneous definitions
*
      CALL HPALLOC(PXEIG  ,KN*KN,IER,8)
      CALL HPALLOC(PXEIGV ,KN   ,IER,8)
      CALL HPALLOC(PXEIGV2,KN*KN,IER,8)
      CALL HPALLOC(PXWRK  ,4*KN ,IER,8)
      CALL HPALLOC(PXRES  ,KN*KN,IER,8)
      IWORK=4*KN
*
      WRITE(NULOUT,*)' MATSQRT-Sqrt matrix of a symmetric matrix'
      WRITE(NULOUT,*)'  -----------------------------------------------'
      WRITE(NULOUT,*)'  '
*
c      IF(ZSIGN.lt.0) THEN
c        WRITE(NULOUT,*) 'BEFORE'
c        WRITE(NULOUT,*)( (PA(JI,J),JI=1,KN),J=1,KN)
c      ENDIF
*
*     1. Computation of eigenvalues and eigenvectors
*

 100  CONTINUE
      ZEIGEN(:,:)=PA(:,:) 
      CALL DSYEV('V','U',KN, ZEIGEN,KN, ZEIGENV,ZWORK, IWORK, INFO )
*
      WRITE(69,'(1x,"ORIGINAL EIGEN VALUES: ")')
      WRITE(69,'(1x,10f15.8)') (ZEIGENV(JI),JI=1,KN)
*
*     2.  Take SQRT of eigenvalues
*
      ZEIGENV2(:,:)= 0.0
      DO JI=1,KN
        ZEIGENV2(JI,JI)= ZEIGENV(JI)**(0.5*ZSIGN)
      END DO
*
!ping      CALL MXMA8(ZEIGEN ,1,KN,ZEIGENV2,1 ,KN,ZRESULT,1,KN,KN,KN,KN)
      CALL DGEMUL(ZEIGEN,KN,'N',ZEIGENV2,KN,'N',ZRESULT,KN,KN,KN,KN) 
!ping      CALL MXMA8(ZRESULT,1,KN,ZEIGEN  ,KN,1 ,PA     ,1,KN,KN,KN,KN)
      CALL DGEMUL(ZRESULT,KN,'N',ZEIGEN,KN,'N',PA,KN,KN,KN,KN)
*
c      IF(ZSIGN.lt.0) THEN
c        WRITE(*,*) 'AFTER'
c        WRITE(*,*)( (PA(JI,J),JI=1,KN),J=1,KN)
c      ENDIF
*
*     4. Deallocate local arrays
*
      CALL HPDEALLC(PXEIG  ,IER,1)
      CALL HPDEALLC(PXEIGV ,IER,1)
      CALL HPDEALLC(PXEIGV2,IER,1)
      CALL HPDEALLC(PXWRK  ,IER,1)
      CALL HPDEALLC(PXRES  ,IER,1)
*
      WRITE(NULOUT,*)'MATSQRT-----------Done--------------- '
      WRITE(NULOUT,*)' '
c 1244 FORMAT( 15(1x,f15.6))

      RETURN
      END