def _retrieve_metadata(stream, channel_name): """Retrieve basic metadata by reading the first file in the cache Parameters ---------- stream: lal stream object Stream containing a channel we want to learn about channel_name: str The name of the channel we want to know the dtype and sample rate of Returns ------- channel_type: lal type enum Enum value which indicates the dtype of the channel sample_rate: int The sample rate of the data within this channel """ lalframe.FrStreamGetVectorLength(channel_name, stream) channel_type = lalframe.FrStreamGetTimeSeriesType(channel_name, stream) create_series_func = _fr_type_map[channel_type][2] get_series_metadata_func = _fr_type_map[channel_type][3] series = create_series_func(channel_name, stream.epoch, 0, 0, lal.ADCCountUnit, 0) get_series_metadata_func(series, stream) return channel_type, int(1.0/series.deltaT)
def _read_channel(channel, stream, start, duration): channel_type = lalframe.FrStreamGetTimeSeriesType(channel, stream) read_func = _fr_type_map[channel_type][0] d_type = _fr_type_map[channel_type][1] data = read_func(stream, channel, start, duration, 0) return TimeSeries(data.data.data, delta_t=data.deltaT, epoch=start, dtype=d_type)
def _read_channel(stream, channel, start, duration): dtype = lalframe.FrStreamGetTimeSeriesType(channel, stream) reader = lalutils.find_typed_function(dtype, 'FrStreamRead', 'TimeSeries', module=lalframe) return reader(stream, channel, start, duration, 0)
def _read_channel(stream, channel, start, duration): try: dtype = lalframe.FrStreamGetTimeSeriesType(channel, stream) except RuntimeError as exc: if str(exc).lower() == "wrong name": exc.args = "channel '{}' not found".format(channel), raise reader = lalutils.find_typed_function(dtype, 'FrStreamRead', 'TimeSeries', module=lalframe) return reader(stream, channel, start, duration, 0)
def read_frame(location, channels, start_time=None, end_time=None, duration=None, check_integrity=True): """Read time series from frame data. Using the `location`, which can either be a frame file ".gwf" or a frame cache ".gwf", read in the data for the given channel(s) and output as a TimeSeries or list of TimeSeries. Parameters ---------- location : string A source of gravitational wave frames. Either a frame filename (can include pattern), a list of frame files, or frame cache file. channels : string or list of strings Either a string that contains the channel name or a list of channel name strings. start_time : {None, LIGOTimeGPS}, optional The gps start time of the time series. Defaults to reading from the beginning of the available frame(s). end_time : {None, LIGOTimeGPS}, optional The gps end time of the time series. Defaults to the end of the frame. Note, this argument is incompatible with `duration`. duration : {None, float}, optional The amount of data to read in seconds. Note, this argument is incompatible with `end`. check_integrity : {True, bool}, optional Test the frame files for internal integrity. Returns ------- Frame Data: TimeSeries or list of TimeSeries A TimeSeries or a list of TimeSeries, corresponding to the data from the frame file/cache for a given channel or channels. """ if end_time and duration: raise ValueError("end time and duration are mutually exclusive") if type(location) is list: locations = location else: locations = [location] cum_cache = locations_to_cache(locations) stream = lalframe.FrStreamCacheOpen(cum_cache) stream.mode = lalframe.FR_STREAM_VERBOSE_MODE if check_integrity: stream.mode = (stream.mode | lalframe.FR_STREAM_CHECKSUM_MODE) lalframe.FrSetMode(stream.mode, stream) # determine duration of data if type(channels) is list: first_channel = channels[0] else: first_channel = channels data_length = lalframe.FrStreamGetVectorLength(first_channel, stream) channel_type = lalframe.FrStreamGetTimeSeriesType(first_channel, stream) create_series_func = _fr_type_map[channel_type][2] get_series_metadata_func = _fr_type_map[channel_type][3] series = create_series_func(first_channel, stream.epoch, 0, 0, lal.ADCCountUnit, 0) get_series_metadata_func(series, stream) data_duration = data_length * series.deltaT if start_time is None: start_time = stream.epoch*1 if end_time is None: end_time = start_time + data_duration if type(start_time) is not lal.LIGOTimeGPS: start_time = lal.LIGOTimeGPS(start_time) if type(end_time) is not lal.LIGOTimeGPS: end_time = lal.LIGOTimeGPS(end_time) if duration is None: duration = float(end_time - start_time) else: duration = float(duration) # lalframe behaves dangerously with invalid duration so catch it here if duration <= 0: raise ValueError("Negative or null duration") #if duration > data_duration: # raise ValueError("Requested duration longer than available data") if type(channels) is list: all_data = [] for channel in channels: channel_data = _read_channel(channel, stream, start_time, duration) lalframe.FrStreamSeek(stream, start_time) all_data.append(channel_data) return all_data else: return _read_channel(channels, stream, start_time, duration)
def _read_channel(stream, channel, start, duration): dtype = lalframe.FrStreamGetTimeSeriesType(channel, stream) typestr = lalutils.LAL_TYPE_STR[dtype] reader = getattr(lalframe, 'FrStreamRead%sTimeSeries' % typestr) return reader(stream, channel, start, duration, 0)