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

Last change on this file since 14229 was 14229, checked in by neise, 12 years ago
und so weiter
File size: 5.7 KB
Line 
1
2// reading a complete binary file
3#include <iostream>
4#include <fstream>
5#include <vector>
6using namespace std;
7
8ifstream::pos_type size;
9char * memblock;
10
11int main () {
12
13 ifstream file ("a.fits", ios::in|ios::binary);
14 ofstream out ("bla.bin", ios::out|ios::binary|ios::trunc);
15
16 if (file.is_open() && out.is_open())
17 {
18 // create our own header
19 //out.write( "FACT_COMPRESS ", 80);
20 //out.write( "END. ", 80);
21
22 // copy first 0x2d00 bytes to new file
23 const int ascii_header_size = 0x2d00;
24 char * memblock = new char [ascii_header_size];
25 file.read(memblock, ascii_header_size);
26 out.write(memblock, ascii_header_size);
27 delete[] memblock;
28
29 for ( int event_id = 0 ; event_id < 1000; event_id++)
30 //while ( file.good() )
31 {
32 // copy binary header to new file
33 const int bin_header_size = 3390;
34 char * memblock = new char [bin_header_size];
35 file.read(memblock, bin_header_size);
36 out.write(memblock, bin_header_size);
37 delete[] memblock;
38
39 // read 300 shorts out of the file
40 for (int chid = 0 ; chid < 1440; chid++)
41 {
42 const int data_size = 300;
43 const int diff_size = data_size;
44 char * memblock = new char [data_size*sizeof(short)];
45 short * data = (short *)memblock;
46 short * diffs = new short [diff_size];
47 unsigned char * sizes = new unsigned char [diff_size];
48
49 file.read(memblock, data_size*sizeof(short) );
50
51 for ( int i = 0; i<diff_size; i++)
52 {
53 diffs[i] = data[i]-data[i];
54 //~ if (diffs[i] >= -32 && diffs[i] <= 31)
55 //~ sizes[i] = 6;
56 //~ else if (diffs[i] >= -128 && diffs[i] <= 127)
57 //~ sizes[i] = 8;
58 //~ else if (diffs[i] >= -512 && diffs[i] <= 511)
59 //~ sizes[i] = 10;
60 //~ else
61 //~ sizes[i] = 16;
62 if (diffs[i] >= -128 && diffs[i] <= 127)
63 sizes[i] = 8;
64 else
65 sizes[i] = 16;
66 }
67
68 // calculate group sizes
69 int counter = 0;
70 unsigned char last_size = sizes[0];
71 vector<int> group_sizes;
72 vector<unsigned char> groups;
73
74 for (int i=0 ; i < diff_size; i++)
75 {
76
77 if (sizes[i] != last_size)
78 {
79 group_sizes.push_back( counter );
80 groups.push_back( last_size );
81 last_size = sizes[i];
82 }
83 counter++;
84 }
85 groups.push_back( last_size );
86 group_sizes.push_back( counter );
87
88
89 // write the first short
90 out.write(memblock, 1*sizeof(short) );
91 // for all groups write header and group
92 short header = 0;
93 int diff_index = 0;
94 for ( int i = 0 ; i < (int)groups.size() ; i++)
95 {
96 // write header
97 if (groups[i] == 8)
98 {
99 header = short(group_sizes[i]);
100 }
101 else
102 {
103 header = 0x8000 | short(group_sizes[i] );
104 }
105 out.write( (char*)&header, sizeof(short));
106
107 // write group
108 if (groups[i] == 8)
109 {
110 for (int j = 0; j<group_sizes[i]; j++)
111 {
112 out.write( (char*)&(diffs[diff_index++]), 1);
113 }
114 }
115 else
116 {
117 for (int j = 0; j<group_sizes[i]; j++)
118 {
119 out.write( (char*)&(diffs[diff_index++]), 1);
120 }
121
122 }
123 }
124
125 //out.write(memblock, data_size*sizeof(short) );
126
127 delete[] memblock;
128 delete[] diffs;
129 delete[] sizes;
130 }
131 }
132 cout << "finished with 1000 events." << endl;
133 long after_address = file.tellg();
134 file.seekg (0, ios::end);
135 long end = file.tellg();
136 file.seekg (after_address, ios::beg);
137
138 cout << "between last event and end:" << end - after_address << endl;
139 // read the last piece...
140 const int rest_size = end-after_address;
141 char * memblock2 = new char [rest_size];
142 file.read(memblock2, rest_size);
143 cout << "first char in memblock: " << int(memblock2[0]) << endl;
144 char lastchar = memblock2[0];
145 for (int i =0 ; i<rest_size; i++)
146 {
147 if (memblock2[i] != lastchar)
148 {
149 cout << "new char at: " << i << " with value:" << int(memblock[0]) << endl;
150 lastchar = memblock2[i];
151 }
152 }
153
154 out.write(memblock2, rest_size);
155 delete[] memblock2;
156
157
158 file.close();
159 out.close();
160 } //end of if file open
161} //end of main
Note: See TracBrowser for help on using the repository browser.