createHeaderMAPSKP.php 2.9 KB
<?php
/* $Id: createHeaderMAPSKP.php,v 1.5 2008/03/05 14:03:45 elena Exp $ */
/**
*  \file createHeaderMAPSKP.php
*  \brief Function to create header.nc for MapsKP datasets which are not self-described
*  \todo Exclude absolute path for /usr/local/bin/ncgen
*/

function createHeaderMAPSKP($remSetID) {
  
   $url = "http://planetoweb.cesr.fr/mapskp-amda/MapskpService.php?";
   $passwd = "ftp://amda:dd;mapskp@";
  
   $args = array('service'=>'getInfoUrl',
                'datasetId'=>$remSetID);                                  
               
   $url  .= http_build_query($args);
   $infoDom = new DOMDocument();
   $infoDom->load($url);
   $File = $infoDom->getElementsByTagName("infoUrl")->item(0)->nodeValue;

   $fileName_ = explode("ftp://",$File);
   $fileName = $fileName_[0].$passwd.$fileName_[1];

   $infoDom->load($fileName);

     $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/bin/ncgen -o header.nc header.cdl");
//  make constant Energy Tables in info.nc
// For MIMI also ...    
    if ($remSetID == "RPWS_KEY") {
        $StartTime =  $infoDom->getElementsByTagName("START_DATE")->item(0)->nodeValue;
        $function = "make".$remSetID."info";
        include($function.".php");
        if ($OK = $function($StartTime)) system("join_nc ".strtolower($remSetID)."_constant.nc ".strtolower($remSetID)."_info.nc");
    }
 }
?>