getDataCDAWEB.php 3.7 KB
<?php
/**
*  @file  getDataCDAWEB.php 
*  @version $Id: getDataCDAWEB.php,v 1.5 2009/08/26 09:32:40 budnik Exp $
*  @brief DD_Server tools <br> Gets Data from CDAWEB
* 
*   Args:  remDataSetID  ddVIdir StartTime StopTime \n
*   Env Vars:  $DDBASE $DDBASEBIN \n
*   Executables: cdf2nc(DECODERS) TimesUpdate clean
*
*
*  @todo  General getData.php(dataBaseID)
*/

/*----------------------------------------------------------------------
 *                     DD SERVER TOOLS
 *                   data update collection 
 *                   getDataCDAWEB.php
 *  php getDataCDAWEB.php <remDataSetID> <ddVIdir> <StartTime> <StopTime>
 *                  17 June 2007 V.1.0
 *                   02 Aug 2007 V.1.1
 *                   06 Aug 2007 V.1.2, cd to Data directory
 *                   23 Aug 2007 V.1.3, Another program for VI times update
 *                   23 Aug 2007 V.1.4, ZIP files before updating
 *                   23 Nov 2007 V.1.5 Copy LOCK file
 *                   21 jan 2008 V.1.6  Process all SOAP exceptions
 *                                      cut file names longer than 32
 *-----------------------------------------------------------------------*/
 
//  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://cdaweb.gsfc.nasa.gov/WS/istp_public/jaxrpc?WSDL";
// Opent STDERR stream
  $STDERR = fopen("php://stderr","w");

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

fprintf($STDERR,"CDAWEB WSDL is unreachable \n");
        exit(0);
    }
    
//  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);

          try 
          {   
               $dataFileDesc = $client->__soapCall("getDataFiles", array($id, $Start, $Stop));       
fprintf($STDERR, "FILES GOT ".count($dataFileDesc)."\n");
           }
          catch (SoapFault $exception)
          {    
              unlink($LockName);
              fprintf($STDERR, $exception."\n"); 
              exit(0);  
          }
        if (count($dataFileDesc) > 0) 
         {
            for ($i = 0; $i < count($dataFileDesc); $i++) {
                 $file = $dataFileDesc[$i]->name;
                 $fileArr = split("/",$file);
//       fprintf($STDERR, $fileArr[count($fileArr)-1]."\n"); 
                 if (copy($file, $fileArr[count($fileArr)-1])) {
                       system($DDBASEBIN."/cdf2nc ".$fileArr[count($fileArr)-1]); 
fprintf($STDERR, "FILES TRANSLATED ".$i."\n");
                       $ncFile = str_replace(".cdf", ".nc", $fileArr[count($fileArr)-1]);
                       unlink($fileArr[count($fileArr)-1]);
 
                       $ncFileTrue = ($strLength = strlen($ncFile)) > $MAX_NAME_LENGTH ?   
                                               $nc_prefix."_".time().$i.".nc" : $ncFile;
                        
                       rename($ncFile,$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");


?>