THEMIS.php 7.13 KB
<?php
/**
 * @class THEMIS 
 * @brief THEMIS
 * @details
 */
class THEMIS extends RemoteDataCenterClass
{
	// /THEMIS_B1/themisdata
   
	protected $baseMgr;
	protected $domTemplate;
	protected $xmlTemplate; 
	
	public function init() 	
	{			
		error_log("THEMIS Proxy creation on ".date("Y-m-d\TH:i:s").PHP_EOL,3,log);
		error_log("THEMIS Proxy creation on ".date("Y-m-d\TH:i:s").PHP_EOL,3,err);
		$this->xmlTemplate = strval(RemoteData).ThemisConfigClass::$TemplateXml;
	} 
	
	protected function getRemoteTree()	
	{
		if (!file_exists($this->xmlTemplate)) {
			error_log('Cannot find THEMIS tree template'.PHP_EOL,3,err); 
			exit('Cannot find THEMIS tree template');
		}
		
		$this->domTemplate = new DomDocument("1.0");
		if (!$this->domTemplate->load($this->xmlTemplate)) {
			error_log('Cannot load THEMIS tree template'.PHP_EOL,3,err); 
			exit('Cannot load THEMIS tree template');
		}
	}
	
	/* 
	*   create base.xml from themis template xml
	*/
	protected function createMissionNodes()
	{
		$missionNodes = array();
			
		$this->baseMgr = new DDBaseMgr();
		foreach (ThemisConfigClass::$themis as $suffix)
		{
			$mission = $this->domAmda->importNode($this->domTemplate->getElementsByTagName("mission")->item(0), true); 
			$template = $mission->getAttribute("name");
			$mission->setAttribute('name', str_replace("XXX", $suffix, $template));
			$mission->setAttribute('desc', str_replace("XXX", $suffix, $template));
			$template = $mission->getAttribute("xml:id");
			$mission->setAttribute('xml:id', str_replace("xxx", strtolower($suffix), $template));
			$instruments = $mission->getElementsByTagName("instrument");
			foreach ($instruments as $instrument) {
				$template  = $instrument->getAttribute("xml:id");
				$instrument->setAttribute('xml:id', str_replace("xxx", strtolower($suffix), $template));
				$datasets = $instrument->getElementsByTagName("dataset");
				foreach ($datasets as $dataset) {
					$template  = $dataset->getAttribute("xml:id");
					$dataset->setAttribute('xml:id', str_replace("xxx", strtolower($suffix), $template));
					$template = $dataset->getAttribute("rem_id");
					$dataset->setAttribute('rem_id', str_replace("xxx", strtolower($suffix), $template));
					$this->updateGlobalStartStop($dataset);
					$this->createVi($dataset);
					
					//$ncFiles = $this->getData($remSetID, $start, $stop);
					$params = $dataset->getElementsByTagName("parameter");
					foreach ($params as $param) {
						$template = $param->getAttribute("xml:id");
						$param->setAttribute('xml:id', str_replace("xxx", strtolower($suffix), $template));
					}
				}
			}
			$missionNodes[] = $mission;
		}
		return $missionNodes;
	}
	
	protected function updateGlobalStartStop($dataset)
	{
		$rem_id = $dataset->getAttribute('rem_id');
		$ViId = strtr(substr($dataset->getAttribute('xml:id'),7),":","_");

		$startTime = $this->getGlobalStart($rem_id);
		$endTime = $this->getGlobalStop($rem_id);
		$dataset->setAttribute('dataStart',$startTime);
		$dataset->setAttribute('dataStop',$endTime);
		$dataset->setAttribute('desc',"$startTime - $endTime");
	}
   
	protected function getGlobalStart($id)
	{
		$startTime = "1970-01-01T00:00:00.000Z";
		$datasetDir = ThemisConfigClass::$NFS_DIR."/".strtr($id,"_" , "/" )."/".ThemisConfigClass::$START_YEAR;
		if (is_dir($datasetDir)) {
			$fileNames = glob($datasetDir."/".$id."*.".ThemisConfigClass::$format);
			//  Get Start  Time  from the First File Name 
			$pos = strpos($fileNames[0],$id) + strlen($id) + 1;
			$firstTime =  substr($fileNames[0], $pos, 8); 
			$startTime = substr($firstTime,0,4)."-".substr($firstTime,4,2)."-".substr($firstTime,6,2)."T00:00:00.000Z";
		}
	  
		return $startTime ;   
	}
	
	protected function getGlobalStop($id)
	{
		$stopTime = "1970-01-01T00:00:00.000Z";
		$datasetDir = ThemisConfigClass::$NFS_DIR."/".strtr($id,"_" , "/" )."/".date('Y');
		if (is_dir($datasetDir)) {
			$fileNames = glob($datasetDir."/".$id."*.".ThemisConfigClass::$format);
			//  Get Stop  Time  from the Last File Name
			$pos = strpos($fileNames[count($fileNames)-1],$id) + strlen($id) + 1;
			$lastTime =  substr($fileNames[count($fileNames)-1], $pos, 8);
			$stopTime = substr($lastTime,0,4)."-".substr($lastTime,4,2)."-".substr($lastTime,6,2)."T23:59:59.000Z";
		}
	  
		return $stopTime ;   
	}
  
	protected function createVi($dataset)
	{
		$ViId = strtr(substr($dataset->getAttribute('xml:id'),7),":","_");
		$globalStart = $dataset->getAttribute('dataStart');
		$globalStop = $dataset->getAttribute('dataStop');
		$info = explode("_",$ViId);
		echo $globalStart." ".$globalStop.PHP_EOL;
		if ($this->baseMgr->viExists($ViId, $this->baseID)) {	
		   // error_log
            $this->baseMgr->setViInfo($info[2]);
            $this->baseMgr->setViLocation($this->baseID."/".$info[0], $info[1]."/".$info[2]."/");
            $this->baseMgr->updateViStop($globalStop); 
			return;
		}
		$this->baseMgr->setViId($ViId);
		$rem_id = $dataset->getAttribute('rem_id').":".$info[2];
		$this->baseMgr->setRemoteViId($rem_id);
		$this->baseMgr->setViInfo($info[2]);
		$this->baseMgr->setViParentsInfo($this->baseID, $info[0], $info[1]);
 
		$this->baseMgr->setViLocation($this->baseID."/".$info[0], $info[1]."/".$info[2]."/");
		$min_sampling = $dataset->getAttribute('minSampling');
		$max_sampling = $dataset->hasAttribute('maxSampling') ? $dataset->getAttribute('maxSampling') : 0 ; 
		$this->baseMgr->setViSampling($min_sampling, $max_sampling);
		$this->baseMgr->globalStart = $globalStart;
		$this->baseMgr->globalStop = $globalStop;
		
		$this->baseMgr->createVi();
		
		$ncFiles = array();
		$start = $globalStart;

		while (count($ncFiles) == 0) {
			// get the very first data file
			$stop = $start;
			$ncFiles = $this->getData($rem_id, $start, $stop);
			
			$newStart = strtotime($start);
			$newStart += 24*60*60;
			$start = date("Y-m-d\TH:i:s",$newStart);
		}
		
		$this->baseMgr->addRemoteData($rem_id, $ncFiles, $globalStart, $globalStart, false);
		
		$this->baseMgr->updateRemoteStart();
	}
     
	protected function setDataCenterAttributes(){	
		$this->dataCenter->setAttribute('xml:id', $this->baseID);
		$this->dataCenter->setAttribute('name', $this->baseID."_iRAP");		
	}
	
	public function updateBufferDataBase() {
        echo "THEMIS : UPDATE automatic".PHP_EOL;
	}
	
	protected function makeArgumentsList(){}
	
	public function getData($remId, $start, $stop)
	{
		$info = explode(":", $remId);
		$rem_id = $info[0];
		$mode = $info[1];
		
		$info_1 =  explode("_", $rem_id);
		$mission = $info_1[0];
		
		$sourceDir = ThemisConfigClass::$NFS_DIR."/".strtr($rem_id,"_" , "/" );
		// Which files 
		$currTime = strtotime($start);
		$stopTime = strtotime($stop);

// 		$currTime -= 3600.0;
		
		while ($currTime <= $stopTime ) {
			$fileName = $sourceDir."/".date('Y', $currTime)."/".$rem_id."_".date('Ymd', $currTime)."_v01.cdf";
			if (file_exists($fileName)) {             
					system(ThemisConfigClass::$program[$mode]." ".$fileName." ".$mission." ".$mode); 
			}
			else {
					// fprintf($STDERR,"NO SUCH FILE  $fileName \n");
			}
			$currTime += 24*60*60;
		}  
		
		$files = glob($mode."*.nc");
	 
		return $files;
	}
	
	public function monitor()
	{
		// check if NFS connection ok
		if (glob(ThemisConfigClass::$NFS_DIR . "/*"))
					return true;
			 
		return false;
	}
} 
?>