#Metview Macro

#  **************************** LICENSE START ***********************************
# 
#  Copyright 2020 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 ************************************
# 

#=================================================================================
# Computes the vertical velocity in m/s from pressure velocity
#
# OneLineDesc   : Computes the vertical velocity in m/s from pressure velocity
#
# Input: 
#       omega: the pressure velocity (Pa/s)
#       t: the temperature (K)
#       p: the pressure (Pa)
# Return:
#       the vertical velocity (m/s)          
#==============================================================================

function w_from_omega(omega, t)
    fn_name = "w_from_omega"   
    p = __get_pressure_from_pl_arg(omega, "w", fn_name)       
    return w_from_omega(omega, t, p)  
      
end w_from_omega

function w_from_omega(omega, t, p)

    fn_name = "w_from_omega"   
    has_pl_fields = 0
    
    if type(omega) = "fieldset" then
        v = __prepare_pressure_field_arg(omega, p, "w", fn_name)
        p = v[1]
        has_pl_fields = v[2]
    end if     
   
    # The actual computation   
    RD = 287.058 #Gas constant
    g = 9.81 # gravitational acceleration
  
    if has_pl_fields then
        w = nil
        for i=1 to count(omega) do
            w = w & (-omega[i] * RD * t[i] / (g*p[i])) 
        end for
    else  
        w = -omega * RD * t / (g*p)
    end if
  
    return w

end w_from_omega
