//mikhailov :
//reading and updating of time sync information

#include "descript.h"
#include "CRC.h"
//int RTtoMT,RT,Timesync,OBTtimesync;  // Resurs time to moscow, Resurs time, ...

extern long  int  Timesync;
extern long int  OBTtimesync;
void RunHeaderReader(char *packet)
 {

//char *packet=new char[Nmax];
 //unsigned int type=0; /*PAMELA packet type*/
// unsigned int error=0;/*PAMELA packet error flag*/
 //int Length=0;        /*Length of filename*/
// long int counter[26];/*Counter for PAMELA packets in each dat file*/
// unsigned long int adr1=0;/*First byte of PAMELA packet in _cln2.pam*/
 //unsigned long int adr2=0;/*Last byte of PAMELA packet in _cln2.pam*/
 long int time=0;     /*PAMELA packet time*/
// long int first[26];  /*First byte for PAMELA packets of given type*/
// long int last[26];   /*Last byte for PAMELA packets of given type*/
 long int temp=0;
 //int type;
// int packet_type;
    char     *subData; 
//    char      eventCRC[2];
    int j;
      FILE  *ftime_sync;
    unsigned short    calCRC;      //calculated CRC of the data
    unsigned short    readCRC;     //CRC read from the end of the subpacket
    long int  dataLength;
    long int length;
     long int RM_ACQ_SETTING_MODE,OBT_TIME_SYNC,
     LAST_TYME_SYNC_INFO,
    FAVOURITE_WORKING_SCHEDULE,
    EFFECTIVE_WORKING_SCHEDULE,
    PRH_VAR_TRIGGER_MODE_A,
    PRH_VAR_TRIGGER_MODE_B,
    RM_ACQ_AFTER_CALIB ;  
   time=(long int)(unsigned char)(packet[11])+
   	          256*(long int)(unsigned char)(packet[10])+
          256*256*(long int)(unsigned char)(packet[9])+
      256*256*256*(long int)(unsigned char)(packet[8]);
  
   temp=(long int)(unsigned char)(packet[7])+
   	          256*(long int)(unsigned char)(packet[6])+
          256*256*(long int)(unsigned char)(packet[5]);
   if (fmod((float)(temp),(float)(10000.))==0.)
	  cout<<"sorting "<<temp<<"\n";
 /*unpacking several packets....   mikhailov
 unpackingPamelaPacket(packet,type) */
  length=(long int)(unsigned char)(packet[14])+
   	          256*(long int)(unsigned char)(packet[13])+
          256*256*(long int)(unsigned char)(packet[12]);

 //cout<<temp<<" time="<<time<<" type=" <<type<<" length="<<length<<"\n";
    
 
    //the 2 bytes subtracted belong to the final event CRC bytes
   dataLength = length - (long int)2;
  subData=new char[dataLength];
  
  for (j=0;j<dataLength+2;j++) subData[j]=packet[j+16];
//  printf("packet %i  %x ",j,(BYTE)packet[j+16]);
//  printf(" SubD= %x \n",(BYTE)subData[j]); }

  readCRC=(unsigned short)((((UINT32)packet[42]<<8)&0x0000FF00) + (((UINT32)packet[43])&0x000000FF));
    /*
    InputFile->read(subData, sizeof(unsigned char)*dataLength); */
     
    calCRC = CM_Compute_CRC16(0, (unsigned char*)subData, dataLength);
      printf("crc   %x  %x\n",calCRC,readCRC);
    //took the final CRC to compare it with the previous calculated CRC of the data
    
//    for(j=0;j<length;j++) printf("crc %x \n",(BYTE)subData[j]);
   
  //   cout<<calCRC<<" =crc ?="<<readCRC<<"\n";
    if (calCRC == readCRC) {cout<<calCRC<<" =crc ok="<<"\n";
    
    RM_ACQ_SETTING_MODE = (BYTE)packet[16];
    OBT_TIME_SYNC       = (((UINT32)packet[21]<<24)&0xFF000000) + (((UINT32)packet[22]<<16)&0x00FF0000) +  (((UINT32)packet[23]<<8)&0x0000FF00) + (((UINT32)packet[24])&0x000000FF);
    LAST_TYME_SYNC_INFO = (((UINT32)packet[25]<<24)&0xFF000000) + (((UINT32)packet[26]<<16)&0x00FF0000) +  (((UINT32)packet[27]<<8)&0x0000FF00) + (((UINT32)packet[28])&0x000000FF);
    FAVOURITE_WORKING_SCHEDULE = (BYTE)packet[25];
    EFFECTIVE_WORKING_SCHEDULE = (BYTE)packet[26];
    PRH_VAR_TRIGGER_MODE_A  = (((UINT32)packet[27]<<24)&0xFF000000) + (((UINT32)packet[28]<<16)&0x00FF0000) +  (((UINT32)packet[29]<<8)&0x0000FF00) + (((UINT32)packet[30])&0x000000FF);
    PRH_VAR_TRIGGER_MODE_B  = (((UINT32)packet[31]<<24)&0xFF000000) + (((UINT32)packet[32]<<16)&0x00FF0000) +  (((UINT32)packet[33]<<8)&0x0000FF00) + (((UINT32)packet[34])&0x000000FF);
    RM_ACQ_AFTER_CALIB      = (BYTE)packet[35];
    cout<<OBT_TIME_SYNC<<" = ="<<LAST_TYME_SYNC_INFO<<"\n";
    



// mikhailov 06.12.2004
 ftime_sync=fopen("timesync.dat","a+");
  Timesync=LAST_TYME_SYNC_INFO;
  OBTtimesync=OBT_TIME_SYNC;
extern long int RTtoMT;
extern long int RT;
 fprintf(ftime_sync,"%u %u %u %u\n",RTtoMT,RT,OBTtimesync,Timesync);
 //  read for initial information  for time synchronization

 cout<<RTtoMT<<"\n";
    cout<<RT<<"\n";
    cout<<Timesync<<"\n";
    cout<<OBTtimesync<<"\n";

                              
                              fclose(ftime_sync);}
 // end mikhailov  
 //RTtoMT,RT,Timesync,OBTtimesync;

    
}
