Coverage for C: \ Users \ peaco \ OneDrive \ Documents \ GitHub \ mth5 \ mth5 \ groups \ filter_groups \ coefficient_filter_group.py: 89%

35 statements  

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

1# -*- coding: utf-8 -*- 

2""" 

3Created on Wed Jun 9 08:58:15 2021 

4 

5:copyright: 

6 Jared Peacock (jpeacock@usgs.gov) 

7 

8:license: MIT 

9 

10""" 

11 

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

13# Imports 

14# ============================================================================= 

15from mt_metadata.timeseries.filters import CoefficientFilter 

16 

17from mth5.groups.base import BaseGroup 

18 

19 

20# ============================================================================= 

21# COEFFCIENT Group 

22# ============================================================================= 

23 

24 

25class CoefficientGroup(BaseGroup): 

26 """ 

27 Container for Coefficient type filters 

28 """ 

29 

30 def __init__(self, group, **kwargs): 

31 super().__init__(group, **kwargs) 

32 

33 @property 

34 def filter_dict(self): 

35 """ 

36 

37 Dictionary of available coefficient filters 

38 

39 :return: DESCRIPTION 

40 :rtype: TYPE 

41 """ 

42 f_dict = {} 

43 for key in self.hdf5_group.keys(): 

44 coefficient_group = self.hdf5_group[key] 

45 f_dict[key] = { 

46 "type": coefficient_group.attrs["type"], 

47 "hdf5_ref": coefficient_group.ref, 

48 } 

49 

50 return f_dict 

51 

52 def add_filter(self, name, coefficient_metadata): 

53 """ 

54 Add a coefficient Filter 

55 

56 :param name: DESCRIPTION 

57 :type name: TYPE 

58 :param coefficient_metadata: DESCRIPTION 

59 :type coefficient_metadata: TYPE 

60 :return: DESCRIPTION 

61 :rtype: TYPE 

62 

63 """ 

64 # create a group for the filter by the name 

65 coefficient_filter_group = self.hdf5_group.create_group(name) 

66 

67 # fill in the metadata 

68 coefficient_filter_group.attrs.update(coefficient_metadata) 

69 

70 return coefficient_filter_group 

71 

72 def remove_filter(self): 

73 pass 

74 

75 def get_filter(self, name): 

76 """ 

77 Get a filter from the name 

78 

79 :param name: name of the filter 

80 :type name: string 

81 

82 :return: HDF5 group of the ZPK filter 

83 """ 

84 return self.hdf5_group[name] 

85 

86 def from_object(self, coefficient_object): 

87 """ 

88 make a filter from a :class:`mt_metadata.timeseries.filters.CoefficientFilter` 

89 

90 :param zpk_object: MT metadata Coefficient Filter 

91 :type zpk_object: :class:`mt_metadata.timeseries.filters.CoefficientFilter` 

92 

93 """ 

94 

95 if not isinstance(coefficient_object, CoefficientFilter): 

96 msg = f"Filter must be a CoefficientFilter not {type(coefficient_object)}" 

97 self.logger.error(msg) 

98 raise TypeError(msg) 

99 

100 input_dict = coefficient_object.to_dict(single=True, required=False) 

101 for k, v in input_dict.items(): 

102 if v is None: 

103 input_dict[k] = str(v) 

104 coefficient_group = self.add_filter(coefficient_object.name, input_dict) 

105 return coefficient_group 

106 

107 def to_object(self, name): 

108 """ 

109 make a :class:`mt_metadata.timeseries.filters.CoefficientFilter` object 

110 

111 :return: DESCRIPTION 

112 :rtype: TYPE 

113 

114 """ 

115 

116 coefficient_group = self.get_filter(name) 

117 

118 coefficient_obj = CoefficientFilter(**coefficient_group.attrs) 

119 

120 return coefficient_obj