def transmit_std(): print 'TX: Initializing standard transport TX to IP', options.ip tx = spead.Transmitter(spead.TransportUDPtx(options.ip, 8888)) ig = spead.ItemGroup() ig.add_item( name='data_timestamp', description='Timestamp in epoch ms for the current visibility sample', shape=[1], fmt=spead.mkfmt(('u', 64))) ig.add_item( name='vis_data', description='The complex visibility spectrum for a single time dump', shape=[channels, baselines, 2], fmt=spead.mkfmt(('u', 32))) # using init_val with a numpy array will use the numpy transport automatically. t_heap_send = 0 for x in range(iterations): ig['data_timestamp'] = int(time.time() * 1000) ig['vis_data'] = np.random.normal(size=(channels, baselines, 2)).astype(np.float32) t_heap_send = time.time() tx.send_heap(ig.get_heap()) print "Sent data for timestamp", ig['data_timestamp'], "in", time.time( ) - t_heap_send, "s" time.sleep(15) tx.end() print 'TX: Done.'
def transmit(): print 'TX: Initializing...' tx = spead.Transmitter(spead.TransportUDPtx('127.0.0.1', PORT)) ig = spead.ItemGroup() ig.add_item(name='Var1', description='Description for Var1', shape=[], fmt=spead.mkfmt(('u', 32), ('u', 32), ('u', 32)), init_val=(1, 2, 3)) tx.send_heap(ig.get_heap()) ig['Var1'] = (4, 5, 6) tx.send_heap(ig.get_heap()) ig.add_item(name='Var2', description='Description for Var2', shape=[100, 100], fmt=spead.mkfmt(('u', 32))) data = numpy.arange(100 * 100) data.shape = (100, 100) ig['Var2'] = data tx.send_heap(ig.get_heap()) tx.end() print 'TX: Done.'
def transmit(): #print 'TX: Initializing...' tx = spead.Transmitter(spead.TransportFile(sys.stdout)) ig = spead.ItemGroup() ig.add_item(name='Var1', description='Description for Var1', shape=[], fmt=spead.mkfmt(('u',32),('u',32),('u',32)), init_val=(1,2,3)) tx.send_heap(ig.get_heap()) ig['Var1'] = (4,5,6) tx.send_heap(ig.get_heap()) ig.add_item(name='Var2', description='Description for Var2', shape=[100,100], fmt=spead.mkfmt(('u',32))) data = numpy.arange(100*100); data.shape = (100,100) ig['Var2'] = data tx.send_heap(ig.get_heap()) tx.end()
def transmit(): print 'TX: Initializing...' tx = spead.Transmitter(spead.TransportFile(FILENAME,'w')) ig = spead.ItemGroup() ig.add_item(name='Var1', description='Description for Var1', shape=[], fmt=spead.mkfmt(('u',32),('u',32),('u',32)), init_val=(1,2,3)) tx.send_heap(ig.get_heap()) ig['Var1'] = (4,5,6) tx.send_heap(ig.get_heap()) ig.add_item(name='Var2', description='Description for Var2', shape=[DIM,DIM], fmt=spead.mkfmt(('u',32))) data = numpy.arange(DIM*DIM); data.shape = (DIM,DIM) ig['Var2'] = data tx.send_heap(ig.get_heap()) tx.end() print 'TX: Done.'
def transmit(): print 'TX: Initializing...' tx = spead.Transmitter(spead.TransportFile(FILENAME, 'w')) ig = spead.ItemGroup() ig.add_item(name='Var1', description='Description for Var1', shape=[], fmt=spead.mkfmt(('u', 32), ('u', 32), ('u', 32)), init_val=(1, 2, 3)) tx.send_heap(ig.get_heap()) ig['Var1'] = (4, 5, 6) tx.send_heap(ig.get_heap()) ig.add_item(name='Var2', description='Description for Var2', shape=[DIM, DIM], fmt=spead.mkfmt(('u', 32))) data = numpy.arange(DIM * DIM) data.shape = (DIM, DIM) ig['Var2'] = data tx.send_heap(ig.get_heap()) tx.end() print 'TX: Done.'
def transmit(): # Initialise SPEAD transmitter tx = spead.Transmitter(spead.TransportUDPtx(IP, PORT)) # Add items to item group ig = spead.ItemGroup() ig.add_item(name="station", shape=[], fmt=spead.mkfmt(('u', 1)), init_val = (1)) data = numpy.arange(40000).astype(numpy.uint32); data.shape = (40000) ig.add_item(name='data', shape=[40000], ndarray=data) # Continuously send heap while True: tx.send_heap(ig.get_heap()) tx.end() print 'TX: Done.'
def iterheaps(self): while self.is_connected: self.acc_num.seek(0) heap_cnt = '\x00' + self.acc_num.read() if heap_cnt == self._tx_heap.get(S.HEAP_CNT_ID,'XX')[1]: #logger.debug('BorphSpeadServer.iterheaps: Waiting for HEAP_CNT=%s (currently %s)' % (S.hexify(self._tx_heap.get(S.HEAP_CNT_ID,'XX')[1]), S.hexify(heap_cnt))) time.sleep(.001) continue tme = time.time() tme = int(tme*1000) htme = S.pack(S.mkfmt(('u',64)),[[tme]]) self._tx_heap[TIMESTAMP_ID] = (S.DIRECTADDR,htme) logger.info('BorphSpeadServer.iterheaps: Sending HEAP_CNT=%s' % S.hexify(heap_cnt)) self._tx_heap[S.HEAP_CNT_ID] = (S.IMMEDIATEADDR, heap_cnt) for id,bram in self.brams.iteritems(): bram.seek(0) self._tx_heap[id] = (S.DIRECTADDR, bram.read()) yield self._tx_heap return
#! /usr/bin/env python import aipy as a, numpy as n, spead, optparse, sys, cPickle #import logging; logging.basicConfig(level=logging.DEBUG) CMD_CACHE, CMD_PROCESS, CMD_FLUSH, CMD_CLEARCACHE, CMD_CLEARALL = range(5) o = optparse.OptionParser() o.add_option('-p', '--port', type='int', help='UDP port to listen to.') opts, args = o.parse_args(sys.argv[1:]) ig = spead.ItemGroup() dbuf = {} aa, cat, tx, ig_tx = None, None, None, None f64 = spead.mkfmt(('f', 64)) i40 = spead.mkfmt(('i', 40)) while True: tport = spead.TransportUDPrx(opts.port, pkt_count=1024) print 'waiting for a connection' for frame in spead.iterframes(tport): ig.update(frame) if ig['command'] == CMD_FLUSH: #print 'flushing' continue if ig['command'] == CMD_CLEARALL: print 'clearing all' dbuf, aa, cat, ig_tx = {}, None, None, None if ig['command'] == CMD_CLEARCACHE: print 'clearing cache'
def ends(num=None): if num is None: for tx in _txs: tx.end() else: _txs[num].end() uv = a.miriad.UV(args[0]) aa = a.cal.get_aa(opts.cal, uv['sdf'], uv['sfreq'], uv['nchan']) chans = a.scripting.parse_chans(opts.chan, uv['nchan']) aa.select_chans(chans) srclist,cutoff,catalogs = a.scripting.parse_srcs(opts.src, opts.cat) cat = a.cal.get_catalog(opts.cal, srclist, cutoff, catalogs) (uvw,t,(i,j)),d = uv.read() aa.set_jultime(t) cat.compute(aa) c8 = spead.mkfmt(('c',8)) u40 = spead.mkfmt(('u',40)) f64 = spead.mkfmt(('f',64)) f32 = spead.mkfmt(('f',32)) i32 = spead.mkfmt(('i',32)) add_items('command', fmt=u40, shape=[], init_val=CMD_CLEARALL) send_frames() add_items('baseline', fmt=spead.mkfmt(('u',20),('u',20)), shape=[]) add_items('juldate', fmt=f64, shape=[]) add_items('pol', fmt=spead.mkfmt(('u',24),('c',8),('c',8)), shape=[]) add_items('cal', fmt=c8, shape=-1, init_val=opts.cal) add_items('src', fmt=c8, shape=-1, init_val=opts.src) add_items('cat', fmt=c8, shape=-1, init_val=opts.cat) add_items('respond_ip', fmt=c8, shape=-1, init_val=opts.hostname)
BASE_RX_ID+4: ('insel_insel_data', S.DEFAULT_FMT), BASE_RX_ID+5: ('quant0_gain', S.DEFAULT_FMT), BASE_RX_ID+6: ('quant1_gain', S.DEFAULT_FMT), BASE_RX_ID+7: ('quant2_gain', S.DEFAULT_FMT), BASE_RX_ID+8: ('quant3_gain', S.DEFAULT_FMT), BASE_RX_ID+9: ('quant0_addr', S.DEFAULT_FMT), BASE_RX_ID+10: ('quant1_addr', S.DEFAULT_FMT), BASE_RX_ID+11: ('quant2_addr', S.DEFAULT_FMT), BASE_RX_ID+12: ('quant3_addr',S.DEFAULT_FMT) # 0-16 coeff, 17 coeff_en, 20-25 coeff_addr, 30-31 ant_select } FPGA_TX_RESOURCES = { BASE_TX_ID+0: ('acc_num','acc_num', S.DEFAULT_FMT, []) } _cnt = 1 for b in ['aa','bb','cc','dd']: FPGA_TX_RESOURCES[BASE_TX_ID+_cnt] = ('dir_x0_%s_real' % b, '%s_er'%b, S.mkfmt(('i',32)), [NCHAN/2]) _cnt += 1 FPGA_TX_RESOURCES[BASE_TX_ID+_cnt] = ('dir_x1_%s_real' % b, '%s_or'%b, S.mkfmt(('i',32)), [NCHAN/2]) _cnt += 1 for b in ['ab','ac','ad','bc','bd','cd']: FPGA_TX_RESOURCES[BASE_TX_ID+_cnt] = ('dir_x0_%s_real' % b, '%s_er'%b, S.mkfmt(('i',32)), [NCHAN/2]) _cnt += 1 FPGA_TX_RESOURCES[BASE_TX_ID+_cnt] = ('dir_x1_%s_real' % b, '%s_or'%b, S.mkfmt(('i',32)), [NCHAN/2]) _cnt += 1 FPGA_TX_RESOURCES[BASE_TX_ID+_cnt] = ('dir_x0_%s_imag' % b, '%s_ei'%b, S.mkfmt(('i',32)), [NCHAN/2]) _cnt += 1 FPGA_TX_RESOURCES[BASE_TX_ID+_cnt] = ('dir_x1_%s_imag' % b, '%s_oi'%b, S.mkfmt(('i',32)), [NCHAN/2]) _cnt += 1 def start_bof(boffile=BOFFILE): return os.spawnv(os.P_NOWAIT, boffile, [boffile])
else: _txs[num].end() uv = a.miriad.UV(args[0]) aa = a.cal.get_aa(opts.cal, uv['sdf'], uv['sfreq'], uv['nchan']) chans = a.scripting.parse_chans(opts.chan, uv['nchan']) NCHAN = chans.size aa.select_chans(chans) srclist, cutoff, catalogs = a.scripting.parse_srcs(opts.src, opts.cat) cat = a.cal.get_catalog(opts.cal, srclist, cutoff, catalogs) (uvw, t, (i, j)), d = uv.read() aa.set_jultime(t) cat.compute(aa) c8 = spead.mkfmt(('c', 8)) u40 = spead.mkfmt(('u', 40)) f64 = spead.mkfmt(('f', 64)) f32 = spead.mkfmt(('f', 32)) i32 = spead.mkfmt(('i', 32)) add_items('command', fmt=u40, shape=[], init_val=CMD_CLEARALL) send_frames() add_items('baseline', fmt=spead.mkfmt(('u', 20), ('u', 20)), shape=[]) add_items('juldate', fmt=f64, shape=[]) add_items('pol', fmt=spead.mkfmt(('u', 24), ('c', 8), ('c', 8)), shape=[]) add_items('cal', fmt=c8, shape=-1, init_val=opts.cal) add_items('src', fmt=c8, shape=-1, init_val=opts.src) add_items('cat', fmt=c8, shape=-1, init_val=opts.cat) add_items('respond_ip', fmt=c8, shape=-1, init_val=opts.hostname)
import aipy as a, numpy as n, spead, optparse, sys, cPickle # import logging; logging.basicConfig(level=logging.DEBUG) CMD_CACHE, CMD_PROCESS, CMD_FLUSH, CMD_CLEARCACHE, CMD_CLEARALL = range(5) o = optparse.OptionParser() o.add_option("-p", "--port", type="int", help="UDP port to listen to.") opts, args = o.parse_args(sys.argv[1:]) ig = spead.ItemGroup() dbuf = {} aa, cat, tx, ig_tx = None, None, None, None f64 = spead.mkfmt(("f", 64)) i40 = spead.mkfmt(("i", 40)) while True: tport = spead.TransportUDPrx(opts.port, pkt_count=1024) print "waiting for a connection" for frame in spead.iterframes(tport): ig.update(frame) if ig["command"] == CMD_FLUSH: # print 'flushing' continue if ig["command"] == CMD_CLEARALL: print "clearing all" dbuf, aa, cat, ig_tx = {}, None, None, None if ig["command"] == CMD_CLEARCACHE: print "clearing cache"
# Set up reciever and item group.The arr variable is so that spead knows to # unpack numpy arrays(added when new item group is added, instead of fmt and # shape, add narray=arr). This makes unpacking faster, whenever we need it. arr = N.zeros(NCHAN * 2) arr = N.array(arr, dtype=N.int32) rx = S.TransportUDPrx(opts.port, pkt_count=4096) ig = S.ItemGroup() for id in rpoco8.FPGA_TX_RESOURCES: bram, name, fmt, shape = rpoco8.FPGA_TX_RESOURCES[id] if name != "acc_num": ig.add_item(name=name, id=id, fmt=fmt, shape=shape) else: ig.add_item(name=name, id=id, fmt=fmt, shape=[]) ig.add_item(name="data_timestamp", id=rpoco8.TIMESTAMP_ID, fmt=S.mkfmt(("u", 64)), shape=[-1]) # # the values of the items that need to be written too in miriad. # sdf=input('sdf(d)=') change in frequency between channels. # sfreq=input('sfreq(d)=') starting frequency, off 0th channel. # nchan=input('nchan(i)=') number of channels # inttime=input('inttime(d)=') integration time # bandpass=input('bandpass=') array of size nchan x nants inttime = (2 ** 30) * 5e-9 sfreq = 0.200 sdf = -0.10 / 1024.0 fre = N.arange(8, dtype=N.integer)
def transmit_std(): print 'TX: Initializing standard transport TX to IP',options.ip tx = spead.Transmitter(spead.TransportUDPtx(options.ip, 8888)) ig = spead.ItemGroup() ig.add_item(name='data_timestamp', description='Timestamp in epoch ms for the current visibility sample',shape=[1], fmt=spead.mkfmt(('u',64))) ig.add_item(name='vis_data', description='The complex visibility spectrum for a single time dump', shape=[channels,baselines,2], fmt=spead.mkfmt(('u',32))) # using init_val with a numpy array will use the numpy transport automatically. t_heap_send = 0 for x in range(iterations): ig['data_timestamp'] = int(time.time() * 1000) ig['vis_data'] = np.random.normal(size=(channels,baselines,2)).astype(np.float32) t_heap_send = time.time() tx.send_heap(ig.get_heap()) print "Sent data for timestamp",ig['data_timestamp'],"in",time.time()-t_heap_send,"s" time.sleep(15) tx.end() print 'TX: Done.'
#! /usr/bin/env python import aipy as a, numpy as n, spead, optparse, sys, cPickle #import logging; logging.basicConfig(level=logging.DEBUG) CMD_CACHE, CMD_PROCESS, CMD_FLUSH, CMD_CLEARCACHE, CMD_CLEARALL = range(5) o = optparse.OptionParser() o.add_option('-p','--port', type='int', help='UDP port to listen to.') opts, args = o.parse_args(sys.argv[1:]) ig = spead.ItemGroup() dbuf = {} aa, cat, tx, ig_tx = None, None, None, None f64 = spead.mkfmt(('f',64)) i40 = spead.mkfmt(('i',40)) while True: tport = spead.TransportUDPrx(opts.port, pkt_count=1024) print 'waiting for a connection' for frame in spead.iterframes(tport): ig.update(frame) if ig['command'] == CMD_FLUSH: #print 'flushing' continue if ig['command'] == CMD_CLEARALL: print 'clearing all' dbuf, aa, cat, ig_tx = {}, None, None, None if ig['command'] == CMD_CLEARCACHE: print 'clearing cache'
def spead_issue_meta(self): """ All FxCorrelators issued SPEAD in the same way, with tweakings that are implemented by the child class. :return: """ sample_rate = int(self.configd['FxCorrelator']['sample_rate_hz']) self.spead_ig.add_item(name='adc_sample_rate', id=0x1007, description='The ADC sample rate (samples per second) ', shape=[],fmt=spead.mkfmt(('u', 64)), init_val=sample_rate) n_ants = int(self.configd['fengine']['n_ants']) n_bls = (n_ants*(n_ants+1)/2)*4 self.spead_ig.add_item(name='n_bls', id=0x1008, description='Number of baselines in the cross correlation product.', shape=[], fmt=spead.mkfmt(('u', spead.ADDRSIZE)), init_val=n_bls) n_chans = int(self.configd['fengine']['n_chans']) self.spead_ig.add_item(name='n_chans', id=0x1009, description='Number of frequency channels in an integration.', shape=[], fmt=spead.mkfmt(('u', spead.ADDRSIZE)), init_val=n_chans) # TODO - the number of inputs, cos antennas can be single or multiple pol? # f-engines now have only got inputs, they don't know what or from where. self.spead_ig.add_item(name='n_ants', id=0x100A, description='The number of antennas.', shape=[], fmt=spead.mkfmt(('u', spead.ADDRSIZE)), init_val=n_ants) self.spead_ig.add_item(name='n_xengs', id=0x100B, description='', shape=[], fmt=spead.mkfmt(('u', spead.ADDRSIZE)), init_val=(len(self.xhosts) * self.x_per_fpga)) # TODO # self.spead_ig.add_item(name='bls_ordering', id=0x100C, # description='', # shape=[], fmt=spead.mkfmt(('u', spead.ADDRSIZE)), # init_val=numpy.array([bl for bl in self.get_bl_order()])) # self.spead_ig.add_item(name='crosspol_ordering', id=0x100D, # description='', # shape=[], fmt=spead.mkfmt(('u', spead.ADDRSIZE)), # init_val=) # self.spead_ig.add_item(name='input_labelling', id=0x100E, # description='', # init_val=self.configd['fengine']['source_names']) # self.spead_ig.add_item(name='n_bengs', id=0x100F, # description='', # shape=[], fmt=spead.mkfmt(('u', spead.ADDRSIZE)), # init_val=) self.spead_ig.add_item(name='center_freq', id=0x1011, description='', shape=[],fmt=spead.mkfmt(('f', 64)), init_val=int(self.configd['fengine']['true_cf'])) self.spead_ig.add_item(name='bandwidth', id=0x1013, description='', shape=[],fmt=spead.mkfmt(('f', 64)), init_val=int(self.configd['fengine']['bandwidth'])) acc_len = int(self.configd['xengine']['accumulation_len']) xeng_acc_len = int(self.configd['xengine']['xeng_accumulation_len']) self.spead_ig.add_item(name='n_accs', id=0x1015, description='', shape=[], fmt=spead.mkfmt(('u', spead.ADDRSIZE)), init_val=(acc_len * xeng_acc_len)) int_time = ((acc_len * xeng_acc_len) * int(self.configd['fengine']['n_chans'])) / int(self.configd['fengine']['bandwidth']) self.spead_ig.add_item(name='int_time', id=0x1016, description='', shape=[],fmt=spead.mkfmt(('f', 64)), init_val=int_time) # self.spead_ig.add_item(name='coarse_chans', id=0x1017, # description='', # shape=[], fmt=spead.mkfmt(('u', spead.ADDRSIZE)), # init_val=) # # self.spead_ig.add_item(name='current_coarse_chan', id=0x1018, # description='', # shape=[], fmt=spead.mkfmt(('u', spead.ADDRSIZE)), # init_val=) # # self.spead_ig.add_item(name='fft_shift_fine', id=0x101C, # description='', # shape=[], fmt=spead.mkfmt(('u', spead.ADDRSIZE)), # init_val=) # # self.spead_ig.add_item(name='fft_shift_coarse', id=0x101D, # description='', # shape=[], fmt=spead.mkfmt(('u', spead.ADDRSIZE)), # init_val=) self.spead_ig.add_item(name='fft_shift', id=0x101E, description='', shape=[], fmt=spead.mkfmt(('u', spead.ADDRSIZE)), init_val=int(self.configd['fengine']['fft_shift'])) self.spead_ig.add_item(name='xeng_acc_len', id=0x101F, description='', shape=[], fmt=spead.mkfmt(('u', spead.ADDRSIZE)), init_val=int(self.configd['xengine']['accumulation_len'])) quant_format = self.configd['fengine']['quant_format'] quant_bits = int(quant_format.split('.')[0]) self.spead_ig.add_item(name='requant_bits', id=0x1020, description='', shape=[], fmt=spead.mkfmt(('u', spead.ADDRSIZE)), init_val=quant_bits) pkt_len = int(self.configd['fengine']['10gbe_pkt_len']) self.spead_ig.add_item(name='feng_pkt_len', id=0x1021, description='', shape=[], fmt=spead.mkfmt(('u', spead.ADDRSIZE)), init_val=pkt_len) port = int(self.configd['xengine']['output_destination_port']) self.spead_ig.add_item(name='rx_udp_port', id=0x1022, description='', shape=[], fmt=spead.mkfmt(('u', spead.ADDRSIZE)), init_val=port) port = int(self.configd['fengine']['10gbe_start_port']) self.spead_ig.add_item(name='feng_udp_port', id=0x1023, description='', shape=[], fmt=spead.mkfmt(('u', spead.ADDRSIZE)), init_val=port) ip = self.configd['xengine']['output_destination_ip'] self.spead_ig.add_item(name='rx_udp_ip_str', id=0x1024, description='', shape=[-1],fmt=spead.STR_FMT, init_val=ip) import struct ip = struct.unpack('>I', socket.inet_aton(self.configd['fengine']['10gbe_start_ip']))[0] self.spead_ig.add_item(name='feng_start_ip', id=0x1025, description='', shape=[], fmt=spead.mkfmt(('u', spead.ADDRSIZE)), init_val=ip) xeng_clk = 217000000 self.spead_ig.add_item(name='xeng_rate', id=0x1026, description='', shape=[], fmt=spead.mkfmt(('u', spead.ADDRSIZE)), init_val=xeng_clk) self.spead_ig.add_item(name='sync_time', id=0x1027, description='', shape=[], fmt=spead.mkfmt(('u', spead.ADDRSIZE)), init_val=self.synchronisation_epoch) # self.spead_ig.add_item(name='n_stokes', id=0x1040, # description='', # shape=[], fmt=spead.mkfmt(('u', spead.ADDRSIZE)), # init_val=) x_per_fpga = int(self.configd['xengine']['x_per_fpga']) self.spead_ig.add_item(name='x_per_fpga', id=0x1041, description='', shape=[], fmt=spead.mkfmt(('u', spead.ADDRSIZE)), init_val=x_per_fpga) n_ants_per_xaui = 1 self.spead_ig.add_item(name='n_ants_per_xaui', id=0x1042, description='', shape=[], fmt=spead.mkfmt(('u', spead.ADDRSIZE)), init_val=n_ants_per_xaui) # self.spead_ig.add_item(name='ddc_mix_freq', id=0x1043, # description='', # shape=[],fmt=spead.mkfmt(('f', 64)), # init_val=) # self.spead_ig.add_item(name='ddc_bandwidth', id=0x1044, # description='', # shape=[],fmt=spead.mkfmt(('f', 64)), # init_val=) sample_bits = int(self.configd['fengine']['sample_bits']) self.spead_ig.add_item(name='adc_bits', id=0x1045, description='', shape=[], fmt=spead.mkfmt(('u', spead.ADDRSIZE)), init_val=sample_bits) # self.spead_ig.add_item(name='scale_factor_timestamp', id=0x1046, # description='', # shape=[],fmt=spead.mkfmt(('f', 64)), # init_val=) # self.spead_ig.add_item(name='b_per_fpga', id=0x1047, # description='', # shape=[], fmt=spead.mkfmt(('u', spead.ADDRSIZE)), # init_val=) xeng_sample_bits = 32 self.spead_ig.add_item(name='xeng_out_bits_per_sample', id=0x1048, description='', shape=[], fmt=spead.mkfmt(('u', spead.ADDRSIZE)), init_val=xeng_sample_bits) f_per_fpga = int(self.configd['fengine']['f_per_fpga']) self.spead_ig.add_item(name='f_per_fpga', id=0x1049, description='', shape=[], fmt=spead.mkfmt(('u', spead.ADDRSIZE)), init_val=f_per_fpga) # self.spead_ig.add_item(name='beng_out_bits_per_sample', id=0x1050, # description='', # shape=[], fmt=spead.mkfmt(('u', spead.ADDRSIZE)), # init_val=) # self.spead_ig.add_item(name='rf_gain_MyAntStr ', id=0x1200+inputN, # description='', # shape=[], fmt=spead.mkfmt(('f', 64)), # init_val=) # self.spead_ig.add_item(name='eq_coef_MyAntStr', id=0x1400+inputN, # description='', # shape=[], fmt=spead.mkfmt(('u', 32)), # init_val=) self.spead_ig.add_item(name=('timestamp'), id=0x1600, description='Timestamp', shape=[], fmt=spead.mkfmt(('u',spead.ADDRSIZE)), init_val=0) import numpy ndarray = numpy.dtype(numpy.int64), (4096 * 40 * 1, 1, 1) self.spead_ig.add_item(name='xeng_raw_sim', id=0x1800, description='X-engine RTS simulation data.', ndarray=ndarray) # self.spead_ig.add_item(name='timestamp', id=0x1600, # description='', # shape=[], fmt=spead.mkfmt(('u', spead.ADDRSIZE)), # init_val=) # self.spead_ig.add_item(name='xeng_raw', id=0x1800, # description='', # shape=[], fmt=spead.mkfmt(('u', spead.ADDRSIZE)), # init_val=) # self.spead_ig.add_item(name='beamweight_MyAntStr', id=0x2000+inputN, # description='', # shape=[], fmt=spead.mkfmt(('u', 32)), # init_val=) # self.spead_ig.add_item(name='incoherent_sum', id=0x3000, # description='', # shape=[], fmt=spead.mkfmt(('u', 32)), # init_val=) # self.spead_ig.add_item(name='n_inputs', id=0x3100, # description='', # shape=[], fmt=spead.mkfmt(('u', spead.ADDRSIZE)), # init_val=) # self.spead_ig.add_item(name='digitiser_id', id=0x3101, # description='', # shape=[], fmt=spead.mkfmt(('u', spead.ADDRSIZE)), # init_val=) # self.spead_ig.add_item(name='digitiser_status', id=0x3102, # description='', # shape=[], fmt=spead.mkfmt(('u', spead.ADDRSIZE)), # init_val=) # self.spead_ig.add_item(name='pld_len', id=0x3103, # description='', # shape=[], fmt=spead.mkfmt(('u', spead.ADDRSIZE)), # init_val=) # self.spead_ig.add_item(name='raw_data_MyAntStr', id=0x3300+inputN, # description='', # shape=[], fmt=spead.mkfmt(('u', spead.ADDRSIZE)), # init_val=) # self.spead_ig.add_item(name='Reserved for SP-CAM meta-data', id=0x7000-0x7fff, # description='', # shape=[], fmt=spead.mkfmt(('u', spead.ADDRSIZE)), # init_val=) # self.spead_ig.add_item(name='feng_id', id=0xf101, # description='', # shape=[], fmt=spead.mkfmt(('u', spead.ADDRSIZE)), # init_val=) # self.spead_ig.add_item(name='feng_status', id=0xf102, # description='', # shape=[], fmt=spead.mkfmt(('u', spead.ADDRSIZE)), # init_val=) # self.spead_ig.add_item(name='frequency', id=0xf103, # description='', # shape=[], fmt=spead.mkfmt(('u', spead.ADDRSIZE)), # init_val=) # self.spead_ig.add_item(name='raw_freq_MyAntStr', id=0xf300+inputN, # description='', # shape=[], fmt=spead.mkfmt(('u', spead.ADDRSIZE)), # init_val=) # self.spead_ig.add_item(name='bf_MyBeamName', id=0xb000+beamN, # description='', # shape=[], fmt=spead.mkfmt(('u', spead.ADDRSIZE)), # init_val=) # and send everything self.spead_tx.send_heap(self.spead_ig.get_heap())
UV_VAR_TYPES = { 'source': 'a', 'operator': 'a', 'version': 'a', 'telescop': 'a', 'antpos': 'd', 'freq': 'd', 'inttime': 'r', 'nants': 'i', 'nchan': 'i', 'nspect': 'i', 'sfreq': 'd', 'sdf': 'd', 'ischan': 'i', 'nschan': 'i', 'restfreq': 'd', 'npol': 'i', 'epoch': 'r', 'veldop': 'r', 'vsource': 'r', 'longitu': 'd', 'latitud': 'd', 'dec': 'd', 'obsdec': 'd', 'nspect': 'i', 'ischan': 'i', 'epoch': 'r', 'veldop': 'r', 'vsource': 'r', 'ra': 'd', 'obsra': 'd', 'lst': 'd', 'pol': 'i', } FPGA_RX_RESOURCES = { BASE_RX_ID+0: ('ctrl_sw', S.DEFAULT_FMT), BASE_RX_ID+1: ('acc_length', S.DEFAULT_FMT), BASE_RX_ID+2: ('eq_coeff', S.mkfmt(('u',4),('u',8),('u',3),('u',17))), BASE_RX_ID+3: ('Sync_sync_sel', S.DEFAULT_FMT), BASE_RX_ID+4: ('Sync_sync_pulse', S.DEFAULT_FMT), BASE_RX_ID+5: ('insel_insel_data', S.DEFAULT_FMT) # 0-16 coeff, 17 coeff_en, 20-25 coeff_addr, 28-29 ant_select } FPGA_TX_RESOURCES = { BASE_TX_ID+0: ('acc_num','acc_num', S.DEFAULT_FMT, []) } _cnt = 1 for b in ['aa','bb','cc','dd']: FPGA_TX_RESOURCES[BASE_TX_ID+_cnt] = ('xengine8_muxed_%s_real' % b, '%s_r'%b, S.mkfmt(('i',32)), [2*NCHAN]) _cnt += 1 for b in ['ab','ac','ad','ae','af','bc','bd','be','bf','cd','cg','ch','dg','dh']: FPGA_TX_RESOURCES[BASE_TX_ID+_cnt] = ('xengine8_muxed_%s_real' % b, '%s_r'%b, S.mkfmt(('i',32)), [2*NCHAN]) _cnt += 1 FPGA_TX_RESOURCES[BASE_TX_ID+_cnt] = ('xengine8_muxed_%s_imag' % b, '%s_i'%b, S.mkfmt(('i',32)), [2*NCHAN])
if opts.verbose: logging.basicConfig(level=logging.DEBUG) else: stream = logging.basicConfig(level=logging.INFO) logging.getLogger('spead').setLevel(logging.WARN) if opts.pid: pid = opts.pid else: pid = rpoco8.start_bof() logger.info('RPOCO8-RX: Started %s with pid=%d' % (rpoco8.BOFFILE, pid)) try: bss = rpoco8.BorphSpeadServer(pid) bss.add_item(name='data_timestamp', description='time stamp for data in ms',fmt=S.mkfmt(('u',64)), shape=[1]) while True: logger.info('RPOCO8-RX: Listening to port %d' % (opts.port)) bss.listen(opts.port) logger.info('RPOCO8-RX: Waiting for client...') client = bss.get_client(opts.port) for heap in bss.iterheaps(): logger.debug('RPOCO8-RX: Sending a heap') client.send_heap(heap) logger.debug('RPOCO8-RX: Heap sent') logger.info('RPOCO8-RX: Client disconnected') bss.stop() time.sleep(1) except(OSError): logger.fatal('RPOCO8-RX: Cannot start RPOCO8. FPGA already programmed') except(KeyboardInterrupt): logger.info('RPOCO8-RX: Got KeyboardInterrupt. Stopping')
def rx_inter(self,data_port=7148, sd_ip='127.0.0.1', sd_port=7149, acc_scale=True, filename=None, **kwargs): ''' Process SPEAD data from X engines and forward it to the SD. ''' print 'WARNING: This function is not yet tested. YMMV.' logger=self.logger logger.info("Data reception on port %i."%data_port) rx = spead.TransportUDPrx(data_port, pkt_count=1024, buffer_size=51200000) logger.info("Sending Signal Display data to %s:%i."%(sd_ip,sd_port)) tx_sd = spead.Transmitter(spead.TransportUDPtx(sd_ip, sd_port)) ig = spead.ItemGroup() ig_sd = spead.ItemGroup() if filename == None: filename=str(int(time.time())) + ".synth.h5" logger.info("Starting file %s."%(filename)) f = h5py.File(filename, mode="w") data_ds = None ts_ds = None idx = 0 dump_size = 0 datasets = {} datasets_index = {} # we need these bits of meta data before being able to assemble and transmit signal display data meta_required = ['n_chans','n_bls','n_xengs','center_freq','bls_ordering','bandwidth'] meta_desired = ['n_accs'] meta = {} sd_frame = None sd_slots = None timestamp = None # log the latest timestamp for which we've stored data currentTimestamp = -1 # iterate through SPEAD heaps returned by the SPEAD receiver. for heap in spead.iterheaps(rx): ig.update(heap) logger.debug("PROCESSING HEAP idx(%i) cnt(%i) @ %.4f" % (idx, heap.heap_cnt, time.time())) for name in ig.keys(): item = ig.get_item(name) # the item is not marked as changed and we already have a record for it, continue if not item._changed and datasets.has_key(name): continue logger.debug("PROCESSING KEY %s @ %.4f" % (name, time.time())) if name in meta_desired: meta[name] = ig[name] if name in meta_required: meta[name] = ig[name] meta_required.pop(meta_required.index(name)) if len(meta_required) == 0: sd_frame = np.zeros((meta['n_chans'],meta['n_bls'],2),dtype=np.float32) logger.info("Got all required metadata. Initialised sd frame to shape %s"%(str(sd_frame.shape))) meta_required = ['n_chans','bandwidth','n_bls','n_xengs','center_freq','bls_ordering'] ig_sd = spead.ItemGroup() for meta_item in meta_required: ig_sd.add_item( name=ig.get_item(meta_item).name, id=ig.get_item(meta_item).id, description=ig.get_item(meta_item).description, #shape=ig.get_item(meta_item).shape, #fmt=ig.get_item(meta_item).format, init_val=ig.get_item(meta_item).get_value()) tx_sd.send_heap(ig_sd.get_heap()) sd_slots = np.zeros(meta['n_xengs']) if not datasets.has_key(name): # check to see if we have encountered this type before shape = ig[name].shape if item.shape == -1 else item.shape dtype = np.dtype(type(ig[name])) if shape == [] else item.dtype if dtype is None: dtype = ig[name].dtype # if we can't get a dtype from the descriptor, try and get one from the value logger.info("Creating dataset for %s (%s,%s)."%(str(name),str(shape),str(dtype))) f.create_dataset(name,[1] + ([] if list(shape) == [1] else list(shape)), maxshape=[None] + ([] if list(shape) == [1] else list(shape)), dtype=dtype) dump_size += np.multiply.reduce(shape) * dtype.itemsize datasets[name] = f[name] datasets_index[name] = 0 # if we built from an empty descriptor if not item._changed: continue else: logger.info("Adding %s to dataset. New size is %i."%(name,datasets_index[name]+1)) f[name].resize(datasets_index[name]+1, axis=0) # now we store this x engine's data for sending sd data. if sd_frame is not None and name.startswith("xeng_raw"): xeng_id = int(name[8:]) sd_frame[xeng_id::meta['n_xengs']] = ig[name] logger.debug('Received data for Xeng %i @ %.4f' % (xeng_id, time.time())) # we got a timestamp. if sd_frame is not None and name.startswith("timestamp"): xeng_id = int(name[9:]) timestamp = ig['sync_time'] + (ig[name] / ig['scale_factor_timestamp']) #in seconds since unix epoch localTime = time.time() print "Decoded timestamp for Xeng", xeng_id, ":", timestamp, " (", time.ctime(timestamp),") @ %.4f" % localTime, " ", time.ctime(localTime), "diff(", localTime-timestamp, ")" # is this timestamp in the past? if currentTimestamp > timestamp: errorString = "Timestamp %.2f (%s) is earlier than the current timestamp %.2f (%s). Ignoring..." % (timestamp, time.ctime(timestamp), currentTimestamp, time.ctime(currentTimestamp)) logger.warning(errorString) continue # is this a new timestamp before a complete set? if (timestamp > currentTimestamp) and sd_slots.any(): errorString = "New timestamp %.2f from Xeng%i before previous set %.2f sent" % (timestamp, xeng_id, currentTimestamp) logger.warning(errorString) sd_slots = np.zeros(meta['n_xengs']) sd_frame = np.zeros((ig['n_chans'],ig['n_bls'],2),dtype=sd_frame.dtype) currentTimestamp = -1 continue # is this new timestamp in the past for this X engine? if timestamp <= sd_slots[xeng_id]: errorString = 'Xeng%i already on timestamp %.2f but got %.2f now, THIS SHOULD NOT HAPPEN' % (xeng_id, sd_slots[xeng_id], timestamp) logger.error(errorString) raise RuntimeError(errorString) # update our info on which integrations we have sd_slots[xeng_id] = timestamp currentTimestamp = timestamp # do we have integration data and timestamps for all the xengines? If so, send the SD frame. if timestamp is not None and sd_frame is not None and sd_slots is not None and sd_slots.all(): ig_sd = spead.ItemGroup() # make sure we have the right dtype for the sd data ig_sd.add_item(name=('sd_data'), id=(0x3501), description="Combined raw data from all x engines.", ndarray=(sd_frame.dtype,sd_frame.shape)) ig_sd.add_item(name=('sd_timestamp'), id=0x3502, description='Timestamp of this sd frame in centiseconds since epoch (40 bit limitation).', shape=[], fmt=spead.mkfmt(('u',spead.ADDRSIZE))) t_it = ig_sd.get_item('sd_data') logger.info("Added SD frame with shape %s, dtype %s" % (str(t_it.shape),str(t_it.dtype))) scale_factor=(meta['n_accs'] if meta.has_key('n_accs') else 1) logger.info("Sending signal display frame with timestamp %i (%s). %s. @ %.4f" % (timestamp, time.ctime(timestamp), "Unscaled" if not acc_scale else "Scaled by %i" % (scale_factor), time.time())) ig_sd['sd_data'] = sd_frame.astype(np.float32) if not acc_scale else (sd_frame / float(scale_factor)).astype(np.float32) ig_sd['sd_timestamp'] = int(timestamp * 100) tx_sd.send_heap(ig_sd.get_heap()) # reset the arrays that hold integration data sd_slots = np.zeros(meta['n_xengs']) sd_frame = np.zeros((ig['n_chans'],ig['n_bls'],2),dtype=sd_frame.dtype) timestamp = None f[name][datasets_index[name]] = ig[name] datasets_index[name] += 1 item._changed = False idx+=1 logger.info("Got a SPEAD end-of-stream marker. Closing File.") f.flush() f.close() rx.stop() sd_frame = None sd_slots = None ig_sd = None
o.add_option('--insel', type='int', default=0x00000000, help='Input selection. Hex word where each hex valuecorresponds to an input type on the roach.0 = adc, 1,2 = digital noise, 3 = digital zero.') opts,args = o.parse_args(sys.argv[1:]) #Set up reciever and item group.The arr variable is so that spead knows to unpack numpy arrays(added when new item group is added, instead of fmt and shape, add narray=arr). This makes unpacking faster, whenever we need it. arr = N.zeros(NCHAN*2) arr = N.array(arr, dtype=N.int32) #rx = S.TransportUDPrx(opts.port, pkt_count=4096) rx = S.TransportUDPrx(opts.port+1)#, pkt_count=4096) ig = S.ItemGroup() for id in rpoco8.FPGA_TX_RESOURCES: bram,name,fmt,shape = rpoco8.FPGA_TX_RESOURCES[id] if name != 'acc_num': ig.add_item(name=name, id=id , fmt=fmt, shape=shape) else: ig.add_item(name=name, id=id, fmt=fmt, shape=[]) ig.add_item(name='data_timestamp', id=rpoco8.TIMESTAMP_ID,fmt=S.mkfmt(('u',64)),shape=[-1]) # #the values of the items that need to be written too in miriad. #sdf=input('sdf(d)=') change in frequency between channels. #sfreq=input('sfreq(d)=') starting frequency, off 0th channel. #nchan=input('nchan(i)=') number of channels #inttime=input('inttime(d)=') integration time #bandpass=input('bandpass=') array of size nchan x nants inttime=(2**30)*5e-9 sfreq = 0.200 sdf = -0.10/1024. fre = N.arange(8,dtype = N.integer)