20from optparse
import OptionParser
26from gstlal
import kernels
27from gstlal
import pipeparts
28from gstlal
import datasource
29from gstlal.psd
import read_psd
30from gstlal.stream
import Stream
171def parse_command_line():
172 parser = OptionParser(description = __doc__)
178 datasource.append_options(parser)
184 parser.add_option(
"--shift", metavar =
"ns", help =
"Number of nanoseconds to delay (negative) or advance (positive) the time stream", type =
"int")
185 parser.add_option(
"--sample-rate", metavar =
"Hz", default = 16384, type =
"int", help =
"Sample rate at which to generate the data, should be less than or equal to the sample rate of the measured psds provided, default = 16384 Hz, max 16384 Hz")
186 parser.add_option(
"--whiten-reference-psd", metavar =
"name", help =
"Set the name of psd xml file to whiten the data with")
187 parser.add_option(
"--whiten-track-psd", action =
"store_true", help =
"Whiten the data by tracking PSD changes, can be combined with --whiten-reference-psd to seed the process.")
188 parser.add_option(
"--color-psd", metavar =
"name", help =
"Set the name of psd xml file to color the data with")
189 parser.add_option(
"--output-path", metavar =
"name", default =
".", help =
"Path to output frame files (default = \".\").")
190 parser.add_option(
"--output-channel-name", metavar =
"name", help =
"The name of the channel in the output frames. The default is the same as the channel name")
191 parser.add_option(
"--output-frame-type", metavar =
"name", help =
"Frame type, required")
192 parser.add_option(
"--output-frame-duration", metavar =
"s", default = 16, type =
"int", help =
"Set the duration of the output frames. The duration of the frame file will be multiplied by --frames-per-file. Default: 16s")
193 parser.add_option(
"--output-frames-per-file", metavar =
"n", default = 256, type =
"int", help =
"Set the number of frames per file. Default: 256")
194 parser.add_option(
"-v",
"--verbose", action =
"store_true", help =
"Be verbose (optional).")
200 options, filenames = parser.parse_args()
202 if options.sample_rate > 16384:
203 raise ValueError(
"--sample-rate must be <= 16384")
205 if options.output_frame_type
is None:
206 raise ValueError(
"--frame-type is required")
209 return options, filenames
216options, filenames = parse_command_line()
219os.makedirs(options.output_path, exist_ok=
True)
222gw_data_source = datasource.DataSourceInfo.from_optparse(options)
225instrument = list(gw_data_source.channel_dict.keys())[0]
228if options.output_channel_name
is None:
229 options.output_channel_name = gw_data_source.channel_dict[instrument]
232injections, gw_data_source.injection_filename = options.injection_file,
None
236gw_data_source.block_size = 8 * options.sample_rate
237stream = Stream.from_datasource(
240 name=os.path.split(sys.argv[0])[1],
241 verbose=options.verbose
246 stream = stream.progressreport(
"frames")
248if options.shift
is not None:
250 stream = stream.shift(shift=options.shift)
253 stream = stream.progressreport(
"frames_shifted")
255if options.whiten_reference_psd:
257 wpsd = read_psd(options.whiten_reference_psd, verbose = options.verbose)[instrument]
262if options.whiten_reference_psd
or options.whiten_track_psd:
264 stream = stream.condition(options.sample_rate, instrument, psd=wpsd, track_psd=options.whiten_track_psd)
267 stream = stream.resample(quality=9).capsfilter(f
"audio/x-raw, rate={options.sample_rate}")
271 if options.data_source ==
"white":
272 renormalize_factor = 1.0 / (pipeparts.audioresample_variance_gain(9, 16384, options.sample_rate))**.5
273 stream = stream.audioamplify(renormalize_factor)
279 rpsd = read_psd(options.color_psd, verbose = options.verbose)[instrument]
282 max_sample = int(round(1.0 / rpsd.deltaF * options.sample_rate / 2.0)) + 1
285 rpsd_data = numpy.array(rpsd.data.data[:max_sample])
286 if len(rpsd_data) < max_sample:
287 rpsd_data = numpy.append(rpsd_data, numpy.zeros(max_sample - len(rpsd_data)))
289 rpsd = lal.CreateCOMPLEX16FrequencySeries(name = rpsd.name, epoch = rpsd.epoch, f0 = rpsd.f0, deltaF = rpsd.deltaF, length = max_sample, sampleUnits = rpsd.sampleUnits)
290 rpsd.data.data = rpsd_data
293 FIRKernel = kernels.PSDFirKernel()
294 fir_matrix, latency, measured_sample_rate = FIRKernel.psd_to_linear_phase_whitening_fir_kernel(rpsd, invert =
False)
297 stream = stream.firbank(latency=latency, fir_matrix=[fir_matrix], block_stride=(1 * options.sample_rate))
301tagstr = f
"units=strain,channel-name={options.output_channel_name},instrument={instrument}"
304stream = stream.taginject(tagstr)
307if injections
is not None:
308 stream = stream.injections(injections)
311stream = stream.framecppchannelmux(
312 channels=f
"{instrument}:{options.output_channel_name}",
313 frame_duration=options.output_frame_duration,
314 frames_per_file=options.output_frames_per_file
318framesink = stream.framecppfilesink(frame_type=options.output_frame_type)
321framesink.connect(
"notify::timestamp", pipeparts.framecpp_filesink_ldas_path_handler, (options.output_path, 5))