comparison weather_server/types.py @ 41:4af79d69b12e v0.2.0

Use units better suited for science.
author Paul Fisher <paul@pfish.zone>
date Tue, 01 Apr 2025 16:04:52 -0400
parents b77c8e7d2742
children 10bde12a9163
comparison
equal deleted inserted replaced
40:5cc8ce54ad84 41:4af79d69b12e
9 T = t.TypeVar('T') 9 T = t.TypeVar('T')
10 10
11 11
12 def c_to_f(c: float) -> float: 12 def c_to_f(c: float) -> float:
13 return c * 9 / 5 + 32 13 return c * 9 / 5 + 32
14
15
16 def c_to_k(c: float) -> float:
17 return c + 273.15
14 18
15 19
16 # Values from Sontag1990 via Wikipedia: 20 # Values from Sontag1990 via Wikipedia:
17 # https://en.wikipedia.org/wiki/Dew_point 21 # https://en.wikipedia.org/wiki/Dew_point
18 _MAGNUS_B = 17.62 22 _MAGNUS_B = 17.62
39 ingest_time = attr.ib(type=datetime.datetime) 43 ingest_time = attr.ib(type=datetime.datetime)
40 44
41 @property 45 @property
42 def temp_f(self) -> float: 46 def temp_f(self) -> float:
43 return c_to_f(self.temp_c) 47 return c_to_f(self.temp_c)
48
49 @property
50 def temp_k(self) -> float:
51 return c_to_k(self.temp_k)
44 52
45 @property 53 @property
46 def dew_point_c(self) -> float: 54 def dew_point_c(self) -> float:
47 gamma = self._gamma 55 gamma = self._gamma
48 return _MAGNUS_C * gamma / (_MAGNUS_B - gamma) 56 return _MAGNUS_C * gamma / (_MAGNUS_B - gamma)
49 57
50 @property 58 @property
51 def dew_point_f(self) -> float: 59 def dew_point_f(self) -> float:
52 return c_to_f(self.dew_point_c) 60 return c_to_f(self.dew_point_c)
61
62 @property
63 def dew_point_k(self) -> float:
64 return c_to_k(self.dew_point_c)
53 65
54 @classmethod 66 @classmethod
55 def from_dict(cls: t.Type[T], d: t.Dict[str, t.Any]) -> T: 67 def from_dict(cls: t.Type[T], d: t.Dict[str, t.Any]) -> T:
56 return cls(**{f.name: d[f.name] for f in attr.fields(cls)}) 68 return cls(**{f.name: d[f.name] for f in attr.fields(cls)})
57 69