source: fact/tools/pyscripts/sandbox/dneise/fact_compress/c++/readfits.cpp

Last change on this file was 14268, checked in by neise, 12 years ago
evolving
File size: 7.4 KB
Line 
1#include <iostream>
2#include <fstream>
3#include <vector>
4#include <string.h>
5
6// I will use the fits class by TB for reading the interesting data from the files
7
8#include "izstream.h"
9
10#define HAVE_ZLIB
11#include "fits.h"
12
13using namespace std;
14
15ifstream::pos_type size;
16char * memblock;
17
18int main (int argc, char * argv[]) {
19
20
21 if (argc < 3)
22 {
23 cout << "Usage: " << argv[0] << " data-file-name calib-file-name [ouput-file-name]" << endl;
24 cout << "" << endl;
25 }
26
27 char * data_file_name = argv[1];
28 char * calib_file_name = argv[2];
29 char * out_file_name = 0;
30 if (argc == 4)
31 {
32 out_file_name = argv[3];
33 }
34 else
35 {
36 out_file_name = new char[strlen(data_file_name)+4];
37 strcpy(out_file_name, data_file_name);
38 strcpy(out_file_name+strlen(data_file_name),".fc");
39 out_file_name[strlen(data_file_name)+3] = 0;
40 }
41
42
43
44
45 //=========================================================================
46 // CALIBRATION CONSTANTS
47 //=========================================================================
48 fits * calib =new fits(calib_file_name);
49 calib->PrintKeys();
50 calib->PrintColumns();
51
52 int size_of_offset = 1440*1024;
53 float * offset_mV = new float[size_of_offset];
54 short * offset = new short[size_of_offset];
55 calib->SetPtrAddress("BaselineMean",offset_mV, size_of_offset);
56
57 calib->GetNextRow();
58 for (int i =0 ; i<size_of_offset; i++)
59 {
60 offset[i] = short(offset_mV[i] / 2000. * 4096 - 0.5);
61 }
62 cout << endl;
63 delete[] offset_mV;
64 delete calib;
65 calib = NULL;
66 //=========================================================================
67 // END OF CALIBRATION CONSTANTS
68 //=========================================================================
69
70
71 ifstream datafile (data_file_name, ios::in|ios::binary);
72 ofstream out (out_file_name, ios::out|ios::binary|ios::trunc);
73 if (datafile.is_open() && out.is_open())
74 {
75 // create our own header
76 //out.write( "FACT_COMPRESS ", 80);
77 //out.write( "END. ", 80);
78
79 // copy first 0x2d00 bytes to new file
80 const int ascii_header_size = 0x2d00;
81 char * memblock = new char [ascii_header_size];
82 datafile.read(memblock, ascii_header_size);
83 out.write(memblock, ascii_header_size);
84 delete[] memblock;
85
86 for ( int event_id = 0 ; event_id < 1000; event_id++)
87 //while ( datafile.good() )
88 {
89 // copy binary header to new file
90 const int bin_header_size = 3390;
91 char * memblock = new char [bin_header_size];
92 datafile.read(memblock, bin_header_size);
93 out.write(memblock, bin_header_size);
94 delete[] memblock;
95
96 // read 300 shorts out of the file
97 for (int chid = 0 ; chid < 1440; chid++)
98 {
99 const int data_size = 300;
100 const int diff_size = data_size;
101 char * memblock = new char [data_size*sizeof(short)];
102 short * data = (short *)memblock;
103 short * diffs = new short [diff_size];
104 unsigned char * sizes = new unsigned char [diff_size];
105
106 datafile.read(memblock, data_size*sizeof(short) );
107
108 for ( int i = 0; i<diff_size; i++)
109 {
110 diffs[i] = data[i]-data[i];
111 //~ if (diffs[i] >= -32 && diffs[i] <= 31)
112 //~ sizes[i] = 6;
113 //~ else if (diffs[i] >= -128 && diffs[i] <= 127)
114 //~ sizes[i] = 8;
115 //~ else if (diffs[i] >= -512 && diffs[i] <= 511)
116 //~ sizes[i] = 10;
117 //~ else
118 //~ sizes[i] = 16;
119 if (diffs[i] >= -128 && diffs[i] <= 127)
120 sizes[i] = 8;
121 else
122 sizes[i] = 16;
123 }
124
125 // calculate group sizes
126 int counter = 0;
127 unsigned char last_size = sizes[0];
128 vector<int> group_sizes;
129 vector<unsigned char> groups;
130
131 for (int i=0 ; i < diff_size; i++)
132 {
133
134 if (sizes[i] != last_size)
135 {
136 group_sizes.push_back( counter );
137 groups.push_back( last_size );
138 last_size = sizes[i];
139 }
140 counter++;
141 }
142 groups.push_back( last_size );
143 group_sizes.push_back( counter );
144
145
146 // write the first short
147 out.write(memblock, 1*sizeof(short) );
148 // for all groups write header and group
149 short header = 0;
150 int diff_index = 0;
151 for ( int i = 0 ; i < (int)groups.size() ; i++)
152 {
153 // write header
154 if (groups[i] == 8)
155 {
156 header = short(group_sizes[i]);
157 }
158 else
159 {
160 header = 0x8000 | short(group_sizes[i] );
161 }
162 out.write( (char*)&header, sizeof(short));
163
164 // write group
165 if (groups[i] == 8)
166 {
167 for (int j = 0; j<group_sizes[i]; j++)
168 {
169 out.write( (char*)&(diffs[diff_index++]), 1);
170 }
171 }
172 else
173 {
174 for (int j = 0; j<group_sizes[i]; j++)
175 {
176 out.write( (char*)&(diffs[diff_index++]), 2);
177 }
178 }
179 }
180
181 //out.write(memblock, data_size*sizeof(short) );
182
183 delete[] memblock;
184 delete[] diffs;
185 delete[] sizes;
186 }
187 }
188 cout << "finished with 1000 events." << endl;
189 long after_address = datafile.tellg();
190 datafile.seekg (0, ios::end);
191 long end = datafile.tellg();
192 datafile.seekg (after_address, ios::beg);
193
194 cout << "between last event and end:" << end - after_address << endl;
195 // read the last piece...
196 const int rest_size = end-after_address;
197 char * memblock2 = new char [rest_size];
198 datafile.read(memblock2, rest_size);
199 cout << "first char in memblock: " << int(memblock2[0]) << endl;
200 char lastchar = memblock2[0];
201 for (int i =0 ; i<rest_size; i++)
202 {
203 if (memblock2[i] != lastchar)
204 {
205 cout << "new char at: " << i << " with value:" << int(memblock[0]) << endl;
206 lastchar = memblock2[i];
207 }
208 }
209
210 out.write(memblock2, rest_size);
211 delete[] memblock2;
212
213
214 datafile.close();
215 out.close();
216 } //end of if file open
217} //end of main
Note: See TracBrowser for help on using the repository browser.