Blame view

src/DATA/TOOLS/infoLocal2nc.c 4.55 KB
531fafe7   Benjamin Renard   Install AddLocalV...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/**
*  @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;
023f1dcc   Benjamin Renard   Add NoCompression...
46
    short  numberShort;
531fafe7   Benjamin Renard   Install AddLocalV...
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
    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]);
 
023f1dcc   Benjamin Renard   Add NoCompression...
104
105
106
          else if (!xmlStrcmp(cur->name, (const xmlChar *)"NoCompression"))
             status = nc_def_var(fileID, (char *)cur->name, NC_SHORT, 0, &InfoDimID, &VarID[i]);
          else
531fafe7   Benjamin Renard   Install AddLocalV...
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
            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);
023f1dcc   Benjamin Renard   Add NoCompression...
122
123
124
125
126
127
             }
             else if (!xmlStrcmp(cur->name, (const xmlChar *)"NoCompression")) {
               numberShort = (atoi(temp[i]) == 1);
               status = nc_put_var_short(fileID, VarID[i], &numberShort);
             }
             else status = nc_put_var_text(fileID,VarID[i],temp[i]);
531fafe7   Benjamin Renard   Install AddLocalV...
128
129
130
131
132
133
134
135
136
137
138
139
140
          }                     
          i++;
                 
      }           
      cur = cur->next; 
   }

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