18"""Wrappings of the LALFrame input methods for reading GWF files
20This module provides the read_timeseries function, the primary method
21by which users can load TimeSeries data from a variety of sources,
23 - a Gravitational-Wave Frame file (.gwf file extension)
24 - a LAL-format cache file (.lcf or .cache file extension)
25 - a LAL-format cache object (either XLALCache()
or glue.lal.Cache [1]
27[1] https://www.lsc-group.phys.uwm.edu/daswg/projects/glue/doc/glue.lal.Cache-class.html
34from lal import utils as lalutils
37 from glue
import lal
as gcache
44from .
import git_version
45__author__ =
"Duncan Macleod <duncan.macleod@ligo.org>"
46__version__ = git_version.id
47__date__ = git_version.date
49__all__ = [
'read_timeseries']
53 datatype=None, verbose=False):
54 r"""Read a TimeSeries of channel data from a source.
56 Acceptable sources are:
57 - a .gwf-format framefile (string ending in '.gwf')
58 - a LAL-format cache file (string ending
in '.lcf' or '.cache')
59 - a lal.Cache object (either
from SWIG-LAL
or GLUE)
62 input source, see above
for details
64 string name of channel, e.g.
'L1:LDAS-STRAIN',
or a list of channel
67 LIGOTimeGPS start time
for output TimeSeries
69 float duration (seconds)
for output TimeSeries
71 datatype, either an integer
from the LALTYPECODE, a string
72 matchine the corresponding type,
or a numpy dtype
74 print verbose output, default:
False
76 @returns a TimeSeries of the imported data
78 Example 1, reading
from a frame file:
81 >>> out =
read_timeseries(
'L-R-1061499968-32.gwf',
'L1:PSL-ISS_PDB_OUT_DQ')
83 <type
'REAL4TimeSeries'>
84 >>> print(out.name, float(out.epoch), out.deltaT)
85 (
'L1:PSL-ISS_PDB_OUT_DQ', 1061499968.0, 3.0517578125e-05)
88 Example 2, reading
from a cache:
92 >>> cache = lal.CacheGlob(
'/scratch/ER4/L0/L1/L-R-10614',
'L-R-1061499968*')
93 >>> out = frread.read_timeseries(cache,
'L1:PSL-ISS_PDB_OUT_DQ')
94 >>> print(out.name, float(out.epoch), out.deltaT)
95 (
'L1:PSL-ISS_PDB_OUT_DQ', 1061499968.0, 3.0517578125e-05)
98 Example 3, restricting data input:
101 >>> out =
read_timeseries(
'L-R-1061499968-32.gwf',
'L1:PSL-ISS_PDB_OUT_DQ',
102 start=1061499970, duration=10)
103 >>> print(out.name, float(out.epoch), out.data.length)
104 (
'L1:PSL-ISS_PDB_OUT_DQ', 1061499970.0, 327680)
107 Example 4, specifying data type:
111 'L1:PSL-ODC_CHANNEL_OUT_DQ')
112 >>> print(type(out), out.data.data[:4])
113 (<type
'REAL4TimeSeries'>,
114 array([ 4259839., 4259839., 4259839., 4259839.], dtype=float32))
116 'L1:PSL-ODC_CHANNEL_OUT_DQ', datatype=
'int8')
117 >>> print(type(out), out.data.data[:4])
118 (<type
'INT8TimeSeries'>, array([4259839, 4259839, 4259839, 4259839]))
123 if isinstance(channel, str):
126 channels = list(channel)
129 if (isinstance(source, str)
and
130 re.search(
r'(.lcf|.cache)\Z', source)):
131 source = lal.CacheImport(os.path.expanduser(source))
133 if _HAS_GLUE
and isinstance(source, gcache.Cache):
134 source = lalutils.lalcache_from_gluecache(source)
137 if isinstance(source, str)
and source.endswith(
'.gwf'):
138 out = _ts_from_frame_file(source, channels, start=start,
139 duration=duration, datatype=datatype,
142 elif isinstance(source, lal.Cache):
143 out = _ts_from_cache(source, channels, start=start,
144 duration=duration, datatype=datatype,
148 raise ValueError(
"Cannot interpret source '%s'." % source)
151 if isinstance(channel, str):
157def _ts_from_cache(cache, channels, start=None, duration=None, datatype=None,
159 """Read a TimeSeries of channel data from a LAL Cache object
162 XLALCache() containing list of GWF file paths
164 list of channel names
166 LIGOTimeGPS start time
for output TimeSeries
168 float duration (seconds)
for output TimeSeries
170 datatype, either an integer
from the LALTYPECODE, a string
171 matchine the corresponding type,
or a numpy dtype
172 @param verbose UNDOCUMENTED
174 @returns a TimeSeries of the imported data
177 stream = lalframe.FrStreamCacheOpen(cache)
179 return _ts_from_stream(stream, channels, start=start, duration=duration,
180 datatype=datatype, verbose=verbose)
183def _ts_from_frame_file(framefile, channels, start=None, duration=None,
184 datatype=None, verbose=False):
185 """Read a TimeSeries of channel data from a GWF-format framefile
188 path to GWF-format framefile to read
190 list of channel names
192 LIGOTimeGPS start time
for output TimeSeries
194 float duration (seconds)
for output TimeSeries
196 datatype, either an integer
from the LALTYPECODE, a string
197 matchine the corresponding type,
or a numpy dtype
199 print verbose output, default:
False
201 @returns a TimeSeries of the imported data
204 framefile = os.path.abspath(framefile)
205 stream = lalframe.FrStreamOpen(
'', framefile)
207 return _ts_from_stream(stream, channels, start=start, duration=duration,
208 datatype=datatype, verbose=verbose)
211def _ts_from_stream(stream, channels, start=None, duration=None, datatype=None,
213 """Read a TimeSeries of channel data from an open FrStream
216 XLALFrStream() of data
from which to read
218 list of channel names
220 LIGOTimeGPS start time
for output TimeSeries
222 float duration (seconds)
for output TimeSeries
224 datatype, either an integer
from the LALTYPECODE, a string
225 matchine the corresponding type,
or a numpy dtype
227 print verbose output, default:
False
229 @returns a TimeSeries of the imported data
232 lalframe.FrStreamSetMode(
234 verbose
and lalframe.FR_STREAM_VERBOSE_MODE
235 or lalframe.FR_STREAM_DEFAULT_MODE,
238 epoch = lal.LIGOTimeGPS(stream.epoch)
242 startoffset = float(start - epoch)
246 for channel
in channels:
249 lalframe.FrStreamSeek(stream, epoch)
254 """Read the TimeSeries of a single channel from an open stream
257 frdatatype = lalframe.FrStreamGetTimeSeriesType(channel, stream)
259 datatype = frdatatype
261 datatype = lalutils.get_lal_type(datatype)
264 read = getattr(lalframe,
'FrStreamRead%sTimeSeries'
265 % lalutils.get_lal_type_str(frdatatype))
266 origin = read(stream, channel, start, duration, 0)
268 if datatype == frdatatype:
270 if datatype != frdatatype:
271 create = lalutils.func_factory(
272 'create',
'%stimeseries' % lalutils.get_lal_type_str(datatype))
273 series = create(channel, start, origin.f0, origin.deltaT,
274 origin.sampleUnits, origin.data.length)
275 series.data.data = origin.data.data.astype(
276 lalutils.get_numpy_type(datatype))
281 """Find the number of samples represented in a frame stream
284 XLALFrStream() of data to measure
286 string name of channel to measure
288 @returns the integer length of the data
for this channel
290 epoch = lal.LIGOTimeGPS(stream.epoch.gpsSeconds,
291 stream.epoch.gpsNanoSeconds)
293 nfile = stream.cache.length
295 for i
in range(nfile):
296 for j
in range(lalframe.FrFileQueryNFrame(stream.file)):
297 length += lalframe.FrFileQueryChanVectorLength(stream.file,
299 lalframe.FrStreamNext(stream)
301 lalframe.FrStreamSeek(stream, epoch)
306 """Find the duration of time stored in a frame stream
309 XLALFrStream() of data to measure
311 @returns the float duration (seconds) of the data
for this channel
313 epoch = lal.LIGOTimeGPS(stream.epoch.gpsSeconds,
314 stream.epoch.gpsNanoSeconds)
316 nfile = stream.cache.length
318 for i
in range(nfile):
319 for j
in range(lalframe.FrFileQueryNFrame(stream.file)):
320 duration += lalframe.FrFileQueryDt(stream.file, 0)
321 lalframe.FrStreamNext(stream)
323 lalframe.FrStreamSeek(stream, epoch)
def get_stream_duration(stream)
Find the duration of time stored in a frame stream.
def read_channel_from_stream(stream, channel, start, duration, datatype=None)
Read the TimeSeries of a single channel from an open stream.
def get_stream_length(stream, channel)
Find the number of samples represented in a frame stream.
def read_timeseries(source, channel, start=None, duration=None, datatype=None, verbose=False)
Read a TimeSeries of channel data from a source.