summaryrefslogtreecommitdiffstats
path: root/sensor_firmware/airhum.py
blob: a4fccc19ff5a25ee7653caea0b1b5993969bc55b (plain)
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
#
# Air humidity
# Copyright (c) 2020 Michael Büsch <m@bues.ch>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#

import micropython
from micropython import const
from util import lim, intp_log

# Table of humidity saturation at a given temperature.
# Data from: https://de.wikipedia.org/wiki/S%C3%A4ttigung_(Physik)
temp2maxhum = {
#   deg C       : mg/m3
    const(-40)  : const(177),
    const(-39)  : const(195),
    const(-38)  : const(215),
    const(-37)  : const(237),
    const(-36)  : const(261),
    const(-35)  : const(287),
    const(-34)  : const(315),
    const(-33)  : const(345),
    const(-32)  : const(378),
    const(-31)  : const(415),
    const(-30)  : const(455),
    const(-29)  : const(497),
    const(-28)  : const(543),
    const(-27)  : const(592),
    const(-26)  : const(647),
    const(-25)  : const(706),
    const(-24)  : const(768),
    const(-23)  : const(836),
    const(-22)  : const(909),
    const(-21)  : const(989),
    const(-20)  : const(1075),
    const(-19)  : const(1165),
    const(-18)  : const(1263),
    const(-17)  : const(1368),
    const(-16)  : const(1483),
    const(-15)  : const(1607),
    const(-14)  : const(1735),
    const(-13)  : const(1874),
    const(-12)  : const(2023),
    const(-11)  : const(2185),
    const(-10)  : const(2360),
    const(-9)   : const(2540),
    const(-8)   : const(2734),
    const(-7)   : const(2942),
    const(-6)   : const(3167),
    const(-5)   : const(3409),
    const(-4)   : const(3662),
    const(-3)   : const(3932),
    const(-2)   : const(4219),
    const(-1)   : const(4524),
    const(0)    : const(4848),
    const(1)    : const(5193),
    const(2)    : const(5559),
    const(3)    : const(5948),
    const(4)    : const(6360),
    const(5)    : const(6796),
    const(6)    : const(7259),
    const(7)    : const(7749),
    const(8)    : const(8268),
    const(9)    : const(8816),
    const(10)   : const(9397),
    const(11)   : const(10010),
    const(12)   : const(10657),
    const(13)   : const(11341),
    const(14)   : const(12062),
    const(15)   : const(12823),
    const(16)   : const(13625),
    const(17)   : const(14471),
    const(18)   : const(15361),
    const(19)   : const(16298),
    const(20)   : const(17285),
    const(21)   : const(18322),
    const(22)   : const(19413),
    const(23)   : const(20560),
    const(24)   : const(21764),
    const(25)   : const(23028),
    const(26)   : const(24355),
    const(27)   : const(25748),
    const(28)   : const(27208),
    const(29)   : const(28739),
    const(30)   : const(30342),
    const(31)   : const(31996),
    const(32)   : const(33740),
    const(33)   : const(35579),
    const(34)   : const(37518),
    const(35)   : const(39562),
    const(36)   : const(41635),
    const(37)   : const(43817),
    const(38)   : const(46113),
    const(39)   : const(48530),
    const(40)   : const(51073),
    const(41)   : const(53647),
    const(42)   : const(56352),
    const(43)   : const(59193),
    const(44)   : const(62177),
    const(45)   : const(65311),
    const(46)   : const(68481),
    const(47)   : const(71804),
    const(48)   : const(75289),
    const(49)   : const(78942),
    const(50)   : const(82773),
    const(51)   : const(86642),
    const(52)   : const(90691),
    const(53)   : const(94930),
    const(54)   : const(99367),
    const(55)   : const(104011),
    const(56)   : const(108696),
    const(57)   : const(113592),
    const(58)   : const(118708),
    const(59)   : const(124054),
    const(60)   : const(129642),
}

mintemp = min(temp2maxhum.keys())
maxtemp = max(temp2maxhum.keys())

@micropython.native
def relhum2abshum(relhum, temp):
    """Calculate the max humidity at the given temperature.
    """
    # Interpolate between full-degree table points.
    t0 = lim(round(temp), mintemp, maxtemp)
    t1 = lim(t0 + 1, mintemp, maxtemp)
    maxhum = intp_log(temp,
                      t0,
                      temp2maxhum[t0] * 1e-3,
                      t1,
                      temp2maxhum[t1] * 1e-3)

    # Calculate the absolute humidity.
    abshum = maxhum * relhum
    return abshum

# vim: ts=4 sw=4 expandtab
bues.ch cgit interface