!-------------------------------------- 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 ens2cornsla2 1
#if defined (DOC)
*
***s/r ens2cornsla2  -  Obtain the estimate of the spectral correlations
*     .                standard deviations from the ensemble sum.
*
*
*Author  : Luc Fillion - ARMA/EC - 04 Mar 2009 - Extended version of ens2cornsla.ftn to include Tb,psb
*Revision:
*
*
*    -------------------
**    Purpose:
*
*Arguments
*    -NONE-
#endif
      IMPLICIT NONE
*
*     Global variables
*
#include "taglam4d.cdk"
#include "comdim.cdk"
#include "comcst.cdk"
#include "comlun.cdk"
#include "comcorr.cdk"
#include "comcse1.cdk"
#include "comfftla.cdk"
*
*     Local variables
*
      INTEGER JN, JK1, JK2, JK, jband
      REAL*8 ZLFACT, ZLFACT2,zcon
      REAL*8 DSUMMED
*
*     1. Extracting the standard deviations from
*     .  the covariance matrix
*
      do jband = 1, nband
        do jk1 = 1, nksdim2
          rstddev(jk1,jband-1) = dsqrt(abs(corns(jk1,jk1,jband-1,1)))
        enddo
      enddo
*
*     2.  Normalize the spectral covariances w.r.t. these standard deviations
*     .   N.B.: No need to explicitly take nensemble into account here...
*
      do jband = 1, nband
        do jk1 = 1, nksdim2
          do jk2 = 1, nksdim2
               IF(RSTDDEV(JK1,jband-1).NE.0..AND.RSTDDEV(JK2,jband-1).NE.0.) THEN
                  CORNS(JK1,JK2,jband-1,1) =  CORNS(JK1,JK2,jband-1,1)
     +                 /(RSTDDEV(JK1,jband-1)*RSTDDEV(JK2,jband-1))
               ELSE
                  CORNS(JK1,JK2,jband-1,1) = 0
               END IF
          enddo
        enddo
      enddo
*
*     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 jband = 1, nband
         do jk1 = 1, nksdim2
            RSTDDEV(JK1,jband-1) = RSTDDEV(JK1,jband-1)*ZLFACT2
         enddo
      enddo
!
!     4. Normalize to ensure correlations in horizontal
!        (i.e. build normalised spectral densities of the variance)
!
      do jk=1,nksdim2
        DSUMMED=0.0
        do jband = 1, nband
          DSUMMED=DSUMMED
     &      + wvnbtot(jband)*(RSTDDEV(JK,jband-1)**2)  ! square because we took sqrt above...
        enddo
        do jband = 1, nband
          IF(DSUMMED.NE.0.0)
     +      RSTDDEV(JK,jband-1)=RSTDDEV(JK,jband-1)*SQRT(1.D0/DSUMMED)
        enddo
      enddo
!
!     Verifiy now and print 
!
      do jk=1,nksdim2
        DSUMMED=0.0
        do jband = 1, nband
          DSUMMED=DSUMMED
     &      + wvnbtot(jband)*(RSTDDEV(JK,jband-1)**2)  ! square because we took sqrt above...
        enddo
        write(nulout,*) 'ens2cornsla2: jk,DSUMMED=',jk,DSUMMED
      enddo
!
      do jband = 1, nband
        write(nulout,*) ' '
        write(nulout,*) 'ens2cornsla2: 1st Spectral Band'
        write(nulout,*) '******************************'
        write(nulout,*) 'ens2cornsla2: band,rstddev=',jband-1,rstddev(1,jband-1)
      enddo
        DSUMMED=0.0
        do jband = 1, nband
          DSUMMED=DSUMMED
     &      + wvnbtot(jband)*(RSTDDEV(1,jband-1)**2)
        enddo
        write(nulout,*) 'ens2cornsla2: For 1st Level,DSUMMED=',DSUMMED
C
      RETURN
      END