Skip to content

Commit

Permalink
generic optimized formula (save ram and execution time). added explan…
Browse files Browse the repository at this point in the history
…ation to

reconstruct our thoughts.
  • Loading branch information
coelner committed Mar 2, 2018
1 parent 6ac91b1 commit 0e3291d
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 12 deletions.
4 changes: 2 additions & 2 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -338,9 +338,9 @@ or
#### float HeatIndex(float temperature, float humidity, TempUnit tempUnit = TempUnit_Celsius)

Calculate the heat index based on the temperature and humidity with the specified units.
The NWS algorithm is used.
The U.S. NWS algorithm is used.
```
return: float heat index
return: float heat index in TempUnit
* Temperature: float
values: any float related to TempUnit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ void printBME280Data
client->print("\t\tHeat Index: ");
float heatIndex = EnvironmentCalculations::HeatIndex(temp, hum, envTempUnit);
client->print(heatIndex);
client->print("°"+ String(envTempUnit == EnvironmentCalculations::TempUnit_Celsius ? "C" :"F"));

client->print("\t\tAbsolute Humidity: ");
client->println(absHum);
Expand Down
26 changes: 16 additions & 10 deletions src/EnvironmentCalculations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ float EnvironmentCalculations::Altitude
if (!isnan(pressure) && !isnan(referencePressure) && !isnan(outdoorTemp))
{
if(tempUnit != TempUnit_Celsius)
outdoorTemp = (outdoorTemp - 32.0) * (5.0 / 9.0); /*conversion to [C]*/
outdoorTemp = (outdoorTemp - 32.0) * (5.0 / 9.0); /*conversion to [°C]*/

altitude = pow(referencePressure / pressure, 0.190234) - 1;
altitude *= ((outdoorTemp + 273.15) / 0.0065);
Expand Down Expand Up @@ -96,6 +96,7 @@ float EnvironmentCalculations::AbsoluteHumidity


/****************************************************************/
//FYI: https://ehp.niehs.nih.gov/1206273/ in detail this flow graph: https://ehp.niehs.nih.gov/wp-content/uploads/2013/10/ehp.1206273.g003.png
float EnvironmentCalculations::HeatIndex
(
float temperature,
Expand All @@ -114,27 +115,32 @@ float EnvironmentCalculations::HeatIndex
{
temperature = (temperature * (9.0 / 5.0) + 32.0); /*conversion to [°F]*/
}

// Using both Rothfusz and Steadman's equations
// https://www.wpc.ncep.noaa.gov/html/heatindex_equation.shtml
if (temperature <= 40)
{
heatIndex = temperature;
heatIndex = temperature; //first red block
}
else
{
heatIndex = 0.5 * (temperature + 61.0 + ((temperature - 68.0) * 1.2) + (humidity * 0.094));
heatIndex = 0.5 * (temperature + 61.0 + ((temperature - 68.0) * 1.2) + (humidity * 0.094)); //calculate A -- from the official site, not the flow graph

if (heatIndex >= 79)
{
// Using both Rothfusz and Steadman's equations
// https://www.wpc.ncep.noaa.gov/html/heatindex_equation.shtml
/*
* calculate B
* the following calculation is optimized. Simply spoken, reduzed cpu-operations to minimize used ram and runtime.
* Check the correctness with the following link:
* https://www.wolframalpha.com/input/?source=nav&i=b%3D+x1+%2B+x2*T+%2B+x3*H+%2B+x4*T*H+%2B+x5*T*T+%2B+x6*H*H+%2B+x7*T*T*H+%2B+x8*T*H*H+%2B+x9*T*T*H*H
*/
heatIndex = hi_coeff1
+ (hi_coeff2 + hi_coeff4 * humidity + temperature * (hi_coeff5 + hi_coeff7 * humidity)) * temperature
+ (hi_coeff3 + humidity * (hi_coeff6 + temperature * (hi_coeff8 + hi_coeff9 * temperature))) * humidity;

//third red block
if ((humidity < 13) && (temperature >= 80.0) && (temperature <= 112.0))
{
heatIndex -= ((13.0 - humidity) * 0.25) * sqrt((17.0 - abs(temperature - 95.0)) * 0.05882);
}
} //fourth red block
else if ((humidity > 85.0) && (temperature >= 80.0) && (temperature <= 87.0))
{
heatIndex += (0.02 * (humidity - 85.0) * (87.0 - temperature));
Expand All @@ -148,7 +154,7 @@ float EnvironmentCalculations::HeatIndex
}
else
{
return heatIndex;
return heatIndex; //fifth red block
}
}

Expand All @@ -167,7 +173,7 @@ float EnvironmentCalculations::EquivalentSeaLevelPressure
if(!isnan(altitude) && !isnan(temp) && !isnan(pres))
{
if(tempUnit != TempUnit_Celsius)
temp = (temp - 32.0) * (5.0 / 9.0); /*conversion to [C]*/
temp = (temp - 32.0) * (5.0 / 9.0); /*conversion to [°C]*/

if(altUnit != AltitudeUnit_Meters)
altitude *= 0.3048; /*conversion to meters*/
Expand Down
2 changes: 2 additions & 0 deletions src/EnvironmentCalculations.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ namespace EnvironmentCalculations
/////////////////////////////////////////////////////////////////
/// Calculate the heatindex based on the humidity and temperature
/// in tempUnit.
/// The formula based on the Heat Index Equation of the US National Weather Service
/// https://www.wpc.ncep.noaa.gov/html/heatindex_equation.shtml
/// @param temperature in tempUnit
/// @param humidity in percentage
/// @param temptUnit in °C or °F. default=TempUnit_Celsius
Expand Down

0 comments on commit 0e3291d

Please sign in to comment.