I'm trying to make a weather simulator using the article Interactive Meso-scale Simulation of Skyscapes. It gives the formula for calculating the mean static energy:
$$MSE_i = C_p \cdot T_i + g \cdot \Theta_i + L_v \cdot Q_i$$
But I don't understand how to calculate specific humidity, the article says that it can be calculated using absolute humidity ($H_i$) and height above the ground ($\Theta_i$) (quote: $Q_i$ is the specific humidity of the air layer (and can be derived from $H_i$ and $\Theta_i$)). Is there any specific formula for calculating specific humidity or am I missing something?
Update 1.
So I have implemented a function to calculate specific humidity in python. Here AH is the absolute humidity in $g/m^3$, p is the pressure in pascals at the calculation point and t is temperature in Celsius.
def get_specific_humidity(AH, t, p):
e = (AH / 1000) * 461.5 * (t + 273.15)
r = (0.622 * e) / (p - e)
return r / (1 + r)
First I calculated the vapor pressure e using the formula: $$\frac{m}{V} = \frac{e}{R_vT}$$ The calculation assumes that the dry air is evenly mixed with water. Also $R_v=461.5$ is specific gas constant for water vapor. Next, I calculated the mixing ratio using this equation:
$$r=\frac{0.622e}{p-e}$$
And finally using the mixing ratio it is possible to calculate the specific humidity: $$q=\frac{r}{1+r}$$
Did I do everything right?