Skip to content

Commit

Permalink
improve windchill() and heatIndex() (#15)
Browse files Browse the repository at this point in the history
* improve windchill() and heatIndex()
  • Loading branch information
RobTillaart committed Jan 9, 2022
1 parent 40883a0 commit 5c2b9f5
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 35 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,17 @@ else ==> formula assumes wind speed @ 1.5 meter
- **float WindChill_C_mps(float Celsius, float meterPerSecond, bool convert = true)**


Indicative table (subjective).

| wind chill °C | description |
|:-------------:|:---------------------|
| > -10 | cold |
| -10 .. -25 | very cold ! |
| -25 .. -35 | very very cold ! |
| -35 .. -55 | chance of frostbite |
| < -55 | serious dangerous |


## temperatureConverter

Since version 0.3.2 a temperature convertor class is added to convert to and from
Expand Down
3 changes: 1 addition & 2 deletions examples/heatindexC_table/heatindexC_table.ino
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void setup()
Serial.println();
}
Serial.println();
Serial.println();
Serial.println(heatIndexC(20, 50));

Serial.print("Done...");
}
Expand All @@ -50,4 +50,3 @@ void loop()


// -- END OF FILE --

10 changes: 6 additions & 4 deletions examples/heatindexC_test/heatindexC_test.ino
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// FILE: heatindexC_test.ino
// AUTHOR: Rob Tillaart
// PURPOSE: demo
// PURPOSE: demo - test average performance per call in micros.
// DATE: 2020-04-04


Expand All @@ -21,16 +21,19 @@ void setup()

Serial.println(heatIndexC(25, 50), 2);

start = millis();
start = micros();
int cnt = 0;
for (int cel = 10; cel < 80; cel++)
{
for (int hum = 1; hum < 100; hum++)
{
hi = heatIndexC(cel, hum);
cnt++;
}
}
duration1 = millis() - start;
duration1 = micros() - start;
Serial.println(duration1);
Serial.println(1.0 * duration1 / cnt);

Serial.print("Done...");
}
Expand All @@ -42,4 +45,3 @@ void loop()


// -- END OF FILE --

35 changes: 35 additions & 0 deletions examples/windChill_test/windChill_test.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// FILE: windChill_test.ino
// AUTHOR: Rob Tillaart
// PURPOSE: demo
// DATE: 2022-01-09


#include "temperature.h"

void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);

Serial.println(WindChill_F_mph (0, 10, true), 3);
Serial.println(WindChill_C_kmph(0, 10, true), 3);
Serial.println(WindChill_C_mps (0, 10, true), 3);
Serial.println(WindChill_C_kmph(-20, 5, true), 3);


Serial.println(WindChill_F_mph (0, 10, false), 3);
Serial.println(WindChill_C_kmph(0, 10, false), 3);
Serial.println(WindChill_C_mps (0, 10, false), 3);


Serial.print("Done...");
}


void loop()
{
}


// -- END OF FILE --
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/Temperature"
},
"version": "0.3.2",
"version": "0.3.3",
"license": "MIT",
"frameworks": "arduino",
"platforms": "*",
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=Temperature
version=0.3.2
version=0.3.3
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Library with weather related functions.
Expand Down
44 changes: 24 additions & 20 deletions temperature.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once
//
// FILE: temperature.h
// VERSION: 0.3.2
// VERSION: 0.3.3
// DATE: 2015-03-29
// PURPOSE: collection temperature functions
//
Expand All @@ -23,9 +23,11 @@
// 0.3.2 2022-01-08 Renamed Celcius to Celsius.
// added a TempConvertor class for more exotic scales.
// added baroToSeaLevel() - kudos to RobertDB59
// 0.3.3 2022-01-09 added working limits to windchill() and heatIndex()


#define TEMPERATURE_VERSION (F("0.3.2"))

#define TEMPERATURE_VERSION (F("0.3.3"))


inline float Fahrenheit(float celsius)
Expand All @@ -34,9 +36,9 @@ inline float Fahrenheit(float celsius)
}


inline float Celsius(float fahrenheit)
inline float Celsius(float Fahrenheit)
{
return (fahrenheit - 32) * 0.55555555555; // 5.0 / 9.0 = 0.555...
return (Fahrenheit - 32) * 0.55555555555; // 5.0 / 9.0 = 0.555...
}


Expand Down Expand Up @@ -96,14 +98,16 @@ float humidex(float celsius, float dewPoint)

// 0.3.0 => https://www.wpc.ncep.noaa.gov/html/heatindex_equation.shtml
// previous https://en.wikipedia.org/wiki/Heat_index
// TODO add valid range for TF & R
// TF = temp in Fahrenheit
// RH = relative humidity in %
float heatIndex(float TF, float RH)
{
float HI = 0;
// Steadman's formula ==> can be optimized :: HI = TF * 1.1 - 10.3 + RH * 0.047
float HI = 0.5 * (TF + 61.0 + ((TF - 68.0) * 1.2) + (RH * 0.094));


if (TF >= 80)
// Rothfusz regression
if (HI >= 80)
{
const float c1 = -42.379;
const float c2 = 2.04901523;
Expand All @@ -128,23 +132,21 @@ float heatIndex(float TF, float RH)
{
HI += ((RH - 85) / 10) * ((87 - TF) / 5);
}
return HI;
}

HI = 0.5 * (TF + 61.0 + ((TF - 68.0) * 1.2) + (RH * 0.094));

return HI;
}


// 0.3.0 => https://www.wpc.ncep.noaa.gov/html/heatindex_equation.shtml
// previous https://en.wikipedia.org/wiki/Heat_index
// TODO add valid range for TF & RH
// TODO optimize?
float heatIndexC(float C, float humidity)
// TC = temp in Celsius
// RH = relative humidity in %
float heatIndexC(float TC, float RH)
{
float TF = Fahrenheit(C);
return Celsius(heatIndex(TF, humidity));
if ( (TC < 27) || (RH < 40)) return TC;
float TF = Fahrenheit(TC);
return Celsius(heatIndex(TF, RH));

/*
const float c1 = -8.78469475556;
Expand All @@ -167,25 +169,27 @@ float heatIndexC(float C, float humidity)


// https://en.wikipedia.org/wiki/Wind_chill
// US = Fahrenheit / miles
// METRIC = Celsius / meter/sec
// US = Fahrenheit / miles / hour
// METRIC = Celsius / meter / hour (sec)
// wind speed @ 10 meter,
// if convert is true => wind speed will be converted to 1.5 meter
// else ==> formula assumes wind speed @ 1.5 meter


// US
float WindChill_F_mph(const float fahrenheit, const float milesPerHour, const bool convert = true)
float WindChill_F_mph(const float Fahrenheit, const float milesPerHour, const bool convert = true)
{
if ((milesPerHour < 3.0) || (Fahrenheit > 50)) return Fahrenheit;
float windSpeed = milesPerHour;
if (convert) windSpeed = pow(milesPerHour, 0.16);
return 35.74 + 0.6125 * fahrenheit + (0.4275 * fahrenheit - 35.75) * windSpeed;
return 35.74 + 0.6125 * Fahrenheit + (0.4275 * Fahrenheit - 35.75) * windSpeed;
}


// METRIC
// METRIC - standard wind chill formula for Environment Canada
float WindChill_C_kmph(const float Celsius, const float kilometerPerHour, const bool convert = true)
{
if ((kilometerPerHour < 4.8) || (Celsius > 10)) return Celsius;
float windSpeed = kilometerPerHour;
if (convert) windSpeed = pow(kilometerPerHour, 0.16);
return 13.12 + 0.6215 * Celsius + (0.3965 * Celsius - 11.37) * windSpeed;
Expand Down
18 changes: 11 additions & 7 deletions test/unit_test_001.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ unittest(test_heatIndex)
assertEqualFloat(14.050, heatIndex(20, 50), 0.001);
assertEqualFloat(66.850, heatIndex(68, 50), 0.001);
// Celsius
assertEqualFloat(19.361, heatIndexC(20, 50), 0.001);
assertEqualFloat(20.000, heatIndexC(20, 50), 0.001);
}


Expand Down Expand Up @@ -110,12 +110,16 @@ unittest(test_heatIndex_2)

unittest(test_windChill)
{
assertEqualFloat(107.108, WindChill_F_mph (100, 10, true), 0.001);
assertEqualFloat(40.8862, WindChill_C_kmph(37, 10, true), 0.001);
assertEqualFloat(41.9713, WindChill_C_mps (37, 10, true), 0.001);
assertEqualFloat(166.99, WindChill_F_mph (100, 10, false), 0.001);
assertEqualFloat(69.1205, WindChill_C_kmph(37, 10, false), 0.001);
assertEqualFloat(154.934, WindChill_C_mps (37, 10, false), 0.001);
fprintf(stderr, "\n Fahrenheit\n");
assertEqualFloat( -15.9, WindChill_F_mph (0, 10, true), 0.1);
assertEqualFloat(-321.8, WindChill_F_mph (0, 10, false), 0.1);

fprintf(stderr, "\n Celsius\n");
assertEqualFloat( -3.3, WindChill_C_kmph(0, 10, true), 0.1);
assertEqualFloat( -7.1, WindChill_C_mps (0, 10, true), 0.1);
assertEqualFloat( -24.3, WindChill_C_kmph(-20, 5, true), 0.1);
assertEqualFloat(-100.5, WindChill_C_kmph(0, 10, false), 0.1);
assertEqualFloat(-396.2, WindChill_C_mps (0, 10, false), 0.1);
}


Expand Down

0 comments on commit 5c2b9f5

Please sign in to comment.