Initial snark14m import
[snark14.git] / src / DIGFileSnark / DIGFileSnarkRec.cpp
1 #include <cstdio>
2 #include <string>
3
4 //#include "DIGFileSnarkRec.h"
5 #include <DIGFileSnark/DIGFileSnarkRec.h>
6 \r
7 #define DIGFILESNARKREC_DEBUG_LEVEL 0
8
9 const char* DIGFileSnarkRec::TypeStr = "SNARK05 recfil";
10 const char* DIGFileSnarkRec::SchemaStr = "DIGFileSnarkRec.xsd";
11
12 DIGFileSnarkRec::DIGFileSnarkRec()
13 {
14 }       
15
16
17 DIGFileSnarkRec::~DIGFileSnarkRec()
18 {
19 }
20
21 ///////////////////////////////////////////////////////////////////////////////
22 ///////////////////////////////////////////////////////////////////////////////
23 // Writing
24 ///////////////////////////////////////////////////////////////////////////////
25 ///////////////////////////////////////////////////////////////////////////////
26
27 ///////////////////////////////////////////////////////////////////////////////
28 // Open for writing
29 ///////////////////////////////////////////////////////////////////////////////
30
31 int DIGFileSnarkRec::Open(const char* pFileName, RecFileMH* pMainHeader)
32 {\r
33 #if DIGFILESNARKREC_DEBUG_LEVEL > 3\r
34   fprintf(stderr, "DIGFileSnarkRec::Open\n");\r
35 #endif\r
36
37   return Open(
38     pFileName,                  // file name
39     pMainHeader->Title.Get(),   // title
40     pMainHeader->Dimensions,    // dimensions
41     pMainHeader->Sampling,      // sampling
42     pMainHeader->Comment.Get()  // comment 
43   );
44 }
45
46 int DIGFileSnarkRec::Open(
47   const char*   pFileName,     // File Name
48   const char*   pProjName,     // Projection Name
49   unsigned int  pNoOfElements, // NELEM
50   double        pSampling,     // PIXSIZ
51   const char*   pComments      // Comments
52
53 {\r
54 #if DIGFILESNARKREC_DEBUG_LEVEL > 3\r
55   fprintf(stderr, "DIGFileSnarkRec::Open\n");\r
56 #endif\r
57
58   PhantomPresent = false;
59   RecPressent = false;
60
61   DIGDimensions  dim;
62   DIGSampling    sampX;
63   DIGSampling    sampY;
64   DIGSampling    sampZ = { 0.0, 0.0, 0.0 };
65
66   dim.x = pNoOfElements;
67   dim.y = pNoOfElements;
68   dim.z = 1;
69
70   sampX.x = pSampling;
71   sampX.y = 0.0;
72   sampX.z = 0.0;
73
74   sampY.x = 0.0;
75   sampY.y = pSampling;
76   sampY.z = 0.0;
77
78   return DIGFile::Open(
79     pFileName,            // File Name
80     SchemaStr,            // Schema
81     pProjName,            // Title
82     TypeStr,              // Type,
83     1,                    // Chanels
84     DIGValueType_REAL,    // ValueType
85     DIGDataType_DOUBLE,   // DataType
86     DIGDataFormat_BINARY, // DataFormat
87     DIGGrid_SC,           // Grid
88     DIGBasis_VORONOI,     // Basis
89     DIGUnit_UNSPECIFIED,  // Unit
90     &dim,                 // Dimensions
91     &sampX,               // SamplingX
92     &sampY,               // SamplingY
93     &sampZ,               // SamplingZ
94     pComments,            // Comments
95     ""                    // Other Unit
96   );
97 };
98
99 ///////////////////////////////////////////////////////////////////////////////
100 // Updating main header after opening file to write
101 ///////////////////////////////////////////////////////////////////////////////
102
103 ///////////////////////////////////////////////////////////////////////////////
104 // Appending phantom
105 ///////////////////////////////////////////////////////////////////////////////
106
107 int DIGFileSnarkRec::AppendPhanom(RecFilePhanomH* pPhanomHeader, double* pData)
108 {\r
109 #if DIGFILESNARKREC_DEBUG_LEVEL > 3\r
110   fprintf(stderr, "DIGFileSnarkRec::AppendPhanom\n");\r
111 #endif\r
112
113   return AppendPhantom(pPhanomHeader->Name.Get(), pPhanomHeader->Comment.Get(), pData);
114 }
115
116 int DIGFileSnarkRec::AppendPhantom(const char* pPhanomName, const char* pComment, double* pData)
117 {\r
118 #if DIGFILESNARKREC_DEBUG_LEVEL > 3\r
119   fprintf(stderr, "DIGFileSnarkRec::AppendPhanom\n");\r
120 #endif\r
121
122         // writes a phantom array into the file
123         // should only be used as first array set
124
125   if(RecPressent) {\r
126 \r
127 #if DIGFILESNARKREC_DEBUG_LEVEL > 0\r
128   printf("** Error: Reconstructions already in the file\n");\r
129 #endif
130     return -1; // reconstructions already in the file
131   }
132
133   if(PhantomPresent) {\r
134 #if DIGFILESNARKREC_DEBUG_LEVEL > 0\r
135   printf("** Error: Phantom already in the file\n");\r
136 #endif
137     return -1; // phantom already in the file
138   }
139
140   if(AppendArraySet("PHAN", pPhanomName, "", pComment) != 0) {\r
141                 return 1;
142   }
143
144   if(AppendArray(0, "", pData) != 0) {
145                 return 2;
146   }
147
148   PhantomPresent = true;
149
150         return 0;
151 }
152
153 ///////////////////////////////////////////////////////////////////////////////
154 // Appending reconstruction set
155 ///////////////////////////////////////////////////////////////////////////////
156
157 int DIGFileSnarkRec::AppendRecSet(RecFileRecSetH* pRecSetHeader)
158 {\r
159 #if DIGFILESNARKREC_DEBUG_LEVEL > 3\r
160   fprintf(stderr, "DIGFileSnarkRec::AppendRecSet\n");\r
161 #endif\r
162
163   return AppendRecSet(pRecSetHeader->AlgName.Get(), pRecSetHeader->Name.Get(), pRecSetHeader->Comment.Get());
164 }
165
166 int DIGFileSnarkRec::AppendRecSet(const char* pAlgName, const char* pName, const char* pComment)
167 {\r
168 #if DIGFILESNARKREC_DEBUG_LEVEL > 3\r
169   fprintf(stderr, "DIGFileSnarkRec::AppendRecSet\n");\r
170 #endif\r
171
172   return AppendArraySet(pAlgName, pName, "", pComment);
173 }
174
175 ///////////////////////////////////////////////////////////////////////////////
176 // Appending reconstruction
177 ///////////////////////////////////////////////////////////////////////////////
178
179 int DIGFileSnarkRec::AppendRec(RecFileRecH* pRecHeader, double* pData)
180 {\r
181 #if DIGFILESNARKREC_DEBUG_LEVEL > 3\r
182   fprintf(stderr, "DIGFileSnarkRec::AppendRec\n");\r
183 #endif\r
184
185   return AppendRec(pRecHeader->Count, pRecHeader->Comment.Get(), pData);
186 }
187
188 int DIGFileSnarkRec::AppendRec(unsigned int pCount, const char* pComment, double* pData)
189 {\r
190 #if DIGFILESNARKREC_DEBUG_LEVEL > 3\r
191   fprintf(stderr, "DIGFileSnarkRec::AppendRec\n");\r
192 #endif\r
193
194   return AppendArray(pCount, pComment, pData);
195 }
196
197 ///////////////////////////////////////////////////////////////////////////////
198 ///////////////////////////////////////////////////////////////////////////////
199 // Reading
200 ///////////////////////////////////////////////////////////////////////////////
201 ///////////////////////////////////////////////////////////////////////////////
202
203 ///////////////////////////////////////////////////////////////////////////////
204 // Open for reading
205 ///////////////////////////////////////////////////////////////////////////////
206
207 int DIGFileSnarkRec::Open(const char* FileName) 
208 {\r
209 #if DIGFILESNARKREC_DEBUG_LEVEL > 3\r
210   fprintf(stderr, "DIGFileSnarkRec::Open\n");\r
211 #endif\r
212
213   if(DIGFile::Open(FileName) != 0) {\r
214 #if DIGFILESNARKREC_DEBUG_LEVEL > 0\r
215   printf("** Error: Openning DIG file\n");\r
216 #endif
217     return -1; // error opening dig file
218   }
219
220   if(strcmp(MainHeader.Type.Get(), TypeStr) != 0) {\r
221 #if DIGFILESNARKREC_DEBUG_LEVEL > 0\r
222   printf("** Error: Not a rsnark05 reffil file\n");\r
223 #endif
224     return -2; // not a RecFile
225   }
226
227   // check if there is a phantom in the file
228
229   // select first array set
230   if(SelectArraySet(0) != 0) {
231                 return 4;
232   }
233
234   const char* type;
235
236   if(GetArraySetType(&type) != 0) {
237                 return 6;
238   }
239
240   //// Move to first reconstruction array
241   if(strncmp(type, "PHAN", 4) == 0) { // if first set is phantom set
242     PhantomPresent = true;
243   }
244   else {
245     PhantomPresent = false;
246   }
247
248   return 0;
249 }
250
251 ///////////////////////////////////////////////////////////////////////////////
252 // Reading main header 
253 ///////////////////////////////////////////////////////////////////////////////
254
255 int DIGFileSnarkRec::GetMainHeader(RecFileMH* MainHeader)
256 {\r
257 #if DIGFILESNARKREC_DEBUG_LEVEL > 3\r
258   fprintf(stderr, "DIGFileSnarkRec::GetMainHeader\n");\r
259 #endif\r
260
261   // read title
262   const char* Title;
263
264   if(GetTitle(&Title) != 0) {
265     printf("Error reading Main Header Title\n");
266     return -1;
267   }
268
269   printf("Main Header Title: %s\n", Title);
270
271   if(MainHeader->Title.Set(Title) != 0) {
272     printf("Error seting Main Header Title\n");
273     return -1;
274   }
275
276
277   if(GetDimensions(&(MainHeader->Dimensions)) != 0) {
278     printf("Error reading Main Header Dimensions\n");
279     return -1;
280   }
281
282   printf("Main Header Dimensions: %d\n", MainHeader->Dimensions);
283
284   if(GetSampling(&(MainHeader->Sampling)) != 0) {
285     printf("Error reading Main Header Sampling\n");
286     return -1;
287   }
288
289   printf("Main Header Sampling: %f\n", MainHeader->Sampling);
290
291
292   const char* Comment;
293
294   if(GetComment(&Comment) != 0) {
295     printf("Error reading Main Header Comment\n");
296     return -1;
297   }
298
299   printf("Main Header Comment: %s\n", Comment);
300
301   if(MainHeader->Comment.Set(Comment) != 0) {
302     printf("Error Seting Main Header Comment\n");
303     return -1;
304   }
305
306   return 0;
307 }
308
309 ///////////////////////////////////////////////////////////////////////////////
310 // Reading phantom header
311 ///////////////////////////////////////////////////////////////////////////////
312
313 int DIGFileSnarkRec::GetPhantomHeader(RecFilePhanomH* pPhantomHeader)
314 {\r
315 #if DIGFILESNARKREC_DEBUG_LEVEL > 3\r
316   fprintf(stderr, "DIGFileSnarkRec::GetPhantomHeader\n");\r
317 #endif\r
318
319   // read Name
320
321   const char* Name;
322
323   if(GetPhantomName(&Name) != 0) {
324     printf("Error reading Phantom Name\n");
325     return -1;
326   }
327
328   printf("Phantom Name: %s\n", Name);
329
330   if(pPhantomHeader->Name.Set(Name) != 0) {
331     printf("Error Setting Phantom Name\n");
332     return -1;
333   }
334
335   // read comment
336
337   const char* Comment;
338
339   if(GetPhantomComment(&Comment) != 0) {
340     printf("Error reading Phantom Comment\n");
341     return -1;
342   }
343
344   printf("Phantom Comment: %s\n", Comment);
345
346   if(pPhantomHeader->Comment.Set(Comment) != 0) {
347     printf("Error Setting Phantom Comment\n");
348     return -1;
349   }
350
351   return 0;
352 }
353
354 int DIGFileSnarkRec::GetPhantomName(const char** pPhantomName)
355 {\r
356 #if DIGFILESNARKREC_DEBUG_LEVEL > 3\r
357   fprintf(stderr, "DIGFileSnarkRec::GetPhantomName\n");\r
358 #endif\r
359
360   if(!PhantomPresent) {\r
361 \r
362 #if DIGFILESNARKREC_DEBUG_LEVEL > 0\r
363   printf("** Error: No phantom in file\n");\r
364 #endif\r
365     return -1; // no phantom in file 
366   }
367
368   // select first array set
369   if(SelectArraySet(0) != 0) {\r
370 \r
371 #if DIGFILESNARKREC_DEBUG_LEVEL > 0\r
372   printf("** Error: No array sets in file\n");\r
373 #endif
374                 return -2;
375   }
376
377   return DIGFile::GetArraySetTitle(pPhantomName);
378 }
379
380 int DIGFileSnarkRec::GetPhantomComment(const char** pPhantomComment)
381 {\r
382 #if DIGFILESNARKREC_DEBUG_LEVEL > 3\r
383   fprintf(stderr, "DIGFileSnarkRec::GetPhantomComment\n");\r
384 #endif\r
385
386   if(!PhantomPresent) {\r
387 \r
388 #if DIGFILESNARKREC_DEBUG_LEVEL > 0\r
389   printf("** Error: No phantom in file\n");\r
390 #endif
391     return -1; // no phantom in file 
392   }
393
394   // select first array set
395   if(SelectArraySet(0) != 0) {\r
396 \r
397 #if DIGFILESNARKREC_DEBUG_LEVEL > 0\r
398   printf("** Error: No phantom in file\n");\r
399 #endif
400                 return -2;
401   }
402
403   return DIGFile::GetArraySetComment(pPhantomComment);
404 }
405
406 ///////////////////////////////////////////////////////////////////////////////
407 // Reading phantom data
408 ///////////////////////////////////////////////////////////////////////////////
409
410 int DIGFileSnarkRec::GetPhantomData(void* pPhantomData)
411 {\r
412 #if DIGFILESNARKREC_DEBUG_LEVEL > 3\r
413   fprintf(stderr, "DIGFileSnarkRec::GetPhantomData\n");\r
414 #endif\r
415
416   if(!PhantomPresent) {
417     return -1; // no phantom in file 
418   }
419
420   // select first array set
421   if(SelectArraySet(0) != 0) {\r
422 #if DIGFILESNARKREC_DEBUG_LEVEL > 0\r
423   printf("** Error: No phantom in file\n");\r
424 #endif
425                 return -2;
426   }
427
428   // select first array
429   if(SelectArray(0) != 0) {\r
430 #if DIGFILESNARKREC_DEBUG_LEVEL > 0\r
431   printf("** Error: No phantom in file\n");\r
432 #endif
433                 return -3;
434   }
435
436   return DIGFile::GetArrayData(pPhantomData);
437 }
438
439 ///////////////////////////////////////////////////////////////////////////////
440 // Reading reconstruction set header
441 ///////////////////////////////////////////////////////////////////////////////
442
443 int DIGFileSnarkRec::GetRecSetHeader(RecFileRecSetH* pRecSetHeader)
444 {\r
445 #if DIGFILESNARKREC_DEBUG_LEVEL > 3\r
446   fprintf(stderr, "DIGFileSnarkRec::GetRecSetHeader\n");\r
447 #endif\r
448
449   // read Name
450
451   const char* Name;
452
453   if(GetRecSetName(&Name) != 0) {
454     printf("Error reading Reconstruction Name\n");
455     return -1;
456   }
457
458   printf("Reconstruction Name: %s\n", Name);
459
460   if(pRecSetHeader->Name.Set(Name) != 0) {
461     printf("Error Setting Reconstruction Name\n");
462     return -1;
463   }
464
465   // read AlgName
466
467   const char* AlgName;
468
469   if(GetRecSetAlgName(&AlgName) != 0) {
470     printf("Error reading Reconstruction AlgName\n");
471     return -1;
472   }
473
474   printf("Reconstruction Name: %s\n", AlgName);
475
476   if(pRecSetHeader->Name.Set(AlgName) != 0) {
477     printf("Error Setting Reconstruction AlgName\n");
478     return -1;
479   }
480
481   // read comment
482
483   const char* Comment;
484
485   if(GetRecSetComment(&Comment) != 0) {
486     printf("Error reading Reconstruction Set Comment\n");
487     return -1;
488   }
489
490   printf("Reconstruction Comment: %s\n", Comment);
491
492   if(pRecSetHeader->Comment.Set(Comment) != 0) {
493     printf("Error Setting Reconstruction Set Comment\n");
494     return -1;
495   }
496
497   return 0;
498 }
499
500 int DIGFileSnarkRec::GetRecSetName(const char** pRecName)
501 {\r
502 #if DIGFILESNARKREC_DEBUG_LEVEL > 3\r
503   fprintf(stderr, "DIGFileSnarkRec::GetRecSetName\n");\r
504 #endif\r
505
506   return DIGFile::GetArraySetTitle(pRecName);
507 }
508
509 int DIGFileSnarkRec::GetRecSetAlgName(const char** pAlgName)
510 {\r
511 #if DIGFILESNARKREC_DEBUG_LEVEL > 3\r
512   fprintf(stderr, "DIGFileSnarkRec::GetRecSetAlgName\n");\r
513 #endif\r
514
515   return DIGFile::GetArraySetType(pAlgName);
516 }
517
518 int DIGFileSnarkRec::GetRecSetComment(const char** pComment)
519 {\r
520 #if DIGFILESNARKREC_DEBUG_LEVEL > 3\r
521   fprintf(stderr, "DIGFileSnarkRec::GetRecSetComment\n");\r
522 #endif\r
523
524   return DIGFile::GetArraySetComment(pComment);
525 }
526
527 ///////////////////////////////////////////////////////////////////////////////
528 // Reading reconstruction header
529 ///////////////////////////////////////////////////////////////////////////////
530
531 int DIGFileSnarkRec::GetRecHeader(RecFileRecH* RecHeader)
532 {\r
533 #if DIGFILESNARKREC_DEBUG_LEVEL > 3\r
534   fprintf(stderr, "DIGFileSnarkRec::GetRecHeader\n");\r
535 #endif\r
536
537   // read Reconstruction Count
538
539   if(GetRecCount(&(RecHeader->Count)) != 0) {
540     printf("Error reading Reconstruction Count\n");
541     return -1;
542   }
543
544   printf("Reconstruction Count: %u\n", RecHeader->Count);
545
546   // read comment
547
548   const char* Comment;
549
550   if(GetRecComment(&Comment) != 0) {
551     printf("Error reading Reconstruction Comment\n");
552     return -1;
553   }
554
555   printf("Reconstruction Comment: %s\n", Comment);
556
557   if(RecHeader->Comment.Set(Comment) != 0) {
558     printf("Error Setting Reconstruction Comment\n");
559     return -1;
560   }
561
562   return 0;
563 }
564
565
566 int DIGFileSnarkRec::GetRecCount(unsigned int* pCount)
567 {\r
568 #if DIGFILESNARKREC_DEBUG_LEVEL > 3\r
569   fprintf(stderr, "DIGFileSnarkRec::GetRecCount\n");\r
570 #endif\r
571
572   return DIGFile::GetArrayEnumNo(pCount);
573 }
574
575 int DIGFileSnarkRec::GetRecComment(const char** pComment)
576 {\r
577 #if DIGFILESNARKREC_DEBUG_LEVEL > 3\r
578   fprintf(stderr, "DIGFileSnarkRec::GetRecComment\n");\r
579 #endif\r
580
581   return DIGFile::GetArrayComment(pComment);
582 }
583
584
585