UpdateInfo.php 3.43 KB
<?php
/** @file    updateInfo.php 
*   @brief   Stand-Alone application Updates Start - Stop Info in  DD Base info files in workng directory
*   @arg     full  prefix of info and times files
*   
*     
*   @version  $Id: UpdateInfo.php 100 2016-04-20 08:26:17Z elena $
*/ 


date_default_timezone_set('UTC');

 function days2MonthDay($oldDate)
 { 
      $year = substr($oldDate, 0, 4);
      $day =  substr($oldDate, 4, 3);
      $hour = substr($oldDate, 7, 2);
      $min =  substr($oldDate, 9, 2);
      $sec =  substr($oldDate, 11, 2);
      $msec = substr($oldDate, 13, 3);
      $newDate = date("Y-m-d",
          strtotime("+$day days",strtotime("$year-01-01")))."T".$hour.":".$min.":".$sec.".".$msec."Z";

  return $newDate;
 } 

 function updateTag($tagName, $tagValue)
 {

    global $dom;

     $vi = $dom->getElementsByTagName("VI");
 
     $Old = $vi->item(0)->getElementsByTagName($tagName)->item(0);
     $New = $dom->createElement($tagName, $tagValue);
     $vi->item(0)->replaceChild($New, $Old); 
 }

/*
*           MAIN 
*/
            if($argc == 2) $infoXml = "$argv[1]_info.xml";
            else exit(" info FULL prefix should be specified");

            global $dom;        
            
            define("DDBASEBIN",getenv("DDBASEBIN")); 
            $dom = new DomDocument();
  
            $path_parts = pathinfo($argv[1]);
            $path = $path_parts['dirname']."/";
            $currDir = getcwd();

            if (!$dom->load($infoXml)) {             
                chdir($path);
                $dom->loadXML(file_get_contents($path_parts['basename']."_info.xml"));  // NFS connection doesn't load 
                chdir($currDir);    
            }
 
// Read *_times.nc          
            $res = system(DDBASEBIN."/StartStopLocal $argv[1]_times.nc");  
            $Time = explode("-",$res);
 // Start   
            $Start = days2MonthDay($Time[0]);
            updateTag('GlobalStart', $Start);
            updateTag('LocalStart', $Start);        
                             
// Stop              
            $Stop = days2MonthDay($Time[1]);
            updateTag('GlobalStop', $Stop);
            updateTag('LocalStop', $Stop);     
                 
            $dom->save($infoXml);
 
// make *_info.nc                      
            system(DDBASEBIN."/infoLocal2nc $infoXml $argv[1]_info.nc"); 
                          
// check if there is TimeRestriction
	  
            if (file_exists($path."TimeRestriction")) {
                    chdir($path); 
                    $restr = file("./TimeRestriction", FILE_IGNORE_NEW_LINES);
   
    //                 $days = $restr[0];
                    $n = sscanf($restr[0], "%d-%d-%d", $yy, $mm, $dd);
 
                    $infoXmlRestr = $restr[1].".xml";
                    $infoNcRestr = $restr[1].".nc";
                 
                    if ($n == 1) {
                        $restrStop = date("Y-m-d", strtotime("-$yy days"))."T23:59:59.999Z";
                    }
                    else {
                        $restrStop = $restr[0]."T23:59:59.999Z";
                    }

                    if ($restrStop <  $Stop) {
                        updateTag('GlobalStop', $restrStop);
                        updateTag('LocalStop', $restrStop); 
                    } 
                
                    $dom->save($infoXmlRestr);
 
                    system(DDBASEBIN."/infoLocal2nc $infoXmlRestr $infoNcRestr");
                    chdir($currDir);
                }    
        
    ?>