# -*- coding: utf-8 -*-
# (c) Jan Schween 2005 (gnuplot)
# (c) Mario Mech 2009 (python)
# (c) Maximilian Maahn 2011 (python)
from __future__ import absolute_import, division, print_function
import numpy as np
from . import constants, density, temperature
# from .due import due, Doi
__all__ = ['a2e', 'e2a', "e2q", "q2e", "rh2q", "rh2a", "rh_to_iwv",
"e_sat_gg_ice", "e_sat_gg_water", "q2rh", "a2rh"]
[docs]def a2e(a, T):
"""
Calculate water vapor pressure from the absolute humidity and air
temperature.
Parameters
----------
a:
absolute humidity [kg / m3]
T:
Temperature in K
Returns
-------
float :
vapor pressure [Pa]
"""
e = a * T * constants.Rvapor
return e
[docs]def e2a(e, T):
"""
Calculate the absolute humidity from water vapor pressure and air
temperature.
Parameters
----------
e:
vapor pressure [Pa]
T:
Temperature in K
Returns
-------
float :
absolute humidity [kg / m3]
"""
a = e / (T * constants.Rvapor)
return a
[docs]def e_sat_gg_water(T):
"""
Calculates the saturation pressure over water after "Guide to
Meteorological Instruments and Methods of Observation" (CIMO Guide)
(WMO, 2008).
Parameters
----------
T:
Temperature in K
Returns
-------
float :
saturation pressure [Pa]
"""
T = temperature.kelvin_2_celsius(T)
e_sat_gg_water = 100 * 6.112 * np.exp(17.62 * T / (243.12 + T))
return e_sat_gg_water
[docs]def e_sat_gg_ice(T):
"""
Calculates the saturation pressure over water after "Guide to
Meteorological Instruments and Methods of Observation" (CIMO Guide)
(WMO, 2008).
Parameters
----------
T:
Temperature in K
Returns
-------
float :
saturation pressure [Pa]
"""
T = temperature.kelvin_2_celsius(T)
e_sat_gg_ice = 100 * 6.112 * np.exp(22.46 * T / (272.62 + T))
return e_sat_gg_ice
[docs]def e2q(e, p):
"""
Calculate the specific humidity from vapor pressure and air
pressure.
Parameters
----------
e:
vapor pressure [Pa]
p:
pressure [Pa]
Returns
-------
float :
specific humidity [kg / kg]
"""
q = constants.Mwml * e / (p - (1 - constants.Mwml) * e)
return q
[docs]def q2e(q, p):
"""
Calculate water vapor pressure from the specific humidity and air
pressure.
Parameters
----------
q:
specific humidity [kg / kg]
p:
pressure [Pa]
Returns
-------
float :
vapor pressure [Pa]
"""
e = p / ((constants.Mwml / q)+1-constants.Mwml)
return e
[docs]def rh2q(rh, T, p, e_sat_func=e_sat_gg_water):
"""
Calculate the specific humidity from relative humidity, air temperature,
and pressure.
Parameters
----------
rh:
Relative humidity in Pa / Pa
T:
Temperature in K
p:
pressure [Pa]
e_sat_func: func, optional
Function to estimate the saturation pressure. E.g. e_sat_gg_water for
water and e_sat_gg_ice for ice.
Returns
-------
float :
specific humidity [kg / kg]
"""
eStar = e_sat_func(T)
e = rh*eStar
q = e2q(e, p)
del e, eStar
return q
[docs]def rh2a(rh, T, e_sat_func=e_sat_gg_water):
"""
Calculate the absolute humidity from relative humidity, air temperature,
and pressure.
Parameters
----------
rh:
Relative humidity in Pa / Pa
T:
Temperature in K
e_sat_func: func, optional
Function to estimate the saturation pressure. E.g. e_sat_gg_water for
water and e_sat_gg_ice for ice.
Returns
-------
float :
absolute humidity [kg / m3]
"""
e = rh*e_sat_func(T)
a = e / (constants.Rvapor*T)
return a
[docs]def a2rh(a, T, e_sat_func=e_sat_gg_water):
"""
Calculate the absolute humidity from relative humidity, air temperature,
and pressure. Source: Kraus, 'Die Atmosphäre der Erde', Chapter 8.1.2
Parameters
----------
a:
absolute humidity [kg / m3]
T:
Temperature in K
e_sat_func: func, optional
Function to estimate the saturation pressure. E.g. e_sat_gg_water for
water and e_sat_gg_ice for ice.
Returns
-------
float :
relative humidity [kg / kg]
"""
e = a*(constants.Rvapor * T)
rh = e / e_sat_func(T)
return rh
[docs]def q2rh(q, T, p, e_sat_func=e_sat_gg_water):
"""
Calculate relative humidity from specific humidity. Source: Kraus, 'Die
Atmosphäre der Erde', Chapter 8.1.2
Parameters
----------
q:
specific humidity [kg / kg]
T:
Temperature in K
p:
pressure [Pa]
e_sat_func: func, optional
Function to estimate the saturation pressure. E.g. e_sat_gg_water for
water and e_sat_gg_ice for ice.
Returns
-------
float :
relative humidity [kg / kg]
"""
e = p / (constants.Mwml *
((1 / q)+(1 / (constants.Mwml) - 1)))
eStar = e_sat_func(T)
rh = e / eStar
return rh
def r2q(r):
'''mixing ratio to specific humidty
Source: http://glossary.ametsoc.org/wiki/Specific_humidity
Parameters
----------
r : mixing ratio [kg/kg]
Returns
-------
float :
specific humidity [kg / m^2]
'''
return r/(1+r)
[docs]def rh_to_iwv(relhum_lev, temp_lev, press_lev, hgt_lev):
"""
Integrate relative humidity to obtain the integrated water vapor (IWV)
column.
Parameters
----------
relhum_lev:
relative humidity at levels humidity [Pa / Pa]
temp_lev:
Temperature at levels [K]
press_lev:
pressure at levels [Pa]
hgt_levels:
altitude of levels [m]
Returns
-------
float :
IWV [kg / m^2]
"""
dz = np.diff(hgt_lev, axis=-1)
relhum = (relhum_lev[..., 0:-1] + relhum_lev[..., 1:]) / 2.
temp = (temp_lev[..., 0:-1] + temp_lev[..., 1:]) / 2.
xp = -1.*np.log(press_lev[..., 1:] / press_lev[..., 0:-1]) / dz
press = -1.*press_lev[..., 0:-1] / xp*(np.exp(-xp*dz)-1.) / dz
q = rh2q(relhum, temp, press)
rho_moist = density.moist_rho_q(press, temp, q)
return np.sum(q*rho_moist*dz)