makeDDsys.c
6.5 KB
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
46
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*================================================================================
* 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(×name[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);
}
/*================================================================================*/