!-------------------------------------- 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 ZLEGINV2(PF,PN,PLEG,KTRUNC,KNJ,KLEV 4
     S     ,KNJDIM,KLEVDIM,KNDIM)
#if defined (DOC)
*
***s/r ZLEGINV2  - Direct Legendre transform restricted to
*     .            fields that vary with latitude only
*
*Author  : P. Gauthier *ARMA/AES  February 1997
*Revision:
*     . S. Pellerin *ARMA/AES February 2000
*       zleginv -> zleginv2: 1) supression of unused DDWT parameter
*                            2) generalization to any number of fields
*                               (no assumed dimensioning to 4*klevdim)
*          JM Belanger CMDA/SMC  Aug 2000
*                   . 32 bits conversion
*                    (MXMA8 instead of MXMA)
*          Bin He   *ARMA/MRB*  Nov. 2011
*                   . replaced MXMA8 with DGEMUL of ESSL. 
*Arguments
*     o   PF(KNJDIM,KLEVDIM)  : field in physical space
*     i   PN(0:KNDIM, KLEVDIM): spectral coefficients
*     i   PLEG(0:KNDIM,KNJDIM): Legendre functions evaluated at the KNJ Gaussian
*     .                         latitudes
*     i   KNJ                 : number of Gaussian latitudes
*     i   KTRUNC              : spectral truncation
*     i   KLEV                : number of fields to transform
*     i   KNJDIM              : dimensioning of the field (in latitude)
*     i   KLEVDIM             : dimensioning of the field (in KLEV)
*     I   KNDIM               : dimensioning of the field (in KTRUNC)
#endif
#include "dgemul.h"
      IMPLICIT NONE
*
*     Arguments
*
      INTEGER KNJ, KLEV, KTRUNC, KNJDIM, KLEVDIM, KNDIM
      REAL*8    PF(KNJDIM,KLEVDIM), PN(0:KNDIM, KLEVDIM)
     S     ,  PLEG(0:KNDIM,KNJDIM)
*
*     Local variables
*
      INTEGER J, JN, ILEN, IERR
      REAL*8 ::   ZWORK(0:KTRUNC,KNJ)
*
*     0. Allocate workspace
*
*
*     1. Prepare the matrix used for the transform
*
      DO J = 1, KNJ
         DO JN = 0,KTRUNC
            ZWORK(JN,J) = PLEG(JN,J)
         END DO
      END DO
*
*     2. Do the transform
*
c       CALL MXMA8(ZWORK(0,1),KTRUNC+1,1,PN(0,1),1,KNDIM+1
c     S     ,PF(1,1),1,KNJDIM,KNJ,KTRUNC+1,KLEV)
!!===*   calling IBM ESSL library..
          CALL DGEMUL(ZWORK(0,1),KTRUNC+1,'T',PN(0,1),KNDIM+1,'N',
     +    PF(1,1),KNJDIM,KNJDIM,KTRUNC+1,KLEVDIM) 
*
      RETURN
      END