IHMImpexParamClass.php 6.76 KB
<?php

/**
 * @class IHMImpexParamClass
 * @brief Class used to manage impex parameters
 * @details
 */
class IHMImpexParamClass extends IHMParamTemplateClass
{
	private $dataProducer = 'UNKNOWN';
	private $simulationRegion = 'UNKNOWN';
	private $dom;
	private $filePath;
	
	public static $paramPrefix = "spase___IMPEX_NumericalOutput_";

	/*
	 * @brief Constructor
	*/
	function __construct()
	{
		$this->filePath = IHMConfigClass::getRemoteParamsFile();		
		$this->dom = new DomDocument("1.0");
		
		if (!file_exists($this->filePath))
			throw new Exception("no RemoteParams File ".$this->filePath);
			
		$this->dom->load($this->filePath);  
	
	}
	
   public function param2dd($paramID) 
	{
			$pairs = array(" " => "_","-" => "_","/" => "_","%" => "_","\\" => "_","$" => "_",":" => "_","+" =>"_","#" => "_","@" => "_","." => "_", ">" => "_", "<" => "_", "," => "_", ")" => "", "(" => "_");    
			return strtr($paramID,$pairs); 	
	}
	
	public function getDataProducer($paramId)
	{			 
		$paramNode = $this->dom->getElementById($paramId);
		
		while ($paramNode->tagName !== 'dataCenter') 
		{		 
			$paramNode = $paramNode->parentNode;
		}
		
		if ( $paramNode )
			 $this->dataProducer = $paramNode->getAttribute('xml:id');
			 
		return $this->dataProducer;	  
	}
	
	public function getResourceID($paramId)
	{			 
		$paramNode = $this->dom->getElementById($paramId);
		
		while ($paramNode->tagName !== 'dataset') 
		{		 
			$paramNode = $paramNode->parentNode;
		}
		
		if ( $paramNode )
			 $resourceID = $paramNode->getAttribute('xml:id');
			 
		return $resourceID;	  
	}
	
	public function getTranslatedResourceID($paramId)
	{	
		return $this->param2dd($this->getResourceID($paramId));
	}
	
	public function getSimulationRegion($paramId)
	{	
		$paramNode = $this->dom->getElementById($paramId);
	
		while ($paramNode->tagName !== 'simulationRegion') 
		{		 
			$paramNode = $paramNode->parentNode;			
			if ( $paramNode->tagName == 'simulationModel') break;	
			
		}
	 
		if ( $paramNode && $paramNode->tagName == 'simulationRegion')
			 $this->simulationRegion = $paramNode->getAttribute('xml:id');
		 
		return $this->simulationRegion;	  
	}
	
	public function getParameterKey($paramId)
	{	
		$parameterKey = 'UNKNOWN';
		
		$paramNode = $this->dom->getElementById($paramId);
		
		if ( $paramNode )
			 $parameterKey = $paramNode->getAttribute('shortName');
			 
		return $parameterKey;	  
	}
	
	
	public function getParameterSize($paramId)
	{	
		$parameterSize = 'UNKNOWN';
		
		$paramNode = $this->dom->getElementById($paramId);
		
		if ( $paramNode )
			 $parameterSize = $paramNode->getAttribute('size');
			 
		return $parameterSize;	  
	}
	
	public function getAccessUrl($paramId)
	{	
		$url = 'UNKNOWN';
		
		$paramNode = $this->dom->getElementById($paramId);
		
		while ($paramNode->tagName !== 'dataset') 
		{		 
			$paramNode = $paramNode->parentNode;			
					
		}
		
		if ( $paramNode && $paramNode->hasAttribute('accessUrl'))
			 $url = $paramNode->getAttribute('accessUrl');
			 
		return $url;	  
	}
	
	public function setParamTemplateListFilePath($paramId)
	{
		$this->paramTemplateListFilePath = IHMConfigClass::$remoteDataDir.$this->getDataProducer($paramId).'_Templates.xml';	 
	}
	
	/*
	 * 
	 */
	public function getImpexFullParamId($param_id, $templateArgs = NULL) {
	
		if (!$templateArgs)
			return $param_id;
		
		$fullParamId = $param_id;
		foreach ($templateArgs as $key => $value)	
			$fullParamId .= "_".$value;
			
		return $fullParamId;
	}
	
	public function getImpexParamId($fullParamId)
	{
		$pos = strrpos($fullParamId,'_');
		$id = $fullParamId;
		
		$find = false;

		while ($pos && !$find)
		{
			$id = substr($id,0,$pos);
			$param = $this->dom->getElementById($id);
			if ($param) $find = true;						
			$pos = strrpos($id,'_');		
		}
		if (!$find)
			throw new Exception("Can not find IMPEX parameter ".$fullParamId);
			
		return $id;
	}
	
	public function parseImpexParam($param_expression)
	{
		$paramId = $this->getImpexParamId($param_expression);
	   $this->getDataProducer($paramId);
		$this->getSimulationRegion($paramId);
		$this->setParamTemplateListFilePath($paramId);
		
		$templateArgsString = substr($param_expression,strlen($paramId."_"));
		
		$allTemplatedParams = $this->getParamTemplates();
		$templatedParams = $allTemplatedParams[$this->simulationRegion];
		$args = $templatedParams['arguments'];
		
		$templateArgs = array();
		foreach ($args as $key => $value)
		{
			/*$r = print_r($templatedParams,true);		*/
			switch($value["type"]) 
			{
				case 'float' :
				case 'double' :
				case 'int':
					$pos = strpos($templateArgsString,'_');
					$arg_value = substr($templateArgsString,0,$pos);
					$templateArgsString = substr($templateArgsString,$pos+1);
				break;
				case 'list' :
					$arg_items = $value['items'];
					foreach ($arg_items as $arg_key => $arg_val)
					{
						if ( strncmp($arg_key, $templateArgsString, strlen($arg_key)) === 0)
						{
							$arg_value = $arg_key;
							$templateArgsString = substr($templateArgsString,strlen($arg_key)+1);
							break;
						}
					}
				break;
				default:	
			}
			 
			$templateArgs[$key] = $arg_value;			 	   
		}
		 
		$params = array(
					"paramid" => $paramId,
					"fullparamid" => $param_expression,
					"template_args" => $templateArgs
				);	
	
		return $params;
	}

	/*
	* List of Vector params in this data set
	*/
	public function getVectorList($resourceId)
	{
		$vectorList = array();
		
		$datasetNode = $this->dom->getElementById($resourceId);
		if ($datasetNode)
		{
			$params = $datasetNode->getElementsByTagName('parameter');
			foreach ($params as $param)
			{
				if ($param->hasAttribute('size'))
					if ($param->getAttribute('size') == '3')
							$vectorList[] = $param->getAttribute('shortName');				
		   }		
		}
		
		return $vectorList;
	}
	
	//IMPEXPLOTINIT - predefined plot output
	public function getParamInfoPlotPath($paramId)
	{
	
		if ($this->getDataProducer($paramId) != "IPIM" && !$this->isSpectra($paramId))		
			return null;
			
		$templateXml = IHMConfigClass::$remoteDataDir.$this->getDataProducer($paramId).'_PlotSettings.xml';
		
		if (!file_exists($templateXml))
			throw new Exception("No $templateXml");
			
		$dom = new DomDocument("1.0");
		if (!$dom->load($templateXml))
			throw new Exception("Cannot load  $templateXml");
			
		$paramNode = $dom->documentElement;
		$paramNode->setAttribute("xml:id", $paramId);
		
		$plotParamNode = $paramNode->getElementsByTagName('param');
		if ($plotParamNode->length == 0)
			throw new Exception("No Plot Param Node in  $templateXml");
			
		$plotParamNode->item(0)->setAttribute("id", $paramId); 
		
		$filePath = IHMConfigClass::getUserWSPath().$paramId."_plot.xml";
		
		$dom->save($filePath);
		
	  return  $filePath;	   
	  	
	}
	
	public function isSpectra($paramId)
	{
		$paramNode = $this->dom->getElementById($paramId);
		 
		return $paramNode->hasAttribute('isSpectra');
	
	}
}