-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathMeasurementValueCalculator.java
112 lines (103 loc) · 4.71 KB
/
MeasurementValueCalculator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package fi.tkgwf.ruuvi.utils;
import fi.tkgwf.ruuvi.bean.EnhancedRuuviMeasurement;
public class MeasurementValueCalculator {
/**
* Calculates values that can be calculated based on other values, such as
* total acceleration and absolute humidity
*
* @param measurement the measurement
* @return The supplied Measurement
*/
public static EnhancedRuuviMeasurement calculateAllValues(EnhancedRuuviMeasurement measurement) {
measurement.setAbsoluteHumidity(absoluteHumidity(measurement.getTemperature(), measurement.getHumidity()));
measurement.setDewPoint(dewPoint(measurement.getTemperature(), measurement.getHumidity()));
measurement.setEquilibriumVaporPressure(equilibriumVaporPressure(measurement.getTemperature()));
measurement.setAirDensity(airDensity(measurement.getTemperature(), measurement.getHumidity(), measurement.getPressure()));
measurement.setAccelerationTotal(totalAcceleration(measurement.getAccelerationX(), measurement.getAccelerationY(), measurement.getAccelerationZ()));
measurement.setAccelerationAngleFromX(angleBetweenVectorComponentAndAxis(measurement.getAccelerationX(), measurement.getAccelerationTotal()));
measurement.setAccelerationAngleFromY(angleBetweenVectorComponentAndAxis(measurement.getAccelerationY(), measurement.getAccelerationTotal()));
measurement.setAccelerationAngleFromZ(angleBetweenVectorComponentAndAxis(measurement.getAccelerationZ(), measurement.getAccelerationTotal()));
return measurement;
}
/**
* Calculates the total acceleration strength
*
* @param accelerationX
* @param accelerationY
* @param accelerationZ
* @return The total acceleration strength
*/
public static Double totalAcceleration(Double accelerationX, Double accelerationY, Double accelerationZ) {
if (accelerationX == null || accelerationY == null || accelerationZ == null) {
return null;
}
return Math.sqrt(accelerationX * accelerationX + accelerationY * accelerationY + accelerationZ * accelerationZ);
}
/**
* Calculates the angle between a vector component and the corresponding
* axis
*
* @param vectorComponent Vector component
* @param vectorLength Vector length
* @return Angle between the components axis and the vector, in degrees
*/
public static Double angleBetweenVectorComponentAndAxis(Double vectorComponent, Double vectorLength) {
if (vectorComponent == null || vectorLength == null || vectorLength == 0) {
return null;
}
return Math.toDegrees(Math.acos(vectorComponent / vectorLength));
}
/**
* Calculates the absolute humidity
*
* @param temperature Temperature in Celsius
* @param relativeHumidity Relative humidity % (range 0-100)
* @return The absolute humidity in g/m^3
*/
public static Double absoluteHumidity(Double temperature, Double relativeHumidity) {
if (temperature == null || relativeHumidity == null) {
return null;
}
return equilibriumVaporPressure(temperature) * relativeHumidity * 0.021674 / (273.15 + temperature);
}
/**
* Calculates the dew point
*
* @param temperature Temperature in Celsius
* @param relativeHumidity Relative humidity % (range 0-100)
* @return The dew point in Celsius
*/
public static Double dewPoint(Double temperature, Double relativeHumidity) {
if (temperature == null || relativeHumidity == null || relativeHumidity == 0) {
return null;
}
double v = Math.log(relativeHumidity / 100 * equilibriumVaporPressure(temperature) / 611.2);
return -243.5 * v / (v - 17.67);
}
/**
* Calculates the equilibrium vapor pressure of water
*
* @param temperature Temperature in Celsius
* @return The vapor pressure in Pa
*/
public static Double equilibriumVaporPressure(Double temperature) {
if (temperature == null) {
return null;
}
return 611.2 * Math.exp(17.67 * temperature / (243.5 + temperature));
}
/**
* Calculates the air density
*
* @param temperature Temperature in Celsius
* @param relativeHumidity Relative humidity % (range 0-100)
* @param pressure Pressure in pa
* @return The air density in kg/m^3
*/
public static Double airDensity(Double temperature, Double relativeHumidity, Double pressure) {
if (temperature == null || relativeHumidity == null || pressure == null) {
return null;
}
return 1.2929 * 273.15 / (temperature + 273.15) * (pressure - 0.3783 * relativeHumidity / 100 * equilibriumVaporPressure(temperature)) / 101300;
}
}