1# =====================================================
2# Imports
3# =====================================================
4from typing import Annotated
5from xml.etree import cElementTree as et
6
7from pydantic import Field
8
9import mt_metadata.transfer_functions.io.emtfxml.metadata.helpers as helpers
10from mt_metadata.base import MetadataBase
11
12
13# =====================================================
14class Electric(MetadataBase):
15 name: Annotated[
16 str,
17 Field(
18 default="",
19 description="Name of the channel",
20 alias=None,
21 json_schema_extra={
22 "units": None,
23 "required": True,
24 "examples": ["ex"],
25 },
26 ),
27 ]
28
29 orientation: Annotated[
30 float,
31 Field(
32 default=0.0,
33 description="orientation angle relative to geographic north",
34 alias=None,
35 json_schema_extra={
36 "units": "degrees",
37 "required": True,
38 "examples": ["11.9"],
39 },
40 ),
41 ]
42
43 x: Annotated[
44 float,
45 Field(
46 default=0.0,
47 description="location of negative sensor relative center point in north direction",
48 alias=None,
49 json_schema_extra={
50 "units": "meters",
51 "required": True,
52 "examples": ["100.0"],
53 },
54 ),
55 ]
56
57 x2: Annotated[
58 float,
59 Field(
60 default=0.0,
61 description="location of positive sensor relative center point in north direction",
62 alias=None,
63 json_schema_extra={
64 "units": "meters",
65 "required": True,
66 "examples": ["100.0"],
67 },
68 ),
69 ]
70
71 y: Annotated[
72 float,
73 Field(
74 default=0.0,
75 description="location of negative sensor relative center point in east direction",
76 alias=None,
77 json_schema_extra={
78 "units": "meters",
79 "required": True,
80 "examples": ["100.0"],
81 },
82 ),
83 ]
84
85 y2: Annotated[
86 float,
87 Field(
88 default=0.0,
89 description="location of positive sensor relative center point in east direction",
90 alias=None,
91 json_schema_extra={
92 "units": "meters",
93 "required": True,
94 "examples": ["100.0"],
95 },
96 ),
97 ]
98
99 z: Annotated[
100 float,
101 Field(
102 default=0.0,
103 description="location of negative sensor relative center point in depth",
104 alias=None,
105 json_schema_extra={
106 "units": "meters",
107 "required": True,
108 "examples": ["100.0"],
109 },
110 ),
111 ]
112
113 z2: Annotated[
114 float,
115 Field(
116 default=0.0,
117 description="location of positive sensor relative center point in depth",
118 alias=None,
119 json_schema_extra={
120 "units": "meters",
121 "required": True,
122 "examples": ["100.0"],
123 },
124 ),
125 ]
126
127 def to_xml(self, string: bool = False, required: bool = True) -> str | et.Element:
128 """
129
130 :param string: return a string representation of the xml, defaults to False
131 :type string: bool, optional
132 :param required: return a string representation of the xml, defaults to True
133 :type required: bool, optional
134 :type required: bool, optional
135 :return: string or xml element representation of the Electric object
136 :rtype: str | et.Element
137
138 """
139
140 for attr in ["orientation", "x", "y", "z", "x2", "y2", "z2"]:
141 value = getattr(self, attr)
142 if value is None:
143 setattr(self, attr, 0)
144
145 root = et.Element(
146 self.__class__.__name__.capitalize(),
147 {
148 "name": self.name,
149 "orientation": f"{self.orientation:.3f}",
150 "x": f"{self.x:.3f}",
151 "y": f"{self.y:.3f}",
152 "z": f"{self.z:.3f}",
153 "x2": f"{self.x2:.3f}",
154 "y2": f"{self.y2:.3f}",
155 "z2": f"{self.z2:.3f}",
156 },
157 )
158
159 if string:
160 return helpers.element_to_string(root)
161 return root