1 // Purpose: Converts ida3 format xma data to netcdf4
2 // Usage:   xma2netcdf <shot number>
3 
4 
5 #include <iostream>
6 #include <ncFile.h>
7 #include <ncDim.h>
8 #include <ncException.h>
9 #include <iomanip>
10 #include <netcdf>
11 using namespace std;
12 using namespace netCDF;
13 using namespace netCDF::exceptions;
14 
15 
main()16 int main()
17 {
18   try
19     {
20       cout<<"Opening file \"firstFile.cdf\" with NcFile::replace"<<endl;
21       NcFile ncFile("firstFile.cdf", NcFile::replace);
22 
23       cout<<left<<setw(55)<<"Testing addGroup(\"groupName\")";
24       NcGroup groupA(ncFile.addGroup("groupA"));
25       NcGroup groupA0(ncFile.addGroup("groupA0"));
26       NcGroup groupB(groupA.addGroup("groupB"));
27       NcGroup groupC(groupA.addGroup("groupC"));
28       cout <<"    -----------   passed\n";
29 
30       cout <<left<<setw(55)<<"Testing addDim(\"dimensionName\")";
31       NcDim dim1 = ncFile.addDim("dim1",11);
32       NcDim dim2 = ncFile.addDim("dim2");
33       NcDim dim3 = ncFile.addDim("dim3",13);
34       NcDim dim4 = groupB.addDim("dim4",14);
35       NcDim dim5 = groupB.addDim("dim5",15);
36       NcDim dim6 = groupB.addDim("dim6",16);
37       NcDim dim7 = groupB.addDim("dim7",17);
38       cout <<"    -----------   passed\n";
39 
40       cout <<left<<setw(55)<<"Testing NcDim::isUnlimited()";
41       if( dim1.isUnlimited())    throw NcException("Error in test 1.1",__FILE__,__LINE__);
42       if( !dim2.isUnlimited())   throw NcException("Error in test 1.2",__FILE__,__LINE__);
43       if( dim3.isUnlimited())    throw NcException("Error in test 1.3",__FILE__,__LINE__);
44       if( dim4.isUnlimited())    throw NcException("Error in test 1.4",__FILE__,__LINE__);
45       if( dim5.isUnlimited())    throw NcException("Error in test 1.5",__FILE__,__LINE__);
46       if( dim6.isUnlimited())    throw NcException("Error in test 1.6",__FILE__,__LINE__);
47       if( dim7.isUnlimited())    throw NcException("Error in test 1.7",__FILE__,__LINE__);
48       cout <<"    -----------   passed\n";
49 
50      cout <<left<<setw(55)<<"Testing NcDim::isNull()";
51      if( dim1.isNull())    throw NcException("Error in test 2.1",__FILE__,__LINE__);
52      NcDim tmpDim;
53      if( !tmpDim.isNull()) throw NcException("Error in test 2.2",__FILE__,__LINE__);
54       cout <<"    -----------   passed\n";
55 
56      cout <<left<<setw(55)<<"Testing NcDim::getSize()";
57      if( dim1.getSize() != 11)    throw NcException("Error in test 3.1",__FILE__,__LINE__);
58      if( dim2.getSize() != 0 )    throw NcException("Error in test 3.2",__FILE__,__LINE__);
59      if( dim3.getSize() != 13)    throw NcException("Error in test 3.3",__FILE__,__LINE__);
60      if( dim4.getSize() != 14)    throw NcException("Error in test 3.4",__FILE__,__LINE__);
61      if( dim5.getSize() != 15)    throw NcException("Error in test 3.5",__FILE__,__LINE__);
62      if( dim6.getSize() != 16)    throw NcException("Error in test 3.6",__FILE__,__LINE__);
63      if( dim7.getSize() != 17)    throw NcException("Error in test 3.7",__FILE__,__LINE__);
64      cout <<"    -----------   passed\n";
65 
66      cout <<left<<setw(55)<<"Testing NcDim::getParentGroup()";
67      if( !(dim1.getParentGroup() == ncFile))   throw NcException("Error in test 4.1",__FILE__,__LINE__);
68      if( !(dim2.getParentGroup() == ncFile))   throw NcException("Error in test 4.2",__FILE__,__LINE__);
69      if( !(dim3.getParentGroup() == ncFile))   throw NcException("Error in test 4.3",__FILE__,__LINE__);
70      if( !(dim4.getParentGroup() == groupB))   throw NcException("Error in test 4.4",__FILE__,__LINE__);
71      if( !(dim5.getParentGroup() == groupB))   throw NcException("Error in test 4.5",__FILE__,__LINE__);
72      if( !(dim6.getParentGroup() == groupB))   throw NcException("Error in test 4.6",__FILE__,__LINE__);
73      if( !(dim7.getParentGroup() == groupB))   throw NcException("Error in test 4.7",__FILE__,__LINE__);
74      cout <<"    -----------   passed\n";
75 
76      cout <<left<<setw(55)<<"Testing NcDim::getName()";
77      if( dim1.getName() != "dim1")   throw NcException("Error in test 5.1",__FILE__,__LINE__);
78      if( dim2.getName() != "dim2")   throw NcException("Error in test 5.2",__FILE__,__LINE__);
79      if( dim3.getName() != "dim3")   throw NcException("Error in test 5.3",__FILE__,__LINE__);
80      if( dim4.getName() != "dim4")   throw NcException("Error in test 5.4",__FILE__,__LINE__);
81      if( dim5.getName() != "dim5")   throw NcException("Error in test 5.5",__FILE__,__LINE__);
82      if( dim6.getName() != "dim6")   throw NcException("Error in test 5.6",__FILE__,__LINE__);
83      if( dim7.getName() != "dim7")   throw NcException("Error in test 5.7",__FILE__,__LINE__);
84      cout <<"    -----------   passed\n";
85 
86 
87 
88 
89 
90       cout <<left<<setw(55)<<"Testing NcGroup::getDimCount([netCDF::Location])";
91       if( ncFile.getDimCount() != 3)                           throw NcException("Error in test 6.1",__FILE__,__LINE__);
92       if( ncFile.getDimCount(NcGroup::Current) != 3)           throw NcException("Error in test 6.2",__FILE__,__LINE__);
93       if( ncFile.getDimCount(NcGroup::All) != 7)               throw NcException("Error in test 6.3",__FILE__,__LINE__);
94       if( ncFile.getDimCount(NcGroup::Parents) != 0)           throw NcException("Error in test 6.4",__FILE__,__LINE__);
95       if( ncFile.getDimCount(NcGroup::Children) != 4)          throw NcException("Error in test 6.5",__FILE__,__LINE__);
96       if( ncFile.getDimCount(NcGroup::ParentsAndCurrent) != 3) throw NcException("Error in test 6.6",__FILE__,__LINE__);
97       if( ncFile.getDimCount(NcGroup::ChildrenAndCurrent) != 7)throw NcException("Error in test 6.7",__FILE__,__LINE__);
98       if( groupA.getDimCount() != 0)                           throw NcException("Error in test 6.8",__FILE__,__LINE__);
99       if( groupA.getDimCount(NcGroup::Current) != 0)           throw NcException("Error in test 6.9",__FILE__,__LINE__);
100       if( groupA.getDimCount(NcGroup::All) != 7)               throw NcException("Error in test 6.10",__FILE__,__LINE__);
101       if( groupA.getDimCount(NcGroup::Parents) != 3)           throw NcException("Error in test 6.11",__FILE__,__LINE__);
102       if( groupA.getDimCount(NcGroup::Children) != 4)          throw NcException("Error in test 6.12",__FILE__,__LINE__);
103       if( groupA.getDimCount(NcGroup::ParentsAndCurrent) != 3) throw NcException("Error in test 6.13",__FILE__,__LINE__);
104       if( groupA.getDimCount(NcGroup::ChildrenAndCurrent) != 4)throw NcException("Error in test 6.14",__FILE__,__LINE__);
105       cout <<"    -----------   passed\n";
106 
107 
108 
109       cout <<left<<setw(55)<<"Testing NcGroup::getDims([netCDF::Location])";
110       {
111 	multimap<string,NcDim> dimMap;
112 	multimap<string,NcDim>::iterator iter;
113 
114 	// operations on ncFile
115 
116 	dimMap = ncFile.getDims();
117 	if( dimMap.size() != 3)        throw NcException("Error in test 7.1",__FILE__,__LINE__);
118 	iter=dimMap.find("dim1");
119 	if( iter == dimMap.end())    throw NcException("Error in test 7.2",__FILE__,__LINE__);
120 	iter=dimMap.find("dim2");
121 	if( iter == dimMap.end())    throw NcException("Error in test 7.3",__FILE__,__LINE__);
122 	iter=dimMap.find("dim3");
123 	if( iter == dimMap.end())    throw NcException("Error in test 7.4",__FILE__,__LINE__);
124 
125 	dimMap = ncFile.getDims(NcGroup::Current);
126 	if( dimMap.size() != 3)        throw NcException("Error in test 7.5",__FILE__,__LINE__);
127 	iter=dimMap.find("dim1");
128 	if( iter == dimMap.end())    throw NcException("Error in test 7.6",__FILE__,__LINE__);
129 	iter=dimMap.find("dim2");
130 	if( iter == dimMap.end())    throw NcException("Error in test 7.7",__FILE__,__LINE__);
131 	iter=dimMap.find("dim3");
132 	if( iter == dimMap.end())    throw NcException("Error in test 7.8",__FILE__,__LINE__);
133 
134 	dimMap = ncFile.getDims(NcGroup::Parents);
135 	if( dimMap.size() != 0)        throw NcException("Error in test 7.9",__FILE__,__LINE__);
136 
137 	dimMap = ncFile.getDims(NcGroup::Children);
138 	if( dimMap.size() != 4)        throw NcException("Error in test 7.10",__FILE__,__LINE__);
139 
140 	dimMap = ncFile.getDims(NcGroup::ParentsAndCurrent);
141 	if( dimMap.size() != 3)        throw NcException("Error in test 7.11",__FILE__,__LINE__);
142 	iter=dimMap.find("dim1");
143 	if( iter == dimMap.end())    throw NcException("Error in test 7.12",__FILE__,__LINE__);
144 	iter=dimMap.find("dim2");
145 	if( iter == dimMap.end())    throw NcException("Error in test 7.13",__FILE__,__LINE__);
146 	iter=dimMap.find("dim3");
147 	if( iter == dimMap.end())    throw NcException("Error in test 7.14",__FILE__,__LINE__);
148 
149 	dimMap = ncFile.getDims(NcGroup::All);
150 	if( dimMap.size() != 7)        throw NcException("Error in test 7.15",__FILE__,__LINE__);
151 	iter=dimMap.find("dim1");
152 	if( iter == dimMap.end())    throw NcException("Error in test 7.16",__FILE__,__LINE__);
153 	iter=dimMap.find("dim2");
154 	if( iter == dimMap.end())    throw NcException("Error in test 7.17",__FILE__,__LINE__);
155 	iter=dimMap.find("dim3");
156 	if( iter == dimMap.end())    throw NcException("Error in test 7.18",__FILE__,__LINE__);
157 	iter=dimMap.find("dim4");
158 	if( iter == dimMap.end())    throw NcException("Error in test 7.19",__FILE__,__LINE__);
159 	iter=dimMap.find("dim5");
160 	if( iter == dimMap.end())    throw NcException("Error in test 7.20",__FILE__,__LINE__);
161 	iter=dimMap.find("dim6");
162 	if( iter == dimMap.end())    throw NcException("Error in test 7.21",__FILE__,__LINE__);
163 	iter=dimMap.find("dim7");
164 	if( iter == dimMap.end())    throw NcException("Error in test 7.22",__FILE__,__LINE__);
165 
166       }
167       cout <<"    -----------   passed\n";
168 
169 
170 
171       cout <<left<<setw(55)<<"Testing NcGroup::getDims(\"name\",[netCDF::Location])";
172       {
173 
174 	set<NcDim> dimSet;
175 	set<NcDim>::iterator iter;
176 
177 	// operations on ncFile:dim2
178 
179 	dimSet = ncFile.getDims("dim2");
180 	if( dimSet.size() != 1)       throw NcException("Error in test 8.1",__FILE__,__LINE__);
181 	iter=dimSet.find(dim2);
182 	if( iter == dimSet.end())     throw NcException("Error in test 8.2",__FILE__,__LINE__);
183 	if( iter->getName() != "dim2")throw NcException("Error in test 8.3",__FILE__,__LINE__);
184 
185 	dimSet = ncFile.getDims("dim2",NcGroup::Current);
186 	if( dimSet.count(dim2) != 1) throw NcException("Error in test 8.4",__FILE__,__LINE__);
187 
188 	dimSet = ncFile.getDims("dim2",NcGroup::Parents);
189 	if( dimSet.count(dim2) != 0) throw NcException("Error in test 8.5",__FILE__,__LINE__);
190 
191 	dimSet = ncFile.getDims("dim2",NcGroup::Children);
192 	if( dimSet.count(dim2) != 0) throw NcException("Error in test 8.6",__FILE__,__LINE__);
193 
194 	dimSet = ncFile.getDims("dim2",NcGroup::ParentsAndCurrent);
195 	if( dimSet.count(dim2) != 1) throw NcException("Error in test 8.7",__FILE__,__LINE__);
196 
197 	dimSet = ncFile.getDims("dim2",NcGroup::ChildrenAndCurrent);
198 	if( dimSet.count(dim2) != 1) throw NcException("Error in test 8.8",__FILE__,__LINE__);
199 
200 	dimSet = ncFile.getDims("dim2",NcGroup::All);
201 	if( dimSet.count(dim2) != 1) throw NcException("Error in test 8.9",__FILE__,__LINE__);
202 
203 
204 	// operations on ncFile:dim6
205 
206 	dimSet = ncFile.getDims("dim6");
207 	if( dimSet.size() != 0)       throw NcException("Error in test 8.10",__FILE__,__LINE__);
208 
209 	dimSet = ncFile.getDims("dim6",NcGroup::Current);
210 	if( dimSet.count(dim6) != 0) throw NcException("Error in test 8.11",__FILE__,__LINE__);
211 
212 	dimSet = ncFile.getDims("dim6",NcGroup::Parents);
213 	if( dimSet.count(dim6) != 0) throw NcException("Error in test 8.12",__FILE__,__LINE__);
214 
215 	dimSet = ncFile.getDims("dim6",NcGroup::Children);
216 	if( dimSet.count(dim6) != 1) throw NcException("Error in test 8.13",__FILE__,__LINE__);
217 
218 	dimSet = ncFile.getDims("dim6",NcGroup::ParentsAndCurrent);
219 	if( dimSet.count(dim6) != 0) throw NcException("Error in test 8.14",__FILE__,__LINE__);
220 
221 	dimSet = ncFile.getDims("dim6",NcGroup::ChildrenAndCurrent);
222 	if( dimSet.count(dim6) != 1) throw NcException("Error in test 8.15",__FILE__,__LINE__);
223 
224 	dimSet = ncFile.getDims("dim6",NcGroup::All);
225 	if( dimSet.count(dim6) != 1) throw NcException("Error in test 8.16",__FILE__,__LINE__);
226 
227 
228 	// operations on groupB:dim1
229 
230 	dimSet = groupB.getDims("dim1");
231 	if( dimSet.size() != 0)       throw NcException("Error in test 8.17",__FILE__,__LINE__);
232 
233 	dimSet = groupB.getDims("dim1",NcGroup::Current);
234 	if( dimSet.count(dim1) != 0) throw NcException("Error in test 8.18",__FILE__,__LINE__);
235 
236 	dimSet = groupB.getDims("dim1",NcGroup::Parents);
237 	if( dimSet.count(dim1) != 1) throw NcException("Error in test 8.19",__FILE__,__LINE__);
238 
239 	dimSet = groupB.getDims("dim1",NcGroup::Children);
240 	if( dimSet.count(dim1) != 0) throw NcException("Error in test 8.20",__FILE__,__LINE__);
241 
242 	dimSet = groupB.getDims("dim1",NcGroup::ParentsAndCurrent);
243 	if( dimSet.count(dim1) != 1) throw NcException("Error in test 8.21",__FILE__,__LINE__);
244 
245 	dimSet = groupB.getDims("dim1",NcGroup::ChildrenAndCurrent);
246 	if( dimSet.count(dim1) != 0) throw NcException("Error in test 8.22",__FILE__,__LINE__);
247 
248 	dimSet = groupB.getDims("dim1",NcGroup::All);
249 	if( dimSet.count(dim1) != 1) throw NcException("Error in test 8.23",__FILE__,__LINE__);
250 
251 
252 	// operations on groupA:dim1
253 
254 	dimSet = groupA.getDims("dim1");
255 	if( dimSet.size() != 0)       throw NcException("Error in test 8.24",__FILE__,__LINE__);
256 
257 	dimSet = groupA.getDims("dim1",NcGroup::Current);
258 	if( dimSet.count(dim1) != 0) throw NcException("Error in test 8.25",__FILE__,__LINE__);
259 
260 	dimSet = groupA.getDims("dim1",NcGroup::Parents);
261 	if( dimSet.count(dim1) != 1) throw NcException("Error in test 8.26",__FILE__,__LINE__);
262 
263 	dimSet = groupA.getDims("dim1",NcGroup::Children);
264 	if( dimSet.count(dim1) != 0) throw NcException("Error in test 8.27",__FILE__,__LINE__);
265 
266 	dimSet = groupA.getDims("dim1",NcGroup::ParentsAndCurrent);
267 	if( dimSet.count(dim1) != 1) throw NcException("Error in test 8.28",__FILE__,__LINE__);
268 
269 	dimSet = groupA.getDims("dim1",NcGroup::ChildrenAndCurrent);
270 	if( dimSet.count(dim1) != 0) throw NcException("Error in test 8.29",__FILE__,__LINE__);
271 
272 	dimSet = groupA.getDims("dim1",NcGroup::All);
273 	if( dimSet.count(dim1) != 1) throw NcException("Error in test 8.30",__FILE__,__LINE__);
274 
275       }
276       cout <<"    -----------   passed\n";
277 
278 
279 
280 
281 
282       cout <<left<<setw(55)<<"Testing NcGroup::getDim(\"name\",[netCDF::Location])";
283       {
284 	if( ncFile.getDim("dim1",NcGroup::All).getName() !="dim1") throw NcException("Error in test 9.1",__FILE__,__LINE__);
285 	if( ncFile.getDim("dim2",NcGroup::All).getName() !="dim2") throw NcException("Error in test 9.2",__FILE__,__LINE__);
286 	if( ncFile.getDim("dim3",NcGroup::All).getName() !="dim3") throw NcException("Error in test 9.3",__FILE__,__LINE__);
287 	if( ncFile.getDim("dim4",NcGroup::All).getName() !="dim4") throw NcException("Error in test 9.4",__FILE__,__LINE__);
288 	if( ncFile.getDim("dim5",NcGroup::All).getName() !="dim5") throw NcException("Error in test 9.5",__FILE__,__LINE__);
289 	if( ncFile.getDim("dim6",NcGroup::All).getName() !="dim6") throw NcException("Error in test 9.6",__FILE__,__LINE__);
290 	if( ncFile.getDim("dim7",NcGroup::All).getName() !="dim7") throw NcException("Error in test 9.7",__FILE__,__LINE__);
291 	if( groupB.getDim("dim1",NcGroup::All).getName() !="dim1") throw NcException("Error in test 9.8",__FILE__,__LINE__);
292 	if( groupB.getDim("dim2",NcGroup::All).getName() !="dim2") throw NcException("Error in test 9.9",__FILE__,__LINE__);
293 	if( groupB.getDim("dim3",NcGroup::All).getName() !="dim3") throw NcException("Error in test 9.10",__FILE__,__LINE__);
294 	if( groupB.getDim("dim4",NcGroup::All).getName() !="dim4") throw NcException("Error in test 9.11",__FILE__,__LINE__);
295 	if( groupB.getDim("dim5",NcGroup::All).getName() !="dim5") throw NcException("Error in test 9.12",__FILE__,__LINE__);
296 	if( groupB.getDim("dim6",NcGroup::All).getName() !="dim6") throw NcException("Error in test 9.13",__FILE__,__LINE__);
297 	if( groupB.getDim("dim7",NcGroup::All).getName() !="dim7") throw NcException("Error in test 9.14",__FILE__,__LINE__);
298 	if( !ncFile.getDim("dim7").isNull())                            throw NcException("Error in test 9.15",__FILE__,__LINE__);
299 	if( !ncFile.getDim("dim7",NcGroup::Current).isNull())           throw NcException("Error in test 9.16",__FILE__,__LINE__);
300 	if( !ncFile.getDim("dim7",NcGroup::Parents).isNull())           throw NcException("Error in test 9.17",__FILE__,__LINE__);
301 	if(  ncFile.getDim("dim7",NcGroup::Children).isNull())          throw NcException("Error in test 9.18",__FILE__,__LINE__);
302 	if( !ncFile.getDim("dim7",NcGroup::ParentsAndCurrent).isNull()) throw NcException("Error in test 9.19",__FILE__,__LINE__);
303 	if(  ncFile.getDim("dim7",NcGroup::ChildrenAndCurrent).isNull())throw NcException("Error in test 9.20",__FILE__,__LINE__);
304 
305 	if( !groupA.getDim("dim7").isNull())                            throw NcException("Error in test 9.21",__FILE__,__LINE__);
306 	if( !groupA.getDim("dim7",NcGroup::Current).isNull())           throw NcException("Error in test 9.22",__FILE__,__LINE__);
307 	if( !groupA.getDim("dim7",NcGroup::Parents).isNull())           throw NcException("Error in test 9.23",__FILE__,__LINE__);
308 	if(  groupA.getDim("dim7",NcGroup::Children).isNull())          throw NcException("Error in test 9.24",__FILE__,__LINE__);
309 	if( !groupA.getDim("dim7",NcGroup::ParentsAndCurrent).isNull()) throw NcException("Error in test 9.25",__FILE__,__LINE__);
310 	if(  groupA.getDim("dim7",NcGroup::ChildrenAndCurrent).isNull())throw NcException("Error in test 9.26",__FILE__,__LINE__);
311 	if(  groupA.getDim("dim7",NcGroup::All).isNull())               throw NcException("Error in test 9.27",__FILE__,__LINE__);
312 
313 	if(  groupB.getDim("dim7").isNull())                            throw NcException("Error in test 9.28",__FILE__,__LINE__);
314 	if(  groupB.getDim("dim7",NcGroup::Current).isNull())           throw NcException("Error in test 9.29",__FILE__,__LINE__);
315 	if( !groupB.getDim("dim7",NcGroup::Parents).isNull())           throw NcException("Error in test 9.30",__FILE__,__LINE__);
316 	if( !groupB.getDim("dim7",NcGroup::Children).isNull())          throw NcException("Error in test 9.31",__FILE__,__LINE__);
317 	if(  groupB.getDim("dim7",NcGroup::ParentsAndCurrent).isNull()) throw NcException("Error in test 9.32",__FILE__,__LINE__);
318 	if(  groupB.getDim("dim7",NcGroup::ChildrenAndCurrent).isNull())throw NcException("Error in test 9.33",__FILE__,__LINE__);
319 	if(  groupB.getDim("dim7",NcGroup::All).isNull())               throw NcException("Error in test 9.34",__FILE__,__LINE__);
320 	if( !ncFile.getDim("dimX",NcGroup::All).isNull())               throw NcException("Error in test 9.35",__FILE__,__LINE__);
321       }
322 
323       cout <<"    -----------   passed\n";
324 
325 
326     }
327   catch (NcException& e)
328     {
329       cout <<"\n";
330       e.what();
331       return e.errorCode();
332     }
333 }
334