source: trunk/DataCheck/Archive/fitsCompare.cpp@ 12873

Last change on this file since 12873 was 12871, checked in by lyard, 13 years ago
added more scripts
File size: 4.5 KB
Line 
1//compile with:
2//g++44 -std=c++0x -o fitsCompare -DHAVE_ZLIB=1 -lz dataChecker.cpp
3#include <string.h>
4#include <fstream>
5#include <map>
6#include <unordered_map>
7
8using namespace std;
9//#include <iostream>
10
11#include "externals/fits.h"
12
13using namespace std;
14
15 /*
16 * Usage: program-name <file1> <file2> <optional -v for verbose output>
17 *
18 *
19 *
20 *
21 *
22 *
23 */
24
25 fits* file1;
26 fits* file2;
27 char* file1Data;
28 char* file2Data;
29
30 int customReturn(int code)
31 {
32 if (file1)
33 {
34 file1->close();
35 delete file1;
36 }
37 if (file2)
38 {
39 file2->close();
40 delete file2;
41 }
42 if (file1Data)
43 {
44 delete[] file1Data;
45 }
46 if (file2Data)
47 {
48 delete[] file2Data;
49 }
50 exit(code);
51 }
52
53int main(int argc, char** argv)
54{
55 if (argc < 3)
56 return -1;
57
58 bool verbose=false;
59
60 if (argc > 3 &&
61 !strcmp(argv[3], "-v"))
62 verbose=true;
63
64 string filename1(argv[1]);
65 string filename2(argv[2]);
66 file1=NULL;
67 file2=NULL;
68 file1Data=NULL;
69 file2Data=NULL;
70 try
71 {
72 file1 = new fits(filename1);
73 file2 = new fits(filename2);
74 }
75 catch (std::runtime_error e)
76 {
77 if (verbose)
78 cout << "Could not open at least one of the two files." << endl;
79 else
80 cout << 2 << endl;
81 return -1;
82 }
83
84 //get the columns in the file.
85 fits::Table::Columns columns1=file1->GetColumns();
86 fits::Table::Columns columns2=file2->GetColumns();
87
88 if (columns1.size() != columns2.size())
89 {
90 if (verbose)
91 cout << "Different number of columns" << endl;
92 else
93 cout << "1" << endl;
94 customReturn(-1);
95 }
96
97 long totalSize=0;
98 for (auto it=columns1.begin(), jt=columns2.begin(); it != columns1.end(); it++, jt++)
99 {
100 if (it->first != jt->first)
101 {
102 if (verbose)
103 cout << "Different column names" << endl;
104 else
105 cout << "1" << endl;
106 customReturn(-1);
107 }
108 if ((it->second.offset != jt->second.offset) ||
109 (it->second.num != jt->second.num) ||
110 (it->second.size != jt->second.size) ||
111 (it->second.type != jt->second.type) ||
112 (it->second.unit != jt->second.unit))
113 {
114 if (verbose)
115 cout << "Different column def" << endl;
116 else
117 cout << "1" << endl;
118 customReturn(-1);
119 }
120
121 totalSize += it->second.size * it->second.num;
122
123 }
124
125 char* file1Data = new char[totalSize];
126 char* file2Data = new char[totalSize];
127
128 if ((sizeof(long) != 8) ||
129 (sizeof(int) != 4) ||
130 (sizeof(short) != 2))
131 {
132 if (verbose)
133 cout << "OS IS NOT SUITABLE (32bits ?) please use a 64 bits system" << endl;
134 else
135 cout << "2" << endl;
136 customReturn(-1);
137 }
138
139 for (auto it=columns1.begin(); it != columns1.end(); it++)
140 {
141 switch (it->second.size) {
142 case 1:
143 file1->SetPtrAddress(it->first, &file1Data[it->second.offset], it->second.num);
144 file2->SetPtrAddress(it->first, &file2Data[it->second.offset], it->second.num);
145 break;
146 case 2:
147 file1->SetPtrAddress(it->first, (short*)(&file1Data[it->second.offset]), it->second.num);
148 file2->SetPtrAddress(it->first, (short*)(&file2Data[it->second.offset]), it->second.num);
149 break;
150 case 4:
151 file1->SetPtrAddress(it->first, (int*)(&file1Data[it->second.offset]), it->second.num);
152 file2->SetPtrAddress(it->first, (int*)(&file2Data[it->second.offset]), it->second.num);
153 break;
154 case 8:
155 file1->SetPtrAddress(it->first, (long*)(&file1Data[it->second.offset]), it->second.num);
156 file2->SetPtrAddress(it->first, (long*)(&file2Data[it->second.offset]), it->second.num);
157 break;
158 default:
159 if (verbose)
160 cout << "Unkown column element size: " << it->second.size << endl;
161 else
162 cout << "2" << endl;
163 customReturn(-1);
164 };
165 }
166
167 int numRows1 = file1->GetInt("NAXIS2");
168 int numRows2 = file2->GetInt("NAXIS2");
169 if (numRows1 > numRows2)
170 {
171 if (verbose)
172 cout << "looks like the files has different number of rows: " << numRows1 << " vs " << numRows2 << endl;
173 else
174 cout << "1" << endl;
175 customReturn(0);
176 }
177 int row=0;
178 if (verbose)
179 cout << "files have " << numRows1 << " rows" << endl << endl;
180 while (file1->GetNextRow() &&
181 file2->GetNextRow() &&
182 row < numRows1)
183 {
184 if (verbose)
185 {
186 cout << "\rrow: " << row;
187 cout.flush();
188 }
189 for (int i=0;i<totalSize;i++)
190 {
191 if (file1Data[i] != file2Data[i])
192 {
193 if (verbose)
194 cout << "Files differ... i: " << i << " " << file1Data[i] << " " << file2Data[i] << endl;
195 else
196 cout << "1" << endl;
197 customReturn(0);
198 }
199 }
200 row++;
201 }
202 if (numRows1 != numRows2)
203 {
204 if (verbose)
205 cout << "Archive has more rows. orig. data is fine" << endl;
206 else
207 cout << "3" << endl;
208 }
209 if (verbose)
210 cout << "Files data is identical" << endl;
211 else
212 cout << "0" << endl;
213 customReturn(0);
214}
Note: See TracBrowser for help on using the repository browser.