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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#![doc = include_str!("../README.md")]
#![deny(unsafe_code, missing_docs)]
#![no_std]

#[allow(unused_imports)]
#[cfg(feature = "no-std")]
use micromath::F32Ext;
#[cfg(not(feature = "no-std"))]
extern crate std;

/// The units for the measures.
pub mod unit;

/// The temperature (either in °C, or in °F).
#[derive(Clone, Copy, Debug, Default)]
pub struct Temperature<U: unit::TemperatureUnit> {
    pub(crate) value: U,
}

impl<U: unit::TemperatureUnit> Temperature<U> {
    /// Get the temperature value in degrees Celcius (°C).
    pub fn celcius(&self) -> f32 {
        self.value.celcius()
    }

    /// Get the temperature value in degrees Farenheit (°F).
    pub fn farenheit(&self) -> f32 {
        self.value.farenheit()
    }
}

impl Temperature<unit::Celcius> {
    /// Create a Celcius temperature.
    pub fn new(value: f32) -> Temperature<unit::Celcius> {
        Temperature {
            value: unit::Celcius { value },
        }
    }
}

impl From<Temperature<unit::Farenheit>> for Temperature<unit::Celcius> {
    fn from(value: Temperature<unit::Farenheit>) -> Self {
        Self::new(value.celcius())
    }
}

impl Temperature<unit::Farenheit> {
    /// Create a Farenheit temperature.
    pub fn new(value: f32) -> Temperature<unit::Farenheit> {
        Temperature {
            value: unit::Farenheit { value },
        }
    }
}

impl From<Temperature<unit::Celcius>> for Temperature<unit::Farenheit> {
    fn from(value: Temperature<unit::Celcius>) -> Self {
        Self::new(value.farenheit())
    }
}

/// The relative humidity type (in %).
pub type RelativeHumidity = f32;

/// The absolute humidity type (in g/m³).
pub type AbsoluteHumidity = f32;

/// The barometric pressure type (in hPa).
pub type BarometricPressure = f32;

/// The altitude type (in m).
pub type Altitude = f32;

/// The combination of the temperature and the relative humidity.
#[derive(Clone, Copy, Debug, Default)]
pub struct TemperatureAndRelativeHumidity<U: unit::TemperatureUnit> {
    /// The relative humidity (in %).
    pub relative_humidity: RelativeHumidity,
    /// The temperature (either in °C or °F).
    pub temperature: Temperature<U>,
}

impl<U: unit::TemperatureUnit> TemperatureAndRelativeHumidity<U> {
    /// Computes the absolute humidity value (in g/m³).
    pub fn absolute_humidity(&self) -> AbsoluteHumidity {
        let temperature = self.temperature.celcius();
        (6.112
            * ((17.67 * temperature) / (temperature + 243.5)).exp()
            * self.relative_humidity
            * 2.1674)
            / (273.15 + temperature)
    }
}

impl TemperatureAndRelativeHumidity<unit::Celcius> {
    /// Create a combination of Celcius temperature and relative humidity.
    pub fn new(
        temperature: f32,
        relative_humidity: f32,
    ) -> TemperatureAndRelativeHumidity<unit::Celcius> {
        TemperatureAndRelativeHumidity {
            relative_humidity,
            temperature: Temperature::<unit::Celcius>::new(temperature),
        }
    }
}

impl From<TemperatureAndRelativeHumidity<unit::Farenheit>>
    for TemperatureAndRelativeHumidity<unit::Celcius>
{
    fn from(value: TemperatureAndRelativeHumidity<unit::Farenheit>) -> Self {
        Self::new(value.temperature.celcius(), value.relative_humidity)
    }
}

impl TemperatureAndRelativeHumidity<unit::Farenheit> {
    /// Create a combination of Farenheit temperature and relative humidity.
    pub fn new(
        temperature: f32,
        relative_humidity: f32,
    ) -> TemperatureAndRelativeHumidity<unit::Farenheit> {
        TemperatureAndRelativeHumidity {
            relative_humidity,
            temperature: Temperature::<unit::Farenheit>::new(temperature),
        }
    }
}

impl From<TemperatureAndRelativeHumidity<unit::Celcius>>
    for TemperatureAndRelativeHumidity<unit::Farenheit>
{
    fn from(value: TemperatureAndRelativeHumidity<unit::Celcius>) -> Self {
        Self::new(value.temperature.farenheit(), value.relative_humidity)
    }
}

/// The combination of the temperature and the barometric pressure.
#[derive(Clone, Copy, Debug, Default)]
pub struct TemperatureAndBarometricPressure<U: unit::TemperatureUnit> {
    /// The barometric pressure (in hPa).
    pub barometric_pressure: BarometricPressure,
    /// The temperature (either in °C or °F).
    pub temperature: Temperature<U>,
}

impl<U: unit::TemperatureUnit> TemperatureAndBarometricPressure<U> {
    /// Compute the altitude (in m).
    pub fn altitude(&self) -> Altitude {
        let temperature = self.temperature.celcius();
        ((1_013.25 / self.barometric_pressure).powf(1.0 / 5.257) - 1.0) * (temperature + 273.15)
            / 0.0065
    }
}

impl TemperatureAndBarometricPressure<unit::Celcius> {
    /// Create a combination of Celcius temperature and barometric pressure.
    pub fn new(
        temperature: f32,
        barometric_pressure: f32,
    ) -> TemperatureAndBarometricPressure<unit::Celcius> {
        TemperatureAndBarometricPressure {
            barometric_pressure,
            temperature: Temperature::<unit::Celcius>::new(temperature),
        }
    }
}

impl From<TemperatureAndBarometricPressure<unit::Farenheit>>
    for TemperatureAndBarometricPressure<unit::Celcius>
{
    fn from(value: TemperatureAndBarometricPressure<unit::Farenheit>) -> Self {
        Self::new(value.temperature.celcius(), value.barometric_pressure)
    }
}

impl TemperatureAndBarometricPressure<unit::Farenheit> {
    /// Create a combination of Farenheit temperature and barometric pressure.
    pub fn new(
        temperature: f32,
        barometric_pressure: f32,
    ) -> TemperatureAndBarometricPressure<unit::Farenheit> {
        TemperatureAndBarometricPressure {
            barometric_pressure,
            temperature: Temperature::<unit::Farenheit>::new(temperature),
        }
    }
}

impl From<TemperatureAndBarometricPressure<unit::Celcius>>
    for TemperatureAndBarometricPressure<unit::Farenheit>
{
    fn from(value: TemperatureAndBarometricPressure<unit::Celcius>) -> Self {
        Self::new(value.temperature.farenheit(), value.barometric_pressure)
    }
}

#[cfg(test)]
mod tests {
    use crate::unit::*;
    use crate::*;

    #[test]
    fn compute_absolute_humidity() {
        assert!(
            (TemperatureAndRelativeHumidity::<Celcius>::new(21.18, 45.59).absolute_humidity()
                - 8.43)
                .abs()
                < 0.01
        );
        assert!(
            (TemperatureAndRelativeHumidity::<Farenheit>::new(70.12, 45.59).absolute_humidity()
                - 8.43)
                .abs()
                < 0.01
        );
        assert!(
            (TemperatureAndRelativeHumidity::<Celcius>::new(2.93, 34.71).absolute_humidity()
                - 2.06)
                .abs()
                < 0.01
        );
        assert!(
            (TemperatureAndRelativeHumidity::<Farenheit>::new(107.7, 74.91).absolute_humidity()
                - 42.49)
                .abs()
                < 0.01
        );
    }

    #[test]
    fn compute_altitude() {
        assert!(
            (TemperatureAndBarometricPressure::<Celcius>::new(20.55, 991.32).altitude() - 188.46)
                .abs()
                < 0.01
        );
        assert!(
            (TemperatureAndBarometricPressure::<Celcius>::new(17.93, 1013.25).altitude() - 0.0)
                .abs()
                < 0.01
        );
        assert!(
            (TemperatureAndBarometricPressure::<Celcius>::new(37.5, 1013.25).altitude() - 0.0)
                .abs()
                < 0.01
        );
        assert!(
            (TemperatureAndBarometricPressure::<Celcius>::new(19.37, 962.81).altitude() - 439.25)
                .abs()
                < 0.01
        );
    }

    #[test]
    fn convert_temperatures() {
        assert!(
            (Temperature::<Farenheit>::from(Temperature::<Celcius>::new(0.0))
                .value
                .value
                - 32.0)
                .abs()
                < 0.01
        );
        assert!(
            (Temperature::<Farenheit>::from(Temperature::<Celcius>::new(15.73))
                .value
                .value
                - 60.31)
                .abs()
                < 0.01
        );
        assert!(
            (Temperature::<Farenheit>::from(Temperature::<Celcius>::new(-7.49))
                .value
                .value
                - 18.52)
                .abs()
                < 0.01
        );
        assert!(
            (Temperature::<Farenheit>::from(Temperature::<Celcius>::new(37.5))
                .value
                .value
                - 99.5)
                .abs()
                < 0.01
        );

        assert!(
            (Temperature::<Celcius>::from(Temperature::<Farenheit>::new(32.0))
                .value
                .value
                - 0.0)
                .abs()
                < 0.01
        );
        assert!(
            (Temperature::<Celcius>::from(Temperature::<Farenheit>::new(60.31))
                .value
                .value
                - 15.73)
                .abs()
                < 0.01
        );
        assert!(
            (Temperature::<Celcius>::from(Temperature::<Farenheit>::new(18.52))
                .value
                .value
                - -7.49)
                .abs()
                < 0.01
        );
        assert!(
            (Temperature::<Celcius>::from(Temperature::<Farenheit>::new(99.5))
                .value
                .value
                - 37.5)
                .abs()
                < 0.01
        );
    }
}