makeOrbitsInKm.php 4.79 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');
						
		$tr = array(':' => '_');
		
		$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 = '$'.strtr($paramXml->get->vi['name'], $tr).'_'.$paramXml->get->vi->baseParam['name'].'*'.$scale[$units];						
					}
					else if (strpos($process, '/RADIUS') > 0)
					{   
						$newProcess = substr($process,0,strpos($process, '/RADIUS'));					  
						if ($newProcess == '$'.strtr($paramXml->get->vi['name'], $tr)) $newProcess = '';
					}
					else
					{
						$newProcess = $process."/".$scale[$units];
					}
					
					$newId = $id."_km";
					$new_param_file =  $amdaParamDir."/".$newId.".xml";
					$newDom = new DomDocument("1.0");
					$newXML = $newDom->createElement("param");
					$newDom->appendChild($newXML);
					$newXML->setAttribute("xml:id", $newId);
					$newInfo = $newDom->createElement('info');
					$newInfo->appendChild($newDom->createElement('name', $paramXml->info->name));
					$newInfo->appendChild($newDom->createElement('short_name', $paramXml->info->name));
					$newInfo->appendChild($newDom->createElement('components', $paramXml->info->components));
					$newInfo->appendChild($newDom->createElement('units', 'km'));
					$newInfo->appendChild($newDom->createElement('coordinates_system', $paramXml->info->coordinates_system));
					$newInfo->appendChild($newDom->createElement('tensor_order', $paramXml->info->tensor_order));
					$newInfo->appendChild($newDom->createElement('si_conversion', 1.e3));
					//TODO scale fill value
					$newInfo->appendChild($newDom->createElement('fill_value', $paramXml->info->fill_value));
					$newInfo->appendChild($newDom->createElement('ucd', $paramXml->info->ucd));
					$newInfo->appendChild($newDom->createElement('dataset_id', $paramXml->info->dataset_id));
					$newXML->appendChild($newInfo);
					$newGet = $newDom->createElement('get');
					$newXML->appendChild($newGet);
					$newVi = $newDom->createElement('vi');
					$newVi->setAttribute('name', $paramXml->get->vi['name']);
					$newGet->appendChild($newVi);
					$newParam = $newDom->createElement('baseParam');
					$newParam->setAttribute('name', $paramXml->get->vi->baseParam['name']);
					$newVi->appendChild($newParam);
					$newXML->appendChild($newDom->createElement('process',$newProcess));
					$newXML->appendChild($newDom->createElement('output'));
					$newDom->save($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');
?>