createVI.php 10.7 KB
<?php
/*----------------------------------------------------------------------
 *                   DD SERVER TOOLS
 *                   data update collection 
 *                    createVI.php
 *   17 Jun 2007 V.1.0
 *   01 Aug 2007 V.1.1 Only one argument in info2nc call
 *   02 Aug 2007 V.1.2 rebuild, clean & cash from templates in DDLIB
 *   05 Sep 2007 V.1.3 creates empty *times.nc
 *   24 Nov 2007 V.1.4 DDrehash -> TimesUpdate
 *   14 Feb 2008 V.1.5 added Mission & Instrument tags ; and createHeader if neccessary
 *   11 Sep 2012 V.1.6 get dataset info from remote DB to keep it at server side in the only one place
 *-----------------------------------------------------------------------*/

function getCDF($remSetID){

      $client =   new SoapClient(wsdlCDAWEB);
/* Get "VIEW" descriptions, select "istp_public" and set endPoint address*/
      $metaData = $client->__soapCall("getAllViewDescriptions", array());

      for ($k = 0; $k < count($metaData); $k++) {
	if ($metaData[$k]->id != viewID) continue;
	    try {
	      $client->__setLocation($metaData[$k]->endpointAddress);
	      break;          
	    }
	  catch (SoapFault $exception){          
	    error_log($exception.PHP_EOL, 3, err); 	      
	    return false;                     
	    }
	}
      $fileXML = INFODIR.'CDAWEB/'.$remSetID.'.xml';
      if (!file_exists($fileXML)) {
	    error_log('No DDServer XML description '.$remSetID.PHP_EOL, 3, err);
	    return false;
      }
      $XML= new DomDocument("1.0");
      $XML->load($fileXML);

      $Start = $XML->getElementsByTagName('GlobalStart')->item(0)->nodeValue;
      $fileName = strtolower($remSetID)."_00000000_v01.cdf";
      $Stop =  strftime("%Y-%m-%dT%H:%M:%S.000Z",strtotime($Start) + 82800);
      $nFiles = 0;
      $INFO_DIR = INFODIR."../CDAWEB/DATASETS/";

      while ($nFiles == 0){
	    $dataFileDesc = $client->__soapCall("getDataFiles", array($remSetID, $Start, $Stop));
	    $nFiles = count($dataFileDesc);
	    if ($nFiles > 0) {
	      if (!copy($dataFileDesc[0]->name, $INFO_DIR.$fileName)){
		error_log('Cannot copy '.$dataFileDesc[0]->name.PHP_EOL, 3, err);
		return false;
	      }
	    }
	  $Start =  strftime("%Y-%m-%dT%H:%M:%S.000Z",strtotime($Stop) + 82800);
	  $Stop =  strftime("%Y-%m-%dT%H:%M:%S.000Z",strtotime($Start) + 82800);  
      }
      if ($nFiles == 0) {
	      error_log('No data file '.$remSetID.' '.$Start.PHP_EOL, 3, err);
	    return false;
      }

    return true;
}

function getInfoFromRemoteBase($dataBaseID, $remSetID){

    $INFO_DIR = INFODIR."../".$dataBaseID."/DATASETS/";
    if (!is_dir($INFO_DIR)) mkdir($INFO_DIR, 0755, true);

    switch ($dataBaseID) {
      case 'CDAWEB' : 
	      require_once "ftpWrapper.php"; 
	      require_once "cdaweb_cfg.php"; 
 
              $fileName = strtolower($remSetID)."_00000000_v01.cdf";
	      if (file_exists($INFO_DIR.$fileName)) return true;
      	  	 
	// Get master CDF 	 
	      if (!ftpWrapper(ftp_server, ftp_user_name, ftp_user_pass,
					  ftp_dir.$fileName, $INFO_DIR.$fileName)) {
		  error_log('No master CDF for '.$remSetID.PHP_EOL, 3, err);
        // No master CDF - try regular file
                  return getCDF($remSetID);
	      }
							   
	      return true;

	      break;
	case "MAPSKP":

                require_once "mapskp_cfg.php";
            
		$XML_NAME = $remSetID.".xml";
		if (file_exists($INFO_DIR.$XML_NAME)) return true;

		$args = array('service'=>'getInfoUrl', 'datasetId'=>$remSetID);                                  
               
		$url = url.http_build_query($args);
 
		$infoDom = new DOMDocument();
		$infoDom->load($url);
		$File = $infoDom->getElementsByTagName("infoUrl")->item(0)->nodeValue;

		$fileName_ = explode("ftp://",$File);
		$fileName = $fileName_[0].passwd.$fileName_[1];
		 
		if (!copy($fileName,$INFO_DIR.$XML_NAME)) return false;
		return true;

	      break;

	case "VEXGRAZ" :
		  $fileName = "MAG_VSO.xml";
		  if (file_exists($INFO_DIR.$fileName)) return true;
		  return false;
	      break;

	case "CDPP" :

	      break;

	default:
    }
 
    return true;

};

function getInfoFromAmda($dataBaseID, $remSetID)
{

   global $DDsysInfo;

   if (file_exists(BASEDIR.'info.xml')) unlink(BASEDIR.'info.xml');

         $DDinfo = new DomDocument("1.0"); 
         $VI = $DDinfo->createElement("VI"); 
       
          $InfoArr = $DDsysInfo->getElementsByTagName("VI")->item(0)->childNodes;
          for ($i = 0; $i < $InfoArr->length; $i++) 
             if ($InfoArr->item($i)->nodeName != '#text' && $InfoArr->item($i)->nodeName != 'PARAMETER') 
                                           $VI->appendChild($DDinfo->importNode($InfoArr->item($i), true));  
             $VI->appendChild($DDinfo->createElement("LocalStart"));
             $VI->appendChild($DDinfo->createElement("LocalStop"));
             $DDinfo->appendChild($VI);
             $DDinfo->save(BASEDIR.'info.xml');
         
          return true;
}
 
/**
*  @file  createVI.php
*  @brief DD_Server tool <br> Creates New Virtual Instrument
*  @version $Id: createVI.php,v 1.11 2013/09/23 09:02:28 budnik Exp $
*   Creates New VI at DD_Server Side \n
*   Args: dataBaseID  ddDataSetID  remoteDataSetID \n
*   Env Vars:  $DDBASE $DDBASEBIN $DDLIB \n
*   Executables: makeDDsys info2nc TimesUpdate \n
*   Functions:  getInfoFromAmda
*
* @arg Checks if VI already exists in dataBase desctiptor $DDBASE/DDsys.xml
* @arg Adds non-existing VI to DDres.xml 
* @arg $DDBASEBIN/makeDDsys creats refer.nc file from DDsys.xml
* @arg Gets dataSet Info from INFO/DDServer dir
* @arg Creates dir, *_cash.nc, *_info.nc and empty *_times.nc
*
*/
  
 global $DDsysInfo;

 define("verbose", true);


  define("BASEDIR",getenv("DDBASE")."/");
  define("INFODIR",BASEDIR."../INFO/DDServer/");
  define("DDBASEBIN",getenv("DDBASEBIN"));
  define("DDLIB", getenv("DDLIB")."/");

 define ("log",BASEDIR."../INFO/addVI.log");
 define ("err",BASEDIR."../INFO/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);
 }
 
  $dataBaseID = strtoupper($argv[1]);
  $remSetID = $argv[2];
  $VIID = strtolower($argv[3]);  
 


    $MAX_VI_NAME_LENGTH = 16;
    $nc_prefix = strlen($VIID) > $MAX_VI_NAME_LENGTH ? substr($VIID,0,15): $VIID;

    $newDataSet = false;
    $DDsysRefer = BASEDIR."DDsys.xml";
    if (verbose)
	error_log("request to add Data Set ".$dataBaseID.":".$remSetID." as ".$VIID.PHP_EOL, 3, log); 
//  Check if dataSet exists already
     $DDsys = new DomDocument("1.0");
     $DDsys->preserveWhiteSpace = false;
     $DDsys->formatOutput = true;
     $DDsys->load($DDsysRefer);

     $xp = new domxpath($DDsys);
     $dataSet = $xp->query("//NAME[.='".$VIID."']"); 
      
     if ($dataSet->item(0) != NULL)      
          if ($dataSet->item(0)->getAttribute("base") != $dataBaseID) {  
             error_log(" Data Set ".$VIID." exists! Corresponding BaseID is "
            .$dataSet->item(0)->getAttribute("base")." not ".$dataBaseID.PHP_EOL, 3, err);
            
            $newDataSet = true;          
     }
     if ($dataSet->item(0) == NULL) $newDataSet = true;

//-------------- Get Full Dataset Info file from distant database-------------
//TODO error processing here
    $res = getInfoFromRemoteBase($dataBaseID, $remSetID);
    if (!$res) 
	error_log("Error: Can't get dataset info for ".$remSetID." from ".$dataBaseID.PHP_EOL, 3, err);

    if (verbose) error_log("Get dataset info for ".$remSetID." from ".$dataBaseID." returns ".$res.PHP_EOL, 3, log);
    if ($newDataSet) { 
 
//----------- Gets INFO from INFO/DDServer -------------------
	if (verbose) error_log("Ready to Add ".$VIID.PHP_EOL, 3, log); 
        $DDsysInfo = new DomDocument("1.0");
  
//----------- Getting INFO file for DataSet
        $XmlName = INFODIR.$dataBaseID."/".$remSetID.".xml";

        if (file_exists($XmlName)){
                
            $DDsysInfo->load($XmlName); 
            $mission = $DDsysInfo->getElementsByTagName("Mission")->item(0)->nodeValue;
            $instrument = $DDsysInfo->getElementsByTagName("Instrument")->item(0)->nodeValue;

//--------- Check if additional Info should be added to VI

            $theFormat = $DDsysInfo->getElementsByTagName("Format")->item(0);
            if ($theFormat != NULL)
                   $AddInfo = ($theFormat->nodeValue == "Text");
            if (!$AddInfo) 
                   $AddInfo = ($DDsysInfo->getElementsByTagName("AdditionalInfoNeed")->item(0) != NULL);

  //---------------------------------------------------------
           $newVIdir = BASEDIR.$dataBaseID."/".strtoupper($VIID)."/";
// New Data Set - add to DDsys.xml
           $base = $xp->query("/DD_BASE")->item(0);
           $newVI= $DDsys->createElement("VI"); 
           $newName = $DDsys->createElement("NAME",$VIID);
           $newattrBase = new DomAttr('base', $dataBaseID);
           $newattrMission = new DomAttr('mission', $mission);
           $newattrIns = new DomAttr('instrument', $instrument);
           $newName->appendChild($newattrBase);
           $newName->appendChild($newattrMission);
           $newName->appendChild($newattrIns);
            $newVI->appendChild($newName);
            $newVI->appendChild($DDsys->createElement("LOCATION", $newVIdir));
            $newVI->appendChild($DDsys->createElement("TIMES", $VIID."_times.nc"));
            $newVI->appendChild($DDsys->createElement("INFO", $VIID."_info.nc"));
            $newVI->appendChild($DDsys->createElement("CACHE", $VIID."_cash.nc"));
          $base->appendChild($newVI);

           $DDsys->save($DDsysRefer);
//  xml to refer.nc
          exec(DDBASEBIN.'/makeDDsys');
      
//   Now make new dir .....
          if (!is_dir(BASEDIR.$dataBaseID)) mkdir(BASEDIR.$dataBaseID."/", 0755, true);
          if (!is_dir($newVIdir)) mkdir($newVIdir, 0755, true);
           copy (DDLIB."Cash.template", $newVIdir."/".$VIID."_cash.nc");         
           $SED = "sed 's/NAME/".$nc_prefix."/g' ".DDLIB."Clean.template | sed 's/CACHE/".$VIID."/g' > ".$newVIdir."/clean";
           exec($SED);
           exec('chmod ugo+x '.$newVIdir.'/clean');
//   Now info.nc from metadonnees          
           $err = getInfoFromAmda($dataBaseID, $remSetID);
           if ($err) exec(DDBASEBIN.'/info2nc '.$newVIdir.'/'.$VIID.'_info.nc');
// Create empty times file
           chdir($newVIdir);
           exec(DDBASEBIN.'/TimesUpdate -r '.$VIID.'_times.nc '.$VIID.'_[0-9]*.nc');

           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 {
	error_log("Error: ".$VIID." not added: no XML DDServer info file ".PHP_EOL, 3, err); 
      }
 }
?>