THEMIS.php 4.34 KB
<?php
/**
 * @class THEMIS
 * @brief  
 */

class THEMIS extends RemoteDataCenterClientClass
{ 
   public $defaultFillValue = -1.e31;
   
// <param xml:id="imf">
//   <info>
//     <name>imf</name>
//     <short_name>b_gse</short_name>
//     <components>bx,by,bz</components>
//     <units>nT</units>
//     <coordinates_system>GSE</coordinates_system>
//     <tensor_order/>
//     <si_conversion>1e-9&gt;T</si_conversion>
//     <fill_value>-1.0E31</fill_value>
//     <ucd>phys.magField</ucd>
//     <dataset_id>ace-imf-all</dataset_id>
//     <instrument_id>ACE_MAG</instrument_id>
//   </info>
//   <get>
//      <vi name="ace:imf:all">
//         <baseParam name="IMF"/>
//     </vi>
//   </get>
//   <process/>
//   <output/>
// </param>          
   protected function makeInternalParamXml($param)
   {
		$xml = new DomDocument("1.0");
		$paramNode = $xml->createElement("param");
		$xml->appendChild($paramNode);
      
		$paramId = $param->getAttribute('xml:id');
		$paramNode->setAttribute("xml:id", $paramId);
		
		$ViId = substr($param->parentNode->getAttribute('xml:id'), strlen($this->baseID)+1);
		
		$infoNode = $xml->createElement("info");
		$infoNode->appendChild($xml->createElement("name",$paramId));
		$infoNode->appendChild($xml->createElement("short_name",$param->getAttribute('name')));
		
		$size = $param->getAttribute('size');
		
		//TODO spectra components 
		if ($size > 1 && $param->hasAttribute('labels')) 
		{	
			$components = $param->getAttribute('labels');
		}
		else {
			$components = null;
		}

		$infoNode->appendChild($xml->createElement("components",$components));
		$infoNode->appendChild($xml->createElement("units",$param->getAttribute('units')));
		$infoNode->appendChild($xml->createElement("coordinates_system"));
		$infoNode->appendChild($xml->createElement("tensor_order"));
		$infoNode->appendChild($xml->createElement("si_conversion"));
		$infoNode->appendChild($xml->createElement("fill_value", $this->fillValue));
		$infoNode->appendChild($xml->createElement("ucd"));
		$infoNode->appendChild($xml->createElement("dataset_id",strtolower($ViId)));
		$infoNode->appendChild($xml->createElement("instrument_id",strtolower($this->baseID." ".$ViId)));
		
		$getNode = $xml->createElement("get");
		$viNode = $xml->createElement("vi");
		$baseParamNode = $xml->createElement("baseParam");
		$arr = explode("_",$paramId); // tha_peif_density -> only last part
		
		$baseParamName = $arr[2];
		
		if ( count($arr) > 3 ) {
			for ($i = 3; $i < count($arr); $i++) 
					$baseParamName .= "_".$arr[$i];	
		}
		 
		$baseParamNode->setAttribute("name", $baseParamName);
		$viNode->setAttribute("name", strtolower(strtr($ViId,"_", ":")));
		$viNode->appendChild($baseParamNode);
		$getNode->appendChild($viNode);
		
		$paramNode->appendChild($infoNode);
		$paramNode->appendChild($getNode);
		$paramNode->appendChild($xml->createElement("process"));
		$paramNode->appendChild($xml->createElement("output"));
		
		$xmlNameRemote = RemoteData."/PARAMS/".$paramId.".xml"; 
		
		if (!is_dir(RemoteData."/PARAMS"))
				mkdir(RemoteData."/PARAMS", 0775, true);
		chmod(RemoteData."/PARAMS",  0775);
		
		return $xml->save($xmlNameRemote);
   }
   
	public function makeAllParams()
	{
		$params = $this->baseDom->getElementsByTagName('parameter');
		
		foreach ($params as $param) 
		{
			if (!$this->makeInternalParamXml($param))
				echo 'Error while making '.$param->getAttribute('xml:id').PHP_EOL;
		}
	}
   
	// make components description from base.xml
	public function makeCenterNode($xmlDom)
	{
		$nodeBase = $this->baseDom->getElementsByTagName('dataCenter')->item(0);
		$node = $xmlDom->importNode($nodeBase, true);
				
		$parameters = $node->getElementsByTagName('parameter');
		foreach ($parameters as $param) {
			if ($param->hasAttribute("labels")) 
			{
				$labels = $param->getAttribute('labels');
			   $id = $param->getAttribute('xml:id');
				$name = $param->getAttribute('name');

// 	      if (!$labels) {           		   			
// 		      $param->setAttribute('needsArgs', true);			   
// 		  return true;

				$labelArr = explode(",",$labels);                                     		
				for ($i = 0; $i < count($labelArr); $i++)
				{
					$component = $xmlDom->createElement('component');
					$component->setAttribute('xml:id',$id.'('.$i.')');

					$component->setAttribute('name',$labelArr[$i]);
					$param->appendChild($component); 
				}        		  
			}
		}
		return $node;
	}
}
?>