Density altitude kills pilots. Not metaphorically — it’s one of the most consistent contributors to takeoff accident reports, especially in summer, especially at high-elevation airports. And yet most aviation apps either skip it entirely or display a static number pulled once at startup. If you’re building anything for GA pilots, flight schools, or backcountry operators, real-time density altitude — not a cached figure from two hours ago — is worth getting right.
Here’s how to pull the data you actually need from WeatherAPI and do the calculation properly.
The Math First
Density altitude is pressure altitude corrected for non-standard temperature. The FAA’s published formula:
Pressure Altitude (ft) = (29.92 – QNH_inHg) × 1000 + field_elevation_ft
Then density altitude:
DA (ft) = PA + 118.8 × (OAT_°C – ISA_temp_at_PA)
Where ISA temp at a given pressure altitude is: 15 – (PA / 1000 × 1.98)
That 118.8 factor comes from collapsing the standard atmosphere constants into a usable scalar. If you want the ICAO-precise version using virtual temperature and the hypsometric equation, you’ll also need dewpoint to compute vapor pressure — more on that below.
What WeatherAPI Gives You
A call to /v1/current.json?q=KBZN&aqi=no for Bozeman Yellowstone International (elevation 4,473 ft MSL) returns:
current.temp_c— outside air temperature in Celsiuscurrent.pressure_mb— station pressure in millibars (not sea-level pressure — important distinction below)current.dewpoint_c— available on current conditions, critical for the humidity correctioncurrent.humidity— relative humidity, percentcurrent.pressure_in— station pressure in inches Hg
Here’s what trips people up: pressure_mb in WeatherAPI’s response is station pressure, not altimeter setting (QNH). Altimeter setting is station pressure reduced to sea level using a standardized temperature lapse rate. For density altitude purposes you actually want station pressure, so WeatherAPI’s pressure_mb is more directly useful than the altimeter setting you’d dial into an aircraft’s Kollsman window.
If you need to display QNH for reference, you can back-calculate it. Don’t use the sea-level pressure field from WeatherAPI for DA calculations directly — it’s already been corrected and will double-count elevation.
The Humidity Correction
The simplified FAA formula ignores humidity. At temperate airports in winter that’s fine — humidity’s contribution to DA is modest. But at a high-DA airport in July, skipping it can underestimate density altitude by 200–400 feet. For a Cessna 172 departing a 6,000 ft strip in Arizona in August, that’s not a rounding error.
To include humidity, you need virtual temperature (Tv), which accounts for the fact that water vapor is lighter than dry air:
e = 6.1078 * 10^(7.5 * Td / (237.3 + Td)) # vapor pressure, mb (Td in °C)
Tv_K = (T_K) / (1 - (e / P_station) * (1 - 0.622)) # T and Tv in Kelvin
Then use Tv in place of T when computing density altitude via the hypsometric formula. WeatherAPI gives you dewpoint_c directly, so skip deriving it from relative humidity — use the field that’s already there.
Code: Pulling Data and Computing DA
Python, minimal dependencies:
import requests
API_KEY = "your_key_here"
AIRPORT = "KBZN"
ELEV_FT = 4473 # field elevation MSL
resp = requests.get(
"https://api.weatherapi.com/v1/current.json",
params={"key": API_KEY, "q": AIRPORT, "aqi": "no"}
)
data = resp.json()["current"]
T_c = data["temp_c"]
Dp_c = data["dewpoint_c"]
P_mb = data["pressure_mb"] # station pressure
# Vapor pressure (Tetens approximation)
e = 6.1078 * (10 ** (7.5 * Dp_c / (237.3 + Dp_c)))
# Virtual temperature
T_k = T_c + 273.15
Tv_k = T_k / (1 - (e / P_mb) * (1 - 0.622))
Tv_c = Tv_k - 273.15
# Pressure altitude
# Convert station pressure to equivalent altimeter setting for PA calc
# PA from station pressure directly:
P_std_mb = 1013.25
PA_ft = (1 - (P_mb / P_std_mb) ** 0.190284) * 145366.45
# ISA temp at PA
ISA_at_PA = 15 - (PA_ft / 1000 * 1.98)
# Density altitude using virtual temp for humidity correction
DA_ft = PA_ft + 118.8 * (Tv_c - ISA_at_PA)
print(f"Pressure Altitude: {PA_ft:.0f} ft")
print(f"Density Altitude: {DA_ft:.0f} ft")
The (1 - (P/P_std)^0.190284) × 145366.45 expression is the ICAO standard atmosphere pressure-altitude relationship — more accurate than the linear 1 inHg = 1000 ft approximation, particularly above 8,000 ft PA.
Where to Pull the Location From
WeatherAPI accepts ICAO codes directly for the q parameter, which makes airport queries clean. For backcountry strips without ICAO codes, pass lat/lon — WeatherAPI returns the nearest observation point. For remote strips, that nearest point might be 30–50 miles away and at a significantly different elevation. If you’re building for serious backcountry ops, validate that the observation elevation roughly matches the strip’s elevation and warn users when the gap is large.
The location.lat, location.lon, and location.name fields in the response let you surface that metadata so pilots can sanity-check it themselves.
Polling Frequency and Caching
Temperature and pressure at a single airport don’t change dramatically minute-to-minute under stable conditions, but during afternoon convective heating — exactly when density altitude peaks — they can shift meaningfully over 15–20 minutes. Poll no more than every 5–10 minutes for a live DA display, and cache the result with a visible timestamp so pilots know how fresh the data is. A timestamp reading “Updated 47 minutes ago” on a DA readout is a safety issue, not a UX quibble.
One Position Worth Taking
There’s a school of thought that says: just use the nearest METAR and parse it, since that’s what pilots are trained to trust. For app development purposes, I disagree. METAR issuance is hourly (or SPECI when conditions change significantly), and the parsing surface area is substantial — covered in detail elsewhere on this blog. WeatherAPI gives you fresher interpolated values, a consistent JSON structure, and the humidity fields you need for the full DA calculation, all in one call. For a flight planning context where pilots will cross-reference the actual METAR anyway, the API-computed DA is a useful supplement, not a replacement.
The one real caveat: WeatherAPI’s pressure figure at very small or private airstrips is interpolated from model data, not a certified surface observation. Label it accordingly — a “calculated estimate” tag and a link to the nearest ASOS/AWOS keeps you honest and keeps pilots from over-trusting an automated number for a short-field departure at gross weight on a 95°F afternoon.
