]> git.basschouten.com Git - openhab-addons.git/blob
2c5239c555f96f7bd672f52b7db4eb6ec4293c70
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.netatmo.internal.utils;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16
17 /**
18  * This class holds various unit/measurement conversion methods
19  *
20  * @author Gaël L'hopital - Initial contribution
21  * @author Rob Nielsen - updated heat index
22  */
23 @NonNullByDefault
24 public class WeatherUtils {
25
26     /**
27      * Calculate the heat index using temperature and humidity
28      * https://www.wpc.ncep.noaa.gov/html/heatindex_equation.shtml
29      *
30      * @param temperature in (°C)
31      * @param humidity relative level (%)
32      * @return heatIndex in (°C)
33      */
34     public static double heatIndex(double temperature, double humidity) {
35         double tempF = (temperature * 9.0 / 5.0) + 32.0; // calculations are done in Fahrenheit
36         double heatIndex;
37         if (tempF >= 80.0) {
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);
46             }
47         } else {
48             heatIndex = 0.5 * (tempF + 61.0 + ((tempF - 68.0) * 1.2) + (humidity * 0.094));
49         }
50
51         return (heatIndex - 32) * 5.0 / 9.0; // convert back to Celsius
52     }
53
54     public static double dewPointDep(double temperature, double dewpoint) {
55         return temperature - dewpoint;
56     }
57
58     /**
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
62      *
63      * @param temperature in (°C)
64      * @param humidity relative level (%)
65      * @return dewpoint temperature
66      */
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);
71     }
72
73     /**
74      * Compute the Humidex index given temperature and hygrometry
75      *
76      * @param temperature in (°C)
77      * @param hygro relative level (%)
78      * @return Humidex index value
79      */
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);
83     }
84
85     /**
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
88      *
89      * @param humidex index value
90      * @return scale between 0 and 4
91      */
92     public static int humidexScale(double humidex) {
93         return humidex < 30 ? 0 : humidex < 40 ? 1 : humidex < 45 ? 2 : humidex < 55 ? 3 : 4;
94     }
95 }