2 * Copyright (c) 2010-2022 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.netatmo.internal.utils;
15 import org.eclipse.jdt.annotation.NonNullByDefault;
18 * This class holds various unit/measurement conversion methods
20 * @author Gaël L'hopital - Initial contribution
21 * @author Rob Nielsen - updated heat index
24 public class WeatherUtils {
27 * Calculate the heat index using temperature and humidity
28 * https://www.wpc.ncep.noaa.gov/html/heatindex_equation.shtml
30 * @param temperature in (°C)
31 * @param humidity relative level (%)
32 * @return heatIndex in (°C)
34 public static double heatIndex(double temperature, double humidity) {
35 double tempF = (temperature * 9.0 / 5.0) + 32.0; // calculations are done in Fahrenheit
38 heatIndex = -42.379 + (2.04901523 * tempF) + (10.14333127 * humidity) - (0.22475541 * tempF * humidity)
39 - (0.00683783 * tempF * tempF) - (0.05481717 * humidity * humidity)
40 + (0.00122874 * tempF * tempF * humidity) + (0.00085282 * tempF * humidity * humidity)
41 - (0.00000199 * tempF * tempF * humidity * humidity);
42 if (humidity < 13.0 && tempF <= 112.0) {
43 heatIndex -= ((13.0 - humidity) / 4.0) * Math.sqrt((17.0 - Math.abs(tempF - 95.0)) / 17.0);
44 } else if (humidity > 85.0 && tempF <= 87.0) {
45 heatIndex += ((humidity - 85.0) / 10.0) * ((87.0 - tempF) / 5.0);
48 heatIndex = 0.5 * (tempF + 61.0 + ((tempF - 68.0) * 1.2) + (humidity * 0.094));
51 return (heatIndex - 32) * 5.0 / 9.0; // convert back to Celsius
54 public static double dewPointDep(double temperature, double dewpoint) {
55 return temperature - dewpoint;
59 * Compute the Dewpoint temperature given temperature and hygrometry
60 * valid up to 60 degrees, from
61 * http://en.wikipedia.org/wiki/Dew_point#Calculating_the_dew_point
63 * @param temperature in (°C)
64 * @param humidity relative level (%)
65 * @return dewpoint temperature
67 public static double dewPoint(double temperature, double humidity) {
68 double a = 17.271, b = 237.2;
69 double gamma = ((a * temperature) / (b + temperature)) + Math.log(humidity / 100.0);
70 return b * gamma / (a - gamma);
74 * Compute the Humidex index given temperature and hygrometry
76 * @param temperature in (°C)
77 * @param hygro relative level (%)
78 * @return Humidex index value
80 public static double humidex(double temperature, double hygro) {
81 double result = 6.112 * Math.pow(10, 7.5 * temperature / (237.7 + temperature)) * hygro / 100;
82 return temperature + 0.555555556 * (result - 10);
86 * Compute the associated scale appreciation of a given humidex index
87 * https://www.researchgate.net/figure/The-scale-of-Humidex-and-the-degree-of-comfort_tbl1_335293174
89 * @param Humidex index value
90 * @return scale between 0 and 4
92 public static int humidexScale(double humidex) {
93 return humidex < 30 ? 0 : humidex < 40 ? 1 : humidex < 45 ? 2 : humidex < 55 ? 3 : 4;