Programming Resources

In this course, I will rely heavily on examples that incorporate scientific programming. Some lecture slides will contain embedded Python and/or R code to help illustrate different concepts. Programming will also be helpful when completing your assignments. However, programming experience is not a prerequisite for this course.

Programming Languages

Just as English, Mandarin, Punjabi, or any other language could be used to explain climate change; Python, R, JavaScript, or any other programming language can all be used to quantify and visualize the scale of the problem. There are a number of different options when it comes to programming languages. Each have their pro’s and cons. In this course, I encourage you to use either Python or R, but if you already know another language (e.g., MATLAB) you could use that instead.

Python

Python is my personal preference. I like python because its very flexible, general-purpose programming language that is fairly easy to read / write. It is well suited for scientific programming and has lots of packages for data analysis and visualization, and it’s completely free! If you want to install Python on your own computer and don’t know where to start, I suggest you go with Anaconda. Its a scientific programming platform that will install python for you along with most of the commonly used extensions.

R

R is also a great option. It is a programming language that was developed specifically for statistical computing and graphics. This means its not a flexible as a langue like Python, but also that it can be a bit easier to use “out of the box” for specific applications, like data visualization. Personally, I find R a bit harder to read/write, but it is still fairly straightforward once you get a handle on the syntax. One advantage R has over python, is that its a bit easier to install. All you need to do to get setup is to install R and RStudio.

Online Access

UBC also provides students with free server space that you can use at https://ubc.syzygy.ca/. This isn’t necessarily a viable long-term option for all your computing needs because it does come withe some storage limitations. However, it already comes with both Python and R installed, and will probably be sufficient for most things we’ll do in this course. So you can use it to try things out without installing anything on your own computer.

Additional Resources

Stack Overflow is a great resource for general programming inquires. Chances are, if you’re having an issue, someone else has already encountered it and the question has already been asked/answered on here.

ChatGPT can read/write code. It won’t give you prefect answers to specific application, so you’ll still need to figure out how to edit things to suit your needs. But it it can be helpful resource to get the ball rolling

  • Be careful when relying on a resource like this. If you let the AI do all the heavy lifting, you risk not actually learning how to code yourself.

GitHub I suggest you create a GitHub account. Git/GitHub are useful for many things beyond just this course. Depending on your career trajectory, it could benefit you immensely down the road. All course materials are hosted on GitHub and you could use Git to easily access them on your own computer.

  • I won’t require you to use GitHub for anything in the course, I will always provide ways to access materials that do not require an account or knowledge of the platform.

  • You can find a bit more info about it here.

Important Constants and Symbols

Sy <- read.csv(file = '_Data/Symbols.csv', check.names = FALSE)

kable(Sy[,1:3])
Table 1: These are list of symbols that will be used to represent key variables used in this course. Note: some symbols may refer to multiple things, it up to you to determine which, based on context.
Symbol Name Definition
\(T\) Temperature Quantitatification of the heat present in an object (in K unless specified otherwise)
\(T_d\) Dew-point temperature ?@fig-saturation-vapor-density
\(T_w\) Wet-bulb temperature The temperature read by a ventilated thermometer covered in water at ambient temperature
\(RH\) Relative Humidity The % of absolute humidity relative to the maximum possible humidity at \(T\)
\(U\) Wind speed Velocity of air movement across (or along) a pressure gradient
\(P_a\) Air pressure Force applied perpendicular to the surface of an object
\(\lambda\) Wavelength The distance between the crest of of two waves (of elector magnetic radiation).
\(SW\) Short wave radiation Electromagnetic radiation with \(\lambda\) less than and 3 \(\mu m\). Includes ultraviolet; visible; and near-infrared radiation.
\(LW\) Long wave radiation Electromagnetic radiation with \(\lambda\) greater than 3 \(\mu m\). Includes thermal radiation (sensible heat).
\(R_n\) Net Radiation The sum of all incoming (\(\downarrow\)) and outgoing (\(\uparrow\)) radiation (both \(SW\) and \(LW\)).
\(\zeta\) Absorptivity Radiation that is absorbed by an object; adding energy to the object and increasing its temperature.
\(\Psi\) Transmissivity coefficient Radiation that passes through an object.
\(\Psi_{sky}\) Sky view coefficient Portion of hemispheric view of the sky blocked by other objects.
\(\alpha\) Reflectivity Radiation that is reflected by a object.
\(P_v\) Vapor pressure Partial pressure exerted by water vapor in the atmosphere; can be calculated using ?@eq-Ideal-Gas-Law-Vapor-Density
\(P_v^*\) Saturation vapor pressure at T Maximum possible vapor pressure for a parcel of air at \(T\); use ?@eq-Buck-Equation or ?@fig-saturation-vapor-pressure
\(\rho_v\) Vapor density ?@eq-Ideal-Gas-Law-Vapor-Density or ?@eq-Vapor-Density-Psychrometer
\(\rho_v^*\) Saturation vapor density at \(T\) ?@fig-saturation-vapor-density
\(\rho_{vw}^*\) Saturation vapor density at \(T_w\) ?@fig-saturation-vapor-density
\(VDD\) Vapor density defect ?@eq-Vapor-Density-Deficit
\(VPD\) Vapor pressure defect ?@eq-Vapor-Pressure-Deficit
\(\theta\) Solar Zenith Angle between the sun and the vertical direction - to calculate the zenith at solar noon see ?@eq-Solar-Zenith-at-Noon
\(\theta\) Volumetric Fraction Fraction of a soil volume occupied by a given substance
\(E_b\) Black-body emissivity An idealized estimate of radiative flux density based on an object’s temperature assuming its a black-body see Equation 64
\(\epsilon\) emissivity The ratio of energy emitted from a surface to that which would be emitted by a black-body emitter at the same temperature
\(E_g\) Grey-body emissivity An adjustment to \(E_b\) which accounts for the object’s emissivity \(\epsilon\) see Equation 63 & see Equation 62
\(\mu\) Attenuation coefficient in Beers Law the proportion of radiation that is not transmitted through a substance - not to be confused with the prefix \(\mu\) meaning “micro”
\(z\) Vertical position Relative to Earth’s surface (height or depth) unless otherwise specified
ELR Environmental Lapse Rate Observed change in air temperature with height
Sy <- read.csv(file = '_Data/Constants.csv', check.names = FALSE)

kable(Sy[,1:3])
Table 2: Values and units of important constants used in this.
Symbol Name Definition
\(C\) Heat capacity of air (at 20 \(^{\circ}\) C) 1210 J m\(^{-3}\) K\(^{-1}\)
\(R\) Ideal gas constant 8.31446261815324 x 10 \(^{-3}\) kPa m \(^3\) g \(^{-1}\) K \(^{-1}\)
\(\gamma\) Psychrometric constant (at 20 \(^{\circ}\) C) 0.495 g m \(^{-3}\) K \({^-1}\)
\(\sigma_b\) Stefan-Boltzman constant 5.67 x 10 \(^{-8}\) W m\(^{-2}\) K\(^{-4}\)
\(c\) Speed of Light 299792458 m s\(^{-1}\)
\(h\) Planks constant 6.62607015*10 \(^{-34}\)
\(b\) Wein’s displacement constant 2898 \(\mu m K\)
\(I_o\) Solar constant 1361\(W m^{-2}\)
\(M_{H_2O}\) Molar mass of water 18.01528 g mol \(^{-1}\)
DALR Dry Adiabatic Lapse Rate -0.01 \(K m^{-1}\)
SALR Saturated Adiabatic Lapse Rate The rate is variable as a function of \(T\) and \(P\) but we can assume a constant rate of -0.005 \(K m^{-1}\) to get a reasonable approximation

List of Equations

A list of important equations used in the course.

  • Note these are listed in no particular order - they’re just scarped from the site and collated here for your reference.

Albedo

\[ albedo = \alpha_{SW} = \frac{SW_{\uparrow}}{SW_{\downarrow}} \tag{1}\]

Beers Law

\[ R_z = R_0 e^{-z\mu} \tag{2}\]

LE Bowen

\[ LE = \frac{R_n-G}{1+B} \tag{3}\]

H Bowen

\[ H = \frac{R_n-G}{1+B^{-1}} \tag{4}\]

Bowen Ratio

\[ B = \frac{H}{LE} \tag{5}\]

Bulk Transmissivity

\[ SW_\downarrow = I_{ex} \Psi_a^{m} \tag{6}\]

Conservation Equation Radiation

\[ \zeta_\lambda + \Psi_\lambda + \alpha_\lambda = 1 \tag{7}\]

Conservation Law Scalar 1D

\[ \frac{dc}{dt} = \frac{\partial c}{\partial t} + w \frac{\partial c}{\partial z} \tag{8}\]

Conservation Law Scalar Homogeneous 2

\[ \frac{dc}{dt} = \frac{\partial c}{\partial t} \color{white}{+ u \frac{\partial c}{\partial x} + v \frac{\partial c}{\partial y}} + w \frac{\partial c}{\partial z} \tag{9}\]

Conservation Law Scalar Homogeneous

\[ \frac{dc}{dt} = \frac{\partial c}{\partial t} \color{gray}{+ u \frac{\partial c}{\partial x} + v \frac{\partial c}{\partial y}} + w \frac{\partial c}{\partial z} \tag{10}\]

Conservation Law Scalar

\[ \frac{dc}{dt} = \frac{\partial c}{\partial t} + u \frac{\partial c}{\partial x} + v \frac{\partial c}{\partial y} + w \frac{\partial c}{\partial z} \tag{11}\]

Conservation Law Simplified

\[ In - Out = \Delta S \tag{12}\]

Cosine Law of Illumination

\[ R_s = R_p cos(Z) \tag{13}\]

Damping Depth

\[ D = \sqrt{\small\frac{2K}{\omega}} = \sqrt{\small\frac{KP}{\pi}} \tag{14}\]

Energy of Photon

\[ e = hv \tag{15}\]

Extraterrestrial Irradiance

\[ I_{ex}=I_0(\frac{R_{av}}{R})^2\cos(Z) \tag{16}\]

Adjusted Constant

\[ \begin{eqnarray}(\frac{R_{av}}{R})^2 = & 1.00011+0.034221\cos(\gamma)+0.001280\sin(\gamma)+\\ &0.000819\cos(2\gamma)+0.000077\sin(2\gamma)\end{eqnarray} \tag{17}\]

Fouriers Law

\[ H_G = -k \small\frac{T_2 - T_1}{z_2 - z_1} \tag{18}\]

Frequency

\[ v=\frac{c}{\lambda} \tag{19}\]

Heat Capacity Soil

\[ C_{s}=C_{m}\theta_{m}+C_{o}\theta_{o}+C_{w}\theta_{w}+C_{a}\theta_{a} \tag{20}\]

Heat Sharing

\[ \frac{H_g}{H} = \frac{\mu_g}{\mu_a} \tag{21}\]

Ideal Gas Law

\[ PV=nRT \tag{22}\]

Kelvin

\[ T(K) = T(\deg C) + 273.15 \tag{23}\]

Kirchhoffs Law

\[ \zeta_{\lambda} = \epsilon_{\lambda} \tag{24}\]

Longwave Radiation Balance

\[ \zeta_{LW} + \alpha_{LW} = 1 \tag{25}\]

Longwave Reflectivity

\[ \alpha_{LW} = 1 - \epsilon_{LW} \tag{26}\]

Latent Heat Vaporization

\[ L_v = 2500.8-2.36T-0.0016T^2-0.00006T^3 \tag{27}\]

Log Wind Law Non Neutral

\[ \frac{\Delta\bar{u_z}}{\Delta z}=\frac{u_*}{k}\Phi_M \tag{28}\]

Phi

\[ \Phi_M = \begin{cases} \zeta > 0 , 1 + 4.7 \zeta \\ \zeta = 0 , 1 \\ \zeta < 0 , (1 - 15 \zeta)^{-0.25}\end{cases} \tag{29}\]

Log Wind Law

\[ \bar{u_z}=\frac{u_*}{k}ln(\frac{z}{z_0}) \tag{30}\]

Long wave Flux Density Stefan Boltzman

\[ LW^* = LW\downarrow - \epsilon_{LW}\sigma_b T_s^4 -(1-\epsilon_{LW})LW\downarrow \tag{31}\]

Long wave Flux Density

\[ LW^* = LW\downarrow - LW\uparrow \tag{32}\]

NDVI

\[ NDVI = \frac{NIR-Red}{NIR+Red} \tag{33}\]

Absorbed SW

\[ SW^* = SW \downarrow (1 - \alpha) \tag{34}\]

Net Absorbed Emitted LW

\[ LW^* = \epsilon LW \downarrow - \epsilon \sigma_b T_s^4 \tag{35}\]

Net Radiation Energy Balance

\[ R_n = SW \downarrow (1 - \alpha) + \epsilon LW \downarrow - \epsilon \sigma_b T_s^4 \tag{36}\]

Net Radiation Short

\[ R_n = SW^* + LW^* \tag{37}\]

Net Radiation

\[ R_n = (SW \downarrow - SW \uparrow) + (LW \downarrow - LW \uparrow) \tag{38}\]

Obukhov Length

\[ \begin{align}& \zeta = \frac{z}{L} \\ \nonumber& L = - \frac{\bar{\theta_v} u_*^3}{kg\overline{w^{\prime}\theta_v^{\prime}}} = - \frac{\bar{\rho c_a \theta_v} u_*^3}{kgH}\end{align} \tag{39}\]

Phase Shifted Sinusoidal Temperature Wave

\[ T_{(z,t)}=\overline{T_o}+\Delta T_o e^{-z(\small\frac{\omega}{2K})^{1/2}} \sin(\omega t - (\small\frac{\omega}{2K})^{1/2}z) \tag{40}\]

Planks Law

\[ E = \frac{2*h*c}{\lambda^{5}}*\frac{1}{e^{\frac{h*c}{\lambda*\sigma_b*T}}-1} \tag{41}\]

Potential Temperature

\[ \theta = T_z + DALR z \tag{42}\]

Prata

\[ \epsilon_a = [1-(1+zeta)\exp{-(a+b\zeta)^{0.5}}] \tag{43}\]

Radiation Balance Air.qmd

\[ \Delta R_{n(\Delta z)}=R_{n(z+\Delta z)}-R_{n(z)}=C_a(\frac{\Delta T_a}{\Delta t})_R \Delta z \tag{44}\]

Reflectivity

\[ \alpha_\lambda = \frac{\lambda_{reflected}}{\lambda_{incident}} \tag{45}\]

Reflectance Coefficient

\[ \alpha_{\lambda_1 \rightarrow \lambda_2} = \frac{\int_{\lambda_1}^{\lambda_2}\alpha_\lambda I_\lambda d_\lambda}{\int_{\lambda_1}^{\lambda_2}I_\lambda d_\lambda} \tag{46}\]

Relative Humidity

\[ RH=\frac{P_v}{P_v^*} \tag{47}\]

Reynolds Decomposition

\[ \begin{align}\bar{a^{\prime}} = 0 \nonumber \\\bar{a }\times \bar{b^{\prime}} = 0 \nonumber \\\overline{a \times b} =\overline{(\bar{a}+a^{\prime})\times(\bar{b}+b^{\prime})} = \bar{a}\times\bar{b} + \overline{a^{\prime}\times b^{\prime}}\end{align} \tag{48}\]

Reynolds Decomposition

\[ a(t) = \bar{a} + a^{\prime}(t) \tag{49}\]

Mean

\[ \bar{a} = \small\frac{1}{N}\sum_{i=0}^{N-1}a(t_i) \tag{50}\]

Shear Stress

\[ \tau = \mu\small\frac{\delta u}{\delta z} \tag{51}\]

Sinusoidal Temperature Wave

\[ T_{(o,t)}=\overline{T_o}+\Delta T_o \sin(\omega t) \tag{52}\]

Soil Heat Flux Divergence

\[ \small\frac{\Delta T_s}{\Delta t} = -\small\frac{1}{C_s}\small\frac{\Delta H_g}{\Delta z} \tag{53}\]

Soil Heat Flux

\[ \frac{H_g}{z} = C_{s} \frac{\Delta T_{s}}{\Delta t} \tag{54}\]

Soil Volume Fractions

\[ \theta_a+\theta_w+\theta_g = 1 \tag{55}\]

Soil Volume Fractions

\[ \theta_P = \theta_a + \theta_w = 1 - \theta_g \tag{56}\]

Organic Mineral

\[ \theta_g = \theta_m + \theta_o \tag{57}\]

Solar Declination

\[ \begin{eqnarray}\delta = & 0.006918 - 0.399912 \cos( \gamma)+0.070257 \sin( \gamma) \\ & -0.006758 \cos(2 \gamma) + 0.000907\sin(2\gamma) \nonumber \\ & -0.002697\cos(3\gamma)+0.00148 \sin(3\gamma) \nonumber \end{eqnarray} \tag{58}\]

Fractional Year

\[ \gamma = \frac{2 \pi }{365} (DOY-1) \tag{59}\]

Solar Zenith

\[ \cos(Z) = \sin(\beta) = \sin(\Phi)\sin(\delta)+\cos(\Phi)\cos(\delta)\cos(h) \tag{60}\]

Hour Angle

\[ h = 15^{\circ}(12-LAT) \tag{61}\]

Stefan Boltzman Law Grey Body Adjusted

\[ E_g = \epsilon\sigma_b T^4 + (1-\epsilon) * (SW\downarrow+LW\downarrow) \tag{62}\]

Stefan Boltzman Law Grey Body

\[ E_g = \epsilon\sigma_b T^4 \tag{63}\]

Stefan Boltzman Law

\[ E_b = \sigma_b T^4 \tag{64}\]

Surface Energy Balance Ice

\[ \color{orange}{R_n} = \color{red}{H} + \color{blue}{LE} + \color{green}{\Delta S} + \color{grey}{\Delta M} \tag{65}\]

Surface Energy Balance No Mass

\[ \color{orange}{R_n} = \color{red}{H} + \color{blue}{LE} + \color{purple}{G} \tag{66}\]

Surface Energy Balance Storage

\[ \color{orange}{R_n} = \color{red}{H} + \color{blue}{LE} + \color{purple}{G} + \color{green}{\Delta S} \tag{67}\]

Surface Energy Balance

\[ R_n = H + LE + G + \Delta S \tag{68}\]

Tetens

\[ P_v^* = 6.112\exp{17.62\frac{T_a}{243.12+T_a}} \tag{69}\]

Thermal Admittance v Temperature

\[ \Delta T_s = \frac{\Delta H_g}{\mu} \tag{70}\]

Thermal Admittance

\[ \mu = \sqrt{k C} \tag{71}\]

Thermal Diffusivity

\[ K = \frac{k}{C} \tag{72}\]

LMST

\[ LMST = (TZ) + (\lambda-TZ_{m})\frac{4}{60} \tag{73}\]

LAT

\[ LAT = LMST-\Delta LAT \tag{74}\]

Delta LAT

\[ \begin{eqnarray}\Delta LAT = & 229.18[0.000075+0.001868\cos(\gamma)-0.032077\sin(\gamma) \\ & -0.014615\cos(2\gamma)-0.040849\sin(2\gamma)]\end{eqnarray} \tag{75}\]

MKE

\[ \frac{MKE}{m}=\frac{1}{2}(\overline{u^2}+\overline{v^2}+\overline{w^2}) \tag{76}\]

TKE

\[ e=\frac{1}{2}(u^{\prime2}+v^{\prime2}+w^{\prime2}) \nonumber \tag{77}\]

Turbulence Intensities

\[ \begin{align}I_u = \small\frac{\sigma_u}{M} \nonumber \\ I_v = \small\frac{\sigma_v}{M} \nonumber \\ I_w = \small\frac{\sigma_w}{M} \nonumber \\ M = \sqrt{\bar{u^2} + \bar{v^2} + \bar{w^2}}\end{align} \tag{78}\]

Variances

\[ \overline{a^{\prime2}} = \frac{1}{N}\sum_{i=0}^{N-1}a^{\prime2}(t_i) \tag{79}\]

std

\[ \sigma_a=\sqrt{\overline{a^{\prime2}}} \tag{80}\]

Virtual Temperature

\[ T_v = (1 + 0.61 r_v - r_l) T \tag{81}\]

Weins Law

\[ \lambda_{max} = \frac{b}{T} \tag{82}\]

Wind Power

\[ U = \frac{\pi}{2}\rho E R^2 \bar{u}^3 \tag{83}\]