def check_state_vec(state_channel, gpsb, gpse, threshold, condition='greater_equal'): if condition == 'greater_equal': cond_function = np.greater_equal else: if condition == 'equal': cond_function = np.equal else: if condition == 'greater': cond_function = np.greater else: if condition == 'less_equal': cond_function = np.less_equal else: if condition == 'less': cond_function = np.less else: raise ValueError('Condition must be either ' '"greater_equal", "greater",' '"equal", "less_equal" or "less"') print "Acquiring state vector" with vrg.getChannel(state_channel, gpsb, gpse - gpsb) as sttvct: print "Evaluating state vector and generating state flag" sttflag = (cond_function(sttvct.data, threshold)).astype('int') # finding the points where the flag changes state u = np.diff(np.concatenate(([0], sttflag, [0]))) ups = np.where(u == 1, u, 0) # shifting the ones (raising state) by one step, # removing length 1 segments u = (u - ups + np.roll(ups, 1))[1:] starts = np.argwhere(u == 1) stops = np.argwhere(u == -1) segments = np.hstack((starts, stops))/sttvct.fsample + gpsb print len(segments) return segments
def process_channel(ch_p, src, segments): step_instances = [] for step in ch_p.steps.itervalues(): step_instances.append(step['class'](step)) brms_buffer = [] for (j, (gpsb, gpse)) in enumerate(segments): with vrg.getChannel(src, ch_p.channel, gpsb, gpse - gpsb) as r_data: # Decimate the channel data ch_data = decimator_wrapper(r_data, ch_p.ds_freq) fft_per_segment = ch_p.compute_n_fft(len(ch_data)) # Estimate the time vector of the brms samples for this seg t_disc = ch_p.compute_time_vector(fft_per_segment, gpsb) # Reset the buffers if there is a time discontinuity between # this segment and the previous one. if t_disc: for step in step_instances: step.reset() # make a function with the spectra as output? # il ciclo su fft_per segment lo terrei cmq qui. o no? for i in xrange(fft_per_segment): freqs, s = sig.welch( ch_data[i * ch_p.n_over_points:i * ch_p.n_over_points + ch_p.n_points], fs=ch_p.ds_freq, window='hanning', nperseg=ch_p.n_points) pipe = (freqs, s) for step in step_instances: print "Processing step {} for channel {}".format( step, ch_p.channel) pipe = step(pipe) # todo:use an hdf5 buffer between segments?probably unnecessary brms_buffer.append(pipe[1]) real_bands = pipe[0] return brms_buffer, real_bands
def process_channel(ch_p, src, segments): step_instances = [] for step in ch_p.steps.itervalues(): step_instances.append(step['class'](step)) brms_buffer = [] step_times = np.zeros(len(step_instances)) fft_time = 0 append_time = 0 for (j, (gpsb, gpse)) in enumerate(segments): print 'segment {} of {}'.format(j, len(segments)) with vrg.getChannel(src, ch_p.channel, gpsb, gpse - gpsb) as r_data: # Decimate the channel data ch_data = decimator_wrapper(ch_p.ds_freq, r_data) fft_per_segment = ch_p.compute_n_fft(len(ch_data)) print fft_per_segment # Estimate the time vector of the brms samples for this seg t_disc = ch_p.compute_time_vector(fft_per_segment, gpsb) # Reset the buffers if there is a time discontinuity between # this segment and the previous one. if t_disc: for step in step_instances: step.reset() # make a function with the spectra as output? # il ciclo su fft_per segment lo terrei cmq qui. o no? for i in xrange(fft_per_segment): if i % 100 == 1: print "FFt number {} of segment {} of {}".format( i, j, len(segments)) tot_time = fft_time + append_time + np.sum(step_times) perc_time = float(tot_time) / 100 print "FFT time: {:.2f}%, step time = {:.2f}%, append time" \ " = {:.2f}%".format(fft_time/perc_time, np.sum(step_times)/perc_time, append_time/perc_time) t_0 = time() freqs, s = sig.welch( ch_data[i * ch_p.n_over_points:i * ch_p.n_over_points + ch_p.n_points], fs=ch_p.ds_freq, window='hanning', nperseg=ch_p.n_points) fft_time += time() - t_0 pipe = (freqs, s) for k, step in enumerate(step_instances): if i % 100 == 1: print step print "step fraction time:{:.2f}".format( float(step_times[k] * 100) / np.sum(step_times)) t_0 = time() pipe = step(pipe) step_times[k] += time() - t_0 # todo:use an hdf5 buffer between segments?probably unnecessary t_0 = time() brms_buffer.append(pipe[1]) real_bands = pipe[0] append_time += time() - t_0 # transpose the buffer in order for it to be used brms_buffer = np.array(brms_buffer).T.tolist() return brms_buffer, real_bands
def getRawData(ch, gps, dt): """Read data from RAW file: ch = channel name gps = gps time dt = duration """ with getChannel('raw', ch, gps, dt) as x: return x.data, x.fsample
def read_virgo_timeseries(source, channel, gstart, gstop_or_dur): """quick and dirty function to read virgo data as timeseries""" # TODO use gwpy from virgotools import getChannel with getChannel(source, channel, gstart, gstop_or_dur) as data: return TimeSeries(data.data, unit=data.unit, t0=gstart, dt=data.dt, channel=channel)
def get_spectrum(gps_start, duration): chan = 'V1:Hrec_hoft_16384Hz' x = getChannel('raw', chan, gps_start, duration) Fs = x.fsample # Define parameters for FFT stride = 5.0 # FFT stride in seconds overlap = stride / 2 # overlap in seconds (50%) Pxx, freq, t = specgram(x.data, NFFT=int(stride * Fs), Fs=int(Fs), noverlap=int(overlap * Fs)) Axx = sqrt(Pxx) ASD = median(Axx, 1) / sqrt(log(2)) return ASD, freq
'figure.figsize': fig_size, 'font.family': "serif", 'font.serif': ["Times"] }) #, # 'xtick.major.pad':'8'}) # Define parameters for FFT stride = 6.0 # FFT stride in seconds overlap = 3.0 # overlap in seconds (50%) #### Grab Hrec data gps_start = 1180111697 duration = 100 chan = 'V1:Hrec_hoft_16384Hz' x = getChannel('raw', chan, gps_start, duration) Fs = x.fsample # for science segment, get PSD. # 6-second FFTs, 50% overlap, hann window (default) Pxx, V1freq, t = specgram(x.data, NFFT=int(stride * Fs), Fs=int(Fs), noverlap=int(overlap * Fs)) Axx = sqrt(Pxx) V1_ASD_NIWI = median(Axx, 1) / sqrt(log(2)) #### Grab Hrec data gps_start = 1179865427 duration = 100
err_chan = 'V1:LSC_DARM_ERR' # grab data in steps, 50% overlap step_duration = 50 t_DARM = arange(0, duration, step_duration / 2) datapoints = zeros(len(t_DARM)) demod_I = zeros(len(t_DARM)) demod_Q = zeros(len(t_DARM)) for i in range(len(t_DARM)): gps_start = start_time + i * step_duration / 2 x = getChannel('raw', err_chan, gps_start, step_duration) Fs = x.fsample ts = arange(0, duration, 1. / Fs) pre_line, t_dec = data_demod(ts, x.data, f0, Fs, step_duration, lp=2500.0, Fs0=10000.0) datapoints[i] = median(pre_line) print i, gps_start, datapoints[i] fignum = 0
def read_virgo_timeseries(source, channel, t0, gstop_or_dur, mask=False, fill_value=np.nan, remote=False): """Function to read virgo data as timeseries. This should one day be included in gwpy. Parameters ---------- source : `str` Frame file, either a full path to a ffl or gwf file, or an abbreviation like 'raw', 'trend', which are looked up in the stancard location. If omitted, defaults to 'raw', but this default value is deprecated. channel : `str` Source datastream for these data. If missing, a prefix 'V1:' is added. t0 : `~gwpy.time.LIGOTimeGPS`, `float`, `str` GPS epoch corresponding to starting time, any input parsable by `~gwpy.time.to_gps` is fine gstop_or_dur: `~gwpy.time.LIGOTimeGPS`, `float`, `str` GPS epoch corresponding to end time, any input parsable by `~gwpy.time.to_gps` is fine If a `float` < 1e6 is provided, it corresponds to a duration in seconds from `t0`. mask : `bool`, optional If mask is False, missing samples will be replaced by fill_value. If it is True, the returned FrVect will have an attribute missing, which is a mask vector that is zero for missing samples. Default : False fill_value : `float`, optional Value that is used for missing samples if mask is False. Default: np.nan remote: `bool` optional If False, use PythonVirgoTools to parse raw data files. If True, use gwpy.TimeSeries.get(), but takes longer. Default : False, assuming the script is ran on Virgo server. Examples -------- >>> from virgotools import getChannel Load the data from channel 'INJ_IMC_TRA_DC', from Sep 14 2015 09:50:45.391, and a duration of 10s >>> x = getChannel('raw', 'INJ_IMC_TRA_DC', 'Sep 14 2015 09:50:45.391', 10) That can be simply visualise: >>> import matplotlib.pyplot as plt >>> plt.plot(x.time, x.data) >>> plt.show() Same, using 2 GPS times: >>> x = getChannel('raw', 'INJ_IMC_TRA_DC', 1126259462.3910, 1126259472.3910) """ # Convert to gps times in seconds # Use the Seconds and NanoSeconds instead of ns() # Because otherwise one needs to multiply by 1e-9 # And this can cause rounding approximation sec = to_gps(t0).gpsSeconds nsec = to_gps(t0).gpsNanoSeconds tstart = str(sec) + '.' + str(nsec) gstart = float(tstart) sec = to_gps(gstop_or_dur).gpsSeconds nsec = to_gps(gstop_or_dur).gpsNanoSeconds tend = str(sec) + '.' + str(nsec) if float(tend) < 1e6: gstop = gstart + float(tend) else: gstop = float(tend) # If the script is running on Virgo's server. if not remote: from virgotools import getChannel # Parse Virgo files with getChannel(source, channel, gstart, gstop, mask=mask, fill_value=fill_value) as data: data = TimeSeries(data.data, unit=data.unit, t0=gstart, dt=data.dt, channel=channel) else: # If not running the script on Virgo's server. Takes longer # Query is working, but crashes when computing q_transform. # Data might not be the same format as with PythonVirgoTools. # Further checks required. if channel[:3] not in ['V1:', 'H1:', 'L1:']: print( 'When accessing the data outside the virgo server, the channel must start with `V1:`, `H1:` or `L1:` ' ) data = TimeSeries.get(channel, gstart, gstop) return data
PRCL_phi = array([]) SSFS_phi = array([]) PRCL_UGF = array([]) SSFS_UGF = array([]) DARM_UGF = array([]) MICH_UGF = array([]) B4_112mag = array([]) for i in range(len(seg_stop)): if duration[i] < 100: continue print i, seg_start[i], seg_stop[i], duration[i] chan = 'LSC_B2_8MHz_DPHI' x = getChannel('rds',chan,seg_start[i],duration[i]-10) PRCL_phi = append(PRCL_phi,x.data) chan = 'LSC_B4_56MHz_DPHI' x = getChannel('rds',chan,seg_start[i],duration[i]-10) SSFS_phi = append(SSFS_phi,x.data) chan = 'LSC_DARM_UGF' x = getChannel('rds',chan,seg_start[i],duration[i]-10) DARM_UGF = append(DARM_UGF,x.data) chan = 'LSC_SSFS_UGF' x = getChannel('rds',chan,seg_start[i],duration[i]-10) SSFS_UGF = append(SSFS_UGF,x.data) chan = 'LSC_MICH_UGF'
def get_channel_data(self, data_source, channel, gpsb, gpse): # TODO: extract other stuff from ch i.e. the units? with vrg.getChannel(data_source, channel, gpsb, gpse - gpsb) as ch: data = decimator_wrapper(self.down_freq, ch) return data
# # Find segments and save seglist # ################################################ # C8 start and end times gps_start = 1178028018 # prior to 17:00 May 5 there were some times when DQ_META_ITF_Mode was not defined, start at 17:00 #gps_start = 1178038817 gps_stop = 1178262018 duration = gps_stop - gps_start chan = 'DQ_META_ITF_Mode' x = getChannel('rds', chan, gps_start, duration) Fs = x.fsample t = arange(0, duration, 1. / Fs) + gps_start seg_start = [] seg_stop = [] seg_flag = False for i in range(len(x.data)): # If a segment has just started if x.data[i] == 1 and seg_flag == False: seg_start.append(t[i]) seg_flag = True