class Range(object):
	"""A range of frequencies or velocities

	Usage:
		Range.unit  = unit
		r = Range(lower, upper)

	Examples:
		# A frequency range
		>>> Range.unit = "GHz"
		>>> f1 = Range(100, 200)
		>>> print(f1)
		frequency range: [100.000, 200.000] GHz
		>>> f1.lower
		100
		>>> f1.upper
		200

	# A velocity range
	>>> Range.unit = "km/s"
	>>> v1 = Range(100, 200)
	>>> print(v1)
	velocity range: [100.000, 200.000] km/s
	>>> v1.lower
	100
	>>> v1.upper
	200

	# A frequency range in GHz
	>>> Range.unit = "GHz"
	>>> f1 = Range(100, 200)
	>>> print(f1)
	frequency range: [100.000, 200.000] GHz
	>>> f1.lower
	100
	>>> f1.upper
	200
	"""
	unit  = "GHz"

	units = { "Hz": "frequency",
			 "kHz": "frequency",
			 "MHz": "frequency",
			 "GHz": "frequency",
		     "m/s": "velocity",
		    "km/s": "velocity",
		    "cm-1": "wavenumber",
		     "m-1": "wavenumber",
	  "micrometer": "wavelength",
		       "m": "wavelength"}


	C = 299792.458 # km/s

	def __init__(self, lower, upper):
		super(Range, self).__init__()

		if lower == "*" and upper == "*":
			lower = float("-inf")
			upper = float("inf")

		# Set the bounds and the monotony
		if lower < upper:
			self.monotony = 1
		else:
			self.monotony = -1

		self.lower = lower
		self.upper = upper
		self.amplitude = abs(upper - lower)

		self._magnitude = None
        
	def get_magnitude(self):
		magnitude = self.units[self.unit]
		return magnitude

	def set_magnitude(self, value):
		self._magnitude = value

	magnitude = property(get_magnitude, set_magnitude)

	def __repr__(self):
		s = "%s range: [%.3f, %.3f] %s" % (self.magnitude, self.lower, self.upper, self.unit)
		return s

	__str__ = __repr__

	def __getitem__(self, i):
		if i == 0:
			return self.lower
		elif i == 1:
			return self.upper
		else:
			raise IndexError("Index can only be 0 or 1.")

	def __len__(self):
		return 2

	def __eq__(self, x):
		if isinstance(x, Range):
			return x.lower == self.lower and x.upper == self.upper
		else:
			return False


if __name__ == '__main__':
	import doctest
	doctest.testmod()


# vim: set noet fdm=indent:
