WsrParamMgr.php 5.01 KB
<?php
/**
 * @class WsrParamMgr
 * @version $Id: $
 *    
 */

  class WsrParamMgr
  {
      protected $startTime, $stopTime;
      protected $productor;
      protected $parameterID;
      protected $dataProducteur;
      
      function __construct() {
  	  ini_set("soap.wsdl_cache_enabled", "0"); 
      }
      
      public function setParams($data){

	$this->parameterID	= $data['paramID']; 
	$this->startTime	= $data['startDate'];	
	$this->stopTime		= $data['stopDate'];
	$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);  
      }  

      protected function getDataProducteur(){
      
    // TODO tests 
    
	if (isset($this->parameterID)){
	  $tmp 		=  explode( '_', $this->parameterID);  
	  return $tmp[0];
	}
	else return false;
    }
    
         protected function makeRequest(){
	 // genaration webservice request 
            $tree = tree_CLWEB;
            $xml = RemoteData.$tree;
            $dom = new DOMDocument('1.0', 'utf-8');

            if($dom->load($xml)){
            
            $paramByID = $dom->getElementById($this->parameterID);
            if ($paramByID != NULL)
                $param = $paramByID->getAttribute('cl_id');

            if ($param != ''){
                $params = array('parameterID'	=>	$param,	
                                'startTime'	=>	$this->startTime,
                                'stopTime'	=>	$this->stopTime,
                                'outputFormat'	=>	'netCDF'
                                );
            return $params;
            }
            else return false;
            }
            else return false;
      }
      
      protected function getDataProducteurWSDL(){
      
        if ($this->dataProducteur){
        $wsdl = wsdl_CLWEB;
        return $wsdl;
        }
        else return false;
      
      }
      
       protected 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));
        }
        $file = getenv('USER_DATA_PATH').$fileName;
        $status = $this->reformatNcTime($file);
        if ($status <= 0) {
                unlink($this->fileName);
                return array('success' => false, 'error' => 'error '.$timeFormat.PHP_EOL.'Time Format problem');	
            }
      return array( 'success' => true, 'file' => $fileName);
      }
      
      protected function reformatNcTime($file) {
        exec('nctimestring2double '.$file, $results); 
        return $results[0]; 
    }

      public function getFileName(){
	// file name generation with parameter ID(frome [DataProducteur]/base.xml), start & stop time in unix format
	if ($this->parameterID && $this->parameterID != ''){
	    $fileName = $this->parameterID.'_'.strtotime($this->startTime).'_'.strtotime($this->stopTime).'.nc';
	    
	    return $fileName;
	}
	else return false;
      }

      public function getMask(){
        if ($this->parameterID && $this->parameterID != ''){
            $mask = $this->parameterID.'_*.nc';
            return $mask;
            }
        else return false;
      }

    public function getData(){

      $fileName = $this->getFileName();
      $newfile = getenv('USER_DATA_PATH').$fileName; 
      if ($fileName){
        try {
                $client = new SoapClient(wsdl_CLWEB,
                array(
                        'wsdl_cache' => 0,
                        'trace' => 1,
        //	        'exceptions' => 0,
                        '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 = $this->makeRequest($this->parameterID); 
        // call WS 
         try {
                $file = $client->getParameter($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;
         }
        // copy result file to user DATA directory
        if (!copy($file->dataFileURLs, $newfile)) {                  
                error_log('Can\'t copy '.$file,1,email);
                return false;
        }
        else {
            chmod($fileName, 0775);
            $this->addFile($fileName);
            $wsrFilenames[$this->parameterID] = $fileName; 
        }        
        return $wsrFilenames;    
       } //end  if ($fileName)
      else
          return false;  
  }

  }

?>