Exemple #1
0
def get_source(source):
    """Get the source data for a particular GW catalog
    """
    if source in _aliases:
        source = _aliases[source]

    if source in _catalogs:
        catalog_type = _catalogs[source]
        if catalog_type == 'LVC':
            fname = get_file(base_lvc_url.format(source), cache=True)
            data = json.load(open(fname, 'r'))
    else:
        raise ValueError('Unkown catalog source {}'.format(source))
    return data['events']
Exemple #2
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.
    """
    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 = get_file(url, cache=True)
            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
Exemple #3
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))
Exemple #4
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

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

        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

        ver = url.split('/')[-1].split('-')[1].split('_')[-1]
        channel = "{}:GWOSC-{}_{}_STRAIN".format(ifo, sampling.upper(), ver)

        filename = get_file(url, cache=True)
        return read_frame(str(filename), str(channel))
Exemple #5
0
def query_flag(ifo, segment_name, start_time, end_time,
               source='any', server="https://segments.ligo.org",
               veto_definer=None, cache=False):
    """Return the times where the flag is active

    Parameters
    ----------
    ifo: string
        The interferometer to query (H1, L1).
    segment_name: string
        The status flag to query from LOSC.
    start_time: int
        The starting gps time to begin querying from LOSC
    end_time: int
        The end gps time of the query
    source: str, Optional
        Choice between "GWOSC" or "dqsegdb". If dqsegdb, the server option may
        also be given. The default is to try GWOSC first then try dqsegdb.
    server: str, Optional
        The server path. Only used with dqsegdb atm.
    veto_definer: str, Optional
        The path to a veto definer to define groups of flags which
        themselves define a set of segments.
    cache: bool
        If true cache the query. Default is not to cache

    Returns
    ---------
    segments: glue.segments.segmentlist
        List of segments
    """
    flag_segments = segmentlist([])

    if source in ['GWOSC', 'any']:
        # Special cases as the LOSC convention is backwards from normal
        # LIGO / Virgo operation!!!!
        if (('_HW_INJ' in segment_name and 'NO' not in segment_name) or
                'VETO' in segment_name):
            data = query_flag(ifo, 'DATA', start_time, end_time)

            if '_HW_INJ' in segment_name:
                name = 'NO_' + segment_name
            else:
                name = segment_name.replace('_VETO', '')

            negate = query_flag(ifo, name, start_time, end_time, cache=cache)
            return (data - negate).coalesce()

        duration = end_time - start_time
        try:
            url = GWOSC_URL.format(get_run(start_time + duration/2, ifo),
                                   ifo, segment_name,
                                   int(start_time), int(duration))

            fname = get_file(url, cache=cache, timeout=10)
            data = json.load(open(fname, 'r'))
            if 'segments' in data:
                flag_segments = data['segments']

        except Exception as e:
            if source != 'any':
                raise ValueError("Unable to find {} segments in GWOSC, check "
                                 "flag name or times".format(segment_name))

            return query_flag(ifo, segment_name, start_time, end_time,
                              source='dqsegdb', server=server,
                              veto_definer=veto_definer)

    elif source == 'dqsegdb':
        # The veto definer will allow the use of MACRO names
        # These directly correspond to the name in the veto definer file
        if veto_definer is not None:
            veto_def = parse_veto_definer(veto_definer, [ifo])

        # We treat the veto definer name as if it were its own flag and
        # process the flags in the veto definer
        if veto_definer is not None and segment_name in veto_def[ifo]:
            for flag in veto_def[ifo][segment_name]:
                partial = segmentlist([])
                segs = query_dqsegdb2(ifo, flag['full_name'],
                                      start_time, end_time, server)
                # Apply padding to each segment
                for rseg in segs:
                    seg_start = rseg[0] + flag['start_pad']
                    seg_end = rseg[1] + flag['end_pad']
                    partial.append(segment(seg_start, seg_end))

                # Limit to the veto definer stated valid region of this flag
                flag_start = flag['start']
                flag_end = flag['end']
                # Corner case: if the flag end time is 0 it means 'no limit'
                # so use the query end time
                if flag_end == 0:
                    flag_end = int(end_time)
                send = segmentlist([segment(flag_start, flag_end)])
                flag_segments += (partial.coalesce() & send)

        else:  # Standard case just query directly
            segs = query_dqsegdb2(ifo, segment_name, start_time, end_time,
                                  server)
            for rseg in segs:
                flag_segments.append(segment(rseg[0], rseg[1]))

        # dqsegdb output is not guaranteed to lie entirely within start
        # and end times, hence restrict to this range
        flag_segments = flag_segments.coalesce() & \
            segmentlist([segment(int(start_time), int(end_time))])

    else:
        raise ValueError("Source must be `dqsegdb`, `GWOSC` or `any`."
                         " Got {}".format(source))

    return segmentlist(flag_segments).coalesce()