createVI.php 3.96 KB
<?php
/**----------------------------------------------------------------------
*                   DD SERVER TOOLS
*                   data update collection 
*  @file  createVI.php
*  @brief DD_Server tool <br> Creates New Virtual Instrument
*  @version  
*   Creates New VI at DD_Server Side \n
*   Args: dataBaseID  ddDataSetID  remoteDataSetID \n
*   Env Vars:  $DDBASE $DDBASEBIN $DDLIB \n
*   Executables: makeDDsys info2nc TimesUpdate \n
*
* @arg Checks if VI already exists in dataBase desctiptor DDsys.xml
* @arg Adds non-existing VI to DDres.xml 
* @arg makeDDsys creats refer.nc file from DDsys.xml
* @arg Gets dataSet Info from INFO/DDServer dir
* @arg Creates dir, *_cache.nc, *_info.nc and empty *_times.nc
*
*/
  
	if (!function_exists('__autoload')) 
	{
		function __autoload($class_name) {
				require_once $class_name . '.php';
		}
	}
	
   date_default_timezone_set('UTC');
   putenv("LD_LIBRARY_PATH=".getenv("LD_LIBRARY_PATH"));
	putenv("PATH=./:".getenv("DDBASEBIN").":/bin:/usr/bin");
	set_include_path("./:".getenv("DATAMANAGER").":".getenv("REMOTEDATA").":".getenv("CALLEXT")); 

	$verbose = true;

	$DDBASE=getenv("DDBASE");
	if (is_link($DDBASE)) {
		$DDBASE=readlink($DDBASE);
		if ($DDBASE === FALSE) {
			exit(0);
		}
	}
	define('RemoteData',  $DDBASE."/../INFO");
	
	$LOGDIR = $DDBASE."/../LOG/";
	if (!is_dir($LOGDIR))
				mkdir($LOGDIR, 0755, true);
				
	define("log",$LOGDIR."/addVI.log");
	define("err",$LOGDIR."/addVI.err");
			
	if ($argc != 4) 
	{
		error_log("Usage: php createVI.php <BaseID> <RemSetID> <NewVIName>".PHP_EOL, 3, err);
		exit("Usage: php createVI.php <BaseID> <RemSetID> <NewVIName>".PHP_EOL);
	}
 
	$base = strtoupper($argv[1]);
	$remSetID = $argv[2];
	$ViId = strtolower($argv[3]); 
	
	if ($verbose)
			error_log(date("Y-m-d\TH:i:s")." Request to add Data Set ".$base.":".$remSetID." as ".$ViId.PHP_EOL, 3, log);      
	error_log(date("Y-m-d\TH:i:s")." Request to add Data Set ".$base.":".$remSetID." as ".$ViId.PHP_EOL, 3, err);
	
	$baseMgr = new DDBaseMgr();	
   
	$nc_prefix = strlen($ViId) > RemoteDataCenterClass::$MAX_VI_NAME_LENGTH ? 
					substr($ViId, 0, RemoteDataCenterClass::$MAX_VI_NAME_LENGTH - 1): $ViId;
 
	if ($baseMgr->viExists($ViId, $base)) {
		if ($verbose)
				error_log($base." : ".$ViId." already Exists!!!".PHP_EOL, 3, log);
				
		die("$ViId Already Exists!!!".PHP_EOL);
	}

	$baseMgr->setViId($ViId);
	$baseMgr->setViInfo($nc_prefix);
	$status = $baseMgr->setViInfoFromFile($base, $remSetID);

	if ($status === 0) 
	{
		$baseMgr->createVi();
		$center = new $base();

		$startStamp = strtotime($baseMgr->globalStart);
		$stopStamp = strtotime($baseMgr->globalStart."+1 day");
		$start = date( "Ymd\THis\Z", $startStamp);
		$stop = date( "Ymd\THis\Z", $stopStamp-60.0);
		
		$startIso = date( "Y-m-d\TH:i:s\.000\Z", $startStamp);
		$stopIso = date( "Y-m-d\TH:i:s\.000\Z", $stopStamp);
		// Get Full Dataset Info file from distant database
		$infoFile = $center->getDatasetInfo($remSetID);
		 
		if ($infoFile) 
		{			
			$baseMgr->setInfo($infoFile);
		}
		 
		$ncFiles = $center->getData($remSetID, $start, $stop);
		
		if (!$infoFile && $ncFiles)
		{
			 if ($verbose) 
					error_log("No dataset info file for ".$remSetID." from ".$ViId." uses regular file".PHP_EOL, 3, log);
					
			$infoFileName = strtolower($remSetID).".nc";		
			copy($ncFiles[0], $infoFileName);		
			$baseMgr->setInfo($infoFileName);
		}
		
		$baseMgr->addRemoteData($remSetID, $ncFiles, $startIso, $stopIso); 
		// put real Global Start from file - IMPORTANT for new AMDA Kernel
		$baseMgr->updateRemoteStart();
		
//     if ($verbose) error_log("Get dataset info for ".$remSetID." from ".$dataBaseID." returns ".$res.PHP_EOL, 3, log);
	 
				 

// 			if ($AddInfo) 
// 			{
// 				$function_name = "createHeader".$dataBaseID;
// 				require_once(DDLIB.$function_name.".php");
// 				$function_name($remSetID); 
// 			}
//			if (verbose) error_log("Added ".$VIID.PHP_EOL, 3, log); 
	}
	else 
	{
		if ($verbose) error_log("No DDServer Info File for $base  $ViId".PHP_EOL, 3, log); 
	}
		 
?>