!-------------------------------------- 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 --------------------------------------
***S/R MFSLESMX  -  MIXED PHASE SATURATION VAPOR PRESSURE CALCULATION
*
#include "phy_macros_f.h"

      SUBROUTINE MFDLESMX(RES,TT,FF,DF,NI,NK) 2,5
*
#include "impnone.cdk"
*
      INTEGER NI,NK
      REAL RES(NI,NK),TT(NI,NK),FF(NI,NK),DF(NI,NK)
*
*Author
*          A. Plante (May 2003), based on FDLESMX from J. Mailhot
*
*Revision
*
*Object
*          To calculate mixed phase saturation vapor pressure
*
*Arguments
*
*          - Output -
* RES      mixed phase saturation vapor pressure
*
*          - Input -
* TT       temperature
* FF       ice fraction
* DF       value of derivative w/r to T for saturation w/r to ice or liquid
* NI       horizontal dimension
* NK       vertical dimension
*
**
*
#include "consphy.cdk"
*
      INTEGER I,K
      REAL*8 MFOEWA
*
************************************************************************
*     AUTOMATIC ARRAYS
************************************************************************
*
      AUTOMATIC ( WORK8 , REAL*8 , (NI,NK) )
*
************************************************************************
*
#include "dintern.cdk"
#include "fintern.cdk"
*
*MODULES
*     
C     COMPUTE FOEWA WITH MASS LIBRARY
!      FOEWA(TTT)=610.78D0*DEXP(17.269D0*(DBLE(TTT)-DBLE(TRPL))/
!     W (DBLE(TTT)-35.86D0))
      DO K=1,NK
         DO I=1,NI
            WORK8(I,K)=17.269D0*(DBLE(TT(I,K))-DBLE(TRPL))/
     $           (DBLE(TT(I,K))-35.86D0)
         ENDDO
      ENDDO
      CALL VEXP(WORK8,WORK8,NI*NK)
      
      DO K=1,NK
         DO I=1,NI
            MFOEWA=610.78D0*WORK8(I,K)
            RES(I,K)= ( (1.D0-DBLE(FF(I,K)))*MFOEWA*FODLA(TT(I,K))
     1           + DBLE(FF(I,K))*FESI(TT(I,K))*FDLESI(TT(I,K))
     1           + DBLE(DF(I,K))*(FESI(TT(I,K))-MFOEWA) )/
     1           FESMX(TT(I,K),FF(I,K))
         ENDDO
      ENDDO
*     
      RETURN
      CONTAINS
#include "fintern90.cdk"
      END