# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python [default]
#     language: python
#     name: python3
# ---

# # Jan 31, 2017 class: Precipitation

# Import numerical tools
import numpy as np

# Here I will enter the weather data I downloaded for today.

TnoonF = 61 #Temperature at noon (really 1pm) in Farenheit
TnightF = 54 #Temperature at 4am in Farenheit
RHnoon = 0.62 #Relative humidity at noon (really 1pm)
RHnight = 0.96 #Relative humidity at 4 am
Tdp_noonF = 48 #Dew point at noon (really 1pm) in Farenheit
Tdp_nightF = 40 #Dew point at 4 am, Farenheit


# Let's convert all of the Farenheit data to Celsius by creating a module (i.e., a function).

def FtoC(TF):
    """
    Takes the temperature in Farenheit (TF) as input and returns it in Celsius (TC)
    """
    TC = (TF-32)*5/9
    return TC


# Now we'll apply that function to our temperature data in Farenheit

TnoonC = FtoC(TnoonF)
TnightC = FtoC(TnightF)
Tdp_noonC = FtoC(Tdp_noonF)
Tdp_nightC = FtoC(Tdp_nightF)
print(TnoonC) #Just checking to make sure it looks reasonable--always a good idea!

# What is today's pressure in Pa?

Pinches = 30.12 #pressure in inches
Pmm = 30.12*25.4 #There are 25.4 mm in an inch
PPa = Pmm*133.322
print(PPa)


# Now let's make a module for the Clausius-Clapeyron approximation.

def CC_esat(TC):
    """
    Takes the temperature in Celsius (TC) as input and returns the 
    saturation vapor pressure in kPa
    """
    esat = 0.61078*np.exp(17.27*TC/(TC+237.3)) #the "e" function comes from the numpy library
    return esat


# Now we can feed this equation today's temperatures to calculate the saturation vapor pressure.

esat_noon = CC_esat(TnoonC) #saturation vapor pressure at noon
esat_night = CC_esat(TnightC) #saturation vapor pressure at 4 am
print(esat_noon)
print(esat_night)

# Let's now figure out actual vapor pressure from the relative humidity.

ed_noon = esat_noon*RHnoon #in kPa
ed_night = esat_night*RHnight #in kPa
print(ed_noon)
print(ed_night)

# Finally, let us convert the vapor pressures to mass in grams. Humidity in g/m<sup>3</sup> is called the **absolute humidity.** We will do this using the ideal gas law, p=$\rho$RT. We need to solve this for the molar density and then convert it to a mass density.

# +
#First, let's enter some constants.
R = 8.314 #m3 Pa K−1 mol−1 We're going to need to do some more units conversions!
rho_noon = (ed_noon*1000)/(R*(TnoonC+273.15)) #now this is in moles/m^3
rho_night = (ed_night*1000)/(R*(TnightC+273.15))

#Now we will convert the molar densities to mass densities
MW_H2O = 18.01528 #grams of water per mole
m_noon = rho_noon*MW_H2O #grams per m^3
m_night = rho_night*MW_H2O
print(m_noon)
print(m_night)
# -

# Is the atmosphere at equilibrium with respect to water vapor content? Let's find out by solving for the actual vapor pressure from the dewpoint. Note that we can do this by plugging the dewpoint in to our saturation vapor pressure approximator above.

ed_act_noon = CC_esat(Tdp_noonC) #in kPa
ed_act_night = CC_esat(Tdp_nightC) #in kPa
print(ed_act_noon)
print(ed_act_night)

# What might be some causes of the discrepancy? Hint: Think about water balances.



StarcoderdataJupyterScriptsDedupFiltered