SimuParamMgr.php 14.6 KB
<?php
/** 
*   @class SimuParamMgr.php
*   @brief  Simations parameters Manager (Simulations db client) 
* 
*   @version $Id: SimuParamMgr.php 2914 2015-05-19 10:31:38Z elena $
*/
  class SimuParamMgr  
  {
// input parameters
      protected $extraParam, $sampling,$paramAtribute;
      protected $dataProducteur;
      protected $orbitsURL;
      protected $parameterID, $resourceID;
      protected $isArgument, $isVector;
      protected $mission;
      protected $soapMethode;
      public $startTime, $stopTime;
      function __construct() {
        ini_set("soap.wsdl_cache_enabled", "0");
      }
      
      public function setParams($paramID, $startDate, $stopDate, $paramsAtribute){
	  $this->parameterID= $paramID; 
	  $this->startTime= $startDate;
	  $this->stopTime= $stopDate;
	  $this->paramAtribute= $paramsAtribute;
	  $this->mission=  $this->paramAtribute[$this->parameterID]['Satellite']; 
	  $this->isArgument = false;      
	  $this->isVector = false;    
	  $this->dataProducteur   = $this->getDataProducteur();
      }

       public function updateInterval($start, $stop){         
          $this->startTime = gmdate("Y-m-d\TH:i:s\Z", $start);
          $this->stopTime =  gmdate("Y-m-d\TH:i:s\Z", $stop);  
      } 

      private function getOrbitsURL(){
	  try {
	      $client = new SoapClient(wsdl_AMDA,
	      array(
                'wsdl_cache' => 0,
                'trace' => 1,
                'soap_version'=>SOAP_1_2
	      ));
	  }
	  catch  (SoapFault $exception) {
                error_log("Problem with WSDL".PHP_EOL.$exception->getMessage().PHP_EOL.$exception->getTraceAsString().PHP_EOL."File =  ".$exception->getFile(),1,email);  
	        return false;
           }
 	  
            $params = array('spacecraft'=> strtoupper($this->mission),
                'startTime'=> $this->startTime, 
                'stopTime'=> $this->stopTime,
                'coordinateSystem'=> $this->getCoordinateSystem(),
                'units'=> $this->getDomainUnits(),
                'outputFormat'  => 'VOTable');

            if ( strtoupper($this->mission) == "MESSENGER") $params['sampling'] = 60;
            if ( strtoupper($this->mission) == "GALILEO") $params['sampling'] = 60;

            try {
                $file = $client->__soapCall("getOrbites", array($params));      
            }
                catch  (SoapFault $exception) {
                error_log("Problem with query".PHP_EOL.$exception->getMessage().PHP_EOL.$exception->getTraceAsString().PHP_EOL."File =  ".$exception->getFile(),1,email);  
                return false;
             } 

            $orbitURL = $file->dataFileURLs;

	return $orbitURL;
      }
      
      private function makeRequest(){

        $this->setResourceID();
  
        if ($this->productor == 'LESIA'){  
            $params = array('ResourceID'=> $this->resourceID,
            'Spacecraft_name'=> $this->mission,
            'StartTime'=> $this->startTime, 
            'StopTime'=> $this->stopTime,
            'Sampling'=> $this->sampling,
            'extraParams'=> $this->extraParam);

            }
        elseif ($this->productor == 'LATMOS') {
            $clockAngle = $this->paramAtribute[$this->parameterID]['ClockAngle'];
            $var = $this->getVariable($this->parameterID);

            if (!$this->isVector) {
                $params = array('ResourceID'=> $this->resourceID,
                'url_XYZ'=> $this->getOrbitsURL(), 
                'extraParams'=> $this->extraParam,
                'IMFClockAngle'=> $clockAngle,
                'Variable'=> $var);
            }
            else {
                $params = array('ResourceID'=> $this->resourceID,
                'url_XYZ'=> $this->getOrbitsURL(), 
                'extraParams'=> $this->extraParam,
                'IMFClockAngle'=> $clockAngle);
            }
        }	    
        else {  
            $params = array('ResourceID'=> $this->resourceID,
            'url_XYZ'=> $this->getOrbitsURL(), 
            'extraParams'=> $this->extraParam);
            // 'Variable'=> $this->getVariable($this->parameterID));
        }
        return $params;
      }

      private function getDataProducteurWSDL(){
   // TODO tests

        switch ($this->dataProducteur){
            case 'LATMOS':
            $wsdl = wsdl_LATMOS;
            $this->productor = 'LATMOS';
            $this->soapMethode = 'getDataPointValue';
            break;
            case 'FMI_HYB':
            $wsdl = wsdl_FMI;
            $this->productor = 'FMI_HYBRID';
        //       $this->extraParam= array('InterpolationMethod' => $options[3]);
            $outputFileType = array('OutputFileType'=>'VOTable');
            $this->soapMethode =  'getDataPointValue';
            $this->extraParam= $outputFileType;
            break;
            case 'FMI_GUMICS':
            $wsdl = wsdl_FMI;
            $this->productor = 'FMI_GUMICS';
        //       $this->extraParam= array('InterpolationMethod' => $options[3]);
            $outputFileType = array('OutputFileType'=>'VOTable');
            $this->soapMethode =  'getDataPointValue';
            $this->extraParam= $outputFileType;
            break;  
            case 'SINP':
            $wsdl = wsdl_SINP;
            $this->productor = 'SINP'; 
        //       $extraParam = array();
            $imf_b = array();
            $this->extraParam['OutputFileType']= 'VOTable'; 
            if (strpos($this->parameterID,'Earth') !== FALSE)
                    $this->soapMethode =  'calculateDataPointValue'; 
            elseif ((strpos($this->parameterID,'Mercury') !== FALSE)||(strpos($this->parameterID,'Saturn') !== FALSE) ||(strpos($this->parameterID,'Jupiter') !== FALSE)){
            if (strpos($this->parameterID,'Mercury') !== FALSE)
                $this->soapMethode =  'calculateDataPointValueMercury';
            elseif (strpos($this->parameterID,'Saturn') !== FALSE)
            $this->soapMethode =  'calculateDataPointValueSaturn';
            elseif (strpos($this->parameterID,'Jupiter') !== FALSE)
            $this->soapMethode =  'calculateDataPointValueJupiter';
            foreach($this->paramAtribute[$this->parameterID] as $key => $value){ 
                if ($key != 'Satellite'){ 
                    if ($key != 'IMF_Bx' && $key != 'IMF_By' && $key != 'IMF_Bz')
                        $this->extraParam[$key]= $value;
                    else  {
                    switch ($key){
                        case 'IMF_Bx':
                            $imf_b['x'] = $value;
                            break;
                        case 'IMF_By':
                            $imf_b['y'] = $value;
                            break;
                        case 'IMF_Bz':
                        $imf_b['z'] = $value;
                        break;
                        }
                        }
                    } 
                }
                $this->extraParam['IMF_B']= $imf_b;
            }
            
            break;
            case 'LESIA':
            $wsdl = wsdl_LESIA;
            $this->productor = 'LESIA';
            $this->soapMethode = 'calculateDataPointValue_Spacecraft';
            $this->extraParam= array('OutputFileType'=>'VOTable',
                                        'InternalModel'=>$this->paramAtribute[$this->parameterID]['InternalModel'],
                                        'CurrentSheet'=>$this->paramAtribute[$this->parameterID]['CurrentSheet'],
                                        'CoordinateSystem'=>$this->paramAtribute[$this->parameterID]['CoordinateSystem']
            );
            $this->sampling = $this->paramAtribute[$this->parameterID]['sampling'];
            break;
        }
        return $wsdl;
      }

      private function setResourceID(){
       
        $this->resourceID = $this->dom->getElementById($this->parameterID)->getAttribute('parentID');

        if (($this->dom->getElementById($this->parameterID)->nodeName)=='component') $this->isArgument = true;
        else {
            $this->isArgument = false;
            if ($this->dom->getElementById($this->parameterID)->getAttribute('size') == 3)
            {
                $this->isVector = true;
            }
        }
    //    return $this->resourceID;
      }
      
      private function getDomainUnits(){
	$units = $this->dom->getElementById($this->resourceID)->getAttribute('domainUnits');
	if ($units == 'm') $units='km';
	return $units;
      }

      private function getCoordinateSystem(){  
        return $this->dom->getElementById($this->resourceID)->getAttribute('coordinateSystem');
      }
      
      private function addFile($fileName){
      
        $myBaseManager = new BaseManager();
        $start = strtotime($this->startTime);
        $stop = strtotime($this->stopTime);
        $mask = $myBaseManager->addWsFile($fileName, $start, $stop); 
        if ($mask != null) {
            $startstop = $myBaseManager->getStartStop($myBaseManager->getVi($mask));
        }
      return array( 'success' => true, 'file' => $fileName);
      }
      
      private function getVariable($paramID){
        $tmp =  explode( '_', $paramID);
        $n = count($tmp)-1;
        $var =  $tmp[$n];

        return $var;
      }
      
      private function getVectorName($paramID){
        $tmp =  explode( '_', $paramID);
        $n = count($tmp)-1;
        $var =  $tmp[$n-2].'_'.$tmp[$n-1].'_'.$tmp[$n];

        return $var;
      }
      
      private function getDataProducteur(){

        if (strncmp($this->parameterID, "impex___", 8) === 0)  
            $par =    str_replace ("impex___", "", $this->parameterID);
        elseif (strncmp($this->parameterID, "spase___IMPEX_NumericalOutput_", 30) === 0)
            $par =    str_replace ("spase___IMPEX_NumericalOutput_", "", $this->parameterID); 
        elseif (strncmp($this->parameterID, "spase___IMPEX_SimulationRun_", 28) === 0)
            $par =    str_replace ("spase___IMPEX_SimulationRun_", "", $this->parameterID); 
        
        // TODO tests
        $tmp =  explode( '_', $par);  
        if ($tmp[0]== 'FMI')
            return $tmp[0].'_'.$tmp[1];
        else
            return $tmp[0];
      
      }
      
      public function getFileName(){

        $paramID = $this->parameterID;
        $paramAtribute = $this->paramAtribute[$this->parameterID];
        foreach ($paramAtribute as $k => $v) 
            $args = $args.$v.'_'; 
        $fileName = $paramID.'_'.$args.strtotime($this->startTime).'_'.strtotime($this->stopTime).'.xml';
        return $fileName;
      }

      public function getMask(){
        $paramID = $this->parameterID;

        $paramAtribute = $this->paramAtribute[$this->parameterID];
        foreach ($paramAtribute as $k => $v) 
            $args = $args.$v.'_'; 
        $mask = $paramID.'_'.$args.'*.xml';
        return $mask;
      }
      
      public function isVector(){
     
        return $this->isVector;
      }
      
      
      public function getSimuData()
      {
        $simuFilenames = array();
           
        if ((strncmp($this->parameterID, "impex___", 8) === 0) || (strncmp($this->parameterID, "spase___", 8) === 0))
        {  
            $paramID = $this->parameterID;
            $fileName = $this->getFileName();
            $newfile = getenv('USER_DATA_PATH').$fileName;
 
            if (!file_exists ($newfile))
            { 
                if ($this->dataProducteur != 'CCMC')
                    {
                    try {
                            $client = new SoapClient($this->getDataProducteurWSDL(),
                            array(
                                'wsdl_cache' => 0,
                                'trace' => 1,
                                'soap_version'=>SOAP_1_2
                            ));
                        }
                    catch  (SoapFault $exception) {
                            error_log("Problem with WSDL".PHP_EOL.$exception->getMessage().PHP_EOL.$exception->getTraceAsString().PHP_EOL."File =  ".$exception->getFile(),1,email);  
                            return false;
                    } 

                    if ($this->productor != 'FMI_GUMICS')
                        $xml = RemoteData.$this->productor."/base.xml";
                    else
                        $xml =  getenv('USER_WS_PATH').'RemoteParams.xml';

                    $this->dom = new DOMDocument('1.0', 'utf-8');
                    $this->dom->load($xml);
                    $params = $this->makeRequest($this->parameterID);

     
                    try {
                        $file = $client->__soapCall($this->soapMethode, array($params)); 
                        }
                    catch  (SoapFault $exception) {
                         error_log($exception->getMessage().PHP_EOL.$exception->getTraceAsString(),1,email); 
                         return false; 
                    } 

                    if (!copy($file, $newfile)) {
                        error_log('Can\'t copy '.$file,1,email);
                        return false;
                    }
                    else {
                        if ($this->isVector){
                                $vectorName = $this->getVectorName($paramID);
                                $myVOTableMgr = new VOTableMgr();
                                $myVOTableMgr->args2vector($newfile, $vectorName);
                        }
                        $this->addFile($fileName);
                        $simuFilenames[$paramID] = $fileName; 
                    }
            }   // end if $this->dataProducteur != 'CCMC'
            else {
                $docBase = new DOMDocument();
                $docBase->load(RemoteData.'CCMC/base.xml');
                $elementParam =  $docBase->getElementById($this->parameterID);

                if ($elementParam->tagName == 'component')
                    $accessURL = $elementParam->parentNode->parentNode->getAttribute('AccessURL');
                elseif ($elementParam->tagName == 'parameter'){
                    $accessURL = $elementParam->parentNode->getAttribute('AccessURL');
                    if ($elementParam->getAttribute('size') == 3) $this->isVector = true;
                }
                else {}

                $doc = new DOMDocument();
                $doc->load($accessURL);
                $doc->save($newfile);

                if ($this->isVector){
                    $vectorName = $this->getVectorName($this->parameterID);
                    $myVOTableMgr = new VOTableMgr();
                    $myVOTableMgr->args2vector($newfile, $vectorName);
                    }

                $this->addFile($fileName);
                $simuFilenames[$paramID] = $fileName; 
            }
        } // if (!file_exists ($newfile))
      } // if ((strncmp($this->parameterID, "impex___", 8) === 0) |

    return $simuFilenames;
    }
  
  }
?>