def gluecache_to_FrCache(cache): """ Convert a glue.lal.Cache object to a lalframe.FrCache object. Writes cache to temporary file and reads to FrCache. Arguments: cache : glue.lal.Cache LAL cache object from GLUE to convert """ with tempfile.NamedTemporaryFile(delete=False) as t: cache = cache for e in cache: e.segment = type(e.segment)(int(e.segment[0]), int(e.segment[1])) cache.tofile(t) frcache = lal.CacheImport(t.name) os.remove(t.name) return frcache
def fromlalcache(cache, chname, start=-1, duration=1, datatype=-1,\ verbose=False): """ Read data from a cache object into a REAL?TimeSeries. Restrict data with the gpsstart and duration parameters. Arguments: cache : [ filepath | file | glue.lal.Cache | lalframe.FrCache ] object to be read to lalframe.FrCache chname : str name of channel to read Keyword arguments: start : lal.LIGOTimeGPS GPS start time of requested data duration : float length of requested data series in seconds datatype : int LAL enum for requested datatype, -1 == read from frame metadata verbose : [ True | False ] verbose output """ # read cache from file object into gluecache for the time being if hasattr(cache, 'readline'): cache = gluecache.Cache.fromfile(cache) # read cache from glue.lal.Cache, or lalframe.FrCache, or file if cache and isinstance(cache, _gluecache.Cache): cache.sort(key=lambda e: e.segment[0]) cache = gluecache_to_FrCache(cache) elif isinstance(cache, str): cache = lal.CacheImport(cache) # open cache to stream stream = lalframe.FrCacheOpen(cache) # return series = fromFrStream(stream, chname, start=start, duration=duration,\ datatype=datatype, verbose=verbose) return series
def read_timeseries(source, channel, start=None, duration=None, datatype=None, verbose=False): r"""Read a TimeSeries of channel data from a source. Acceptable sources are: - a .gwf-format framefile (string ending in '.gwf') - a LAL-format cache file (string ending in '.lcf' or '.cache') - a lal.Cache object (either from SWIG-LAL or GLUE) @param source input source, see above for details @param channel string name of channel, e.g. 'L1:LDAS-STRAIN', or a list of channel names @param start LIGOTimeGPS start time for output TimeSeries @param duration float duration (seconds) for output TimeSeries @param datatype datatype, either an integer from the LALTYPECODE, a string matchine the corresponding type, or a numpy dtype @param verbose print verbose output, default: False @returns a TimeSeries of the imported data Example 1, reading from a frame file: \code >>> out = read_timeseries('L-R-1061499968-32.gwf', 'L1:PSL-ISS_PDB_OUT_DQ') >>> print(type(out)) <type 'REAL4TimeSeries'> >>> print(out.name, float(out.epoch), out.deltaT) ('L1:PSL-ISS_PDB_OUT_DQ', 1061499968.0, 3.0517578125e-05) \endcode Example 2, reading from a cache: \code >>> import lal >>> cache = lal.CacheGlob('/scratch/ER4/L0/L1/L-R-10614', 'L-R-1061499968*') >>> out = frread.read_timeseries(cache, 'L1:PSL-ISS_PDB_OUT_DQ') >>> print(out.name, float(out.epoch), out.deltaT) ('L1:PSL-ISS_PDB_OUT_DQ', 1061499968.0, 3.0517578125e-05) \endcode Example 3, restricting data input: \code >>> out = read_timeseries('L-R-1061499968-32.gwf', 'L1:PSL-ISS_PDB_OUT_DQ', start=1061499970, duration=10) >>> print(out.name, float(out.epoch), out.data.length) ('L1:PSL-ISS_PDB_OUT_DQ', 1061499970.0, 327680) \endcode Example 4, specifying data type: \code >>> out = read_timeseries('L-R-1061499968-32.gwf', 'L1:PSL-ODC_CHANNEL_OUT_DQ') >>> print(type(out), out.data.data[:4]) (<type 'REAL4TimeSeries'>, array([ 4259839., 4259839., 4259839., 4259839.], dtype=float32)) >>> out = read_timeseries('L-R-1061499968-32.gwf', 'L1:PSL-ODC_CHANNEL_OUT_DQ', datatype='int8') >>> print(type(out), out.data.data[:4]) (<type 'INT8TimeSeries'>, array([4259839, 4259839, 4259839, 4259839])) \endcode """ # parse channels if isinstance(channel, string_types): channels = [channel] else: channels = list(channel) # read cache file if (isinstance(source, string_types) and re.search(r'(.lcf|.cache)\Z', source)): source = lal.CacheImport(os.path.expanduser(source)) # convert GLUE cache file if _HAS_GLUE and isinstance(source, gcache.Cache): source = lalutils.lalcache_from_gluecache(source) # read from single frame if isinstance(source, string_types) and source.endswith('.gwf'): out = ts_from_frame_file(source, channels, start=start, duration=duration, datatype=datatype, verbose=verbose) # read from XLALCache elif isinstance(source, lal.Cache): out = ts_from_cache(source, channels, start=start, duration=duration, datatype=datatype, verbose=verbose) # otherwise barf else: raise ValueError("Cannot interpret source '%s'." % source) # return if isinstance(channel, string_types): return out[0] else: return out