# unless asked otherwise, revert the dynamic range factor if not args.dyn_range_factor: out_strain /= pycbc.DYN_RANGE_FAC # Adding strain channel info data = {} det_channels = {} logging.info("Adding strain channel named {} ...".format(args.channel_name)) det_channels[args.channel_name.split(':')[0]] = [args.channel_name] data[args.channel_name.split(':')[0]] = [out_strain] if args.other_channel_names: for channel in args.other_channel_names: logging.info("Reading channel {} ...".format(channel)) det = channel.split(':')[0] det_channels[det].append(channel) data[det].append( frame.query_and_read_frame(args.frame_type, channel, start, stop)) logging.info("Writing frames, each of duration {} sec".format(step)) for d in data.keys(): for s in tqdm.trange(start, stop, step): fname = os.path.join( args.outdir, filename.format(d, args.frame_type, s, step if s + step < stop else stop - s)) frame.write_frame(fname, det_channels[d], [ ts.time_slice(s, s + step if s + step < stop else stop) for ts in data[d] ])
def from_cli(opt, dyn_range_fac=1, precision='single'): """Parses the CLI options related to strain data reading and conditioning. Parameters ---------- opt : object Result of parsing the CLI with OptionParser, or any object with the required attributes (gps-start-time, gps-end-time, strain-high-pass, pad-data, sample-rate, (frame-cache or frame-files), channel-name, fake-strain, fake-strain-seed, gating_file). dyn_range_fac: {float, 1}, optional A large constant to reduce the dynamic range of the strain. Returns ------- strain : TimeSeries The time series containing the conditioned strain data. """ gating_info = {} if opt.frame_cache or opt.frame_files or opt.frame_type: if opt.frame_cache: frame_source = opt.frame_cache if opt.frame_files: frame_source = opt.frame_files logging.info("Reading Frames") if opt.frame_type: strain = query_and_read_frame(opt.frame_type, opt.channel_name, start_time=opt.gps_start_time-opt.pad_data, end_time=opt.gps_end_time+opt.pad_data) else: strain = read_frame(frame_source, opt.channel_name, start_time=opt.gps_start_time-opt.pad_data, end_time=opt.gps_end_time+opt.pad_data) if opt.zpk_z and opt.zpk_p and opt.zpk_k: logging.info("Highpass Filtering") strain = highpass(strain, frequency=opt.strain_high_pass) logging.info("Applying zpk filter") z = numpy.array(opt.zpk_z) p = numpy.array(opt.zpk_p) k = float(opt.zpk_k) strain = filter_zpk(strain.astype(numpy.float64), z, p, k) if opt.normalize_strain: logging.info("Dividing strain by constant") l = opt.normalize_strain strain = strain / l if opt.injection_file: logging.info("Applying injections") injections = InjectionSet(opt.injection_file) injections.apply(strain, opt.channel_name[0:2], distance_scale=opt.injection_scale_factor) if opt.sgburst_injection_file: logging.info("Applying sine-Gaussian burst injections") injections = SGBurstInjectionSet(opt.sgburst_injection_file) injections.apply(strain, opt.channel_name[0:2], distance_scale=opt.injection_scale_factor) logging.info("Highpass Filtering") strain = highpass(strain, frequency=opt.strain_high_pass) if precision == 'single': logging.info("Converting to float32") strain = (strain * dyn_range_fac).astype(float32) if opt.gating_file is not None: logging.info("Gating glitches") gate_params = numpy.loadtxt(opt.gating_file) if len(gate_params.shape) == 1: gate_params = [gate_params] strain = gate_data(strain, gate_params) gating_info['file'] = \ [gp for gp in gate_params \ if (gp[0] + gp[1] + gp[2] >= strain.start_time) \ and (gp[0] - gp[1] - gp[2] <= strain.end_time)] if opt.autogating_threshold is not None: # the + 0 is for making a copy glitch_times = detect_loud_glitches( strain + 0., threshold=opt.autogating_threshold, cluster_window=opt.autogating_cluster, low_freq_cutoff=opt.strain_high_pass, high_freq_cutoff=opt.sample_rate/2, corrupted_time=opt.pad_data+opt.autogating_pad) gate_params = [[gt, opt.autogating_width, opt.autogating_taper] \ for gt in glitch_times] if len(glitch_times) > 0: logging.info('Autogating at %s', ', '.join(['%.3f' % gt for gt in glitch_times])) strain = gate_data(strain, gate_params) gating_info['auto'] = gate_params logging.info("Resampling data") strain = resample_to_delta_t(strain, 1.0/opt.sample_rate, method='ldas') logging.info("Highpass Filtering") strain = highpass(strain, frequency=opt.strain_high_pass) logging.info("Remove Padding") start = opt.pad_data*opt.sample_rate end = len(strain)-opt.sample_rate*opt.pad_data strain = strain[start:end] if opt.fake_strain: logging.info("Generating Fake Strain") duration = opt.gps_end_time - opt.gps_start_time tlen = duration * opt.sample_rate pdf = 1.0/128 plen = int(opt.sample_rate / pdf) / 2 + 1 logging.info("Making PSD for strain") strain_psd = pycbc.psd.from_string(opt.fake_strain, plen, pdf, opt.low_frequency_cutoff) logging.info("Making colored noise") strain = pycbc.noise.noise_from_psd(tlen, 1.0/opt.sample_rate, strain_psd, seed=opt.fake_strain_seed) strain._epoch = lal.LIGOTimeGPS(opt.gps_start_time) if opt.injection_file: logging.info("Applying injections") injections = InjectionSet(opt.injection_file) injections.apply(strain, opt.channel_name[0:2], distance_scale=opt.injection_scale_factor) if opt.sgburst_injection_file: logging.info("Applying sine-Gaussian burst injections") injections = SGBurstInjectionSet(opt.sgburst_injection_file) injections.apply(strain, opt.channel_name[0:2], distance_scale=opt.injection_scale_factor) if precision == 'single': logging.info("Converting to float32") strain = (dyn_range_fac * strain).astype(float32) if opt.injection_file or opt.sgburst_injection_file: strain.injections = injections strain.gating_info = gating_info return strain
def from_cli(opt, dyn_range_fac=1, precision='single'): """Parses the CLI options related to strain data reading and conditioning. Parameters ---------- opt : object Result of parsing the CLI with OptionParser, or any object with the required attributes (gps-start-time, gps-end-time, strain-high-pass, pad-data, sample-rate, (frame-cache or frame-files), channel-name, fake-strain, fake-strain-seed, gating_file). dyn_range_fac: {float, 1}, optional A large constant to reduce the dynamic range of the strain. Returns ------- strain : TimeSeries The time series containing the conditioned strain data. """ gating_info = {} if opt.frame_cache or opt.frame_files or opt.frame_type: if opt.frame_cache: frame_source = opt.frame_cache if opt.frame_files: frame_source = opt.frame_files logging.info("Reading Frames") if opt.frame_type: strain = query_and_read_frame(opt.frame_type, opt.channel_name, start_time=opt.gps_start_time-opt.pad_data, end_time=opt.gps_end_time+opt.pad_data) else: strain = read_frame(frame_source, opt.channel_name, start_time=opt.gps_start_time-opt.pad_data, end_time=opt.gps_end_time+opt.pad_data) if opt.zpk_z and opt.zpk_p and opt.zpk_k: logging.info("Highpass Filtering") strain = highpass(strain, frequency=opt.strain_high_pass) logging.info("Applying zpk filter") z = numpy.array(opt.zpk_z) p = numpy.array(opt.zpk_p) k = float(opt.zpk_k) strain = filter_zpk(strain.astype(numpy.float64), z, p, k) if opt.normalize_strain: logging.info("Dividing strain by constant") l = opt.normalize_strain strain = strain / l if opt.injection_file: logging.info("Applying injections") injections = InjectionSet(opt.injection_file) injections.apply(strain, opt.channel_name[0:2], distance_scale=opt.injection_scale_factor) if opt.sgburst_injection_file: logging.info("Applying sine-Gaussian burst injections") injections = SGBurstInjectionSet(opt.sgburst_injection_file) injections.apply(strain, opt.channel_name[0:2], distance_scale=opt.injection_scale_factor) logging.info("Highpass Filtering") strain = highpass(strain, frequency=opt.strain_high_pass) if precision == 'single': logging.info("Converting to float32") strain = (strain * dyn_range_fac).astype(pycbc.types.float32) if opt.gating_file is not None: logging.info("Gating glitches") gate_params = numpy.loadtxt(opt.gating_file) if len(gate_params.shape) == 1: gate_params = [gate_params] strain = gate_data(strain, gate_params) gating_info['file'] = \ [gp for gp in gate_params \ if (gp[0] + gp[1] + gp[2] >= strain.start_time) \ and (gp[0] - gp[1] - gp[2] <= strain.end_time)] if opt.autogating_threshold is not None: # the + 0 is for making a copy glitch_times = detect_loud_glitches( strain + 0., threshold=opt.autogating_threshold, cluster_window=opt.autogating_cluster, low_freq_cutoff=opt.strain_high_pass, high_freq_cutoff=opt.sample_rate/2, corrupt_time=opt.pad_data+opt.autogating_pad) gate_params = [[gt, opt.autogating_width, opt.autogating_taper] \ for gt in glitch_times] if len(glitch_times) > 0: logging.info('Autogating at %s', ', '.join(['%.3f' % gt for gt in glitch_times])) strain = gate_data(strain, gate_params) gating_info['auto'] = gate_params logging.info("Resampling data") strain = resample_to_delta_t(strain, 1.0/opt.sample_rate, method='ldas') logging.info("Highpass Filtering") strain = highpass(strain, frequency=opt.strain_high_pass) logging.info("Remove Padding") start = opt.pad_data*opt.sample_rate end = len(strain)-opt.sample_rate*opt.pad_data strain = strain[start:end] if opt.fake_strain: logging.info("Generating Fake Strain") duration = opt.gps_end_time - opt.gps_start_time tlen = duration * opt.sample_rate pdf = 1.0/128 plen = int(opt.sample_rate / pdf) / 2 + 1 logging.info("Making PSD for strain") strain_psd = pycbc.psd.from_string(opt.fake_strain, plen, pdf, opt.low_frequency_cutoff) logging.info("Making colored noise") strain = pycbc.noise.noise_from_psd(tlen, 1.0/opt.sample_rate, strain_psd, seed=opt.fake_strain_seed) strain._epoch = lal.LIGOTimeGPS(opt.gps_start_time) if opt.injection_file: logging.info("Applying injections") injections = InjectionSet(opt.injection_file) injections.apply(strain, opt.channel_name[0:2], distance_scale=opt.injection_scale_factor) if opt.sgburst_injection_file: logging.info("Applying sine-Gaussian burst injections") injections = SGBurstInjectionSet(opt.sgburst_injection_file) injections.apply(strain, opt.channel_name[0:2], distance_scale=opt.injection_scale_factor) if precision == 'single': logging.info("Converting to float32") strain = (dyn_range_fac * strain).astype(pycbc.types.float32) if opt.injection_file or opt.sgburst_injection_file: strain.injections = injections if opt.taper_data: logging.info("Tapering data") # Use auto-gating stuff for this, a one-sided gate is a taper pd_taper_window = opt.taper_data gate_params = [(strain.start_time, 0., pd_taper_window)] gate_params.append( (strain.end_time, 0., pd_taper_window) ) gate_data(strain, gate_params) strain.gating_info = gating_info return strain
def from_cli(opt, dyn_range_fac=1, precision='single'): """Parses the CLI options related to strain data reading and conditioning. Parameters ---------- opt : object Result of parsing the CLI with OptionParser, or any object with the required attributes (gps-start-time, gps-end-time, strain-high-pass, pad-data, sample-rate, (frame-cache or frame-files), channel-name, fake-strain, fake-strain-seed, gating_file). dyn_range_fac: {float, 1}, optional A large constant to reduce the dynamic range of the strain. Returns ------- strain : TimeSeries The time series containing the conditioned strain data. """ if opt.frame_cache or opt.frame_files or opt.frame_type: if opt.frame_cache: frame_source = opt.frame_cache if opt.frame_files: frame_source = opt.frame_files logging.info("Reading Frames") if opt.frame_type: strain = query_and_read_frame(opt.frame_type, opt.channel_name, start_time=opt.gps_start_time-opt.pad_data, end_time=opt.gps_end_time+opt.pad_data) else: strain = read_frame(frame_source, opt.channel_name, start_time=opt.gps_start_time-opt.pad_data, end_time=opt.gps_end_time+opt.pad_data) if opt.zpk_z and opt.zpk_p and opt.zpk_k: logging.info("Highpass Filtering") strain = highpass(strain, frequency=opt.strain_high_pass) logging.info("Applying zpk filter") z = numpy.array(opt.zpk_z) p = numpy.array(opt.zpk_p) k = float(opt.zpk_k) strain = filter_zpk(strain.astype(numpy.float64), z, p, k) if opt.normalize_strain: logging.info("Dividing strain by constant") l = opt.normalize_strain strain = strain / l if opt.injection_file: logging.info("Applying injections") injections = InjectionSet(opt.injection_file) injections.apply(strain, opt.channel_name[0:2]) if opt.sgburst_injection_file: logging.info("Applying sine-Gaussian burst injections") injections = SGBurstInjectionSet(opt.sgburst_injection_file) injections.apply(strain, opt.channel_name[0:2]) logging.info("Highpass Filtering") strain = highpass(strain, frequency=opt.strain_high_pass) if precision == 'single': logging.info("Converting to float32") strain = (strain * dyn_range_fac).astype(float32) if opt.gating_file is not None: logging.info("Gating glitches") gate_params = numpy.loadtxt(opt.gating_file) if len(gate_params.shape) == 1: gate_params = [gate_params] strain = gate_data( strain, gate_params, data_start_time=(opt.gps_start_time - opt.pad_data)) logging.info("Resampling data") strain = resample_to_delta_t(strain, 1.0/opt.sample_rate, method='ldas') logging.info("Highpass Filtering") strain = highpass(strain, frequency=opt.strain_high_pass) logging.info("Remove Padding") start = opt.pad_data*opt.sample_rate end = len(strain)-opt.sample_rate*opt.pad_data strain = strain[start:end] if opt.fake_strain: logging.info("Generating Fake Strain") duration = opt.gps_end_time - opt.gps_start_time tlen = duration * opt.sample_rate pdf = 1.0/128 plen = int(opt.sample_rate / pdf) / 2 + 1 logging.info("Making PSD for strain") strain_psd = psd.from_string(opt.fake_strain, plen, pdf, opt.low_frequency_cutoff) logging.info("Making colored noise") strain = pycbc.noise.noise_from_psd(tlen, 1.0/opt.sample_rate, strain_psd, seed=opt.fake_strain_seed) strain._epoch = lal.LIGOTimeGPS(opt.gps_start_time) if opt.injection_file: logging.info("Applying injections") injections = InjectionSet(opt.injection_file) injections.apply(strain, opt.channel_name[0:2]) if opt.sgburst_injection_file: logging.info("Applying sine-Gaussian burst injections") injections = SGBurstInjectionSet(opt.sgburst_injection_file) injections.apply(strain, opt.channel_name[0:2]) if precision == 'single': logging.info("Converting to float32") strain = (dyn_range_fac * strain).astype(float32) if opt.injection_file: strain.injections = injections return strain