LESIA.php 10.3 KB
<?php
/**
 * @class LESIA 
 * @brief  
 * @details
 */
class LESIA extends SimulationDataCenterClass
{
	protected $url = "http://maser.obspm.fr/IMPExWS";
	protected $treeXML = "tree_Mag.xml";
	protected $WSDL = "Methods_LESIA-Mag.wsdl";
	
	protected $needsArgs = true;
	
	//TODO temporary
	protected $prefix = "spase://IMPEX/NumericalOutput/LESIA/MAG/Jupiter";
	protected $resourceID ="impex://LESIA/MAG/Jupiter";	
	/*
		 public $soapMethod = "calculateDataPointValue_Spacecraft";
		 <xs:element minOccurs="1" maxOccurs="1" ref="Spacecraft_name"/>
						<xs:enumeration value="Cassini"/>
						<xs:enumeration value="Galileo"/>
		 			<xs:element minOccurs="1" maxOccurs="1" ref="StartTime"/>
					<xs:element minOccurs="1" maxOccurs="1" ref="StopTime"/>
					<xs:element minOccurs="1" maxOccurs="1" ref="Sampling"/>	
			 				
	*/
 
   public $soapMethods = array("calculateDataPointValue", "calculateDataPointValue_Spacecraft");
//                <xs:element minOccurs="1" maxOccurs="1" ref="url_XYZ"/>
// 					<xs:element minOccurs="1" maxOccurs="1" ref="StartTime"/>

   public $additionalArgs = array("spase://IMPEX/NumericalOutput/LESIA/MAG/Jupiter/sc" => array("StartTime" => "dateTime", "StopTime" => "dateTime", "Sampling"=> "float" ), 
											 "spase://IMPEX/NumericalOutput/LESIA/MAG/Jupiter/xyz" => array("StartTime" => "dateTime")); 
   
   private $calculateDataPointValue_Args = null;  
   
   private $spacecraft = array("Cassini",	"Galileo");
   
   private $internalModel = array("O6", "VIP4", "VIT4", "VIPAL");
   private $currentSheet  = array("None", "Connerney", "Khurana");
   private $coordinateSystem = array("JSO", "JSM", "JSS", "PlanetaryCartesian","PlanetarySpherical","Planetarographic");
										
	/*				
	<xs:sequence>
					<xs:element minOccurs="0" maxOccurs="1" ref="IMF"/>
					
			 			*/
	//Extraparam:  OutputFormat => netCDF, VOTable	
	public $outputFormat = "VOTable";
	
	private function initArgs()
	{
	   if ($this->calculateDataPointValue_Args) return;
	   
		$this->calculateDataPointValue_Args = array();
		$this->calculateDataPointValue_Args["InternalModel"] = $this->internalModel;
		$this->calculateDataPointValue_Args["CurrentSheet"] = $this->currentSheet;
		$this->calculateDataPointValue_Args["CoordinateSystem"] = $this->coordinateSystem;
	
	}
	
	protected function createMissionNodes() 
	 {	 		
		$missionNodes =  array();
		
		foreach ($this->tree->SimulationModel as $sm)
		{			
			$ResourceID = trim($sm->ResourceID);
						
			$simulationModel = $this->domAmda->createElement('simulationModel');
			$simReg = $sm->SimulatedRegion;
			$simulationModel->setAttribute('xml:id', $sm->ResourceID); 
			
			$simulationModel->setAttribute('name',   $sm->ResourceHeader->ResourceName." : ".$simReg);
		
		   $simulationModel->setAttribute('desc',   $sm->ResourceHeader->Description.'<br/> ReleaseDate: '.$sm->ResourceHeader->ReleaseDate);
		   
		   if (file_exists(DATAPATH.'rank.json')) 
				$cmpArr = json_decode(file_get_contents(DATAPATH.'rank.json'), true);
			else 
				$cmpArr = null;
 	
			$index = $cmpArr["$simReg"];
			if ($index)  
				$simulationModel->setAttribute('rank',$index);
			else 
				$simulationModel->setAttribute('rank', 999);					 					
		  
		   $simulationModel->setAttribute('att', "simu/simu$simReg");
//			$this->setModelHelp($simulationModel,"");

			$this->addSimulatedRegionToTargetsFile($simReg, "JSM"); //TODO check this
 
			$datasetNodes = $this->createDatasetNodes($simReg);
						 
			foreach ($datasetNodes as $ds)
			{
				$parameterNodes = $this->createParameterNodes($sm->OutputParameters->Parameter, $ds->getAttribute('xml:id'));
				
				foreach ($parameterNodes as $node )
						$ds->appendChild($node);
			
				$simulationModel->appendChild($ds);			
		   }    
 			$missionNodes[] = $simulationModel;
	   }
	 	   
	   return $missionNodes;
	 }
	 
	protected function createDatasetNodes()	 
	{
		$datasetNodes = array();
		
		foreach ($this->soapMethods as $soapMethod)
		{
			$datasetNode = $this->domAmda->createElement('dataset');	
			$datasetNode->setAttribute('name', $soapMethod);
			if ($soapMethod == "calculateDataPointValue_Spacecraft")
				$datasetNode->setAttribute('xml:id', $this->prefix."/sc");
			else 
				$datasetNode->setAttribute('xml:id', $this->prefix."/xyz");
			
			$datasetNode->setAttribute('globalStart', 'depending on mission');
			$datasetNodes[] = $datasetNode;
		}
		return $datasetNodes;
	}
	
	 protected function createParameterNodes($params, $dsid)
	{
		$paramNodes = array();		
			
		foreach ($params as $param)
		{
			$paramNode = $this->domAmda->createElement('parameter');			 
			$id = $this->param2dd($dsid."/".$param->ParameterKey);		
			$paramNode->setAttribute('xml:id',$id);	
			$paramNode->setAttribute('name', $param->ParameterKey);
			$paramNode->setAttribute('desc', 'Units: '.$param->Units);
			$paramNode->setAttribute('shortName',$param->ParameterKey); 
			$paramNode->setAttribute('needsArgs', $this->needsArgs);
			
			if ($this->hasComponents($param))
			{
				if (strpos(trim($param->ParameterKey), ',') === false)
					$comps = explode(' ',trim($param->ParameterKey)); 
				else
					$comps = explode(',',trim($param->ParameterKey));
					
				$paramNode->setAttribute('size', count($comps));
				
				foreach ($comps as $comp)
				{
					$compNode = $this->domAmda->createElement('component');
				//	$compNode->setAttribute('xml:id', $id."_".$comp);

					$compNode->setAttribute('xml:id',$this->param2dd($dsid."_".$comp));
						
					$compNode->setAttribute('name', $comp);
					$compNode->setAttribute('needsArgs', $this->needsArgs);	
					$compNode->setAttribute('shortName',$comp); 
					
					$paramNode->appendChild($compNode);
				}
			}
			$paramNodes[] = $paramNode;
		}
		
		return $paramNodes;
	}
	
	protected function createSpecialArgs($dom)
	{
		$this->initArgs();
		$nodes = array();
		
		foreach ($this->calculateDataPointValue_Args as $key => $values) 
		{
			$node = $dom->createElement('argument');
			$node->setAttribute('key', $key);
			$node->setAttribute('name', $key);
			$node->setAttribute('type', 'list');
			foreach ($values as $value)
			{
				$item = $dom->createElement('item');
				$item->setAttribute('name',$value);
				$item->setAttribute('key', $value);
				
				$node->appendChild($item);
			}
			$node->setAttribute('default', $values[0]);
			
			$nodes[] = $node;
		}
		
		$node =  $dom->createElement('argument');
		
		return $nodes;
	}
	
	protected function createSpecialScArgs($dom)
	{
			$node = $dom->createElement('argument');
			$node->setAttribute('key', 'Spacecraft_name');
			$node->setAttribute('name', $key);
			$node->setAttribute('type', 'list');
			foreach ($this->spacecraft as $value)
			{
				$item = $dom->createElement('item');
				$item->setAttribute('name',$value);
				$item->setAttribute('key', $value);
				
				$node->appendChild($item);
			}
			$node->setAttribute('default', $this->spacecraft[0]);
			
		return $node;
	}
	
	public function makeArgumentsList()
	{		
		if (!file_exists(SimuTargetsXml))
			exit('NOT YET!'.PHP_EOL);
			
		if (file_exists($this->templateFile))
			unlink($this->templateFile);	
		
		$TemplatesXml = new DomDocument("1.0");
		$TemplatesXml->formatOutput = TRUE;
		$TemplatesXml->preserveWhiteSpace = FALSE;

		$rootNode = $TemplatesXml->createElement('paramTemplateList');
		$TemplatesXml->appendChild($rootNode);
				
		$TargetsXml = new DomDocument("1.0");
		$TargetsXml->load(SimuTargetsXml);
		$targets = $TargetsXml->getElementsByTagName('Target'); 
		foreach ($targets as $target)
		{		   
			if ($target->hasAttribute($this->baseID))
			{
				echo $target->getAttribute('xml:id').PHP_EOL;
				foreach ($this->soapMethods as $soapMethod)
				{
					$paramNode = $TemplatesXml->createElement('paramTemplate');
					 
					$paramNode->setAttribute('fileName','##internalField####currentSheet##');
					$argsNode = $TemplatesXml->createElement('arguments');
					$paramNode->appendChild($argsNode);
					$specialNodes = $this->createSpecialArgs($TemplatesXml);
					
					foreach ($specialNodes as $specialNode)
						$argsNode->appendChild($specialNode);	
						
					if ($soapMethod == "calculateDataPointValue")
					{
						$missionGrps = json_decode(file_get_contents(RemoteData.$target->nodeValue.".json"), true);
						$node = $TemplatesXml->createElement('argument');
						$node->setAttribute('key', 'url_XYZ');
						$node->setAttribute('name', 'Mission Name');
						$node->setAttribute('type', 'list');				
						
						foreach ($missionGrps as $grp => $missions)
						{
							foreach ( $missions as $mission => $datasets)
							{	
								echo 'MISSION: '.$mission.PHP_EOL;
								foreach ($datasets as $dataset => $parameters)
								{
									echo "       ".$dataset.PHP_EOL;
									foreach ($parameters as $param => $value) 							
									{
										$item = $TemplatesXml->createElement('item');
										$item->setAttribute('name', $mission);
										$item->setAttribute('key', $value);
										
										$node->appendChild($item);
									}
								}
							}					
						}
						//TODO select default s/c for each target 
						$node->setAttribute('default', $value);
						$argsNode->appendChild($node);
						$paramNode->setAttribute('paramId', $this->prefix."/xyz");
					}
					else // calculateDataPointValue_Spacecraft
					{
					  $scNode = $this->createSpecialScArgs($TemplatesXml);
					  $argsNode->appendChild($scNode);
					  $paramNode->setAttribute('paramId', $this->prefix."/sc");
					}
					
					$rootNode->appendChild($paramNode); 
				}
			}
		}
		$TemplatesXml->save(RemoteData.$this->baseID."/".$this->templateFile);
	} 
	
	public function getData($params)
	{
//   	$r = print_r($params,true);
//   	error_log($r,3,'/home/budnik/LOG');
  	
  	   if (strpos('xyz', $params['ResourceID']) > 0)
			$realSoapMethod = $this->soapMethods[0]; 
		else
			$realSoapMethod = $this->soapMethods[1];
			
		$params['ResourceID'] = $this->resourceID;
		
		try 
		{
			$client = new SoapClient($this->url."/".$this->WSDL,
			array(
				'wsdl_cache' => 0,
				'trace' => 1,
				'soap_version'=>SOAP_1_2
			));
		}
		catch  (SoapFault $exception) 
		{
			throw new Exception($exception->getMessage()." ".$exception->getTraceAsString());
		}
		
		$realSoapMethod = $this->soapMethods[0]; 
		
		try 
		{
			$file = $client->__soapCall($realSoapMethod, array($params)); 
		}
		catch  (SoapFault $exception) 
		{
			throw new Exception($exception->getMessage()." ".$exception->getTraceAsString()); 
		} 
		
		return $file;

	}		
}
?>