forked from Botts-Innovative-Research/OSHConnect-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswe_components.py
More file actions
207 lines (161 loc) · 7.09 KB
/
swe_components.py
File metadata and controls
207 lines (161 loc) · 7.09 KB
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
# =============================================================================
# Copyright (c) 2025 Botts Innovative Research Inc.
# Date: 2025/9/30
# Author: Ian Patterson
# Contact Email: ian@botts-inc.com
# =============================================================================
from __future__ import annotations
from numbers import Real
from typing import Union, Any
from pydantic import BaseModel, Field, field_validator, SerializeAsAny
from .csapi4py.constants import GeometryTypes
from .api_utils import UCUMCode, URI
from .geometry import Geometry
"""
NOTE: The following classes are used to represent the Record Schemas that are required for use with Datastreams
The names are likely to change to include a "Schema" suffix to differentiate them from the actual data structures.
The current scope of the project likely excludes conversion from received data to actual SWE Common data structures,
in the event this is added it will most likely be in a separate module as those structures have use cases outside of
the API solely
"""
# TODO: Add field validators that are missing
# TODO: valid string fields that are intended to represent time/date values
# TODO: Validate places where string fields are not allowed to be empty
class AnyComponentSchema(BaseModel):
type: str = Field(...)
id: str = Field(None)
label: str = Field(None)
description: str = Field(None)
updatable: bool = Field(False)
optional: bool = Field(False)
definition: str = Field(None)
class DataRecordSchema(AnyComponentSchema):
type: str = "DataRecord"
fields: SerializeAsAny[list[AnyComponentSchema]] = Field(...)
class VectorSchema(AnyComponentSchema):
label: str = Field(...)
name: str = Field(...)
type: str = "Vector"
definition: str = Field(...)
reference_frame: str = Field(...)
local_frame: str = Field(None)
# TODO: VERIFY might need to be moved further down when these are defined
coordinates: SerializeAsAny[Union[list[CountSchema], list[QuantitySchema], list[TimeSchema]]] = Field(...)
class DataArraySchema(AnyComponentSchema):
type: str = "DataArray"
name: str = Field(...)
element_count: dict | str | CountSchema = Field(..., serialization_alias='elementCount') # Should type of Count
element_type: SerializeAsAny[AnyComponentSchema] = Field(..., serialization_alias='elementType')
encoding: str = Field(...) # TODO: implement an encodings class
values: list = Field(None)
class MatrixSchema(AnyComponentSchema):
type: str = "Matrix"
element_count: dict | str | CountSchema = Field(..., serialization_alias='elementCount') # Should be type of Count
element_type: SerializeAsAny[list[AnyComponentSchema]] = Field(..., serialization_alias='elementType')
encoding: str = Field(...) # TODO: implement an encodings class
values: list = Field(None)
reference_frame: str = Field(None)
local_frame: str = Field(None)
class DataChoiceSchema(AnyComponentSchema):
type: str = "DataChoice"
updatable: bool = Field(False)
optional: bool = Field(False)
choice_value: CategorySchema = Field(..., serialization_alias='choiceValue') # TODO: Might be called "choiceValues"
items: SerializeAsAny[list[AnyComponentSchema]] = Field(...)
class GeometrySchema(AnyComponentSchema):
label: str = Field(...)
type: str = "Geometry"
updatable: bool = Field(False)
optional: bool = Field(False)
definition: str = Field(...)
constraint: dict = Field(default_factory=lambda: {
'geomTypes': [
GeometryTypes.POINT.value,
GeometryTypes.LINESTRING.value,
GeometryTypes.POLYGON.value,
GeometryTypes.MULTI_POINT.value,
GeometryTypes.MULTI_LINESTRING.value,
GeometryTypes.MULTI_POLYGON.value
]
})
nil_values: list = Field(None, serialization_alias='nilValues')
srs: str = Field(...)
value: Geometry = Field(None)
class AnySimpleComponentSchema(AnyComponentSchema):
label: str = Field(...)
description: str = Field(None)
type: str = Field(...)
updatable: bool = Field(False)
optional: bool = Field(False)
definition: str = Field(...)
reference_frame: str = Field(None, serialization_alias='referenceFrame')
axis_id: str = Field(None, serialization_alias='axisID')
quality: list[Union[QuantitySchema, QuantityRangeSchema, CategorySchema, TextSchema]] = Field(
None) # TODO: Union[Quantity, QuantityRange, Category, Text]
nil_values: list = Field(None, serialization_alias='nilValues')
constraint: Any = Field(None)
value: Any = Field(None)
name: str = Field(...)
class AnyScalarComponentSchema(AnySimpleComponentSchema):
"""
A base class for all scalar components. The structure is essentially that of AnySimpleComponent
"""
pass
class BooleanSchema(AnyScalarComponentSchema):
type: str = "Boolean"
value: bool = Field(None)
class CountSchema(AnyScalarComponentSchema):
type: str = "Count"
value: int = Field(None)
class QuantitySchema(AnyScalarComponentSchema):
type: str = "Quantity"
value: Union[float, str] = Field(None)
uom: Union[UCUMCode, URI] = Field(...)
@field_validator('value')
@classmethod
def validate_value(cls, v):
if isinstance(v, Real):
return v
elif isinstance(v, str):
if v in ['NaN', 'INFINITY', '+INFINITY', '-INFINITY']:
return v
else:
raise ValueError(
'string representation of value must be one of the following: NaN, INFINITY, +INFINITY, -INFINITY')
else:
try:
return float(v)
except ValueError:
raise ValueError('value must be a number or a string representing a special value '
'[NaN, INFINITY, +INFINITY, -INFINITY]')
class TimeSchema(AnyScalarComponentSchema):
type: str = "Time"
value: str = Field(None)
reference_time: str = Field(None, serialization_alias='referenceTime')
local_frame: str = Field(None)
uom: Union[UCUMCode, URI] = Field(...)
class CategorySchema(AnyScalarComponentSchema):
type: str = "Category"
value: str = Field(None)
code_space: str = Field(None, serialization_alias='codeSpace')
class TextSchema(AnyScalarComponentSchema):
type: str = "Text"
value: str = Field(None)
class CountRangeSchema(AnySimpleComponentSchema):
type: str = "CountRange"
value: list[int] = Field(None)
uom: Union[UCUMCode, URI] = Field(...)
class QuantityRangeSchema(AnySimpleComponentSchema):
type: str = "QuantityRange"
value: list[Union[float, str]] = Field(None)
uom: Union[UCUMCode, URI] = Field(...)
class TimeRangeSchema(AnySimpleComponentSchema):
type: str = "TimeRange"
value: list[str] = Field(None)
reference_time: str = Field(None, serialization_alias='referenceTime')
local_frame: str = Field(None)
uom: Union[UCUMCode, URI] = Field(...)
class CategoryRangeSchema(AnySimpleComponentSchema):
type: str = "CategoryRange"
value: list[str] = Field(None)
code_space: str = Field(None, serialization_alias='codeSpace')