<jupyter_start><jupyter_text>ATSC-500 Assignment III (scale heights & TPW)<jupyter_code>import numpy as np
import pandas as pd
import netCDF4 as nc
from glob import glob
from os.path import basename
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
% matplotlib inline<jupyter_output><empty_output><jupyter_text>Import data<jupyter_code>csv_names = glob('_data/ATSC-500/std_soundings/*.csv')

sound_dict={}
for name in csv_names:
    print('importing '+name)
    sound_dict[basename(name[:-4])] = pd.read_csv(name)
# print out column names
sound_dict['midsummer'].head()<jupyter_output><empty_output><jupyter_text>Assignment i - calculate pressure & density scale heightReference: [hydrostatic balance notes](https://clouds.eos.ubc.ca/~phil/courses/atsc500/docs/hydro.pdf)**Pressure scale height**---------------Apply *reference equ.2* into the definition of pressure scale height ($H_p$):$$H_p = - \left(\frac{d\log{p}}{dz}\right)^{-1} =  \frac{R_dT_v}{g}$$The average of $H_p$ in the lower atmosphere:$$\overline{H_p} = \frac{1}{z_1 - z_0}\int^{z_1}_{z_0}{H_p dz} = \frac{R_d}{(z_1 - z_0)g}\int^{z_1}_{z_0}{T_v dz}$$**Density scale height**---------------Density scale height ($H_{\rho}$) is defined as:$$H_\rho = -\left(\frac{d\log{\rho}}{dz}\right)^{-1}$$Apply log and z-derivative to the ideal gas law: $$p = \rho R_dT_v \Rightarrow \frac{d\log{p}}{dz} = \frac{d\log{\rho}}{dz} + \frac{d\log{T_v}}{dz}$$Which brings the reltion between $H_p$ and $H_\rho$ $$\frac{1}{H_\rho} = \frac{1}{H_p} + \frac{1}{T_v}\frac{dT_v}{dz} = \frac{1}{H_p} + \frac{1}{T_v}\Gamma$$<jupyter_code>def Tv_midpoint(T, RMIX):
    Tv = (1+0.608*RMIX)*T
    return (Tv[1:] + Tv[:-1])/2.

def Hp_ave(T, Z, RMIX, thres=1e4):
    g = 9.8
    Rd = 286.888
    flag = (Z <= thres)
    Tv = Tv_midpoint(T[flag], RMIX[flag])
    return Rd*(Tv*np.diff(Z[flag])).sum()/(Z[flag][-1]-Z[flag][0])/g

def Hr_ave(T, Z, RMIX, thres=1e4):
    g = 9.8
    Rd = 286.888
    flag = (Z <= thres)
    Tv = Tv_midpoint(T[flag], RMIX[flag])
    dz = np.diff(Z[flag])
    gamma = 1*np.diff((1+0.608*RMIX[flag])*T[flag])/dz
    Hr = 1/(1/(Rd*Tv/g) + (1/Tv)*gamma)
    return (Hr*dz).sum()/(Z[flag][-1]-Z[flag][0])
for area in sound_dict.keys():
    df = sound_dict[area]
    T = df['temp'].as_matrix()
    Z = df['z'].as_matrix()
    RMIX = df['rmix'].as_matrix()
    print(area+': Hp = {:.4f}m, Hr = {:.4f}m'.format(Hp_ave(T, Z, RMIX, thres=1e4), Hr_ave(T, Z, RMIX, thres=1e4)))<jupyter_output>midsummer: Hp = 7815.2694m, Hr = 9525.8942m
midwinter: Hp = 7280.9117m, Hr = 8617.1956m
subwinter: Hp = 7006.3113m, Hr = 7999.8264m
tropics: Hp = 7915.6647m, Hr = 9833.4523m
subsummer: Hp = 7566.0687m, Hr = 9289.5719m<jupyter_text>Assignment ii - calculate total precipitable watertotal precipitable water ($W$) is defined as:$$W = \int_0^{z_{top}} \rho_v dz$$Apply idea gas law on water vapor to the definition of absolute humidity ($\rho_v$):$$\rho_v = \frac{m(\mathrm{H_2O})}{V(\mathrm{air})} = \frac{eM(\mathrm{H_2O})}{RT}\frac{V(\mathrm{H_2O})}{V(\mathrm{air})}$$Where $R$ is the ideal gas law constant with pressure unit as Pa, $\rho_v$ has the unit of $g/m^3$ Since water vapor is equally distributed in the air:$$V(\mathrm{H_2O}) = V(\mathrm{air})$$Then$$\rho_v = \frac{eM(\mathrm{H_2O})}{RT} = \frac{18}{8.314}\frac{e}{T} = 2.1650\frac{e}{T}$$According to *Dalton's law of partial pressure*:$$e = \frac{\gamma}{\gamma+\epsilon}p \quad \epsilon=\frac{M(\mathrm{H_2O})}{M(\mathrm{air})}=0.622$$<jupyter_code>def rho_midpoint(RMIX, P, T):
    P_midpoint = (P[1:] + P[:-1])/2.
    T_midpoint = (T[1:] + T[:-1])/2.
    RMIX_midpoint = (RMIX[1:] + RMIX[:-1])/2.
    e = RMIX_midpoint/(RMIX_midpoint+0.622)*P_midpoint
    return 2.1650*e/T_midpoint

def TPW(RMIX, P, T, Z, thres=1e4):
    flag = (Z <= thres)
    rho = rho_midpoint(RMIX[flag], P[flag], T[flag])
    dz = np.diff(Z[flag])
    return (rho*dz).sum()/(Z[flag][-1]-Z[flag][0])
    
for area in sound_dict.keys():
    df = sound_dict[area]
    T = df['temp'].as_matrix()
    Z = df['z'].as_matrix()
    P = df['press'].as_matrix()
    RMIX = df['rmix'].as_matrix()
    print(area+': total precipitable water = {} g/m2'.format(TPW(RMIX, P, T, Z)))<jupyter_output>midsummer: total precipitable water = 2.9537241007742856 g/m2
midwinter: total precipitable water = 0.9046481753742888 g/m2
subwinter: total precipitable water = 0.4658669610888579 g/m2
tropics: total precipitable water = 4.131246232351342 g/m2
subsummer: total precipitable water = 2.107768842955067 g/m2
{'fraction_non_alphanumeric': 0.20520231213872833, 'fraction_numerical': 0.06425077812361049, 'mean_word_length': 4.446731234866828, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 15, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '484895', 'license': 'Unlicense', 'n_tokens_mistral': 2096, 'n_tokens_neox': 1889, 'n_words': 372, 'path': 'ATSC_500/ATSC_500_Assignment_III_scale_heights_TPW.ipynb', 'repo_name': 'yingkaisha/Homework', 'size': '4498'}