1# =====================================================
2# Imports
3# =====================================================
4from typing import Annotated
5from xml.etree import cElementTree as et
6
7from pydantic import Field, field_validator
8
9from mt_metadata.base import MetadataBase
10from mt_metadata.common import Comment
11from mt_metadata.transfer_functions.io.emtfxml.metadata import helpers
12
13
14# =====================================================
15class DataQualityNotes(MetadataBase):
16 good_from_period: Annotated[
17 float | None,
18 Field(
19 default=None,
20 description="Data are good for periods larger than this number",
21 alias=None,
22 json_schema_extra={
23 "units": None,
24 "required": False,
25 "examples": ["0.01"],
26 },
27 ),
28 ]
29
30 good_to_period: Annotated[
31 float | None,
32 Field(
33 default=None,
34 description="Data are good for periods smaller than this number",
35 alias=None,
36 json_schema_extra={
37 "units": None,
38 "required": False,
39 "examples": ["1000"],
40 },
41 ),
42 ]
43
44 rating: Annotated[
45 int | None,
46 Field(
47 default=None,
48 description="Rating of the data from 0 to 5 where 5 is the best and 0 is unrated",
49 alias=None,
50 json_schema_extra={
51 "units": None,
52 "required": False,
53 "examples": ["4"],
54 },
55 ),
56 ]
57
58 comments: Annotated[
59 Comment | str | None,
60 Field(
61 default_factory=Comment, # type: ignore
62 description="Comments about the data quality",
63 alias=None,
64 json_schema_extra={
65 "units": None,
66 "required": False,
67 "examples": ["Data quality is good", "Some issues found"],
68 },
69 ),
70 ]
71
72 @field_validator("comments", mode="before")
73 @classmethod
74 def validate_comments(cls, value) -> Comment:
75 """
76 Validate that the value is a valid string.
77 """
78 if isinstance(value, str):
79 return Comment(value=value) # type: ignore[return-value]
80 return value
81
82 def read_dict(self, input_dict: dict) -> None:
83 """
84
85 :param input_dict: input dictionary to read and populate the model fields.
86 :type input_dict: dict
87 :return: None
88 :rtype: None
89
90 """
91 try:
92 comments_dict = {
93 "comments": input_dict["data_quality_notes"].get("comments", "")
94 }
95 except KeyError:
96 comments_dict = {"comments": ""}
97 self.comments = Comment() # type: ignore
98 self.comments.read_dict(comments_dict)
99 helpers._read_element(self, input_dict, "data_quality_notes")
100
101 def to_xml(self, string: bool = False, required: bool = True) -> str | et.Element:
102 """
103 Convert the DataQualityNotes instance to XML format.
104
105 :param string: If True, return the XML as a string. If False, return an ElementTree Element.
106 :type string: bool, optional
107 :param required: If True, include all required fields in the XML.
108 :type required: bool, optional
109 :return: The XML representation of the DataQualityNotes instance.
110 :rtype: str | et.Element
111 :rtype: TYPE
112
113 """
114
115 return helpers.to_xml(
116 self,
117 string=string,
118 required=required,
119 order=[
120 "rating",
121 "good_from_period",
122 "good_to_period",
123 "comments",
124 ],
125 )