-
-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathspectrogrambase.py
More file actions
109 lines (91 loc) · 3 KB
/
Copy pathspectrogrambase.py
File metadata and controls
109 lines (91 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
from radiospectra.exceptions import SpectraMetaValidationError
from radiospectra.mixins import NonUniformImagePlotMixin, PcolormeshPlotMixin
__all__ = ["GenericSpectrogram"]
class GenericSpectrogram(PcolormeshPlotMixin, NonUniformImagePlotMixin):
"""
Base spectrogram class all spectrograms inherit.
Attributes
----------
meta : `dict-like`
Metadata for the spectrogram.
data : `numpy.ndarray`
The spectrogram data itself is a 2D array.
"""
_registry = {}
def __init_subclass__(cls, **kwargs):
super().__init_subclass__(**kwargs)
if hasattr(cls, "is_datasource_for"):
cls._registry[cls] = cls.is_datasource_for
def __init__(self, data, meta, **kwargs):
self.data = data
self.meta = meta
self._validate_meta()
@property
def observatory(self):
"""
The name of the observatory which recorded the spectrogram.
"""
return self.meta["observatory"].upper()
@property
def instrument(self):
"""
The name of the instrument which recorded the spectrogram.
"""
return self.meta["instrument"].upper()
@property
def detector(self):
"""
The detector which recorded the spectrogram.
"""
return self.meta["detector"].upper()
@property
def start_time(self):
"""
The start time of the spectrogram.
"""
return self.meta["start_time"]
@property
def end_time(self):
"""
The end time of the spectrogram.
"""
return self.meta["end_time"]
@property
def wavelength(self):
"""
The wavelength range of the spectrogram.
"""
return self.meta["wavelength"]
@property
def times(self):
"""
The times of the spectrogram.
"""
return self.meta["times"]
@property
def frequencies(self):
"""
The frequencies of the spectrogram.
"""
return self.meta["freqs"]
def _validate_meta(self):
"""
Validates the meta-information associated with a Spectrogram.
This method includes very basic validation checks which apply to
all of the kinds of files that radiospectra can read.
Datasource-specific validation should be handled in the relevant
file in radiospectra.spectrogram.sources.
"""
msg = "Spectrogram coordinate units for {} axis not present in metadata."
err_message = []
for i, ax in enumerate(["times", "freqs"]):
if self.meta.get(ax) is None:
err_message.append(msg.format(ax))
if err_message:
raise SpectraMetaValidationError("\n".join(err_message))
def __repr__(self):
return (
f"<{self.__class__.__name__} {self.observatory}, {self.instrument}, {self.detector}"
f" {self.wavelength.min} - {self.wavelength.max},"
f" {self.start_time.isot} to {self.end_time.isot}>"
)