makeDDsys.c 6.5 KB
/*================================================================================
 *                       DD
 *                Server Package
 *                  makeDDsys.c
 *                     V.4.2
 *  Stand-alone executable to create refer.nc file form DDsys.xml file
 *  Program search the DDsys.xml in the directory $DDBASE, and creates
 *  refer.nc in this directory. See structure of DDsys.xml in 
 * ../DOC/TECHDOC/DDBaseStruc.html
 * 
 * Versions:
 *     Apr 23 1997, V.2.0 Adding CASH into refer.nc file 
 *     Jun 18 2007, V.3.0 Length 128
 *     1 Aug 2007,  V.4.0 XML version
 *     6 Sep 2007,  V.4.1 Knizhnikova-Fedorova, small changes
 *     9 Nov 2009,  V.4.2 netCDF 3
 *================================================================================*/

#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 MAXVIRIN 1000
#define SOURCENAME  "/DDsys.xml"
#define BASEDIR     "DDBASE"

/*----------- Global variables ------------------------------*/
char varname[MAXVIRIN][PATHLENGTH];
char datadirname[MAXVIRIN][PATHLENGTH];
char timesname[MAXVIRIN][PATHLENGTH];
char infoname[MAXVIRIN][PATHLENGTH];
char cashname[MAXVIRIN][PATHLENGTH];

int basenumber;           /* number of virtual instruments */

/*---------------------- REFER CREATE ------------------------*/
void ReferCreate(char *BasePathP)
{
   int fileID;
   int InfoDimID, LengthDimID;
   int VarDimVector[2];
   int VarID[MAXVIRIN];
   size_t DirStart[2] = {0L,0L};
   size_t TimesStart[2] = {1L,0L};
   size_t InfoStart[2] = {2L,0L};
   size_t CashStart[2] = {3L,0L};
   size_t VarCount[2] = {1L,PATHLENGTH};
   int i, status;
   char DestPath[PATHLENGTH];

  // ncopts = NC_VERBOSE;
   
   /*
    * Full path name
    */
   
    strcpy(DestPath,BasePathP);
    strcat(DestPath,REFNAME);

   if((status =  nc_create( DestPath, NC_CLOBBER, &fileID)) != NC_NOERR)
   {
     fprintf(stderr,"makeDDsys: Could not create file %s\n",DestPath);
     exit(1);
   }

   nc_def_dim(fileID, "info", 4L, &InfoDimID);
   nc_def_dim(fileID, "length", PATHLENGTH, &LengthDimID);
  
   VarDimVector[0] = InfoDimID;
   VarDimVector[1] = LengthDimID;

   for(i = 0; i < basenumber; i++)
        nc_def_var(fileID, varname[i], NC_CHAR, 2, VarDimVector, &VarID[i]);

   nc_enddef(fileID);

   for(i = 0; i < basenumber; i++)
   {       
       nc_put_vara_text(fileID,VarID[i],DirStart,VarCount,(void *)datadirname[i]);
       nc_put_vara_text(fileID,VarID[i],TimesStart,VarCount,(void *)timesname[i]);
       nc_put_vara_text(fileID,VarID[i],InfoStart,VarCount,(void *)infoname[i]);
       nc_put_vara_text(fileID,VarID[i],CashStart,VarCount,(void *)cashname[i]);
      
   }
   status = nc_close(fileID);
}
/*--------------------------------------------------------------------------*/
 
 int main(int argc, char **argv)
{
    
    xmlDocPtr doc;
    xmlChar *xpath = (xmlChar*) "//VI";    
    xmlNodeSetPtr nodeset;
    xmlXPathObjectPtr result;
    int i, j, size = 2, length;
    xmlChar *name, *argsNr;            
    xmlXPathContextPtr context;
    xmlNodePtr cur;
    char BasePath[PATHLENGTH], *BasePathP;

    /*
     * Create full Source Name 
     */
    if((BasePathP = getenv(BASEDIR)) == NULL)
    {
       fprintf(stderr,"makeDDsys: no %s environment\n",BASEDIR);
       exit(0);
    }
    strcpy(BasePath,BasePathP);
    strcat(BasePath,SOURCENAME);
    
    doc = xmlParseFile(BasePath);
    if (doc == NULL ) 
    {
       fprintf(stderr,"Document not parsed successfully. \n");
       exit(0);
    } 

    context = xmlXPathNewContext(doc);
    if (context == NULL) 
    {
        fprintf(stderr,"Error in xmlXPathNewContext\n");
        exit(0);
    }
    result = xmlXPathEvalExpression(xpath, context);
  
    if (result == NULL) 
    {
        fprintf(stderr,"Error in xmlXPathEvalExpression\n");
        exit(0);
    }

    if(xmlXPathNodeSetIsEmpty(result->nodesetval))
    {
       xmlXPathFreeObject(result);
       fprintf(stderr,"No result\n");
       exit(0);
    }
    if (result) 
    {
       nodeset = result->nodesetval;
       fprintf(stderr," VI number: %d\n", nodeset->nodeNr);
       basenumber = nodeset->nodeNr;
       for (i = 0; i < nodeset->nodeNr; i++) 
       { 
          cur = nodeset->nodeTab[i]->xmlChildrenNode;  
          while (cur != NULL)
          { 
             if((xmlStrcmp(cur->name, (const xmlChar *)"text"))){
             length = strlen((char *)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1));
 
             }
              if (!(xmlStrcmp(cur->name, (const xmlChar *)"NAME")))
              { 
                  
                           strncpy(varname[i], (char *)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1), length);              
              }    
              if (!(xmlStrcmp(cur->name, (const xmlChar *)"LOCATION")))
              { 
                 strncpy(datadirname[i], (char *)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1), length);                  
              }   
              if (!(xmlStrcmp(cur->name, (const xmlChar *)"TIMES")))
              {                    
                 strncpy(timesname[i],datadirname[i], strlen(datadirname[i]));
                 strncpy(&timesname[i][strlen(datadirname[i])], (char *)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1), length);
             
              }   
              if (!(xmlStrcmp(cur->name, (const xmlChar *)"INFO")))
              {                 
                  strncpy(infoname[i],datadirname[i], strlen(datadirname[i]));
                  strncpy(&infoname[i][strlen(datadirname[i])], (char *)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1), length);
              }   
              if (!(xmlStrcmp(cur->name, (const xmlChar *)"CACHE")))
              {                  
                 strncpy(cashname[i],datadirname[i], strlen(datadirname[i]));
                 strncpy(&cashname[i][strlen(datadirname[i])], (char *)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1), length);
              }  
                                                                      
              cur = cur->next;              
           }                          
       }
       xmlFree(cur);
       xmlXPathFreeObject(result);    
   }
           
   xmlXPathFreeContext(context);   
   xmlFreeDoc(doc);
   xmlCleanupParser();

   ReferCreate(BasePathP);
   return (1);
}
/*================================================================================*/