krishan prajapat / Mbed 2 deprecated sd_card_making_header_sss2

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers compression.h Source File

compression.h

00001 int disk_write(const uint8_t *, uint64_t);
00002 uint64_t  RTC_TIME;                                    //need to  be changed to uint_64
00003 
00004 
00005 namespace Science_TMframe {
00006     
00007     #define OUTLENGTH 360                                           //length of the output frame after convolution
00008     #define SDcard_block 512                                        //block size of the sd card
00009     
00010     Convolution ConvObj;                                            //object which stores the frame after convolution
00011     bool fresh[3] = {true,true,true};                               // True only for the first time                                                                 
00012     unsigned char frames[3][134] = {0};                             // "frame" stores the address of the current frame...."first_frame_address" stores the address of the first node made.
00013     unsigned int FCN[4] = {0};                                      //frame count number
00014     unsigned int data_starting_point[3] = {8,5,10};             
00015     unsigned int max_data[3] = {124,127,122};                       //number of bytes in each frame excluding TMID,FCN,first_header_point,crc
00016     unsigned char TM_convoluted_data[270] = {0};                    //270 bytes is the size after convolution of 1072 bits
00017     unsigned char complete_frame[SDcard_block] = {0};
00018     uint64_t  SDC_address = 10;
00019     bool SCH_FCCH_FLAG = true;
00020     
00021     
00022     void add_SCH_FCCH(){
00023         int i = 0;
00024         complete_frame[0] = 0x0a;
00025         complete_frame[1] = 0x3f;;
00026         complete_frame[2] = 0x46;
00027         complete_frame[3] = 0xb4;
00028         complete_frame[4] = 0x00;
00029         
00030         for(i = 149 ; i < 159 ; i ++){
00031             complete_frame[i] = 0 ; 
00032         }
00033         
00034         complete_frame[159] = 0x0a;
00035         complete_frame[160] = 0x3f;;
00036         complete_frame[161] = 0x46;
00037         complete_frame[162] = 0xb4;
00038         complete_frame[163] = 0x00;
00039         
00040         for(i = 308 ; i < 318 ; i ++){
00041             complete_frame[i] = 0 ; 
00042         }
00043         
00044     }
00045     
00046     void making_frameHeader(unsigned char  TMID){
00047     
00048         unsigned char frame_type_identifier = 0;                                                        // not conform about the values , yet to be done    
00049         frames[TMID][0] = (frame_type_identifier<<7) + ( (TMID + 1)<<3 ) + ( (FCN[TMID]>>24) & 0x7 );   //frame number should be less than 2^23 since 23 bits are assigned for that
00050         frames[TMID][1] = ((FCN[TMID]>>16) & 0xff );
00051         frames[TMID][2] = ( (FCN[TMID]>>8 )& 0xff );
00052         frames[TMID][3] = ( FCN[TMID] & 0xff      );                                                        // first bit for (frame identifier), next 4 for (TMID) and next 27 for FCN
00053         
00054         if(TMID == 0){
00055             frames[TMID][5] =( (RTC_TIME>>29) & 0xff );
00056             frames[TMID][6] =( (RTC_TIME>>21) & 0xff ); 
00057             frames[TMID][7] =( (RTC_TIME>>13) & 0xff );
00058             
00059         }else if(TMID == 2){
00060             frames[TMID][5] =( (RTC_TIME>>32) & 0xff );
00061             frames[TMID][6] =( (RTC_TIME>>24) & 0xff ); 
00062             frames[TMID][7] =( (RTC_TIME>>16) & 0xff );
00063             frames[TMID][8] =( (RTC_TIME>>8 ) & 0xff );
00064             frames[TMID][9] =( (RTC_TIME    ) & 0xff );
00065         }
00066         
00067     }
00068 
00069     void convolution (unsigned char * ptr){
00070     
00071         ConvObj.convolutionEncode(ptr , TM_convoluted_data);
00072         ConvObj.convolutionEncode(ptr + 67, TM_convoluted_data + 135);
00073 
00074     }
00075 
00076     /*
00077         @brief : take the address of array of LCR or HCR and stores it into a frame
00078         @parameters: type->L or H , deprnding on wheather it is LCR or HCR respectively
00079         @return: nothing
00080     */
00081 
00082 //  type 2 yet to be done
00083     void making_frame(unsigned char TMID ,unsigned char type, unsigned char* pointer){
00084         
00085         TMID--;                                          //TMID goes from 1 to 3 , convinient to ue from 0 to 2
00086         static int frame_space_number[3] = {0};          //this variable represents the register number of the frame in which LCR or HCR data to be written not including header
00087         int  packet_len = 0;
00088         int copy_count = 0 ;
00089         
00090         switch(int(TMID)){
00091             case 0:                                     //SCP
00092                 if(type == 'L'){                        //below threshold
00093                     packet_len = 22;   
00094                 }
00095                 else if(type == 'H'){                   //above threshold
00096                     packet_len = 26;
00097                 }
00098                 break;
00099             
00100             case 1:                                     //SFP above threshold
00101                 packet_len = 35;
00102                 break;
00103         
00104             case 2:                                     //SFP  below threshold
00105                 packet_len = 23;
00106                 break;  
00107         }
00108         
00109         if(SCH_FCCH_FLAG){
00110             add_SCH_FCCH();
00111             SCH_FCCH_FLAG = false;
00112         }
00113 
00114         if(fresh[TMID]){
00115             //welcome to first frame
00116             making_frameHeader(TMID);
00117             frames[TMID][4] = 0;
00118             fresh[TMID] = false;
00119         }
00120     
00121     
00122         while(copy_count < packet_len){                                                                  // 22 bytes is the size of the LCR
00123             frames[TMID][ frame_space_number[TMID] + data_starting_point[TMID] ]= *(pointer + copy_count);
00124             frame_space_number[TMID]++;                                          
00125             copy_count++;
00126             if( frame_space_number[TMID] == max_data[TMID] ){                                //frame space number can go from 0 to 126 as data is written from 0+5 to 126+5
00127                 FCN[TMID]++;
00128                 // convolution and save frame in the sd card
00129                 
00130                 // copying crc in 132 and 133
00131                 int temp_crc;
00132                 temp_crc = CRC::crc16_gen(frames[TMID],132);
00133                 frames[TMID][132] = temp_crc>>8;
00134                 frames[TMID][133] = temp_crc & 0xff;                
00135                 
00136                 //convolution and interleaving  
00137                 convolution(frames[TMID]);
00138                 interleave(TM_convoluted_data , complete_frame + 5);
00139                 interleave(TM_convoluted_data+ 135,complete_frame + 164);
00140                 
00141                 disk_write(complete_frame , SDC_address);
00142                 SDC_address++;
00143                
00144             
00145                 
00146                 //now save to the sd card TM_convoluted_data
00147 //              std::bitset<8> b;
00148 //              printf("\nthis is frame %d\n",TMID);                                        //for printing frame 
00149 //                  for(int j =0; j<134;j++){
00150 //                  printf(" %d",frames[TMID][j]);
00151 ////                    b =  frames[TMID][j];
00152 ////                    cout<<b;
00153 //                  }
00154                 
00155                 frame_space_number[TMID] = 0;
00156                 making_frameHeader(TMID);
00157                 frames[TMID][4]=packet_len - copy_count;
00158                 //write time  here also
00159                 continue;
00160             }       
00161         }
00162     
00163 //      printf("\nthis is frame %d\n",TMID);                                              //for printing frame 
00164 //      for(int j =0; j<134;j++){
00165 //          printf(" %d",frames[TMID][j]);
00166 //      }
00167 
00168     }   
00169 
00170 
00171 }
00172 
00173 
00174 
00175 
00176 
00177 namespace Science_Data_Compression{
00178 
00179     # define PACKET_SEQUENCE_COUNT 1                         //1 byte
00180     # define NUM_PROTON_BIN 17                               //2 byte each
00181     # define NUM_ELECTRON_BIN 14                             //2 byte each 
00182     # define VETO 1                                          //2 byte
00183     # define FASTCHAIN 2                                     //4 byte each
00184     #define RAW_PACKET_LENGTH 73                             //73 bytes
00185 
00186 //  #define PACKET_SEQ_COUNT 1
00187 //  #define PROTON_BIN_SIZE 2
00188 //  #define ELECTRON_BIN_SIZE 2
00189 //  #define VETO 2
00190 //  #define FAST_CHAIN 4
00191     
00192     
00193     /*
00194     @brief:     read one uint16_t equivalent of first two chars from the stream. short int because 16 bits
00195     @param:     pointer to the start of the short int
00196     @return:    uint16_t 
00197     */
00198     
00199     
00200     unsigned int read_2byte(unsigned char* ptr){
00201         unsigned int output = (unsigned int) *(ptr+1);
00202         output += ( (unsigned int)(*ptr) ) << 8;
00203         return output;
00204     }
00205     
00206     /*
00207     @brief:     read one int equivalent of first four chars from the stream.  int because 32 bits
00208     @param:     pointer to the start of the short int
00209     @return:    unsigned int  
00210     */
00211 
00212     unsigned int read_4byte(unsigned char* ptr){
00213         unsigned int output = (unsigned int) *(ptr+3);
00214         output += (unsigned int)*(ptr+2)<<8;
00215         output += (unsigned int)*(ptr+1)<<16;
00216         output += (unsigned int)*(ptr)<<24;
00217         return output;
00218     }
00219 
00220     unsigned int SFP_thresholds[35]={0  ,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100};//threashold values 
00221     unsigned int SCP_thresholds[35]={0  ,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,0  ,0  ,0  };
00222     unsigned int SFP_bin[35];
00223     unsigned int SCP_bin[35]={0};
00224     unsigned char SFP_outputBT[23];                         //BT = below threshold
00225     unsigned char SFP_outputAT[35];
00226     unsigned char SCP_outputLCR[22];
00227     unsigned char SCP_outputHCR[26];
00228 
00229     
00230 //**********************************************************************************************************************************************************************
00231     //lots of compression functions are listed below
00232 
00233     
00234     unsigned char SFP_compress4_BT(unsigned int input){                                 //for veto
00235         int de_4 = 0;
00236         unsigned char output;
00237         
00238         if(input<= 3){
00239         //  DE = 0;
00240             output = 0x0;
00241             de_4 = 0;
00242         }
00243         else if(input <= 12){
00244         //  DE = 01;
00245             output = 0x1;
00246             de_4 = 2;   
00247         }
00248         else if(input <= 48){
00249         //  DE = 10
00250             output = 0x2;
00251             de_4 = 4;   
00252         }
00253         else {
00254         //  DE = 11
00255             output = 0x3;
00256             de_4 = 6;
00257         }
00258         
00259         unsigned short int temp = input >> de_4;
00260         output += (temp ) << 2;
00261         return output;
00262     }
00263     
00264     
00265     unsigned char SFP_compress5_BT(unsigned int input){
00266         int de_4 = 0;                                   //number by which bin value need to be shifted
00267         unsigned char output;
00268         
00269         if(input <= 15){
00270 //            D = 0 [0]
00271             output = 0x0;
00272             de_4 = 0;
00273         }
00274         else if(input <= 60){
00275 //            D = 1
00276             output = 0x1;
00277             de_4 = 2;
00278         }
00279         
00280         unsigned short int temp = input >> de_4;
00281         output += (temp ) << 1;
00282         
00283         return output;      
00284     };
00285     
00286     
00287     unsigned char SFP_compress6_BT(unsigned int input){
00288         int de_4 = 0;;
00289         unsigned char output;
00290         
00291         if(input <= 31){
00292 //          E = 0 [0]
00293             output = 0x0;
00294             de_4 = 0;
00295         }
00296         else if(input <= 124){
00297 //            E = 1
00298             output = 0x1;
00299             de_4 = 2;
00300         }
00301          
00302         
00303         unsigned short int temp = input >> de_4;
00304         output += (temp ) << 1;
00305         
00306         return output;
00307     };
00308     
00309     unsigned char SFP_compress7_AT(unsigned int input){
00310         int de_4  = 0;
00311         unsigned char output;
00312         
00313         if(input <= 31){
00314 //            DE = 00 [0]
00315             output = 0x0;
00316             de_4 = 0;
00317         }
00318         else if(input <= 124){
00319 //            DE = 01 [1]
00320             output = 0x1;
00321             de_4 = 2;
00322         }
00323         
00324         else if(input <= 496){
00325 //            DE = 10 [2]
00326             output = 0x2;
00327             de_4 = 4;
00328         }
00329         
00330         else if(input <= 1984){
00331 //           DE = 11 [3]
00332             output = 0x3;
00333             de_4 = 6;
00334         }
00335         
00336         unsigned short int temp = input >> de_4;
00337         output += (temp ) << 2;
00338         
00339         return output;
00340         
00341     };
00342     
00343     
00344     unsigned char SFP_compress8_AT(unsigned int input){
00345         
00346         int de_4 = 0;;
00347         unsigned char output;
00348         
00349         if(input <= 63){
00350 //            DE = 00 [0]
00351             output = 0x0;
00352             de_4 = 0;
00353         }
00354         else if(input <= 252){
00355 //            DE = 01 [1]
00356             output = 0x1;
00357             de_4 = 2;
00358         }
00359         
00360         else if(input <= 1008){
00361 //            DE = 10 [2]
00362             output = 0x2;
00363             de_4 = 4;
00364         }
00365         
00366         else {
00367 //           DE = 11 [3]
00368             output = 0x3;
00369             de_4 = 6;
00370         }
00371         
00372         unsigned short int temp = input >> de_4;
00373         output += (temp ) << 2;
00374         
00375         return output;
00376     };
00377     
00378     unsigned char SFP_compress5_AT(unsigned int input){
00379         int de_4 = 0;;
00380         unsigned char output;
00381         
00382         if(input <= 3){
00383 //            DE = 000 [0]
00384             output = 0x0;
00385             de_4 = 0;
00386         }
00387         else if(input <= 12){
00388 //            DE = 001 [1]
00389             output = 0x1;
00390             de_4 = 2;
00391         }
00392         
00393         else if(input <= 48){
00394 //            DE = 010 [2]
00395             output = 0x2;
00396             de_4 = 4;
00397         }
00398         
00399         else if(input <= 192) {
00400 //           DE = 011 [3]
00401             output = 0x3;
00402             de_4 = 6;
00403         }
00404         
00405         else if(input <= 768) {
00406 //           DE = 100 [4]
00407             output = 0x4;
00408             de_4 = 8;
00409         }
00410         
00411         else if(input <= 3072) {
00412 //           DE = 101 [5]
00413             output = 0x5;
00414             de_4 = 10;
00415         }
00416         
00417         else if(input <= 12288) {
00418 //           DE = 110 [6]
00419             output = 0x6;
00420             de_4 = 12;
00421         }
00422         
00423         else  {
00424 //          DE = 111 [7]
00425             output = 0x7;
00426             de_4 = 14;
00427         }
00428            
00429         unsigned short int temp = input >> de_4;
00430         output += (temp ) << 3;
00431         
00432         return output;
00433     }
00434     
00435     unsigned char SFP_compress7FC_AT(unsigned int input){                   // for fast chain above threshold
00436         int de_4 = 0;;
00437         unsigned char output;
00438         
00439         if(input <= 15){
00440 //            DE = 000 [0]
00441             output = 0x0;
00442             de_4 = 0;
00443         }
00444         else if(input <= 60){
00445 //            DE = 001 [1]
00446             output = 0x1;
00447             de_4 = 2;
00448         }
00449         
00450         else if(input <= 240){
00451 //            DE = 010 [2]
00452             output = 0x2;
00453             de_4 = 4;
00454         }
00455         
00456         else if(input <= 960) {
00457 //           DE = 011 [3]
00458             output = 0x3;
00459             de_4 = 6;
00460         }
00461         
00462         else if(input <= 3840) {
00463 //           DE = 100 [4]
00464             output = 0x4;
00465             de_4 = 8;
00466         }
00467         
00468         else if(input <= 15360) {
00469 //           DE = 101 [5]
00470             output = 0x5;
00471             de_4 = 10;
00472         }
00473         
00474         else if(input <= 61440) {
00475 //           DE = 110 [6]
00476             output = 0x6;
00477             de_4 = 12;
00478         }
00479         
00480         else  {
00481 //          DE = 111 [7]
00482             output = 0x7;
00483             de_4 = 14;
00484         }
00485            
00486         unsigned short int temp = input >> de_4;
00487         output += (temp ) << 3;
00488         
00489         return output;
00490     }
00491     
00492     
00493     
00494     //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00495     unsigned char SCP_compress6(unsigned int input){
00496         int ef_4;
00497         unsigned char output;
00498         
00499         if(input <= 15){
00500 //      EF = 00
00501         output = 0x0;
00502         ef_4 = 0;
00503         }
00504         
00505         else if(input <= 60 ){
00506 //          EF = 01 [1]
00507             output = 0x01;
00508             ef_4 = 2;
00509         }
00510         else if(input <= 240){
00511 //          EF = 10 [2]
00512             output = 0x02;
00513             ef_4 = 4;
00514         }
00515         else{
00516 //          EF = 11 [3]
00517             output = 0x03;
00518             ef_4 = 6;
00519         }
00520             
00521             unsigned short int temp = input >> ef_4;
00522             output += (temp & 0xf) << 2;
00523             
00524             return output;
00525     }
00526     
00527     unsigned char SCP_compress5(unsigned int input){
00528         
00529         int de_4 = 0;;
00530         unsigned char output;
00531         
00532         if(input <= 7){
00533 //            DE = 00 [0]
00534             output = 0x0;
00535             de_4 = 0;
00536         }
00537         
00538         else if(input <= 28){
00539 //            DE = 01 [1]
00540             output = 0x01;
00541             de_4 = 2;
00542         }
00543         else if(input <= 112){
00544 //            DE = 10 [2]
00545             output = 0x02;
00546             de_4 = 4;
00547         }
00548         else{
00549 //        DE = 11 [3]
00550             output = 0x03;
00551             de_4 = 6;
00552         }
00553         
00554         unsigned short int temp = input >> de_4;
00555         output += (temp & 0x7) << 2;
00556         
00557         return output;
00558     }
00559     
00560         unsigned char SCP_compress6h(unsigned int input) {
00561     
00562         int ef_4;
00563         unsigned char output;
00564         
00565         if(input <=7){
00566 //          EF = 000 [0]
00567             output = 0x00;
00568             ef_4 = 0;
00569         }
00570         
00571         else if(input <=28){
00572 //          EF = 001 [1]
00573             output = 0x01;
00574             ef_4 = 2;
00575         }
00576         else if(input <= 112){
00577         
00578 //          EF = 010 [2]
00579             output = 0x02;
00580             ef_4 = 4;
00581         }
00582         else if(input <= 448){
00583 //          EF = 011 [3]
00584             output = 0x03;
00585             ef_4 = 6;
00586         }
00587         else if(input <= 1792){
00588 //          EF = 100 [4]
00589             output = 0x04;
00590             ef_4 = 8;
00591             
00592         }
00593         else if(input <= 7168){
00594 //          EF = 101 [5]
00595             output = 0x05;
00596             ef_4 = 10;
00597             
00598         }
00599         else if(input <= 28672){
00600 //          EF = 110 [6]
00601             output = 0x06;
00602             ef_4 = 12;
00603         }
00604         else{
00605 //          EF = 111 [7]
00606             output = 0x07;
00607             ef_4 =14;
00608         }
00609         
00610         unsigned short int temp = input >> ef_4;
00611         output += (temp & 0x7) << 3;
00612         
00613         return output;
00614         
00615     }
00616     
00617     
00618     unsigned char SCP_compress7h(unsigned int input) {
00619     
00620         int fg_4;
00621         unsigned char output;
00622         
00623         if(input <= 15){
00624 //          EF = 000 [0]
00625             output = 0x0;
00626             fg_4 = 0;
00627         }
00628         
00629         else if(input <= 60){
00630 //          EF = 001 [1]
00631             output = 0x01;
00632             fg_4 = 2;
00633         }
00634         else if(input <= 240){
00635         
00636 //          EF = 010 [2]                    
00637             output = 0x02;
00638             fg_4 = 4;
00639         }
00640         else if(input <= 960){
00641 //          EF = 011 [3]
00642             output = 0x03;
00643             fg_4 = 6;
00644         }
00645         else if(input <= 3840){
00646 //          EF = 100 [4]
00647             output = 0x04;
00648             fg_4 = 8;
00649             
00650         }
00651         else if(input <= 15360){
00652 //          EF = 101 [5]
00653             output = 0x05;
00654             fg_4 = 10;
00655             
00656         }
00657         else if(input <= 61440){
00658 //          EF = 110 [6]
00659             output = 0x06;
00660             fg_4 = 12;
00661         }
00662         else{
00663 //          EF = 111 [7]
00664             output = 0x07;
00665             fg_4 =14;
00666         }
00667         
00668         unsigned short int temp = input >> fg_4;
00669         output += (temp & 0xf) << 3;
00670         
00671         return output;
00672         
00673     }
00674     //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00675     void SCP_compress_data();                   
00676     void SFP_compress_data(unsigned char* input){
00677 
00678             bool LCR = true;
00679             int i = 0;
00680             static int packet_no = 0;                       //takes value from 0 to 29
00681             //TRAVERSE THE LIST TO DETERMINE LCR OR HCR and stroing the values in proton_bin and electron bin
00682             SFP_bin[0] = *input;
00683             for(i=1 ; i<= NUM_PROTON_BIN + NUM_ELECTRON_BIN + VETO ; ++i){          //storing the bin values into an array name bin
00684                 SFP_bin[i]=read_2byte(input+1+((i-1)<<1));                             //proton bin and elecron bin are 2 byte, hence read_2byte and 
00685                 SCP_bin[i]+=SFP_bin[i];
00686                 if(SFP_bin[i] > SFP_thresholds[i]){                                         //fast cahin is 4 byte hence read_4byte
00687                     LCR = false;                                                    // if a single value is above threshold then lcr becomes false
00688                     i++;
00689                     break;
00690                 }
00691             }
00692             
00693             for( ; i<= NUM_PROTON_BIN + NUM_ELECTRON_BIN + VETO ; ++i){
00694                 SCP_bin[i]+=SFP_bin[i];
00695                 SFP_bin[i] = read_2byte(input + 1 + ( (i-1)<<1) );
00696             }
00697             
00698             SFP_bin[i] = read_4byte(input+1+ ((i-1)<<1))    ;
00699             SCP_bin[i]+=SFP_bin[i];
00700             
00701             if(SFP_bin[i]>SFP_thresholds[i])
00702                 LCR = false;                                                    //since veto starts from location (input + 65) and (input + 69)
00703                     
00704             SFP_bin[i+1] = read_4byte(input+69);
00705             SCP_bin[i]+=SFP_bin[i];
00706             
00707             if(SFP_bin[i]>SFP_thresholds[i])
00708                 LCR = false;
00709             
00710             
00711 //          printf("\n");                       //for printing the sfp bin
00712 //          for (i=0;i<35;i++){
00713 //              printf("sfp[%d] = %d",i,SFP_bin[i]);
00714 //          }
00715 //          printf("\n");
00716 
00717             if(LCR){   
00718                 
00719                  SFP_outputBT[0]  =  (packet_no<<3) + ( SFP_compress5_BT(SFP_bin[1])>>2 );
00720                  SFP_outputBT[1]  =  ( SFP_compress5_BT(SFP_bin[1])<<6 ) + ( SFP_compress5_BT(SFP_bin[2])<<1 ) +   ( SFP_compress5_BT(SFP_bin[3])>>4 );
00721                  SFP_outputBT[2]  =  ( SFP_compress5_BT(SFP_bin[3])<<4 ) + ( SFP_compress5_BT(SFP_bin[4])>>1 );
00722                  SFP_outputBT[3]  =  ( SFP_compress5_BT(SFP_bin[4])<<7 ) + ( SFP_compress5_BT(SFP_bin[5])<<2 ) + ( SFP_compress5_BT(SFP_bin[6])>>3 );            
00723                  SFP_outputBT[4]  =  ( SFP_compress5_BT(SFP_bin[6])<<5 ) + ( SFP_compress5_BT(SFP_bin[7]) );
00724                  SFP_outputBT[5]  =  ( SFP_compress5_BT(SFP_bin[8])<<3 ) + ( SFP_compress5_BT(SFP_bin[9])>>2 );
00725                  SFP_outputBT[6]  =  ( SFP_compress5_BT(SFP_bin[9])<<6 ) + ( SFP_compress5_BT(SFP_bin[10])<<1 ) +  ( SFP_compress5_BT(SFP_bin[11])>>4 );
00726                  SFP_outputBT[7]  =  ( SFP_compress5_BT(SFP_bin[11])<<4 ) + ( SFP_compress5_BT(SFP_bin[12])>>1 );            
00727                  SFP_outputBT[8]  =  ( SFP_compress5_BT(SFP_bin[12])<<7 ) + ( SFP_compress5_BT(SFP_bin[13])<<2 ) + ( SFP_compress5_BT(SFP_bin[14])>>3 );
00728                  SFP_outputBT[9]  =  ( SFP_compress5_BT(SFP_bin[14])<<5 ) + ( SFP_compress5_BT(SFP_bin[15]) );
00729                  SFP_outputBT[10] =  ( SFP_compress5_BT(SFP_bin[16])<<3 ) + ( SFP_compress5_BT(SFP_bin[17])>>2 );
00730                  SFP_outputBT[11] =  ( SFP_compress5_BT(SFP_bin[17])<<6 ) + ( SFP_compress6_BT(SFP_bin[18]) );
00731                  SFP_outputBT[12] =  ( SFP_compress6_BT(SFP_bin[19])<<2 ) + ( SFP_compress6_BT(SFP_bin[20])>>4 );
00732                  SFP_outputBT[13] =  ( SFP_compress6_BT(SFP_bin[20])<<4 ) + ( SFP_compress5_BT(SFP_bin[21])>>1 );        
00733                  SFP_outputBT[14] =  ( SFP_compress5_BT(SFP_bin[21])<<7 ) + ( SFP_compress5_BT(SFP_bin[22])<<2 )  + ( SFP_compress5_BT(SFP_bin[23])>>3 );
00734                  SFP_outputBT[15] =  ( SFP_compress5_BT(SFP_bin[23])<<5 ) + ( SFP_compress5_BT(SFP_bin[24]) );
00735                  SFP_outputBT[16] =  ( SFP_compress5_BT(SFP_bin[25])<<3 ) + ( SFP_compress5_BT(SFP_bin[26])>>2 );
00736                  SFP_outputBT[17] =  ( SFP_compress5_BT(SFP_bin[26])<<6 ) + ( SFP_compress5_BT(SFP_bin[27])<<1 ) + ( SFP_compress5_BT(SFP_bin[28])>>4);
00737                  SFP_outputBT[18] =  ( SFP_compress5_BT(SFP_bin[28])<<4 ) + ( SFP_compress5_BT(SFP_bin[29])>>1) ;
00738                  SFP_outputBT[19] =  ( SFP_compress5_BT(SFP_bin[29])<<7 ) + ( SFP_compress5_BT(SFP_bin[30])<<2 ) + ( SFP_compress5_BT(SFP_bin[31])>>3)  ; 
00739                  SFP_outputBT[20] =  ( SFP_compress5_BT(SFP_bin[31])<<5 ) + ( SFP_compress4_BT(SFP_bin[32])<<1)  + ( SCP_compress5(SFP_bin[33])>>4 ) ; //
00740                  SFP_outputBT[21] =  ( SCP_compress5(SFP_bin[33])<<4 ) + ( SCP_compress5(SFP_bin[34])>>1 );        //here intentionally SCP_compress is used instead of SCP_compress5 is different than SFP_compress5
00741                  SFP_outputBT[22] =  ( SCP_compress5(SFP_bin[34])<<7 );                                             //7 bits are spare
00742                  
00743                  Science_TMframe::making_frame(3,'L',SFP_outputBT);
00744                  if(++packet_no == 30){
00745                     packet_no=0;
00746                     SCP_compress_data();
00747                     for(i=0;i<PACKET_SEQUENCE_COUNT + NUM_PROTON_BIN + NUM_ELECTRON_BIN + VETO + FASTCHAIN;i++){
00748                         if(packet_no==0){
00749                             SCP_bin[i]=0;
00750                         }
00751                     }
00752                  }
00753                  
00754                  
00755             }
00756 
00757             else {
00758                 
00759                 
00760                 SFP_outputAT[0]  = (RTC_TIME>>27)&(0xff);
00761                 SFP_outputAT[1]  = (RTC_TIME>>19)&(0xff);
00762                 SFP_outputAT[2]  = (RTC_TIME>>11)&(0xff);
00763                 SFP_outputAT[3]  = (RTC_TIME>>3 )&(0xff);
00764                 SFP_outputAT[4]  = (RTC_TIME<<5 )&(0xff) + (packet_no);
00765                 SFP_outputAT[5]  = ( SFP_compress7_AT(SFP_bin[1])<<1 ) + ( SFP_compress7_AT(SFP_bin[2])>>6 );
00766                 SFP_outputAT[6]  = ( SFP_compress7_AT(SFP_bin[2])<<2 ) + ( SFP_compress7_AT(SFP_bin[3])>>5 );
00767                 SFP_outputAT[7]  = ( SFP_compress7_AT(SFP_bin[3])<<3 ) + ( SFP_compress7_AT(SFP_bin[4])>>4 );
00768                 SFP_outputAT[8]  = ( SFP_compress7_AT(SFP_bin[4])<<4 ) + ( SFP_compress7_AT(SFP_bin[5])>>3 );
00769                 SFP_outputAT[9]  = ( SFP_compress7_AT(SFP_bin[5])<<5 ) + ( SFP_compress7_AT(SFP_bin[6])>>2 );
00770                 SFP_outputAT[10] = ( SFP_compress7_AT(SFP_bin[6])<<6 ) + ( SFP_compress7_AT(SFP_bin[7])>>1 );
00771                 SFP_outputAT[11] = ( SFP_compress7_AT(SFP_bin[7])<<7 ) + ( SFP_compress7_AT(SFP_bin[8]) );
00772                 SFP_outputAT[12] = ( SFP_compress7_AT(SFP_bin[9])<<1 ) + ( SFP_compress7_AT(SFP_bin[10])>>6);
00773                 SFP_outputAT[13] = ( SFP_compress7_AT(SFP_bin[10])<<2 ) + ( SFP_compress7_AT(SFP_bin[11])>>5);
00774                 SFP_outputAT[14] = ( SFP_compress7_AT(SFP_bin[11])<<3 ) + ( SFP_compress7_AT(SFP_bin[12])>>4);
00775                 SFP_outputAT[15] = ( SFP_compress7_AT(SFP_bin[12])<<4 ) + ( SFP_compress7_AT(SFP_bin[13])>>3);
00776                 SFP_outputAT[16] = ( SFP_compress7_AT(SFP_bin[13])<<5 ) + ( SFP_compress7_AT(SFP_bin[14])>>2);
00777                 SFP_outputAT[17] = ( SFP_compress7_AT(SFP_bin[14])<<6 ) + ( SFP_compress7_AT(SFP_bin[15])>>1);
00778                 SFP_outputAT[18] = ( SFP_compress7_AT(SFP_bin[15])<<7 ) + ( SFP_compress7_AT(SFP_bin[16]));
00779                 SFP_outputAT[19] = ( SFP_compress7_AT(SFP_bin[17])<<1 ) + ( SFP_compress8_AT(SFP_bin[18])>>7 );
00780                 SFP_outputAT[20] = ( SFP_compress8_AT(SFP_bin[18])<<1 ) + ( SFP_compress8_AT(SFP_bin[19])>>7 );
00781                 SFP_outputAT[21] = ( SFP_compress8_AT(SFP_bin[19])<<1 ) + ( SFP_compress8_AT(SFP_bin[20])>>7 );
00782                 SFP_outputAT[22] = ( SFP_compress8_AT(SFP_bin[20])<<1 ) + ( SFP_compress7_AT(SFP_bin[21])>>6 );
00783                 SFP_outputAT[23] = ( SFP_compress7_AT(SFP_bin[21])<<2 ) + ( SFP_compress7_AT(SFP_bin[22])>>5 );
00784                 SFP_outputAT[24] = ( SFP_compress7_AT(SFP_bin[22])<<3 ) + ( SFP_compress7_AT(SFP_bin[23])>>4 );
00785                 SFP_outputAT[25] = ( SFP_compress7_AT(SFP_bin[23])<<4 ) + ( SFP_compress7_AT(SFP_bin[24])>>3 );
00786                 SFP_outputAT[26] = ( SFP_compress7_AT(SFP_bin[24])<<5 ) + ( SFP_compress7_AT(SFP_bin[25])>>2 );
00787                 SFP_outputAT[27] = ( SFP_compress7_AT(SFP_bin[25])<<6 ) + ( SFP_compress7_AT(SFP_bin[26])>>1 );
00788                 SFP_outputAT[28] = ( SFP_compress7_AT(SFP_bin[26])<<7 ) + ( SFP_compress7_AT(SFP_bin[27]) );
00789                 SFP_outputAT[29] = ( SFP_compress7_AT(SFP_bin[28])<<1 ) + ( SFP_compress7_AT(SFP_bin[29])>>6);
00790                 SFP_outputAT[30] = ( SFP_compress7_AT(SFP_bin[29])<<2 ) + ( SFP_compress7_AT(SFP_bin[30])>>5);
00791                 SFP_outputAT[31] = ( SFP_compress7_AT(SFP_bin[30])<<3 ) +( SFP_compress7_AT(SFP_bin[31])>>4);  
00792                 SFP_outputAT[32] = ( SFP_compress7_AT(SFP_bin[31])<<4 ) +( SFP_compress5_AT(SFP_bin[32])>>1); 
00793                 SFP_outputAT[33] = ( SFP_compress5_AT(SFP_bin[32])<<7 ) +( SFP_compress7FC_AT(SFP_bin[33])); 
00794                 SFP_outputAT[34] =  ( SFP_compress7FC_AT(SFP_bin[34])<<1 );                                             // 1 bit is spare          
00795                 
00796                 Science_TMframe::making_frame(2,'H',SFP_outputAT);
00797                 if(++packet_no == 30){
00798                     packet_no=0;
00799                     SCP_compress_data();
00800                     for(i=0;i<PACKET_SEQUENCE_COUNT + NUM_PROTON_BIN + NUM_ELECTRON_BIN + VETO + FASTCHAIN;i++){
00801                         if(packet_no==0){
00802                             SCP_bin[i]=0;
00803                         }
00804                     }
00805                 }
00806                             
00807             }
00808 
00809     }
00810 
00811 
00812 /*
00813     brief: takes the  pointer of the raw data string and return the address of the array which stores the address of 30 packets.
00814     input: pointer to the raw data.
00815     output : void
00816 */
00817 
00818     void complete_compression(unsigned char *SRP,uint64_t x){
00819         RTC_TIME = x;
00820         int i;                  //30 times because 3 second data
00821     
00822         for(i=0;i<30;i++){
00823          SFP_compress_data(SRP + 73*i); 
00824         }
00825 
00826     }
00827 
00828 
00829 
00830 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00831 
00832     
00833     
00834     /*
00835     @brief:     compresses the given input stream and return output packet
00836     @param:     pointer to input stream. Input stream always has the fixed size of RAW_PACKET_LENGTH
00837     @return:    pointer to output stream. Output stream has the size of 22 or 26 bytes
00838     */
00839     void SCP_compress_data(){
00840         
00841         bool LCR = true;
00842         int i = 0;
00843         
00844         for(i=1;i<=PACKET_SEQUENCE_COUNT + NUM_PROTON_BIN + NUM_ELECTRON_BIN + VETO + FASTCHAIN ;i++){
00845             if(SCP_bin[i]>SCP_thresholds[i]){
00846             LCR = false;
00847             break;
00848             }
00849         }
00850 //      printf("\n");                    //for printing the scp bin
00851 //          for (i=0;i<35;i++){
00852 //              printf(" scp[%d] = %d    ",i,SCP_bin[i]);
00853 //          }
00854 //          printf("\n");         
00855         // compressing the data
00856         if(LCR){   
00857              SCP_outputLCR[0] = (RTC_TIME>>5)&(0xff);                //first 13 bits for time tagging
00858              SCP_outputLCR[1] = (RTC_TIME<<3)&(0xff);                //then 4 bits for attitude tag
00859              SCP_outputLCR[2] = 0x00;                                //only attitude tag is left
00860              SCP_outputLCR[2] += ( SCP_compress5(SCP_bin[0])<<1 ) + ( SCP_compress5(SCP_bin[1])>>4 );
00861              SCP_outputLCR[3] = ( SCP_compress5(SCP_bin[1])<<4 ) + ( SCP_compress5(SCP_bin[2])>>1 );
00862              SCP_outputLCR[4] = ( SCP_compress5(SCP_bin[2])<<7 ) + ( SCP_compress5(SCP_bin[3])<<2 ) + ( SCP_compress5(SCP_bin[4])>>3 );
00863              SCP_outputLCR[5] = ( SCP_compress5(SCP_bin[4])<<5 ) + ( SCP_compress5(SCP_bin[5]) );            
00864              SCP_outputLCR[6] = ( SCP_compress5(SCP_bin[6])<<3 ) + ( SCP_compress5(SCP_bin[7])>>2 );
00865              SCP_outputLCR[7] = ( SCP_compress5(SCP_bin[7])<<6 ) + ( SCP_compress5(SCP_bin[8])<<1 ) +  ( SCP_compress5(SCP_bin[9])>>4 );
00866              SCP_outputLCR[8] = ( SCP_compress5(SCP_bin[9])<<4 ) + ( SCP_compress5(SCP_bin[10])>>1 );
00867              SCP_outputLCR[9] = ( SCP_compress5(SCP_bin[10])<<7 ) + ( SCP_compress5(SCP_bin[11])<<2) + ( SCP_compress5(SCP_bin[12])>>3 );            
00868              SCP_outputLCR[10] = ( SCP_compress5(SCP_bin[12])<<5 ) + ( SCP_compress5(SCP_bin[13]) );
00869              SCP_outputLCR[11] = ( SCP_compress5(SCP_bin[14])<<3 ) + ( SCP_compress5(SCP_bin[15])>>2 );
00870              SCP_outputLCR[12] = ( SCP_compress5(SCP_bin[15])<<6 ) + ( SCP_compress5(SCP_bin[16])<<1) +  ( SCP_compress6(SCP_bin[17])>>5 );
00871              SCP_outputLCR[13] = ( SCP_compress6(SCP_bin[17])<<3 ) + ( SCP_compress6(SCP_bin[18])>>3 );
00872              SCP_outputLCR[14] = ( SCP_compress5(SCP_bin[18])<<5 ) + ( SCP_compress6(SCP_bin[19])>>1 );
00873              SCP_outputLCR[15] = ( SCP_compress6(SCP_bin[19])<<7 ) + ( SCP_compress5(SCP_bin[20])<<2 ) + ( SCP_compress5(SCP_bin[21])>>3 );          
00874              SCP_outputLCR[16] = ( SCP_compress5(SCP_bin[21])<<5 ) + ( SCP_compress5(SCP_bin[22]) );
00875              SCP_outputLCR[17] = ( SCP_compress5(SCP_bin[23])<<3 ) + ( SCP_compress5(SCP_bin[24])>>2 );
00876              SCP_outputLCR[18] = ( SCP_compress5(SCP_bin[24])<<6 ) + ( SCP_compress5(SCP_bin[25])<<1) +  ( SCP_compress5(SCP_bin[26])>>4 );
00877              SCP_outputLCR[19] = ( SCP_compress5(SCP_bin[26])<<4 ) + ( SCP_compress5(SCP_bin[27])>>1 );
00878              SCP_outputLCR[20] = ( SCP_compress5(SCP_bin[27])<<7 ) + ( SCP_compress5(SCP_bin[28])<<2 ) + ( SCP_compress5(SCP_bin[29])>>3 ); 
00879              SCP_outputLCR[21] = ( SCP_compress5(SCP_bin[29])<<5 ) + ( SCP_compress5(SCP_bin[30]) );
00880          
00881              Science_TMframe::making_frame(1,'L',SCP_outputLCR);
00882         }
00883         else{
00884             
00885             SCP_outputLCR[0] = (RTC_TIME>>5)&(0xff);                //first 13 bits for time tagging
00886             SCP_outputLCR[1] = (RTC_TIME<<3)&(0xff);
00887             SCP_outputHCR[2] = 0x40;
00888             SCP_outputHCR[2] += ( SCP_compress6h(SCP_bin[0])<<6 ) ;
00889             SCP_outputHCR[3] = ( SCP_compress6h(SCP_bin[1])<<2 ) + ( SCP_compress6h(SCP_bin[2])>>4) ;
00890             SCP_outputHCR[4] = ( SCP_compress6h(SCP_bin[2])<<4 ) + ( SCP_compress6h(SCP_bin[3])>>2) ;
00891             SCP_outputHCR[5] = ( SCP_compress6h(SCP_bin[3])<<6 ) + ( SCP_compress6h(SCP_bin[4])) ;
00892             SCP_outputHCR[6] = ( SCP_compress6h(SCP_bin[5])<<2 ) + ( SCP_compress6h(SCP_bin[6])>>4) ;
00893             SCP_outputHCR[7] = ( SCP_compress6h(SCP_bin[6])<<4 ) + ( SCP_compress6h(SCP_bin[7])>>2) ;
00894             SCP_outputHCR[8] = ( SCP_compress6h(SCP_bin[7])<<6 ) + ( SCP_compress6h(SCP_bin[8])) ;
00895             SCP_outputHCR[9] = ( SCP_compress6h(SCP_bin[9])<<2 ) + ( SCP_compress6h(SCP_bin[10])>>4) ;
00896             SCP_outputHCR[10] = ( SCP_compress6h(SCP_bin[10])<<4 ) + ( SCP_compress6h(SCP_bin[11])>>2);
00897             SCP_outputHCR[11] = ( SCP_compress6h(SCP_bin[11])<<6 ) + ( SCP_compress6h(SCP_bin[12])) ;
00898             SCP_outputHCR[12] = ( SCP_compress6h(SCP_bin[13])<<2 ) + ( SCP_compress6h(SCP_bin[14])>>4)  ;
00899             SCP_outputHCR[13] = ( SCP_compress6h(SCP_bin[14])<<4 ) + ( SCP_compress6h(SCP_bin[15])>>2)  ;
00900             SCP_outputHCR[14] = ( SCP_compress6h(SCP_bin[15])<<6 ) + ( SCP_compress6h(SCP_bin[16]))     ;
00901             SCP_outputHCR[15] = ( SCP_compress7h(SCP_bin[17])<<1 ) + ( SCP_compress7h(SCP_bin[18])>>6)  ;
00902             SCP_outputHCR[16] = ( SCP_compress7h(SCP_bin[18])<<2 ) + ( SCP_compress7h(SCP_bin[19])>>5)  ;
00903             SCP_outputHCR[17] = ( SCP_compress7h(SCP_bin[19])<<3 ) + ( SCP_compress6h(SCP_bin[20])>>3)  ;
00904             SCP_outputHCR[18] = ( SCP_compress6h(SCP_bin[20])<<5 ) + ( SCP_compress6h(SCP_bin[21])>>1)  ;
00905             SCP_outputHCR[19] = ( SCP_compress6h(SCP_bin[21])<<7 ) + ( SCP_compress6h(SCP_bin[22])<<1) +  ( SCP_compress6h(SCP_bin[23])>>5) ;
00906             SCP_outputHCR[20] = ( SCP_compress6h(SCP_bin[23])<<3 ) + ( SCP_compress6h(SCP_bin[24])>>3)  ;
00907             SCP_outputHCR[21] = ( SCP_compress6h(SCP_bin[24])<<5 ) + ( SCP_compress6h(SCP_bin[25])>>1)  ;
00908             SCP_outputHCR[22] = ( SCP_compress6h(SCP_bin[25])<<7 ) + ( SCP_compress6h(SCP_bin[26])<<1) +  ( SCP_compress6h(SCP_bin[27])>>5) ;
00909             SCP_outputHCR[23] = ( SCP_compress6h(SCP_bin[27])<<3 ) + ( SCP_compress6h(SCP_bin[28])>>3)  ;
00910             SCP_outputHCR[24] = ( SCP_compress6h(SCP_bin[28])<<5 ) + ( SCP_compress6h(SCP_bin[29])>>1)  ;
00911             SCP_outputHCR[25] = ( SCP_compress6h(SCP_bin[29])<<7 ) + ( SCP_compress6h(SCP_bin[30])<<1)  ; //last bit is empty
00912         
00913         /*  for (i=0;i<26;i++){
00914                 printf("\nscp[%d] = %d",i,SCP_outputHCR[i]);
00915             }*/
00916             Science_TMframe::making_frame(1,'H',SCP_outputHCR);
00917         }
00918     }
00919     
00920 }