getDataMAPSKP.php 3.21 KB
<?php
/**
*  @file  getDataMAPSKP.php 
*  @version $Id: getDataMAPSKP.php,v 1.6 2009/08/26 09:32:56 budnik Exp $
*  @brief DD_Server tools <br> Gets Data from MAPSKP
* 
*   Args:  remDataSetID  ddVIdir StartTime StopTime \n
*   Env Vars:  $DDBASE $DDBASEBIN \n
*   Executables: ascii2nc(DECODERS) TimesUpdate TimesUpdateNoData clean
*
*
*  @todo  General getData.php(dataBaseID)
*/
  function name2DD($fileName) {  
   $pairs = array(".tab" => ".nc","__" => "_");   
  return strtr(strtolower($fileName),$pairs) ;
}
//  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;

//  MAPSKP STUFF
   $passwd = "//amda:dd;mapskp@";   
   $url = "http://planetoweb.cesr.fr/mapskp-amda/MapskpService.php?";

   $args = array('service'=>'getDataUrl',
                 'datasetId'=> $id,                  
                 'dateBegin'=> strtr($Start,".000Z","Z"),
                 'dateEnd'=> strtr($Stop,".000Z","Z"));

// Open STDERR stream
               $STDERR = fopen("php://stderr","w");

//  Lock VI directory for Updating
//  Stamp -> this directory is being updated
    $LockName = $ViDir."/LOCK";
    touch($LockName);
  
fprintf($STDERR,$ViDir." is LOCKED\n");    
    
// Send - Receive query              
    $url  .= http_build_query($args); 
    $fileList = new DOMDocument();
    $fileList->load($url);

    $Files = $fileList->getElementsByTagName("url");

                fprintf($STDERR,  "FILES RETURN ". $Files->length."\n");
    
// Clean VI directory 
    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 ($Files->length  > 0) {            
       for ($i = 0; $i < $Files->length; $i++) {

          copy($ViDir."/header.nc","temp.nc");
          $fileName_ = explode("//",$Files->item($i)->nodeValue);
          $fileName = $fileName_[0].$passwd.$fileName_[1];        
          $fileArr = split("/",$fileName); 
// Copy URL file to DD_Server dir
          $ok =  copy($fileName, $fileArr[count($fileArr)-1]);
          if ($ok)  {
// Convert ascii TAB to nc using header.nc
            system($DDBASEBIN."/ascii2nc ".$fileArr[count($fileArr)-1]); 
                fprintf($STDERR, "FILE $i TRANSLATED  \n");
            unlink($fileArr[count($fileArr)-1]);
            $ncFile = name2DD($fileArr[count($fileArr)-1]);  
// Truncate file if it is too long  
            $ncFileTrue = ($strLength = strlen($ncFile)) > $MAX_NAME_LENGTH ?   
                                            $nc_prefix."_".time().$i.".nc" : $ncFile;
// Move nc file to VIdir                        
             rename("temp.nc", $ViDir."/".$ncFileTrue); 

// Updates VI time info 
             system("cd ".$ViDir."; ".$DDBASEBIN."/TimesUpdate -u ".strtolower($id)."_times.nc ".$ncFileTrue);
          }
       } 
           fprintf($STDERR, "TIMES IS UPDATED \n");
    }

        system("cd ".$ViDir."; ".$DDBASEBIN."/TimesUpdateNoData ".strtolower($id)."_times.nc ".$argv[3]." ".$argv[4]);
// Unlock VI dir
      unlink($LockName);
      fprintf($STDERR, "UNLOCK \n");
?>