Changeset 14259 for fact/tools/pyscripts
- Timestamp:
- 07/13/12 09:54:18 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
fact/tools/pyscripts/sandbox/dneise/fact_compress/c++/readfits.cpp
r14229 r14259 4 4 #include <fstream> 5 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 6 11 using namespace std; 7 12 … … 9 14 char * memblock; 10 15 11 int 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()) 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()) 17 95 { 18 96 // create our own header … … 23 101 const int ascii_header_size = 0x2d00; 24 102 char * memblock = new char [ascii_header_size]; 25 file.read(memblock, ascii_header_size);103 data.read(memblock, ascii_header_size); 26 104 out.write(memblock, ascii_header_size); 27 105 delete[] memblock; 28 106 29 107 for ( int event_id = 0 ; event_id < 1000; event_id++) 30 //while ( file.good() )108 //while ( data.good() ) 31 109 { 32 110 // copy binary header to new file 33 111 const int bin_header_size = 3390; 34 112 char * memblock = new char [bin_header_size]; 35 file.read(memblock, bin_header_size);113 data.read(memblock, bin_header_size); 36 114 out.write(memblock, bin_header_size); 37 115 delete[] memblock; … … 47 125 unsigned char * sizes = new unsigned char [diff_size]; 48 126 49 file.read(memblock, data_size*sizeof(short) );127 data.read(memblock, data_size*sizeof(short) ); 50 128 51 129 for ( int i = 0; i<diff_size; i++) … … 117 195 for (int j = 0; j<group_sizes[i]; j++) 118 196 { 119 out.write( ( char*)&(diffs[diff_index++]), 1);197 out.write( (short*)&(diffs[diff_index++]), 2); 120 198 } 121 122 199 } 123 200 } … … 131 208 } 132 209 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);210 long after_address = data.tellg(); 211 data.seekg (0, ios::end); 212 long end = data.tellg(); 213 data.seekg (after_address, ios::beg); 137 214 138 215 cout << "between last event and end:" << end - after_address << endl; … … 140 217 const int rest_size = end-after_address; 141 218 char * memblock2 = new char [rest_size]; 142 file.read(memblock2, rest_size);219 data.read(memblock2, rest_size); 143 220 cout << "first char in memblock: " << int(memblock2[0]) << endl; 144 221 char lastchar = memblock2[0]; … … 156 233 157 234 158 file.close();235 data.close(); 159 236 out.close(); 160 237 } //end of if file open
Note:
See TracChangeset
for help on using the changeset viewer.