def acc2sql(): ''' This is just a test version to read the stateframe once a second from the ACC and send it to the SQL server. A more complete version of this is implemented in schedule.py for "production" work. ''' # Get stateframe structure and version accini = stateframe.rd_ACCfile() sf_version = accini['version'] brange, outlist = sfdef(accini['sf']) with pyodbc.connect("DRIVER={FreeTDS};SERVER=192.168.24.106,1433; \ DATABASE=eOVSA06;UID=aaa;PWD=I@bsbn2w;") as cnxn: cursor = cnxn.cursor() lineno = 0 while 1: lineno += 1 data, msg = stateframe.get_stateframe(accini) version = stateframe.extract(data,['d',8]) if version == sf_version: bufout = transmogrify(data, brange) try: cursor.execute('insert into fBin (Bin) values (?)',pyodbc.Binary(bufout)) print 'Record '+str(lineno)+' successfully written\r', cnxn.commit() except: # An exception could be an error, or just that the entry was already inserted pass else: print 'Error: Incompatible version in stateframe.' break time.sleep(1)
def set_fem_attn(level=3, ant_str='ant1-15'): ''' Read current power and attenuation values in FEMs, and set the attenuation such that the power level will be as close to "level" as possible. ''' accini = stf.rd_ACCfile() acc = {'host': accini['host'], 'scdport': accini['scdport']} ant_list = ant_str2list(ant_str) if ant_list is None: return 'Bad antenna list' accini = stf.rd_ACCfile() hatn1 = np.zeros((10, 15), dtype='int') vatn1 = np.zeros((10, 15), dtype='int') hatn2 = np.zeros((10, 15), dtype='int') vatn2 = np.zeros((10, 15), dtype='int') hpwr = np.zeros((10, 15), dtype='float') vpwr = np.zeros((10, 15), dtype='float') # Read 10 instances of attenuation and power, and take the median to avoid # glitches for i in range(10): # Read the frontend attenuations and powers for each antenna data, msg = stf.get_stateframe(accini) for iant in range(15): fem = accini['sf']['Antenna'][iant]['Frontend']['FEM'] hatn1[i, iant] = stf.extract(data, fem['HPol']['Attenuation']['First']) vatn1[i, iant] = stf.extract(data, fem['VPol']['Attenuation']['First']) hatn2[i, iant] = stf.extract(data, fem['HPol']['Attenuation']['Second']) vatn2[i, iant] = stf.extract(data, fem['VPol']['Attenuation']['Second']) hpwr[i, iant] = stf.extract(data, fem['HPol']['Power']) vpwr[i, iant] = stf.extract(data, fem['VPol']['Power']) time.sleep(1) hatn1 = np.median(hatn1, 0).astype('int') vatn1 = np.median(vatn1, 0).astype('int') hatn2 = np.median(hatn2, 0).astype('int') vatn2 = np.median(vatn2, 0).astype('int') hpwr = np.median(hpwr, 0) vpwr = np.median(vpwr, 0) hatn2 = np.clip(hatn2 - (level - hpwr).astype('int'), 0, 31) vatn2 = np.clip(vatn2 - (level - vpwr).astype('int'), 0, 31) # Send attenuation commands to each antenna in ant_list for iant in ant_list: hatn = str(hatn1[iant]) + ' ' + str( hatn2[iant]) + ' ant' + str(iant + 1) vatn = str(vatn1[iant]) + ' ' + str( vatn2[iant]) + ' ant' + str(iant + 1) send_cmds(['HATTN ' + hatn, 'VATTN ' + vatn], acc) return 'Success'
def chk_lo1a(accini, band, iteration=1): data, msg = stf.get_stateframe(accini) errstr = stf.extract(data,accini['sf']['LODM']['LO1A']['ERR']).split('"')[1] if iteration == 1 and errstr != 'No error': # Looks like a reboot of LO1A is needed! print '10-s delay while attempting to reboot LO1A' send_cmds(['LO1A-REBOOT'],acc) time.sleep(10) acc = {'host': accini['host'], 'scdport':accini['scdport']} acc_tune(band+1,acc) time.sleep(5) errstr = chk_lo1a(accini,band,iteration=2) if errstr != 'No error': errstr = 'Reboot attempt failed.' return errstr
def fseq_is_running(fseqfile, accini=None): ''' Check current stateframe to see if the given sequence file is running. Returns True if so, False otherwise ''' import stateframe as stf if accini is None: accini = stf.rd_ACCfile() # Make sure this sequence is actually running, or start it if not buf, msg = stf.get_stateframe(accini) if msg != 'No Error': print 'Error reading stateframe.' return None fseq = stf.extract(buf, accini['sf']['LODM']['LO1A']['FSeqFile']) fseq = fseq.strip('\x00') # strip nulls from name result = fseq == fseqfile and stf.extract( buf, accini['sf']['LODM']['LO1A']['SweepStatus']) == 8 return result
def set_fem_attn(level=3,ant_str='ant1-15'): ''' Read current power and attenuation values in FEMs, and set the attenuation such that the power level will be as close to "level" as possible. ''' accini = stf.rd_ACCfile() acc = {'host': accini['host'], 'scdport':accini['scdport']} ant_list = ant_str2list(ant_str) if ant_list is None: return 'Bad antenna list' accini = stf.rd_ACCfile() hatn1 = np.zeros((10,15),dtype='int') vatn1 = np.zeros((10,15),dtype='int') hatn2 = np.zeros((10,15),dtype='int') vatn2 = np.zeros((10,15),dtype='int') hpwr = np.zeros((10,15),dtype='float') vpwr = np.zeros((10,15),dtype='float') # Read 10 instances of attenuation and power, and take the median to avoid # glitches for i in range(10): # Read the frontend attenuations and powers for each antenna data, msg = stf.get_stateframe(accini) for iant in range(15): fem = accini['sf']['Antenna'][iant]['Frontend']['FEM'] hatn1[i,iant] = stf.extract(data,fem['HPol']['Attenuation']['First']) vatn1[i,iant] = stf.extract(data,fem['VPol']['Attenuation']['First']) hatn2[i,iant] = stf.extract(data,fem['HPol']['Attenuation']['Second']) vatn2[i,iant] = stf.extract(data,fem['VPol']['Attenuation']['Second']) hpwr[i,iant] = stf.extract(data,fem['HPol']['Power']) vpwr[i,iant] = stf.extract(data,fem['VPol']['Power']) time.sleep(1) hatn1 = np.median(hatn1,0).astype('int') vatn1 = np.median(vatn1,0).astype('int') hatn2 = np.median(hatn2,0).astype('int') vatn2 = np.median(vatn2,0).astype('int') hpwr = np.median(hpwr,0) vpwr = np.median(vpwr,0) hatn2 = np.clip(hatn2 - (level - hpwr).astype('int'),0,31) vatn2 = np.clip(vatn2 - (level - vpwr).astype('int'),0,31) # Send attenuation commands to each antenna in ant_list for iant in ant_list: hatn = str(hatn1[iant])+' '+str(hatn2[iant])+' ant'+str(iant+1) vatn = str(vatn1[iant])+' '+str(vatn2[iant])+' ant'+str(iant+1) send_cmds(['HATTN '+hatn,'VATTN '+vatn],acc) return 'Success'
def old_version_test(sflog=None,sfxml=None,outbinfile=None,outtabfile=None): ''' Read stateframe log files of older versions and create output file of rearranged binary data, and corresponding stateframedef table as text file. sflog = file name of stateframe log to read sfxml = file name of corresponding XML file outbinfile = file name of output binary data file outtabfile = file name of output table text file ''' if sfxml: sf, version = rxml.xml_ptrs(sfxml) else: sf = None version = 0.0 if sflog: try: f = open(sflog,'rb') except: print 'Could not open file',sflog,'-- Exiting.' return # Get binary size and check version number data = f.read(100) if stateframe.extract(data,['d',8]) != version: print 'Stateframe log file version does not match XML version. -- Exiting' return recsize = stateframe.extract(data,sf['Binsize']) f.close() else: # No log file specified, so we will try to read directly from ACC once per second # Read one as a test and get its version number # Read from ACC accini = stateframe.rd_ACCfile() data, msg = stateframe.get_stateframe(accini) version = stateframe.extract(data,['d',8]) # Parse the stateframe dictionary and generate the brange and outlist dicts brange, outlist = sfdef(sf) # Calculate the startbytes in the list -- modifies outlist in place startbyte(outlist) stdout = sys.stdout # Save current stdout if outtabfile: # Write the table info to the given file name -- just sets stdout to the file, # writes it, and resets stdout try: sys.stdout = open(outtabfile,'w') except: print 'Could not redirect STDOUT to',outtabfile,' -- Will print to screen' sys.stdout = stdout outlist2table(outlist,version) sys.stdout = stdout # Reset to standard stdout if outbinfile: try: g = open(outbinfile,'wb') except: print 'Could not open file',outbinfile,'for writing. -- Exiting.' return if sflog: # Read from log file f = open(sflog,'rb') while 1: # Read and rearrange 1000 records try: indata = f.read(recsize) outdata = transmogrify(indata,brange) g.write(outdata) except: f.close() g.close() return else: # Read from ACC accini = stateframe.rd_ACCfile() for i in range(60): # Read from ACC and rearrange 60 records -- takes 1 min indata, msg = stateframe.get_stateframe(accini) outdata = transmogrify(indata,brange) g.write(outdata) time.sleep(1) g.close() return