def write(tsdict, outfile, start=None, end=None, name='gwpy', run=0): """Write data to a GWF file using the LALFrame API """ if not start: start = list(tsdict.values())[0].xspan[0] if not end: end = list(tsdict.values())[0].xspan[1] duration = end - start # get ifos list detectors = 0 for series in tsdict.values(): try: idx = list(lalutils.LAL_DETECTORS.keys()).index(series.channel.ifo) detectors |= 1 << 2*idx except (KeyError, AttributeError): continue # create new frame frame = lalframe.FrameNew(start, duration, name, run, 0, detectors) for series in tsdict.values(): # convert to LAL lalseries = series.to_lal() # find adder add_ = lalutils.find_typed_function( series.dtype, 'FrameAdd', 'TimeSeriesProcData', module=lalframe) # add time series to frame add_(frame, lalseries) # write frame lalframe.FrameWrite(frame, outfile)
def write(tsdict, outfile, start=None, end=None, name='gwpy', run=0): """Write data to a GWF file using the LALFrame API """ if not start: start = tsdict.values()[0].xspan[0] if not end: end = tsdict.values()[0].xspan[1] duration = end - start # get ifos list detectors = 0 for series in tsdict.values(): try: idx = list(lalutils.LAL_DETECTORS.keys()).index(series.channel.ifo) detectors |= 2**idx except (KeyError, AttributeError): continue # create new frame frame = lalframe.FrameNew(start, duration, name, run, 0, detectors) for series in tsdict.values(): # convert to LAL lalseries = series.to_lal() # find adder laltype = lalutils.LAL_TYPE_FROM_NUMPY[series.dtype.type] typestr = lalutils.LAL_TYPE_STR[laltype] add_ = getattr(lalframe, 'FrameAdd%sTimeSeriesProcData' % typestr) # add time series to frame add_(frame, lalseries) # write frame lalframe.FrameWrite(frame, outfile)
def write_frame(location, channels, timeseries): """Write a list of time series to a single frame file. Parameters ---------- location : string A frame filename. channels : string or list of strings Either a string that contains the channel name or a list of channel name strings. timeseries: TimeSeries A TimeSeries or list of TimeSeries, corresponding to the data to be written to the frame file for a given channel. """ # check if a single channel or a list of channels if type(channels) is list and type(timeseries) is list: channels = channels timeseries = timeseries else: channels = [channels] timeseries = [timeseries] # check that timeseries have the same start and end time gps_start_times = {series.start_time for series in timeseries} gps_end_times = {series.end_time for series in timeseries} if len(gps_start_times) != 1 or len(gps_end_times) != 1: raise ValueError("Start and end times of TimeSeries must be identical.") # check that start, end time, and duration are integers gps_start_time = gps_start_times.pop() gps_end_time = gps_end_times.pop() duration = int(gps_end_time - gps_start_time) if gps_start_time % 1 or gps_end_time % 1: raise ValueError("Start and end times of TimeSeries must be integer seconds.") # create frame frame = lalframe.FrameNew(epoch=gps_start_time, duration=duration, project='', run=1, frnum=1, detectorFlags=lal.LALDETECTORTYPE_ABSENT) for i,tseries in enumerate(timeseries): # get data type for seriestype in _fr_type_map.keys(): if _fr_type_map[seriestype][1] == tseries.dtype: create_series_func = _fr_type_map[seriestype][2] create_sequence_func = _fr_type_map[seriestype][4] add_series_func = _fr_type_map[seriestype][5] break # add time series to frame series = create_series_func(channels[i], tseries.start_time, 0, tseries.delta_t, lal.ADCCountUnit, len(tseries.numpy())) series.data = create_sequence_func(len(tseries.numpy())) series.data.data = tseries.numpy() add_series_func(frame, series) # write frame lalframe.FrameWrite(frame, location)
def write( tsdict, outfile, start=None, end=None, type=None, name='gwpy', run=0, ): """Write data to a GWF file using the LALFrame API """ if not start: start = list(tsdict.values())[0].xspan[0] if not end: end = list(tsdict.values())[0].xspan[1] duration = end - start # get ifos list detectors = 0 _detidx = list(lalutils.LAL_DETECTORS.keys()) for series in tsdict.values(): ifo = getattr(series.channel, "ifo", None) try: idx = _detidx.index(ifo) except ValueError: # don't worry about mismatched detectors continue else: detectors |= 1 << 2 * idx # create new frame frame = lalframe.FrameNew(start, duration, name, run, 0, detectors) for series in tsdict.values(): # get type ctype = ( type or getattr(series.channel, "_ctype", "proc") or "proc" ).title() # convert to LAL lalseries = series.to_lal() # find adder add_ = lalutils.find_typed_function( series.dtype, 'FrameAdd', 'TimeSeries{}Data'.format(ctype), module=lalframe, ) # add time series to frame add_(frame, lalseries) # write frame lalframe.FrameWrite(frame, outfile)
def write_frame_files(self, fs, T_frame, comment, out_dir="."): """ Write frame files [1] containing strain time series of a continuous-wave signal. The strain time series is written as double-precision post-processed data (ProcData) channel named <tt><detector>:SIMCW-STRAIN</tt>, where <tt><detector></tt> is the 2-character detector prefix (e.g. <tt>H1</tt> for LIGO Hanford, <tt>L1</tt> for LIGO Livingston, <tt>V1</tt> for Virgo). @param fs: sampling frequency of strain time series, in Hz @param T_frame: length of each frame, in seconds; should divide evenly into @b T @param comment: frame file name comment, may only contain A-Z, a-z, 0-9, _, +, # characters @param out_dir: output directory to write frame files into @return (@b file, @b i, @b N), where: @b file = name of frame file just written; @b i = frame file index, starting from zero; @b N = number of frame files This is a Python generator function and so should be called as follows: ~~~ S = CWSimulator(...) for t, h, i, N in S.write_frame_files(...): ... ~~~ [1] https://dcc.ligo.org/LIGO-T970130/public """ try: import lalframe except ImportError: raise ImportError("SWIG wrappings of LALFrame cannot be imported") # check for valid frame filename comment (see LIGO-T010150) valid_comment = re.compile(r'^[A-Za-z0-9_+#]+$') if not valid_comment.match(comment): raise ValueError( "Frame file comment='%s' may only contain A-Z, a-z, 0-9, _, +, # characters" % comment) # generate strain time series in blocks of length 'T_frame' frame_h = None for t, h, i, N in self.get_strain_blocks(fs, T_frame): # create and initialise REAL8TimeSeries to write to frame files if frame_h is None: frame_h_channel = '%s:SIMCW-STRAIN' % lal.CachedDetectors[ self.__det_index].frDetector.prefix frame_h = lal.CreateREAL8TimeSeries(frame_h_channel, t, 0, 1.0 / fs, lal.DimensionlessUnit, len(h)) frame_h.epoch = t frame_h.data.data = h # create standard frame file name (see LIGO-T010150) frame_src = lal.CachedDetectors[ self.__det_index].frDetector.prefix[0] frame_desc = 'simCW_%s' % comment frame_t0 = int(t.gpsSeconds) frame_T = int(math.ceil(float(t + T_frame)) - math.floor(float(t))) frame_name = '%s-%s-%u-%u.gwf' % (frame_src, frame_desc, frame_t0, frame_T) frame_path = os.path.join(out_dir, frame_name) # create frame frame_det_bits = 2 * self.__det_index frame = lalframe.FrameNew(t, T_frame, "simCW", -1, i, frame_det_bits) # add strain time series to frame lalframe.FrameAddREAL8TimeSeriesProcData(frame, frame_h) # add history lalframe.FrameAddFrHistory(frame, "origin", self.__origin_str) # write frame lalframe.FrameWrite(frame, frame_path) # yield current file name for e.g. printing progress yield frame_path, i, N