Lesson 01: Welcome to Environmental Python — Automating the Boring Engineering Stuff
Why Learn Python When You Already Have Excel and a Scientific Calculator?
Picture an actual Friday afternoon in an environmental consulting firm or municipal water utility: your lead engineer drops a CSV file with 43,824 hourly PM2.5 and meteorology readings (Beijing Multi-Site Air Quality Dataset, UCI ML Repository) or 527 days of wastewater treatment plant telemetry on your desk and asks:
- How many days violated the 24-hour \(\text{PM}_{2.5}\) limit (\(35\,\mu\text{g/m}^3\)) or wastewater effluent BOD (\(30\text{ mg/L}\))?
- What is the daily pollutant mass loading \(\dot{m} = Q \cdot C\) entering the river during wet-weather peak flow?
- How do emissions change across all four downstream unit processes in
our Life Cycle Assessment
(
LCA Track 1)?
Typing that into a handheld calculator guarantees unit mistakes.
Dragging formulas down 43,824 rows in Excel freezes your
laptop—and one accidentally shifted cell reference ($B$2
becoming B3) silently corrupts your permit report without
raising any alarm.
Python fixes this. Instead of doing repetitive arithmetic by hand, you write a short, crystal-clear recipe once, check the conservation physics on a hand-verified number, and let Python automate 10 or 1,000,000 rows in milliseconds with printed units.
How This Course Connects Directly to Your Website Curriculum
Every lesson links Python programming straight to the Engineering & Environmental courses on this portal:
- Life Cycle Assessment
(
LCA Track 1 & 2): Technosphere/biosphere inventory balances (\(\mathbf{s} = \mathbf{A}^{-1}\mathbf{f}\)), GWP/AP/EP midpoint characterization, co-product allocation, and USEtox aquatic ecotoxicity. - Material Flow Analysis (
MFA / ODYM): Circular recycling throughput loops (\(\mathbf{x} = \mathbf{A}\mathbf{x} + \mathbf{y}\)) and dynamic product cohort lifetimes. - Renewable Energy (
Solar Thermal): Hourly clear-sky irradiance, flat-plate thermal collection, and carbon displacement. - Fluid Mechanics & Hydraulics: Clarifier overflow, Colebrook–White sewer friction, centrifugal pump curves, and Stokes particle settling.
- Heat & Mass Transfer: Binary gas-water diffusivity, Henry’s Law volatility partitioning, and Earth radiative forcing.
- Pre-Calculus & Calculus I + Thermodynamics
(
ME 200/300): Pollutant first-order decay, logistic growth, gas-turbine \(\text{NO}_x\) emissions, and trace gas ideal-gas conversions.
How Python Thinks: Your First Mental Model
Think of a Python script inside a Jupyter cell as an ultra-fast, completely literal lab assistant:
- Top-to-Bottom Execution: Python runs Line 1, then Line 2, then Line 3 in strict order.
- Memory Stickers (Variables): Writing
flow_m3_s = 0.45creates the number0.45in memory and attaches a clear labelflow_m3_sto it. - Expressions Reduce to One Value: When Python
evaluates
0.45 * 86400, it reduces it down to38880.0before saving or printing. - Always Put Units in Variable Names: Mixing up
mg/Lvs.kg/m^3orm^3/svs.m^3/daycauses more real-world engineering failures than Python syntax errors!
# Example 1 (Water & Wastewater Mass Balance): Municipal Outfall BOD Loading + Clarifier HRT
# Notice every variable carries its unit directly in its name!
Q_river_effluent_m3_s = 0.45 # Treated outfall discharge [m^3/s]
C_bod_mg_L = 22.0 # 5-day Biochemical Oxygen Demand [mg/L] (= g/m^3)
V_clarifier_m3 = 1800.0 # Secondary clarifier tank volume [m^3]
Q_plant_daily_m3_d = 14400.0 # Daily treatment plant design flow [m^3/day]
# Step 1: Convert m^3/s -> m^3/day (86,400 seconds per day)
Q_outfall_m3_d = Q_river_effluent_m3_s * 86400.0
# Step 2: 1 mg/L = 1 g/m^3 = 0.001 kg/m^3
C_bod_kg_m3 = C_bod_mg_L * 1e-3
bod_load_kg_d = Q_outfall_m3_d * C_bod_kg_m3
# Step 3: Hydraulic Retention Time (theta = V / Q)
hrt_days = V_clarifier_m3 / Q_plant_daily_m3_d
hrt_hours = hrt_days * 24.0
print("=== Environmental Control-Volume Quick Check ===")
print(f"Outfall Volume : {Q_outfall_m3_d:,.1f} m^3/day")
print(f"BOD Mass Load : {bod_load_kg_d:.2f} kg/day")
print(f"Clarifier HRT : {hrt_hours:.2f} hours ({hrt_hours * 60:.0f} min)")=== Environmental Control-Volume Quick Check ===
Outfall Volume : 38,880.0 m^3/day
BOD Mass Load : 855.36 kg/day
Clarifier HRT : 3.00 hours (180 min)
# Example 2 (LCA Track 1 + Industrial Ecology): Quick Process Carbon Footprint Check
# Cross-reference: LCA Track 1 Module 3 & 5 (IPCC AR6 GWP100 Factors: CO2=1, CH4=28, N2O=265)
co2_fossil_kg = 1420.0 # Direct boiler & electricity CO2 [kg]
ch4_fugitive_kg = 4.5 # Uncaptured digester / landfill methane [kg]
n2o_nitrification_kg = 0.65 # Wastewater biological nitrogen removal N2O [kg]
co2_biogenic_uptake = -310.0 # Biogenic credit for paper/timber feedstock [kg]
gwp_fossil_only = (
1.0 * co2_fossil_kg
+ 28.0 * ch4_fugitive_kg
+ 265.0 * n2o_nitrification_kg
)
gwp_net_with_bio = gwp_fossil_only + 1.0 * co2_biogenic_uptake
print("=== GHG Inventory Accounting (LCA Track 1 Module 5) ===")
print(f"CO2 Contribution : {1.0 * co2_fossil_kg:8.1f} kg CO2-eq")
print(f"CH4 Contribution : {28.0 * ch4_fugitive_kg:8.1f} kg CO2-eq")
print(f"N2O Contribution : {265.0 * n2o_nitrification_kg:8.1f} kg CO2-eq")
print(f"Gross Fossil GWP : {gwp_fossil_only:8.1f} kg CO2-eq")
print(f"Net Balance : {gwp_net_with_bio:8.1f} kg CO2-eq (biogenic uptake included)")=== GHG Inventory Accounting (LCA Track 1 Module 5) ===
CO2 Contribution : 1420.0 kg CO2-eq
CH4 Contribution : 126.0 kg CO2-eq
N2O Contribution : 172.2 kg CO2-eq
Gross Fossil GWP : 1718.2 kg CO2-eq
Net Balance : 1408.2 kg CO2-eq (biogenic uptake included)
Watch Out! Three Day-One Traps That Catch Beginners
- Exponents Use
**, NEVER^: In Python,3**2is9. Writing3^2runs bitwise XOR (1) on integers and crashes completely on floating-point numbers (3.0^2->TypeError)! - Always Multiply Explicitly: Write
2 * math.pi * r— never2(pi)ror2pi r. - Never Omit the Unit Factor
1e-3inQ * C: Multiplying \(\text{m}^3/\text{d}\) directly by \(\text{mg/L}\) gives grams per day, not kilograms per day. A forgotten \(10^{-3}\) factor causes a \(1,000\times\) error in a Total Maximum Daily Load (TMDL) permit!
Concept Check Questions
- Why does storing
C_bod_kg_m3 = C_bod_mg_L * 1e-3in a separate variable make regulatory peer review easier than hiding/ 1000inside a complex formula? - What happens if you type
radius = 5.0followed byarea = 3.14159 * radius^2? - Look at Example 2: even though only
0.65 kgof \(\text{N}_2\text{O}\) escaped, why did its GWP100 impact (172.25 kg CO2-eq) exceed4.5 kgof \(\text{CH}_4\) (126.0 kg CO2-eq)? - In Jupyter, if you edit
C_bod_mg_L = 10.0in Cell 1 without re-running Cell 1, what value doesbod_load_kg_dkeep?
Guided Practice Problem 1 (Worked Self-Check): Industrial Stack PM2.5 Permit Loading
An industrial cement kiln emits \(Q_{\text{gas}} = 28.5\text{ m}^3/\text{s}\) of dry exhaust gas carrying \(\text{PM}_{2.5}\) at \(C = 32.0\text{ mg/m}^3\) (operating \(8,400\text{ hours/year}\)). Write a clean script computing:
- Hourly mass emission rate \(\dot{m}_{\text{h}}\) (\(\text{kg/h}\)),
- Annual \(\text{PM}_{2.5}\) emissions (\(\text{metric tonnes/year}\), where \(1\text{ tonne} = 1,000\text{ kg}\)),
- Compliance ratio against an annual facility cap of
30.0 tonnes/yr.
Hint: Track Seconds -> Hours -> Years and mg -> kg -> tonnes
Remember \(1\text{ kg} = 10^6\text{
mg}\). Multiply Q_gas * 3600 to get \(\text{m}^3/\text{h}\), then multiply by
C_mg_m3 * 1e-6 for kg/h!
# Guided Practice 1 Starter — Run & Verify Units!
Q_gas_m3_s = 28.5
C_pm25_mg_m3 = 32.0
hours_per_year = 8400.0
annual_cap_tonnes = 30.0
kg_per_hour = Q_gas_m3_s * 3600.0 * C_pm25_mg_m3 * 1e-6
tonnes_per_year = (kg_per_hour * hours_per_year) / 1000.0
print(f"PM2.5 hourly emission : {kg_per_hour:.3f} kg/h")
print(f"PM2.5 annual total : {tonnes_per_year:.2f} tonnes/yr (Cap: {annual_cap_tonnes} tonnes/yr)")
print(f"Within permit cap? : {tonnes_per_year <= annual_cap_tonnes}")PM2.5 hourly emission : 3.283 kg/h
PM2.5 annual total : 27.58 tonnes/yr (Cap: 30.0 tonnes/yr)
Within permit cap? : True