def locations_to_cache(locations): """ Return a cumulative cache file build from the list of locations Parameters ---------- locations : list A list of strings containing files, globs, or cache files used to build a combined lal cache file object. Returns ------- cache : lal.Cache A cumulative lal cache object containing the files derived from the list of locations """ cum_cache = lal.Cache() for source in locations: for file_path in glob.glob(source): dir_name, file_name = os.path.split(file_path) _, file_extension = os.path.splitext(file_name) if file_extension in [".lcf", ".cache"]: cache = lal.CacheImport(file_path) elif file_extension == ".gwf": cache = lalframe.FrOpen(dir_name, file_name).cache else: raise TypeError("Invalid location name") cum_cache = lal.CacheMerge(cum_cache, cache) return cum_cache
def open_data_source(source): """Open a GWF file source into a `lalframe.XLALFrStream` object Parameters ---------- source : `str`, `file`, `list` Data source to read. Returns ------- stream : `lalframe.FrStream` An open `FrStream`. Raises ------ ValueError If the input format cannot be identified. """ # -- preformatting try: source = file_path(source) except ValueError: # not parsable as a single file pass # import cache from file if ( isinstance(source, str) and source.endswith(('.lcf', '.cache')) ): source = lal.CacheImport(source) # reformat cache (or any list of files) as a lal cache object if isinstance(source, list) and is_cache(source): cache = lal.Cache() for entry in file_list(source): cache = lal.CacheMerge(cache, lal.CacheGlob(*os.path.split(entry))) source = cache # -- now we have a lal.Cache or a filename # read lal cache object if isinstance(source, lal.Cache): return lalframe.FrStreamCacheOpen(source) # read single file if isinstance(source, str): return lalframe.FrStreamOpen(*map(str, os.path.split(source))) raise ValueError("Don't know how to open data source of type %r" % type(source))
def locations_to_cache(locations, latest=False): """ Return a cumulative cache file build from the list of locations Parameters ---------- locations : list A list of strings containing files, globs, or cache files used to build a combined lal cache file object. latest : Optional, {False, Boolean} Only return a cache with the most recent frame in the locations. If false, all results are returned. Returns ------- cache : lal.Cache A cumulative lal cache object containing the files derived from the list of locations """ cum_cache = lal.Cache() for source in locations: flist = glob.glob(source) if latest: def relaxed_getctime(fn): # when building a cache from a directory of temporary # low-latency frames, files might disappear between # the glob() and getctime() calls try: return os.path.getctime(fn) except OSError: return 0 flist = [max(flist, key=relaxed_getctime)] for file_path in flist: dir_name, file_name = os.path.split(file_path) _, file_extension = os.path.splitext(file_name) if file_extension in [".lcf", ".cache"]: cache = lal.CacheImport(file_path) elif file_extension == ".gwf" or _is_gwf(file_path): cache = lalframe.FrOpen(str(dir_name), str(file_name)).cache else: raise TypeError("Invalid location name") cum_cache = lal.CacheMerge(cum_cache, cache) return cum_cache
def open_data_source(source): """Open a GWF file source into a `lalframe.XLALFrStream` object Parameters ---------- source : `str`, `file`, `lal.Cache`, :class:`glue.lal.Cache` data source to read Returns ------- stream : `lalframe.FrStream` an open `FrStream` Raises ------ ValueError if the input format cannot be identified """ if isinstance(source, FILE_LIKE): source = source.name if isinstance(source, GlueCacheEntry): source = source.path # read cache file if (isinstance(source, string_types) and source.endswith( ('.lcf', '.cache'))): return lalframe.FrStreamCacheOpen(lal.CacheImport(source)) # read glue cache object if isinstance(source, GlueCache): cache = lal.Cache() for entry in source: cache = lal.CacheMerge(cache, lal.CacheGlob(*os.path.split(entry.path))) return lalframe.FrStreamCacheOpen(cache) # read lal cache object if isinstance(source, lal.Cache): return lalframe.FrStreamCacheOpen(source) # read single file if isinstance(source, string_types): return lalframe.FrStreamOpen(*os.path.split(source)) raise ValueError("Don't know how to open data source of type %r" % type(source))
def open_data_source(source): """Open a GWF file source into a `lalframe.XLALFrStream` object Parameters ---------- source : `str`, `file`, `list` Data source to read. Returns ------- stream : `lalframe.FrStream` An open `FrStream`. Raises ------ ValueError If the input format cannot be identified. """ if isinstance(source, io_cache.FILE_LIKE): source = source.name if isinstance(source, CacheEntry): source = source.path # read cache file if (isinstance(source, string_types) and source.endswith(('.lcf', '.cache'))): return lalframe.FrStreamCacheOpen(lal.CacheImport(source)) # read glue cache object if isinstance(source, list) and io_cache.is_cache(source): cache = lal.Cache() for entry in io_cache.file_list(source): cache = lal.CacheMerge(cache, lal.CacheGlob(*os.path.split(entry))) return lalframe.FrStreamCacheOpen(cache) # read lal cache object if isinstance(source, lal.Cache): return lalframe.FrStreamCacheOpen(source) # read single file if isinstance(source, string_types): return lalframe.FrStreamOpen(*map(str, os.path.split(source))) raise ValueError("Don't know how to open data source of type %r" % type(source))
def locations_to_cache(locations, latest=False): """ Return a cumulative cache file build from the list of locations Parameters ---------- locations : list A list of strings containing files, globs, or cache files used to build a combined lal cache file object. latest : Optional, {False, Boolean} Only return a cache with the most recent frame in the locations. If false, all results are returned. Returns ------- cache : lal.Cache A cumulative lal cache object containing the files derived from the list of locations """ cum_cache = lal.Cache() for source in locations: flist = glob.glob(source) if latest: flist = [max(flist, key=os.path.getctime)] for file_path in flist: dir_name, file_name = os.path.split(file_path) _, file_extension = os.path.splitext(file_name) if file_extension in [".lcf", ".cache"]: cache = lal.CacheImport(file_path) elif file_extension == ".gwf": cache = lalframe.FrOpen(str(dir_name), str(file_name)).cache else: raise TypeError("Invalid location name") cum_cache = lal.CacheMerge(cum_cache, cache) return cum_cache
def read_frame(location, channels, start_time=None, end_time=None, duration=None): """Read time series from frame data. Using a 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`. 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 = lal.Cache() for source in locations: dir_name, file_name = os.path.split(source) base_name, file_extension = os.path.splitext(file_name) if file_extension == ".lcf" or file_extension == ".cache": cache = lal.CacheImport(source) elif file_extension == ".gwf": cache = lalframe.FrOpen(dir_name, file_name).cache else: raise TypeError("Invalid location name") cum_cache = lal.CacheMerge(cum_cache, cache) stream = lalframe.FrStreamCacheOpen(cum_cache) stream.mode = lalframe.FR_STREAM_VERBOSE_MODE lalframe.FrSetMode(stream.mode | lalframe.FR_STREAM_CHECKSUM_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)