makeOrbitsInKm.php 4.15 KB
<?php
/*
*  Executable to make dynamically internal ephemeris params in km  
*   
*/
		$scale = array('AU'=>'ASTRONOMICAL_UNIT','Rme'=>'RADIUS_MERCURY','Rv'=>'RADIUS_VENUS','Re'=>'RADIUS_EARTH','Rma'=>'RADIUS_MARS','Rm'=>'RADIUS_MARS',
						'Rj'=>'RADIUS_JUPITER','Rs'=>'RADIUS_SATURN','Ru'=>'RADIUS_URANUS','Rn' =>'RADIUS_NEPTUNE',
						'Rga'=>'RADIUS_GANYMEDE','Rg'=>'RADIUS_GANYMEDE');
 
		$AMDA_IHM = getenv('AMDA_IHM');
      
		if (!isset($AMDA_IHM)) 
			require_once __DIR__."/config.php";      
		else 
			require_once $AMDA_IHM."/php/config.php"; 
      
		if (!defined('orbitsXml'))
			exit("orbitsXml is not defined in config.php".PHP_EOL);
			
		if (!file_exists(orbitsXml))
			exit("No orbitsXml file".PHP_EOL);
			
		$amdaParamDir = DATAPATH."newKernelDDBase";
		
		$dom = new DomDocument("1.0");
		$dom->load(orbitsXml);
		
		$orbits = $dom->getElementsByTagName("orbites");
		
		$domKm = new DomDocument("1.0");
		$domKm->formatOutput = TRUE;
		$domKm->preserveWhiteSpace = FALSE;
		
		$rootKm = $domKm->createElement('orbitesList');
		$domKm->appendChild($rootKm);
		
   	Header('Content-type: text/xml');
		
		foreach ($orbits as $orbit)
		{
			$id = $orbit->getAttribute('xml:id');
			$units = $orbit->getAttribute('units');
			
			if ($units != 'km') // add orbital parameter in km
			{
				$param_file = $amdaParamDir."/".$id.".xml";
				if (file_exists($param_file))
				{
					$paramXml = simplexml_load_file($param_file);
					$process = $paramXml->process;					 
					
					if ($process == "")
					{						
						$newProcess = '$'.$paramXml->get->vi['name'].'*'.$scale[$units];						
					}
					else if (strpos($process, '/RADIUS') > 0)
					{   
						$newProcess = substr($process,0,strpos($process, '/RADIUS'));					  
						if ($newProcess == '$'.$paramXml->get->vi['name']) $newProcess = '';
					}
					else
					{
						$newProcess = $process."/".$scale[$units];
					}
					
					$newId = $id."_km";
					$new_param_file =  $amdaParamDir."/".$newId.".xml";
					$newXML = new SimpleXMLElement("<param></param>");
					$newXML->addAttribute('xml:id', $newId);
					$newInfo = $newXML->addChild('info');
					$newInfo->addChild('name', $paramXml->info->name);
					$newInfo->addChild('short_name', $paramXml->info->name);
					$newInfo->addChild('components', $paramXml->info->components);
					$newInfo->addChild('units', 'km');
					$newInfo->addChild('coordinates_system', $paramXml->info->coordinates_system);
					$newInfo->addChild('tensor_order', $paramXml->info->tensor_order);
					$newInfo->addChild('si_conversion', 1.e3);
					//TODO scale fill value
					$newInfo->addChild('fill_value', $paramXml->info->fill_value);
					$newInfo->addChild('ucd', $paramXml->info->ucd);
					$newInfo->addChild('dataset_id', $paramXml->info->dataset_id);
					$newGet = $newXML->addChild('get');
					$newVi = $newXML->addChild('vi');
					$newVi->addAttribute('name', $paramXml->get->vi['name']);
					$newParam = $newVi->addChild('baseParam');
					$newParam->addAttribute('name', $paramXml->get->vi->baseParam['name']);
					$newXML->addChild('process',$newProcess);
					$newXML->addChild('output');
					$newXML->asXML($new_param_file); 	
		
					$newOrbit = $domKm->createElement('orbites');
					$newOrbit->setAttribute('xml:id', $newId);
					$newOrbit->setAttribute('coordinate_system', $paramXml->info->coordinates_system);
					$newOrbit->setAttribute('units','km');
					$newOrbit->setAttribute('mission', $orbit->getAttribute('mission'));
					if ($orbit->hasAttribute('missionGroup')) 
						$newOrbit->setAttribute('missionGroup', $orbit->getAttribute('missionGroup'));
					$newOrbit->setAttribute('dataset', $orbit->getAttribute('dataset'));	
					$newOrbit->setAttribute('target', $orbit->getAttribute('target'));
					$rootKm->appendChild($newOrbit);
				}	
			}
		}
		
		$domKm->save(LocalData.'OrbitsInternal.xml');
		
		if (file_exists(LocalData.'OrbitsAll.xml'))
			unlink(LocalData.'OrbitsAll.xml');
			
	// Merge internal orbits and open orbits	
		$orbitsInternal = $rootKm->getElementsByTagName('orbites');
		foreach ($orbitsInternal as $orbitInternal)
		{
			$dom->documentElement->appendChild($dom->importNode($orbitInternal));
		}
		
		$dom->save(LocalData.'OrbitsAll.xml');
?>