コード例 #1
0
def sweepSource(statusMessageQueue, ctrlNs, printQueue, ringBuf):



	from SignalHound import SignalHound

	logSetup.initLogging(printQ = printQueue)
	loop_timer = time.time()
	print "Starting sweep-logger!"
	log = logging.getLogger("Main.AcqProcess")

	loop_timer = time.time()
	seq_num = 0

	sh = SignalHound()
	startAcquisition(sh, statusMessageQueue)


	temperature = sh.getDeviceDiagnostics()["temperature"]

	while ctrlNs.run:
		bufPtr, lock = ringBuf.getAddPointer()
		try:
			sh.fetchRaw_s(ctDataBufPtr=bufPtr)
		except Exception:
			log.error("IOError in Acquisition Thread!")
			log.error(traceback.format_exc())

			statusMessageQueue.put({"status" : (time.time(), "Error: Device interface crashed. Reinitializing")})
			log.error("Resetting hardware!")
			# sh.preset()
			sh.forceClose()
			try:
				while 1:
					log.warning("Freeing python device handle")
					del(sh)
			except UnboundLocalError:
				pass

			log.error("Hardware shut down, completely re-initializing device interface!")
			# sys.exit()
			sh = SignalHound()
			startAcquisition(sh, statusMessageQueue)
		finally:
			lock.release()

		if seq_num % PRINT_LOOP_CNT == 0:
			now = time.time()
			delta = now-loop_timer
			updateInterval = delta / PRINT_LOOP_CNT
			freq = 1 / updateInterval
			log.info("Elapsed Time = %0.5f, Frequency = %s. Items in buffer = %s", delta, freq, ringBuf.getItemsNum())
			loop_timer = now

			# print

		if seq_num % CAL_CHK_LOOP_CNT == 0:
			diags = sh.getDeviceDiagnostics()
			statusMessageQueue.put({"status" : (time.time(), diags)})

			temptmp = diags["temperature"]
			if abs(temperature - temptmp) > 2.0:    # Temperature deviations of > 2° cause IF shifts. Therefore, we do a re-cal if they're detected
				statusMessageQueue.put({"status" : (time.time(), "Recalibrating IF due to temperature change")})
				sh.selfCal()
				startAcquisition(sh, statusMessageQueue)
				log.warning("Temperature changed > 2.0 C. Delta is %f. Recalibrated!", abs(temperature - temptmp))
				temperature = temptmp
			else:
				log.info("Temperature deviation = %f. Not doing recal, since drift < 2C", abs(temperature - temptmp))

		seq_num += 1



	sh.abort()
	sh.closeDevice()

	del(sh)

	ctrlNs.acqRunning = False


	log.info("Acquisition-thread exiting!")
コード例 #2
0
def sweepSource(dataQueues, ctrlNs, printQueue):

	dataQueue, plotQueue = dataQueues


	from SignalHound import SignalHound

	logSetup.initLogging(printQ = printQueue)
	loop_timer = time.time()
	print "Starting sweep-logger!"
	log = logging.getLogger("Main.AcqProcess")

	loop_timer = time.time()
	loops = 0

	sh = SignalHound()
	startAcquisition(sh, dataQueue, plotQueue)

	# Send the trace size to the acq thread so I can properly set up the data-log file
	numPoints = sh.queryTraceInfo()["arr-size"]
	dataQueue.put({"arrSize" : numPoints})

	temperature = sh.getDeviceDiagnostics()["temperature"]


	runningSum = np.array(())
	runningSumItems = 0
	startFreq = 0


	while 1:
		try:
			trace = sh.fetchTrace()
			traceInfo = sh.queryTraceInfo()
			dataDict = {
							"info": traceInfo,
							"data": trace
						}

			acqInfo = dataDict["info"]
			if runningSum.shape != dataDict["data"]["max"].shape:
				runningSum = np.zeros_like(dataDict["data"]["max"])
				runningSumItems = 0
				startFreq = acqInfo["ret-start-freq"]
				binSize = acqInfo["arr-bin-size"]
				log.info("Running average array size changed! Either the system just started, or something is seriously wrong!")

			changed = False
			if startFreq != acqInfo["ret-start-freq"]:
				changed = True

			else:
				runningSum += dataDict["data"]["max"]
				runningSumItems += 1



			# if we've reached the number of average items per output array, or the frequency has changed, requiring an early dump of the specra data.
			if runningSumItems == NUM_AVERAGE or changed:

				# Divide down to the average
				arr = runningSum / runningSumItems

				# Build array to write out.
				saveTime = time.time()
				# log.info("Saving data record with timestamp %f", saveTime)

				# Only write out to the file if we actually have data
				if runningSumItems != 0:


					dataQueue.put({"row" : (saveTime, startFreq, binSize, runningSumItems, arr)})
					if plotQueue:
						plotQueue.put({"row" : (saveTime, startFreq, binSize, runningSumItems, arr)})


					del(trace)


					runningSum = np.zeros_like(runningSum)
					log.info("Estimated items in processing queue %s", dataQueue.qsize())
					log.info("Running sum shape = %s, items = %s", runningSum.shape, runningSumItems)
					runningSumItems = 0

				# now = time.time()
				# delta = now-loop_timer
				# freq = 1 / (delta)
				# log.info("Elapsed Time = %0.5f, Frequency = %s", delta, freq)
				# loop_timer = now


				# If we wrote the output because the current spectra has changed, we need to update the running acq info variables with the new frequencies.
				if changed:
					log.info("Retuned! Old freq = %s, new freq = %s", startFreq, acqInfo["ret-start-freq"])

					runningSum += dataDict["data"]["max"]
					startFreq = acqInfo["ret-start-freq"]
					binSize = acqInfo["arr-bin-size"]
					runningSumItems = 1




		except Exception:
			log.error("IOError in Acquisition Thread!")
			log.error(traceback.format_exc())

			dataQueue.put({"status" : "Error: Device interface crashed. Reinitializing"})
			log.error("Resetting hardware!")
			# sh.preset()
			sh.forceClose()
			try:
				while 1:
					log.warning("Freeing python device handle")
					del(sh)
			except UnboundLocalError:
				pass

			log.error("Hardware shut down, completely re-initializing device interface!")
			# sys.exit()
			sh = SignalHound()
			startAcquisition(sh, dataQueue, plotQueue)

		if loops % PRINT_LOOP_CNT == 0:
			now = time.time()
			delta = now-loop_timer
			freq = 1 / (delta / PRINT_LOOP_CNT)
			# log.info("Elapsed Time = %0.5f, Frequency = %s", delta, freq)
			loop_timer = now

		if loops % CAL_CHK_LOOP_CNT == 0:
			diags = sh.getDeviceDiagnostics()
			dataQueue.put({"status" : diags})

			temptmp = diags["temperature"]
			if abs(temperature - temptmp) > 2.0:    # Temperature deviations of > 2° cause IF shifts. Therefore, we do a re-cal if they're detected
				dataQueue.put({"status" : "Recalibrating IF"})
				sh.selfCal()
				startAcquisition(sh, dataQueue, plotQueue)
				log.warning("Temperature changed > 2.0 C. Delta is %f. Recalibrated!", abs(temperature - temptmp))
				temperature = temptmp
			else:
				log.info("Temperature deviation = %f. Not doing recal, since drift < 2C", abs(temperature - temptmp))

		loops += 1

		if ctrlNs.run == False:
			log.info("Stopping Acq-thread!")
			break


	sh.abort()
	sh.closeDevice()

	del(sh)



	log.info("Acq-thread closing dataQueue!")
	dataQueue.close()
	dataQueue.join_thread()
	if plotQueue:
		plotQueue.close()
		plotQueue.cancel_join_thread()

	ctrlNs.acqRunning = False

	log.info("Acq-thread exiting!")
	printQueue.close()
	printQueue.join_thread()
コード例 #3
0
def sweepSource(dataQueues, ctrlNs, printQueue):

    dataQueue, plotQueue = dataQueues

    from SignalHound import SignalHound

    logSetup.initLogging(printQ=printQueue)
    loop_timer = time.time()
    print "Starting sweep-logger!"
    log = logging.getLogger("Main.AcqProcess")

    loop_timer = time.time()
    loops = 0

    sh = SignalHound()
    startAcquisition(sh, dataQueue, plotQueue)

    # Send the trace size to the acq thread so I can properly set up the data-log file
    numPoints = sh.queryTraceInfo()["arr-size"]
    dataQueue.put({"arrSize": numPoints})

    temperature = sh.getDeviceDiagnostics()["temperature"]

    runningSum = np.array(())
    runningSumItems = 0
    startFreq = 0

    while 1:
        try:
            trace = sh.fetchTrace()
            traceInfo = sh.queryTraceInfo()
            dataDict = {"info": traceInfo, "data": trace}

            acqInfo = dataDict["info"]
            if runningSum.shape != dataDict["data"]["max"].shape:
                runningSum = np.zeros_like(dataDict["data"]["max"])
                runningSumItems = 0
                startFreq = acqInfo["ret-start-freq"]
                binSize = acqInfo["arr-bin-size"]
                log.info(
                    "Running average array size changed! Either the system just started, or something is seriously wrong!"
                )

            changed = False
            if startFreq != acqInfo["ret-start-freq"]:
                changed = True

            else:
                runningSum += dataDict["data"]["max"]
                runningSumItems += 1

            # if we've reached the number of average items per output array, or the frequency has changed, requiring an early dump of the specra data.
            if runningSumItems == NUM_AVERAGE or changed:

                # Divide down to the average
                arr = runningSum / runningSumItems

                # Build array to write out.
                saveTime = time.time()
                # log.info("Saving data record with timestamp %f", saveTime)

                # Only write out to the file if we actually have data
                if runningSumItems != 0:

                    dataQueue.put({
                        "row":
                        (saveTime, startFreq, binSize, runningSumItems, arr)
                    })
                    if plotQueue:
                        plotQueue.put({
                            "row": (saveTime, startFreq, binSize,
                                    runningSumItems, arr)
                        })

                    del (trace)

                    runningSum = np.zeros_like(runningSum)
                    log.info("Estimated items in processing queue %s",
                             dataQueue.qsize())
                    log.info("Running sum shape = %s, items = %s",
                             runningSum.shape, runningSumItems)
                    runningSumItems = 0

                # now = time.time()
                # delta = now-loop_timer
                # freq = 1 / (delta)
                # log.info("Elapsed Time = %0.5f, Frequency = %s", delta, freq)
                # loop_timer = now

                # If we wrote the output because the current spectra has changed, we need to update the running acq info variables with the new frequencies.
                if changed:
                    log.info("Retuned! Old freq = %s, new freq = %s",
                             startFreq, acqInfo["ret-start-freq"])

                    runningSum += dataDict["data"]["max"]
                    startFreq = acqInfo["ret-start-freq"]
                    binSize = acqInfo["arr-bin-size"]
                    runningSumItems = 1

        except Exception:
            log.error("IOError in Acquisition Thread!")
            log.error(traceback.format_exc())

            dataQueue.put(
                {"status": "Error: Device interface crashed. Reinitializing"})
            log.error("Resetting hardware!")
            # sh.preset()
            sh.forceClose()
            try:
                while 1:
                    log.warning("Freeing python device handle")
                    del (sh)
            except UnboundLocalError:
                pass

            log.error(
                "Hardware shut down, completely re-initializing device interface!"
            )
            # sys.exit()
            sh = SignalHound()
            startAcquisition(sh, dataQueue, plotQueue)

        if loops % PRINT_LOOP_CNT == 0:
            now = time.time()
            delta = now - loop_timer
            freq = 1 / (delta / PRINT_LOOP_CNT)
            # log.info("Elapsed Time = %0.5f, Frequency = %s", delta, freq)
            loop_timer = now

        if loops % CAL_CHK_LOOP_CNT == 0:
            diags = sh.getDeviceDiagnostics()
            dataQueue.put({"status": diags})

            temptmp = diags["temperature"]
            if abs(
                    temperature - temptmp
            ) > 2.0:  # Temperature deviations of > 2° cause IF shifts. Therefore, we do a re-cal if they're detected
                dataQueue.put({"status": "Recalibrating IF"})
                sh.selfCal()
                startAcquisition(sh, dataQueue, plotQueue)
                log.warning(
                    "Temperature changed > 2.0 C. Delta is %f. Recalibrated!",
                    abs(temperature - temptmp))
                temperature = temptmp
            else:
                log.info(
                    "Temperature deviation = %f. Not doing recal, since drift < 2C",
                    abs(temperature - temptmp))

        loops += 1

        if ctrlNs.run == False:
            log.info("Stopping Acq-thread!")
            break

    sh.abort()
    sh.closeDevice()

    del (sh)

    log.info("Acq-thread closing dataQueue!")
    dataQueue.close()
    dataQueue.join_thread()
    if plotQueue:
        plotQueue.close()
        plotQueue.cancel_join_thread()

    ctrlNs.acqRunning = False

    log.info("Acq-thread exiting!")
    printQueue.close()
    printQueue.join_thread()
コード例 #4
0
class InternalSweepAcqThread(object):
	log = logging.getLogger("Main.AcqProcess")

	def __init__(self, printQueue):
		self.printQueue = printQueue
		logSetup.initLogging(printQ=printQueue)

		self.calcScanBands()

		if ACQ_TYPE != "real-time-sweeping":
			raise ValueError("internalSweep module only supports 'real-time-sweeping' mode! Configured mode = {mode}".format(mode=ACQ_TYPE))

	def calcScanBands(self):
		if ACQ_SPAN < IF_WIDTH:
			raise ValueError("Scan width is smaller then the IF bandwidth!")
		if ACQ_SPAN == IF_WIDTH:
			raise ValueError("Scan width is exactly the IF bandwith. Maybe use the real-time mode instead?")

		sweepWidth = IF_WIDTH * (1-ACQ_OVERLAP)
		bins = ACQ_SPAN/sweepWidth
		sweepSteps = int(bins+0.5)

		effectiveScanWidth = sweepWidth*sweepSteps

		self.binFreqs = []
		baseFreq = ACQ_FREQ - (effectiveScanWidth/2 + sweepWidth/2)

		for x in xrange(1, sweepSteps+1):
			self.binFreqs.append(baseFreq+x*sweepWidth)

		self.binFreqIndice = 0

	def retune(self):
		self.sh.configureCenterSpan(center = self.binFreqs[self.binFreqIndice], span = IF_WIDTH)
		self.binFreqIndice = (self.binFreqIndice + 1) % len(self.binFreqs)


	def startAcquisition(self, dataQueue, plotQueue):


		self.sh.configureAcquisition(ACQ_MODE, ACQ_Y_SCALE)

		self.retune()

		self.sh.configureLevel(ref = ACQ_REF_LEVEL_DB, atten = ACQ_ATTENUATION_DB)
		self.sh.configureGain(gain = ACQ_GAIN_SETTING)
		self.sh.configureSweepCoupling(rbw = ACQ_RBW, vbw = ACQ_VBW, sweepTime = ACQ_SWEEP_TIME_SECONDS, rbwType = "native", rejection = "no-spur-reject")
		self.sh.configureWindow(window = ACQ_WINDOW_TYPE)
		self.sh.configureProcUnits(units = ACQ_UNITS)
		self.sh.configureTrigger(trigType = "none", edge = "rising-edge", level = 0, timeout = 5)

		self.sh.initiate(mode = "real-time", flag = "ignored")



		dataQueue.put({"settings" : self.sh.getCurrentAcquisitionSettings()})
		plotQueue.put({"settings" : self.sh.getCurrentAcquisitionSettings()})

	def sweepSource(self, dataQueues, ctrlNs):

		dataQueue, plotQueue = dataQueues


		from SignalHound import SignalHound


		loop_timer = time.time()
		print "Starting sweep-logger!"

		loop_timer = time.time()
		loops = 0

		self.sh = SignalHound()
		self.startAcquisition(dataQueue, plotQueue)

		# Send the trace size to the acq thread so I can properly set up the data-log file
		numPoints = self.sh.queryTraceInfo()["arr-size"]
		dataQueue.put({"arrSize" : numPoints})

		temperature = self.sh.getDeviceDiagnostics()["temperature"]


		runningSum = np.array(())
		runningSumItems = 0
		startFreq = 0


		while 1:
			try:


				trace = self.sh.fetchTrace()
				traceInfo = self.sh.queryTraceInfo()
				dataDict = {
								"info": traceInfo,
								"data": trace
							}

				acqInfo = dataDict["info"]
				if runningSum.shape != dataDict["data"]["max"].shape:
					runningSum = np.zeros_like(dataDict["data"]["max"])
					runningSumItems = 0
					startFreq = acqInfo["ret-start-freq"]
					binSize = acqInfo["arr-bin-size"]
					self.log.info("Running average array size changed! Either the system just started, or something is seriously wrong!")

				changed = False
				if startFreq != acqInfo["ret-start-freq"]:
					changed = True

				else:
					runningSum += dataDict["data"]["max"]
					runningSumItems += 1



				# if we've reached the number of average items per output array, or the frequency has changed, requiring an early dump of the specra data.
				if runningSumItems == NUM_AVERAGE or changed:
					self.log.info("Running sum shape = %s, items = %s", runningSum.shape, runningSumItems)
					# Divide down to the average
					arr = runningSum / runningSumItems

					# Build array to write out.
					saveTime = time.time()
					# self.log.info("Saving data record with timestamp %f", saveTime)

					# Only write out to the file if we actually have data
					if runningSumItems != 0:


						dataQueue.put({"row" : (saveTime, startFreq, binSize, runningSumItems, arr)})
						if plotQueue:
							plotQueue.put({"row" : (saveTime, startFreq, binSize, runningSumItems, arr)})





						runningSum = np.zeros_like(runningSum)
						self.log.info("Estimated items in processing queue %s", dataQueue.qsize())
						self.log.info("Running sum shape = %s, items = %s", runningSum.shape, runningSumItems)
						runningSumItems = 0

					# now = time.time()
					# delta = now-loop_timer
					# freq = 1 / (delta)
					# self.log.info("Elapsed Time = %0.5f, Frequency = %s", delta, freq)
					# loop_timer = now


					# If we wrote the output because the current spectra has changed, we need to update the running acq info variables with the new frequencies.
					if changed:
						self.log.info("Retuned! Old freq = %s, new freq = %s", startFreq, acqInfo["ret-start-freq"])

						runningSum += dataDict["data"]["max"]
						startFreq = acqInfo["ret-start-freq"]
						binSize = acqInfo["arr-bin-size"]
						runningSumItems = 1


					del(trace)




			except Exception:
				self.log.error("IOError in Acquisition Thread!")
				self.log.error(traceback.format_exc())

				dataQueue.put({"status" : "Error: Device interface craself.shed. Reinitializing"})
				self.log.error("Resetting hardware!")
				# self.sh.preset()
				self.sh.forceClose()
				try:
					while 1:
						self.log.warning("Trying to free python device handle")
						del(self.sh)
				except UnboundLocalError:
					self.log.info("Handle freed?")
				except AttributeError:
					self.log.info("Handle freed?")

				self.log.error("Hardware shut down, completely re-initializing device interface!")
				# sys.exit()

				self.log.info("Preparing to reset device.")
				self.sh = SignalHound()
				self.log.info("Performing hardware reset")
				self.sh.preset()

				self.log.info("Reset triggered. Waiting 5 seconds for device to restart.")
				time.sleep(5)


				self.log.info("Hardware reset. Reopening device.")
				self.sh = SignalHound()



				self.startAcquisition(dataQueue, dataQueue)


			loops += 1

			if loops % ACQ_BIN_SAMPLES == 0:
				print("Should retune frontend!")
				self.sh.abort()
				self.startAcquisition(dataQueue, dataQueue)

				# print("Current acq mode = ", self.sh.queryTraceInfo())

			if loops % PRINT_LOOP_CNT == 0:
				now = time.time()
				delta = now-loop_timer
				freq = 1 / (delta / PRINT_LOOP_CNT)
				# self.log.info("Elapsed Time = %0.5f, Frequency = %s", delta, freq)
				loop_timer = now

			if loops % CAL_CHK_LOOP_CNT == 0:
				diags = self.sh.getDeviceDiagnostics()
				dataQueue.put({"status" : diags})

				temptmp = diags["temperature"]
				if abs(temperature - temptmp) > 2.0:    # Temperature deviations of > 2° cause IF self.shifts. Therefore, we do a re-cal if they're detected
					dataQueue.put({"status" : "Recalibrating IF"})
					self.sh.selfCal()
					self.startAcquisition(dataQueue, dataQueue)
					self.log.warning("Temperature changed > 2.0 C. Delta is %f. Recalibrated!", abs(temperature - temptmp))
					temperature = temptmp
				else:
					self.log.info("Temperature deviation = %f. Not doing recal, since drift < 2C", abs(temperature - temptmp))


			if ctrlNs.run == False:
				self.log.info("Stopping Acq-thread!")
				break


		self.sh.abort()
		self.sh.closeDevice()

		del(self.sh)



		self.log.info("Acq-thread closing dataQueue!")
		dataQueue.close()
		dataQueue.join_thread()

		plotQueue.close()
		plotQueue.cancel_join_thread()

		ctrlNs.acqRunning = False

		self.log.info("Acq-thread exiting!")
		self.printQueue.close()
		self.printQueue.join_thread()
コード例 #5
0
class InternalSweepAcqThread(object):
    log = logging.getLogger("Main.AcqProcess")

    def __init__(self, printQueue):
        self.printQueue = printQueue
        logSetup.initLogging(printQ=printQueue)

        self.calcScanBands()

        if ACQ_TYPE != "real-time-sweeping":
            raise ValueError(
                "internalSweep module only supports 'real-time-sweeping' mode! Configured mode = {mode}"
                .format(mode=ACQ_TYPE))

    def calcScanBands(self):
        if ACQ_SPAN < IF_WIDTH:
            raise ValueError("Scan width is smaller then the IF bandwidth!")
        if ACQ_SPAN == IF_WIDTH:
            raise ValueError(
                "Scan width is exactly the IF bandwith. Maybe use the real-time mode instead?"
            )

        sweepWidth = IF_WIDTH * (1 - ACQ_OVERLAP)
        bins = ACQ_SPAN / sweepWidth
        sweepSteps = int(bins + 0.5)

        effectiveScanWidth = sweepWidth * sweepSteps

        self.binFreqs = []
        baseFreq = ACQ_FREQ - (effectiveScanWidth / 2 + sweepWidth / 2)

        for x in xrange(1, sweepSteps + 1):
            self.binFreqs.append(baseFreq + x * sweepWidth)

        self.binFreqIndice = 0

    def retune(self):
        self.sh.configureCenterSpan(center=self.binFreqs[self.binFreqIndice],
                                    span=IF_WIDTH)
        self.binFreqIndice = (self.binFreqIndice + 1) % len(self.binFreqs)

    def startAcquisition(self, dataQueue, plotQueue):

        self.sh.configureAcquisition(ACQ_MODE, ACQ_Y_SCALE)

        self.retune()

        self.sh.configureLevel(ref=ACQ_REF_LEVEL_DB, atten=ACQ_ATTENUATION_DB)
        self.sh.configureGain(gain=ACQ_GAIN_SETTING)
        self.sh.configureSweepCoupling(rbw=ACQ_RBW,
                                       vbw=ACQ_VBW,
                                       sweepTime=ACQ_SWEEP_TIME_SECONDS,
                                       rbwType="native",
                                       rejection="no-spur-reject")
        self.sh.configureWindow(window=ACQ_WINDOW_TYPE)
        self.sh.configureProcUnits(units=ACQ_UNITS)
        self.sh.configureTrigger(trigType="none",
                                 edge="rising-edge",
                                 level=0,
                                 timeout=5)

        self.sh.initiate(mode="real-time", flag="ignored")

        dataQueue.put({"settings": self.sh.getCurrentAcquisitionSettings()})
        plotQueue.put({"settings": self.sh.getCurrentAcquisitionSettings()})

    def sweepSource(self, dataQueues, ctrlNs):

        dataQueue, plotQueue = dataQueues

        from SignalHound import SignalHound

        loop_timer = time.time()
        print "Starting sweep-logger!"

        loop_timer = time.time()
        loops = 0

        self.sh = SignalHound()
        self.startAcquisition(dataQueue, plotQueue)

        # Send the trace size to the acq thread so I can properly set up the data-log file
        numPoints = self.sh.queryTraceInfo()["arr-size"]
        dataQueue.put({"arrSize": numPoints})

        temperature = self.sh.getDeviceDiagnostics()["temperature"]

        runningSum = np.array(())
        runningSumItems = 0
        startFreq = 0

        while 1:
            try:

                trace = self.sh.fetchTrace()
                traceInfo = self.sh.queryTraceInfo()
                dataDict = {"info": traceInfo, "data": trace}

                acqInfo = dataDict["info"]
                if runningSum.shape != dataDict["data"]["max"].shape:
                    runningSum = np.zeros_like(dataDict["data"]["max"])
                    runningSumItems = 0
                    startFreq = acqInfo["ret-start-freq"]
                    binSize = acqInfo["arr-bin-size"]
                    self.log.info(
                        "Running average array size changed! Either the system just started, or something is seriously wrong!"
                    )

                changed = False
                if startFreq != acqInfo["ret-start-freq"]:
                    changed = True

                else:
                    runningSum += dataDict["data"]["max"]
                    runningSumItems += 1

                # if we've reached the number of average items per output array, or the frequency has changed, requiring an early dump of the specra data.
                if runningSumItems == NUM_AVERAGE or changed:
                    self.log.info("Running sum shape = %s, items = %s",
                                  runningSum.shape, runningSumItems)
                    # Divide down to the average
                    arr = runningSum / runningSumItems

                    # Build array to write out.
                    saveTime = time.time()
                    # self.log.info("Saving data record with timestamp %f", saveTime)

                    # Only write out to the file if we actually have data
                    if runningSumItems != 0:

                        dataQueue.put({
                            "row": (saveTime, startFreq, binSize,
                                    runningSumItems, arr)
                        })
                        if plotQueue:
                            plotQueue.put({
                                "row": (saveTime, startFreq, binSize,
                                        runningSumItems, arr)
                            })

                        runningSum = np.zeros_like(runningSum)
                        self.log.info("Estimated items in processing queue %s",
                                      dataQueue.qsize())
                        self.log.info("Running sum shape = %s, items = %s",
                                      runningSum.shape, runningSumItems)
                        runningSumItems = 0

                    # now = time.time()
                    # delta = now-loop_timer
                    # freq = 1 / (delta)
                    # self.log.info("Elapsed Time = %0.5f, Frequency = %s", delta, freq)
                    # loop_timer = now

                    # If we wrote the output because the current spectra has changed, we need to update the running acq info variables with the new frequencies.
                    if changed:
                        self.log.info("Retuned! Old freq = %s, new freq = %s",
                                      startFreq, acqInfo["ret-start-freq"])

                        runningSum += dataDict["data"]["max"]
                        startFreq = acqInfo["ret-start-freq"]
                        binSize = acqInfo["arr-bin-size"]
                        runningSumItems = 1

                    del (trace)

            except Exception:
                self.log.error("IOError in Acquisition Thread!")
                self.log.error(traceback.format_exc())

                dataQueue.put({
                    "status":
                    "Error: Device interface craself.shed. Reinitializing"
                })
                self.log.error("Resetting hardware!")
                # self.sh.preset()
                self.sh.forceClose()
                try:
                    while 1:
                        self.log.warning("Trying to free python device handle")
                        del (self.sh)
                except UnboundLocalError:
                    self.log.info("Handle freed?")
                except AttributeError:
                    self.log.info("Handle freed?")

                self.log.error(
                    "Hardware shut down, completely re-initializing device interface!"
                )
                # sys.exit()

                self.log.info("Preparing to reset device.")
                self.sh = SignalHound()
                self.log.info("Performing hardware reset")
                self.sh.preset()

                self.log.info(
                    "Reset triggered. Waiting 5 seconds for device to restart."
                )
                time.sleep(5)

                self.log.info("Hardware reset. Reopening device.")
                self.sh = SignalHound()

                self.startAcquisition(dataQueue, dataQueue)

            loops += 1

            if loops % ACQ_BIN_SAMPLES == 0:
                print("Should retune frontend!")
                self.sh.abort()
                self.startAcquisition(dataQueue, dataQueue)

                # print("Current acq mode = ", self.sh.queryTraceInfo())

            if loops % PRINT_LOOP_CNT == 0:
                now = time.time()
                delta = now - loop_timer
                freq = 1 / (delta / PRINT_LOOP_CNT)
                # self.log.info("Elapsed Time = %0.5f, Frequency = %s", delta, freq)
                loop_timer = now

            if loops % CAL_CHK_LOOP_CNT == 0:
                diags = self.sh.getDeviceDiagnostics()
                dataQueue.put({"status": diags})

                temptmp = diags["temperature"]
                if abs(
                        temperature - temptmp
                ) > 2.0:  # Temperature deviations of > 2° cause IF self.shifts. Therefore, we do a re-cal if they're detected
                    dataQueue.put({"status": "Recalibrating IF"})
                    self.sh.selfCal()
                    self.startAcquisition(dataQueue, dataQueue)
                    self.log.warning(
                        "Temperature changed > 2.0 C. Delta is %f. Recalibrated!",
                        abs(temperature - temptmp))
                    temperature = temptmp
                else:
                    self.log.info(
                        "Temperature deviation = %f. Not doing recal, since drift < 2C",
                        abs(temperature - temptmp))

            if ctrlNs.run == False:
                self.log.info("Stopping Acq-thread!")
                break

        self.sh.abort()
        self.sh.closeDevice()

        del (self.sh)

        self.log.info("Acq-thread closing dataQueue!")
        dataQueue.close()
        dataQueue.join_thread()

        plotQueue.close()
        plotQueue.cancel_join_thread()

        ctrlNs.acqRunning = False

        self.log.info("Acq-thread exiting!")
        self.printQueue.close()
        self.printQueue.join_thread()