Coverage for C: \ Users \ peaco \ OneDrive \ Documents \ GitHub \ mth5 \ mth5 \ groups \ filter_groups \ fir_filter_group.py: 93%
43 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-27 20:09 -0800
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-27 20:09 -0800
1# -*- coding: utf-8 -*-
2"""
3Created on Wed Jun 9 08:55:16 2021
5:copyright:
6 Jared Peacock (jpeacock@usgs.gov)
8:license: MIT
10"""
12# =============================================================================
13# Imports
14# =============================================================================
17from mt_metadata.timeseries.filters import FIRFilter
19from mth5.groups.base import BaseGroup
22# =============================================================================
23# fir Group
24# =============================================================================
25class FIRGroup(BaseGroup):
26 """
27 Container for fir type filters
29 """
31 def __init__(self, group, **kwargs):
32 super().__init__(group, **kwargs)
34 @property
35 def filter_dict(self):
36 """
38 Dictionary of available fir filters
40 :return: DESCRIPTION
41 :rtype: TYPE
42 """
43 f_dict = {}
44 for key in self.hdf5_group.keys():
45 fir_group = self.hdf5_group[key]
46 f_dict[key] = {
47 "type": fir_group.attrs["type"],
48 "hdf5_ref": fir_group.ref,
49 }
51 return f_dict
53 def add_filter(self, name, coefficients, fir_metadata):
54 """
55 create an HDF5 group/dataset from information given.
57 :param name: Nane of the filter
58 :type name: string
59 :param poles: poles of the filter as complex numbers
60 :type poles: np.ndarray(dtype=complex)
61 :param zeros: zeros of the filter as complex numbers
62 :type zeros: np.ndarray(dtype=comples)
63 :param fir_metadata: metadata dictionary see
64 :class:`mt_metadata.timeseries.filters.PoleZeroFilter` for details on entries
65 :type fir_metadata: dictionary
67 """
68 # create a group for the filter by the name
69 fir_filter_group = self.hdf5_group.create_group(name)
71 # create datasets for the poles and zeros
72 fir_ds = fir_filter_group.create_dataset(
73 "coefficients",
74 coefficients.shape,
75 **self.dataset_options,
76 )
78 fir_ds[:] = coefficients
80 # fill in the metadata
81 fir_filter_group.attrs.update(fir_metadata)
83 return fir_filter_group
85 def remove_filter(self):
86 pass
88 def get_filter(self, name):
89 """
90 Get a filter from the name
92 :param name: name of the filter
93 :type name: string
95 :return: HDF5 group of the fir filter
96 """
97 return self.hdf5_group[name]
99 def from_object(self, fir_object):
100 """
101 make a filter from a :class:`mt_metadata.timeseries.filters.PoleZeroFilter`
103 :param fir_object: MT metadata PoleZeroFilter
104 :type fir_object: :class:`mt_metadata.timeseries.filters.PoleZeroFilter`
106 """
108 if not isinstance(fir_object, FIRFilter):
109 msg = "Filter must be a FrequencyResponseTableFilter not %s"
110 self.logger.error(msg, type(fir_object))
111 raise TypeError(msg)
113 input_dict = fir_object.to_dict(single=True, required=False)
114 input_dict.pop("coefficients")
115 for k, v in input_dict.items():
116 if v is None:
117 input_dict[k] = str(v)
119 fir_group = self.add_filter(
120 fir_object.name,
121 fir_object.coefficients,
122 input_dict,
123 )
124 return fir_group
126 def to_object(self, name):
127 """
128 make a :class:`mt_metadata.timeseries.filters.pole_zeros_filter` object
130 :return: DESCRIPTION
131 :rtype: TYPE
133 """
135 fir_group = self.get_filter(name)
137 fir_obj = FIRFilter(**fir_group.attrs)
139 try:
140 fir_obj.coefficients = fir_group["coefficients"][:]
141 except TypeError:
142 self.logger.debug("fir filter %s has no coefficients", name)
143 fir_obj.coefficients = []
145 return fir_obj