createHeaderVEXGRAZ.php 2.08 KB
<?php
/**
*  @file createHeaderVEXGRAZ.php
*  @version $Id: createHeaderVEXGRAZ.php,v 1.1 2009/03/23 14:02:39 budnik Exp $ 
*  @brief Function to create header.nc for MapsKP datasets which are not self-described
*  @todo Exclude absolute path for /usr/local/bin/ncgen
*/


function createHeaderVEXGRAZ($remSetID) {
 
   $infoDom = new DOMDocument();
   $infoDom->load(INFODIR."../AmdaInfo/MAG_VSO.xml");
 
     $sampling =  $infoDom->getElementsByTagName("TIME_RESOLUTION")->item(0)->nodeValue;
     $globalAttrs = ":minSampling = ".$sampling.";";

     $params = $infoDom->getElementsByTagName("PARAM_METADATA");
 
     $Dimensions = "Time = unlimited, TimeLength = 17 ";
     $charVariables = "char Time(Time,TimeLength), StartTime(TimeLength), StopTime(TimeLength);";
     $floatVariables = "float ";

     $Size = array();
     $addDim = true;

     for ($i = 0; $i < $params->length; $i++) {

       $Name = $params->item($i)->getElementsByTagName("PARAM_ID")->item(0)->nodeValue;
       $Size[$i] = $params->item($i)->getElementsByTagName("SIZES")->item(0)->nodeValue;
       $Type = $params->item($i)->getElementsByTagName("DATA_TYPE")->item(0)->nodeValue;

       if ($i > 0) $floatVariables .= ",";

       if ($Size[$i] != "1" ) {
           for ($j = 0; $j < $i+1; $j++)
              if ($Size[$i] == $Size[$j]) {
                  if ($i != 0 && $j != $i) $addDim = false;
                  break;
               }
           if ($addDim) $Dimensions .= ", Dim".$j." = ".$Size[$j];
           $floatVariables .= $Name."(Time, Dim".$j.")"; 
           $addDim = true;
        }
       else 
          $floatVariables .= $Name."(Time)";
//      
     }

// Create CDL ASCII file
    $cdl = fopen("header.cdl","w");
     fwrite($cdl, "netcdf header { \n \n dimensions:  \n");
     fwrite($cdl,$Dimensions."; \n\n");

     fwrite($cdl, "variables:  \n ");
     fwrite($cdl, $charVariables."\n");
     fwrite($cdl, $floatVariables."; \n \n");
     fwrite($cdl, $globalAttrs."\n");
     fwrite($cdl, "data: \n\n }");
    fclose($cdl);
// transform to nc
    system("/usr/local/bin/ncgen -o header.nc header.cdl");
 }
?>