def getRawData(channel, gps, dt): """Read data from RAW file: ch = channel name gps = gps time dt = duration """ global files # get the list of frame files obs = channel[0:2] if len(files) == 0: files = find_data_path(obs, gps, gps + dt) # read data from all files data = array([]) for f in files: # get the frame file start GPS and duration from the name gps0 = int(f.split('-')[-2]) gps1 = gps0 + int(f.split('-')[-1].split('.')[-2]) # find the right time segment to load from this file gps0 = max(gps0, gps) gps1 = min(gps1, gps + dt) # read data and append x = Fr.frgetvect(f, channel, gps0, gps1 - gps0) data = concatenate([data, x[0]]) return data, int(1 / x[3][0])
import pmns_utils import pmns_simsig import matplotlib matplotlib.rc('xtick', labelsize=16) matplotlib.rc('ytick', labelsize=16) matplotlib.rc('font', size=16) from matplotlib import pyplot as pl import matplotlib.cm as cm from mpl_toolkits.axes_grid1 import make_axes_locatable # # Load frame data # h1data = Fr.frgetvect('H-H1-871727590-513.gwf', 'H1:LSC-STRAIN', span=500) # # Put frame data in lal TimeSeries # noise = lal.CreateREAL8TimeSeries('H1', lal.LIGOTimeGPS(h1data[1]), 0, h1data[3][0], lal.StrainUnit, len(h1data[0])) noise.data.data = h1data[0] nonoise = lal.CreateREAL8TimeSeries('H1', lal.LIGOTimeGPS(h1data[1]), 0, h1data[3][0], lal.StrainUnit, len(h1data[0])) nonoise.data.data = np.zeros(len(h1data[0])) # # Generate a post-merger signal #
def readframedata(frameCache, channelName, frameType, startTime, stopTime, allowRedundantFlag, debugLevel): #READFRAMEDATA Read a single channel of data from frame files # #READFRAMEDATA finds and retrieves the requested time series data from a #set of frame files. The data is specified by the frame file type, #channel name, start time, and duration or stop time. The necessary frame #files are located using a file name caching scheme. #% #usage: [data, sampleFrequency, time] = ... #readframedata(frameCache, channelName, frameType, ... #startTime, stopTime, allowRedundantFlag, ... #debugLevel); #% #frameCache file name cache #channelName channel name #frameType frame file type #startTime GPS start time #stopTime GPS stop time (or duration) #allowRedundantFlag permit redundant frame data #debugLevel verboseness of debug output #% #data data vector #sampleFrequency sample frequency [Hz] #time time vector #% #READFRAMEDATA expects frame cache information in the format produced by #LOADFRAMECACHE which contains the site, frame type, duration, time, and #location of available frame data files. #% #The requested site designator is determined from the first character of #the requested channel name. The frame file type may contain wildcards. #Unless redundant frame data is permitted, it is an error if the same #frame file appears more than once in the frame cache file. If redundant #frame data is permitted, the first matching frame file from the cache is #used. It is always an error if two frame files overlap but do not cover #the same time range or differ in type. By default, redundant frame data #is permitted. #% #READFRAMEDATA retrieves data from the requested start time up to, but not #including, the requested stop time, such that stop minus start seconds #are retrieved. Alternatively, the desired duration in seconds may be #specified instead of the GPS stop time parameter. #% #The resulting time series is returned as two row vectors containing the #data sequence and the corresponding GPS timestamps, as well as the scalar #sample frequency. To protect against roundoff error, an integer sample #frequency is assumed. #% #If it is unable to load the requested data, READFRAMEDATA returns empty #result vectors and zero sample frequency as well as a warning if #debugLevel is set to 1 or higher. By default, a debugLevel of unity is #assumed. #% #READFRAMEDATA is built on top of the FRGETVECT function from the FrameL #library, which is available from the following URL. #% # #% # # #Shourov K. Chatterji <*****@*****.**> #Jameson Rollins <*****@*****.**> # #$Id: readframedata.m 2326 2009-09-21 08:37:42Z jrollins $ # # Rewritten in Python by Sudarshan Ghonge <*****@*****.**> # 2015-09-04 # # if specified stop time precedes start time, if(stopTime<startTime): # treat stop time as a duration stopTime = startTime + stopTime # determine site designator from channel name site = channelName[0] #if specified frame cache is invalid if(frameCache==None): if(debugLevel>=1): # Issue warning print 'Warning: Invalid frame cache' data = [] time = [] sampleFrequency = 0 return [data, sampleFrequency] # return empty results # Identifying matching segments from frame cache # find overlap of cache segments with requested data segmentStartTimes = np.maximum(startTime, frameCache.startTimes) segmentStopTimes = np.minimum(stopTime, frameCache.stopTimes) # identify cache segments which overlap requested times segments = np.where(segmentStopTimes > segmentStartTimes)[0] # if no segments overlap with requested times if(len(segments)==0): if debugLevel>=1: print 'Warning: No data available for [%d, %d]' %(startTime, stopTime) data = [] time = [] sampleFrequency = 0 # return empty results return [data, sampleFrequency] # otherwise, find overlapping segments else: # identify cache segments with requested site and frame type siteMatches = [] sitesScanned=0 for iSite in frameCache.sites[segments]: if site in iSite: siteMatches.append(sitesScanned) sitesScanned+=1 siteMatches = np.asarray(siteMatches) frameTypeMatches = [] frameTypesScanned=0 for iType in frameCache.frameTypes[segments]: if frameType in iType: frameTypeMatches.append(frameTypesScanned) frameTypesScanned+=1 frameTypeMatches = np.asarray(frameTypeMatches) segIdx = np.intersect1d(siteMatches, frameTypeMatches) if(len(segIdx)==0): segments = [] else: segments = segments[segIdx] # Identify available frame files # initialize list of available frame files frameFilePaths = [] frameFileTypes = [] frameFileStartTimes = [] frameFileStopTimes = [] # lopp over the matching segments for segment in segments: # frame type of the frame files in segment frameFileType = frameCache.frameTypes[segment] firstFrameFileStartTime = frameCache.startTimes[segment] + frameCache.durations[segment]*np.floor((segmentStartTimes[segment]-frameCache.startTimes[segment])/frameCache.durations[segment]) lastFrameFileStartTime = frameCache.startTimes[segment] + frameCache.durations[segment]*np.ceil((segmentStopTimes[segment] - frameCache.startTimes[segment])/frameCache.durations[segment] - 1) for frameFileStartTime in np.arange(firstFrameFileStartTime, lastFrameFileStartTime + frameCache.durations[segment], frameCache.durations[segment]): frameFileStopTime = frameFileStartTime + frameCache.durations[segment] frameFilePath = frameCache.directories[segment] + '/' + frameCache.sites[segment] + '-' + frameCache.frameTypes[segment] + '-' + '%09d' %(frameFileStartTime) + '-' + '%d' %(frameCache.durations[segment]) + '.gwf' # In case the frame file durations start becoming variable. The ls <name> with a '*' in place of the duration field # returns the file name. we then use that file name import os.path if(not os.path.isfile(frameFilePath)): frameFilePath = frameCache.directories[segment] + '/' + frameCache.sites[segment] +'-' + frameCache.frameTypes[segment] + '-' + '%010d' %(frameFileStartTime) + '-' + '*'+ '.gwf' frameFilePath = os.popen('ls %s'%(frameFilePath)).readlines()[0].split('\n')[0] if(os.path.isfile(frameFilePath)): frameFilePaths.append(frameFilePath) frameFileTypes.append(frameFileType) frameFileStartTimes.append(frameFileStartTime) frameFileStopTimes.append(frameFileStopTime) frameFilePaths = np.asarray(frameFilePaths) frameFileTypes = np.asarray(frameFileTypes) frameFileStartTimes = np.asarray(frameFileStartTimes) frameFileStopTimes = np.asarray(frameFileStopTimes) numberOfFrameFiles = len(frameFilePaths) keepFrameFileNumbers = [] for frameFileNumber in range(numberOfFrameFiles): keepFrameFileFlag = True for previousFrameFileNumber in range(frameFileNumber): overlapStartTime = np.maximum(frameFileStartTimes[frameFileNumber], frameFileStartTimes[previousFrameFileNumber]) overlapStoptime = np.minimum(frameFileStopTimes[frameFileNumber], frameFileStopTimes[previousFrameFileNumber]) if (overlapStartTime < overlapStoptime): if(allowRedundantFlag): if((frameFileStartTimes[frameFileNumber]==frameFileStartTimes[previousFrameFileNumber]) & (frameFileStopTimes[frameFileNumber] == frameFileStopTimes[previousFrameFileNumber]) & (frameFileTypes[frameFileNumber]==frameFileTypes[previousFrameFileNumber])): keepFrameFileFlag = False continue else: if(debugLevel>=1): print 'Warning: Overlapping but dissimilar frame files %s and %s.' %(frameFilePaths[frameFileNumber], frameFilePaths[previousFrameFileNumber]) data = [] time = [] sampleFrequency=0 return [data, sampleFrequency] else: if(debugLevel>=1): print 'Warning: Redundant frame files %s and %s.' %(frameFilePaths[frameFileNumber], frameFilePaths[previousFrameFileNumber]) data = [] time = [] sampleFrequency = 0 return [data, sampleFrequency] if(keepFrameFileFlag): keepFrameFileNumbers.append(frameFileNumber) keepFrameFileNumbers = np.asarray(keepFrameFileNumbers) frameFilePaths = frameFilePaths[keepFrameFileNumbers] frameFileTypes = frameFileTypes[keepFrameFileNumbers] frameFileStartTimes = frameFileStartTimes[keepFrameFileNumbers] frameFileStopTimes = frameFileStopTimes[keepFrameFileNumbers] sortedIndices = np.argsort(frameFileStartTimes) frameFilePaths = frameFilePaths[sortedIndices] frameFiletypes = frameFileTypes[sortedIndices] frameFileStartTimes = frameFileStartTimes[sortedIndices] frameFileStopTimes = frameFileStopTimes[sortedIndices] continuityStartTimes = np.append(np.maximum(startTime, frameFileStartTimes), stopTime) continuityStopTimes = np.append(startTime, np.minimum(stopTime, frameFileStopTimes)) discontinuities = np.where(continuityStartTimes!=continuityStopTimes)[0] if(len(discontinuities)>0): if(debugLevel >=1): print 'Warning: Missing %s '%(channelName), frameType[1:len(frameType)-1], ' data at ' , '%d'%(np.round(continuityStopTimes[discontinuities[0]])), '.' data = [] time = [] sampleFrequency = 0 return [data, sampleFrequency] data = np.array([]) time = np.array([]) sampleFrequency = None numberOfFrameFiles = len(frameFilePaths) for frameFileNumber in range(numberOfFrameFiles): frameFilePath = frameFilePaths[frameFileNumber] if(debugLevel>=2): print 'Reading %s...\n' %(frameFilePath) frameFileStartTime = frameFileStartTimes[frameFileNumber] frameFileStopTime = frameFileStopTimes[frameFileNumber] readData = [] readStartTime = np.maximum(startTime, frameFileStartTime) readDuration = np.minimum(stopTime, frameFileStopTime) - readStartTime realChannelName = channelName try: outputStruct = Fr.frgetvect(frameFilePath, realChannelName, readStartTime, readDuration, False) readData = outputStruct[0] readTime = outputStruct[2] readSampleFrequency = 1.0/outputStruct[3][0] readTimeStep = outputStruct[3][0] readGPS = outputStruct[1] except Exception as inst: if(debugLevel>=2): print inst.message if((len(readData)==0) | np.any(np.isnan(readData))): if(debugLevel>=1): print 'Warning: Error reading %s from %s.' %(channelName, frameFilePath) data = [] time = [] sampleFrequency = 0 return [data, sampleFrequency] if(sampleFrequency==None): sampleFrequency = readSampleFrequency elif(sampleFrequency!=readSampleFrequency): if(debugLevel>=1): print 'Warning: Inconsistent sample frequency for %s in frameFilePath.' %(channelname, frameFilePath) data = [] time = [] sampleFrequency = 0 return [data, sampleFrequency] data = np.append(data, readData) return [data, sampleFrequency]
#!/usr/bin/env python from pylal import Fr h_frame = 'H-H1_magA_tapered_1105199095-946076460-512.gwf' l_frame = 'L-L1_magA_tapered_1105199095-946076460-512.gwf' gps_start = 946076563.480737805 ra = 1.39 dec = -0.93 h_data = Fr.frgetvect(h_frame, 'H1:STRAIN', start=gps_start, span=1) l_data = Fr.frgetvect(l_frame, 'L1:STRAIN', start=gps_start, span=1)
def readframedata(frameCache, channelName, frameType, startTime, stopTime, allowRedundantFlag, debugLevel): #READFRAMEDATA Read a single channel of data from frame files # #READFRAMEDATA finds and retrieves the requested time series data from a #set of frame files. The data is specified by the frame file type, #channel name, start time, and duration or stop time. The necessary frame #files are located using a file name caching scheme. #% #usage: [data, sampleFrequency, time] = ... #readframedata(frameCache, channelName, frameType, ... #startTime, stopTime, allowRedundantFlag, ... #debugLevel); #% #frameCache file name cache #channelName channel name #frameType frame file type #startTime GPS start time #stopTime GPS stop time (or duration) #allowRedundantFlag permit redundant frame data #debugLevel verboseness of debug output #% #data data vector #sampleFrequency sample frequency [Hz] #time time vector #% #READFRAMEDATA expects frame cache information in the format produced by #LOADFRAMECACHE which contains the site, frame type, duration, time, and #location of available frame data files. #% #The requested site designator is determined from the first character of #the requested channel name. The frame file type may contain wildcards. #Unless redundant frame data is permitted, it is an error if the same #frame file appears more than once in the frame cache file. If redundant #frame data is permitted, the first matching frame file from the cache is #used. It is always an error if two frame files overlap but do not cover #the same time range or differ in type. By default, redundant frame data #is permitted. #% #READFRAMEDATA retrieves data from the requested start time up to, but not #including, the requested stop time, such that stop minus start seconds #are retrieved. Alternatively, the desired duration in seconds may be #specified instead of the GPS stop time parameter. #% #The resulting time series is returned as two row vectors containing the #data sequence and the corresponding GPS timestamps, as well as the scalar #sample frequency. To protect against roundoff error, an integer sample #frequency is assumed. #% #If it is unable to load the requested data, READFRAMEDATA returns empty #result vectors and zero sample frequency as well as a warning if #debugLevel is set to 1 or higher. By default, a debugLevel of unity is #assumed. #% #READFRAMEDATA is built on top of the FRGETVECT function from the FrameL #library, which is available from the following URL. #% # #% # # #Shourov K. Chatterji <*****@*****.**> #Jameson Rollins <*****@*****.**> # #$Id: readframedata.m 2326 2009-09-21 08:37:42Z jrollins $ # # Rewritten in Python by Sudarshan Ghonge <*****@*****.**> # 2015-09-04 # # if specified stop time precedes start time, if (stopTime < startTime): # treat stop time as a duration stopTime = startTime + stopTime # determine site designator from channel name site = channelName[0] #if specified frame cache is invalid if (frameCache == None): if (debugLevel >= 1): # Issue warning print 'Warning: Invalid frame cache' data = [] time = [] sampleFrequency = 0 return [data, sampleFrequency] # return empty results # Identifying matching segments from frame cache # find overlap of cache segments with requested data segmentStartTimes = np.maximum(startTime, frameCache.startTimes) segmentStopTimes = np.minimum(stopTime, frameCache.stopTimes) # identify cache segments which overlap requested times segments = np.where(segmentStopTimes > segmentStartTimes)[0] # if no segments overlap with requested times if (len(segments) == 0): if debugLevel >= 1: print 'Warning: No data available for [%d, %d]' % (startTime, stopTime) data = [] time = [] sampleFrequency = 0 # return empty results return [data, sampleFrequency] # otherwise, find overlapping segments else: # identify cache segments with requested site and frame type siteMatches = [] sitesScanned = 0 for iSite in frameCache.sites[segments]: if site in iSite: siteMatches.append(sitesScanned) sitesScanned += 1 siteMatches = np.asarray(siteMatches) frameTypeMatches = [] frameTypesScanned = 0 for iType in frameCache.frameTypes[segments]: if frameType in iType: frameTypeMatches.append(frameTypesScanned) frameTypesScanned += 1 frameTypeMatches = np.asarray(frameTypeMatches) segIdx = np.intersect1d(siteMatches, frameTypeMatches) if (len(segIdx) == 0): segments = [] else: segments = segments[segIdx] # Identify available frame files # initialize list of available frame files frameFilePaths = [] frameFileTypes = [] frameFileStartTimes = [] frameFileStopTimes = [] # lopp over the matching segments for segment in segments: # frame type of the frame files in segment frameFileType = frameCache.frameTypes[segment] firstFrameFileStartTime = frameCache.startTimes[ segment] + frameCache.durations[segment] * np.floor( (segmentStartTimes[segment] - frameCache.startTimes[segment]) / frameCache.durations[segment]) lastFrameFileStartTime = frameCache.startTimes[ segment] + frameCache.durations[segment] * np.ceil( (segmentStopTimes[segment] - frameCache.startTimes[segment]) / frameCache.durations[segment] - 1) for frameFileStartTime in np.arange( firstFrameFileStartTime, lastFrameFileStartTime + frameCache.durations[segment], frameCache.durations[segment]): frameFileStopTime = frameFileStartTime + frameCache.durations[ segment] frameFilePath = frameCache.directories[ segment] + '/' + frameCache.sites[ segment] + '-' + frameCache.frameTypes[ segment] + '-' + '%09d' % ( frameFileStartTime) + '-' + '%d' % ( frameCache.durations[segment]) + '.gwf' # In case the frame file durations start becoming variable. The ls <name> with a '*' in place of the duration field # returns the file name. we then use that file name import os.path if (not os.path.isfile(frameFilePath)): frameFilePath = frameCache.directories[ segment] + '/' + frameCache.sites[ segment] + '-' + frameCache.frameTypes[ segment] + '-' + '%010d' % ( frameFileStartTime) + '-' + '*' + '.gwf' frameFilePath = os.popen( 'ls %s' % (frameFilePath)).readlines()[0].split('\n')[0] if (os.path.isfile(frameFilePath)): frameFilePaths.append(frameFilePath) frameFileTypes.append(frameFileType) frameFileStartTimes.append(frameFileStartTime) frameFileStopTimes.append(frameFileStopTime) frameFilePaths = np.asarray(frameFilePaths) frameFileTypes = np.asarray(frameFileTypes) frameFileStartTimes = np.asarray(frameFileStartTimes) frameFileStopTimes = np.asarray(frameFileStopTimes) numberOfFrameFiles = len(frameFilePaths) keepFrameFileNumbers = [] for frameFileNumber in range(numberOfFrameFiles): keepFrameFileFlag = True for previousFrameFileNumber in range(frameFileNumber): overlapStartTime = np.maximum( frameFileStartTimes[frameFileNumber], frameFileStartTimes[previousFrameFileNumber]) overlapStoptime = np.minimum( frameFileStopTimes[frameFileNumber], frameFileStopTimes[previousFrameFileNumber]) if (overlapStartTime < overlapStoptime): if (allowRedundantFlag): if ((frameFileStartTimes[frameFileNumber] == frameFileStartTimes[previousFrameFileNumber]) & (frameFileStopTimes[frameFileNumber] == frameFileStopTimes[previousFrameFileNumber]) & (frameFileTypes[frameFileNumber] == frameFileTypes[previousFrameFileNumber])): keepFrameFileFlag = False continue else: if (debugLevel >= 1): print 'Warning: Overlapping but dissimilar frame files %s and %s.' % ( frameFilePaths[frameFileNumber], frameFilePaths[previousFrameFileNumber]) data = [] time = [] sampleFrequency = 0 return [data, sampleFrequency] else: if (debugLevel >= 1): print 'Warning: Redundant frame files %s and %s.' % ( frameFilePaths[frameFileNumber], frameFilePaths[previousFrameFileNumber]) data = [] time = [] sampleFrequency = 0 return [data, sampleFrequency] if (keepFrameFileFlag): keepFrameFileNumbers.append(frameFileNumber) keepFrameFileNumbers = np.asarray(keepFrameFileNumbers) frameFilePaths = frameFilePaths[keepFrameFileNumbers] frameFileTypes = frameFileTypes[keepFrameFileNumbers] frameFileStartTimes = frameFileStartTimes[keepFrameFileNumbers] frameFileStopTimes = frameFileStopTimes[keepFrameFileNumbers] sortedIndices = np.argsort(frameFileStartTimes) frameFilePaths = frameFilePaths[sortedIndices] frameFiletypes = frameFileTypes[sortedIndices] frameFileStartTimes = frameFileStartTimes[sortedIndices] frameFileStopTimes = frameFileStopTimes[sortedIndices] continuityStartTimes = np.append( np.maximum(startTime, frameFileStartTimes), stopTime) continuityStopTimes = np.append(startTime, np.minimum(stopTime, frameFileStopTimes)) discontinuities = np.where(continuityStartTimes != continuityStopTimes)[0] if (len(discontinuities) > 0): if (debugLevel >= 1): print 'Warning: Missing %s ' % (channelName), frameType[ 1:len(frameType) - 1], ' data at ', '%d' % (np.round( continuityStopTimes[discontinuities[0]])), '.' data = [] time = [] sampleFrequency = 0 return [data, sampleFrequency] data = np.array([]) time = np.array([]) sampleFrequency = None numberOfFrameFiles = len(frameFilePaths) for frameFileNumber in range(numberOfFrameFiles): frameFilePath = frameFilePaths[frameFileNumber] if (debugLevel >= 2): print 'Reading %s...\n' % (frameFilePath) frameFileStartTime = frameFileStartTimes[frameFileNumber] frameFileStopTime = frameFileStopTimes[frameFileNumber] readData = [] readStartTime = np.maximum(startTime, frameFileStartTime) readDuration = np.minimum(stopTime, frameFileStopTime) - readStartTime realChannelName = channelName try: outputStruct = Fr.frgetvect(frameFilePath, realChannelName, readStartTime, readDuration, False) readData = outputStruct[0] readTime = outputStruct[2] readSampleFrequency = 1.0 / outputStruct[3][0] readTimeStep = outputStruct[3][0] readGPS = outputStruct[1] except Exception as inst: if (debugLevel >= 2): print inst.message if ((len(readData) == 0) | np.any(np.isnan(readData))): if (debugLevel >= 1): print 'Warning: Error reading %s from %s.' % (channelName, frameFilePath) data = [] time = [] sampleFrequency = 0 return [data, sampleFrequency] if (sampleFrequency == None): sampleFrequency = readSampleFrequency elif (sampleFrequency != readSampleFrequency): if (debugLevel >= 1): print 'Warning: Inconsistent sample frequency for %s in frameFilePath.' % ( channelname, frameFilePath) data = [] time = [] sampleFrequency = 0 return [data, sampleFrequency] data = np.append(data, readData) return [data, sampleFrequency]
virgo_fs = 20000.0 f, ax = pl.subplots(nrows=2,ncols=1) for s,site in enumerate(sites): start=float(trigger_time)-0.5*datalen if site=='V': delta_t = 1/virgo_fs else: delta_t = 1/ligo_fs # Stick these in pycbc time series and we'll have an easy time computing # matches etc later bmdc_data[site] = pycbc.types.TimeSeries(Fr.frgetvect(burstmdc_frame, burstmdc_channels[s], start=start, span=datalen)[0], delta_t=delta_t, epoch=start) lalsim_data[site] = pycbc.types.TimeSeries(Fr.frgetvect(lalsim_frames[s], lalsim_channels[s], start=start, span=datalen)[0], delta_t=1/ligo_fs, epoch=start) # Plot burstMDC channels ax[0].plot(bmdc_data[site].sample_times-float(trigger_time), bmdc_data[site], label=burstmdc_channels[s]) ax[0].set_xlabel('Seconds after %s'%trigger_time) ax[0].legend() ax[0].set_title('BurstMDC') # Plot lalsim channels