makeOrbitsList.php 2.89 KB
<?php
/*
*  Executable to make Orbits.xml with the description for all Ephemeris parameters 
*  Uses LocalParams.xml and AMDA params descriptions
*/

		$AMDA_IHM = getenv('AMDA_IHM');
      error_reporting(E_ERROR);
      
		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);
            
		$pairs = array("-" => "_" );  	
	
		$amdaXml = new DomDocument("1.0");
		$amdaXml->load(LocalData."/LocalParams.xml");
		
		$xpath = new DOMXpath($amdaXml);
		
		$amdaParamDir = PARAMS_LOCALDB_DIR;

		$orbitsXml =  new DomDocument("1.0");
		$orbitsXml->formatOutput = TRUE;
		$orbitsXml->preserveWhiteSpace = FALSE;
		
		$orbitsRoot = $orbitsXml->createElement('orbitesList');
		$orbitsXml->appendChild($orbitsRoot);
		
		$nodesAmda = $xpath->query('//*[@target]');
		
		foreach ($nodesAmda as $node)
		{
			$target = $node->getAttribute('target');
			
			if ( (($node->tagName == 'mission' && $node->getElementsByTagName('observatory')->length == 0) ||
  				 ($node->tagName == 'observatory') ) && $node->getAttribute('name') != $target )
			{
				$instruments =  $node->getElementsByTagName('instrument');
				foreach ($instruments as $ins)
				{
					if (substr($ins->getAttribute('name'),0,9) != 'Ephemeris') continue; // Ephemeris: cruise
					$parameters = $ins->getElementsByTagName('parameter');
					foreach ($parameters as $parameter)
					{
						if ($parameter->hasAttribute('size') && $parameter->getAttribute('size') == '3')
						{
							$param_file = $amdaParamDir."/".$parameter->getAttribute('xml:id').".xml";
							if (file_exists($param_file))
							{
								$paramXml = simplexml_load_file($param_file);

								$orbitNode = $orbitsXml->createElement('orbites');
								$dsId = strtr($paramXml->info->dataset_id,$pairs);
								$orbitNode->setAttribute('xml:id',$parameter->getAttribute('xml:id'));
								$orbitNode->setAttribute('coordinate_system', $paramXml->info->coordinates_system);
								$orbitNode->setAttribute('units', $paramXml->info->units);
								
								$name = strtolower($node->getAttribute('name'));
								$name = preg_replace('/\s+/', '', $name);
								$name = str_replace('-', '', $name);
								
								$orbitNode->setAttribute('mission', $name);
								
								if ( $node->tagName == 'observatory' )
									$orbitNode->setAttribute('missionGroup',$node->parentNode->getAttribute('name'));
									
								$orbitNode->setAttribute('dataset', $dsId);
								$ds = $amdaXml->getElementById($paramXml->info->dataset_id);
								if ($ds->hasAttribute('target'))
									$orbitNode->setAttribute('target', $ds->getAttribute('target'));
								else
									$orbitNode->setAttribute('target', $target);
								
								$orbitsRoot->appendChild($orbitNode);
							}
						}
					}
				}
			}
		}
		
		$orbitsXml->save(orbitsXml);
?>