FilterResSimu.php 9.26 KB
<?php
/** 
*   @file FilterResSimu.php
*   @brief  Simations Filter Manager 
*
*   
*   @version $Id: 
*/
  class SimuFileMgr 
  {
  	 protected $simXmlName, $simXml, $simRootEl, $simXP;
  	 public $centerID;
  	 
  	 function __construct($basePath,$centerID) 
  	 {
 	    $this->simXmlName = $basePath;
	    $this->simXml     = new DomDocument("1.0");
	    if (file_exists($this->simXmlName)) {
	    	@$this->simXml->load($this->simXmlName); 
	    	$this->simRootEl  = $this->simXml->documentElement;
	    }
	    $this->simXP      = new domxpath($this->simXml);
	    $this->centerID   = $centerID;
  	 }
  
    function queryDatasets()
      {
	      return $this->simXP->query("//dataRoot/dataCenter/simulationModel/simulationRegion/runID/dataset");
      }

    function getDatasetFilterPath($dataset)
      {
	    $datasetID = $dataset->getAttribute('xml:id');
				    
	    $run = $dataset->parentNode;
	    $runID = $run->getAttribute('xml:id');
			      
	    $simReg = $run->parentNode;
	    $simRegID = $simReg->getAttribute('xml:id');

	    $model = $simReg->parentNode;
	    $modelID = $model->getAttribute('xml:id');
		    
	    $center = $model->parentNode;
	    $centerID = $center->getAttribute('xml:id');
	    
	    return $centerID.";".$modelID.";".$simRegID.";".$runID.";".$datasetID;
      }
  	 
    private function getRunFromDataset($dataset)
  	 {
	    $run        = $dataset->parentNode;
	    return $run;
  	 }
   	 
  	 function getReleaseDate($dataset)
  	 {
	    $run = $this->getRunFromDataset($dataset);
	    if (!run || !$run->hasAttribute('ReleaseDate'))

	      return '0';
		  return strtotime($run->getAttribute('ReleaseDate'));
  	 }

	 function getSimulatedRegion($dataset) {
	 
	    if (isset($dataset->parentNode))
	      $run = $dataset->parentNode;  
	    if (isset($run->parentNode)) {
	    if ($run->parentNode->hasAttribute('name'))
	      $simReg = $run->parentNode->getAttribute('name');
	    return $simReg;
	    }
	    else return false;
  	 }

	function getDatasetAttribute($dataset ,$attribute)
  	 {
	    $run = $this->getRunFromDataset($dataset);
	    if (!run || !$run->hasAttribute($attribute))

	      return '';
		  return $run->getAttribute($attribute);
  	 }
  }


  //Get filter result for all simulations bases
  class FilterResSimu extends FilterRes
  {
  	protected $arrayFilesMgr;
  	
  	function __construct() 
  	{
	    $this->arrayFilesMgr = array();
	    array_push($this->arrayFilesMgr,new SimuFileMgr(RemoteData."LATMOS/base.xml",'LATMOS'));
	    array_push($this->arrayFilesMgr,new SimuFileMgr(RemoteData."FMI_GUMICS/base.xml",'FMI_GUMICS'));
	    array_push($this->arrayFilesMgr,new SimuFileMgr(RemoteData."FMI_HYBRID/base.xml",'FMI_HYBRID'));
	    array_push($this->arrayFilesMgr,new SimuFileMgr(RemoteData."SINP/base.xml",'SINP'));
  	}
  	
	function getDataProd($value,$opId)
  	{
  		 $result = array();

  		 foreach ($this->arrayFilesMgr as $fileMgr){
  		 	 
  		   if ((($value == $fileMgr->centerID) && ($opId == 'eq')) || (($value != $fileMgr->centerID) && ($opId == 'ne')))
  		   {
  		     $datasets = $fileMgr->queryDatasets();
  		     foreach ($datasets as $dataset){ 
  		       array_push($result,$fileMgr->getDatasetFilterPath($dataset));
		      }
  		   }
  		  }
  		 return $result;
  	}
  	
  	function getReleaseDate($value,$opId)
  	{
  		 $result = array();
  		  
  		 $val  = strtotime($value);
  		   	 
  	  foreach ($this->arrayFilesMgr as $fileMgr)
  	  {
  		   $datasets = $fileMgr->queryDatasets();
  		   foreach ($datasets as $dataset)
  		   {
  		   	 $date = $fileMgr->getReleaseDate($dataset);
  		   	 if (($date == $val) || (($date < $val) && ($opId == 'le')) || (($date > $val) && ($opId == 'ge')))
  		       array_push($result,$fileMgr->getDatasetFilterPath($dataset));
  		   }
  		 }
  	  
  	  return $result;
  	}


  	function getSimuRegion($value,$opId) {
	  $result = array();
  		
  	  foreach ($this->arrayFilesMgr as $fileMgr) {
	      $datasets = $fileMgr->queryDatasets();
	      foreach ($datasets as $dataset)
	      {
		  if ($fileMgr->getSimulatedRegion($dataset)){
		    $isSimuRegion = ($fileMgr->getSimulatedRegion($dataset) == $value);
		    if (($isSimuRegion && ($opId == 'eq')) || (!$isSimuRegion && ($opId == 'ne')))
			array_push($result,$fileMgr->getDatasetFilterPath($dataset));
		  }
	      }
  	}
  	  
  	  return $result;

  	}

	  	
  	function getInfoFromRunIDEqual($value,$opId, $attribute)
  	{
  		 $result = array();
  		  
  	  foreach ($this->arrayFilesMgr as $fileMgr)
  	  {
  		   $datasets = $fileMgr->queryDatasets();
  		   foreach ($datasets as $dataset)
  		   {
  		   	 $isAttribute = ($fileMgr->getDatasetAttribute($dataset, $attribute) == $value);
  		   	 if (($isAttribute && ($opId == 'eq')) || (!$isAttribute && ($opId == 'ne')))
  		       array_push($result,$fileMgr->getDatasetFilterPath($dataset));
  		   }
  		 }
  	  
  	  return $result;
  	}

	function getInfoFromRunIDComp($value,$opId, $attribute)
  	{
  		 $result = array();
		 $val  = floatval($value); 
  		  
  	  foreach ($this->arrayFilesMgr as $fileMgr)
  	  {
  		   $datasets = $fileMgr->queryDatasets();
  		   foreach ($datasets as $dataset)
  		   {
  		   	 $attrVal = floatval($fileMgr->getDatasetAttribute($dataset, $attribute));
  		   	 if ((($attrVal == $val) || (($attrVal < $val) && ($opId == 'le')) || (($attrVal > $val) && ($opId == 'ge'))) && $attrVal != '')
  		       array_push($result,$fileMgr->getDatasetFilterPath($dataset));
  		   }
  		 }
  	  
  	  return $result;
  	}
  	
  	function getAll()
  	{
  		 
  	}
	
  	function getCondResult($cond)
  	{
  		$condId = $cond['condId'];
  		switch($condId)
  		{
  			case 'dprod' : //data productor
  				return $this->getDataProd($cond['value'],$cond['opId']);
  			case 'simt' : //simulation type
  				return $this->getInfoFromRunIDEqual($cond['value'],$cond['opId'], 'SimulationType');
  			case 'reld' : //release date
  				return $this->getReleaseDate($cond['value'],$cond['opId']);
  			case 'simReg' : //simulated region
  				return $this->getSimuRegion($cond['value'],$cond['opId']);
 			case 'tempDepend' : //Temporal Dependence
  				return $this->getInfoFromRunIDEqual($cond['value'],$cond['opId'], 'TemporalDependence');
 			case 'rating' : //Likelihood Rating
  				return $this->getInfoFromRunIDEqual($cond['value'],$cond['opId'], 'LikelihoodRating');
 			case 'GridCellSize_X' : //Likelihood Rating
  				return $this->getInfoFromRunIDComp($cond['value'],$cond['opId'], 'GridCellSize_X');
 			case 'GridCellSize_Y' : //Likelihood Rating
  				return $this->getInfoFromRunIDComp($cond['value'],$cond['opId'], 'GridCellSize_Y');
 			case 'GridCellSize_Z' : //Likelihood Rating
  				return $this->getInfoFromRunIDComp($cond['value'],$cond['opId'], 'GridCellSize_Z');
 			case 'FieldValue_Bx' : //Likelihood Rating
  				return $this->getInfoFromRunIDComp($cond['value'],$cond['opId'], 'FieldValue_Bx');
			case 'FieldValue_By' : //Likelihood Rating
  				return $this->getInfoFromRunIDComp($cond['value'],$cond['opId'], 'FieldValue_By');
			case 'FieldValue_Bz' : //Likelihood Rating
  				return $this->getInfoFromRunIDComp($cond['value'],$cond['opId'], 'FieldValue_Bz');
			case 'Solar_Wind_electrons-Density' : //Likelihood Rating
  				return $this->getInfoFromRunIDComp($cond['value'],$cond['opId'], 'Solar_Wind_electrons-Density');
			case 'Solar_Wind_electrons-Temperature' : //Likelihood Rating
  				return $this->getInfoFromRunIDComp($cond['value'],$cond['opId'], 'Solar_Wind_electrons-Temperature');
			case 'Solar_Wind_electrons-FlowSpeed' : //Likelihood Rating
  				return $this->getInfoFromRunIDComp($cond['value'],$cond['opId'], 'Solar_Wind_electrons-FlowSpeed');
			case 'Solar_Wind_H-Density' : //Likelihood Rating
  				return $this->getInfoFromRunIDComp($cond['value'],$cond['opId'], 'Solar_Wind_H-Density');
			case 'Solar_Wind_H-Temperature' : //Likelihood Rating
  				return $this->getInfoFromRunIDComp($cond['value'],$cond['opId'], 'Solar_Wind_H-Temperature');
			case 'Solar_Wind_H-FlowSpeed' : //Likelihood Rating
  				return $this->getInfoFromRunIDComp($cond['value'],$cond['opId'], 'Solar_Wind_H-FlowSpeed');
			case 'Solar_Wind_He-Density' : //Likelihood Rating
  				return $this->getInfoFromRunIDComp($cond['value'],$cond['opId'], 'Solar_Wind_He-Density');
			case 'Solar_Wind_He-Temperature' : //Likelihood Rating
  				return $this->getInfoFromRunIDComp($cond['value'],$cond['opId'], 'Solar_Wind_He-Temperature');
			case 'SolarUVFlux' : //Likelihood Rating
  				return $this->getInfoFromRunIDComp($cond['value'],$cond['opId'], 'SolarUVFlux');
		}
	}
	 	function getResult($filter,$isEmptyFilter)
  	{  		
 		 if ($isEmptyFilter)
  			return $this->getAll();
  		$ops = array();
		$cond_res = array();
  		for ($i = 0 ; $i < count($filter); $i++)
  		{     
  			$cond = $filter[$i];
  			$cond_res[$i] = $this->getCondResult($cond);
  			$ops[$i] = $cond['logical'];
  		}
  		
  		for ($i = 1; $i < count($cond_res); $i++)
  		{
  			if ($ops[$i] != 'and')
  				continue;
  			$cond_res[$i-1] = $this->andOp($cond_res[$i-1],$cond_res[$i]);
  			array_splice($cond_res, $i, 1);
  			array_splice($ops, $i, 1);
			$i--;		
  		}
  		
  		for ($i = 1; $i < count($cond_res); $i++)
  		{
  			if ($ops[$i] != 'or')
  				continue;
  			$cond_res[$i-1] = $this->orOp($cond_res[$i-1],$cond_res[$i]);
  			array_splice($cond_res, $i, 1);
  			array_splice($ops, $i, 1);
			$i--;			
  		}
  		return $cond_res[0];
  	}
  }
?>