Repository for CDMS code

Dependencies:   SimpleDMA mbed-rtos mbed eeprom

Fork of COM_MNG_TMTC_SIMPLE by Shreesha S

Files at this revision

API Documentation at this revision

Comitter:
ee12b079
Date:
Sun Jan 24 09:30:22 2016 +0000
Parent:
92:bce22b38c440
Child:
94:611c066467a1
Commit message:
Science Thread integrated, to be tested,

Changed in this revision

Compression.h Show annotated file Show diff for this revision Revisions of this file
DefinitionsAndGlobals.h Show annotated file Show diff for this revision Revisions of this file
ThreadsAndFunctions.h Show annotated file Show diff for this revision Revisions of this file
cdms_rtc.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/Compression.h	Sat Jan 23 12:59:06 2016 +0000
+++ b/Compression.h	Sun Jan 24 09:30:22 2016 +0000
@@ -1,936 +1,572 @@
-int disk_write(const uint8_t *, uint64_t);
-uint64_t  RTC_TIME;                                    //need to  be changed to uint_64
-unsigned char SDcard_lastWritten[512]  = {0};
- 
-namespace Science_TMframe {
-    
-    #define OUTLENGTH 360                                           //length of the output frame after convolution
-    #define SDcard_block 512                                        //block size of the sd card
-    
-    Convolution ConvObj;                                            //object which stores the frame after convolution
-    bool fresh[3] = {true,true,true};                               // True only for the first time                                                                 
-    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.
-    unsigned int FCN[4] = {0};                                      //frame count number
-    unsigned int data_starting_point[3] = {8,5,10};             
-    unsigned int max_data[3] = {124,127,122};                       //number of bytes in each frame excluding TMID,FCN,first_header_point,crc
-    unsigned char TM_convoluted_data[270] = {0};                    //270 bytes is the size after convolution of 1072 bits
-    unsigned char complete_frame[SDcard_block] = {0};
-    uint64_t  SDC_address = 200;
-    bool SCH_FCCH_FLAG = true;
-    
+unsigned int read_2byte(unsigned char* ptr){
+    unsigned int output = (unsigned int) *(ptr+1);
+    output += ( (unsigned int)(*ptr) ) << 8;
+    return output;
+}
     
-    void add_SCH_FCCH(){
-        int i = 0;
-        complete_frame[0] = 0x0a;
-        complete_frame[1] = 0x3f;;
-        complete_frame[2] = 0x46;
-        complete_frame[3] = 0xb4;
-        complete_frame[4] = 0x00;
-        
-        for(i = 149 ; i < 159 ; i ++){
-            complete_frame[i] = 0 ; 
+unsigned int read_4byte(unsigned char* ptr){
+    unsigned int output = (unsigned int) *(ptr+3);
+    output += (unsigned int)*(ptr+2)<<8;
+    output += (unsigned int)*(ptr+1)<<16;
+    output += (unsigned int)*(ptr)<<24;
+    return output;
+}
+
+int adjust(int size, unsigned int data, unsigned char* ptr , int space){
+    space = space&0x0f;
+    data = data&((1<<size)-1);
+    if(space >= size){
+        *ptr += data<<(space-size);
+        if(space - size == 0){
+            return 0x18;
+        }else{
+            return space-size;
         }
-        
-        complete_frame[159] = 0x0a;
-        complete_frame[160] = 0x3f;;
-        complete_frame[161] = 0x46;
-        complete_frame[162] = 0xb4;
-        complete_frame[163] = 0x00;
-        
-        for(i = 308 ; i < 318 ; i ++){
-            complete_frame[i] = 0 ; 
+    }else{
+        ptr[0] += data>>(size-space);
+        ptr[1] = (data<<(8-(size-space)))&0xff ;
+        return 0x10 + 8-(size - space);
+    }
+}
+
+
+int compress (int data, int x, int y){  //to be compressed with scheme (msb x)*4^y ;
+    for(int i = 0 ; i < 1<<y ; i++){
+        if(data <= ( (1<<x)-1) * (1<<(2*i)) ){
+            return ( ((data>>i*2)<<y) + i);
         }
-        
+    }
+    if ( data > ( (1<<x)-1) * (1<<(2*((1<<y)-1))) ){
+//      cout <<"compression exception"<<endl;
+        return 0;
+    }
+}
+
+
+
+//variable declerations
+unsigned char srp_mode , at , pzf , ezf ,sfp[65] ,scp[55];; //tells which mode is calibrated or plot 0 for calibrated and 1 for scatterered, below threshold , proton zero flux, electron zero flux.
+unsigned int sfp_bin[52] , scp_bin[52];         //storing the bin values.
+unsigned int sfp_threshold_m0[52] = {8128,8128,8128,8128,8128,8128,8128,8128,960,960,960,960,960,960,960,960,960,960,960,960,4032,4032,4032,4032,8128,8128,8128,8128,4032,4032,124,124,60,60,60,60,60,60,60,60,60,60,60,60,60,60,60,252,252,252,252,252}; 
+const unsigned int scp_threshold_m0[52] = {245760,245760,245760,245760,245760,245760,245760,245760,49152,49152,49152,49152,49152,49152,49152,49152,49152,49152,49152,49152,114688,114688,114688,114688,245760,245760,245760,245760,114688,114688,4032,4032,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,8128,4032,4032,4032,4032};
+unsigned int scp_threshold_m0_1[9]={114688,245760,245760,245760,245760,114688,114688,65472,65472} ; //for mode 0..ie callibrated mode
+const unsigned int sfp_threshold_m1[2] ={252,8128},scp_threshold_m1[2]={7680,245760};
+unsigned char frames[3][134] = {0};
+unsigned char position_tm_frame[3] = {8,11,5} , position_tm_starting[3] = {8,11,5}; //{sc,sf-bt,sf-at}
+unsigned char id;   //sf = 0,sc-at = 1,sc-bt = 2;
+unsigned char TM_interleave_data[512] , TM_convoluted_data[270] = {0};
+int proton_scp_sum,electron_scp_sum,length, temp_crc,attitude = 0,FSC_science = 0,debug_cntr = 0, size,space;   
+unsigned char *ptr ,* pointer;
+long long int sci_time = 0x221000000;       //call some function and get the time or get in the starting, what ever akshay says
+
+    Convolution ConvObj;
+    void convolution (unsigned char * ptr){
+        ConvObj.convolutionEncode(ptr, TM_convoluted_data);
+        ConvObj.convolutionEncode(ptr + 67, TM_convoluted_data + 135);
+    }
+
+//give the pointer of 6 second data to this function
+void srp(unsigned char * head){
+    sci_time = FCTN_CDMS_RD_RTC();
+    for (int i = 0; i < 52 ; i++){
+        scp_bin[i] = 0;
     }
-    
-    void making_frameHeader(unsigned char  TMID){
-    
-        unsigned char frame_type_identifier = 0;                                                        // not conform about the values , yet to be done    
-        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
-        frames[TMID][1] = ((FCN[TMID]>>16) & 0xff );
-        frames[TMID][2] = ( (FCN[TMID]>>8 )& 0xff );
-        frames[TMID][3] = ( FCN[TMID] & 0xff      );                                                        // first bit for (frame identifier), next 4 for (TMID) and next 27 for FCN
-        
-        if(TMID == 0){
-            frames[TMID][5] =( (RTC_TIME>>29) & 0xff );
-            frames[TMID][6] =( (RTC_TIME>>21) & 0xff ); 
-            frames[TMID][7] =( (RTC_TIME>>13) & 0xff );
-            
-        }else if(TMID == 2){
-            frames[TMID][5] =( (RTC_TIME>>32) & 0xff );
-            frames[TMID][6] =( (RTC_TIME>>24) & 0xff ); 
-            frames[TMID][7] =( (RTC_TIME>>16) & 0xff );
-            frames[TMID][8] =( (RTC_TIME>>8 ) & 0xff );
-            frames[TMID][9] =( (RTC_TIME    ) & 0xff );
+    ptr = head + 3;
+    srp_mode = head[2]&0x1;
+    at = 0;     //above threshold is false
+    for(int counter = 0 ; counter < 60 ; counter++){
+        pzf = 1;    ezf = 1;
+        if(srp_mode == 0){      //calibrated mode
+            for(int i=0; i<48 ; i++){
+                sfp_bin[i] = read_2byte(ptr + i*2);
+                scp_bin[i] += sfp_bin[i];
+                if(sfp_bin[i]>sfp_threshold_m0[i]){
+                    at = 1;
+                }
+                if(i<17){
+                    if(sfp_bin[i] > 0)
+                        pzf = 0;
+                }
+                else if (i>17 && i < 23){
+                    if(sfp_bin[i]>0)
+                        ezf = 0;
+                }
+            }
+            for(int i=0; i<4; i++){
+                sfp_bin[i+48] = read_4byte( (ptr+96) + 4*i );
+                scp_bin[i+48] += sfp_bin[i+48];
+                if(sfp_bin[i+48]>sfp_threshold_m0[i+48])
+                    at = 1;
+            }
+        }
+        else if(srp_mode == 1){ //scattered mode
+            for(int i = 0; i <32; i++){
+                sfp_bin[i] = read_2byte(ptr+2*i);
+                scp_bin[i] += sfp_bin[i];
+                if(sfp_bin[i] > sfp_threshold_m1[0])
+                    at = 1;
+            }
+            for(int i = 0; i < 4 ; i++){
+                sfp_bin[i+32] = read_4byte( (ptr+64) + 4*i );
+                scp_bin[i+32] += sfp_bin[i+32];
+                if(sfp_bin[i+32] > sfp_threshold_m1[1])
+                    at = 1;
+            }
         }
-        
-    }
- 
-    void convolution (unsigned char * ptr){
-    
-        ConvObj.convolutionEncode(ptr , TM_convoluted_data);
-        ConvObj.convolutionEncode(ptr + 67, TM_convoluted_data + 135);
- 
-    }
- 
-    /*
-        @brief : take the address of array of LCR or HCR and stores it into a frame
-        @parameters: type->L or H , deprnding on wheather it is LCR or HCR respectively
-        @return: nothing
-    */
- 
-//  type 2 yet to be done
-    void making_frame(unsigned char TMID ,unsigned char type, unsigned char* pointer){
+        ptr = ptr + 112;
+        for(int i = 0; i<65; i++)
+            sfp[i] = 0;
+        if(srp_mode == 0){      //calibrated mode
+            if(at == 0){
+                pointer = sfp; debug_cntr = 0;
+                space = adjust(4, attitude,pointer,8);  pointer += space>>4;    debug_cntr += space>>4;
+                space = adjust(6, counter,pointer,space);   pointer += space>>4;    debug_cntr += space>>4;
+                space = adjust(1, srp_mode,pointer,space);  pointer += space>>4;    debug_cntr += space>>4;
+                space = adjust(1, pzf,pointer,space);   pointer += space>>4;    debug_cntr += space>>4;
+                space = adjust(1, ezf,pointer,space);   pointer += space>>4;    debug_cntr += space>>4;
+                for(int i = 0 ; i < 8 ; i++){
+                    space = adjust(1, ((compress(sfp_bin[24+i],7,2))&0x100)>>8 ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+                    space = adjust(8, ((compress(sfp_bin[24+i],7,2))&0xff) ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+                }
+                for(int i = 0 ; i <12 ; i++){
+                    space = adjust(6, (compress(sfp_bin[32+i],4,2)) ,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
+                }
+                for(int i = 0; i < 4; i++){
+                    space = adjust(8, (compress(sfp_bin[44+i],6,2)) ,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
+                }
+                for(int i = 0 ; i < 4 ; i++){
+                    space = adjust(1, (compress(sfp_bin[48+i],7,2))>>8 ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+                    space = adjust(8, compress(sfp_bin[48+i],7,2) ,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
+                }
+                space = adjust(8, ((compress(sfp_bin[17],6,2))&0xff) ,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
+                space = adjust(8, ((compress(sfp_bin[23],6,2))&0xff) ,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
+                if(pzf == 0){
+                    for(int i = 0; i<2 ; i++){
+                        space = adjust(6, ((compress(sfp_bin[i],5,1))&0xff) ,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
+                    }
+                    for(int i = 0; i<15 ; i++){
+                        space = adjust(5, ((compress(sfp_bin[i+2],4,1))&0xff) ,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
+                    }
+                }
+                if(ezf == 0){
+                    for(int i = 0; i <5 ;i++){
+                        space = adjust(7, ((compress(sfp_bin[18+i],6,1))&0xff) ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+                    }
+                }
+            }//below thershold ends here.
         
-        TMID--;                                          //TMID goes from 1 to 3 , convinient to ue from 0 to 2
-        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
-        int  packet_len = 0;
-        int copy_count = 0 ;
-        
-        switch(int(TMID)){
-            case 0:                                     //SCP
-                if(type == 'L'){                        //below threshold
-                    packet_len = 22;   
+            if(at == 1){
+                pointer = sfp + 6; debug_cntr = 6;space = 8;
+                sfp[0] = (sci_time>>27)&0xff;   sfp[1] = (sci_time>>19)&0xff;   sfp[2] = (sci_time>>11)&0xff;   sfp[3] = (sci_time>>3)&0xff;
+                sfp[4] = ((sci_time&0x07)<<5) + ((attitude&0x0f)<<1) + (counter>>5);    
+                sfp[5] = ((counter&0x0f)<<3) + (srp_mode<<2);   
+                sfp[5] += (pzf<<1) + ezf ;
+                for(int i = 0 ; i < 8 ; i++){
+                    space = adjust(1, (compress(sfp_bin[24+i],7,2))>>8 ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+                    space = adjust(8, ((compress(sfp_bin[24+i],7,2))&0xff) ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
                 }
-                else if(type == 'H'){                   //above threshold
-                    packet_len = 26;
+                for(int i = 0 ; i <12 ; i++){
+                    space = adjust(6, ((compress(sfp_bin[32+i],3,3))&0xff) ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
                 }
-                break;
-            
-            case 1:                                     //SFP above threshold
-                packet_len = 35;
-                break;
-        
-            case 2:                                     //SFP  below threshold
-                packet_len = 23;
-                break;  
-        }
-        
-        if(SCH_FCCH_FLAG){
-            add_SCH_FCCH();
-            SCH_FCCH_FLAG = false;
-        }
- 
-        if(fresh[TMID]){
-            //welcome to first frame
-            making_frameHeader(TMID);
-            frames[TMID][4] = 0;
-            fresh[TMID] = false;
-        }
-    
-    
-        while(copy_count < packet_len){                                                                  // 22 bytes is the size of the LCR
-            frames[TMID][ frame_space_number[TMID] + data_starting_point[TMID] ]= *(pointer + copy_count);
-            frame_space_number[TMID]++;                                          
-            copy_count++;
-            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
-                FCN[TMID]++;
-                // convolution and save frame in the sd card
-                
-                // copying crc in 132 and 133
-                int temp_crc;
-                temp_crc = crc16_gen(frames[TMID],132);
-                frames[TMID][132] = temp_crc>>8;
-                frames[TMID][133] = temp_crc & 0xff;                
-                // xor 
-                for(int j = 0 ; j < 134 ; j++){
- //                   frames[TMID][j] = frames[TMID][j]^exorThisWithTMFrame[j];
+                for(int i = 0 ; i < 4 ; i++){
+                    space = adjust(2, (compress(sfp_bin[44+i],8,2))>>8 ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+                    space = adjust(8, compress(sfp_bin[44+i],8,2) ,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
                 }
-                //convolution and interleaving  
-                convolution(frames[TMID]);
-                interleave(TM_convoluted_data , complete_frame + 5);
-                interleave(TM_convoluted_data+ 135,complete_frame + 164);
-                
-                // writing the SDC_address in a buffer , to store it in SDcard at address 5
-                SDcard_lastWritten[0] = SDC_address>>56;
-                SDcard_lastWritten[1] = (SDC_address>>48)&0xFF;
-                SDcard_lastWritten[2] = (SDC_address>>40)&0xFF;
-                SDcard_lastWritten[3] = (SDC_address>>32)&0xFF;
-                SDcard_lastWritten[4] = (SDC_address>>24)&0xFF;
-                SDcard_lastWritten[5] = (SDC_address>>16)&0xFF;
-                SDcard_lastWritten[6] = (SDC_address>>8)&0xFF;
-                SDcard_lastWritten[7] = (SDC_address)&0xFF;
-                
-                SPI_mutex.lock();
-                disk_write(complete_frame , SDC_address);
-                SPI_mutex.unlock();
-                SDC_address++;
-               
-            
-                
-                //now save to the sd card TM_convoluted_data
-//              std::bitset<8> b;
-//              printf("\nthis is frame %d\n",TMID);                                        //for printing frame 
-//                  for(int j =0; j<134;j++){
-//                  printf(" %d",frames[TMID][j]);
-////                    b =  frames[TMID][j];
-////                    cout<<b;
-//                  }
+                for(int i = 0 ; i < 4 ; i++){
+                    space = adjust(1, (compress(sfp_bin[48+i],6,3))>>8 ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+                    space = adjust(8, compress(sfp_bin[48+i],6,3) ,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
+                }
+                space = adjust(2, (compress(sfp_bin[17],8,2))>>8 ,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
+                space = adjust(8, compress(sfp_bin[17],8,2) ,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
+                space = adjust(2, (compress(sfp_bin[23],8,2))>>8 ,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
+                space = adjust(8, compress(sfp_bin[23],8,2) ,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
                 
-                frame_space_number[TMID] = 0;
-                making_frameHeader(TMID);
-                frames[TMID][4]=packet_len - copy_count;
-                //write time  here also
-                continue;
-            }       
-        }
-    
-//      printf("\nthis is frame %d\n",TMID);                                              //for printing frame 
-//      for(int j =0; j<134;j++){
-//          printf(" %d",frames[TMID][j]);
-//      }
- 
-    }   
- 
- 
-}
- 
- 
- 
- 
- 
-namespace Science_Data_Compression{
- 
-    # define PACKET_SEQUENCE_COUNT 1                         //1 byte
-    # define NUM_PROTON_BIN 17                               //2 byte each
-    # define NUM_ELECTRON_BIN 14                             //2 byte each 
-    # define VETO 1                                          //2 byte
-    # define FASTCHAIN 2                                     //4 byte each
-    #define RAW_PACKET_LENGTH 73                             //73 bytes
- 
-//  #define PACKET_SEQ_COUNT 1
-//  #define PROTON_BIN_SIZE 2
-//  #define ELECTRON_BIN_SIZE 2
-//  #define VETO 2
-//  #define FAST_CHAIN 4
-    
-    
-    /*
-    @brief:     read one uint16_t equivalent of first two chars from the stream. short int because 16 bits
-    @param:     pointer to the start of the short int
-    @return:    uint16_t 
-    */
-    
-    
-    unsigned int read_2byte(unsigned char* ptr){
-        unsigned int output = (unsigned int) *(ptr+1);
-        output += ( (unsigned int)(*ptr) ) << 8;
-        return output;
-    }
-    
-    /*
-    @brief:     read one int equivalent of first four chars from the stream.  int because 32 bits
-    @param:     pointer to the start of the short int
-    @return:    unsigned int  
-    */
- 
-    unsigned int read_4byte(unsigned char* ptr){
-        unsigned int output = (unsigned int) *(ptr+3);
-        output += (unsigned int)*(ptr+2)<<8;
-        output += (unsigned int)*(ptr+1)<<16;
-        output += (unsigned int)*(ptr)<<24;
-        return output;
-    }
- 
-    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 
-    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  };
-    unsigned int SFP_bin[35];
-    unsigned int SCP_bin[35]={0};
-    unsigned char SFP_outputBT[23];                         //BT = below threshold
-    unsigned char SFP_outputAT[35];
-    unsigned char SCP_outputLCR[22];
-    unsigned char SCP_outputHCR[26];
- 
-    
-//**********************************************************************************************************************************************************************
-    //lots of compression functions are listed below
- 
-    
-    unsigned char SFP_compress4_BT(unsigned int input){                                 //for veto
-        int de_4 = 0;
-        unsigned char output;
-        
-        if(input<= 3){
-        //  DE = 0;
-            output = 0x0;
-            de_4 = 0;
+                if(pzf == 0){
+//                  cout<<"proton bins ";
+                    for(int i = 0; i<17 ; i++){
+                        space = adjust(2, ((compress(sfp_bin[i],8,2))>>8) ,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
+                        space = adjust(8, compress(sfp_bin[i],8,2) ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+//                      printf("%02X ",compress(sfp_bin[i],8,2));
+                    }
+                }
+                if(ezf == 0){
+//                  cout<<"electron bins ";
+                    for(int i = 0; i<5 ; i++){
+                        space = adjust(2, ((compress(sfp_bin[18+i],8,2))>>8) ,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
+                        space = adjust(8, compress(sfp_bin[18+i],8,2) ,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
+//                      printf("%02X ",compress(sfp_bin[i],8,2));
+                    }
+                }
+//              cout<<debug_cntr<<" "<<(space&0xf)<<endl;
+//              cout<<"packet ";
+//              for(int i = 0; i< 64; i++){
+//                  cout<<bitset<8>(sfp[i]);
+//              }
+//              cout<<"ends"<<endl;
+            }//above threshold ends here.
+        }else if(srp_mode == 1){    //scattered mode
+            if(at == 0){
+                pointer = sfp; debug_cntr = 0;  space = 8;
+                space = adjust(4, attitude,pointer,8);  pointer += space>>4;    debug_cntr += space>>4;
+                space = adjust(6, counter,pointer,space);   pointer += space>>4;    debug_cntr += space>>4;
+                space = adjust(1, srp_mode,pointer,space);  pointer += space>>4;    debug_cntr += space>>4;
+                for(int i=0; i<32; i++){
+                    space = adjust(7, compress(sfp_bin[i],6,1) ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+                }
+                for(int i = 0 ; i < 4 ; i++){
+                    space = adjust(1, (compress(sfp_bin[32+i],7,2))>>8 ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+                    space = adjust(8, compress(sfp_bin[32+i],7,2) ,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
+                }
+            }
+            if(at == 1){
+                pointer = sfp; debug_cntr = 0;  space = 8;
+                space = adjust(3, sci_time>>32 ,pointer,space); pointer += space>>4;    debug_cntr += space>>4;
+                space = adjust(8, sci_time>>24 ,pointer,space); pointer += space>>4;    debug_cntr += space>>4;
+                space = adjust(8, sci_time>>16 ,pointer,space); pointer += space>>4;    debug_cntr += space>>4;
+                space = adjust(8, sci_time>>8 ,pointer,space);  pointer += space>>4;    debug_cntr += space>>4;
+                space = adjust(8, sci_time ,pointer,space); pointer += space>>4;    debug_cntr += space>>4;
+                space = adjust(4, attitude,pointer,space);  pointer += space>>4;    debug_cntr += space>>4;
+                space = adjust(6, counter,pointer,space);   pointer += space>>4;    debug_cntr += space>>4;
+                space = adjust(1, srp_mode,pointer,space);  pointer += space>>4;    debug_cntr += space>>4;
+                for(int i=0; i<32; i++){
+                    space = adjust(2, (compress(sfp_bin[i],8,2))>>8 ,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
+                    space = adjust(8, compress(sfp_bin[i],8,2) ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+                }
+                for(int i = 0 ; i < 4 ; i++){
+                    space = adjust(1, (compress(sfp_bin[32+i],6,3))>>8 ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+                    space = adjust(8, compress(sfp_bin[32+i],6,3) ,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
+                }
+            }
+        }   
+        //science fine packet is complete here.
+        //lets try to make frame now******************************************************************************************************************************
+        if(srp_mode == 0){
+            if(at == 0){
+                id = 1;     length = 241;
+                if(pzf == 0)
+                    length += 87;
+                if(ezf == 0)
+                    length += 35;
+            }else{
+                id = 2;     length = 288;
+                if(pzf == 0)
+                    length += 170;
+                if(ezf == 0)
+                    length += 50;
+            }
         }
-        else if(input <= 12){
-        //  DE = 01;
-            output = 0x1;
-            de_4 = 2;   
-        }
-        else if(input <= 48){
-        //  DE = 10
-            output = 0x2;
-            de_4 = 4;   
-        }
-        else {
-        //  DE = 11
-            output = 0x3;
-            de_4 = 6;
-        }
-        
-        unsigned short int temp = input >> de_4;
-        output += (temp ) << 2;
-        return output;
-    }
-    
-    
-    unsigned char SFP_compress5_BT(unsigned int input){
-        int de_4 = 0;                                   //number by which bin value need to be shifted
-        unsigned char output;
-        
-        if(input <= 15){
-//            D = 0 [0]
-            output = 0x0;
-            de_4 = 0;
-        }
-        else if(input <= 60){
-//            D = 1
-            output = 0x1;
-            de_4 = 2;
+        else if(srp_mode == 1){
+            if(at == 0){
+                id = 1;     length = 272;
+            }else{
+                id = 2;     length = 408;
+            }
         }
-        
-        unsigned short int temp = input >> de_4;
-        output += (temp ) << 1;
-        
-        return output;      
-    };
-    
-    
-    unsigned char SFP_compress6_BT(unsigned int input){
-        int de_4 = 0;;
-        unsigned char output;
-        
-        if(input <= 31){
-//          E = 0 [0]
-            output = 0x0;
-            de_4 = 0;
-        }
-        else if(input <= 124){
-//            E = 1
-            output = 0x1;
-            de_4 = 2;
-        }
-         
-        
-        unsigned short int temp = input >> de_4;
-        output += (temp ) << 1;
-        
-        return output;
-    };
-    
-    unsigned char SFP_compress7_AT(unsigned int input){
-        int de_4  = 0;
-        unsigned char output;
-        
-        if(input <= 31){
-//            DE = 00 [0]
-            output = 0x0;
-            de_4 = 0;
-        }
-        else if(input <= 124){
-//            DE = 01 [1]
-            output = 0x1;
-            de_4 = 2;
-        }
-        
-        else if(input <= 496){
-//            DE = 10 [2]
-            output = 0x2;
-            de_4 = 4;
-        }
-        
-        else if(input <= 1984){
-//           DE = 11 [3]
-            output = 0x3;
-            de_4 = 6;
+        length = (length%8==0)?(length/8):(length/8)+1;
+        for(int j=0 ; j<length ;j++){
+//          printf("%02X",sfp[j]);
+            frames[id][position_tm_frame[id]] = sfp[j];
+            position_tm_frame[id]++;
+            if(position_tm_frame[id] == 132){   //space full in the frame bro
+                pointer = frames[id];
+                if(id == 1){    //below thereshold
+                    space = adjust(1,0,pointer,8);
+                    space = adjust(4,3,pointer,space);
+                    FSC_science = FCTN_SD_MNGR(3);
+                    gPC.printf("SID = 3, FSC = %02X", FSC_science);
+                    frames[id][1] = (FSC_science>>24)&0xff;
+                    frames[id][2] = (FSC_science>>16)&0xff;
+                    frames[id][3] = (FSC_science>>8)&0xff;
+                    frames[id][4] = FSC_science&0xff;
+                    frames[id][6] = (sci_time>>32)&0xff;
+                    frames[id][7] = (sci_time>>24)&0xff;
+                    frames[id][8] = (sci_time>>16)&0xff;
+                    frames[id][9] = (sci_time>>8)&0xff;
+                    frames[id][10] = (sci_time)&0xff;
+                }else if(id == 2){
+                    space = adjust(1,0,pointer,8);
+                    space = adjust(4,2,pointer,space);
+                    FSC_science = FCTN_SD_MNGR(2);
+                    gPC.printf("SID = 2, FSC = %02X", FSC_science);
+                    frames[id][1] = (FSC_science>>16)&0xff;
+                    frames[id][2] = (FSC_science>>8)&0xff;
+                    frames[id][3] = FSC_science&0xff;
+                }
+                temp_crc = crc16_gen(frames[id],132);
+                frames[id][132] = temp_crc>>8;
+                frames[id][133] = temp_crc & 0xff;
+                convolution(frames[id]);
+                interleave(TM_convoluted_data,TM_interleave_data);
+                interleave(TM_convoluted_data+ 135,TM_interleave_data + 144);
+                if(id == 1)
+                    SD_WRITE(TM_interleave_data,FSC_science,3);
+                else if (id == 2)
+                    SD_WRITE(TM_interleave_data,FSC_science,2);
+                position_tm_frame[id] = position_tm_starting[id];
+                frames[id][6-id] = (length-1) - j;      // first head pointer.
+            }
         }
         
-        unsigned short int temp = input >> de_4;
-        output += (temp ) << 2;
-        
-        return output;
-        
-    };
-    
-    
-    unsigned char SFP_compress8_AT(unsigned int input){
-        
-        int de_4 = 0;;
-        unsigned char output;
-        
-        if(input <= 63){
-//            DE = 00 [0]
-            output = 0x0;
-            de_4 = 0;
-        }
-        else if(input <= 252){
-//            DE = 01 [1]
-            output = 0x1;
-            de_4 = 2;
-        }
-        
-        else if(input <= 1008){
-//            DE = 10 [2]
-            output = 0x2;
-            de_4 = 4;
-        }
-        
-        else {
-//           DE = 11 [3]
-            output = 0x3;
-            de_4 = 6;
-        }
-        
-        unsigned short int temp = input >> de_4;
-        output += (temp ) << 2;
-        
-        return output;
-    };
-    
-    unsigned char SFP_compress5_AT(unsigned int input){
-        int de_4 = 0;;
-        unsigned char output;
-        
-        if(input <= 3){
-//            DE = 000 [0]
-            output = 0x0;
-            de_4 = 0;
-        }
-        else if(input <= 12){
-//            DE = 001 [1]
-            output = 0x1;
-            de_4 = 2;
-        }
-        
-        else if(input <= 48){
-//            DE = 010 [2]
-            output = 0x2;
-            de_4 = 4;
-        }
-        
-        else if(input <= 192) {
-//           DE = 011 [3]
-            output = 0x3;
-            de_4 = 6;
-        }
-        
-        else if(input <= 768) {
-//           DE = 100 [4]
-            output = 0x4;
-            de_4 = 8;
-        }
-        
-        else if(input <= 3072) {
-//           DE = 101 [5]
-            output = 0x5;
-            de_4 = 10;
-        }
-        
-        else if(input <= 12288) {
-//           DE = 110 [6]
-            output = 0x6;
-            de_4 = 12;
-        }
-        
-        else  {
-//          DE = 111 [7]
-            output = 0x7;
-            de_4 = 14;
-        }
-           
-        unsigned short int temp = input >> de_4;
-        output += (temp ) << 3;
-        
-        return output;
-    }
-    
-    unsigned char SFP_compress7FC_AT(unsigned int input){                   // for fast chain above threshold
-        int de_4 = 0;;
-        unsigned char output;
-        
-        if(input <= 15){
-//            DE = 000 [0]
-            output = 0x0;
-            de_4 = 0;
-        }
-        else if(input <= 60){
-//            DE = 001 [1]
-            output = 0x1;
-            de_4 = 2;
-        }
-        
-        else if(input <= 240){
-//            DE = 010 [2]
-            output = 0x2;
-            de_4 = 4;
-        }
-        
-        else if(input <= 960) {
-//           DE = 011 [3]
-            output = 0x3;
-            de_4 = 6;
-        }
-        
-        else if(input <= 3840) {
-//           DE = 100 [4]
-            output = 0x4;
-            de_4 = 8;
-        }
-        
-        else if(input <= 15360) {
-//           DE = 101 [5]
-            output = 0x5;
-            de_4 = 10;
-        }
-        
-        else if(input <= 61440) {
-//           DE = 110 [6]
-            output = 0x6;
-            de_4 = 12;
-        }
-        
-        else  {
-//          DE = 111 [7]
-            output = 0x7;
-            de_4 = 14;
-        }
-           
-        unsigned short int temp = input >> de_4;
-        output += (temp ) << 3;
-        
-        return output;
-    }
-    
-    
+    }//for loop bracket which runs 60 times
     
-    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-    unsigned char SCP_compress6(unsigned int input){
-        int ef_4;
-        unsigned char output;
-        
-        if(input <= 15){
-//      EF = 00
-        output = 0x0;
-        ef_4 = 0;
-        }
-        
-        else if(input <= 60 ){
-//          EF = 01 [1]
-            output = 0x01;
-            ef_4 = 2;
-        }
-        else if(input <= 240){
-//          EF = 10 [2]
-            output = 0x02;
-            ef_4 = 4;
-        }
-        else{
-//          EF = 11 [3]
-            output = 0x03;
-            ef_4 = 6;
-        }
-            
-            unsigned short int temp = input >> ef_4;
-            output += (temp & 0xf) << 2;
-            
-            return output;
-    }
-    
-    unsigned char SCP_compress5(unsigned int input){
-        
-        int de_4 = 0;;
-        unsigned char output;
-        
-        if(input <= 7){
-//            DE = 00 [0]
-            output = 0x0;
-            de_4 = 0;
-        }
-        
-        else if(input <= 28){
-//            DE = 01 [1]
-            output = 0x01;
-            de_4 = 2;
-        }
-        else if(input <= 112){
-//            DE = 10 [2]
-            output = 0x02;
-            de_4 = 4;
-        }
-        else{
-//        DE = 11 [3]
-            output = 0x03;
-            de_4 = 6;
-        }
-        
-        unsigned short int temp = input >> de_4;
-        output += (temp & 0x7) << 2;
-        
-        return output;
-    }
-    
-        unsigned char SCP_compress6h(unsigned int input) {
-    
-        int ef_4;
-        unsigned char output;
-        
-        if(input <=7){
-//          EF = 000 [0]
-            output = 0x00;
-            ef_4 = 0;
-        }
-        
-        else if(input <=28){
-//          EF = 001 [1]
-            output = 0x01;
-            ef_4 = 2;
-        }
-        else if(input <= 112){
-        
-//          EF = 010 [2]
-            output = 0x02;
-            ef_4 = 4;
-        }
-        else if(input <= 448){
-//          EF = 011 [3]
-            output = 0x03;
-            ef_4 = 6;
-        }
-        else if(input <= 1792){
-//          EF = 100 [4]
-            output = 0x04;
-            ef_4 = 8;
-            
-        }
-        else if(input <= 7168){
-//          EF = 101 [5]
-            output = 0x05;
-            ef_4 = 10;
-            
-        }
-        else if(input <= 28672){
-//          EF = 110 [6]
-            output = 0x06;
-            ef_4 = 12;
-        }
-        else{
-//          EF = 111 [7]
-            output = 0x07;
-            ef_4 =14;
-        }
-        
-        unsigned short int temp = input >> ef_4;
-        output += (temp & 0x7) << 3;
-        
-        return output;
-        
-    }
-    
-    
-    unsigned char SCP_compress7h(unsigned int input) {
-    
-        int fg_4;
-        unsigned char output;
-        
-        if(input <= 15){
-//          EF = 000 [0]
-            output = 0x0;
-            fg_4 = 0;
-        }
-        
-        else if(input <= 60){
-//          EF = 001 [1]
-            output = 0x01;
-            fg_4 = 2;
-        }
-        else if(input <= 240){
-        
-//          EF = 010 [2]                    
-            output = 0x02;
-            fg_4 = 4;
-        }
-        else if(input <= 960){
-//          EF = 011 [3]
-            output = 0x03;
-            fg_4 = 6;
-        }
-        else if(input <= 3840){
-//          EF = 100 [4]
-            output = 0x04;
-            fg_4 = 8;
-            
-        }
-        else if(input <= 15360){
-//          EF = 101 [5]
-            output = 0x05;
-            fg_4 = 10;
-            
-        }
-        else if(input <= 61440){
-//          EF = 110 [6]
-            output = 0x06;
-            fg_4 = 12;
-        }
-        else{
-//          EF = 111 [7]
-            output = 0x07;
-            fg_4 =14;
-        }
-        
-        unsigned short int temp = input >> fg_4;
-        output += (temp & 0xf) << 3;
-        
-        return output;
-        
-    }
-    //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-    void SCP_compress_data();                   
-    void SFP_compress_data(unsigned char* input){
- 
-            bool LCR = true;
-            int i = 0;
-            static int packet_no = 0;                       //takes value from 0 to 29
-            //TRAVERSE THE LIST TO DETERMINE LCR OR HCR and stroing the values in proton_bin and electron bin
-            SFP_bin[0] = *input;
-            for(i=1 ; i<= NUM_PROTON_BIN + NUM_ELECTRON_BIN + VETO ; ++i){          //storing the bin values into an array name bin
-                SFP_bin[i]=read_2byte(input+1+((i-1)<<1));                             //proton bin and elecron bin are 2 byte, hence read_2byte and 
-                SCP_bin[i]+=SFP_bin[i];
-                if(SFP_bin[i] > SFP_thresholds[i]){                                         //fast cahin is 4 byte hence read_4byte
-                    LCR = false;                                                    // if a single value is above threshold then lcr becomes false
-                    i++;
+    //---------------below is scp --------------**************************************************************************************************
+    at = 0;     pzf = 1;    ezf = 1;
+    srp_mode = head[2]&0x1;
+    unsigned char compression_option = (head[2]>>1)&0x3;    //--------------------------------------------------------------------------------------
+    if(srp_mode ==0){
+        if(compression_option == 0){
+            for(int i=0; i<52 ;i++){
+                if(scp_bin[i] > scp_threshold_m0[i]){
+                    at = 1;
                     break;
                 }
             }
-            
-            for( ; i<= NUM_PROTON_BIN + NUM_ELECTRON_BIN + VETO ; ++i){
-                SCP_bin[i]+=SFP_bin[i];
-                SFP_bin[i] = read_2byte(input + 1 + ( (i-1)<<1) );
-            }
-            
-            SFP_bin[i] = read_4byte(input+1+ ((i-1)<<1))    ;
-            SCP_bin[i]+=SFP_bin[i];
-            
-            if(SFP_bin[i]>SFP_thresholds[i])
-                LCR = false;                                                    //since veto starts from location (input + 65) and (input + 69)
-                    
-            SFP_bin[i+1] = read_4byte(input+69);
-            SCP_bin[i]+=SFP_bin[i];
-            
-            if(SFP_bin[i]>SFP_thresholds[i])
-                LCR = false;
-            
-            
-//          printf("\n");                       //for printing the sfp bin
-//          for (i=0;i<35;i++){
-//              printf("sfp[%d] = %d",i,SFP_bin[i]);
-//          }
-//          printf("\n");
- 
-            if(LCR){   
-                
-                 SFP_outputBT[0]  =  (packet_no<<3) + ( SFP_compress5_BT(SFP_bin[1])>>2 );
-                 SFP_outputBT[1]  =  ( SFP_compress5_BT(SFP_bin[1])<<6 ) + ( SFP_compress5_BT(SFP_bin[2])<<1 ) +   ( SFP_compress5_BT(SFP_bin[3])>>4 );
-                 SFP_outputBT[2]  =  ( SFP_compress5_BT(SFP_bin[3])<<4 ) + ( SFP_compress5_BT(SFP_bin[4])>>1 );
-                 SFP_outputBT[3]  =  ( SFP_compress5_BT(SFP_bin[4])<<7 ) + ( SFP_compress5_BT(SFP_bin[5])<<2 ) + ( SFP_compress5_BT(SFP_bin[6])>>3 );            
-                 SFP_outputBT[4]  =  ( SFP_compress5_BT(SFP_bin[6])<<5 ) + ( SFP_compress5_BT(SFP_bin[7]) );
-                 SFP_outputBT[5]  =  ( SFP_compress5_BT(SFP_bin[8])<<3 ) + ( SFP_compress5_BT(SFP_bin[9])>>2 );
-                 SFP_outputBT[6]  =  ( SFP_compress5_BT(SFP_bin[9])<<6 ) + ( SFP_compress5_BT(SFP_bin[10])<<1 ) +  ( SFP_compress5_BT(SFP_bin[11])>>4 );
-                 SFP_outputBT[7]  =  ( SFP_compress5_BT(SFP_bin[11])<<4 ) + ( SFP_compress5_BT(SFP_bin[12])>>1 );            
-                 SFP_outputBT[8]  =  ( SFP_compress5_BT(SFP_bin[12])<<7 ) + ( SFP_compress5_BT(SFP_bin[13])<<2 ) + ( SFP_compress5_BT(SFP_bin[14])>>3 );
-                 SFP_outputBT[9]  =  ( SFP_compress5_BT(SFP_bin[14])<<5 ) + ( SFP_compress5_BT(SFP_bin[15]) );
-                 SFP_outputBT[10] =  ( SFP_compress5_BT(SFP_bin[16])<<3 ) + ( SFP_compress5_BT(SFP_bin[17])>>2 );
-                 SFP_outputBT[11] =  ( SFP_compress5_BT(SFP_bin[17])<<6 ) + ( SFP_compress6_BT(SFP_bin[18]) );
-                 SFP_outputBT[12] =  ( SFP_compress6_BT(SFP_bin[19])<<2 ) + ( SFP_compress6_BT(SFP_bin[20])>>4 );
-                 SFP_outputBT[13] =  ( SFP_compress6_BT(SFP_bin[20])<<4 ) + ( SFP_compress5_BT(SFP_bin[21])>>1 );        
-                 SFP_outputBT[14] =  ( SFP_compress5_BT(SFP_bin[21])<<7 ) + ( SFP_compress5_BT(SFP_bin[22])<<2 )  + ( SFP_compress5_BT(SFP_bin[23])>>3 );
-                 SFP_outputBT[15] =  ( SFP_compress5_BT(SFP_bin[23])<<5 ) + ( SFP_compress5_BT(SFP_bin[24]) );
-                 SFP_outputBT[16] =  ( SFP_compress5_BT(SFP_bin[25])<<3 ) + ( SFP_compress5_BT(SFP_bin[26])>>2 );
-                 SFP_outputBT[17] =  ( SFP_compress5_BT(SFP_bin[26])<<6 ) + ( SFP_compress5_BT(SFP_bin[27])<<1 ) + ( SFP_compress5_BT(SFP_bin[28])>>4);
-                 SFP_outputBT[18] =  ( SFP_compress5_BT(SFP_bin[28])<<4 ) + ( SFP_compress5_BT(SFP_bin[29])>>1) ;
-                 SFP_outputBT[19] =  ( SFP_compress5_BT(SFP_bin[29])<<7 ) + ( SFP_compress5_BT(SFP_bin[30])<<2 ) + ( SFP_compress5_BT(SFP_bin[31])>>3)  ; 
-                 SFP_outputBT[20] =  ( SFP_compress5_BT(SFP_bin[31])<<5 ) + ( SFP_compress4_BT(SFP_bin[32])<<1)  + ( SCP_compress5(SFP_bin[33])>>4 ) ; //
-                 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
-                 SFP_outputBT[22] =  ( SCP_compress5(SFP_bin[34])<<7 );                                             //7 bits are spare
-                 
-                 Science_TMframe::making_frame(3,'L',SFP_outputBT);
-                 if(++packet_no == 30){
-                    packet_no=0;
-                    SCP_compress_data();
-                    for(i=0;i<PACKET_SEQUENCE_COUNT + NUM_PROTON_BIN + NUM_ELECTRON_BIN + VETO + FASTCHAIN;i++){
-                        if(packet_no==0){
-                            SCP_bin[i]=0;
-                        }
-                    }
-                 }
-                 
-                 
+        }
+        else if(compression_option == 1){
+            if(scp_bin[44] > scp_threshold_m0_1[0]) at=1;
+            for(int i=0; i<4 ;i++){
+                if(scp_bin[48+i] > scp_threshold_m0_1[i+1]){
+                    at = 1;
+                    break;
+                }
             }
- 
-            else {
-                
-                
-                SFP_outputAT[0]  = (RTC_TIME>>27)&(0xff);
-                SFP_outputAT[1]  = (RTC_TIME>>19)&(0xff);
-                SFP_outputAT[2]  = (RTC_TIME>>11)&(0xff);
-                SFP_outputAT[3]  = (RTC_TIME>>3 )&(0xff);
-                SFP_outputAT[4]  = (RTC_TIME<<5 )&(0xff) + (packet_no);
-                SFP_outputAT[5]  = ( SFP_compress7_AT(SFP_bin[1])<<1 ) + ( SFP_compress7_AT(SFP_bin[2])>>6 );
-                SFP_outputAT[6]  = ( SFP_compress7_AT(SFP_bin[2])<<2 ) + ( SFP_compress7_AT(SFP_bin[3])>>5 );
-                SFP_outputAT[7]  = ( SFP_compress7_AT(SFP_bin[3])<<3 ) + ( SFP_compress7_AT(SFP_bin[4])>>4 );
-                SFP_outputAT[8]  = ( SFP_compress7_AT(SFP_bin[4])<<4 ) + ( SFP_compress7_AT(SFP_bin[5])>>3 );
-                SFP_outputAT[9]  = ( SFP_compress7_AT(SFP_bin[5])<<5 ) + ( SFP_compress7_AT(SFP_bin[6])>>2 );
-                SFP_outputAT[10] = ( SFP_compress7_AT(SFP_bin[6])<<6 ) + ( SFP_compress7_AT(SFP_bin[7])>>1 );
-                SFP_outputAT[11] = ( SFP_compress7_AT(SFP_bin[7])<<7 ) + ( SFP_compress7_AT(SFP_bin[8]) );
-                SFP_outputAT[12] = ( SFP_compress7_AT(SFP_bin[9])<<1 ) + ( SFP_compress7_AT(SFP_bin[10])>>6);
-                SFP_outputAT[13] = ( SFP_compress7_AT(SFP_bin[10])<<2 ) + ( SFP_compress7_AT(SFP_bin[11])>>5);
-                SFP_outputAT[14] = ( SFP_compress7_AT(SFP_bin[11])<<3 ) + ( SFP_compress7_AT(SFP_bin[12])>>4);
-                SFP_outputAT[15] = ( SFP_compress7_AT(SFP_bin[12])<<4 ) + ( SFP_compress7_AT(SFP_bin[13])>>3);
-                SFP_outputAT[16] = ( SFP_compress7_AT(SFP_bin[13])<<5 ) + ( SFP_compress7_AT(SFP_bin[14])>>2);
-                SFP_outputAT[17] = ( SFP_compress7_AT(SFP_bin[14])<<6 ) + ( SFP_compress7_AT(SFP_bin[15])>>1);
-                SFP_outputAT[18] = ( SFP_compress7_AT(SFP_bin[15])<<7 ) + ( SFP_compress7_AT(SFP_bin[16]));
-                SFP_outputAT[19] = ( SFP_compress7_AT(SFP_bin[17])<<1 ) + ( SFP_compress8_AT(SFP_bin[18])>>7 );
-                SFP_outputAT[20] = ( SFP_compress8_AT(SFP_bin[18])<<1 ) + ( SFP_compress8_AT(SFP_bin[19])>>7 );
-                SFP_outputAT[21] = ( SFP_compress8_AT(SFP_bin[19])<<1 ) + ( SFP_compress8_AT(SFP_bin[20])>>7 );
-                SFP_outputAT[22] = ( SFP_compress8_AT(SFP_bin[20])<<1 ) + ( SFP_compress7_AT(SFP_bin[21])>>6 );
-                SFP_outputAT[23] = ( SFP_compress7_AT(SFP_bin[21])<<2 ) + ( SFP_compress7_AT(SFP_bin[22])>>5 );
-                SFP_outputAT[24] = ( SFP_compress7_AT(SFP_bin[22])<<3 ) + ( SFP_compress7_AT(SFP_bin[23])>>4 );
-                SFP_outputAT[25] = ( SFP_compress7_AT(SFP_bin[23])<<4 ) + ( SFP_compress7_AT(SFP_bin[24])>>3 );
-                SFP_outputAT[26] = ( SFP_compress7_AT(SFP_bin[24])<<5 ) + ( SFP_compress7_AT(SFP_bin[25])>>2 );
-                SFP_outputAT[27] = ( SFP_compress7_AT(SFP_bin[25])<<6 ) + ( SFP_compress7_AT(SFP_bin[26])>>1 );
-                SFP_outputAT[28] = ( SFP_compress7_AT(SFP_bin[26])<<7 ) + ( SFP_compress7_AT(SFP_bin[27]) );
-                SFP_outputAT[29] = ( SFP_compress7_AT(SFP_bin[28])<<1 ) + ( SFP_compress7_AT(SFP_bin[29])>>6);
-                SFP_outputAT[30] = ( SFP_compress7_AT(SFP_bin[29])<<2 ) + ( SFP_compress7_AT(SFP_bin[30])>>5);
-                SFP_outputAT[31] = ( SFP_compress7_AT(SFP_bin[30])<<3 ) +( SFP_compress7_AT(SFP_bin[31])>>4);  
-                SFP_outputAT[32] = ( SFP_compress7_AT(SFP_bin[31])<<4 ) +( SFP_compress5_AT(SFP_bin[32])>>1); 
-                SFP_outputAT[33] = ( SFP_compress5_AT(SFP_bin[32])<<7 ) +( SFP_compress7FC_AT(SFP_bin[33])); 
-                SFP_outputAT[34] =  ( SFP_compress7FC_AT(SFP_bin[34])<<1 );                                             // 1 bit is spare          
-                
-                Science_TMframe::making_frame(2,'H',SFP_outputAT);
-                if(++packet_no == 30){
-                    packet_no=0;
-                    SCP_compress_data();
-                    for(i=0;i<PACKET_SEQUENCE_COUNT + NUM_PROTON_BIN + NUM_ELECTRON_BIN + VETO + FASTCHAIN;i++){
-                        if(packet_no==0){
-                            SCP_bin[i]=0;
-                        }
-                    }
-                }
-                            
+            if(scp_bin[17] > scp_threshold_m0_1[5]) at=1;
+            if(scp_bin[23] > scp_threshold_m0_1[6]) at=1;
+            
+            proton_scp_sum = 0; electron_scp_sum = 0;
+            for(int i=0;i<17;i++){
+                proton_scp_sum += scp_bin[i];
             }
- 
-    }
- 
- 
-/*
-    brief: takes the  pointer of the raw data string and return the address of the array which stores the address of 30 packets.
-    input: pointer to the raw data.
-    output : void
-*/
- 
-    void complete_compression(unsigned char *SRP,uint64_t x){
-        RTC_TIME = x;
-        int i;                  //30 times because 3 second data
-    
-        for(i=0;i<30;i++){
-         SFP_compress_data(SRP + 73*i); 
+            for(int i=0; i<5; i++){
+                electron_scp_sum += scp_bin[18+i];
+            }
+            if(proton_scp_sum > scp_threshold_m0_1[7]) at=1;
+            if(electron_scp_sum > scp_threshold_m0_1[8]) at=1;
         }
- 
-    }
- 
- 
- 
-/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- 
-    
-    
-    /*
-    @brief:     compresses the given input stream and return output packet
-    @param:     pointer to input stream. Input stream always has the fixed size of RAW_PACKET_LENGTH
-    @return:    pointer to output stream. Output stream has the size of 22 or 26 bytes
-    */
-    void SCP_compress_data(){
-        
-        bool LCR = true;
-        int i = 0;
-        
-        for(i=1;i<=PACKET_SEQUENCE_COUNT + NUM_PROTON_BIN + NUM_ELECTRON_BIN + VETO + FASTCHAIN ;i++){
-            if(SCP_bin[i]>SCP_thresholds[i]){
-            LCR = false;
-            break;
+    }else if(srp_mode ==1){
+        for(int i=0; i<32; i++){
+            if(scp_bin[i] > scp_threshold_m1[0]){
+                at = 1;
+                break;
             }
         }
-//      printf("\n");                    //for printing the scp bin
-//          for (i=0;i<35;i++){
-//              printf(" scp[%d] = %d    ",i,SCP_bin[i]);
-//          }
-//          printf("\n");         
-        // compressing the data
-        if(LCR){   
-             SCP_outputLCR[0] = (RTC_TIME>>5)&(0xff);                //first 13 bits for time tagging
-             SCP_outputLCR[1] = (RTC_TIME<<3)&(0xff);                //then 4 bits for attitude tag
-             SCP_outputLCR[2] = 0x00;                                //only attitude tag is left
-             SCP_outputLCR[2] += ( SCP_compress5(SCP_bin[0])<<1 ) + ( SCP_compress5(SCP_bin[1])>>4 );
-             SCP_outputLCR[3] = ( SCP_compress5(SCP_bin[1])<<4 ) + ( SCP_compress5(SCP_bin[2])>>1 );
-             SCP_outputLCR[4] = ( SCP_compress5(SCP_bin[2])<<7 ) + ( SCP_compress5(SCP_bin[3])<<2 ) + ( SCP_compress5(SCP_bin[4])>>3 );
-             SCP_outputLCR[5] = ( SCP_compress5(SCP_bin[4])<<5 ) + ( SCP_compress5(SCP_bin[5]) );            
-             SCP_outputLCR[6] = ( SCP_compress5(SCP_bin[6])<<3 ) + ( SCP_compress5(SCP_bin[7])>>2 );
-             SCP_outputLCR[7] = ( SCP_compress5(SCP_bin[7])<<6 ) + ( SCP_compress5(SCP_bin[8])<<1 ) +  ( SCP_compress5(SCP_bin[9])>>4 );
-             SCP_outputLCR[8] = ( SCP_compress5(SCP_bin[9])<<4 ) + ( SCP_compress5(SCP_bin[10])>>1 );
-             SCP_outputLCR[9] = ( SCP_compress5(SCP_bin[10])<<7 ) + ( SCP_compress5(SCP_bin[11])<<2) + ( SCP_compress5(SCP_bin[12])>>3 );            
-             SCP_outputLCR[10] = ( SCP_compress5(SCP_bin[12])<<5 ) + ( SCP_compress5(SCP_bin[13]) );
-             SCP_outputLCR[11] = ( SCP_compress5(SCP_bin[14])<<3 ) + ( SCP_compress5(SCP_bin[15])>>2 );
-             SCP_outputLCR[12] = ( SCP_compress5(SCP_bin[15])<<6 ) + ( SCP_compress5(SCP_bin[16])<<1) +  ( SCP_compress6(SCP_bin[17])>>5 );
-             SCP_outputLCR[13] = ( SCP_compress6(SCP_bin[17])<<3 ) + ( SCP_compress6(SCP_bin[18])>>3 );
-             SCP_outputLCR[14] = ( SCP_compress5(SCP_bin[18])<<5 ) + ( SCP_compress6(SCP_bin[19])>>1 );
-             SCP_outputLCR[15] = ( SCP_compress6(SCP_bin[19])<<7 ) + ( SCP_compress5(SCP_bin[20])<<2 ) + ( SCP_compress5(SCP_bin[21])>>3 );          
-             SCP_outputLCR[16] = ( SCP_compress5(SCP_bin[21])<<5 ) + ( SCP_compress5(SCP_bin[22]) );
-             SCP_outputLCR[17] = ( SCP_compress5(SCP_bin[23])<<3 ) + ( SCP_compress5(SCP_bin[24])>>2 );
-             SCP_outputLCR[18] = ( SCP_compress5(SCP_bin[24])<<6 ) + ( SCP_compress5(SCP_bin[25])<<1) +  ( SCP_compress5(SCP_bin[26])>>4 );
-             SCP_outputLCR[19] = ( SCP_compress5(SCP_bin[26])<<4 ) + ( SCP_compress5(SCP_bin[27])>>1 );
-             SCP_outputLCR[20] = ( SCP_compress5(SCP_bin[27])<<7 ) + ( SCP_compress5(SCP_bin[28])<<2 ) + ( SCP_compress5(SCP_bin[29])>>3 ); 
-             SCP_outputLCR[21] = ( SCP_compress5(SCP_bin[29])<<5 ) + ( SCP_compress5(SCP_bin[30]) );
-         
-             Science_TMframe::making_frame(1,'L',SCP_outputLCR);
-        }
-        else{
-            
-            SCP_outputLCR[0] = (RTC_TIME>>5)&(0xff);                //first 13 bits for time tagging
-            SCP_outputLCR[1] = (RTC_TIME<<3)&(0xff);
-            SCP_outputHCR[2] = 0x40;
-            SCP_outputHCR[2] += ( SCP_compress6h(SCP_bin[0])<<6 ) ;
-            SCP_outputHCR[3] = ( SCP_compress6h(SCP_bin[1])<<2 ) + ( SCP_compress6h(SCP_bin[2])>>4) ;
-            SCP_outputHCR[4] = ( SCP_compress6h(SCP_bin[2])<<4 ) + ( SCP_compress6h(SCP_bin[3])>>2) ;
-            SCP_outputHCR[5] = ( SCP_compress6h(SCP_bin[3])<<6 ) + ( SCP_compress6h(SCP_bin[4])) ;
-            SCP_outputHCR[6] = ( SCP_compress6h(SCP_bin[5])<<2 ) + ( SCP_compress6h(SCP_bin[6])>>4) ;
-            SCP_outputHCR[7] = ( SCP_compress6h(SCP_bin[6])<<4 ) + ( SCP_compress6h(SCP_bin[7])>>2) ;
-            SCP_outputHCR[8] = ( SCP_compress6h(SCP_bin[7])<<6 ) + ( SCP_compress6h(SCP_bin[8])) ;
-            SCP_outputHCR[9] = ( SCP_compress6h(SCP_bin[9])<<2 ) + ( SCP_compress6h(SCP_bin[10])>>4) ;
-            SCP_outputHCR[10] = ( SCP_compress6h(SCP_bin[10])<<4 ) + ( SCP_compress6h(SCP_bin[11])>>2);
-            SCP_outputHCR[11] = ( SCP_compress6h(SCP_bin[11])<<6 ) + ( SCP_compress6h(SCP_bin[12])) ;
-            SCP_outputHCR[12] = ( SCP_compress6h(SCP_bin[13])<<2 ) + ( SCP_compress6h(SCP_bin[14])>>4)  ;
-            SCP_outputHCR[13] = ( SCP_compress6h(SCP_bin[14])<<4 ) + ( SCP_compress6h(SCP_bin[15])>>2)  ;
-            SCP_outputHCR[14] = ( SCP_compress6h(SCP_bin[15])<<6 ) + ( SCP_compress6h(SCP_bin[16]))     ;
-            SCP_outputHCR[15] = ( SCP_compress7h(SCP_bin[17])<<1 ) + ( SCP_compress7h(SCP_bin[18])>>6)  ;
-            SCP_outputHCR[16] = ( SCP_compress7h(SCP_bin[18])<<2 ) + ( SCP_compress7h(SCP_bin[19])>>5)  ;
-            SCP_outputHCR[17] = ( SCP_compress7h(SCP_bin[19])<<3 ) + ( SCP_compress6h(SCP_bin[20])>>3)  ;
-            SCP_outputHCR[18] = ( SCP_compress6h(SCP_bin[20])<<5 ) + ( SCP_compress6h(SCP_bin[21])>>1)  ;
-            SCP_outputHCR[19] = ( SCP_compress6h(SCP_bin[21])<<7 ) + ( SCP_compress6h(SCP_bin[22])<<1) +  ( SCP_compress6h(SCP_bin[23])>>5) ;
-            SCP_outputHCR[20] = ( SCP_compress6h(SCP_bin[23])<<3 ) + ( SCP_compress6h(SCP_bin[24])>>3)  ;
-            SCP_outputHCR[21] = ( SCP_compress6h(SCP_bin[24])<<5 ) + ( SCP_compress6h(SCP_bin[25])>>1)  ;
-            SCP_outputHCR[22] = ( SCP_compress6h(SCP_bin[25])<<7 ) + ( SCP_compress6h(SCP_bin[26])<<1) +  ( SCP_compress6h(SCP_bin[27])>>5) ;
-            SCP_outputHCR[23] = ( SCP_compress6h(SCP_bin[27])<<3 ) + ( SCP_compress6h(SCP_bin[28])>>3)  ;
-            SCP_outputHCR[24] = ( SCP_compress6h(SCP_bin[28])<<5 ) + ( SCP_compress6h(SCP_bin[29])>>1)  ;
-            SCP_outputHCR[25] = ( SCP_compress6h(SCP_bin[29])<<7 ) + ( SCP_compress6h(SCP_bin[30])<<1)  ; //last bit is empty
-        
-        /*  for (i=0;i<26;i++){
-                printf("\nscp[%d] = %d",i,SCP_outputHCR[i]);
-            }*/
-            Science_TMframe::making_frame(1,'H',SCP_outputHCR);
+        for(int i=32; i<36; i++){
+            if(scp_bin[i] > scp_threshold_m1[1]){
+                at = 1;
+                break;
+            }
         }
     }
     
-}
- 
\ No newline at end of file
+    for(int i=0; i<17 ;i++){
+        if(scp_bin[i]>0)
+            pzf = 0;
+    }
+    for(int i=18; i<23 ;i++){
+        if(scp_bin[i]>0)
+            ezf = 0;
+    }
+    
+    pointer = scp;      debug_cntr = 0;     space = 8;
+    unsigned char packet_pp = 1;    //where to get this packet present bit----------------------------------------------------------------------------------
+    int sfp_at_counter; //----------------------------------------------------------------------------------------------------------------------------------
+    space = adjust(1, packet_pp,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+    space = adjust(5, (sci_time>>8)&0x1f,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
+    space = adjust(8, (sci_time)&0xff,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
+    space = adjust(4, (attitude)&0xf,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
+    space = adjust(2, compression_option,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
+    space = adjust(1,srp_mode,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
+    space = adjust(1,srp_mode,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
+    space = adjust(8, sfp_at_counter>>16,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
+    space = adjust(8, sfp_at_counter>>8,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+    space = adjust(8, sfp_at_counter,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
+    if(srp_mode == 0 && compression_option == 0){       //normal callibrated mode
+        space = adjust(1, pzf,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
+        space = adjust(1, ezf,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
+        if(at == 0 ){
+            for(int i = 0; i<8 ;i++){
+                space = adjust(7,compress(sfp_bin[24+i],4,3) ,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
+            }
+            for(int i = 0; i<12 ;i++){
+                space = adjust(5,compress(sfp_bin[32+i],2,3) ,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
+            }
+            for(int i = 0; i<4 ;i++){
+                space = adjust(6,compress(scp_bin[44+i],3,3) ,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
+            }
+            for(int i = 0; i<4 ;i++){
+                space = adjust(7,compress(scp_bin[48+i],4,3) ,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
+            }
+            space = adjust(6,compress(scp_bin[17],3,3) ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+            space = adjust(6,compress(scp_bin[23],3,3),pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
+            if(pzf == 0){
+                for(int i = 0; i<2 ;i++){
+                    space = adjust(8,compress(scp_bin[i],6,2) ,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
+                }
+                for(int i = 0; i<15 ;i++){
+                    space = adjust(7,compress(scp_bin[i+2],5,2) ,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
+                }
+            }
+            if(ezf == 0){
+                space = adjust(1,compress(scp_bin[18],7,2)>>8 ,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
+                space = adjust(8,compress(scp_bin[18],7,2) ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+                for(int i=0; i<4; i++){
+                    space = adjust(8,compress(scp_bin[19+i],6,2) ,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
+                }
+            }
+        }// below threshold ends here
+        if(at == 1){
+            for(int i = 0; i<8 ;i++){
+                space = adjust(7,compress(scp_bin[24+i],4,3) ,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
+            }
+            for(int i = 0; i<12 ;i++){
+                space = adjust(5,compress(scp_bin[32+i],2,3) ,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
+            }
+            for(int i = 0; i<4 ;i++){
+                space = adjust(1,compress(scp_bin[44+i],6,3)>>8 ,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
+                space = adjust(8,compress(scp_bin[44+i],6,3) ,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
+            }
+            for(int i=0; i<4 ;i++){
+                space = adjust(2,compress(scp_bin[48+i],6,4)>>8 ,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
+                space = adjust(8,compress(scp_bin[48+i],6,4) ,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
+            }
+            space = adjust(1,compress(scp_bin[17],6,3)>>8 ,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
+            space = adjust(8,compress(scp_bin[17],6,3),pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
+            space = adjust(1,compress(scp_bin[23],6,3)>>8,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
+            space = adjust(8,compress(scp_bin[23],6,3),pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
+            if(pzf == 0){
+                for(int i = 0; i<17 ; i++){
+                    space = adjust(8, (compress(scp_bin[i],5,3)) ,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
+                }
+            }
+            if(ezf == 0){
+                for(int i = 0; i<5 ; i++){
+                    space = adjust(8, (compress(scp_bin[18+i],5,3)) ,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
+                }
+            }
+        }   //above thresholds ends
+    }   //srp_mode == 0 ends
+    if(srp_mode == 1){
+        if(at == 0){
+            for(int i=0; i<32; i++){
+                space = adjust(6, ((compress(scp_bin[i],4,2))&0xff) ,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
+            }
+            for(int i = 0 ; i < 4 ; i++){
+                space = adjust(7, (compress(scp_bin[32+i],4,3))>>8 ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+            }
+        }
+        if(at == 1){
+            for(int i=0; i<32; i++){
+                space = adjust(6, (compress(scp_bin[i],3,3))>>8 ,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
+            }
+            for(int i = 0 ; i < 4 ; i++){
+                space = adjust(4, (compress(scp_bin[32+i],9,3))>>8 ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+                space = adjust(8, (compress(scp_bin[32+i],9,3)) ,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
+            }
+        }
+    }// scp mode 1 end
+    if( srp_mode == 0 && compression_option == 1 ){ //scp data conservation mode
+        space = adjust(1, pzf,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
+        space = adjust(1, ezf,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
+        if(at == 0){
+            if(at == 0){
+                space = adjust(6, (compress(scp_bin[44],3,3)) ,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
+                for(int i=0; i<4; i++){
+                    space = adjust(7, (compress(scp_bin[48+i],4,3)) ,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
+                }
+                space = adjust(6, (compress(scp_bin[17],3,3)) ,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
+                space = adjust(6, (compress(scp_bin[23],3,3)) ,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
+                if(pzf==0){
+                    space = adjust(2, (compress(proton_scp_sum,10,2))>>8 ,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
+                    space = adjust(8, (compress(proton_scp_sum,10,2)) ,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
+                }
+                if(ezf==0){
+                    space = adjust(2, (compress(electron_scp_sum,10,2))>>8 ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+                    space = adjust(8, (compress(electron_scp_sum,10,2)) ,pointer,space);    pointer += space>>4;    debug_cntr += space>>4;
+                }
+            }   // scp data conservaton mode 
+        }else if(at == 1){
+            space = adjust(1, (compress(scp_bin[44],6,3))>>8 ,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
+            space = adjust(8, (compress(scp_bin[44],6,3)) ,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
+            for(int i=0; i<4; i++){
+                space = adjust(4, (compress(scp_bin[48+i],9,3))>>8 ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+                space = adjust(8, (compress(scp_bin[48+i],9,3)) ,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
+            }
+            space = adjust(1, (compress(scp_bin[17],6,3)>>8) ,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
+            space = adjust(8, (compress(scp_bin[17],6,3)) ,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
+            space = adjust(1, (compress(scp_bin[23],6,3))>>8 ,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
+            space = adjust(8, (compress(scp_bin[23],6,3)) ,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
+            if(pzf==0){
+                space = adjust(1, (compress(proton_scp_sum,6,3))>>8 ,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
+                space = adjust(8, (compress(proton_scp_sum,6,3)) ,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
+            }
+            if(ezf==0){
+                space = adjust(1, (compress(electron_scp_sum,6,3))>>8 ,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
+                space = adjust(8, (compress(electron_scp_sum,6,3)) ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+            }
+        }
+    }//-----------------------------------------------------------------------------------------------------------------------------------------------------
+    //time to make scp frame
+    id = 0;
+    if(srp_mode == 0 && compression_option == 0){
+        if(at == 0){
+            length = 228;
+            if(pzf == 0)
+                length += 121;
+            if(ezf == 0)
+                length +=41;
+        }else if(at == 1){
+            length = 266;
+            if(pzf == 0)
+                length += 136;
+            if(ezf == 0)
+                length += 40;
+        }
+            
+    }else if(srp_mode == 0 && compression_option == 1){     //data conservation mode
+        if(at == 0){
+            length = 94;
+            if(pzf == 0)
+                length += 12;
+            if(ezf == 0)
+                length += 12;
+        }else if(at == 1){
+            length = 123;
+            if(pzf == 0)
+                length += 9;
+            if(ezf == 0)
+                length += 9;
+        }
+    }else if( srp_mode == 1){
+        if(at == 0)
+            length = 272;
+        else if(at == 1)
+            length = 288;
+    }
+    length = (length%8==0)?(length/8):(length/8)+1;
+    for(int j= 0; j < length ; j++){
+        frames[id][position_tm_frame[id]] = scp[j];
+            position_tm_frame[id]++;
+            if(position_tm_frame[id] == 132){   //space full in the frame bro
+                pointer = frames[id];
+                space = adjust(1,0,pointer,8);
+                space = adjust(4,1,pointer,space);
+                FSC_science = FCTN_SD_MNGR(1);
+                gPC.printf("SID = 1, FSC = %02X", FSC_science);
+                frames[id][1] = (FSC_science>>16)&0xff;
+                frames[id][2] = (FSC_science>>8)&0xff;
+                frames[id][3] = (FSC_science)&0xff;
+                frames[id][5] = (sci_time>>16)&0x3f;
+                frames[id][6] = (sci_time>>8)&0xff;
+                frames[id][7] = (sci_time)&0xff;
+                temp_crc = crc16_gen(frames[id],132);
+                frames[id][132] = temp_crc>>8;
+                frames[id][133] = temp_crc & 0xff;
+                convolution(frames[id]);
+                interleave(TM_convoluted_data,TM_interleave_data);
+                interleave(TM_convoluted_data+ 135,TM_interleave_data + 144);
+                SD_WRITE(TM_interleave_data,FSC_science,1);
+                position_tm_frame[id] = position_tm_starting[id];
+                frames[id][4] = (length-1) - j;
+            }
+    }
+}//srp fucntion end bracket
\ No newline at end of file
--- a/DefinitionsAndGlobals.h	Sat Jan 23 12:59:06 2016 +0000
+++ b/DefinitionsAndGlobals.h	Sun Jan 24 09:30:22 2016 +0000
@@ -49,7 +49,7 @@
     #define COM_MNG_TMTC_SIGNAL_UART_INT 0x01
     #define COM_MNG_TMTC_SIGNAL_ADF_NSD 0x02
     #define COM_MNG_TMTC_SIGNAL_ADF_SD 0x03
-
+    #define SCIENCE_SIGNAL 0x04
 // COM_MNG_TMTC
     #define COM_PA_COOLING_TIME_LIMIT 20
     #define COM_MAX_TC_LIMIT 200
--- a/ThreadsAndFunctions.h	Sat Jan 23 12:59:06 2016 +0000
+++ b/ThreadsAndFunctions.h	Sun Jan 24 09:30:22 2016 +0000
@@ -269,6 +269,34 @@
     }
 }
 
+
+uint8_t payload_data[6723];
+ 
+void SCIENCE_FUN(void const *args){
+    uint8_t *ptr;
+    ptr = payload_data+3;
+    int counter = 0;
+    payload_data[2] = 0;//for mode
+    for(int i = 0; i<60 ;i++){      //for mode 0.....callibra
+        for(int j= 0; j<48; j++){   //2 byte values
+            ptr[counter++] = 0;     ptr[counter++] = 65;
+        }
+        for(int j=96; j <100 ; j++){
+            ptr[counter++] = 0;     ptr[counter++] = 0;     ptr[counter++] = 0;     ptr[counter++] = 65;    
+        }
+        ptr = ptr+112;
+        counter = 0;
+    }
+    
+    srp(payload_data);
+//    while(true){
+//        gSCIENCE_THREAD->signal_wait(SCIENCE_SIGNAL);
+//        srp(payload_data);
+//    }
+}
+
+
+
 //void SCIENCE_FUN(void const *args){
 ////    SPIpayload.format(8,0);
 ////    SPIpayload.frequency(1000000);
--- a/cdms_rtc.h	Sat Jan 23 12:59:06 2016 +0000
+++ b/cdms_rtc.h	Sun Jan 24 09:30:22 2016 +0000
@@ -1,5 +1,6 @@
 void FCTN_CDMS_INIT_RTC()
 {
+    SPI_mutex.lock();
     gCS_RTC=1;
     spi.format(8,0);
     spi.frequency(1000000);
@@ -74,10 +75,12 @@
     spi.write(0x01); //set year to 00(2000)
     gCS_RTC=1;
     printf("\n\r rtc initalised \n");
+    SPI_mutex.unlock();
 }
  
 uint64_t FCTN_CDMS_RD_RTC()
 {    
+    SPI_mutex.lock();
     uint8_t response;
     printf("\n\r Entered rtc\n");
     gCS_RTC=1;
@@ -145,4 +148,5 @@
     //printf("\n\r%x%x", (int)(time >> 32), (int)(time));
     printf("\n\r0x%016llx\n\r", time);
    return time;
+   SPI_mutex.unlock();
 }
\ No newline at end of file
--- a/main.cpp	Sat Jan 23 12:59:06 2016 +0000
+++ b/main.cpp	Sun Jan 24 09:30:22 2016 +0000
@@ -13,12 +13,12 @@
 #include "cdms_sd.h"
 #include "OBSRS.h"
 #include "adf.h"
-//#include "Compression.h"
 //#include "SDC_init.h"
 #include "COM_RCV_TC.h"
 #include "COM_MNG_TMTC.h"
 #include "COM_POWER_ON_TX.h"
 #include "COM_POWER_OFF_TX.h"
+#include "Compression.h"
 #include "ThreadsAndFunctions.h"
 
 int main()
@@ -45,6 +45,10 @@
     cs_sd = 1;
     gCS_RTC = 1;
     gCS_ADF = 1;
+    
+    FCTN_CDMS_INIT_RTC();/* rtc initialization*/
+    FCTN_CDMS_SD_INIT();/* sd card initialization*/
+    
     // initialise_card();
     // int result= initialise_card();
     // disk_initialize();    
@@ -57,6 +61,8 @@
     // COM_MNG_TMTC THREAD
     gCOM_MNG_TMTC_THREAD = new Thread(COM_MNG_TMTC_FUN);
     gCOM_MNG_TMTC_THREAD->set_priority(osPriorityAboveNormal);
+    gSCIENCE_THREAD = new Thread(SCIENCE_FUN);
+    gSCIENCE_THREAD->set_priority(osPriorityBelowNormal);
     // *******************INITIALISATIONS END********************
     
     int theAcount = 0;