1 |
|
---|
2 | // reading a complete binary file
|
---|
3 | #include <iostream>
|
---|
4 | #include <fstream>
|
---|
5 | #include <vector>
|
---|
6 |
|
---|
7 | // I will use the fits class by TB for reading the interesting data from the files
|
---|
8 | #define HAVE_ZLIB
|
---|
9 | #include "fits"
|
---|
10 |
|
---|
11 | using namespace std;
|
---|
12 |
|
---|
13 | ifstream::pos_type size;
|
---|
14 | char * memblock;
|
---|
15 |
|
---|
16 | int main (int argc, char * argv[]) {
|
---|
17 |
|
---|
18 | if (argc < 3)
|
---|
19 | {
|
---|
20 | cout << "Usage: " << argv[0] << " data-file-name calib-file-name [ouput-file-name]" << endl;
|
---|
21 | cout << "" << endl;
|
---|
22 | }
|
---|
23 |
|
---|
24 | char * data_file_name = argv[1];
|
---|
25 | char * calib_file_name = argv[2];
|
---|
26 | char * out_file_name = 0;
|
---|
27 | if (argc == 4)
|
---|
28 | {
|
---|
29 | out_file_name = argv[3];
|
---|
30 | }
|
---|
31 | else
|
---|
32 | {
|
---|
33 | out_file_name = new char[strlen(data_file_name)+4];
|
---|
34 | strcpy(out_file_name, data_file_name);
|
---|
35 | strcpy(out_file_name+strlen(data_file_name),".fc");
|
---|
36 | out_file_name[strlen(data_file_name)+3] = 0;
|
---|
37 | }
|
---|
38 |
|
---|
39 |
|
---|
40 |
|
---|
41 |
|
---|
42 | //=========================================================================
|
---|
43 | // CALIBRATION CONSTANTS
|
---|
44 | //=========================================================================
|
---|
45 | fits * calib =new fits(calib_file_name);
|
---|
46 | calib->PrintKeys();
|
---|
47 | calib->PrintColums();
|
---|
48 | offset_mV = new float[]
|
---|
49 |
|
---|
50 | // I need the offset calibration constants from the calibration files
|
---|
51 | // they are stored as floats at a known position inside the calibration file...
|
---|
52 | long position_of_calibration_constants = 0x1000;
|
---|
53 | // The calibration constants are stored in units of pseudo-mV.
|
---|
54 | // but I need them in ADC units
|
---|
55 | // I will first read offset_mV from the file, then multiply (or divide)
|
---|
56 | // with the conversion factor 2000/4096.;
|
---|
57 | // then I will convert it to shorts.
|
---|
58 | // From that point on I will only use the shorts in *offset*, so I can
|
---|
59 | // free the memory for *offset_mV* already.
|
---|
60 | int size_of_offset = 1440*1024;
|
---|
61 | int size_of_offset_memblock = 1440*1024*sizeof(float);
|
---|
62 | float * offset_mV = new float[size_of_offset];
|
---|
63 | short * offset = new short[size_of_offset];
|
---|
64 | char * memblock = new char[size_of_offset_memblock];
|
---|
65 |
|
---|
66 | ifstream calib (calib_file_name, ios::in|ios::binary);
|
---|
67 | if ( !calib.is_open() )
|
---|
68 | {
|
---|
69 | cerr << "Could not open Calibration File:" << calib_file_name << ".. ABORT." << endl;
|
---|
70 | return 1;
|
---|
71 | }
|
---|
72 | else // file was opened, I can go on...
|
---|
73 | {
|
---|
74 | calib.seekg(position_of_calibration_constants, ios::beg);
|
---|
75 | calib.read(memblock, size_of_offset_memblock);
|
---|
76 | offset_mV = (float*)memblock;
|
---|
77 | for (int i=0; i<size_of_offset; i++)
|
---|
78 | {
|
---|
79 | // -0.5 is for rounding correctly negative integers.
|
---|
80 | // in all cases where it worked, the offset should be negative for FACT.
|
---|
81 | offset[i] = short(offset_mV / 2000. * 4096 - 0.5);
|
---|
82 | }
|
---|
83 | }
|
---|
84 | delete[] offset_mV;
|
---|
85 | delete[] memblock;
|
---|
86 | calib.close();
|
---|
87 | //=========================================================================
|
---|
88 | // END OF CALIBRATION CONSTANTS
|
---|
89 | //=========================================================================
|
---|
90 |
|
---|
91 |
|
---|
92 | ifstream data (data_file_name, ios::in|ios::binary);
|
---|
93 | ofstream out (out_file_name, ios::out|ios::binary|ios::trunc);
|
---|
94 | if (data.is_open() && out.is_open())
|
---|
95 | {
|
---|
96 | // create our own header
|
---|
97 | //out.write( "FACT_COMPRESS ", 80);
|
---|
98 | //out.write( "END. ", 80);
|
---|
99 |
|
---|
100 | // copy first 0x2d00 bytes to new file
|
---|
101 | const int ascii_header_size = 0x2d00;
|
---|
102 | char * memblock = new char [ascii_header_size];
|
---|
103 | data.read(memblock, ascii_header_size);
|
---|
104 | out.write(memblock, ascii_header_size);
|
---|
105 | delete[] memblock;
|
---|
106 |
|
---|
107 | for ( int event_id = 0 ; event_id < 1000; event_id++)
|
---|
108 | //while ( data.good() )
|
---|
109 | {
|
---|
110 | // copy binary header to new file
|
---|
111 | const int bin_header_size = 3390;
|
---|
112 | char * memblock = new char [bin_header_size];
|
---|
113 | data.read(memblock, bin_header_size);
|
---|
114 | out.write(memblock, bin_header_size);
|
---|
115 | delete[] memblock;
|
---|
116 |
|
---|
117 | // read 300 shorts out of the file
|
---|
118 | for (int chid = 0 ; chid < 1440; chid++)
|
---|
119 | {
|
---|
120 | const int data_size = 300;
|
---|
121 | const int diff_size = data_size;
|
---|
122 | char * memblock = new char [data_size*sizeof(short)];
|
---|
123 | short * data = (short *)memblock;
|
---|
124 | short * diffs = new short [diff_size];
|
---|
125 | unsigned char * sizes = new unsigned char [diff_size];
|
---|
126 |
|
---|
127 | data.read(memblock, data_size*sizeof(short) );
|
---|
128 |
|
---|
129 | for ( int i = 0; i<diff_size; i++)
|
---|
130 | {
|
---|
131 | diffs[i] = data[i]-data[i];
|
---|
132 | //~ if (diffs[i] >= -32 && diffs[i] <= 31)
|
---|
133 | //~ sizes[i] = 6;
|
---|
134 | //~ else if (diffs[i] >= -128 && diffs[i] <= 127)
|
---|
135 | //~ sizes[i] = 8;
|
---|
136 | //~ else if (diffs[i] >= -512 && diffs[i] <= 511)
|
---|
137 | //~ sizes[i] = 10;
|
---|
138 | //~ else
|
---|
139 | //~ sizes[i] = 16;
|
---|
140 | if (diffs[i] >= -128 && diffs[i] <= 127)
|
---|
141 | sizes[i] = 8;
|
---|
142 | else
|
---|
143 | sizes[i] = 16;
|
---|
144 | }
|
---|
145 |
|
---|
146 | // calculate group sizes
|
---|
147 | int counter = 0;
|
---|
148 | unsigned char last_size = sizes[0];
|
---|
149 | vector<int> group_sizes;
|
---|
150 | vector<unsigned char> groups;
|
---|
151 |
|
---|
152 | for (int i=0 ; i < diff_size; i++)
|
---|
153 | {
|
---|
154 |
|
---|
155 | if (sizes[i] != last_size)
|
---|
156 | {
|
---|
157 | group_sizes.push_back( counter );
|
---|
158 | groups.push_back( last_size );
|
---|
159 | last_size = sizes[i];
|
---|
160 | }
|
---|
161 | counter++;
|
---|
162 | }
|
---|
163 | groups.push_back( last_size );
|
---|
164 | group_sizes.push_back( counter );
|
---|
165 |
|
---|
166 |
|
---|
167 | // write the first short
|
---|
168 | out.write(memblock, 1*sizeof(short) );
|
---|
169 | // for all groups write header and group
|
---|
170 | short header = 0;
|
---|
171 | int diff_index = 0;
|
---|
172 | for ( int i = 0 ; i < (int)groups.size() ; i++)
|
---|
173 | {
|
---|
174 | // write header
|
---|
175 | if (groups[i] == 8)
|
---|
176 | {
|
---|
177 | header = short(group_sizes[i]);
|
---|
178 | }
|
---|
179 | else
|
---|
180 | {
|
---|
181 | header = 0x8000 | short(group_sizes[i] );
|
---|
182 | }
|
---|
183 | out.write( (char*)&header, sizeof(short));
|
---|
184 |
|
---|
185 | // write group
|
---|
186 | if (groups[i] == 8)
|
---|
187 | {
|
---|
188 | for (int j = 0; j<group_sizes[i]; j++)
|
---|
189 | {
|
---|
190 | out.write( (char*)&(diffs[diff_index++]), 1);
|
---|
191 | }
|
---|
192 | }
|
---|
193 | else
|
---|
194 | {
|
---|
195 | for (int j = 0; j<group_sizes[i]; j++)
|
---|
196 | {
|
---|
197 | out.write( (short*)&(diffs[diff_index++]), 2);
|
---|
198 | }
|
---|
199 | }
|
---|
200 | }
|
---|
201 |
|
---|
202 | //out.write(memblock, data_size*sizeof(short) );
|
---|
203 |
|
---|
204 | delete[] memblock;
|
---|
205 | delete[] diffs;
|
---|
206 | delete[] sizes;
|
---|
207 | }
|
---|
208 | }
|
---|
209 | cout << "finished with 1000 events." << endl;
|
---|
210 | long after_address = data.tellg();
|
---|
211 | data.seekg (0, ios::end);
|
---|
212 | long end = data.tellg();
|
---|
213 | data.seekg (after_address, ios::beg);
|
---|
214 |
|
---|
215 | cout << "between last event and end:" << end - after_address << endl;
|
---|
216 | // read the last piece...
|
---|
217 | const int rest_size = end-after_address;
|
---|
218 | char * memblock2 = new char [rest_size];
|
---|
219 | data.read(memblock2, rest_size);
|
---|
220 | cout << "first char in memblock: " << int(memblock2[0]) << endl;
|
---|
221 | char lastchar = memblock2[0];
|
---|
222 | for (int i =0 ; i<rest_size; i++)
|
---|
223 | {
|
---|
224 | if (memblock2[i] != lastchar)
|
---|
225 | {
|
---|
226 | cout << "new char at: " << i << " with value:" << int(memblock[0]) << endl;
|
---|
227 | lastchar = memblock2[i];
|
---|
228 | }
|
---|
229 | }
|
---|
230 |
|
---|
231 | out.write(memblock2, rest_size);
|
---|
232 | delete[] memblock2;
|
---|
233 |
|
---|
234 |
|
---|
235 | data.close();
|
---|
236 | out.close();
|
---|
237 | } //end of if file open
|
---|
238 | } //end of main |
---|