getDataCDPP.php 3.9 KB
<?php

/**
*  @file  getDataCDPP.php 
*  @version $Id: getDataCDPP.php,v 1.4 2009/08/26 09:32:56 budnik Exp $
*  @brief DD_Server tools <br> Gets Data from CDPP
* 
*   Args:  remDataSetID  ddVIdir StartTime StopTime \n
*   Env Vars:  $DDBASE $DDBASEBIN \n
*   Executables: TimesUpdate TimesUpdateNoData clean
*
*
*  @todo  General getData.php(dataBaseID)
*/

/*----------------------------------------------------------------------
 *                     DD SERVER TOOLS
 *                   data update collection 
 *                   getDataCDPP.php
 *-----------------------------------------------------------------------*/
 
//  Args
  $id    =  $argv[1];
  $ViDir  = $argv[2];
  $Start =  $argv[3];
  $Stop  =  $argv[4];

 
// Env variables
  $DDBASEBIN = getenv("DDBASEBIN");
  $DDBASE = getenv("DDBASE");
  $MAX_NAME_LENGTH = 31;
  $MAX_VI_NAME_LENGTH = 16;

  $wsdl = "http://cdpp2.cesr.fr:8080/AMDA/services/AMDAServer?WSDL";
// Opent STDERR stream
  $STDERR = fopen("php://stderr","w");

  define("restrictions", $DDBASE."/../INFO/CDPP_restrictions.xml");

   try {
         $client = new SoapClient($wsdl);
       }
    catch (SoapFault $exception) {

fprintf($STDERR,"CDPP WSDL is unreachable \n");
        exit(-1);
    }
    
//  Stamp -> this directory is being updated
          $LockName = $ViDir."/LOCK";
          touch($LockName);
  
fprintf($STDERR,$ViDir." is LOCKED\n");

          system("cd ".$ViDir."; ./clean");
// If name is too long....        
          $nc_prefix = strlen($id) > $MAX_VI_NAME_LENGTH ? substr(strtolower($id),0,15): strtolower($id);


// If restrictions exists...
          $dataset->parameters = null;
          if (file_exists(restrictions)) {
             $restrictionsDom = new DomDocument('1.0');
             $restrictionsDom -> load(restrictions);
 
             $dataSetRestrictions = $restrictionsDom->getElementById($id);
           
             if ($dataSetRestrictions != null) {
               $parametres = $dataSetRestrictions->getElementsByTagName("PARAMETER_ID");
               foreach ($parametres as $parameter) {
                  $param_id = phpversion() >= "5.2.0" ? $parameter->getAttribute("xml:id") : $parameter->getAttribute("id");
                  $dataset->parameters .= strtoupper($param_id)." ";
              }              
            }
          }
          $dataset->datasetID = $id;
          $dataset->timeStart = $Start;
          $dataset->timeStop = $Stop;
          try 
          {   
              $result = $client->__soapCall("getNcdfUrls", array($dataset));    
           }
          catch (SoapFault $exception)
          {    
              unlink($LockName);
              fprintf($STDERR, "EXCEPTION".$exception->faultstring."\n"); 
              exit(-1);  
          }

           $files = $result->getNcdfUrlsReturn;
fprintf($STDERR, "FILES GOT ".count($files)."\n");

         if (count($files) > 0) 
         {
            for ($i = 0; $i < count($files); $i++) {
                 $file =  count($files) == 1 ? $files : $files[$i];
                 $fileArr = split("/",$file);
                 $ncFile = $fileArr[count($fileArr)-1];
                 $ncFileTrue = ($strLength = strlen($ncFile)) > $MAX_NAME_LENGTH ?   
                                         $nc_prefix."_".time().$i.".nc" : $ncFile;
                        
 
                 if (copy($file,$ViDir."/".$ncFileTrue)) { 

                     if (strpos($ViDir, "CP_CIS_CODIF_HS_H1_MOMENTS") !== false) system("nc2nc ".$ViDir."/".$ncFileTrue);

                     system("cd ".$ViDir."; ".$DDBASEBIN."/TimesUpdate -u ".strtolower($id)."_times.nc ".$ncFileTrue);
fprintf($STDERR, "TIMES IS UPDATED ".$i."\n");
                    }
                 else fprintf($STDERR, $file." is not found\n");
              }
           } 
        
       system("cd ".$ViDir."; ".$DDBASEBIN."/TimesUpdateNoData ".strtolower($id)."_times.nc ".$argv[3]." ".$argv[4]);
       unlink($LockName);
fprintf($STDERR, "UNLOCK \n");

?>