# Imports from delivery/script
import ScriptEnvironment
from Utils import Utils


class PCygniPhysicalModel(object):
	"""A PCygniPhysicalModel is a unique combination of the physical parameters
	for a given component.

	It is used to model a molecular spectrum in LineAnalysis.

	Usage:
		physicalModel = PCygniPhysicalModel(parameters)

	* parameters: a dictionary of the form:
				  {parameter_name : parameter_value, ...}
				  representing a unique set of parameter values
				  amongst all the possible combinations with the
				  different components

	Example:
		physicalModel = PCygniPhysicalModel({"vlsr": 11.33, "nmol": 1e14, ...})

	"""
	def __init__(self, parameters):
		self.parameters = parameters
		self.continuum  = None
		self.names      = [name for name in Utils.ORDERED_PARAMETER_NAMES\
						   if name in self.parameters.keys()]

		# Set the parameters names as attributes
		for name, value in parameters.items():
			self.checkParameterName(name)
			setattr(self, name, value)

	# TODO Move checkParameterName to Utils
	def checkParameterName(self, name):
		"""Check the name of a parameter in the following categories:
			- mandatory

		using defined constants. (cf Utils)

		"""
		if name not in Utils.MANDATORY_PARAMETERS:
			msg = "%s is not a valid parameter name.\n" % name
			msg += "Choose between:\n"
			msg += "- mandatory parameters: %s\n" % ", ".join(Utils.MANDATORY_PARAMETERS)
			raise ValueError(msg)

	def __str__(self):
		attrs = []
		attrs.extend(self.names)
		if hasattr(self, "nmol"):
			strings = ["nmol = %.2g" % self.nmol]
		else:
			strings = []
		strings += ["%s = %.2f" % (x, getattr(self, x)) for x in attrs if x != 'nmol']

		return "PCygniPhysicalModel(%s)" % (", ".join(strings))

	__repr__ = __str__


# vim: set noet fdm=indent:
