34 gobject.threads_init()
40 from pylal
import datatypes
as laltypes
43 __author__ =
"Kipp Cannon <kipp.cannon@ligo.org>, Chad Hanna <chad.hanna@ligo.org>, Drew Keppel <drew.keppel@ligo.org>"
57 def repack_complex_array_to_real(arr):
59 Repack a complex-valued array into a real-valued array with twice
60 as many columns. Used to set complex arrays as values on elements
61 that expose them as real-valued array properties (gobject doesn't
62 understand complex numbers). The return value is a view into the
67 if arr.dtype.kind !=
"c":
69 assert arr.dtype.itemsize % 2 == 0
70 return arr.view(dtype = numpy.dtype(
"f%d" % (arr.dtype.itemsize // 2)))
73 def repack_real_array_to_complex(arr):
75 Repack a real-valued array into a complex-valued array with half as
76 many columns. Used to retrieve complex arrays from elements that
77 expose them as real-valued array properties (gobject doesn't
78 understand complex numbers). The return value is a view into the
83 if arr.dtype.kind !=
"f":
85 return arr.view(dtype = numpy.dtype(
"c%d" % (arr.dtype.itemsize * 2)))
97 def get_unit_size(caps):
99 name = struct.get_name()
100 if name
in (
"audio/x-raw-complex",
"audio/x-raw-float",
"audio/x-raw-int"):
101 assert struct[
"width"] % 8 == 0
102 return struct[
"channels"] * struct[
"width"] // 8
103 elif name ==
"video/x-raw-rgb":
104 assert struct[
"bpp"] % 8 == 0
105 return struct[
"width"] * struct[
"height"] * struct[
"bpp"] // 8
106 raise ValueError(caps)
109 def numpy_dtype_from_caps(caps):
111 name = struct.get_name()
112 if name ==
"audio/x-raw-float":
113 assert struct[
"width"] % 8 == 0
114 return "f%d" % (struct[
"width"] // 8)
115 elif name ==
"audio/x-raw-int":
116 assert struct[
"width"] % 8 == 0
118 return "i%d" % (struct[
"width"] // 8)
120 return "u%d" % (struct[
"width"] // 8)
121 elif name ==
"audio/x-raw-complex":
122 assert struct[
"width"] % 8 == 0
123 return "c%d" % (struct[
"width"] // 8)
124 raise ValueError(name)
127 def caps_from_numpy_dtype(dtype):
128 if dtype.char ==
'f':
129 caps = gst.Caps(
"audio/x-raw-float, width=32")
130 elif dtype.char ==
'd':
131 caps = gst.Caps(
"audio/x-raw-float, width=64")
132 elif dtype.char ==
'b':
133 caps = gst.Caps(
"audio/x-raw-int, width=8, signed=true")
134 elif dtype.char ==
'B':
135 caps = gst.Caps(
"audio/x-raw-int, width=8, signed=false")
136 elif dtype.char ==
'h':
137 caps = gst.Caps(
"audio/x-raw-int, width=16, signed=true")
138 elif dtype.char ==
'H':
139 caps = gst.Caps(
"audio/x-raw-int, width=16, signed=false")
140 elif dtype.char ==
'i':
141 caps = gst.Caps(
"audio/x-raw-int, width=32, signed=true")
142 elif dtype.char ==
'I':
143 caps = gst.Caps(
"audio/x-raw-int, width=32, signed=false")
144 elif dtype.char ==
'l':
145 caps = gst.Caps(
"audio/x-raw-int, width=64, signed=true")
146 elif dtype.char ==
'L':
147 caps = gst.Caps(
"audio/x-raw-int, width=64, signed=false")
149 raise ValueError(dtype)
150 caps[0][
"endianness"] = {
151 "=": 1234
if sys.byteorder ==
"little" else 4321,
158 def caps_from_array(arr, rate = None):
159 caps = caps_from_numpy_dtype(arr.dtype)
161 caps[0][
"rate"] = rate
162 caps[0][
"channels"] = arr.shape[1]
166 def array_from_audio_buffer(buf):
167 channels = buf.caps[0][
"channels"]
172 a = numpy.frombuffer(buf, dtype = numpy_dtype_from_caps(buf.caps))
174 a = numpy.array((), dtype = numpy_dtype_from_caps(buf.caps))
175 a.shape = (len(a) // channels, channels)
179 def audio_buffer_from_array(arr, timestamp, offset, rate):
180 buf = gst.Buffer(arr.data)
181 buf.caps = caps_from_array(arr, rate = rate)
182 buf.timestamp = timestamp
183 buf.duration = (gst.SECOND * arr.shape[0] + rate // 2) // rate
185 buf.offset_end = offset + arr.shape[0]
198 def parse_spectrum_message(message):
200 Parse a "spectrum" message from the lal_whiten element, return a
201 LAL REAL8FrequencySeries containing the strain spectral density.
203 s = message.structure
204 return laltypes.REAL8FrequencySeries(
205 name = s[
"instrument"]
if s.has_field(
"instrument")
else "",
206 epoch = laltypes.LIGOTimeGPS(0, message.timestamp),
208 deltaF = s[
"delta-f"],
209 sampleUnits = laltypes.LALUnit(s[
"sample-units"].strip()),
210 data = numpy.array(s[
"magnitude"])
223 def parse_framesrc_tags(taglist):
225 instrument = taglist[
"instrument"]
229 channel_name = taglist[
"channel-name"]
232 if "units" in taglist:
233 sample_units = laltypes.LALUnit(taglist[
"units"].strip())
237 "instrument": instrument,
238 "channel-name": channel_name,
239 "sample-units": sample_units