This commit is contained in:
2025-11-18 10:14:45 +01:00
parent c07db58a7e
commit ca5e6b479e
3 changed files with 287 additions and 273 deletions

View File

@@ -49,7 +49,7 @@ QMap <QByteArray, QPair<QString, QByteArray>> propertyName
// {"dewptf", {"Point de rosée", "°C"}},
{"windchill", {"Température ressentie", "°C"}},
{"winddir", {"Direction du vent", "°"}},
{"windspeedmph", {"Vitesse du vent", " km/h"}},
{"windspeedmph", {"Vitesse du vent", " km/h"}},
{"windgustmph", {"Rafales", " km/h"}},
{"rainin", {"Pluie", " mm/h"}},
{"dailyrainin", {"Pluie de la journée", " mm"}},
@@ -58,7 +58,7 @@ QMap <QByteArray, QPair<QString, QByteArray>> propertyName
// {"solarradiation", {"Énergie solaire", " W/m²"}},
// {"indoortempf", {"Température intérieure", "°C"}},
// {"indoorhumidity", {"Humidité intérieure", "%"}},
{"baromin", {"Pression atmosphérique", " hPa"}},
{"baromin", {"Pression atmosphérique", " hPa"}},
// {"lowbatt", {"Alerte batterie faible", ""}},
{"UV", {"Alerte UV", ""}}
};
@@ -74,7 +74,7 @@ QList <QList <QVariant>> forceVent
{38, "Bonne brise", 1},
{49, "Vent frais", 3},
{61, "Grand vent", 4},
{74, "Vent fort", 4},
{74, "Vent fort", 4},
{88, "Vent violent", 5},
{117, "Tempête", 5},
{1000, "Ouragan", 5}
@@ -126,11 +126,11 @@ void Pws2mqtt::init()
return QHttpServerResponse("text/plain", "Success\n");
});
for (auto [name, pair]: propertyName.asKeyValueRange())
{
propertyList[name].append(formatNotifString(pair.first, pair.second, "0"));
debug (DEBUGMACRO, "Init " + name + " => " + propertyList[name], DEBUG);
}
for (auto [name, pair]: propertyName.asKeyValueRange())
{
propertyList[name].append(formatNotifString(pair.first, pair.second, "0"));
debug (DEBUGMACRO, "Init " + name + " => " + propertyList[name], DEBUG);
}
}
void Pws2mqtt::listeningHttp()
@@ -154,12 +154,14 @@ void Pws2mqtt::parseData(QList<std::pair<QString, QString>> queryList)
QString notif = "";
QStringList priorityList {"", "min", "low", "default", "High", "urgent"};
QString attachment = "";
QString tag = "";
quint8 priority = 2;
double propertyValue = 0;
bool propertyFlag = false;
bool deviceFlag = false;
static QDateTime timer = QDateTime::currentDateTime().addSecs(-2000);
QString tag = "";
double propertyValue = 0;
bool propertyFlag = false;
bool deviceFlag = false;
static QDateTime timer = QDateTime::currentDateTime().addSecs(-2000);
static QHash <QByteArray, quint8> priority = {};
static QHash <QByteArray, quint8> announced = {};
static bool start = false;
debug(DEBUGMACRO, "looping list of query", DEBUG);
@@ -206,6 +208,13 @@ void Pws2mqtt::parseData(QList<std::pair<QString, QString>> queryList)
timeTemp = timeTemp.currentDateTime().addSecs(300);
propertyList[name] = formatNotifString(propertyName[name].first, propertyName[name].second, QByteArray::number(propertyValue));
propertiesValue[name] = propertyValue;
if (propertyValue > 35)
{
priority[name] = 4;
}else if (propertyValue > 27)
{
priority[name] = 3;
}
}
}else if (name == "humidity")
{
@@ -217,26 +226,26 @@ void Pws2mqtt::parseData(QList<std::pair<QString, QString>> queryList)
//notif += formatNotifString (propertyName[name].first, propertyName[name].second , value);
propertyList[name] = formatNotifString(propertyName[name].first, propertyName[name].second, QByteArray::number(propertyValue));
propertiesValue[name] = propertyValue;
}
}
}else if (name == "winddir")
{
propertyValue = value.toFloat();
rotateAndSaveImage(this->inputPath, this->outputPath, (qreal)propertyValue);
debug (DEBUGMACRO, name + " : " + QByteArray::number(propertyValue), DEBUG);
propertyList[name] = formatNotifString(propertyName[name].first, propertyName[name].second, QByteArray::number(propertyValue));
propertiesValue[name] = propertyValue;
}else if (name == "windspeedmph")
propertiesValue[name] = propertyValue;
}else if (name == "windspeedmph")
{
QString msg = "";
quint8 i;
static QDateTime timeWind = QDateTime::currentDateTime().addSecs(-600);
quint8 i;
static QDateTime timeWind = QDateTime::currentDateTime().addSecs(-600);
quint8 windPriority = 1;
propertiesValue[name] = round(mphTokmh(value.toFloat()));
debug (DEBUGMACRO, name + " : " + QByteArray::number(propertiesValue[name]), DEBUG);
for (i=0; i<forceVent.size();i++)
for (i=0; i<forceVent.size();i++)
{
if (propertiesValue[name] <= forceVent[i][0].toDouble())
{
@@ -247,34 +256,34 @@ void Pws2mqtt::parseData(QList<std::pair<QString, QString>> queryList)
}
propertyList[name] = formatNotifString ("Vent - " + msg, propertyName[name].second , QByteArray::number(propertiesValue[name]));
propertiesValue["forcevent"] = i;
if (i >= 5)
{
attachment = this->outputPath;
}
if (propertiesValue["forcevent"] != i)
{
priority = qMax(priority, windPriority);
propertiesValue["forcevent"] = i;
if (i >= 5)
{
attachment = this->outputPath;
}
debug (DEBUGMACRO, "priority = " + QString::number(priority), DEBUG);
}else if (name == "windgustmph")
{
QString msg = "";
static QDateTime timeWind = QDateTime::currentDateTime().addSecs(-600);
propertiesValue[name] = round(mphTokmh(value.toFloat()));
if (propertiesValue["forcevent"] != i)
{
priority[name] = qMax(priority[name], windPriority);
}
debug (DEBUGMACRO, "priority = " + QString::number(priority[name]), DEBUG);
}else if (name == "windgustmph")
{
QString msg = "";
static QDateTime timeWind = QDateTime::currentDateTime().addSecs(-600);
propertiesValue[name] = round(mphTokmh(value.toFloat()));
debug (DEBUGMACRO, name + " : " + QByteArray::number(propertiesValue[name]), DEBUG);
propertyList[name] = formatNotifString (propertyName[name].first, propertyName[name].second , QByteArray::number(propertiesValue[name]));
propertyList[name] = formatNotifString (propertyName[name].first, propertyName[name].second , QByteArray::number(propertiesValue[name]));
if (propertiesValue[name] > 50)
{
priority = qMax(priority, (quint8)4);
}
debug (DEBUGMACRO, "priority = " + QString::number(priority), DEBUG);
}else if (name == "rainin")
{
priority[name] = qMax(priority[name], (quint8)4);
}
debug (DEBUGMACRO, "priority = " + QString::number(priority[name]), DEBUG);
}else if (name == "rainin")
{
static double ecart;
quint8 raininPriority = 1;
@@ -290,13 +299,13 @@ void Pws2mqtt::parseData(QList<std::pair<QString, QString>> queryList)
if (compare (propertiesValue[name], propertyValue, ecart))
{
QString pluviosite = getPluviosite(propertyValue, raininPriority);
priority = setPriority (priority, raininPriority);
priority[name] = setPriority (priority[name], raininPriority);
//debug (DEBUGMACRO, "Notif = #" + notif + "#", DEBUG);
propertyList[name] = formatNotifString(pluviosite, propertyName[name].second, QByteArray::number(propertyValue));
propertyList[name] = formatNotifString(pluviosite, propertyName[name].second, QByteArray::number(propertyValue));
propertiesValue[name] = propertyValue;
}
debug (DEBUGMACRO, "priority = " + QString::number(priority), DEBUG);
}else if (name == "dailyrainin")
debug (DEBUGMACRO, "priority = " + QString::number(priority[name]), DEBUG);
}else if (name == "dailyrainin")
{
static double ecart;
propertyValue = round(pair.second.toFloat()*100)/100;
@@ -310,63 +319,63 @@ void Pws2mqtt::parseData(QList<std::pair<QString, QString>> queryList)
debug (DEBUGMACRO, name + " : " + QByteArray::number(propertyValue), DEBUG);
if (compare (propertiesValue[name], propertyValue, ecart))
{
//priority = setPriority (priority, 3);
//priority[name] = setPriority (priority[name], 3);
//debug (DEBUGMACRO, "Notif = #" + notif + "#", DEBUG);
propertyList[name] = formatNotifString(propertyName[name].first, propertyName[name].second, QByteArray::number(propertyValue));
propertiesValue[name] = propertyValue;
propertyList[name] = formatNotifString(propertyName[name].first, propertyName[name].second, QByteArray::number(propertyValue));
propertiesValue[name] = propertyValue;
}
debug (DEBUGMACRO, "priority = " + QString::number(priority), DEBUG);
}else if (name == "baromin")
debug (DEBUGMACRO, "priority = " + QString::number(priority[name]), DEBUG);
}else if (name == "baromin")
{
//static QDateTime timePrevision = QDateTime::currentDateTime().addSecs(-1000);
propertyValue = round(tohPa(pair.second.toFloat()) * 100) / 100;
debug (DEBUGMACRO, "Barometre en hPa : " + QByteArray::number(propertyValue), DEBUG);
baro->addPressure(propertyValue);
propertyList[name] = formatNotifString(propertyName[name].first, "", QByteArray::number(propertyValue));
propertiesValue[name] = propertyValue;
propertyValue = round(tohPa(pair.second.toFloat()) * 100) / 100;
debug (DEBUGMACRO, "Barometre en hPa : " + QByteArray::number(propertyValue), DEBUG);
baro->addPressure(propertyValue);
propertyList[name] = formatNotifString(propertyName[name].first, "", QByteArray::number(propertyValue));
propertiesValue[name] = propertyValue;
static QString prevision;
auto ret = baro->forecast(propertiesValue["tempf"], propertiesValue["humidity"]);
static QString prevision;
auto ret = baro->forecast(propertiesValue["tempf"], propertiesValue["humidity"]);
prevision = ret.message;
//if (prevision != newPrevision)
//{
priority = setPriority(priority, ret.urgency);
tag += ret.icons;
prevision = ret.message;
//if (prevision != newPrevision)
//{
priority[name] = setPriority(priority[name], ret.urgency);
tag += ret.icons;
propertyList["prevision"] = prevision;
debug (DEBUGMACRO, "priority = " + QString::number(priority), DEBUG);
}else if (name == "UV")
propertyList["prevision"] = prevision;
debug (DEBUGMACRO, "priority = " + QString::number(priority[name]), DEBUG);
}else if (name == "UV")
{
static QDateTime timeUV = QDateTime::currentDateTime().addSecs(-600);
propertyValue = pair.second.toUInt();
propertyValue = pair.second.toUInt();
debug (DEBUGMACRO, name + " : " + QByteArray::number(propertyValue), DEBUG);
if (compare (propertiesValue[name], propertyValue, 1) and timeUV < QDateTime::currentDateTime())
{
//notif += formatNotifString (propertyName[name].first, propertyName[name].second , value);
if (propertyValue == 5 )
priority = setPriority (priority, 4);
priority[name] = setPriority (priority[name], 4);
if (propertyValue >= 6 )
priority = setPriority (priority, 5);
priority[name] = setPriority (priority[name], 5);
timeUV = timeUV.currentDateTime().addSecs(300);
propertiesValue[name] = propertyValue;
debug (DEBUGMACRO, "Notif = #" + notif + "#", DEBUG);
propertyList[name] = formatNotifString(propertyName[name].first, "", QByteArray::number(propertyValue));
}
debug (DEBUGMACRO, "priority = " + QString::number(priority), DEBUG);
propertyList[name] = formatNotifString(propertyName[name].first, "", QByteArray::number(propertyValue));
}
debug (DEBUGMACRO, "priority = " + QString::number(priority[name]), DEBUG);
}else if (name == "solarradiation")
{
propertiesValue[name] = round(pair.second.toFloat()*100/100);
}
}else if (name == "solarradiation")
{
propertiesValue[name] = round(pair.second.toFloat()*100/100);
}
}
}
propertiesValue["windchill"] = calc.computeUTCI(propertiesValue["tempf"], propertiesValue["vent"], propertiesValue["solarradiation"], propertiesValue["humidity"]);
propertyList["windchill"] = formatNotifString(propertyName["windchill"].first, propertyName["windchill"].second, QByteArray::number(propertiesValue["windchill"]));
//windChill(propertiesValue["tempf"], propertiesValue["vent"]);
//calculerHumidex(propertiesValue["tempf"], propertiesValue["humidity"]);
propertiesValue["windchill"] = calc.computeUTCI(propertiesValue["tempf"], propertiesValue["vent"], propertiesValue["solarradiation"], propertiesValue["humidity"]);
propertyList["windchill"] = formatNotifString(propertyName["windchill"].first, propertyName["windchill"].second, QByteArray::number(propertiesValue["windchill"]));
//windChill(propertiesValue["tempf"], propertiesValue["vent"]);
//calculerHumidex(propertiesValue["tempf"], propertiesValue["humidity"]);
if (!jsonString.isEmpty())
{
@@ -380,66 +389,70 @@ void Pws2mqtt::parseData(QList<std::pair<QString, QString>> queryList)
debug(DEBUGMACRO, "current datetime : " + QDateTime::currentDateTime().toString() + ", timer = " + timer.toString(), DEBUG);
if (priority > 3 or QDateTime::currentDateTime() > timer.addSecs(1800))
//bool changed = false;
quint8 aPriority= 0;
debug(DEBUGMACRO, "looping to fill notif", DEBUG);
for (auto [name, value]: propertyList.asKeyValueRange())
{
bool changed = false;
timer = QDateTime::currentDateTime();
debug(DEBUGMACRO, "looping to fill notif, priority = " + QString::number(priority), DEBUG);
if (precPropertyList.size() == 0)
{
changed = true;
}
for (auto [name, value]: propertyList.asKeyValueRange())
if (precPropertyList.contains(name))
{
if (precPropertyList.contains(name))
{
if (precPropertyList[name] != propertyList[name])
{
debug(DEBUGMACRO, "changed = true", DEBUG);
changed = true;
}
}
if (precPropertyList[name] != propertyList[name])
{
if (announced.contains(name) and priority.contains(name))
{
if (announced[name] != priority[name])
{
debug(DEBUGMACRO, "changed = true", DEBUG);
announced[name] = priority[name];
//changed = true;
aPriority = qMax(aPriority, priority[name]);
}
}
}
}
debug(DEBUGMACRO, "Name = " + name + ", value = " + value, DEBUG);
if (! (name == "prevision"))
{
notif += propertyList[name];
}
}
if (changed)
{
notify (notif, priorityList[priority], attachment, tag);
precPropertyList = propertyList;
}
debug(DEBUGMACRO, "Name = " + name + ", value = " + value, DEBUG);
if (name != "prevision")
{
notif += propertyList[name];
}
}
if (QDateTime::currentDateTime() > timer.addSecs(3600) or start == false or aPriority >= 3)
{
timer = QDateTime::currentDateTime();
start = true;
notify (notif, priorityList[aPriority], attachment, tag);
precPropertyList = propertyList;
notif = "**Prévisions à 12/24h**\n" + propertyList["prevision"];
notify (notif, priorityList[priority], "", tag);
debug(DEBUGMACRO, "calling notify with notif = #" + notif + "#", DEBUG);
}
notif = "**Prévisions à 12/24h**\n" + propertyList["prevision"];
notify (notif, priorityList[aPriority], "", tag);
debug(DEBUGMACRO, "calling notify with notif = #" + notif + "#", DEBUG);
}
debug(DEBUGMACRO, "parseData: Returning", DEBUG);
}
double calculerUTCI(double temperature, double humiditeRelative, double vitesseVent, double rayonnement)
{
double e = (humiditeRelative / 100.0) * 6.105 * exp((17.27 * temperature) / (237.7 + temperature));
debug(DEBUGMACRO, "UTCI => e = " + QString::number(e), DEBUG);
double V = vitesseVent * 5.0 / 18.0; // Vitesse du vent en m/s
debug(DEBUGMACRO, "UTCI => V = " + QString::number(V), DEBUG);
double e = (humiditeRelative / 100.0) * 6.105 * exp((17.27 * temperature) / (237.7 + temperature));
debug(DEBUGMACRO, "UTCI => e = " + QString::number(e), DEBUG);
double V = vitesseVent * 5.0 / 18.0; // Vitesse du vent en m/s
debug(DEBUGMACRO, "UTCI => V = " + QString::number(V), DEBUG);
double utci = temperature +
(0.607562 + 0.022437 * e + 7.3869e-4 * temperature * e - 3.5582e-6 * temperature * temperature * e) +
(-11.4 + 0.11 * temperature + 0.55 * V) +
(0.0023 * temperature * temperature - 0.118 * V - 0.0014 * temperature * V);
debug(DEBUGMACRO, "UTCI = " + QString::number(utci), DEBUG);
double utci = temperature +
(0.607562 + 0.022437 * e + 7.3869e-4 * temperature * e - 3.5582e-6 * temperature * temperature * e) +
(-11.4 + 0.11 * temperature + 0.55 * V) +
(0.0023 * temperature * temperature - 0.118 * V - 0.0014 * temperature * V);
debug(DEBUGMACRO, "UTCI = " + QString::number(utci), DEBUG);
// Ajustement simplifié pour le rayonnement
double ref = 200.0; // Rayonnement de référence (W/m²)
double k = 0.02; // Coefficient empirique
utci = utci + k * (rayonnement - ref);
debug(DEBUGMACRO, "UTCI = " + QString::number(utci), DEBUG);
return round(utci * 100) / 100;
// Ajustement simplifié pour le rayonnement
double ref = 200.0; // Rayonnement de référence (W/m²)
double k = 0.02; // Coefficient empirique
utci = utci + k * (rayonnement - ref);
debug(DEBUGMACRO, "UTCI = " + QString::number(utci), DEBUG);
return round(utci * 100) / 100;
}
QString getPluviosite(double value, quint8 &priority)
@@ -509,13 +522,13 @@ bool compare (double value, double currentValue, double ecart)
QString formatNotifString (QString name, QString unit ,QByteArray value)
{
QString text = "- " + name + " : " + value;
QString text = "- " + name + " : " + value;
if (!unit.isEmpty())
{
text += unit;
//"- " + propertyName[name].first + " : " + value + " " + propertyName[name].second + " ";
//"- " + propertyName[name].first + " : " + value + " " + propertyName[name].second + " ";
}
return text += "\n";
return text += "\n";
}
static size_t ReadFile(void *ptr, size_t size, size_t nmemb, void *stream) {
@@ -527,9 +540,9 @@ static size_t ReadFile(void *ptr, size_t size, size_t nmemb, void *stream) {
// Callback for curl library
static size_t WriteCallback(void *contents, size_t size, size_t nmemb, std::string *output)
{
size_t total_size = size * nmemb;
output->append((char*)contents, total_size);
return total_size;
size_t total_size = size * nmemb;
output->append((char*)contents, total_size);
return total_size;
}
void notify(QString notif, QString priority, QString inputPath, QString tag)
@@ -539,35 +552,35 @@ void notify(QString notif, QString priority, QString inputPath, QString tag)
std::string readBuffer;
priority = "Priority: " + priority;
debug (DEBUGMACRO, "notifying at priority " + priority + "with tag : " + tag + " - message : " + notif, DEBUG);
debug (DEBUGMACRO, "notifying at priority " + priority + "with tag : " + tag + " - message : " + notif, DEBUG);
// Initialise libcurl
curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
if (curl)
if (curl)
{
// Définis les en-têtes pour le titre et les priorités
struct curl_slist *headers = NULL;
notif.replace("\\", "\\\\\\'"); // Échappe les apostrophes
notif.replace("\\", "\\\\\\'"); // Échappe les apostrophes
if (!tag.isEmpty())
{
headers = curl_slist_append(headers, ("Tags: " + tag).toStdString().c_str());
}
headers = curl_slist_append(headers, "Title: Météo");
if (!tag.isEmpty())
{
headers = curl_slist_append(headers, ("Tags: " + tag).toStdString().c_str());
}
headers = curl_slist_append(headers, "Title: Météo");
headers = curl_slist_append(headers, priority.toStdString().c_str());
headers = curl_slist_append(headers, "Markdown: yes");
headers = curl_slist_append(headers, "Markdown: yes");
// headers = curl_slist_append(headers, "Config: /etc/ntfy.client.yml");
headers = curl_slist_append(headers, "Firebase: no");
// headers = curl_slist_append(headers, "Content-Type: charset=UTF-8; text/markdown"); // Ajout de l'encodage
struct curl_slist *h = headers;
while (h) {
debug(DEBUGMACRO, "Header: " + QString(h->data), DEBUG);
h = h->next;
}
debug(DEBUGMACRO, "Notif: " + notif, DEBUG);
struct curl_slist *h = headers;
while (h) {
debug(DEBUGMACRO, "Header: " + QString(h->data), DEBUG);
h = h->next;
}
debug(DEBUGMACRO, "Notif: " + notif, DEBUG);
// Configure la requête POST
curl_easy_setopt(curl, CURLOPT_URL, "http://localhost:81/Meteo");
@@ -575,11 +588,11 @@ void notify(QString notif, QString priority, QString inputPath, QString tag)
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, notif.toUtf8().constData());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
// Exécute la requête
res = curl_easy_perform(curl);
res = curl_easy_perform(curl);
// Vérifie les erreurs
if (res != CURLE_OK)
@@ -587,7 +600,7 @@ void notify(QString notif, QString priority, QString inputPath, QString tag)
debug(DEBUGMACRO, "Erreur libcurl :" + QString(curl_easy_strerror(res)), DEBUG);
} else
{
debug(DEBUGMACRO, "Réponse du serveur: " + QString::fromStdString(readBuffer), DEBUG);
debug(DEBUGMACRO, "Réponse du serveur: " + QString::fromStdString(readBuffer), DEBUG);
}
// Nettoie les ressources

View File

@@ -5,6 +5,7 @@
#include <QObject>
#include <QtMqtt>
#include <QtMqtt/QMqttClient>
#include <cstdint>
#define RED "\e[31m"
#define GREEN "\e[32m"

View File

@@ -5,7 +5,7 @@
#include <QRegularExpression>
UtciCalculator::UtciCalculator(QObject *parent)
: QObject(parent)
: QObject(parent)
{
}
@@ -14,165 +14,165 @@ UtciCalculator::UtciCalculator(QObject *parent)
// appear in the same order as in the official table (210 coefficients).
bool UtciCalculator::loadCoefficientsFromFile(const QString &path)
{
QFile f(path);
if (!f.open(QIODevice::ReadOnly | QIODevice::Text))
return false;
QFile f(path);
if (!f.open(QIODevice::ReadOnly | QIODevice::Text))
return false;
QTextStream in(&f);
QVector<double> coeffs;
coeffs.reserve(300);
QTextStream in(&f);
QVector<double> coeffs;
coeffs.reserve(300);
while (!in.atEnd()) {
QString line = in.readLine().trimmed();
if (line.isEmpty()) continue;
// Allow lines with comments or 'term coefficient' headings:
// We try to extract numbers from the line.
QStringList parts = line.split(QRegularExpression("[\\s,\\t]+"), Qt::SkipEmptyParts);
for (const QString &p : parts) {
bool ok = false;
double val = p.toDouble(&ok);
if (ok) coeffs.append(val);
}
}
if (coeffs.isEmpty()) return false;
while (!in.atEnd()) {
QString line = in.readLine().trimmed();
if (line.isEmpty()) continue;
// Allow lines with comments or 'term coefficient' headings:
// We try to extract numbers from the line.
QStringList parts = line.split(QRegularExpression("[\\s,\\t]+"), Qt::SkipEmptyParts);
for (const QString &p : parts) {
bool ok = false;
double val = p.toDouble(&ok);
if (ok) coeffs.append(val);
}
}
if (coeffs.isEmpty()) return false;
m_coeffs = coeffs;
return true;
m_coeffs = coeffs;
return true;
}
*/
// Magnus formula to estimate saturation vapour pressure (hPa) then return pa in kPa
double UtciCalculator::vapourPressureFromRH(double Ta, double rh)
{
// Magnus-Tetens approximation for saturation vapour pressure (hPa)
// valid roughly for -45C..+60C
// constants for water over liquid
double a = 17.27;
double b = 237.7; // °C
double es = 6.112 * qExp((a * Ta) / (Ta + b));
double ea = es * (rh / 100.0);
return ea / 10.0; // hPa -> kPa
// Magnus-Tetens approximation for saturation vapour pressure (hPa)
// valid roughly for -45C..+60C
// constants for water over liquid
double a = 17.27;
double b = 237.7; // °C
double es = 6.112 * qExp((a * Ta) / (Ta + b));
double ea = es * (rh / 100.0);
return ea / 10.0; // hPa -> kPa
}
double UtciCalculator::computeUTCI(double Ta, double va, double Tr, double rh) const
{
double pa = vapourPressureFromRH(Ta, rh);
double tm = Tr - Ta;
double offset = evalOffset(Ta, va, tm, pa);
return Ta + offset;
double pa = vapourPressureFromRH(Ta, rh);
double tm = Tr - Ta;
double offset = evalOffset(Ta, va, tm, pa);
return Ta + offset;
}
double UtciCalculator::evalOffset(double Ta, double va, double tm, double pa) const
{
if (m_coeffs.size() < 210)
return 0.0;
if (m_coeffs.size() < 210)
return 0.0;
// Pré-calcul des puissances
double Ta2 = Ta*Ta, Ta3 = Ta2*Ta, Ta4 = Ta3*Ta, Ta5 = Ta4*Ta, Ta6 = Ta5*Ta;
double va2 = va*va, va3 = va2*va, va4 = va3*va, va5 = va4*va, va6 = va5*va;
double tm2 = tm*tm, tm3 = tm2*tm, tm4 = tm3*tm, tm5 = tm4*tm, tm6 = tm5*tm;
double pa2 = pa*pa, pa3 = pa2*pa, pa4 = pa3*pa, pa5 = pa4*pa, pa6 = pa5*pa;
// Pré-calcul des puissances
double Ta2 = Ta*Ta, Ta3 = Ta2*Ta, Ta4 = Ta3*Ta, Ta5 = Ta4*Ta, Ta6 = Ta5*Ta;
double va2 = va*va, va3 = va2*va, va4 = va3*va, va5 = va4*va, va6 = va5*va;
double tm2 = tm*tm, tm3 = tm2*tm, tm4 = tm3*tm, tm5 = tm4*tm, tm6 = tm5*tm;
double pa2 = pa*pa, pa3 = pa2*pa, pa4 = pa3*pa, pa5 = pa4*pa, pa6 = pa5*pa;
QVector<double> terms(210, 0.0);
QVector<double> terms(210, 0.0);
// Construire les termes dans le même ordre que les coefficients ESM_3
terms[0] = 1.0; // constante
terms[1] = Ta;
terms[2] = Ta2;
terms[3] = Ta3;
terms[4] = Ta4;
terms[5] = Ta5;
terms[6] = Ta6;
// Construire les termes dans le même ordre que les coefficients ESM_3
terms[0] = 1.0; // constante
terms[1] = Ta;
terms[2] = Ta2;
terms[3] = Ta3;
terms[4] = Ta4;
terms[5] = Ta5;
terms[6] = Ta6;
terms[7] = va;
terms[8] = Ta*va;
terms[9] = Ta2*va;
terms[10] = Ta3*va;
terms[11] = Ta4*va;
terms[12] = Ta5*va;
terms[7] = va;
terms[8] = Ta*va;
terms[9] = Ta2*va;
terms[10] = Ta3*va;
terms[11] = Ta4*va;
terms[12] = Ta5*va;
terms[13] = va2;
terms[14] = Ta*va2;
terms[15] = Ta2*va2;
terms[16] = Ta3*va2;
terms[17] = Ta4*va2;
terms[13] = va2;
terms[14] = Ta*va2;
terms[15] = Ta2*va2;
terms[16] = Ta3*va2;
terms[17] = Ta4*va2;
terms[18] = va3;
terms[19] = Ta*va3;
terms[20] = Ta2*va3;
terms[21] = Ta3*va3;
terms[18] = va3;
terms[19] = Ta*va3;
terms[20] = Ta2*va3;
terms[21] = Ta3*va3;
terms[22] = va4;
terms[23] = Ta*va4;
terms[24] = Ta2*va4;
terms[22] = va4;
terms[23] = Ta*va4;
terms[24] = Ta2*va4;
terms[25] = va5;
terms[26] = Ta*va5;
terms[25] = va5;
terms[26] = Ta*va5;
terms[27] = va6;
terms[27] = va6;
terms[28] = tm;
terms[29] = Ta*tm;
terms[30] = Ta2*tm;
terms[31] = Ta3*tm;
terms[32] = Ta4*tm;
terms[33] = Ta5*tm;
terms[34] = Ta6*tm;
terms[28] = tm;
terms[29] = Ta*tm;
terms[30] = Ta2*tm;
terms[31] = Ta3*tm;
terms[32] = Ta4*tm;
terms[33] = Ta5*tm;
terms[34] = Ta6*tm;
terms[35] = va*tm;
terms[36] = Ta*va*tm;
terms[37] = Ta2*va*tm;
terms[38] = Ta3*va*tm;
terms[39] = Ta4*va*tm;
terms[35] = va*tm;
terms[36] = Ta*va*tm;
terms[37] = Ta2*va*tm;
terms[38] = Ta3*va*tm;
terms[39] = Ta4*va*tm;
terms[40] = va2*tm;
terms[41] = Ta*va2*tm;
terms[42] = Ta2*va2*tm;
terms[43] = Ta3*va2*tm;
terms[40] = va2*tm;
terms[41] = Ta*va2*tm;
terms[42] = Ta2*va2*tm;
terms[43] = Ta3*va2*tm;
terms[44] = va3*tm;
terms[45] = Ta*va3*tm;
terms[46] = Ta2*va3*tm;
terms[44] = va3*tm;
terms[45] = Ta*va3*tm;
terms[46] = Ta2*va3*tm;
terms[47] = va4*tm;
terms[48] = Ta*va4*tm;
terms[47] = va4*tm;
terms[48] = Ta*va4*tm;
terms[49] = va5*tm;
terms[49] = va5*tm;
// tm^2 à tm^6
terms[50] = tm2; terms[51] = Ta*tm2; terms[52] = Ta2*tm2; terms[53] = Ta3*tm2; terms[54] = Ta4*tm2;
terms[55] = va*tm2; terms[56] = Ta*va*tm2; terms[57] = Ta2*va*tm2; terms[58] = Ta3*va*tm2;
terms[59] = va2*tm2; terms[60] = Ta*va2*tm2; terms[61] = Ta2*va2*tm2;
terms[62] = va3*tm2;
terms[63] = tm3; terms[64] = Ta*tm3; terms[65] = Ta2*tm3;
terms[66] = va*tm3; terms[67] = Ta*va*tm3; terms[68] = va2*tm3;
terms[69] = tm4; terms[70] = Ta*tm4; terms[71] = va*tm4;
terms[72] = tm5;
terms[73] = tm6;
// tm^2 à tm^6
terms[50] = tm2; terms[51] = Ta*tm2; terms[52] = Ta2*tm2; terms[53] = Ta3*tm2; terms[54] = Ta4*tm2;
terms[55] = va*tm2; terms[56] = Ta*va*tm2; terms[57] = Ta2*va*tm2; terms[58] = Ta3*va*tm2;
terms[59] = va2*tm2; terms[60] = Ta*va2*tm2; terms[61] = Ta2*va2*tm2;
terms[62] = va3*tm2;
terms[63] = tm3; terms[64] = Ta*tm3; terms[65] = Ta2*tm3;
terms[66] = va*tm3; terms[67] = Ta*va*tm3; terms[68] = va2*tm3;
terms[69] = tm4; terms[70] = Ta*tm4; terms[71] = va*tm4;
terms[72] = tm5;
terms[73] = tm6;
// pa et combinaisons
terms[74] = pa; terms[75] = Ta*pa; terms[76] = Ta2*pa; terms[77] = Ta3*pa; terms[78] = Ta4*pa; terms[79] = Ta5*pa;
terms[80] = va*pa; terms[81] = Ta*va*pa; terms[82] = Ta2*va*pa; terms[83] = Ta3*va*pa;
terms[84] = va2*pa; terms[85] = Ta*va2*pa; terms[86] = Ta2*va2*pa;
terms[87] = va3*pa; terms[88] = Ta*va3*pa;
terms[89] = va4*pa;
// pa et combinaisons
terms[74] = pa; terms[75] = Ta*pa; terms[76] = Ta2*pa; terms[77] = Ta3*pa; terms[78] = Ta4*pa; terms[79] = Ta5*pa;
terms[80] = va*pa; terms[81] = Ta*va*pa; terms[82] = Ta2*va*pa; terms[83] = Ta3*va*pa;
terms[84] = va2*pa; terms[85] = Ta*va2*pa; terms[86] = Ta2*va2*pa;
terms[87] = va3*pa; terms[88] = Ta*va3*pa;
terms[89] = va4*pa;
terms[90] = pa2; terms[91] = Ta*pa2; terms[92] = Ta2*pa2;
terms[90] = pa2; terms[91] = Ta*pa2; terms[92] = Ta2*pa2;
// Remplir le reste avec 0.0 pour simplifier (les 210 coefficients réels peuvent être affectés directement)
for (int i = 93; i < 210; ++i) terms[i] = 0.0;
// Remplir le reste avec 0.0 pour simplifier (les 210 coefficients réels peuvent être affectés directement)
for (int i = 93; i < 210; ++i) terms[i] = 0.0;
// Calcul de l'offset
double offset = 0.0;
for (int i = 0; i < 210; ++i)
offset += m_coeffs[i] * terms[i];
// Calcul de l'offset
double offset = 0.0;
for (int i = 0; i < 210; ++i)
offset += m_coeffs[i] * terms[i];
return offset;
return offset;
}
void UtciCalculator::initCoefficients()
{
// Coefficients ESM_3 (exemple simplifié, à remplacer par la liste complète 210 valeurs)
m_coeffs = {
6.07562052E-01, -2.27712343E-02, 8.06470249E-04, -1.54271372E-04, -3.24651735E-06, 7.32602852E-08, 1.35959073E-09, -2.25836520E+00, 8.80326035E-02, 2.16844454E-03, -1.53347087E-05, -5.72983704E-07, -2.55090145E-09, -7.51269505E-01, -4.08350271E-03, -5.21670675E-05, 1.94544667E-06, 1.14099531E-08, 1.58137256E-01, -6.57263143E-05, 2.22697524E-07, -4.16117031E-08, -1.27762753E-02, 9.66891875E-06, 2.52785852E-09, 4.56306672E-04, -1.74202546E-07, -5.91491269E-06, 3.98374029E-01, 1.83945314E-04, -1.73754510E-04, -7.60781159E-07, 3.77830287E-08, 5.43079673E-10, -2.00518269E-02, 8.92859837E-04, 3.45433048E-06, -3.77925774E-07, -1.69699377E-09, 1.69992415E-04, -4.99204314E-05, 2.47417178E-07, 1.07596466E-08, 8.49242932E-05, 1.35191328E-06, -6.21531254E-09, -4.99410301E-06, -1.89489258E-08, 8.15300114E-08, 7.55043090E-04, -3.69476348E-02, 1.62325322E-03, -3.14279680E-05, 2.59835559E-06, -4.77136523E-08, 8.64203390E-03, -6.87405181E-04, -9.13863872E-06, 5.15916806E-07, -3.59217476E-05, 3.28696511E-05, -7.10542454E-07, -1.24382300E-05, -7.38584400E-09, 2.20609296E-07, -7.32469180E-04, -1.87381964E-05, 4.80925239E-06, -8.75492040E-08, 2.77862930E-05, -5.06004592E-06, 1.14325367E-07, 2.53016723E-06, -1.72857035E-08, -3.95079398E-08, -3.59413173E-07, 7.04388046E-07, -1.89309167E-08, -4.79768731E-07, 7.96079978E-09, 1.62897058E-09, 3.94367674E-08, -1.18566247E-09, 3.34678041E-10, -1.15606447E-10, -2.80626406E+00, 5.48712484E-01, -3.99428410E-03, -9.54009191E-04, 1.93090978E-05, -3.08806365E-01, 1.16952364E-02, 4.95271903E-04, -1.90710882E-05, 2.10787756E-03, -6.98445738E-04, 2.30109073E-05, 4.17856590E-04, -1.27043871E-05, -3.04620472E-06, -5.65095215E-05, -4.52166564E-07, 2.46688878E-08, 2.42674348E-10, 1.54547250E-04, 5.24110970E-06, -8.75874982E-08, -1.50743064E-09, -1.56236307E-05, -1.33895614E-07, 2.49709824E-09, 6.51711721E-07, 1.94960053E-09, -1.00361113E-08, -1.21206673E-05, -2.18203660E-07, 7.51269482E-09, 9.79063848E-11, 1.25006734E-06, -1.81584736E-09, -3.52197671E-10, -3.36514630E-08, 1.35908359E-10, 4.17032620E-10, -1.30369025E-09, 4.13908461E-10, 9.22652254E-12, -5.08220384E-09, -2.24730961E-11, 1.17139133E-10, 6.62154879E-10, 4.03863260E-13, 1.95087203E-12, -4.73602469E-12, 5.12733497E+00, -3.12788561E-01, -1.96701861E-02, 9.99690870E-04, 9.51738512E-06, -4.66426341E-07, 5.48050612E-01, -3.30552823E-03, -1.64119440E-03, -5.16670694E-06, 9.52692432E-07, -4.29223622E-02, 5.00845667E-03, 1.00601257E-06, -1.81748644E-06, -1.25813502E-03, -1.79330391E-04, 2.34994441E-06, 1.29735808E-04, 1.29064870E-06, -2.28558686E-06, 5.14507424E-02, -4.32510997E-03, 8.99281156E-05, -7.14663943E-07, -2.66016305E-04, 2.63789586E-04, -7.01199003E-06, -1.06823306E-04, 3.61341136E-06, 2.29748967E-07, 3.04788893E-04, -6.42070836E-05, 1.16257971E-06, 7.68023384E-06, -5.47446896E-07, -3.59937910E-08, -4.36497725E-06, 1.68737969E-07, 2.67489271E-08, 3.23926897E-09, -3.53874123E-02, -2.21201190E-01, 1.55126038E-02, -2.63917279E-04, 4.53433455E-02, -4.32943862E-03, 1.45389826E-04, 2.17508610E-04, -6.66724702E-05, 3.33217140E-05, -2.26921615E-03, 3.80261982E-04, -5.45314314E-09, -7.96355448E-04, 2.53458034E-05, -6.31223658E-06, 3.02122035E-04, -4.77403547E-06, 1.73825715E-06, -4.09087898E-07, 6.14155345E-01, -6.16755931E-02, 1.33374846E-03, 3.55375387E-03, -5.13027851E-04, 1.02449757E-04, -1.48526421E-03, -4.11469183E-05, -6.80434415E-06, -9.77675906E-06, 8.82773108E-02, -3.01859306E-03, 1.04452989E-03, 2.47090539E-04, 1.48348065E-03
};
// Coefficients ESM_3 (exemple simplifié, à remplacer par la liste complète 210 valeurs)
m_coeffs = {
6.07562052E-01, -2.27712343E-02, 8.06470249E-04, -1.54271372E-04, -3.24651735E-06, 7.32602852E-08, 1.35959073E-09, -2.25836520E+00, 8.80326035E-02, 2.16844454E-03, -1.53347087E-05, -5.72983704E-07, -2.55090145E-09, -7.51269505E-01, -4.08350271E-03, -5.21670675E-05, 1.94544667E-06, 1.14099531E-08, 1.58137256E-01, -6.57263143E-05, 2.22697524E-07, -4.16117031E-08, -1.27762753E-02, 9.66891875E-06, 2.52785852E-09, 4.56306672E-04, -1.74202546E-07, -5.91491269E-06, 3.98374029E-01, 1.83945314E-04, -1.73754510E-04, -7.60781159E-07, 3.77830287E-08, 5.43079673E-10, -2.00518269E-02, 8.92859837E-04, 3.45433048E-06, -3.77925774E-07, -1.69699377E-09, 1.69992415E-04, -4.99204314E-05, 2.47417178E-07, 1.07596466E-08, 8.49242932E-05, 1.35191328E-06, -6.21531254E-09, -4.99410301E-06, -1.89489258E-08, 8.15300114E-08, 7.55043090E-04, -3.69476348E-02, 1.62325322E-03, -3.14279680E-05, 2.59835559E-06, -4.77136523E-08, 8.64203390E-03, -6.87405181E-04, -9.13863872E-06, 5.15916806E-07, -3.59217476E-05, 3.28696511E-05, -7.10542454E-07, -1.24382300E-05, -7.38584400E-09, 2.20609296E-07, -7.32469180E-04, -1.87381964E-05, 4.80925239E-06, -8.75492040E-08, 2.77862930E-05, -5.06004592E-06, 1.14325367E-07, 2.53016723E-06, -1.72857035E-08, -3.95079398E-08, -3.59413173E-07, 7.04388046E-07, -1.89309167E-08, -4.79768731E-07, 7.96079978E-09, 1.62897058E-09, 3.94367674E-08, -1.18566247E-09, 3.34678041E-10, -1.15606447E-10, -2.80626406E+00, 5.48712484E-01, -3.99428410E-03, -9.54009191E-04, 1.93090978E-05, -3.08806365E-01, 1.16952364E-02, 4.95271903E-04, -1.90710882E-05, 2.10787756E-03, -6.98445738E-04, 2.30109073E-05, 4.17856590E-04, -1.27043871E-05, -3.04620472E-06, -5.65095215E-05, -4.52166564E-07, 2.46688878E-08, 2.42674348E-10, 1.54547250E-04, 5.24110970E-06, -8.75874982E-08, -1.50743064E-09, -1.56236307E-05, -1.33895614E-07, 2.49709824E-09, 6.51711721E-07, 1.94960053E-09, -1.00361113E-08, -1.21206673E-05, -2.18203660E-07, 7.51269482E-09, 9.79063848E-11, 1.25006734E-06, -1.81584736E-09, -3.52197671E-10, -3.36514630E-08, 1.35908359E-10, 4.17032620E-10, -1.30369025E-09, 4.13908461E-10, 9.22652254E-12, -5.08220384E-09, -2.24730961E-11, 1.17139133E-10, 6.62154879E-10, 4.03863260E-13, 1.95087203E-12, -4.73602469E-12, 5.12733497E+00, -3.12788561E-01, -1.96701861E-02, 9.99690870E-04, 9.51738512E-06, -4.66426341E-07, 5.48050612E-01, -3.30552823E-03, -1.64119440E-03, -5.16670694E-06, 9.52692432E-07, -4.29223622E-02, 5.00845667E-03, 1.00601257E-06, -1.81748644E-06, -1.25813502E-03, -1.79330391E-04, 2.34994441E-06, 1.29735808E-04, 1.29064870E-06, -2.28558686E-06, 5.14507424E-02, -4.32510997E-03, 8.99281156E-05, -7.14663943E-07, -2.66016305E-04, 2.63789586E-04, -7.01199003E-06, -1.06823306E-04, 3.61341136E-06, 2.29748967E-07, 3.04788893E-04, -6.42070836E-05, 1.16257971E-06, 7.68023384E-06, -5.47446896E-07, -3.59937910E-08, -4.36497725E-06, 1.68737969E-07, 2.67489271E-08, 3.23926897E-09, -3.53874123E-02, -2.21201190E-01, 1.55126038E-02, -2.63917279E-04, 4.53433455E-02, -4.32943862E-03, 1.45389826E-04, 2.17508610E-04, -6.66724702E-05, 3.33217140E-05, -2.26921615E-03, 3.80261982E-04, -5.45314314E-09, -7.96355448E-04, 2.53458034E-05, -6.31223658E-06, 3.02122035E-04, -4.77403547E-06, 1.73825715E-06, -4.09087898E-07, 6.14155345E-01, -6.16755931E-02, 1.33374846E-03, 3.55375387E-03, -5.13027851E-04, 1.02449757E-04, -1.48526421E-03, -4.11469183E-05, -6.80434415E-06, -9.77675906E-06, 8.82773108E-02, -3.01859306E-03, 1.04452989E-03, 2.47090539E-04, 1.48348065E-03
};
}