From b3fda5303666f63b5d93e1ce76119e1f75cc41dd Mon Sep 17 00:00:00 2001 From: Leon Krieg Date: Thu, 19 Sep 2024 17:28:41 +0200 Subject: [PATCH] Implement all required conversion functions --- src/common/math.c | 51 +++++++++++++++++++++++++++++------------------ 1 file changed, 32 insertions(+), 19 deletions(-) diff --git a/src/common/math.c b/src/common/math.c index be86f41..7ed003a 100644 --- a/src/common/math.c +++ b/src/common/math.c @@ -1,43 +1,56 @@ #include "common.h" +#include + // https://bmcnoldy.earth.miami.edu/Humidity.html // https://onlinelibrary.wiley.com/doi/abs/10.1002/andp.18280890511 // https://onlinelibrary.wiley.com/doi/abs/10.1002/andp.18441370202 -// NOTE: EXP and LN are exponential and natural logarithm functions +// TODO: Make sure all results for conversion functions are correct. + +// Conversion constants +static const float C1 = 100.0f; +static const float C2 = 243.04f; +static const float C3 = 17.625f; // Dewpoint TD -// 243.04*(LN(RH/100)+((17.625*T)/(243.04+T)))/ -// (17.625-LN(RH/100)-((17.625*T)/(243.04+T))) float Dewp(float t, float rh) { - // TODO - UNUSED(tc); - UNUSED(rh); + float a, b; - return 0.0f; + a = log(rh / C1); + b = C3 * t / (C2 + t); + + // 243.04*(LN(RH/100)+((17.625*T)/(243.04+T)))/ + // (17.625-LN(RH/100)-((17.625*T)/(243.04+T))) + + return C2 * (a + b) / (C3 - a - b); } // Temperature T -// 243.04*(((17.625*TD)/(243.04+TD))-LN(RH/100))/ -// (17.625+LN(RH/100)-((17.625*TD)/(243.04+TD))) float Temp(float td, float rh) { - // TODO - UNUSED(dc); - UNUSED(rh); + float a, b; - return 0.0f; + a = log(rh / C1); + b = C3 * td / (C2 + td); + + // 243.04*(((17.625*TD)/(243.04+TD))-LN(RH/100))/ + // (17.625+LN(RH/100)-((17.625*TD)/(243.04+TD))) + + return C2 * (b - a) / (C3 + a - b); } // Relative Humidity RH -// 100*(EXP((17.625*TD)/(243.04+TD))/ -// EXP((17.625*T)/(243.04+T))) float Rhum(float t, float td) { - // TODO - UNUSED(tc); - UNUSED(dc); + float a, b; - return 0.0f; + a = C3 * td / (C2 + td); + b = C3 * t / (C2 + t); + + // 100*(EXP((17.625*TD)/(243.04+TD))/ + // EXP((17.625*T)/(243.04+T))) + + return C1 * (exp(a) / exp(b)); }