#Metview Macro

#  **************************** LICENSE START ***********************************
# 
#  Copyright 2019 ECMWF. This software is distributed under the terms
#  of the Apache License version 2.0. In applying this license, ECMWF does not
#  waive the privileges and immunities granted to it by virtue of its status as
#  an Intergovernmental Organization or submit itself to any jurisdiction.
# 
#  ***************************** LICENSE END ************************************
# 
#=============================================================================
# Function      : equivalent_potential_temperature
#
# Syntax        : number equivalent_potential_temperature(t:number,td: number,p: number)
#                                          
# Category      : THERMODYNAMICS
#
# OneLineDesc   : Compute the equivalent potential temperature from initial conditions
#
# Description   : Compute the equivalent potential temperature for a given temperature,
#                 dewpoint temperature and pressure as initial conditions
#
# Parameters    : t - the temperature (K)
#                 t - the dewpoint temperature (K)
#		 		  p - the pressure (Pa)
#           
# Return Value  : the equivalent potential temperature (K)
#
# Dependencies  : none
#
#==============================================================================

function __equivalent_potential_temperature(t: number, td: number,p: number)

    b=2.674456
    
    # compute the potential temperature of the initial condition,
    # this is the same as at the LCL
    th_lcl = potential_temperature(t,p)
    
    # compute the temperature at the LCL
    lcl = lifted_condensation_level(t,td,p)   

    if lcl = nil then
        return nil
    end if    
    
    t_lcl = lcl.t
  
    # compute the saturation mixing ratio at the LCL 
    mr_lcl = saturation_mixing_ratio(td,p) * 1000 # g/kg
    
    return th_lcl*exp(b*mr_lcl/t_lcl)
	
end __equivalent_potential_temperature
