def test_tbn_rate(self): """Test finding out the sample rate of a TBN file.""" fh = open(tbnFile, 'rb') rate = tbn.get_sample_rate(fh) self.assertEqual(rate, 100000) code = tbn.get_sample_rate(fh, filter_code=True) self.assertEqual(code, 7) fh.close()
def main(args): fh = open(args[0], "rb", buffering=tbn.FRAME_SIZE * 10000) # Get the first frame and find out what the firt time tag is, which the # first frame number is, and what the sample rate it. From the sample # rate, estimate how the time tag should advance between frames. junkFrame = tbn.read_frame(fh) sample_rate = tbn.get_sample_rate(fh) tagSkip = fS // sample_rate * junkFrame.payload.data.shape[0] fh.seek(0) # Store the information about the first frame. prevTime = junkFrame.payload.timetag prevDate = junkFrame.time.datetime prevFrame = junkFrame.header.frame_count # Report on the file print("Filename: %s" % os.path.basename(args[0])) print("Date of first frame: %i -> %s" % (prevTime, str(prevDate))) print("Sample rate: %i Hz" % sample_rate) print("Time tag skip per frame: %i" % tagSkip) k = 0 while True: try: currFrame = tbn.read_frame(fh) except errors.EOFError: break except errors.SyncError: continue stand, pol = currFrame.id currTime = currFrame.payload.timetag currDate = currFrame.time.datetime currFrame = currFrame.header.frame_count if k == 0 or (currFrame % 5000 == 0 and stand == 1 and pol == 0): print("At stand %i, pol %i: frame %i -> %s" % (stand, pol, currFrame, currDate)) if currTime < prevTime: print("ERROR: t.t. %i @ frame %i < t.t. %i @ frame %i" % (currTime, currFrame, prevTime, prevFrame)) print(" -> difference: %i (%.5f seconds); %s" % (currTime - prevTime, float(currTime - prevTime) / fS, str(currDate))) if (currTime - prevTime) > tagSkip: print("ERROR: t.t. %i @ frame %i > t.t. %i @ frame %i + skip" % (currTime, currFrame, prevTime, prevFrame)) print(" -> difference: %i (%.5f seconds); %s" % (currTime - prevTime, float(currTime - prevTime) / fS, str(currDate))) prevTime = currTime prevFrame = currFrame k = k + 1 fh.close()
def main(args): # The task at hand filename = args.filename # The station if args.metadata is not None: site = parse_ssmif(args.metadata) ssmifContents = open(args.metadata).readlines() else: site = lwa1 ssmifContents = open(os.path.join(dataPath, 'lwa1-ssmif.txt')).readlines() observer = site.get_observer() antennas = site.antennas # The file's parameters fh = open(filename, 'rb') nFramesFile = os.path.getsize(filename) // tbn.FRAME_SIZE srate = tbn.get_sample_rate(fh) antpols = len(antennas) fh.seek(0) if srate < 1000: fh.seek(len(antennas) * 4 * tbn.FRAME_SIZE) srate = tbn.get_sample_rate(fh) antpols = len(antennas) fh.seek(len(antennas) * 4 * tbn.FRAME_SIZE) # Reference antenna ref = args.reference foundRef = False for i, a in enumerate(antennas): if a.stand.id == ref and a.pol == 0: refX = i foundRef = True elif a.stand.id == ref and a.pol == 1: refY = i else: pass if not foundRef: raise RuntimeError("Cannot file Stand #%i" % ref) # Integration time (seconds and frames) tInt = args.average nFrames = int(round(tInt * srate / 512 * antpols)) tInt = nFrames / antpols * 512 / srate # Total run length nChunks = int(1.0 * nFramesFile / antpols * 512 / srate / tInt) # Read in the first frame and get the date/time of the first sample # of the frame. This is needed to get the list of stands. junkFrame = tbn.read_frame(fh) fh.seek(-tbn.FRAME_SIZE, 1) startFC = junkFrame.header.frame_count try: central_freq = junkFrame.central_freq except AttributeError: from lsl.common.dp import fS central_freq = fS * junkFrame.header.second_count / 2**32 beginDate = junkFrame.time.datetime observer.date = beginDate srcs = [ ephem.Sun(), ] for line in _srcs: srcs.append(ephem.readdb(line)) for i in xrange(len(srcs)): srcs[i].compute(observer) if srcs[i].alt > 0: print("source %s: alt %.1f degrees, az %.1f degrees" % (srcs[i].name, srcs[i].alt * 180 / numpy.pi, srcs[i].az * 180 / numpy.pi)) # File summary print("Filename: %s" % filename) print("Date of First Frame: %s" % str(beginDate)) print("Ant/Pols: %i" % antpols) print("Sample Rate: %i Hz" % srate) print("Tuning Frequency: %.3f Hz" % central_freq) print("Frames: %i (%.3f s)" % (nFramesFile, 1.0 * nFramesFile / antpols * 512 / srate)) print("---") print("Integration: %.3f s (%i frames; %i frames per stand/pol)" % (tInt, nFrames, nFrames // antpols)) print("Chunks: %i" % nChunks) # Create the FrameBuffer instance buffer = TBNFrameBuffer(stands=range(1, antpols // 2 + 1), pols=[0, 1]) # Create the phase average and times LFFT = 512 times = numpy.zeros(nChunks, dtype=numpy.float64) simpleVis = numpy.zeros((nChunks, antpols), dtype=numpy.complex64) central_freqs = numpy.zeros(nChunks, dtype=numpy.float64) # Go! k = 0 for i in xrange(nChunks): # Find out how many frames remain in the file. If this number is larger # than the maximum of frames we can work with at a time (maxFrames), # only deal with that chunk framesRemaining = nFramesFile - k if framesRemaining > nFrames: framesWork = nFrames data = numpy.zeros((antpols, framesWork // antpols * 512), dtype=numpy.complex64) else: framesWork = framesRemaining + antpols * buffer.nsegments data = numpy.zeros((antpols, framesWork // antpols * 512), dtype=numpy.complex64) print("Working on chunk %i, %i frames remaining" % (i + 1, framesRemaining)) count = [0 for a in xrange(len(antennas))] j = 0 fillsWork = framesWork // antpols # Inner loop that actually reads the frames into the data array done = False while j < fillsWork: cFrames = deque() for l in xrange(len(antennas)): try: cFrames.append(tbn.read_frame(fh)) k = k + 1 except errors.EOFError: ## Exit at the EOF done = True break except errors.SyncError: #print("WARNING: Mark 5C sync error on frame #%i" % (int(fh.tell())/tbn.FRAME_SIZE-1)) ## Exit at the first sync error done = True break buffer.append(cFrames) cFrames = buffer.get() if cFrames is None: continue for cFrame in cFrames: stand, pol = cFrame.header.id # In the current configuration, stands start at 1 and go up to 260. So, we # can use this little trick to populate the data array aStand = 2 * (stand - 1) + pol # Save the time if j == 0 and aStand == 0: times[i] = cFrame.time try: central_freqs[i] = cFrame.central_freq except AttributeError: central_freqs[ i] = fS * cFrame.header.second_count / 2**32 if i > 0: if central_freqs[i] != central_freqs[i - 1]: print( "Frequency change from %.3f to %.3f MHz at chunk %i" % (central_freqs[i - 1] / 1e6, central_freqs[i] / 1e6, i + 1)) data[aStand, count[aStand] * 512:(count[aStand] + 1) * 512] = cFrame.payload.data # Update the counters so that we can average properly later on count[aStand] = count[aStand] + 1 j += 1 if done: break if done: break # Time-domain blanking and cross-correlation with the outlier simpleVis[i, :] = fringe.Simple(data, refX, refY, args.clip) fh.close() # Save the data outname = os.path.split(filename)[1] outname = os.path.splitext(outname)[0] outname = "%s-ref%03i-multi-vis.npz" % (outname, args.reference) numpy.savez(outname, ref=ref, refX=refX, refY=refY, tInt=tInt, central_freqs=central_freqs, times=times, simpleVis=simpleVis, ssmifContents=ssmifContents)
def main(args): filename = args.filename sizeB = os.path.getsize(filename) # Open the file and get some basic info about the data contained fh = open(filename, 'rb') sample_rate = tbn.get_sample_rate(fh) nFramesX, nFramesY = tbn.get_frames_per_obs(fh) nCaptures = sizeB // tbn.FRAME_SIZE // (nFramesX + nFramesY) print("Filename: %s" % filename) print("Size: %.1f MB" % (float(sizeB) / 1024 / 1024)) print("Captures: %i (%.2f seconds)" % (nCaptures, nCaptures * 512 / sample_rate)) print("Stands: %i (%i x pol., %i y pol.)" % ((nFramesX + nFramesY), nFramesX, nFramesY)) print("Sample Rate: %.2f kHz" % (sample_rate / 1000.0)) print("===") if args.count > 0: nCaptures = args.count * sample_rate // 512 else: nCaptures -= args.offset * sample_rate // 512 args.count = nCaptures * 512 // sample_rate nSkip = int(args.offset * sample_rate / 512) print("Seconds to Skip: %.2f (%i captures)" % (args.offset, nSkip)) print("Seconds to Split: %.2f (%i captures)" % (args.count, nCaptures)) # Make sure that the first frame in the file is the first frame if a capture # (stand 1, pol 0). If not, read in as many frames as necessary to get to # the beginning of a complete capture. frame = tbn.read_frame(fh) stand, pol = frame.id skip = 0 while (2 * (stand - 1) + pol) != 0: frame = tbn.read_frame(fh) stand, pol = frame.id skip += 1 fh.seek(fh.tell() - tbn.FRAME_SIZE) if skip != 0: print("Skipped %i frames at the beginning of the file" % skip) for c in list(range(nSkip)): if c < nSkip: fh.seek(fh.tell() + tbn.FRAME_SIZE * (nFramesX + nFramesY)) continue nFramesRemaining = (sizeB - fh.tell()) // tbn.FRAME_SIZE nRecursions = int(nFramesRemaining // (nCaptures * (nFramesX + nFramesY))) if not args.recursive: nRecursions = 1 scale = int(math.log10(nRecursions)) + 1 ifString = "Working on #%%%ii of %i (%%s)" % (scale, nRecursions) for r in range(nRecursions): if args.date: filePos = fh.tell() junkFrame = tbn.read_frame(fh) fh.seek(filePos) dt = junkFrame.time.datetime captFilename = "%s_%s.dat" % (os.path.splitext( os.path.basename(filename))[0], dt.isoformat()) else: captFilename = "%s_s%04i_p%%0%ii.dat" % (os.path.splitext( os.path.basename(filename))[0], args.count, scale) captFilename = captFilename % r if not args.recursive: captFilename = "%s_s%04i.dat" % (os.path.splitext( os.path.basename(filename))[0], args.count) print(ifString % (r + 1, captFilename)) t0 = time.time() fhOut = open(captFilename, 'wb') split_file(fh, fhOut, nCaptures, nFramesX + nFramesY) fhOut.close() t1 = time.time() print(" Copied %i bytes in %.3f s (%.3f MB/s)" % (os.path.getsize(captFilename), t1 - t0, os.path.getsize(captFilename) / 1024.0**2 / (t1 - t0))) fh.close()
def main(args): # Set the station if args.lwasv: station = stations.lwasv else: station = stations.lwa1 antennas = station.antennas fh = open(args.filename, "rb", buffering=tbn.FRAME_SIZE*10000) # Get the first frame and find out what the firt time tag is, which the # first frame number is, and what the sample rate it. From the sample # rate, estimate how the time tag should advance between frames. junkFrame = tbn.read_frame(fh) sample_rate = tbn.get_sample_rate(fh) antpols = len(antennas) tagSkip = fS // sample_rate * junkFrame.payload.data.shape[0] fh.seek(0) # Store the information about the first frame. prevTime = junkFrame.payload.timetag prevDate = junkFrame.time.datetime prevFrame = junkFrame.header.frame_count # Report on the file print("Filename: %s" % os.path.basename(args.filename)) print("Date of first frame: %i -> %s" % (prevTime, str(prevDate))) print("Sample rate: %i Hz" % sample_rate) print("Time tag skip per frame: %i" % tagSkip) # Create the FrameBuffer instance buffer = TBNFrameBuffer(stands=range(1,antpols//2+1), pols=[0, 1]) j = 0 k = 0 while True: try: cFrame = tbn.read_frame(fh) k += 1 except errors.EOFError: break except errors.SyncError: #print("WARNING: Mark 5C sync error on frame #%i" % (int(fh.tell())/tbn.FRAME_SIZE-1)) continue buffer.append(cFrame) cFrames = buffer.get() if cFrames is None: continue valid = reduce(lambda x,y: x+int(y.valid), cFrames, 0) if valid != antpols: print("WARNING: frame count %i at %i missing %.2f%% of frames" % (cFrames[0].header.frame_count, cFrames[0].payload.timetag, float(antpols - valid)/antpols*100)) timetags = numpy.zeros(len(cFrames), dtype=numpy.int64) - 1 for cFrame in cFrames: stand,pol = cFrame.id timetags[2*(stand-1)+pol] = cFrame.payload.timetag if j == 0: prevTime = numpy.median(timetags) prevDate = cFrames[0].time.datetime prevFrame = cFrames[0].header.frame_count j += 1 continue else: currTime = numpy.median(timetags) currDate = cFrames[0].time.datetime currFrame = cFrames[0].header.frame_count if currFrame % 1000 == 0: print("At frame %i t.t. is %i -> %s" % (currFrame, currTime, currDate)) if currTime < prevTime: print("ERROR: t.t. %i @ frame %i < t.t. %i @ frame %i" % (currTime, currFrame, prevTime, prevFrame)) print(" -> difference: %i (%.5f seconds); %s" % (currTime-prevTime, float(currTime-prevTime)/fS, str(currDate))) if (currTime-prevTime) > tagSkip: print("ERROR: t.t. %i @ frame %i > t.t. %i @ frame %i + skip" % (currTime, currFrame, prevTime, prevFrame)) print(" -> difference: %i (%.5f seconds); %s" % (currTime-prevTime, float(currTime-prevTime)/fS, str(currDate))) for i in xrange(timetags.size): if timetags[i] != currTime: print("ERROR: t.t. of dig. %i != frame set median of %i" % (i, currTime)) print(" -> difference: %i" % (currTime-timetags[i],)) prevTime = currTime prevData = currDate prevFrame = currFrame j += 1
def main(args): # The task at hand clnfile = args[0] az = float(args[1]) el = float(args[2]) filename = args[3] # The station observer = lwa1.get_observer() antennas = lwa1.antennas # The file's parameters fh = open(filename, 'rb') nFramesFile = os.path.getsize(filename) / tbn.FRAME_SIZE srate = tbn.get_sample_rate(fh) antpols = len(antennas) # Reference antenna ref = 258 for a in antennas: if a.stand.id == ref and a.pol == 0: refX = a.digitizer elif a.stand.id == ref and a.pol == 1: refY = a.digitizer else: pass # Integration time (seconds and frames) tInt = 5.0 nFrames = int(round(tInt * srate / 512 * antpols)) tInt = nFrames / antpols * 512 / srate # Total run length #nChunks = int(round(1.0*nFramesFile / nFrames)) nChunks = 240 # Read in the first frame and get the date/time of the first sample # of the frame. This is needed to get the list of stands. junkFrame = tbn.read_frame(fh) fh.seek(-tbn.FRAME_SIZE, 1) startFC = junkFrame.header.frame_count central_freq = junkFrame.central_freq beginDate = junkFrame.time.datetime observer.date = beginDate srcs = [] for line in _srcs: srcs.append(ephem.readdb(line)) srcs[-1].compute(observer) if srcs[-1].alt > 0: print("source %s: alt %.1f degrees, az %.1f degrees" % (srcs[-1].name, srcs[-1].alt * 180 / numpy.pi, srcs[-1].az * 180 / numpy.pi)) # File summary print("Filename: %s" % filename) print("Date of First Frame: %s" % str(beginDate)) print("Ant/Pols: %i" % antpols) print("Sample Rate: %i Hz" % srate) print("Tuning Frequency: %.3f Hz" % central_freq) print("Frames: %i (%.3f s)" % (nFramesFile, 1.0 * nFramesFile / antpols * 512 / srate)) print("---") print("Integration: %.3f s (%i frames; %i frames per stand/pol)" % (tInt, nFrames, nFrames / antpols)) print("Chunks: %i" % nChunks) junkFrame = tbn.read_frame(fh) while junkFrame.header.frame_count < startFC + 3: junkFrame = tbn.read_frame(fh) fh.seek(-tbn.FRAME_SIZE, 1) # Get the beamformer coefficients - three sets: # (1) at the requested az, el # (2) at az, el - 15 degrees # (3) at the transit location of Cyg A dataDict = numpy.load(clnfile) cln = dataDict['cln'] aln1 = [] aln2 = [] aln3 = [] for i in xrange(cln.shape[1]): gd = getGeoDelay(antennas[i], az, el, central_freq, Degrees=True) aln1.append(numpy.exp(2j * numpy.pi * central_freq * gd)) gd = getGeoDelay(antennas[i], az, el - 15, central_freq, Degrees=True) aln2.append(numpy.exp(2j * numpy.pi * central_freq * gd)) gd = getGeoDelay(antennas[i], 0.5, 83.3, central_freq, Degrees=True) aln3.append(numpy.exp(2j * numpy.pi * central_freq * gd)) aln1 = numpy.array(aln1) aln2 = numpy.array(aln2) aln3 = numpy.array(aln3) bln1 = (cln * aln1).conj() / numpy.abs(cln * aln1) bln2 = (cln * aln2).conj() / numpy.abs(cln * aln2) bln3 = (cln * aln3).conj() / numpy.abs(cln * aln3) for i in xrange(cln.shape[1]): if antennas[i].combined_status != 33 or antennas[i].stand.id == ref: bln1[:, i] = 0.0 bln2[:, i] = 0.0 bln3[:, i] = 0.0 # Create the FrameBuffer instance buffer = TBNFrameBuffer(stands=range(1, antpols / 2 + 1), pols=[0, 1], reorder=False) # Create the beam times = numpy.zeros(nChunks, dtype=numpy.float64) beam1 = numpy.zeros((nChunks, 2), dtype=numpy.float64) beam2 = numpy.zeros((nChunks, 2), dtype=numpy.float64) beam3 = numpy.zeros((nChunks, 2), dtype=numpy.float64) # Go! k = 0 for i in xrange(nChunks): # Find out how many frames remain in the file. If this number is larger # than the maximum of frames we can work with at a time (maxFrames), # only deal with that chunk framesRemaining = nFramesFile - k if framesRemaining > nFrames: framesWork = nFrames data = numpy.zeros((antpols, framesWork / antpols * 512), dtype=numpy.complex64) else: framesWork = framesRemaining + antpols * buffer.nsegments data = numpy.zeros((antpols, framesWork / antpols * 512), dtype=numpy.complex64) print("Working on chunk %i, %i frames remaining" % (i + 1, framesRemaining)) count = [0 for a in xrange(antpols)] j = 0 fillsWork = framesWork / antpols # Inner loop that actually reads the frames into the data array while j < fillsWork: try: cFrame = tbn.read_frame(fh) k = k + 1 except errors.EOFError: break except errors.SyncError: #print("WARNING: Mark 5C sync error on frame #%i" % (int(fh.tell())/tbn.FRAME_SIZE-1)) continue buffer.append(cFrame) cFrames = buffer.get() if cFrames is None: continue valid = sum(lambda x, y: x + int(y.valid), cFrames, 0) if valid != antpols: print( "WARNING: frame count %i at %i missing %.2f%% of frames" % (cFrames[0].header.frame_count, cFrames[0].payload.timetag, float(antpols - valid) / antpols * 100)) continue for cFrame in cFrames: stand, pol = cFrame.header.id # In the current configuration, stands start at 1 and go up to 260. So, we # can use this little trick to populate the data array aStand = 2 * (stand - 1) + pol # Save the time if j == 0 and aStand == 0: times[i] = cFrame.time data[aStand, count[aStand] * 512:(count[aStand] + 1) * 512] = cFrame.payload.data # Update the counters so that we can average properly later on count[aStand] = count[aStand] + 1 j += 1 # Mask bad = numpy.where(numpy.abs(data) >= 90) data[bad] = 0.0 # Beam forming taskPool = Pool(processes=6) taskList = [] taskList.append( (i, 1, 0, taskPool.apply_async(form_beam, args=(data[0::2, :], bln1[0, 0::2])))) taskList.append( (i, 1, 1, taskPool.apply_async(form_beam, args=(data[1::2, :], bln1[0, 1::2])))) taskList.append( (i, 2, 0, taskPool.apply_async(form_beam, args=(data[0::2, :], bln2[0, 0::2])))) taskList.append( (i, 2, 1, taskPool.apply_async(form_beam, args=(data[1::2, :], bln2[0, 1::2])))) taskList.append( (i, 3, 0, taskPool.apply_async(form_beam, args=(data[0::2, :], bln3[0, 0::2])))) taskList.append( (i, 3, 1, taskPool.apply_async(form_beam, args=(data[1::2, :], bln3[0, 1::2])))) taskPool.close() taskPool.join() for i, b, p, task in taskList: if b == 1: beam1[i, p] = task.get() elif b == 2: beam2[i, p] = task.get() else: beam3[i, p] = task.get() print('1', beam1[i, 0], '2', beam2[i, 0], '3', beam3[i, 0], '1/2', beam1[i, 0] / beam2[i, 0], '3/2', beam3[i, 0] / beam2[i, 0]) del data # Plot the data print('CygA :', beam1[:, 0]) print('Pointing 2:', beam2[:, 0]) print('Pointing 1:', beam3[:, 0]) fig = plt.figure() ax1 = fig.add_subplot(1, 2, 1) ax2 = fig.add_subplot(1, 2, 2) ax1.plot(times - times[0], beam1[:, 0]) ax1.plot(times - times[0], beam2[:, 0]) ax1.plot(times - times[0], beam3[:, 0]) ax2.plot(times - times[0], beam1[:, 1]) ax2.plot(times - times[0], beam2[:, 1]) ax2.plot(times - times[0], beam3[:, 1]) plt.show()
def main(args): # Set the station if args.lwasv: station = stations.lwasv else: station = stations.lwa1 antennas = station.antennas fh = open(args.filename, "rb") nFramesFile = os.path.getsize(args.filename) // tbn.FRAME_SIZE srate = tbn.get_sample_rate(fh) #antpols = tbn.get_frames_per_obs(fh) antpols = len(antennas) # Offset in frames for beampols beam/tuning/pol. sets offset = int(args.skip * srate / 512 * antpols) offset = int(1.0 * offset / antpols) * antpols args.skip = 1.0 * offset / antpols * 512 / srate fh.seek(offset * tbn.FRAME_SIZE) # Number of frames to integrate over nFrames = int(args.average * srate / 512 * antpols) args.average = 1.0 * nFrames / antpols * 512 / srate # Number of remaining chunks nChunks = int(math.ceil(1.0 * (nFrames) / (200 * 520))) # Read in the first frame and get the date/time of the first sample # of the frame. This is needed to get the list of stands. junkFrame = tbn.read_frame(fh) fh.seek(0) beginDate = junkFrame.time.datetime # File summary print("Filename: %s" % args.filename) print("Date of First Frame: %s" % str(beginDate)) print("Ant/Pols: %i" % antpols) print("Sample Rate: %i Hz" % srate) print("Frames: %i (%.3f s)" % (nFramesFile, 1.0 * nFramesFile / antpols * 512 / srate)) print("---") print("Offset: %.3f s (%i frames)" % (args.skip, offset)) print("Integration: %.3f s (%i frames; %i frames per stand/pol)" % (args.average, nFrames, nFrames / antpols)) print("Chunks: %i" % nChunks) # Sanity check if offset > nFramesFile: raise RuntimeError("Requested offset is greater than file length") if nFrames > (nFramesFile - offset): raise RuntimeError( "Requested integration time+offset is greater than file length") # Create the FrameBuffer instance buffer = TBNFrameBuffer(stands=range(1, antpols // 2 + 1), pols=[0, 1]) # Master loop over all of the file chunks masterCount = [0 for a in xrange(len(antennas))] # Missing packet control variables missingPackets = numpy.ones((antpols, 2048), dtype=numpy.int8) pc = 0 missing = 0 missingList = [] # Figure fig = plt.figure() ax1 = fig.add_subplot(1, 2, 1) ax2 = fig.add_subplot(1, 2, 2) k = 0 for i in xrange(nChunks): # Find out how many frames remain in the file. If this number is larger # than the maximum of frames we can work with at a time ((200*520)), # only deal with that chunk framesRemaining = nFrames - k if framesRemaining > (200 * 520): framesWork = (200 * 520) else: framesWork = framesRemaining count = [0 for a in xrange(len(antennas))] j = 0 fillsWork = framesWork // antpols # Inner loop that actually reads the frames into the data array while j < fillsWork: try: cFrame = tbn.read_frame(fh) k = k + 1 except errors.EOFError: break except errors.SyncError: print("WARNING: Mark 5C sync error on frame #%i" % (int(fh.tell()) / tbn.FRAME_SIZE - 1)) continue #print(cFrame.header.frame_count, cFrame.payload.timetag, cFrame.id) buffer.append(cFrame) cFrames = buffer.get() if cFrames is None: continue valid = sum(lambda x, y: x + int(y.valid), cFrames, 0) print("Frame #%5i: %.4f seconds with %i valid ant/pols%s" % (cFrames[0].header.frame_count, cFrames[0].time, valid, '!' if valid != antpols else '')) if valid != antpols: bad = [] for cFrame in cFrames: if not cFrame.valid: bad.append(cFrame.id) missingPackets[2 * (bad[-1][0] - 1) + bad[-1][1], pc] = 0 bad.sort() pc += 1 if pc == missingPackets.shape[1]: plotMissing(ax1, ax2, missingPackets, missingList, antpols) plt.show() sys.exit(0) missing += (antpols - valid) total = (buffer.full + buffer.partial) * antpols #print(j, valid, antpols-valid, cFrames[0].header.frame_count, 1.0*missing / total* 100, bad[0], bad[-1], buffer.dropped) #print(buffer.status()) missingList.append(antpols - valid) else: total = (buffer.full + buffer.partial) * antpols missingList.append(0) times = numpy.array([f.payload.timetag for f in cFrames], dtype=numpy.int64) #print(cFrames[0].header.frame_count, times.min(), times.max(), times.max()-times.min(), "%6.3f%%" % (1.0*missing/total*100,)) for cFrame in cFrames: stand, pol = cFrame.header.id # In the current configuration, stands start at 1 and go up to 260. So, we # can use this little trick to populate the data array aStand = 2 * (stand - 1) + pol # Update the counters so that we can average properly later on count[aStand] = count[aStand] + 1 masterCount[aStand] = masterCount[aStand] + 1 j += 1 # Empty the remaining portion of the buffer and integrate what's left for cFrames in buffer.flush(): valid = sum(lambda x, y: x + int(y.valid), cFrames, 0) print("Frame #%5i: %.4f seconds with %i valid ant/pols" % (cFrames[0].header.frame_count, cFrames[0].time, valid)) if valid != antpols: bad = [] for cFrame in cFrames: if not cFrame.valid: bad.append(cFrame.id) missingPackets[2 * (bad[-1][0] - 1) + bad[-1][1], pc] = 0 bad.sort() pc += 1 if pc == missingPackets.shape[1]: plotMissing(ax1, ax2, missingPackets, missingList, antpols) plt.show() sys.exit(0) missing += (antpols - valid) total = (buffer.full + buffer.partial) * antpols #print(j, valid, antpols-valid, cFrames[0].header.frame_count, 1.0*missing / total* 100, bad[0], bad[-1], buffer.dropped) #print(buffer.status()) missingList.append(antpols - valid) else: total = (buffer.full + buffer.partial) * antpols missingList.append(0) # Inner loop that actually reads the frames into the data array for cFrame in cFrames: stand, pol = cFrame.header.id # In the current configuration, stands start at 1 and go up to 10. So, we # can use this little trick to populate the data array aStand = 2 * (stand - 1) + pol # Update the counters so that we can average properly later on count[aStand] = count[aStand] + 1 masterCount[aStand] = masterCount[aStand] + 1 j += 1 plotMissing(ax1, ax2, missingPackets, missingList, antpols) plt.show() sys.exit(0)
def main(args): filename = args.filename # Set the station if args.lwasv: station = stations.lwasv else: station = stations.lwa1 antennas = station.antennas fh = open(filename, "rb") nFramesFile = os.path.getsize(filename) // tbn.FRAME_SIZE srate = tbn.get_sample_rate(fh) antpols = len(antennas) # Read in the first frame and get the date/time of the first sample # of the frame. This is needed to get the list of stands. junkFrame = tbn.read_frame(fh) fh.seek(-tbn.FRAME_SIZE, 1) central_freq = junkFrame.central_freq beginDate = junkFrame.time.datetime # File summary print("Filename: %s" % filename) print("Date of First Frame: %s" % str(beginDate)) print("Ant/Pols: %i" % antpols) print("Sample Rate: %i Hz" % srate) print("Tuning Frequency: %.3f Hz" % central_freq) print(" ") # Convert chunk length to total frame count chunkLength = int(args.length * srate / 512 * antpols) chunkLength = int(1.0 * chunkLength / antpols) * antpols # Convert chunk skip to total frame count chunkSkip = int(args.skip * srate / 512 * antpols) chunkSkip = int(1.0 * chunkSkip / antpols) * antpols # Create the FrameBuffer instance buffer = TBNFrameBuffer(stands=range(1,antpols//2+1), pols=[0, 1]) # Output arrays clipFraction = [] meanPower = [] # Find stands #10 toUse = [] for i in xrange(antpols): ant = antennas[i] if ant.stand.id == 10: toUse.append(i) # Go! i = 1 done = False print(" | Clipping | Power |") print(" | 10X 10Y | 10X 10Y |") print("---+-----------------+-------------------+") while True: count = [0 for j in xrange(antpols)] data = numpy.zeros((antpols, chunkLength*512//antpols), dtype=numpy.csingle) for j in xrange(chunkLength): try: cFrame = tbn.read_frame(fh) except errors.EOFError: done = True break except errors.SyncError: continue buffer.append(cFrame) cFrames = buffer.get() if cFrames is None: continue for cFrame in cFrames: stand,pol = cFrame.header.id # In the current configuration, stands start at 1 and go up to 260. So, we # can use this little trick to populate the data array aStand = 2*(stand-1)+pol try: data[aStand, count[aStand]*512:(count[aStand]+1)*512] = cFrame.payload.data # Update the counters so that we can average properly later on count[aStand] = count[aStand] + 1 except ValueError: pass # Empty the remaining portion of the buffer for cFrames in buffer.flush(): # Inner loop that actually reads the frames into the data array for cFrame in cFrames: stand,pol = cFrame.header.id # In the current configuration, stands start at 1 and go up to 10. So, we # can use this little trick to populate the data array aStand = 2*(stand-1)+pol try: data[aStand, count[aStand]*512:(count[aStand]+1)*512] = cFrame.payload.data # Update the counters so that we can average properly later on count[aStand] = count[aStand] + 1 except ValueError: pass if done: break else: data = numpy.abs(data)**2 data = data.astype(numpy.int32) clipFraction.append( numpy.zeros(antpols) ) meanPower.append( data.mean(axis=1) ) for j in xrange(antpols): bad = numpy.nonzero(data[j,:] > args.trim_level)[0] clipFraction[-1][j] = 1.0*len(bad) / data.shape[1] clip = clipFraction[-1] power = meanPower[-1] print("%2i | %6.2f%% %6.2f%% | %8.2f %8.2f |" % (i, clip[toUse[0]]*100.0, clip[toUse[1]]*100.0, power[toUse[0]], power[toUse[1]])) i += 1 fh.seek(tbn.FRAME_SIZE*chunkSkip, 1) clipFraction = numpy.array(clipFraction) meanPower = numpy.array(meanPower) clip = clipFraction.mean(axis=0) power = meanPower.mean(axis=0) print("---+-----------------+-------------------+") print("%2s | %6.2f%% %6.2f%% | %8.2f %8.2f |" % ('M', clip[toUse[0]]*100.0, clip[toUse[1]]*100.0, power[toUse[0]], power[toUse[1]]))