BestRunsMgr.php 7.03 KB
<?php
/**
 * @class BestRunsMgr
 * @version $Id: BestRunsMgr.php $
 * 
 */

class BestRunsMgr 
{ 
	protected $runs;
	protected $domRemoteData, $domTemplate;
	protected $xpRemoteData, $xpTemplate;
	protected $runsTemplate;
	protected $json;
	protected $factors = array("SW_Density"=>1e6,
			"SW_Utot"=>1e3,
	//		"SW_Temperature"=>1e3,
			"SW_Temperature"=>1.16e4, // ev => K
			"SW_Btot"=>1e-9,
			"SW_Bx"=>1e-9,
			"SW_By"=>1e-9,
			"SW_Bz"=>1e-9
	);
 
	function __construct() {}


	public function init() 
	{  
		// DOM of SIMU RUNs template
		$this->domTemplate = new DomDocument("1.0");
		if (!$this->domTemplate->load(DATAPATH.'SimuTemplate/templateGUMICS.xml')) 
				return false;
				
		$this->xpTemplate = new domxpath($this->domTemplate); 
		$this->runTemplate = $this->xpTemplate->query("//runID")->item(0);
		
		// DOM of user RemoteParams
		$this->domRemoteData = new DomDocument("1.0");				 
		
		if (!$this->domRemoteData->load(USERWSDIR.'RemoteParams.xml'))							 			
			return false;
	
		$this->xpRemoteData = new domxpath($this->domRemoteData); 
		
		$this->json = file_get_contents(USERWSDIR.'runs.json');
		
		return true;
	}

	// $obj ={"Object":"Earth","RunCount":"2",
	// "SW_Density_value":"10","SW_Density_weight":"1","SW_Density_scale":"7","SW_Temperature_value":"100", ....}
	public function getRun($obj)
	{
		$params = $this->makeParams($obj);
		try 
		{ 
			$client = new SoapClient("http://impex-fp7.fmi.fi/ws/Methods_FMI.wsdl",
					array(
					'wsdl_cache' => 0,
					'trace' => 1,
					'exceptions' => 0,
					'soap_version'=>SOAP_1_2
					));
		}
		catch  (SoapFault $exception) 
		{				
			$res =  array('success' => false,'error' => $exception->getMessage());
		}		
		
		try 
		{
			$data_json = $client->getMostRelevantRun($params);
			$result = json_decode($data_json,true);

			if (count($result["runs"]) > 0) 
			{
				$result = $this->changeJson($result);
				$res =  array('success' => true,'runs' => $result["runs"]);
				file_put_contents(USERWSDIR.'runs.json',json_encode($res));		    
			}
		}
		catch (Exception $e) 
		{
			//error_log ($e->getMessage(),1,email);
			$res =  array('success' => false,'error' => $e->getMessage());
		}	      
		return  $res; 
	}
             
	public function addRuns($obj)
	{      
		if (!$obj) return false;
		else 
		{
			$this->runsId = (array)$obj;
			$object = json_decode($this->json,true);

			$simuRegion =  $this->domRemoteData->getElementById('FMI_GUMICS_Earth_Magnetosphere');
	  
			foreach ($this->runsId as $runId)
			{
				if ($this->domRemoteData->getElementById($runId) == null)
				{				
					$newRun = $this->domRemoteData->importNode($this->runTemplate, true);
					foreach ($object['runs'] as $r)
					{
						if ($r['ResourceID'] == $runId) $run=$r;
					}
					$newRun->setAttribute('xml:id', $run['ResourceID']);
					$newRun->setAttribute('name', $run['ResourceName']);
					$desc = $this->runTemplate->getAttribute('start_desc'). "<b>Param_values: </b><br/>";
					foreach ($run['Param_values'] as $key => $value) 
					{
						$desc = $desc."<u>".$key." </u>".$value."<br/>";
					}
					$newRun->setAttribute('desc', $desc);
					$newRun->removeAttribute('start_desc'); 
					$datasets = $newRun->getElementsByTagName('dataset');
					
					$numericalOutputs = $run['NumericalOutput']; // array !					
					
					foreach ($datasets as $dataset)
					{
						$name = $dataset->getAttribute('name');
						$find = false;
						
						foreach ($numericalOutputs as $no)
						{						
							if (strpos($no, $name) > 0)
							{ 
							
								$datasetId = $no;
								$find = true;
								break;
							}
						}
						
						if (!$find)							
							throw new Exception($name." is not found in FMI_GUMICS response");
							
					//	$datasetId = str_replace('SimulationRun','NumericalOutput',$run['ResourceID'].'/'.$name);
						$dataset->setAttribute('xml:id', $datasetId); 
						$dataset->setAttribute('desc'); 
						$dataset->setAttribute('globalStart', 'depending on mission');
						$params = $dataset->getElementsByTagName('parameter');
						foreach($params as $param)
						{
							$paramName = $param->getAttribute('name');
				// 		    $datasetId = str_replace('SimulationRun','NumericalOutput',$datasetId);
							switch ($paramName) 
							{
								case "H+ number density" : $paramName = "Density"; break;
								case "H+ velocity" : $paramName = "Ux,Uy,Uz"; break;
								case "H+ total velocity" : $paramName = "Utot"; break;
								case "H+ thermal pressure" : $paramName = "Pressure"; break;
								case "H+ temperature" : $paramName = "Temperature"; break;
							}
	    
							$paramId = $this->param2ddSimu($datasetId.'/'.$paramName);
							$param->setAttribute('xml:id', str_replace('_EARTH___n_T_Vx_Bx_By_Bz','',str_replace('earth_synth_stationary_','',$paramId))); 
						//	$param->setAttribute('parentID',$datasetId); 
							$param->setAttribute('needsArgs','1');
							if ($param->hasChildNodes()) 
							{
								$components = $param->getElementsByTagName('component');
								
								foreach ($components as $component)
								{
									$compName = $component->getAttribute('name');
									$component->setAttribute('xml:id', str_replace('_EARTH___n_T_Vx_Bx_By_Bz','',str_replace('earth_synth_stationary_','',$paramId.'_'.$compName))); 
								//	$component->setAttribute('parentID',$datasetId); 
									$component->setAttribute('needsArgs','1');
								}
							}
						}
					}
					$simuRegion->appendChild($newRun);
					$this->domRemoteData->save(USERWSDIR.'RemoteParams.xml');
				}
			}  
		}
	}
      
	protected function makeParams($json)
	{
		$obj = (array)$json;
		$SW_parameters = array();
		
		foreach ($obj as $k => $v) 
		{
			if (empty($v))
				unset($obj[$k]);
		}
		
		foreach ($obj as $k => $v) 
		{
			if (strpos($k,'_value') !== FALSE ) 
			{
				$sw= str_replace ('_value', '', $k);
				$SW_val = array();
				$SW_val['value'] = $obj[$sw.'_value'] * $this->factors[$sw];
				if ($obj[$sw.'_weight'] != '') $SW_val['weight'] = $obj[$sw.'_weight'];
				if ($obj[$sw.'_scale'] != '') $SW_val['scale'] = $obj[$sw.'_scale']* $this->factors[$sw];
				$SW_parameters[$sw] = $SW_val;
			}
		}
		$params = array (
			'Object'    => 'Earth',	// Mandatory
			'RunCount'  => $obj['RunCount'],	// Number of runs returned. Optional, default = 1
			'SW_parameters' => $SW_parameters);
		
		return $params;
	}
        
	protected function changeJson($object)
	{         
		$i=0;
		foreach ($object['runs'] as $run)
		{
			$run['S_diff'] = round($run['S_diff'],1);
			
			foreach ($run['S_diff_n'] as $k => $v)
			{
				$v = round($v,3);
				$run['S_diff_n'][$k] = $v;
			}
			
			foreach ($run['Param_values'] as $k => $v)
			{
				if ($this->factors[$k])
				{
					$newValue = round($v / $this->factors[$k], 3);
					$run['Param_values'][$k] = $newValue;
				}
			}
			$object['runs'][$i] = $run;
			$i++;
		}		
		return $object;
	}
      
	protected function param2ddSimu($paramID) 
	{
		$pairs = array("?" => "_", " " => "_","-" => "_","/" => "_","%" => "_","\\" => "_","$" => "_",":" => "_","+" =>"_",
							"#" => "_","@" => "_","." => "_", ">" => "_", "<" => "_", "," => "_");    
		return strtr($paramID,$pairs); 
	}
	
}
?>