def do_get_nop(self):
		'''
		Get Number of Points (nop) for sweep

		Input:
			None
		Output:
			nop (int)
		'''
		logging.debug(__name__ + ' : getting Number of Points')
		_signal_hound.initiate(self._device, _signal_hound.sweeping, 0)
		nop, start_freq, bin_size = _signal_hound.query_sweep_info(self._device)
		return nop 
	def do_get_startfreq(self):
		'''
		Get Start frequency
		
		Input:
			None

		Output:
			span (float) : Start Frequency in Hz
		'''
		logging.debug(__name__ + ' : getting start frequency')
		_signal_hound.initiate(self._device, _signal_hound.sweeping, 0)
		nop, start_freq, bin_size = _signal_hound.query_sweep_info(self._device)
		return start_freq
	def do_get_centerfreq(self):
		'''
		Get the center frequency

		Input:
			None

		Output:
			cf (float) :Center Frequency in Hz
		'''
		logging.debug(__name__ + ' : getting center frequency')
		_signal_hound.initiate(self._device, _signal_hound.sweeping, 0)
		nop, start_freq, bin_size = _signal_hound.query_sweep_info(self._device)
		return start_freq+(nop-1)*bin_size/2.
	def do_get_span(self):
		'''
		Get Span
		
		Input:
			None

		Output:
			span (float) : Span in Hz
		'''
		#logging.debug(__name__ + ' : getting center frequency')
		_signal_hound.initiate(self._device, _signal_hound.sweeping, 0)
		nop, start_freq, bin_size = _signal_hound.query_sweep_info(self._device)
		span = (nop-1)*bin_size #float( self.ask('SENS1:FREQ:SPAN?'))
		return span
	def do_set_span(self,span):
		'''
		Set Span

		Input:
			span (float) : Span in KHz

		Output:
			None
		'''
		logging.debug(__name__ + ' : setting span to %s Hz' % span)
		_signal_hound.initiate(self._device, _signal_hound.sweeping, 0)
		nop, start_freq, bin_size = _signal_hound.query_sweep_info(self._device)
		start = start_freq
		if nop<2: nop = 2
		stop = start_freq + (nop-1)*bin_size
		
		_signal_hound.config_center_span(self._device, (start+stop)*0.5, span)
		
		self.get_startfreq();
		self.get_stopfreq();
		self.get_centerfreq(); 
	def get_tracedata(self):
		'''
		Get the data of the current trace in dBm

		Output:
			'AmpPha':_ Amplitude and Phase
		'''
		_signal_hound.initiate(self._device, _signal_hound.sweeping, 0)
		nop, start_freq, bin_size = _signal_hound.query_sweep_info(self._device)
		min = (ctypes.c_float*nop)()
		max = (ctypes.c_float*nop)()
		end = 0
		
		while end < nop:
			begin, end = _signal_hound.get_partial_sweep_32f(self._device, min, max)
			plt.pause(0.05)
			
		datax = np.linspace(start_freq, start_freq+bin_size*(nop-1), nop)
		datamin = 10**(np.asarray(max, dtype=np.float)/10)
		datamax = 10**(np.asarray(min, dtype=np.float)/10)
		
		return [datax, datamin, datamax]
	def do_set_video_bw(self, video_bw):
		vbw_max = 250e3
		vbw_min = 0.1
		
		_signal_hound.initiate(self._device, _signal_hound.sweeping, 0)
		nop, start_freq, bin_size = _signal_hound.query_sweep_info(self._device)
		span = (nop-1)*bin_size
		
		res_bw = self.get_res_bw()
		reject_if = self.get_reject_if()
		if not res_bw: 
			res_bw = 250e3
		if video_bw > res_bw:
			video_bw = res_bw
			self.set_video_bw(video_bw)
			return
		vbw_6MHz_allowed = False
		
		if span > 100e6:
			vbw_min = 6.5e3
		if span > 200e3 and start_freq < 16e6:
			vbw_min = 6.5e3
		if start_freq > 200e6 and span > 200e6:
			vbw_6MHz_allowed = True
			
		if video_bw > vbw_max:
			if vbw_6MHz_allowed and video_bw>3e6:
				video_wb = 6e6
			else:
				video_bw = vbw_max
			self.set_video_bw(video_bw)
			return
		if video_bw < vbw_min:
			video_bw = vbw_min
			self.set_video_bw(video_bw)
			return
			
		_signal_hound.config_sweep_coupling(self._device, res_bw, video_bw, reject_if)
	def do_set_stopfreq(self,val):
		'''
		Set STop frequency

		Input:
			val (float) : Stop Frequency in Hz

		Output:
			None
		'''
		logging.debug(__name__ + ' : setting start freq to %s Hz' % val)
		_signal_hound.initiate(self._device, _signal_hound.sweeping, 0)
		nop, start_freq, bin_size = _signal_hound.query_sweep_info(self._device)
		if nop<2: nop = 1
		#stop = start_freq + (nop-1)*bin_size 
		new_center = (val+start_freq)*0.5
		new_span = val - start_freq
		
		_signal_hound.config_center_span(self._device, new_center, new_span)
		
		self.get_centerfreq();
		self.get_stopfreq();
		self.get_span();
	def do_set_centerfreq(self,cf):
		'''
		Set the center frequency

		Input:
			cf (float) :Center Frequency in Hz

		Output:
			None
		'''
		logging.debug(__name__ + ' : setting center frequency to %s' % cf)
		_signal_hound.initiate(self._device, _signal_hound.sweeping, 0)
		nop, start_freq, bin_size = _signal_hound.query_sweep_info(self._device)
		if nop<2: nop = 2
		start = start_freq
		stop = start_freq + (nop-1)*bin_size
		span = stop-start
		
		_signal_hound.config_center_span(self._device, cf, span)
		
		self.get_startfreq();
		self.get_stopfreq();
		self.get_span();
	def __init__(self, name, serial):
		'''
		Initializes 

		Input:
			name (string)    : name of the instrument
			address (string) : GPIB address
		'''
		
		logging.info(__name__ + ' : Initializing instrument')
		Instrument.__init__(self, name, tags=['physical'])
		self._device = _signal_hound.open_device_by_serial_number(serial)

		self._serial = serial
		
		# Implement parameters
		
		self.add_parameter('ref', type=float,
			flags=Instrument.FLAG_SET|Instrument.FLAG_SOFTGET,
			minval=-80, maxval=_signal_hound.max_ref,
			units='dBm', tags=['sweep']) 
		
		self.add_parameter('reject_if', type=bool,
			flags=Instrument.FLAG_SET|Instrument.FLAG_SOFTGET, 
			tags=['sweep'])
		
		self.add_parameter('nop', type=int,
			flags=Instrument.FLAG_GET,
			tags=['sweep'])

		self.add_parameter('video_bw', type=float,
			flags=Instrument.FLAG_SET|Instrument.FLAG_SOFTGET,
			minval=_signal_hound.min_rbw, maxval=_signal_hound.max_rbw,
			units='Hz', tags=['sweep']) 

		self.add_parameter('res_bw', type=float,
			flags=Instrument.FLAG_SET|Instrument.FLAG_SOFTGET,
			minval=_signal_hound.min_rbw, maxval=_signal_hound.max_rbw,
			units='Hz', tags=['sweep']) 

		self.add_parameter('centerfreq', type=float,
			flags=Instrument.FLAG_GETSET,
			minval=_signal_hound.sa124_min_freq, maxval=_signal_hound.sa124_max_freq,
			units='Hz', tags=['sweep'])
			
		self.add_parameter('startfreq', type=float,
			flags=Instrument.FLAG_GETSET,
			minval=_signal_hound.sa124_min_freq, maxval=_signal_hound.sa124_max_freq,
			units='Hz', tags=['sweep'])            
			
		self.add_parameter('stopfreq', type=float,
			flags=Instrument.FLAG_GETSET,
			minval=_signal_hound.sa124_min_freq, maxval=_signal_hound.sa124_max_freq,
			units='Hz', tags=['sweep'])                        
			
		self.add_parameter('span', type=float,
			flags=Instrument.FLAG_GETSET,
			minval=_signal_hound.min_span, maxval=_signal_hound.sa124_max_freq,
			units='Hz', tags=['sweep'])        

#		self.add_parameter('zerospan', type=bool,
#			flags=Instrument.FLAG_GETSET)
					
		#Triggering Stuff
#		self.add_parameter('trigger_source', type=str,
#			flags=Instrument.FLAG_GETSET)
		
		
		# Implement functions
		self.add_function('get_freqpoints')
		self.add_function('get_tracedata')
		self.add_function('set_xlim')
		self.add_function('get_xlim')
		#self.add_function('avg_clear')
		#self.add_function('avg_status')
		
		#self._oldspan = self.get_span()
		#self._oldnop = self.get_nop()
		#if self._oldspan==0.002:
		#  self.set_zerospan(True)
		
		self.set_xlim(_signal_hound.sa124_min_freq, _signal_hound.sa124_max_freq)
		self.set_res_bw(_signal_hound.max_rbw)
		self.set_video_bw(_signal_hound.max_rbw)
		self.set_reject_if(True)
		_signal_hound.initiate(self._device, _signal_hound.sweeping, 0)
		
		self.get_all()
	def get_xlim(self):
		_signal_hound.initiate(self._device, _signal_hound.sweeping, 0)
		nop, start_freq, bin_size = _signal_hound.query_sweep_info(self._device)
		start = start_freq
		stop = start_freq + (nop-1)*bin_size
		return start, stop
	def set_xlim(self, start, stop):
		logging.debug(__name__ + ' : setting start freq to %s Hz and stop freq to %s Hz' % (start, stop))
		_signal_hound.initiate(self._device, _signal_hound.sweeping, 0)
		_signal_hound.config_center_span(self._device, (start+stop)*0.5, stop-start)
	def get_freqpoints(self, query = False):      
		_signal_hound.initiate(self._device, _signal_hound.sweeping, 0)
		nop, start_freq, bin_size = _signal_hound.query_sweep_info(self._device)
		return np.linspace(start_freq, start_freq+bin_size*(nop-1), nop)