infoLocal2nc.c 4.13 KB
/**
*  @file infoLocal2nc.c
*  @version $Id: infoLocal2nc.c,v 1.4 2012/11/12 09:55:15 budnik Exp $
*  @brief DD Server Tools: Stand alone executable to convert info.xml to info.nc
*  @arg  xml_name nc_name
*
*/
/*================================================================================
 *                       DD
 *                  Server Manager
 *                    infoLocal2nc.c
 *                      V.1.0
 *     args -> XML name and nc name
 *     Modification of info2nc
 *================================================================================*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <math.h>
#include <netcdf.h>
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
#include <libxml/xpath.h>
#include <DD.h>
#include <DD_comm.h>

/*----------- General constants ----------------------------*/
#define MAXVAR       100
#define BASEDIR     "DDBASE"

/*------------- Global variables ---------------------------*/
char temp[MAXVAR][PATHLENGTH];

int main(int argc, char **argv)
{
    char *ncname;
    xmlDocPtr doc;    
    int i = 0, status;               
    xmlNodePtr cur;
    size_t length;

    int fileID;
    int InfoDimID, VarID[MAXVAR];
    double number;
    char DocPath[PATHLENGTH], *BasePathP;
    /*---------- Get Target name from arguments -------------*/
    if (argc < 3) 
    {
       fprintf(stderr,"Usage: infoLocal2nc SourceName TargetName\n");
       exit(0);
    }
 
    /*------- Get full source name --------------------*/
    if((BasePathP = getenv(BASEDIR)) == NULL)
    {
       fprintf(stderr,"infoLocal2nc: no %s environment\n",BASEDIR);
       exit(0);
    }
    strcpy(DocPath,argv[1]);
         
    ncname = argv[2];
    doc = xmlParseFile(DocPath);
        
    if(doc == NULL ) 
    {
       fprintf(stderr,"infoLocal2nc: Source document %s is not parsed successfully.\n",DocPath);
       exit(0);  
    } 

    cur = xmlDocGetRootElement(doc); //GO the first node
    if (cur == NULL) 
    {
        fprintf(stderr,"info2nc: empty Source Document %s\n",DocPath);
        xmlFreeDoc(doc);
        exit(0);
    }

//  Define nc file
    ncopts = NC_VERBOSE;

    if((status = nc_create(ncname,NC_CLOBBER, &fileID))  != NC_NOERR)
    {
       fprintf(stderr,"info2nc: Could not create file %s\n",ncname);
       exit(1);
    }
    status = nc_def_dim(fileID,"info",PATHLENGTH, &InfoDimID);        
    status = nc_enddef(fileID);
    
//  Now treat  info.xml          
    cur = cur->xmlChildrenNode;
    while (cur != NULL)
    {                                                                   
       if (xmlStrcmp(cur->name, (const xmlChar *)"text"))
       { 
          status = nc_redef(fileID);
          if ((!xmlStrcmp(cur->name, (const xmlChar *)"MinSampling")) || 
              (!xmlStrcmp(cur->name, (const xmlChar *)"MaxSampling")) ||
	      (!xmlStrcmp(cur->name, (const xmlChar *)"FillValue")))
 
             status = nc_def_var(fileID, (char *)cur->name, NC_DOUBLE, 0, &InfoDimID, &VarID[i]);
 
          else 
            status = nc_def_var(fileID, (char *)cur->name, NC_CHAR, 1, &InfoDimID, &VarID[i]);
          status = nc_enddef(fileID);

          if ((cur->xmlChildrenNode) != NULL)
          { 
             length = strlen((char *)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1));                      
             strcpy(temp[i], (char *)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1));
             temp[i][length]='\0';
 
             if ((!xmlStrcmp(cur->name, (const xmlChar *)"MinSampling")) ||
                 (!xmlStrcmp(cur->name, (const xmlChar *)"MaxSampling")) || 
		 (!xmlStrcmp(cur->name, (const xmlChar *)"FillValue"))) 
             {
                number = atof(temp[i]);
                status = nc_put_var_double(fileID, VarID[i], &number);
             } else status = nc_put_var_text(fileID,VarID[i],temp[i]);
          }                     
          i++;
                 
      }           
      cur = cur->next; 
   }

   xmlFree(cur);                                    
   xmlFreeDoc(doc);   
   status=nc_close(fileID);
   exit(1);
}
/*==========================================================================================================*/