1
|
|
|
"""Data Class for IMS Meteorological Readings.""" |
2
|
|
|
|
3
|
|
|
from __future__ import annotations |
4
|
|
|
|
5
|
|
|
import textwrap |
6
|
|
|
import time |
7
|
|
|
from dataclasses import dataclass, field |
8
|
|
|
from datetime import datetime |
9
|
|
|
from datetime import time as dttime |
10
|
|
|
|
11
|
|
|
from .const import ( |
12
|
|
|
API_BP, |
13
|
|
|
API_CHANNELS, |
14
|
|
|
API_DATA, |
15
|
|
|
API_DATETIME, |
16
|
|
|
API_DIFF, |
17
|
|
|
API_GRAD, |
18
|
|
|
API_NAME, |
19
|
|
|
API_NIP, |
20
|
|
|
API_RAIN, |
21
|
|
|
API_RH, |
22
|
|
|
API_STATION_ID, |
23
|
|
|
API_STATUS, |
24
|
|
|
API_STD_WD, |
25
|
|
|
API_TD, |
26
|
|
|
API_TD_MAX, |
27
|
|
|
API_TD_MIN, |
28
|
|
|
API_TG, |
29
|
|
|
API_TIME, |
30
|
|
|
API_TW, |
31
|
|
|
API_VALID, |
32
|
|
|
API_VALUE, |
33
|
|
|
API_WD, |
34
|
|
|
API_WD_MAX, |
35
|
|
|
API_WS, |
36
|
|
|
API_WS_1MM, |
37
|
|
|
API_WS_10MM, |
38
|
|
|
API_WS_MAX, |
39
|
|
|
VARIABLES, |
40
|
|
|
) |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
@dataclass |
44
|
|
|
class MeteorologicalData: |
45
|
|
|
"""Meteorological Data.""" |
46
|
|
|
|
47
|
|
|
station_id: int |
48
|
|
|
"""Station ID""" |
49
|
|
|
datetime: datetime |
|
|
|
|
50
|
|
|
"""Date and time of the data""" |
51
|
|
|
rain: float |
52
|
|
|
"""Rainfall in mm""" |
53
|
|
|
ws: float |
54
|
|
|
"""Wind speed in m/s""" |
55
|
|
|
ws_max: float |
56
|
|
|
"""Gust wind speed in m/s""" |
57
|
|
|
wd: float |
58
|
|
|
"""Wind direction in deg""" |
59
|
|
|
wd_max: float |
60
|
|
|
"""Gust wind direction in deg""" |
61
|
|
|
std_wd: float |
62
|
|
|
"""Standard deviation wind direction in deg""" |
63
|
|
|
td: float |
64
|
|
|
"""Temperature in °C""" |
65
|
|
|
td_max: float |
66
|
|
|
"""Maximum Temperature in °C""" |
67
|
|
|
td_min: float |
68
|
|
|
"""Minimum Temperature in °C""" |
69
|
|
|
tg: float |
70
|
|
|
"""Ground Temperature in °C""" |
71
|
|
|
tw: float |
72
|
|
|
"""TW Temperature (?) in °C""" |
73
|
|
|
rh: float |
74
|
|
|
"""Relative humidity in %""" |
75
|
|
|
ws_1mm: float |
76
|
|
|
"""Maximum 1 minute wind speed in m/s""" |
77
|
|
|
ws_10mm: float |
78
|
|
|
"""Maximum 10 minute wind speed in m/s""" |
79
|
|
|
time: dttime |
80
|
|
|
"""Time""" |
81
|
|
|
bp: float |
82
|
|
|
"""Maximum barometric pressure in mb""" |
83
|
|
|
diff_r: float |
84
|
|
|
"""Distributed radiation in w/m^2""" |
85
|
|
|
grad: float |
86
|
|
|
"""Global radiation in w/m^2""" |
87
|
|
|
nip: float |
88
|
|
|
"""Direct radiation in w/m^2""" |
89
|
|
|
|
90
|
|
|
def _pretty_print(self) -> str: |
91
|
|
|
return textwrap.dedent( |
92
|
|
|
"""Station: {}, Date: {}, Readings: [(TD: {}{}), (TDmax: {}{}), (TDmin: {}{}), (TG: {}{}), (RH: {}{}), (Rain: {}{}), (WS: {}{}), (WSmax: {}{}), (WD: {}{}), (WDmax: {}{}), (STDwd: {}{}), (WS1mm: {}{}), (WS10mm: {}{}), (Time: {}{})] |
93
|
|
|
""" |
94
|
|
|
).format( |
95
|
|
|
self.station_id, |
96
|
|
|
self.datetime, |
97
|
|
|
self.td, |
98
|
|
|
VARIABLES[API_TD].unit, |
99
|
|
|
self.td_max, |
100
|
|
|
VARIABLES[API_TD_MAX].unit, |
101
|
|
|
self.td_min, |
102
|
|
|
VARIABLES[API_TD_MIN].unit, |
103
|
|
|
self.tg, |
104
|
|
|
VARIABLES[API_TG].unit, |
105
|
|
|
self.rh, |
106
|
|
|
VARIABLES[API_RH].unit, |
107
|
|
|
self.rain, |
108
|
|
|
VARIABLES[API_RAIN].unit, |
109
|
|
|
self.ws, |
110
|
|
|
VARIABLES[API_WS].unit, |
111
|
|
|
self.ws_max, |
112
|
|
|
VARIABLES[API_WS_MAX].unit, |
113
|
|
|
self.wd, |
114
|
|
|
VARIABLES[API_WD].unit, |
115
|
|
|
self.wd_max, |
116
|
|
|
VARIABLES[API_WD_MAX].unit, |
117
|
|
|
self.std_wd, |
118
|
|
|
VARIABLES[API_STD_WD].unit, |
119
|
|
|
self.ws_1mm, |
120
|
|
|
VARIABLES[API_WS_1MM].unit, |
121
|
|
|
self.ws_10mm, |
122
|
|
|
VARIABLES[API_WS_10MM].unit, |
123
|
|
|
self.time, |
124
|
|
|
VARIABLES[API_TIME].unit, |
125
|
|
|
) |
126
|
|
|
|
127
|
|
|
def __str__(self) -> str: |
128
|
|
|
return self._pretty_print() |
129
|
|
|
|
130
|
|
|
def __repr__(self) -> str: |
131
|
|
|
return self._pretty_print().replace("\n", " ") |
132
|
|
|
|
133
|
|
|
|
134
|
|
|
@dataclass |
135
|
|
|
class StationMeteorologicalReadings: |
136
|
|
|
"""Station Meteorological Readings.""" |
137
|
|
|
|
138
|
|
|
station_id: int |
139
|
|
|
""" Station Id""" |
140
|
|
|
data: list[MeteorologicalData] = field(default_factory=list) |
141
|
|
|
""" List of Meteorological Data """ |
142
|
|
|
|
143
|
|
|
def __repr__(self) -> str: |
144
|
|
|
return textwrap.dedent("""Station ({}), Data: {}""").format( |
145
|
|
|
self.station_id, self.data |
146
|
|
|
) |
147
|
|
|
|
148
|
|
|
|
149
|
|
|
def meteo_data_from_json(station_id: int, data: dict) -> MeteorologicalData: |
150
|
|
|
"""Create a MeteorologicalData object from a JSON object.""" |
151
|
|
|
dt = datetime.fromisoformat(data[API_DATETIME]) |
152
|
|
|
channel_value_dict = {} |
153
|
|
|
for channel_value in data[API_CHANNELS]: |
154
|
|
|
if channel_value[API_VALID] is True and channel_value[API_STATUS] == 1: |
155
|
|
|
channel_value_dict[channel_value[API_NAME]] = float( |
156
|
|
|
channel_value[API_VALUE] |
157
|
|
|
) |
158
|
|
|
|
159
|
|
|
rain = channel_value_dict.get(API_RAIN) |
160
|
|
|
ws_max = channel_value_dict.get(API_WS_MAX) |
161
|
|
|
wd_max = channel_value_dict.get(API_WD_MAX) |
162
|
|
|
ws = channel_value_dict.get(API_WS) |
163
|
|
|
wd = channel_value_dict.get(API_WD) |
164
|
|
|
std_wd = channel_value_dict.get(API_STD_WD) |
165
|
|
|
td = channel_value_dict.get(API_TD) |
166
|
|
|
rh = channel_value_dict.get(API_RH) |
167
|
|
|
td_max = channel_value_dict.get(API_TD_MAX) |
168
|
|
|
td_min = channel_value_dict.get(API_TD_MIN) |
169
|
|
|
ws_1mm = channel_value_dict.get(API_WS_1MM) |
170
|
|
|
ws_10mm = channel_value_dict.get(API_WS_10MM) |
171
|
|
|
tg = channel_value_dict.get(API_TG) |
172
|
|
|
tw = channel_value_dict.get(API_TW) |
173
|
|
|
time_val = channel_value_dict.get(API_TIME) |
174
|
|
|
if time_val: |
175
|
|
|
time_val = time.strptime(str(int(time_val)), "%H%M") |
176
|
|
|
bp = channel_value_dict.get(API_BP) |
177
|
|
|
diff_r = channel_value_dict.get(API_DIFF) |
178
|
|
|
grad = channel_value_dict.get(API_GRAD) |
179
|
|
|
nip = channel_value_dict.get(API_NIP) |
180
|
|
|
|
181
|
|
|
return MeteorologicalData( |
182
|
|
|
station_id=station_id, |
183
|
|
|
datetime=dt, |
184
|
|
|
rain=rain, |
185
|
|
|
ws=ws, |
186
|
|
|
ws_max=ws_max, |
187
|
|
|
wd=wd, |
188
|
|
|
wd_max=wd_max, |
189
|
|
|
std_wd=std_wd, |
190
|
|
|
td=td, |
191
|
|
|
td_max=td_max, |
192
|
|
|
td_min=td_min, |
193
|
|
|
tg=tg, |
194
|
|
|
tw=tw, |
195
|
|
|
rh=rh, |
196
|
|
|
ws_1mm=ws_1mm, |
197
|
|
|
ws_10mm=ws_10mm, |
198
|
|
|
time=time_val, |
199
|
|
|
bp=bp, |
200
|
|
|
diff_r=diff_r, |
201
|
|
|
grad=grad, |
202
|
|
|
nip=nip |
203
|
|
|
) |
204
|
|
|
|
205
|
|
|
|
206
|
|
|
def station_meteo_data_from_json(json: dict) -> StationMeteorologicalReadings: |
207
|
|
|
station_id = int(json[API_STATION_ID]) |
208
|
|
|
data = json.get(API_DATA) |
209
|
|
|
if not data: |
210
|
|
|
return None |
211
|
|
|
meteo_data = [meteo_data_from_json(station_id, single_meteo_data) for single_meteo_data in data] |
212
|
|
|
return StationMeteorologicalReadings(station_id, meteo_data) |
213
|
|
|
|