Coverage for C: \ Users \ peaco \ OneDrive \ Documents \ GitHub \ mt_metadata \ mt_metadata \ transfer_functions \ io \ zonge \ metadata \ unit.py: 90%

20 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-01-10 00:11 -0800

1# ===================================================== 

2# Imports 

3# ===================================================== 

4from typing import Annotated 

5 

6from pydantic import Field, field_validator, ValidationInfo 

7 

8from mt_metadata.base import MetadataBase 

9from mt_metadata.common.units import get_unit_object 

10 

11 

12# ===================================================== 

13 

14 

15class Unit(MetadataBase): 

16 length: Annotated[ 

17 str, 

18 Field( 

19 default="m", 

20 description="Type of smoothing for phase slope algorithm", 

21 alias=None, 

22 json_schema_extra={ 

23 "units": None, 

24 "required": True, 

25 "examples": ["m"], 

26 }, 

27 ), 

28 ] 

29 

30 e: Annotated[ 

31 str, 

32 Field( 

33 default="mV/km", 

34 description="Units for the electric field", 

35 alias=None, 

36 json_schema_extra={ 

37 "units": None, 

38 "required": False, 

39 "examples": ["mV/km"], 

40 }, 

41 ), 

42 ] 

43 

44 b: Annotated[ 

45 str, 

46 Field( 

47 default="nT", 

48 description="Units for the magnetic field", 

49 alias=None, 

50 json_schema_extra={ 

51 "units": None, 

52 "required": True, 

53 "examples": ["nT"], 

54 }, 

55 ), 

56 ] 

57 

58 @field_validator("length", "b", "e", mode="before") 

59 @classmethod 

60 def validate_units(cls, value: str, info: ValidationInfo) -> str: 

61 """ 

62 validate units base on input string will return the long name 

63 

64 Parameters 

65 ---------- 

66 value : units string 

67 unit string separated by either '/' for division or ' ' for 

68 multiplication. Or 'per' and ' ', respectively 

69 info : ValidationInfo 

70 _description_ 

71 

72 Returns 

73 ------- 

74 str 

75 return the long descriptive name of the unit. For example 'kilometers'. 

76 """ 

77 if value in [None, ""]: 

78 return "" 

79 try: 

80 unit_object = get_unit_object(value) 

81 return unit_object.name 

82 except ValueError as error: 

83 raise KeyError(error) 

84 except KeyError as error: 

85 raise KeyError(error)