Ejemplo n.º 1
0
def get_swstat_bits(frame_filenames, swstat_channel_name, start_time,
                    end_time):
    ''' This function just checks the first time in the SWSTAT channel
    to see if the filter was on, it doesn't check times beyond that.

    This is just for a first test on a small chunck of data.

    To read the SWSTAT bits, reference: https://dcc.ligo.org/DocDB/0107/T1300711/001/LIGO-T1300711-v1.pdf

    Bit 0-9 = Filter on/off switches for the 10 filters in an SFM.
    Bit 10 = Filter module input switch on/off
    Bit 11 = Filter module offset switch on/off
    Bit 12 = Filter module output switch on/off
    Bit 13 = Filter module limit switch on/off 
    Bit 14 = Filter module history reset momentary switch
    '''

    # read frames
    swstat = frame.read_frame(frame_filenames,
                              swstat_channel_name,
                              start_time=start_time,
                              end_time=end_time)

    # convert number in channel to binary
    bits = bin(int(swstat[0]))

    # check if filterbank input or output was off
    filterbank_off = False
    if len(bits) < 14 or int(bits[-13]) == 0 or int(bits[-11]) == 0:
        filterbank_off = True

    return bits[-10:], filterbank_off
Ejemplo n.º 2
0
    def strain(self, ifo, duration=32, sample_rate=4096):
        """ Return strain around the event

        Currently this will return the strain around the event in the smallest
        format available. Selection of other data is not yet available.

        Parameters
        ----------
        ifo: str
            The name of the observatory you want strain for. Ex. H1, L1, V1

        Returns
        -------
        strain: pycbc.types.TimeSeries
            Strain around the event.
        """
        from astropy.utils.data import download_file
        from pycbc.frame import read_frame

        if sample_rate == 4096:
            sampling = "4KHz"
        elif sample_rate == 16384:
            sampling = "16KHz"

        channel = "{}:GWOSC-{}_R1_STRAIN".format(ifo, sampling.upper())

        for fdict in self.data['strain']:
            if (fdict['detector'] == ifo and fdict['duration'] == duration and
                    fdict['sampling_rate'] == sample_rate and
                    fdict['format'] == 'gwf'):
                url = fdict['url']
                break

        filename = download_file(url, cache=True)
        return read_frame(str(filename), str(channel))
Ejemplo n.º 3
0
def get_noise(detector):

    from pycbc import frame
    import random as randi
    import os



    h1_list = ['H-H1_LOSC_16_V1-1126084608-4096.gwf', 'H-H1_LOSC_16_V1-1126109184-4096.gwf',
               'H-H1_LOSC_16_V1-1126264832-4096.gwf', 'H-H1_LOSC_16_V1-1126293504-4096.gwf',
               'H-H1_LOSC_16_V1-1127194624-4096.gwf',
               'H-H1_LOSC_16_V1-1128042496-4096.gwf', 'H-H1_LOSC_16_V1-1128345600-4096.gwf',
               'H-H1_LOSC_16_V1-1128407040-4096.gwf', 'H-H1_LOSC_16_V1-1128624128-4096.gwf',
               'H-H1_LOSC_16_V1-1128665088-4096.gwf',
               'H-H1_LOSC_16_V1-1129127936-4096.gwf', 'H-H1_LOSC_16_V1-1129664512-4096.gwf']
    h1_begin = [1126084608, 1126109184, 1126264832, 1126293504, 1127194624, 1128042496, 1128345600, 1128407040,
                1128624128, 1128665088, 1129127936, 1129664512]



    if detector == 'O1-H':

        file = 'H-H1_LOSC_16_V1-1126084608-4096.gwf'

        strain = frame.read_frame(file, 'H1:GWOSC-16KHZ_R1_STRAIN',1126084608,1126084608+32)




    return strain
Ejemplo n.º 4
0
    def strain(self, ifo):
        """ Return strain around the event

        Currently this will return the strain around the event in the smallest
        format available. Selection of other data is not yet available.

        Parameters
        ----------
        ifo: str
            The name of the observatory you want strain for. Ex. H1, L1, V1

        Returns
        -------
        strain: pycbc.types.TimeSeries
            Strain around the event.
        """
        import tempfile, requests, shutil
        from pycbc.frame import read_frame

        channel = '%s:LOSC-STRAIN' % ifo
        url = self.data['frames'][ifo]
        f = tempfile.NamedTemporaryFile(suffix='.gwf')
        r = requests.get(url, stream=True)

        if r.status_code != 200:
            raise ValueError("Could not download file, %s", r.status_code)

        shutil.copyfileobj(r.raw, f)
        f.flush()
        return read_frame(f.name, channel)
Ejemplo n.º 5
0
    def loadts(self, inj):
        """Loads an injection time series.

        After the first time a time series is loaded it will be added to an
        internal buffer for faster in case another injection uses the same
        series.
        """
        if self._buffer is None:
            # create the buffer
            self._buffer = LimitedSizeDict(size_limit=self._buffersize)
        try:
            return self._buffer[inj.filename]
        except KeyError:
            pass
        # not in buffer, so load
        if inj.filename.endswith('.gwf'):
            try:
                channel = inj.channel
            except AttributeError as _err:
                # Py3.XX: uncomment the "from _err" when we drop 2.7
                raise ValueError("Must provide a channel for "
                                 "frame files") #from _err
            ts = frame.read_frame(inj.filename, channel)
        else:
            ts = load_timeseries(inj.filename)
        # cache
        self._buffer[inj.filename] = ts
        return ts
Ejemplo n.º 6
0
    def strain(self, ifo):
        """ Return strain around the event

        Currently this will return the strain around the event in the smallest
        format available. Selection of other data is not yet available.

        Parameters
        ----------
        ifo: str
            The name of the observatory you want strain for. Ex. H1, L1, V1

        Returns
        -------
        strain: pycbc.types.TimeSeries
            Strain around the event.
        """
        import tempfile, requests, shutil
        from pycbc.frame import read_frame
        
        channel = '%s:LOSC-STRAIN' % ifo
        url = self.data['frames'][ifo]
        f = tempfile.NamedTemporaryFile(suffix='.gwf')
        r = requests.get(url, stream=True)

        if r.status_code != 200:
            raise ValueError("Could not download file, %s", r.status_code)

        shutil.copyfileobj(r.raw, f)
        f.flush()
        return read_frame(f.name, channel)
Ejemplo n.º 7
0
    def strain(self, ifo, duration=32, sample_rate=4096):
        """ Return strain around the event

        Currently this will return the strain around the event in the smallest
        format available. Selection of other data is not yet available.

        Parameters
        ----------
        ifo: str
            The name of the observatory you want strain for. Ex. H1, L1, V1

        Returns
        -------
        strain: pycbc.types.TimeSeries
            Strain around the event.
        """
        from astropy.utils.data import download_file
        from pycbc.frame import read_frame

        # Information is currently wrong on GWOSC!
        # channels = self.data['files']['FrameChannels']
        # for channel in channels:
        #    if ifo in channel:
        #        break

        length = "{}sec".format(duration)
        if sample_rate == 4096:
            sampling = "4KHz"
        elif sample_rate == 16384:
            sampling = "16KHz"

        channel = "{}:GWOSC-{}_R1_STRAIN".format(ifo, sampling.upper())
        url = self.data['files'][ifo][length][sampling]['GWF']
        filename = download_file(url, cache=True)
        return read_frame(str(filename), str(channel))
Ejemplo n.º 8
0
def get_swstat_bits(frame_filenames, swstat_channel_name, start_time, end_time):
    ''' This function just checks the first time in the SWSTAT channel
    to see if the filter was on, it doesn't check times beyond that.

    This is just for a first test on a small chunck of data.

    To read the SWSTAT bits, reference: https://dcc.ligo.org/DocDB/0107/T1300711/001/LIGO-T1300711-v1.pdf

    Bit 0-9 = Filter on/off switches for the 10 filters in an SFM.
    Bit 10 = Filter module input switch on/off
    Bit 11 = Filter module offset switch on/off
    Bit 12 = Filter module output switch on/off
    Bit 13 = Filter module limit switch on/off 
    Bit 14 = Filter module history reset momentary switch
    '''

    # read frames
    swstat = frame.read_frame(frame_filenames, swstat_channel_name,
                      start_time=start_time, end_time=end_time)

    # convert number in channel to binary
    bits = bin(int(swstat[0]))

    # check if filterbank input or output was off
    filterbank_off = False
    if len(bits) < 14 or int(bits[-13]) == 0 or int(bits[-11]) == 0:
        filterbank_off = True

    return bits[-10:], filterbank_off
Ejemplo n.º 9
0
    def strain(self, ifo, duration=32, sample_rate=4096):
        """ Return strain around the event

        Currently this will return the strain around the event in the smallest
        format available. Selection of other data is not yet available.

        Parameters
        ----------
        ifo: str
            The name of the observatory you want strain for. Ex. H1, L1, V1

        Returns
        -------
        strain: pycbc.types.TimeSeries
            Strain around the event.
        """
        from astropy.utils.data import download_file
        from pycbc.frame import read_frame

        # Information is currently wrong on GWOSC!
        # channels = self.data['files']['FrameChannels']
        # for channel in channels:
        #    if ifo in channel:
        #        break

        length = "{}sec".format(duration)
        if sample_rate == 4096:
            sampling = "4KHz"
        elif sample_rate == 16384:
            sampling = "16KHz"

        channel = "{}:GWOSC-{}_R1_STRAIN".format(ifo, sampling.upper())
        url = self.data['files'][ifo][length][sampling]['GWF']
        filename = download_file(url, cache=True)
        return read_frame(str(filename), str(channel))
Ejemplo n.º 10
0
    def setUp(self):
        ###### Get data for references analysis of 170817
        m = Merger("GW170817")
        ifos = ['H1', 'V1', 'L1']
        self.psds = {}
        self.data = {}

        for ifo in ifos:
            print("Processing {} data".format(ifo))

            # Download the gravitational wave data for GW170817
            url = "https://dcc.ligo.org/public/0146/P1700349/001/"
            url += "{}-{}1_LOSC_CLN_4_V1-1187007040-2048.gwf"
            fname = download_file(url.format(ifo[0], ifo[0]), cache=True)
            ts = read_frame(fname,
                            "{}:LOSC-STRAIN".format(ifo),
                            start_time=int(m.time - 260),
                            end_time=int(m.time + 40))
            ts = highpass(ts, 15.0)
            ts = resample_to_delta_t(ts, 1.0 / 2048)
            ts = ts.time_slice(m.time - 112, m.time + 16)
            self.data[ifo] = ts.to_frequencyseries()

            psd = interpolate(ts.psd(4), ts.delta_f)
            psd = inverse_spectrum_truncation(psd,
                                              int(4 * psd.sample_rate),
                                              trunc_method='hann',
                                              low_frequency_cutoff=20.0)
            self.psds[ifo] = psd

        self.static = {
            'mass1': 1.3757,
            'mass2': 1.3757,
            'f_lower': 20.0,
            'approximant': "TaylorF2",
            'polarization': 0,
            'ra': 3.44615914,
            'dec': -0.40808407,
            'tc': 1187008882.42840,
        }
        self.variable = (
            'distance',
            'inclination',
        )
        self.flow = {'H1': 25, 'L1': 25, 'V1': 25}
        inclination_prior = SinAngle(inclination=None)
        distance_prior = Uniform(distance=(10, 100))
        tc_prior = Uniform(tc=(m.time - 0.1, m.time + 0.1))
        self.prior = JointDistribution(self.variable, inclination_prior,
                                       distance_prior)

        ###### Expected answers
        # Answer taken from marginalized gaussian model
        self.q1 = {'distance': 42.0, 'inclination': 2.5}
        self.a1 = 541.8235746138382

        # answer taken from brute marginize pol + phase
        self.a2 = 542.581
        self.pol_samples = 200
Ejemplo n.º 11
0
def read_gain_from_frames(frame_filenames, gain_channel_name, start_time, end_time):
    '''
    Returns the gain from the file.
    '''

    # get timeseries from frame
    gain = frame.read_frame(frame_filenames, gain_channel_name,
                      start_time=start_time, end_time=end_time)

    return gain[0]
Ejemplo n.º 12
0
def read_gain_from_frames(frame_filenames, gain_channel_name, start_time, end_time):
    '''
    Returns the gain from the file.
    '''

    # get timeseries from frame
    gain = frame.read_frame(frame_filenames, gain_channel_name,
                      start_time=start_time, end_time=end_time)

    return gain[0]
Ejemplo n.º 13
0
def get_GWSC_Full_strain(pth, tstart, tend):
    """This function, require the strain .gwf name, without the detector definition 
    (H-H1,L-L1), and loads the entire frame file downloaded from GW OSC,
    applies a high-pass filter at 15 Hz, then return the whole data without cutting that
    """
    strain = {}
    for ifo in ifos:
        fname = '%s-%s_%s' % (ifo[0], ifo, pth)
        channel_name = '%s:GWOSC-4KHZ_R1_STRAIN' % ifo
        strain[ifo] = read_frame(fname, channel_name, tstart, tend)
        strain[ifo] = highpass(strain[ifo], 15.0)
    return strain
Ejemplo n.º 14
0
def get_strain_from_gwf_files(
    gwf_files: Dict[str, List[Union[str, bytes, os.PathLike]]],
    gps_start: int,
    window: int,
    original_sampling_rate: int = 4096,
    target_sampling_rate: int = 4096,
    as_pycbc_timeseries: bool = True,
    channel: str = 'GDS-CALIB_STRAIN',
    check_integrity: bool = True,
):
    assert isinstance(gps_start, int), 'time is not an int'
    assert isinstance(window, int), 'interval_width is not an int'
    assert isinstance(original_sampling_rate,
                      int), 'original_sampling_rate is not an int'
    assert isinstance(target_sampling_rate,
                      int), 'target_sampling_rate is not an int'
    assert (original_sampling_rate % target_sampling_rate) == 0, (
        'Invalid target_sampling_rate: Not a divisor of original_sampling_rate!'
    )

    sampling_factor = int(original_sampling_rate / target_sampling_rate)
    samples = defaultdict(list)

    for ifo in gwf_files:
        detector_channel = f'{ifo}:{channel}'
        for file_path in gwf_files[ifo]:
            strain = read_frame(
                str(file_path),
                detector_channel,
                start_time=gps_start,
                end_time=gps_start + window,
                check_integrity=check_integrity,
            )

            samples[ifo].append(strain[::sampling_factor])

        samples[ifo] = np.ascontiguousarray(np.concatenate(samples[ifo]))

    if not as_pycbc_timeseries:
        return samples

    else:
        # Convert strain of both detectors to a TimeSeries object
        timeseries = {
            ifo: TimeSeries(initial_array=samples[ifo],
                            delta_t=1.0 / target_sampling_rate,
                            epoch=LIGOTimeGPS(gps_start))
            for ifo in samples
        }

        return timeseries
Ejemplo n.º 15
0
def get_raw_strain(window=512):
    """This function loads the entire frame file downloaded from GW OSC,
    applies a high-pass filter at 15 Hz, then keeps the data around
    ``event_time`` +/-``window``. The ``event_time`` is from above; the
    default window is 512 (s).
    """
    strain = {}
    for ifo in ifos:
        fname = '%s-%s_LOSC_4_V2-1126257414-4096.gwf' % (ifo[0], ifo)
        channel_name = '%s:LOSC-STRAIN' % ifo
        strain[ifo] = read_frame(fname, channel_name)
        strain[ifo] = highpass(strain[ifo], 15.0)
        strain[ifo] = strain[ifo].time_slice(event_time-window/2, event_time+window/2)
    return strain
Ejemplo n.º 16
0
def get_LSC_NoNan_strain(pth, tev, tstart, tend, window):
    """This function, require the strain .gwf name, without the detector definition 
    (H-H1,L-L1), and loads the entire frame file downloaded from GW OSC,
    applies a high-pass filter at 15 Hz, then keeps the data around
    ``event_time`` +/-``window``. The ``event_time`` is from above; the
    default window is 512 (s).
    """
    strain = {}
    for ifo in ifos:
        fname = '%s-%s_%s' % (ifo[0], ifo, pth)
        channel_name = '%s:LOSC-STRAIN' % ifo
        strain[ifo] = read_frame(fname, channel_name, tstart, tend)
        strain[ifo] = highpass(strain[ifo], 15.0)
        strain[ifo] = strain[ifo].time_slice(tev - window / 2,
                                             tev + window / 2)
    return strain
Ejemplo n.º 17
0
def get_raw_strain(window=198):
    """This function loads the entire frame file downloaded from GW OSC,
    applies a high-pass filter at 15 Hz, then keeps the data around
    ``event_time`` +/-``window``. The ``event_time`` is from above; the
    default window is 512 (s).
    """
    #Here I have changed: _LOSC_4_V2-1126257414-4096.gwf to _GWOSC_4KHZ_R1-1135134303-4096.gwf (i.e. the data_file name)
    #LOSC-STRAIN was changed as well to GWOSC-4KHZ_R1_STRAIN
    strain = {}
    for ifo in ifos:
        fname = '%s-%s_GWOSC_4KHZ_R1-1135134303-4096.gwf' % (ifo[0], ifo)
        channel_name = '%s:GWOSC-4KHZ_R1_STRAIN' % ifo
        strain[ifo] = read_frame(fname, channel_name)
        strain[ifo] = strain[ifo].time_slice(event_time - window / 2,
                                             event_time + window / 2)
        #strain[ifo] = highpass(strain[ifo], 15.0)
    return strain
Ejemplo n.º 18
0
def read_frame_losc(channels, start_time, end_time):
    """ Read channels from losc data

    Parameters
    ----------
    channels: str or list
        The channel name to read or list of channel names.
    start_time: int
        The gps time in GPS seconds
    end_time: int
        The end time in GPS seconds
      
    Returns
    -------
    ts: TimeSeries
        Returns a timeseries or list of timeseries with the requested data.
    """
    import urllib
    from pycbc.frame import read_frame
    if not isinstance(channels, list):
        channels = [channels]
    ifos = [c[0:2] for c in channels]
    urls = {}
    for ifo in ifos:
        urls[ifo] = losc_frame_urls(ifo, start_time, end_time)
        if len(urls[ifo]) == 0:
            raise ValueError("No data found for %s so we "
                             "can't produce a time series" % ifo)

    fnames = {ifo: [] for ifo in ifos}
    for ifo in ifos:
        for url in urls[ifo]:
            fname, _ = urllib.urlretrieve(url)
            fnames[ifo].append(fname)

    ts = [
        read_frame(fnames[channel[0:2]],
                   channel,
                   start_time=start_time,
                   end_time=end_time) for channel in channels
    ]
    if len(ts) == 1:
        return ts[0]
    else:
        return ts
Ejemplo n.º 19
0
def plot_gwfs( frame_location1, frame_location2, label=['frame1','frame2'] ):
    
    #
    import matplotlib as mpl
    mpl.rc('font',family='Times New Roman')
    from matplotlib.pyplot import plot,xlabel,ylabel,figure,legend,xlim,ylim,title
    from pycbc import frame
    import numpy

    #
    frame_location = [frame_location1,frame_location2]
    #
    figure( figsize=0.8*numpy.array([16,4]) )

    clrspec = [ 'b', [0.8,0.8,0.8] ]
    widspec = [ 1, 3 ]
    for k in [1,0]:

        #
        data_L = frame.read_frame( frame_location[k], '%s1:HWINJ_INJECTED'%frame_location[k][0] )

        #
        trigger_time = data_L.end_time.gpsSeconds-10.0
        time = data_L.delta_t * (numpy.array(range(len(data_L.data)))-1.0) + data_L.start_time - trigger_time
        #channel_data_H = data_H.data.real
        channel_data_L = data_L.data.real

        #
        xlim( numpy.array([-0.18,0.18]) )
        #plot( time, channel_data_H, color=0.7*numpy.array([1,1,1]) )

        # plot( time, channel_data_L, color=0.8*numpy.array([numpy.sin(numpy.pi/(k+1)),1,1]), label=label[k] )
        plot( time, channel_data_L, color=clrspec[k], label=label[k], linewidth=widspec[k] )

        #plot( trigger_time * numpy.array([1,1]) , ylim(), color='r' )

    fs=22
    hfont = {'fontname':'serif'}
    title(frame_location[k][0]+'1')
    xlabel(r'$t$ (sec)', fontsize=fs)
    ylabel(r'$h(t)$', fontsize=fs)
    legend(frameon=False, fontsize=fs )
Ejemplo n.º 20
0
def read_frame_losc(channels, start_time, end_time):
    """ Read channels from losc data

    Parameters
    ----------
    channels: str or list
        The channel name to read or list of channel names.
    start_time: int
        The gps time in GPS seconds
    end_time: int
        The end time in GPS seconds

    Returns
    -------
    ts: TimeSeries
        Returns a timeseries or list of timeseries with the requested data.
    """
    import urllib
    from pycbc.frame import read_frame
    if not isinstance(channels, list):
        channels = [channels]
    ifos = [c[0:2] for c in channels]
    urls = {}
    for ifo in ifos:
        urls[ifo] = losc_frame_urls(ifo, start_time, end_time)
        if len(urls[ifo]) == 0:
            raise ValueError("No data found for %s so we "
                             "can't produce a time series" % ifo)

    fnames = {ifo:[] for ifo in ifos}
    for ifo in ifos:
        for url in urls[ifo]:
            fname, _ = urllib.urlretrieve(url)
            fnames[ifo].append(fname)

    ts = [read_frame(fnames[channel[0:2]], channel,
           start_time=start_time, end_time=end_time) for channel in channels]
    if len(ts) == 1:
        return ts[0]
    else:
        return ts
Ejemplo n.º 21
0
    def strain(self, ifo):
        """ Return strain around the event

        Currently this will return the strain around the event in the smallest
        format available. Selection of other data is not yet available.

        Parameters
        ----------
        ifo: str
            The name of the observatory you want strain for. Ex. H1, L1, V1

        Returns
        -------
        strain: pycbc.types.TimeSeries
            Strain around the event.
        """
        from astropy.utils.data import download_file
        from pycbc.frame import read_frame

        channel = '%s:LOSC-STRAIN' % ifo
        url = self.data['frames'][ifo]
        filename = download_file(url, cache=True)
        return read_frame(filename, channel)
Ejemplo n.º 22
0
    def strain(self, ifo, duration=32, sample_rate=4096):
        """ Return strain around the event

        Currently this will return the strain around the event in the smallest
        format available. Selection of other data is not yet available.

        Parameters
        ----------
        ifo: str
            The name of the observatory you want strain for. Ex. H1, L1, V1

        Returns
        -------
        strain: pycbc.types.TimeSeries
            Strain around the event.
        """
        from pycbc.io import get_file
        from pycbc.frame import read_frame

        for fdict in self.data['strain']:
            if (fdict['detector'] == ifo and fdict['duration'] == duration
                    and fdict['sampling_rate'] == sample_rate
                    and fdict['format'] == 'gwf'):
                url = fdict['url']
                break
        else:
            raise ValueError('no strain data is available as requested '
                             'for ' + self.common_name)

        ver = url.split('/')[-1].split('-')[1].split('_')[-1]
        sampling_map = {4096: "4KHZ", 16384: "16KHZ"}
        channel = "{}:GWOSC-{}_{}_STRAIN".format(ifo,
                                                 sampling_map[sample_rate],
                                                 ver)

        filename = get_file(url, cache=True)
        return read_frame(str(filename), str(channel))
Ejemplo n.º 23
0
from pycbc.frame import read_frame
from pycbc.filter import highpass_fir, matched_filter
from pycbc.waveform import get_fd_waveform
from pycbc.psd import welch, interpolate
import urllib

# Read data and remove low frequency content
fname = 'H-H1_LOSC_4_V2-1126259446-32.gwf'
url = "https://www.gw-openscience.org/GW150914data/" + fname
urllib.urlretrieve(url, filename=fname)
h1 = read_frame('H-H1_LOSC_4_V2-1126259446-32.gwf', 'H1:LOSC-STRAIN')
h1 = highpass_fir(h1, 15, 8)

# Calculate the noise spectrum
psd = interpolate(welch(h1), 1.0 / h1.duration)

# Generate a template to filter with
hp, hc = get_fd_waveform(approximant="IMRPhenomD",
                         mass1=40,
                         mass2=32,
                         f_lower=20,
                         delta_f=1.0 / h1.duration)
hp.resize(len(h1) / 2 + 1)

# Calculate the complex (two-phase SNR)
snr = matched_filter(hp, h1, psd=psd, low_frequency_cutoff=20.0)

# Remove regions corrupted by filter wraparound
snr = snr[len(snr) / 4:len(snr) * 3 / 4]

import pylab
Ejemplo n.º 24
0
from pycbc.frame import read_frame
from pycbc.filter import highpass_fir, matched_filter
from pycbc.waveform import get_fd_waveform 
from pycbc.psd import welch, interpolate
import urllib

# Read data and remove low frequency content
fname = 'H-H1_LOSC_4_V2-1126259446-32.gwf'
url = "https://losc.ligo.org/s/events/GW150914/" + fname
urllib.urlretrieve(url, filename=fname)
h1 = read_frame('H-H1_LOSC_4_V2-1126259446-32.gwf', 'H1:LOSC-STRAIN')
h1 = highpass_fir(h1, 15, 8)

# Calculate the noise spectrum
psd = interpolate(welch(h1), 1.0 / 32)

# Generate a template to filter with
hp, hc = get_fd_waveform(approximant="IMRPhenomD", mass1=40, mass2=32, 
                         f_lower=20, delta_f=1.0/32)
hp.resize(len(h1) / 2 + 1)

# Calculate the complex (two-phase SNR)
snr = matched_filter(hp, h1, psd=psd, low_frequency_cutoff=20.0)

# Remove regions corrupted by filter wraparound 
snr = snr[len(snr) / 4: len(snr) * 3 / 4]

import pylab
pylab.plot(snr.sample_times, abs(snr))
pylab.ylabel('signal-to-noise')
pylab.xlabel('GPS Time (s)')
Ejemplo n.º 25
0
from scipy import signal
import matplotlib.pyplot as plt
from pycbc.frame import read_frame

file_name = "H-H1_GWOSC_16KHZ_R1-1187008867-32.gwf"

merger = 1187008882.4

# LOSC bulk data typically uses the same convention for internal channels names
# Strain is typically IFO:LOSC-STRAIN, where IFO can be H1/L1/V1.
channel_name = "H1:GWOSC-16KHZ_R1_STRAIN"

hc = 500
lc = 30

ts = read_frame(file_name, channel_name, 1187008867, 1187008867 + 32)

start = ts.start_time

ts = ts.highpass_fir(lc, 512)
ts = ts.lowpass_fir(hc, 512)

strain = ts.data

psd = ts.psd(2)

whitened = ts.whiten(4, 4, low_frequency_cutoff=20)

fs = 10e3
N = 1e5
amp = 2 * np.sqrt(2)
Ejemplo n.º 26
0
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
Ejemplo n.º 27
0
import pylab
import numpy
from pycbc.frame import read_frame
from pycbc.types import load_timeseries

lal_out_file = "H1-INSPIRAL_lalsuite_FULL_DATA-968605000-2048.gwf"

# Take only the first 16 seconds to use as a small comparison
lal_raw = read_frame(lal_out_file, 'H1:LDAS-STRAIN_RAW', start_time=968605000, duration="16")
lal_resample = read_frame(lal_out_file, 'H1:LDAS-STRAIN_RAW_RESAMP', start_time=968605000, duration="16")
lal_conditioned = read_frame(lal_out_file, 'H1:LDAS-STRAIN_FILTER', start_time=968605000, duration="16")

# Take only the first 16 seconds to use as a small comparison
start_offset = 8 # This is the offest between the output that PyCBC gives and lalapps_inspiral 
                 # lalapps_inspiral does not output the 8 second padding in the frame file
pycbc_raw = load_timeseries("raw_pycbc.npy")[start_offset*16384:(start_offset+16)*16384].astype(lal_raw.dtype)
pycbc_resample = load_timeseries("resampled_pycbc.npy")[start_offset*4096:(start_offset+16)*4096].astype(lal_raw.dtype)

# the lalapps_inspiral gwf file misreportes the epoch for some of the streams
pycbc_conditioned = load_timeseries("conditioned_pycbc.npy")[start_offset*4096:(start_offset+16)*4096].astype(lal_raw.dtype)
pycbc_conditioned._epoch = lal_conditioned._epoch

print float(pycbc_conditioned.start_time)

def p(a, b, an, bn):
    t = numpy.arange(0, len(a), 1)
    mindif = 1e-7

    pylab.figure()
    pylab.scatter(t, a, label=an, color="red", marker='x')
    pylab.scatter(t, b, label=bn, color="blue", marker='+')
Ejemplo n.º 28
0
def get_noise(detector):

    import pycbc.frame as frame
    import random as randi
    import os

    h1_list = [
        'H-H1_LOSC_16_V1-1126084608-4096.gwf',
        'H-H1_LOSC_16_V1-1126109184-4096.gwf',
        'H-H1_LOSC_16_V1-1126264832-4096.gwf',
        'H-H1_LOSC_16_V1-1126293504-4096.gwf',
        'H-H1_LOSC_16_V1-1127194624-4096.gwf',
        'H-H1_LOSC_16_V1-1128042496-4096.gwf',
        'H-H1_LOSC_16_V1-1128345600-4096.gwf',
        'H-H1_LOSC_16_V1-1128407040-4096.gwf',
        'H-H1_LOSC_16_V1-1128624128-4096.gwf',
        'H-H1_LOSC_16_V1-1128665088-4096.gwf',
        'H-H1_LOSC_16_V1-1129127936-4096.gwf',
        'H-H1_LOSC_16_V1-1129664512-4096.gwf'
    ]
    h1_begin = [
        1126084608, 1126109184, 1126264832, 1126293504, 1127194624, 1128042496,
        1128345600, 1128407040, 1128624128, 1128665088, 1129127936, 1129664512
    ]

    l1_list = [
        'L-L1_LOSC_16_V1-1126088704-4096.gwf',
        'L-L1_LOSC_16_V1-1126105088-4096.gwf',
        'L-L1_LOSC_16_V1-1127018496-4096.gwf',
        'L-L1_LOSC_16_V1-1127022592-4096.gwf',
        'L-L1_LOSC_16_V1-1127030784-4096.gwf',
        'L-L1_LOSC_16_V1-1127051264-4096.gwf',
        'L-L1_LOSC_16_V1-1127059456-4096.gwf',
        'L-L1_LOSC_16_V1-1127096320-4096.gwf',
        'L-L1_LOSC_16_V1-1129521152-4096.gwf',
        'L-L1_LOSC_16_V1-1134870528-4096.gwf',
        'L-L1_LOSC_16_V1-1136615424-4096.gwf',
        'L-L1_LOSC_16_V1-1136631808-4096.gwf'
    ]

    l1_begin = [
        1126088704, 1126105088, 1127018496, 1127022592, 1127030784, 1127051264,
        1127059456, 1127096320, 1129521152, 1134870528, 1136615424, 1136631808
    ]

    if detector == 'O1-H':

        file_index = randi.randrange(1, 12, 1)
        print file_index

        a = h1_begin[file_index]
        print a

        start_time = randi.randrange(a, a + 4054, 1)
        print start_time

        file = h1_list[file_index]
        print file

        strain = frame.read_frame(file, 'H1:GWOSC-16KHZ_R1_STRAIN')

    elif detector == 'O1-L':

        a = l1_begin[file_index]
        print a
        file_index = randi.randrange(1, 12, 1)
        print file_index
        start_time = randi.randrange(a, l1_begin[file_index] + 4054, 1)
        print start_time

        file = l1_list[file_index]

        strain = frame.read_frame(file, 'L1:GWOSC-16KHZ_R1_STRAIN', start_time,
                                  start_time + 32)

    shift = randi.randrange(5, 27, 1)

    return strain, shift
Ejemplo n.º 29
0
from pycbc.frame import read_frame
from pycbc.filter import highpass_fir, lowpass_fir, matched_filter
from pycbc.waveform import get_fd_waveform
from pycbc.psd import welch, interpolate
import urllib
import pylab

for ifo in ['H1', 'L1']:
    # Read data and remove low frequency content
    fname = '%s-%s_LOSC_4_V2-1126259446-32.gwf' % (ifo[0], ifo)
    url = "https://losc.ligo.org/s/events/GW150914/" + fname
    urllib.urlretrieve(url, filename=fname)
    h1 = read_frame(fname, '%s:LOSC-STRAIN' % ifo)
    h1 = highpass_fir(h1, 15, 8)

    # Calculate the noise spectrum
    psd = interpolate(welch(h1), 1.0 / h1.duration)

    # whiten
    white_strain = (h1.to_frequencyseries() / psd**0.5 *
                    psd.delta_f).to_timeseries()

    # remove some of the high and low
    smooth = highpass_fir(white_strain, 35, 8)
    smooth = lowpass_fir(white_strain, 300, 8)

    # time shift and flip L1
    if ifo == 'L1':
        smooth *= -1
        smooth.roll(int(.007 / smooth.delta_t))
Ejemplo n.º 30
0
from pycbc.frame import read_frame
from pycbc.filter import highpass_fir, lowpass_fir, matched_filter
from pycbc.waveform import get_fd_waveform
from pycbc.psd import welch, interpolate
import urllib
import pylab

for ifo in ['H1', 'L1']:
    # Read data and remove low frequency content
    fname = '%s-%s_LOSC_4_V2-1126259446-32.gwf' % (ifo[0], ifo)
    url = "https://losc.ligo.org/s/events/GW150914/" + fname
    urllib.urlretrieve(url, filename=fname)
    h1 = read_frame(fname, '%s:LOSC-STRAIN' % ifo)
    h1 = highpass_fir(h1, 15, 8)

    # Calculate the noise spectrum
    psd = interpolate(welch(h1), 1.0 / h1.duration)

    # whiten
    white_strain = (h1.to_frequencyseries() / psd ** 0.5 * psd.delta_f).to_timeseries()

    # remove some of the high and low
    smooth = highpass_fir(white_strain, 35, 8)
    smooth = lowpass_fir(white_strain, 300, 8)

    # time shift and flip L1
    if ifo == 'L1':
        smooth *= -1
        smooth.roll(int(.007 / smooth.delta_t))

    pylab.plot(smooth.sample_times, smooth, label=ifo)
Ejemplo n.º 31
0
def principal(runs,archivo_nombre,local,ifo,hilo):

	
#   Loading our modules
    from modulos import gw_resample, gw_injection, gw_matched_filter
    from modulos import  gw_detection, gw_expected, gw_qtransform, gw_data, gw_chisqv2, gw_readtemplatev
    from Noise_Sources import gw_load_noise, load
    import numpy as np
    import random as randi
    from pycbc import frame, types
    import urllib
    grafica = 0


    SNRthr = 5.5
    
    NSegmnt = 128
    
    #initial distance
    
    i_d = 1
    
    #final distance 
    
    f_d = 10
    
    #number of slices
    
    N_d = 128
    
    #dic = np.linspace(i_d,f_d,N_d)
    #di = []

    #di = [1,5,10,15,20,25,30,35,40,45,50]

    #di.extend(dic)
    if local == False:
        
        url = 'https://www.gw-openscience.org/archive/data/O1_16KHZ/1126170624/'+ifo+'-'+ifo+'1_LOSC_16_V1-'
        urllib.urlretrieve(url+str(archivo_nombre)+'-4096.gwf', str(archivo_nombre)+'.gwf')


#-------------------------------------------------------------------------
#temporal stuff


    aprox = 'NEW'
    
    numero = 1

    total_time = 32
    fs = 8192 # Sampling frequency
    
    fs_ligo = 16384
#---------------------------------------------------------------------------#
#               loading files                                               #
#---------------------------------------------------------------------------#

    archivo = str(archivo_nombre)+'.gwf'

    strain_initial = frame.read_frame(archivo, 'H1:GWOSC-16KHZ_R1_STRAIN')

    strain_n = strain_initial.data

    del strain_initial

    strain_light = []

    for i in range(NSegmnt):
        a = i * total_time * fs_ligo
        b = (i + 1) * total_time * fs_ligo
        strain_light.append(strain_n[a:b])
###############################################################################
#
#       vectores de resultados
#        
###############################################################################        
    SNRrec = []
    SNRexp = []
    trigger_time = []
    injection_time = []
    total_flag = []

    numero = 20
    
    for i in range(numero):
        
        for dat in  range(len(strain_light)):
        
            print dat, ' indice de segmento'

    
            strain = strain_light[dat]
        
            
            strain = types.TimeSeries(initial_array=strain, delta_t=1.0/fs_ligo , epoch=0)


            rec = []
            exp = []
            flag = []
            tt = []
            ti = []


            assert isinstance(runs, object)

#        for d in range(len(di)):


            distancia = 3#di[d]

            n=numero
            
            print distancia, ' distancia'

                #	Reading template
            h,dt,nombre = gw_readtemplatev.lectura(n,aprox)
                #	Distance
                
                
            hh, dts,dtf = gw_resample.interpolacion(h,dt,grafica,fs)
                
            hh = hh/distancia
                
           
            shift = 28

            template, noise = gw_resample.resampleado(strain,hh,grafica,fs,fs_ligo)


#   Making uniform the distribution of the dt in the template
#   These function returns the resampled strain,

#Resampling and resizing data

#   Making the injection of the template into the data
            strain_inj,tmax,injection_shift = gw_injection.make(noise,template,shift,grafica)

#   The limit values for the frequency in the filters
            fc = 15
            hc = 1200
            mc = 733
                        #	Making the matched filter
                        #	This function give us the timeseries object of the snr over the time, the maximum value of the snr and
#	the time that it occurs
            snr,psd,index_snr_trigger,time,rho,psd_o,rho_0 = gw_matched_filter.make(strain_inj,template,fc,mc,hc,grafica)
            

#   Getting the Chi^2 value
            snr2,HSNR2_time,nsnr = gw_chisqv2.xis(snr,template,strain_inj,psd_o,fc,grafica,time,dtf,hc,shift,index_snr_trigger)
            
#   Getting the theoretical value of snr
#-------------------------------------------------------------------------------------------------------------------------
#   Detection part
            desicion = gw_detection.decision(rho,SNRthr)

#--------------------------------------------------------------------------------------------------------------------------
#	The time, amplitude, and phase of the SNR peak tell us how to align
#	our proposed signal with the data.

            rec.append(abs(rho))
            exp.append(abs(rho_0))
            flag.append(desicion)
            tt.append(shift)
            ti.append(time)
        
    SNRrec.extend(rec)
    SNRexp.extend(exp)
    total_flag.extend(flag)
    trigger_time.extend(ti)
    injection_time.extend(tt)
        
        
    rec = []
    exp = []
    flag = []
    tt = []
    ti = []
        
    np.savetxt('Total'+str(archivo_nombre)+ifo+'Hilo'+hilo+'.out',(SNRrec,SNRexp,total_flag,trigger_time,injection_time),fmt='%3.2f')

          
   
    return
Ejemplo n.º 32
0
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:
        if opt.frame_cache:
            frame_source = opt.frame_cache
        if opt.frame_files:
            frame_source = opt.frame_files

        logging.info("Reading Frames")
        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
                    type=int,
                    help='Value of DQ vector indicating good data')
parser.add_argument('--dq-bad-times',
                    type=float,
                    nargs='+',
                    metavar='TIME',
                    help='Center time(s) of bad DQ epoch(s)')
parser.add_argument('--dq-bad-pad',
                    type=float,
                    help='Duration of bad DQ epoch(s)')

args = parser.parse_args()

# load frame file

strain = read_frame(args.input_file, args.strain_channel)

out_channel_names = [args.strain_channel]
out_timeseries = [strain]

# add state vector

if args.state_vector is not None:
    state_dt = 1. / 16.
    state_size = int(strain.duration / state_dt)
    state_data = np.zeros(state_size, dtype=np.uint32)
    state_data[:] = args.state_vector_good

    state_ts = TimeSeries(state_data,
                          delta_t=state_dt,
                          epoch=strain.start_time)
Ejemplo n.º 34
0
from pycbc.frame import read_frame
from pycbc.filter import highpass_fir, lowpass_fir
from pycbc.psd import welch, interpolate
from pycbc.types import TimeSeries
import urllib

# Read data and remove low frequency content
fname = 'H-H1_LOSC_4_V2-1126259446-32.gwf'
url = "https://losc.ligo.org/s/events/GW150914/" + fname
urllib.urlretrieve(url, filename=fname)
h1 = highpass_fir(read_frame(fname, 'H1:LOSC-STRAIN'), 15.0, 8)

# Calculate the noise spectrum and whiten
psd = interpolate(welch(h1), 1.0 / 32)
white_strain = (h1.to_frequencyseries() / psd ** 0.5 * psd.delta_f).to_timeseries()

# remove some of the high and low frequencies
smooth = highpass_fir(white_strain, 25, 8)
smooth = lowpass_fir(white_strain, 250, 8)

#strech out and shift the frequency upwards to aid human hearing
fdata = smooth.to_frequencyseries()
fdata.roll(int(1200 / fdata.delta_f))
smooth = TimeSeries(fdata.to_timeseries(), delta_t=1.0/1024)

#Take slice around signal
smooth = smooth[len(smooth)/2 - 1500:len(smooth)/2 + 3000]
smooth.save_to_wav('gw150914_h1_chirp.wav')

Ejemplo n.º 35
0
#!/usr/bin/env python

import numpy as np
from pycbc import frame
from glob import glob
import sys

filename = glob('./*{0:s}.gwf'.format(sys.argv[1]))[0]
data = frame.read_frame(
    filename, '{0:s}:HWINJ_INJECTED'.format(sys.argv[1]))

idx_max = np.argmax(np.abs(data.numpy()))
time = data.get_sample_times().numpy()[idx_max]

with open('./time.txt', 'w+') as f:
    f.write('{0:.2f}'.format(time))
"""Pick the event with the code *GW170817*. We pick the time frame from 224 seconds before merging and end 32 seconds after merging."""

# Commented out IPython magic to ensure Python compatibility.
# %matplotlib inline
import pylab
from pycbc.filter import highpass
from pycbc.catalog import Merger
from pycbc.frame import read_frame

merger = Merger("GW170817") # the merging part of the event, event = two black holes merging and causing gravitational wave 
strain, stilde = {}, {}
for ifo in ['L1', 'H1']:
    ts = read_frame("{}-{}_LOSC_CLN_4_V1-1187007040-2048.gwf".format(ifo[0], ifo),
                    '{}:LOSC-STRAIN'.format(ifo),
                    start_time=merger.time - 224, # merger.time = the time of merging
                    end_time=merger.time + 32,
                    check_integrity=False)

"""Cleaning and applying highpass filter. Downsample to 2048 Hz to make the data analysis more convenient. Power density of the noise is higher than the signal. At higher frequency the amplitude of the noise is lower. And then graph."""

from pycbc.catalog import Merger
from pycbc.filter import resample_to_delta_t, highpass

from pycbc.catalog import Merger
from pycbc.filter import resample_to_delta_t, highpass

merger = Merger("GW170817")
strain, stilde = {}, {} # tuple of directories: strain and stilde=cleaned data
for ifo in ['L1', 'H1']:
    # We'll download the data and select 256 seconds that includes the event time
Ejemplo n.º 37
0
import pylab
import numpy
from pycbc.frame import read_frame
from pycbc.types import load_timeseries

lal_out_file = "H1-INSPIRAL_lalsuite_FULL_DATA-968605000-2048.gwf"

# Take only the first 16 seconds to use as a small comparison
lal_raw = read_frame(lal_out_file,
                     'H1:LDAS-STRAIN_RAW',
                     start_time=968605000,
                     duration="16")
lal_resample = read_frame(lal_out_file,
                          'H1:LDAS-STRAIN_RAW_RESAMP',
                          start_time=968605000,
                          duration="16")
lal_conditioned = read_frame(lal_out_file,
                             'H1:LDAS-STRAIN_FILTER',
                             start_time=968605000,
                             duration="16")

# Take only the first 16 seconds to use as a small comparison
start_offset = 8  # This is the offest between the output that PyCBC gives and lalapps_inspiral
# lalapps_inspiral does not output the 8 second padding in the frame file
pycbc_raw = load_timeseries("raw_pycbc.npy")[start_offset *
                                             16384:(start_offset + 16) *
                                             16384].astype(lal_raw.dtype)
pycbc_resample = load_timeseries(
    "resampled_pycbc.npy")[start_offset * 4096:(start_offset + 16) *
                           4096].astype(lal_raw.dtype)
Ejemplo n.º 38
0
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
Ejemplo n.º 39
0
w_lal = lal.CreateHannREAL4Window(seg_len)
print "hann props pycbc", w_pycbc.sum(), w_pycbc.squared_norm().sum()
print "hann props lal", w_lal.sum, w_lal.sumofsquares

# Check that the median bias is the same
print "%s segments" % nsegs
print "BIAS pycbc", pycbc.psd.median_bias(nsegs)
print "BIAS lal", lal.RngMedBias(nsegs)

# Check the psd norm
print "PYCBC psd norm", 2.0 / float(sample_rate) / (
    w_pycbc.squared_norm().sum()) / pycbc.psd.median_bias(nsegs)

# Same strain preparation for lal and pycbc psd estimation
strain = read_frame("LER2.lcf",
                    "H1:LDAS-STRAIN",
                    start_time=start_time - pad_data,
                    duration=duration + pad_data * 2)
strain = highpass(strain, frequency=30)
strain *= pycbc.DYN_RANGE_FAC
strain = TimeSeries(strain,
                    delta_t=strain.delta_t,
                    epoch=strain.start_time,
                    dtype=float32)
strain = resample_to_delta_t(strain, 1.0 / sample_rate)
strain = strain[int(pad_data * 1.0 / strain.delta_t):len(strain) -
                int(1.0 / strain.delta_t * pad_data)]

psd_pycbc = pycbc.psd.welch(strain, seg_len=seg_len, seg_stride=stride)
psd_lal = lal_psd_estimate(seg_len, strain)

print psd_pycbc[-1]
Ejemplo n.º 40
0
from pycbc.frame import read_frame
from pycbc.filter import highpass_fir, lowpass_fir
from pycbc.psd import welch, interpolate
from pycbc.types import TimeSeries
try:
    from urllib.request import urlretrieve
except ImportError:  # python < 3
    from urllib import urlretrieve

# Read data and remove low frequency content
fname = 'H-H1_LOSC_4_V2-1126259446-32.gwf'
url = "https://www.gw-openscience.org/GW150914data/" + fname
urlretrieve(url, filename=fname)
h1 = highpass_fir(read_frame(fname, 'H1:LOSC-STRAIN'), 15.0, 8)

# Calculate the noise spectrum and whiten
psd = interpolate(welch(h1), 1.0 / 32)
white_strain = (h1.to_frequencyseries() / psd ** 0.5 * psd.delta_f).to_timeseries()

# remove some of the high and low frequencies
smooth = highpass_fir(white_strain, 25, 8)
smooth = lowpass_fir(white_strain, 250, 8)

#strech out and shift the frequency upwards to aid human hearing
fdata = smooth.to_frequencyseries()
fdata.roll(int(1200 / fdata.delta_f))
smooth = TimeSeries(fdata.to_timeseries(), delta_t=1.0/1024)

#Take slice around signal
smooth = smooth[len(smooth)/2 - 1500:len(smooth)/2 + 3000]
smooth.save_to_wav('gw150914_h1_chirp.wav')
Ejemplo n.º 41
0
# Check that we are using the same hann window
w_pycbc = Array(numpy.hanning(seg_len).astype(float32))
w_lal = lal.CreateHannREAL4Window(seg_len)
print "hann props pycbc", w_pycbc.sum(), w_pycbc.squared_norm().sum()
print "hann props lal", w_lal.sum, w_lal.sumofsquares

# Check that the median bias is the same
print "%s segments" % nsegs
print "BIAS pycbc", pycbc.psd.median_bias(nsegs)
print "BIAS lal", lal.RngMedBias(nsegs)

# Check the psd norm
print "PYCBC psd norm", 2.0 / float(sample_rate) / (w_pycbc.squared_norm().sum()) / pycbc.psd.median_bias(nsegs)

# Same strain preparation for lal and pycbc psd estimation
strain = read_frame("LER2.lcf", "H1:LDAS-STRAIN", start_time=start_time-pad_data, duration=duration+pad_data*2)
strain = highpass(strain, frequency=30)
strain *= pycbc.DYN_RANGE_FAC
strain = TimeSeries(strain, delta_t=strain.delta_t, epoch=strain.start_time, dtype=float32)
strain = resample_to_delta_t(strain, 1.0/sample_rate)
strain = strain[int(pad_data*1.0/strain.delta_t):len(strain)-int(1.0/strain.delta_t*pad_data)]

psd_pycbc = pycbc.psd.welch(strain, seg_len=seg_len, seg_stride=stride)
psd_lal = lal_psd_estimate(seg_len, strain)

print psd_pycbc[-1]
print psd_lal[-1]

pylab.figure(1)
pylab.loglog(psd_pycbc.sample_frequencies.numpy(), psd_pycbc.numpy(), label="PyCBC")
pylab.loglog(psd_lal.sample_frequencies.numpy(), psd_lal.numpy(), label="LAL")