1# =====================================================
2# Imports
3# =====================================================
4from typing import Annotated
5
6from pydantic import Field, field_validator
7
8from mt_metadata.base import MetadataBase
9from mt_metadata.common.units import get_unit_object
10
11
12# =====================================================
13class Channels(MetadataBase):
14 ref: Annotated[
15 str,
16 Field(
17 default="",
18 description="reference to the site name",
19 alias=None,
20 json_schema_extra={
21 "units": None,
22 "required": True,
23 "examples": ["site"],
24 },
25 ),
26 ]
27
28 units: Annotated[
29 str,
30 Field(
31 default="",
32 description="units of the distance coordinates",
33 alias=None,
34 json_schema_extra={
35 "units": None,
36 "required": True,
37 "examples": ["site"],
38 },
39 ),
40 ]
41
42 inputs: Annotated[
43 list[str],
44 Field(
45 default_factory=list,
46 description="list of input channel names (sources)",
47 alias=None,
48 json_schema_extra={
49 "units": None,
50 "required": True,
51 "examples": [["Hx", "Hy"]],
52 },
53 ),
54 ]
55
56 outputs: Annotated[
57 list[str],
58 Field(
59 default_factory=list,
60 description="list of output channel names (responses)",
61 alias=None,
62 json_schema_extra={
63 "units": None,
64 "required": True,
65 "examples": [["Ex", "Ey", "Hz"]],
66 },
67 ),
68 ]
69
70 @field_validator("units", mode="before")
71 @classmethod
72 def validate_units(cls, value: str) -> str:
73 if value in [None, ""]:
74 return ""
75 try:
76 unit_object = get_unit_object(value)
77 return unit_object.name
78 except ValueError as error:
79 raise KeyError(error)
80 except KeyError as error:
81 raise KeyError(error)