!-------------------------------------- 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 ENS2CORNS 3
#if defined (DOC)
*
***s/r ENS2CORNS  -  Obtain the estimate of the spectral correlations
*     .              standard deviations from the ensemble sum.
*
*
*Author  : P. Gauthier *ARMA/AES  December 1996
*Revision:
*          M. Buehner ARMA May 2008
*          - added index to CORNS
*
*
*    -------------------
**    Purpose:
*
*Arguments
*    -NONE-
#endif
      IMPLICIT NONE
*
*     Global variables
*
#include "comdim.cdk"
#include "comlun.cdk"
#include "comcorr.cdk"
#include "comcse1.cdk"
*
*     Local variables
*
      INTEGER JN, JK1, JK2, JK
      REAL*8 ZLFACT, ZLFACT2
      REAL*8 DSUMMED
*
*     1. Extracting the standard deviations from
*     .  the covariance matrix
*
      DO JN = 0, NTRUNC
         DO JK1 = 1, NKSDIM
            RSTDDEV(JK1,JN) = DSQRT(ABS(CORNS(JK1,JK1,JN,1)))
         END DO
      END DO
*
*     2.  Normalize the correlations with respect to
*     .   these standard deviations
*
      DO JN = 0, NTRUNC
         DO JK1 = 1, NKSDIM
            DO JK2 = 1, NKSDIM
               IF(RSTDDEV(JK1,JN).NE.0..AND.RSTDDEV(JK2,JN).NE.0.) THEN
                  CORNS(JK1,JK2,JN,1) =  CORNS(JK1,JK2,JN,1)
     +                 /(RSTDDEV(JK1,JN)*RSTDDEV(JK2,JN))
               ELSE
                  CORNS(JK1,JK2,JN,1) = 0
               END IF
            END DO
         END DO
      END DO
*
*     3.  Normalize the spectral variances according to the size
*     .   of the ensemble
*
      WRITE(NULOUT, 9300)NENSEMBLE
 9300 FORMAT(///,20X,"Estimation of the correlations is completed"
     S     ,". Total size of the  ensemble: ", I4)
      ZLFACT = DSQRT(DBLE(NENSEMBLE) - 1)
      ZLFACT2 = 1.0/ZLFACT
      DO JN = 0, NTRUNC
         ZLFACT = SQRT(2.0D0*JN + 1.0D0)
         ZLFACT = 1.0D0/ZLFACT
         DO JK1 = 1, NKSDIM
            RSTDDEV(JK1,JN) = RSTDDEV(JK1,JN)*ZLFACT2*ZLFACT
         END DO
      END DO
*
*     4. Normalize to ensure correlations in horizontal
*     .  and Multiply by sqrt(0.5) to make valid for m.ne.0
*
      DO JK=1,NKSDIM
        DSUMMED=0.0
        DO JN=0,NTRUNC
          DSUMMED=DSUMMED + (RSTDDEV(JK,JN)**2)*((2.0D0*JN)+1.0D0)/2.0D0
        ENDDO
        DO JN=0,NTRUNC
          IF(DSUMMED.NE.0.0)
     +      RSTDDEV(JK,JN)=RSTDDEV(JK,JN)*SQRT(0.5D0/DSUMMED)
        ENDDO
      ENDDO
C
      RETURN
      END