Files
UAV-CO2/src/gasflux/gas.py
DXC 72a20adc87 fix(gas.py): 修复理想气体定律温度项重复相加错误
gas_density() 函数第 36 行:
- 原代码: (local_temperature_kelvin + standard_temperature) / standard_temperature
  物理错误:T_K 已在第32行完成 °C→K 转换(+273.15),这里又加了一次 273.15,
  导致体积修正因子偏大约 2 倍(对 7.4°C 数据:553.7/273.15≈2.03 而非正确的 280.55/273.15≈1.03)
- 修复: local_temperature_kelvin / standard_temperature
  符合理想气体定律 V₂ = V₁ × (P₁/P₂) × (T₂/T₁)

影响范围:所有通过 gas_flux_column() 计算的通量值,修复后通量约为原值的 2 倍
2026-07-10 14:48:17 +08:00

59 lines
2.1 KiB
Python

"""functions related to gas transformations and calculations, e.g. density, point flux etc."""
import molmass
import pandas as pd
gas_variables = {
"standard_pressure": 1013.25, # hPa/mbar
"standard_temperature": 273.15, # degrees K
"standard_molar_volume": 0.022413969545014, # m3⋅mol-1
}
def mass(formula: str) -> float:
"""Return the molar mass of a gas in g/mol."""
return molmass.Formula(formula.upper()).mass # only accepts capital letters
def gas_density(local_pressure: float, local_temperature_celsius: float, gas: str) -> float: # millibars and celsius
"""
Calculate the density of a gas in kg/m3 based on local pressure and temperature.
Parameters:
- local_pressure: The local pressure in hPa/mbar.
- local_temperature: The local temperature in degrees Celsius.
- gas: The chemical formula of the gas.
Returns:
- The density of the gas in kg/m3.
Assumes ideal gas behavior.
"""
local_temperature_kelvin = local_temperature_celsius + gas_variables["standard_temperature"]
local_volume = (
gas_variables["standard_molar_volume"]
* (gas_variables["standard_pressure"] / local_pressure)
* (local_temperature_kelvin / gas_variables["standard_temperature"])
) # m3⋅mol-1
return mass(gas) / 1000 / local_volume
def gas_flux_column(df: pd.DataFrame, gas: str, wind: str = "windspeed") -> pd.DataFrame:
"""
Add columns to the DataFrame for the gas density, mass, and flux.
Parameters:
- df: The DataFrame.
- gas: The chemical formula of the gas.
- wind: The column name for the wind speed (NB - must be perpendicular to the plane)
Returns:
- The DataFrame with the added columns.
"""
average_temp = df["temperature"].mean() # celsius
average_pressure = df["pressure"].mean() # hPa
gd = gas_density(local_pressure=average_pressure, local_temperature_celsius=average_temp, gas=gas) # kg/m3
df[f"{gas}_kg_m3"] = gd * (df[f"{gas}_normalised"] * 1e-6) # kg/m3
df[f"{gas}_kg_h_m2"] = df[f"{gas}_kg_m3"] * df[wind] * 60 * 60 # kg/h/m2
return df