-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_nco.py
178 lines (158 loc) · 5.44 KB
/
test_nco.py
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
from pydantic import BaseModel, ValidationError, validator
from typing import Any
import json
metadata = {
"attributes": {
# "name": {"type"mock1",
"make": {"type": "char", "data": "MockCo"},
"model": {"type": "char", "data": "Mock1"},
"description": {
"type": "char",
"data": "Simulates a meterological type of sensor for the purposes of testing. Data records are emitted once per second.",
},
"tags": {"type": "char", "data": "testing, mock, meteorology, sensor"},
"format_version": {"type": "char", "data": "1.0.0"},
},
"variables": {
"time": {
"type": "str",
"shape": ["time"],
"attributes": {"long_name": {"type": "char", "data": "Time"}},
},
"temperature": {
"type": "float",
"shape": ["time"],
"attributes": {
"long_name": {"type": "char", "data": "Temperature"},
"units": {"type": "char", "data": "degree_C"},
},
},
"rh": {
"type": "float",
"shape": ["time"],
"attributes": {
"long_name": {"type": "char", "data": "RH"},
"units": {"type": "char", "data": "percent"},
},
},
"pressure": {
"type": "float",
"shape": ["time"],
"attributes": {
"long_name": {"type": "char", "data": "Pressure"},
"units": {"type": "char", "data": "hPa"},
},
},
"wind_speed": {
"type": "float",
"shape": ["time"],
"attributes": {
"long_name": {"type": "char", "data": "Wind Speed"},
"units": {"type": "char", "data": "m s-1"},
},
},
"wind_direction": {
"type": "float",
"shape": ["time"],
"attributes": {
"long_name": {"type": "char", "data": "Wind Direction"},
"units": {"type": "char", "data": "degree"},
"valid_min": {"type": "float", "data": 0.0},
"valid_max": {"type": "float", "data": 360.0},
},
},
"flow_rate": {
"type": "float",
"shape": ["time"],
"attributes": {
"long_name": {"type": "char", "data": "Flow Rate"},
"units": {"type": "char", "data": "l min-1"},
"valid_min": {"type": "float", "data": 0.0},
"valid_max": {"type": "float", "data": 5.0},
},
},
},
"settings": {
"flow_rate": {
"type": "float",
"shape": ["time"],
"attributes": {
"long_name": {"type": "char", "data": "Flow Rate"},
"units": {"type": "char", "data": "l min-1"},
"valid_min": {"type": "float", "data": 0.0},
"valid_max": {"type": "float", "data": 5.0},
},
},
},
}
class SensorAttribute(BaseModel):
# name: str
type: str | None = "str"
data: Any
@validator("data")
def data_check(cls, v, values):
if "type" in values:
data_type = values["type"]
if data_type == "char":
data_type = "str"
if "type" in values and not isinstance(v, eval(data_type)):
raise ValueError("attribute data is wrong type")
return v
class SensorVariable(BaseModel):
"""docstring for SensorVariable."""
# name: str
type: str | None = "str"
shape: list[str] | None = ["time"]
attributes: dict[str, SensorAttribute]
# attributes: dict | None = dict()
# modes: list[str] | None = ["default"]
class SensorSetting(BaseModel):
"""docstring for SensorSetting."""
# name: str
type: str | None = "str"
shape: list[str] | None = ["time"]
attributes: dict[str, SensorAttribute]
# attributes: dict | None = dict()
# modes: list[str] | None = ["default"]
class SensorMetadata(BaseModel):
"""docstring for SensorMetadata."""
attributes: dict[str, SensorAttribute]
variables: dict[str, SensorVariable]
settings: dict[str, SensorSetting]
class SensorConfig(BaseModel):
"""docstring for SensorConfig."""
make: str
model: str
serial_number: str
metadata: SensorMetadata
# # variables: list | None = []
# attributes: dict[str, SensorAttribute]
# variables: dict[str, SensorVariable]
# # # variables: dict | None = {}
# settings: dict[str, SensorSetting]
interfaces: dict | None = {}
daq: str | None = "default"
def configure():
# attributes
attributes = dict()
# for name, att in metadata["attributes"]:
meta = SensorMetadata(
attributes=metadata["attributes"],
variables=metadata["variables"],
settings=metadata["settings"]
)
config = SensorConfig(
make=metadata["attributes"]["make"]["data"],
model=metadata["attributes"]["model"]["data"],
serial_number="1234",
metadata=meta
# attributes=metadata["attributes"],
# variables=metadata["variables"],
# settings=metadata["settings"]
)
# print(json.dumps(config.metadata.dict(), indent=3))
# if "time" in config.metadata.variables:
# print(config.metadata.dict(include={"attributes"}))
print(config.metadata.attributes["format_version"].data)
if __name__ == "__main__":
configure()