Python 2
Python 2
5
print(f'Turbulent Kinetic Energy = {tke:.4f} m^2/s^2')
Turbulent Scales
import numpy as np
#mean
wm = np.mean(w)
um = np.mean(u)
vm = np.mean(v)
wd = w - wm
ud = u - um
vd = v - vm
#friction velocity
rho = 1.293
tau_x = rho*(np.mean(wd*ud))
tau_y = rho*(np.mean( wd*vd))
#temperature scale
theta_star = -np.mean(wd*td)/ustar
print(f'Turbulence scale for temperature = {theta_star:.4f} k')
4. P4
Plotting Air Temperature Profile under DALR and MALR
import numpy as np
import matplotlib.pyplot as plt
tsurf = 24
dalr = 0.0098
malr = 0.0045
z = np.arange(0, 1001, 10)
td = tsurf - dalr * z
tm = tsurf - malr * z
plt.xlabel('Temperature (C)')
plt.ylabel('Altitude (m)')
plt.title('Vertical Air Temperature Profile')
plt.legend()
plt.show()
import netCDF4 as nc
import numpy as np
import matplotlib.pyplot as plt
d = nc.Dataset('/content/ecmwf-airt-2000-01-01-06.nc')
c = nc.Dataset('/content/ecmwf-airt-2000-01-01-15.nc')
tsurf_6 = t_6[0]
tsurf_15 = t_15[0]
dalr = 9.8
malr = 4.5
R = 8.314
M = 0.0289
g = 9.8
P0 = 1013.25
z_6 = (((R * tsurf_6) / (M * g)) * np.log(P0 / p)) / 1000
z_15 = (((R * tsurf_15) / (M * g)) * np.log(P0 / p)) / 1000
# 06:00 Hours
ax = fig.add_subplot(1, 2, 1)
ax.plot(t_6, z_6, label='Temperature profile at 06:00', color='blue', linewidth=2)
ax.plot(tdlr_6, z_6, linestyle='dashed', label='DALR (06:00)', color='grey')
ax.plot(tmlr_6, z_6, linestyle='dashed', label='MALR (06:00)', color='purple')
plt.xlabel('Temperature (K)')
plt.ylabel('Altitude (km)')
plt.title('Temperature Profile at 06:00 Hours')
plt.legend()
# 15:00 Hours
ax = fig.add_subplot(1, 2, 2)
ax.plot(t_15, z_15, label='Temperature profile at 15:00', color='brown',
linewidth=2)
ax.plot(tdlr_15, z_15, linestyle='dashed', label='DALR (15:00)', color='black')
ax.plot(tmlr_15, z_15, linestyle='dashed', label='MALR (15:00)', color='green')
plt.xlabel('Temperature (K)')
plt.ylabel('Altitude (km)')
plt.title('Temperature Profile at 15:00 Hours')
plt.legend()
plt.tight_layout()
plt.show()
5. P5
Wind Profile Using Neutral Similarity Law
import numpy as np
import matplotlib.pyplot as plt
ustar = 0.4
z0 = 0.5
k = 0.4
z = np.arange(z0, 201)
#neutral similarity theory
u = (ustar / k) * np.log(z / z0)
#log y axis
ax = fig.add_subplot(1,3,2)
ax.semilogy(u,z)
plt.xlabel('Wind Speed (m/s)')
plt.ylabel('log')
ax.set_title('Logarithmic y-axis')
#log z
ax = fig.add_subplot(1,3,3)
ax.plot(u,np.log(z))
plt.xlabel('Wind Speed (m/s)')
plt.ylabel('log(z)')
ax.set_title('log(z) on y-axis')
import numpy as np
import matplotlib.pyplot as plt
z = np.arange(z0, 201)
for i in ustar:
u = (i / k) * np.log(z / z0)
plt.semilogy(u, z, label=f'u* = {i} m/s')
plt.xlabel('Wind Speed (m/s)')
plt.ylabel('Height(m)')
plt.title('Wind Profiles for Different u*')
plt.legend()
plt.show()
import numpy as np
import matplotlib.pyplot as plt
ustar = 0.5
k = 0.4
z0 = [ 0.1, 0.2, 0.5]
def get_z(z0):
z = np.arange(z0, 201)
return z
for i in z0:
z = get_z(i)
u = get_u(z, ustar, i)
plt.semilogy(u, z, label=f'z0 = {i} m')
plt.xlabel('Wind Speed (m/s)')
plt.ylabel('Height(m)')
plt.title('Wind Profiles for Different z0')
plt.legend()
plt.show()
import numpy as np
import matplotlib.pyplot as plt
z = [ 0.5, 1, 2, 4, 8, 16]
u = [ 4.91, 5.44, 6.06, 6.64, 7.17, 7.71]
k = 0.4
for i in range(len(z)-1):
du_dz = (u[i + 1] - u[i]) / (z[i + 1] - z[i])
mz = (z[i]+z[i+1])/2
ustar = mz * k*du_dz
print(f'At midpoint = {mz}, Friction Velocity (u*) = {ustar:.4f}')
import numpy as np
import matplotlib.pyplot as plt
z = np.array([ 0.5, 1, 2, 4, 8, 16])
u = np.array([ 4.91, 5.44, 6.06, 6.64, 7.17, 7.71])
k = 0.4
rho = 1.293
# regression coefficient using python function polyfit
m, c = np.polyfit(u, np.log(z), 1)
ustar = k/m
z0 = np.exp(c)
#mmtm flux
mf = rho * ustar**2
#mmtm flux k-theory
Km = k * ustar * z
mfk = np.array([])
for i in range(len(z)-1):
bestfit = m * u + c
plt.plot(u,bestfit)
plt.scatter(u, np.log(z), color = 'green')
plt.xlabel('Wind Speed (m/s)')
plt.ylabel('log(z)')
plt.title('Regression Using Least Square Method')
plt.show()
6. P6
Computing Bulk Richardson Number
import numpy as np
z1,z2 = 8, 11
u1,u2 = 6.5, 7.2
v1,v2 = 5.4, 5.6
t1,t2 = 28.0, 28.4
g = 9.8
#dz
dz = (z2-z1)
#d(theta)/dz
theta_grad = (t2-t1)/dz
#du/dz
u_grad = (u2-u1)/dz
#dv/dz
v_grad = (v2-v1)/dz
#Bulk Richardson Number
ri_b = ( (g/(t1+273.15))*theta_grad ) / ( (u_grad)**2 + (v_grad)**2 )
print(f'Bulk Richardson Number = {ri_b:.4f}')
Modification
z1,z2 = 8, 11
u1,u2 = 18, 19
v1,v2 = 20, 23
t1,t2 = 22.0, 20.4
g = 9.8
>>>>> Bulk Richardson Number = -0.0159
z1,z2 = 38, 21
u1,u2 = 8, 9
v1,v2 = 20, 23
t1,t2 = 28.0, 25.4
g = 9.8
>>>>> Bulk Richardson Number = 0.1438
z1,z2 = 38, 11
u1,u2 = 2, 6
v1,v2 = 2, 4
t1,t2 = 30.0, 20
g = 9.8
>>>>> Bulk Richardson Number = 0.4364
7. P7
Gradient
import numpy as np
z1, z2 = 2,8
u0, u1 = 3.34, 3.98
t0,t1 = 302.19, 301.25
dudz = (u1-u0)/(z2-z1)
dtdz = (t1-t0)/(z2-z1)
zm = (z1+z2)/2
g = 9.8
k = 0.4
rho = 1.2
cp = 1.005
ri = ((g/t0)*dtdz)/(dudz**2)
zeta = ri
pm = (1-(15*zeta))**(-1/4)
#ph=pw
ph = (1-(15*zeta))**(-1/2)
ustar = (k*zm*dudz)/pm
tstar = (k*zm*dtdz)/ph
print(f'u* = {ustar:.4f}')
print(f't* = {tstar:.4f}')
l = zm/ri
#mf
mf = rho*(ustar**2)
#sh
h0 = -(rho*cp*ustar*tstar)
#find km & kh
km = mf/(dudz)
kh = -(h0/(dtdz))
print(f'Km = {km:.2f}')
print(f'Kh = {kh:.2f}')
Bulk
import numpy as np
rho = 1.1
# stanton number
ch = 1.3e-3
ta = 295.15
ts = 301.15
m = 12
cp = 1000
qa = 0.016
#SHF
h = rho*cp*ch*m*(ta-ts)
print(f'Sensible Heat Flux = {h:.2f}')
#MF
p = 1000
mw = 18.02e-3
le = 2.46e6
r = 8.314
a = (mw*le/r)*((1/273.2)-(1/301.15))
es = 0.611*np.exp(a)
print(f'Saturation vapour pressure = {es:.2f}')
qs = 0.622*(es/p)
print(f'Surface humidity = {qs:.5f}')
#LHF
qe = e0*le
print(f'Latent Heat Flux = {qe:.2f}')
#Momentum flux
cd = 1.3e-3
u = 12*np.sin(np.radians(120))
v = 12*np.cos(np.radians(120))
tau_x = rho*cd*m*u
print(f'taux = {tau_x:.2f}')
tau_y = rho*cd*m*v
print(f'tauy = {tau_y:.2f}')
tau = np.sqrt(tau_x**2+tau_y**2)
print(f'Momentum flux = {tau:.2f}')
Profile
import numpy as np
import matplotlib.pyplot as plt
z = [2, 4, 8, 16]
u = [2.84, 3.39, 4.01, 4.85]
tc = [33.09, 33.31, 33.57, 33.82]
g = 9.8
ri = []
zm = []
for i in range(len(z)-1):
dudz = (u[i+1]-u[i])/(z[i+1]-z[i])
dtdz = (t[i+1]-t[i])/(z[i+1]-z[i])
r = ((g/t[i])*dtdz)/(dudz**2)
ri.append(r)
zm.append((z[i+1]-z[i])/2)
print(ri)
m,c = np.polyfit(zm,ri,1)
bf = []
for i in range(len(zm)):
bf.append(m * zm[i] + c)
plt.scatter(ri,zm,color = 'green')
plt.plot(bf,zm)
plt.xlabel('Richardson Number')
plt.ylabel('Midpoint Height')
plt.show()
8. P8
import numpy as np
import matplotlib.pyplot as plt
import netCDF4 as nc
# Load data
x = nc.Dataset("/content/lhf (1).nc")
lat = x.variables["lat"][:]
lon = x.variables["lon"][:]
Lheat = x.variables["lhf"][:]
# Define months
z = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov",
"Dec"]
# Create a figure
fig = plt.figure(figsize=(12, 4))
ax.set_xlim(45, 110)
ax.set_ylim(-30, 30)
ax.set_title(z[i]) # Set month title
ax.set_xlabel("Longitude")
ax.set_ylabel("Latitude")
# Adjust layout
plt.colorbar(c)
plt.tight_layout()
plt.show()
9. P9
import netCDF4 as nc
import numpy as np
import matplotlib.pyplot as plt
# Load data
at = nc.Dataset("at.nc")
qa = nc.Dataset("qair.nc")
slp = nc.Dataset("slp.nc")
sst = nc.Dataset("sst.nc")
ws = nc.Dataset("ws.nc")
# Extract variables
lat = at.variables["lat"][:]
lon = at.variables["lon"][:]
time = at.variables["time"][:]
T_a = at.variables["at"][:]
T_s = sst.variables["sst"][:]
U = ws.variables["wspd"][:]
q_a = qa.variables["qair"][:]
P = slp.variables["slp"][:]
# Constants
rho = 1.225
c_p = 1005
L_v = 2.5e6
C_H = 1.1e-3
C_E = 1.1e-3
z = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov",
"Dec"]
# Plot Sensible Heat Flux (SHF) with subplots
fig = plt.figure(figsize=(12, 10))
for i in range(12):
ax = fig.add_subplot(4, 3, i + 1)
cf = ax.contourf(lon, lat, SHF[i, :, :], cmap="coolwarm")
ax.set_title(f"{z[i]}")
if i % 4 == 0:
ax.set_ylabel("Latitude")
if i >= 8:
ax.set_xlabel("Longitude")
for i in range(12):
ax = fig.add_subplot(3, 4, i + 1)
cf = ax.contourf(lon, lat, LHF[i, :, :], cmap="coolwarm")
ax.set_title(f"{z[i]}")
if i % 4 == 0:
ax.set_ylabel("Latitude")
if i >= 8:
ax.set_xlabel("Longitude")