makeMultiMission.php 7.9 KB
<?php
/**  @file makeMultiMission  
*    @version $Id: makeMultiMission.php 2321 2014-04-26 12:36:18Z elena $
*    @brief  StandAlone executable launched in BASE_PATH/php to create LOCALLY 
*    @brief   DD_MISSION.xml and MISSION.xml from templates
*    @brief   and LocalParamsList.xml and LocalParams.xml
*    @brief   it is analog of AmdaUpdate.php INSTALL
*/
                 require_once "config.php";

                 $missionsDom = new DomDocument("1.0");                   
                 $missionsDom->load(missionXml);
                 $locBases = $missionsDom->getElementsByTagName("MissionID");
 
                 foreach ($locBases as $locbase) {                                               		                                   
                     $multi =  $locbase->getAttribute("missions");
                     $nameArr = explode(";",$multi);
  
		     $templatename = LocalDataTemplate."DD_".$locbase->nodeValue.".template"; 
		     $paramtemplate = LocalDataTemplate.$locbase->nodeValue.".template";
		     if (count($nameArr) > 1 &&  
                         (file_exists($templatename) && file_exists($paramtemplate))) {
  
			$suffix = array();
			foreach ($nameArr as $item) {
			  $temp = explode("-",$item);
			  if (count($temp) > 1) {
			      $suffix[] = $temp[1];
			  }
			  else {
			      $suffix[] = substr($item,-1);
			  }
			}
                   			 
                        $template = new DomDocument("1.0");
                        $status = $template->load($templatename);
 			
			$paramdom = new DomDocument("1.0");
			$status = $paramdom->load($paramtemplate);  
 
 			 			
// Create DD_Mission.xml
			$locbasename =  LocalData."DD_".$locbase->nodeValue.".xml";
			$locbasedom = new DomDocument("1.0");
			$root = $locbasedom->importNode($template->documentElement);	
			$amda = $locbasedom->importNode($template->getElementsByTagName("dataCenter")->item(0));
			$missionMain = $locbasedom->importNode($template->getElementsByTagName("mission")->item(0)); 

                        $mission = $locbasedom->importNode($template->getElementsByTagName("mission")->item(0), true);
                      
                        $targets = $mission->getElementsByTagName("target"); 
                        $targetNames = array();
                        foreach ($targets as $target)
                               if ($target->parentNode->tagName == 'mission')
                                            $targetNames[] = $target->nodeValue;

                        $observatory = $mission->getElementsByTagName("observatory")->item(0);
                        $instruments = $observatory->getElementsByTagName("instrument");
// create param descriptor  Mission.xml
			$parambasename =  LocalDataParam.$locbase->nodeValue.".xml";
			$parambasedom = new DomDocument("1.0");
			$paramroot = $parambasedom->importNode($paramdom->documentElement);	
   			
			$instrumentNon = array();
			foreach ($instruments as $instrument) {

				if ($instrument->hasAttribute("absent")) {
				    $absent = $instrument->getAttribute("absent");
				    $absentArray = explode(";",$absent);
 			            $tempArr = array();

			 	    if (count($absentArray) > 1) {
				     	for ($i = 0; $i < count($suffix); $i++) $tempArr[$i] = false;			  				   
					for ($j = 0; $j < count($absentArray); $j++) {
					  $temp = array_search($absentArray[$j], $suffix, true); 
					  $tempArr[$temp] = true;
				       }
				        
			 	    }	
				     else { 
                                          for ($i = 0; $i < count($suffix); $i++) {
					      $tempArr[$i] = $absent == $suffix[$i];
					}				  				                            
				    }

			    $instrumentNon[$instrument->getAttribute("name")] = $tempArr;
			   }
			}

                        foreach ($targetNames as $targetName) {
                                $missionMain->appendChild($locbasedom->createElement("target",$targetName));
                        }
                                    
			for ($index = 0; $index < count($suffix); $index++) {

			    $missionArr[$index] = $observatory->cloneNode();			   
                 
                            $missionArr[$index]->setAttribute("name", str_replace("XXX", $suffix[$index], $missionArr[$index]->getAttribute("name")));
                            $missionArr[$index]->setAttribute("xml:id", str_replace("XXX", $suffix[$index], $missionArr[$index]->getAttribute("name")));
			    $instruments = $observatory->getElementsByTagName("instrument");

			    foreach ($instruments as $instrumentReal){			  
				if (!$instrumentNon[$instrumentReal->getAttribute("name")] || !$instrumentNon[$instrumentReal->getAttribute("name")][$index])  { 
				$instrument = $instrumentReal->cloneNode(true);
				$instrument->setAttribute("desc",str_replace("XXX", $suffix[$index], $instrument->getAttribute("desc")));
				$instrument->setAttribute("xml:id",str_replace("XXX", $suffix[$index], $instrument->getAttribute("xml:id")));
                                $tags = array("dataset", "parameter", "component"); 

				foreach ($tags as $tag) {
				    $all = $instrument->getElementsByTagName($tag);
				    foreach ($all as $one) {
					  if ($tag == 'parameter') {
					      $id = $one->getAttribute("xml:id");
 // create param tag in param XML descriptor
					      $param = $paramdom->getElementById($id);
					      if ($param){
                                                    $paramAdd = $parambasedom->importNode($param, true);
						    $paramAdd->setAttribute("xml:id",str_replace("XXX", strtolower($suffix[$index]), $id)); 
						    $paramAdd->setAttribute("vi",str_replace("XXX", strtolower($suffix[$index]), $paramAdd->getAttribute("vi")));
						    $yTitle = $paramAdd->getElementsByTagName("YTITLE");
						    if ($yTitle->length > 0) {
						      $value =  $yTitle->item(0)->nodeValue;
						      $yTitle->item(0)->nodeValue = str_replace("XXX", $suffix[$index], $value);
						    }
						    $paramroot->appendChild($paramAdd);
					      }
					   }
					  $one->setAttribute("xml:id",str_replace("XXX", strtolower($suffix[$index]), $one->getAttribute("xml:id")));
					  if ($tag == 'dataset' && $one->hasAttribute("vi")) 
						$one->setAttribute("vi",str_replace("XXX", strtolower($suffix[$index]), $one->getAttribute("vi")));					  
				    }
				  }
				$missionArr[$index]->appendChild($instrument);
				}			     
			   } 
			     
			    $missionMain->appendChild($missionArr[$index]);			    
			  }

                        $amda->appendChild($missionMain);
			$root->appendChild($amda);
			$locbasedom->appendChild($root);
		        $locbasedom->save($locbasename);
			
			$parambasedom->appendChild($paramroot);
			$parambasedom->save($parambasename);		 
		     }  		    
       	 	  }

// temporary here
                 $paramsDom = new DomDocument("1.0");
       	 	 $paramsDom->formatOutput = true;
       	 	 $paramsDom->preserveWhiteSpace = false;
       	 	 $paramsRoot = $paramsDom->createElement('LOCAL');
       	 	 $paramsDom->appendChild($paramsRoot);
       	 	 
       	 	 $listDom = new DomDocument("1.0");
       	 	 $listDom->formatOutput = true;
       	 	 $listDom->preserveWhiteSpace = false;
       	 	 $listRoot = $listDom->createElement('LOCAL');
       	 	 $listDom->appendChild($listRoot);
		  		   
                 $dom = new DomDocument("1.0");
     	 	 foreach ($locBases as $locbase)
		    {
			  $ddparamfilename = $locbase->nodeValue.".xml";
		
			  $status = $dom->load(LocalDataParam.$ddparamfilename);       	   
			  $params = $dom->getElementsByTagName('PARAM');
		//	  if ($locbase->nodeValue == 'SW_Mars') printf($status.PHP_EOL);
			  foreach ($params as $param)
			  {
				$node = $paramsDom->importNode($param,TRUE);	 
				$paramsRoot->appendChild($node);
				$id = $param->getAttribute('xml:id');
		//	 	if ($locbase->nodeValue == 'SW_Mars') printf($id.PHP_EOL);
				$node = $listDom->createElement('PARAM',$id);
				$listRoot->appendChild($node);
			      
			  }
		    }
       	  
       	   $paramsDom->save(paramXml);
       	   $listDom->save(paramListXml);

?>