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 |
|
---|
13 | using namespace std;
|
---|
14 |
|
---|
15 | ifstream::pos_type size;
|
---|
16 | char * memblock;
|
---|
17 |
|
---|
18 | int 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),".uc");
|
---|
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 | // copy first 0x2d00 bytes to new file
|
---|
76 | const int ascii_header_size = 0x2d00;
|
---|
77 | char * memblock = new char [ascii_header_size];
|
---|
78 | datafile.read(memblock, ascii_header_size);
|
---|
79 | out.write(memblock, ascii_header_size);
|
---|
80 | delete[] memblock;
|
---|
81 |
|
---|
82 | for ( int event_id = 0 ; event_id < 1000; event_id++)
|
---|
83 | //while ( datafile.good() )
|
---|
84 | {
|
---|
85 | // copy binary header to new file
|
---|
86 | const int bin_header_size = 3390;
|
---|
87 | char * memblock = new char [bin_header_size];
|
---|
88 | datafile.read(memblock, bin_header_size);
|
---|
89 | out.write(memblock, bin_header_size);
|
---|
90 | delete[] memblock;
|
---|
91 |
|
---|
92 | // read the size of the compressed next event
|
---|
93 | int cs;
|
---|
94 | datafile.read((char*)&cs, sizeof(cs));
|
---|
95 | const int data_size = cs;
|
---|
96 |
|
---|
97 | // read cs bytes out of the file
|
---|
98 | for (int chid = 0 ; chid < 1440; chid++)
|
---|
99 | {
|
---|
100 | int slices =0;
|
---|
101 | short first_short;
|
---|
102 | datafile.read((char*)&first_short, sizeof(first_short));
|
---|
103 | out.write( (char*)&first_short, sizeof(first_short) );
|
---|
104 |
|
---|
105 | while (slices < 300)
|
---|
106 | {
|
---|
107 | // read header
|
---|
108 | short header;
|
---|
109 | short group_size;
|
---|
110 | int is_short;
|
---|
111 | datafile.read((char*)&header, sizeof(header));
|
---|
112 | if (header & 0x8000)
|
---|
113 | {
|
---|
114 | // the group data consists of shorts
|
---|
115 | group_size = header | 0x7FFF;
|
---|
116 | short *group;
|
---|
117 | group = new short [group_size];
|
---|
118 | datafile.read( (char*)group, group_size*sizeof(short) );
|
---|
119 | for (int groupsl = 0; groupsl < group_size; groupsl++)
|
---|
120 | {
|
---|
121 | first_short += group[groupsl];
|
---|
122 | out.write( (char*)&first_short, sizeof(short) );
|
---|
123 | }
|
---|
124 | delete[] group;
|
---|
125 | }
|
---|
126 | else
|
---|
127 | {
|
---|
128 | // the group data consists of bytes
|
---|
129 | group_size = header;
|
---|
130 | char *group;
|
---|
131 | group = new char [group_size];
|
---|
132 | datafile.read( (char*)group, group_size*sizeof(char) );
|
---|
133 | for (int groupsl = 0; groupsl < group_size; groupsl++)
|
---|
134 | {
|
---|
135 | first_short += group[groupsl];
|
---|
136 | out.write( (char*)&first_short, sizeof(short) );
|
---|
137 | }
|
---|
138 | delete[] group;
|
---|
139 | }
|
---|
140 |
|
---|
141 | slices += group_size;
|
---|
142 |
|
---|
143 | }
|
---|
144 | }
|
---|
145 |
|
---|
146 |
|
---|
147 | datafile.close();
|
---|
148 | out.close();
|
---|
149 | } //end of if file open
|
---|
150 | } //end of main
|
---|