///////////////PAMELA PACKETS READER/////////////////////////////////
#include "descript.h"
#include "flag.h"

#include "CRC.h"

short int CRC_H(char [], char []);
extern char MYSQL[80];
extern char database[80];
extern float YODA_flag;
extern float transmit_flag;
extern int print_flag;

void verify(char fni[], char inf[], char log[], char pam[],
		  char fno[], char path[], char name_ini[], char name_in[], int packet_type[],
		  float IndexQ, int r_counter, int real_route[])
{
 FILE *fo_log, *fo_inf, *fo_miss, *fo_row;

 time_t seconds;

 struct tm *timeinfo;
 char time_year[5], time_month[5], time_day[5];
 char data_time[80];
 char cirquit_no[10], route_no[80], name_path[80], name_cln2[80];
 char connection[80];
 bool file_ready, file_deleted, file_archived, file_proces;
 int flag_to_transmit=0;
 int flag_sec_trans=0;             /*Second transmission flag*/
 int num_rows=0;
 int current_route=0;

 char str_route[10];
 char str_file[80];

 char ch, ttt[80];
 char miss_file[80];
 char Name_Buf[9];                 /*Name buffer*/
 char Name_Buf_add[9];             /*Header buffer*/
 char buff[16];
 char Name_Buf_old[9];             /*Old name buffer*/
 char Name_Buf_add_old[9];         /*Old header buffer*/
 
 short int CalcCheckSum=0;
 int num_type=0;                   /*Packet type number*/
 int num_type1=0;
 int num_type2=0;
 int stop=0;
 int Hex[3];
 int Byte_adr1[4];                 /*Bytes for Packet begin packing*/
 int Byte_adr2[4];                 /*Bytes for Packet end packing*/
 int packet_counter[NumberPack+1]; /*Counter for each packet type*/
 int count_name_cor=0;             /*Counter for packet with corrected name*/
 int er_pack_CRC=0;                /*CRC error packet counter*/
 int er_pack_Len=0;                /*Length error packet counter*/ 
 int er_pack_count=0;              /*Errors counter*/ 
 int count_miss=0;                 /*Missed packet counter*/
 int count_sec_miss=0;
 int P_er=0;                       /*Number of packet with counter error*/
 int flag_CRC_H[3];                /*Flag for CRC header error*/
 int flag_pack[3];                 /*Flag for error in packet counter*/
 int flag_len[3];                  /*Flag for PAMELA packet length error*/
 int flag_len_weak[3];             /*Suspicion flag for PAMELA packet length error*/
 int flag_name[3];                 /*Flag for name error*/
 int flag_type[3];                 /*Flag for packet type error*/
 int flag_time[3];                 /*Flag for time error*/
 int flag_CRC_cor[3];              /*Flag for CRC error correction*/
 int CRC_bad_pack[3];              /*Current header CRC bad packet number*/
 int CRC_Header[3];                /*CRC in header for previous packets*/
 int Mis_group=0;                  /*Number of missed packet group*/
 short int flags=0;                /*Number for all errors*/
 long int tmpP=0;                  /*Temporary PAMELA packet number*/
 long long int icount=0;           /*Number of bytes in identified packets*/ 
 long long int icount_total=0;     /*Total number of bytes*/
 long int icount_int=0;            /*Entire part of total file size in Kbytes*/
 long int icount_frac=0;           /*Fractional part of total file size*/
 long int N1=0;                    /*Number of identified PAMELA packets*/
 long int numP[3];	               /*Packet numbers for previous packets*/
 long int LenP[3];                 /*Packet length for previous packets*/
 unsigned long int time_p[3];      /*Packet time*/
 unsigned long int time_max=0;
 unsigned long int time_min=0;
 long int pack_length=0;           /*PAMELA packet length*/
 long long int adr_first[3];       /*Start displacement from file begin*/
 long long int adr_last[3];        /*Finish displacement from file begin*/
 long int temp[2];  
 char *buffer=new char[Nmax];      /*Buffer for PAMELA current packet*/
 int FLAG_BIG=0;                   /*Flag for large packet*/
 unsigned long int k=0, j=0;
 int i;
 //initialization  , all =0
 float ratioCln1Cln2;
 int output;
 char str[80];

 stringstream oss, oss1;

 strcpy(connection,MYSQL);
 strcat(connection,database);
 TSQLServer *sqlServer = TSQLServer::Connect(connection, "pamelaprod", "rawprod");

 oss.str("");
 oss<<"select * from FILES1";
 TSQLResult *res1=sqlServer->Query(oss.str().c_str());
 num_rows=res1->GetRowCount();
 if (num_rows>0)
 {
  oss.str("");
  oss<<"select max(my_route) from FILES1";
  TSQLResult *res2=sqlServer->Query(oss.str().c_str());  
  TSQLRow *row=res2->Next();
  strcpy(str_route,row->GetField(0));

  strcpy(str_file,inf);
  str_file[strlen(inf)-9]='\0';
  strcat(str_file,"_tmp.dat"); 
  fo_row=fopen(str_file,"w+"); 
  fprintf(fo_row,"%s",str_route);
  fseek(fo_row,0,0);
  fscanf(fo_row,"%i",&current_route); 
  current_route+=1; 
  fclose(fo_row);
  remove(str_file); 
 }
 else
 {
  current_route=1;
 }

 for (i=0; i<NumberPack+1; i++) packet_counter[i]=0;
 for (i=0; i<3; i++)
 {numP[i]=0; time_p[i]=0; CRC_Header[i]=0; CRC_bad_pack[i]=0;
  flag_CRC_H[i]=0; flag_len[i]=0; flag_len_weak[i]=0; flag_pack[i]=0; flag_CRC_cor[i]=0;
  flag_name[i]=0; flag_type[i]=0; flag_time[i]=0;
  LenP[i]=0;
  adr_first[i]=0; adr_last[i]=0;
  Hex[i]=0;}
 for (i=0; i<4; i++) {Byte_adr1[i]=0; Byte_adr2[i]=0;}
 for (i=0; i<2; i++) temp[i]=0;

 ifstream fin;
 ofstream fo_pam, fo_work;  
/**********************OPEN NECESSARY FILES************************/
/******************************************************************/
 fin.open(fni, ios::binary);     // open input file "nnnnn_mmm_xxx_cln1.pam"
 if(fin.good()==0) {cerr<<"can not open input file "<<fni<<"\n"; exit(1);}

 fo_work.open(fno, ios::out);    //open output help file "work.dat"
 if(!fo_work) {cerr<<"can not open outputfile"; exit(1);}

 fo_log=fopen(log, "wb");        //open output file "xxx_mmm_nnnnn_cln2.log"
 if(fo_log==NULL) {cerr<<"can not open log file"; exit(1);}

 fo_pam.open(pam, ios::binary);  //open output file "xxx_mmm_nnnnn_cln2.pam"
 if(fo_pam.good()==0) {cerr<<"can not open pam file"; exit(1);}

 fo_inf=fopen(inf, "wb");        //open output file "xxx_mmm_nnnnn_cln2.inf"
 if(fo_inf==NULL) {cerr<<"can not open inf file"; exit(1);}

 strcpy(miss_file,path);
 strncat(miss_file,name_in,strlen(name_in)-8); 
 fo_miss=fopen(strcat(miss_file,"mis_cln2.dat"), "wb");
                                 //open output file "xxx_mmm_nnnnn_mis_cln2.dat"
 if(fo_miss==NULL) {cerr<<"can not open miss file"; exit(1);}
/******************************************************************/
/******************************************************************/

//Write proccessed filename in help file
  fo_work<<"initial file name : "<<name_in<<"\n"; 
 
/******************************************************************/
/******************MAIN READING CYCLE BEGIN************************/
/******************************************************************/

 while(fin.eof()==0)
 {
  num_type=0; num_type1=0;  num_type2=0;

/**********************data reading********************************/
  if ((N1==0)&&(icount_total==0)) {fin.read(Name_Buf,8); icount_total+=8;}

/*******************temporary packet number************************/
  tmpP=(long int)(unsigned char)(Name_Buf[7])+
   	         256*(long int)(unsigned char)(Name_Buf[6])+
             256*256*(long int)(unsigned char)(Name_Buf[5]);

  if (N1==0) numP[2]=tmpP-1;    //initial packet identification

/******************packet type identification**********************/
  for (i=0;i<NumberPack;i++)
  {if (packet_type[i]==(int)(unsigned char)(Name_Buf[3]))
      num_type1=i+1;
   if (packet_type[i]==(int)(unsigned char)(Name_Buf[4]))
	  num_type2=i+1;}
  if ((num_type1==num_type2)&&(num_type1!=0)) num_type=num_type1;
  if ((num_type1==0)&&(num_type2!=0)) num_type=num_type2;	  
  if ((num_type1!=0)&&(num_type2==0)) num_type=num_type1;

///////////////////////////////////////////////////////////////////
////////////////// PAMELA PACKET IDENTIFICATION////////////////////
///////////////////////////////////////////////////////////////////
  if (((int)(unsigned char)Name_Buf[0]==CODE_BYTE1&&
	   (tmpP==numP[2]+1)&&num_type!=0&&N1!=0)||
      ((int)(unsigned char)Name_Buf[1]==CODE_BYTE2&&
	   (tmpP==numP[2]+1)&&num_type!=0&&N1!=0)||
	  ((int)(unsigned char)Name_Buf[2]==CODE_BYTE3&&
	   (tmpP==numP[2]+1)&&num_type!=0&&N1!=0)||
	  ((int)(unsigned char)Name_Buf[0]==CODE_BYTE1&&
	   (int)(unsigned char)Name_Buf[1]==CODE_BYTE2&&
	   (int)(unsigned char)Name_Buf[2]==CODE_BYTE3&&num_type1!=0&&num_type1==num_type2)||
	  ((int)(unsigned char)Name_Buf[0]==CODE_BYTE1&&
	   (int)(unsigned char)Name_Buf[1]==CODE_BYTE2&&
	   (int)(unsigned char)Name_Buf[2]==CODE_BYTE3&&
	   (tmpP==numP[2]+1)))	   
  {if (N1==0) {for (i=0;i<8;i++) buffer[i]=Name_Buf[i]; }
   N1+=1; FLAG_BIG=0;
   if (N1==1) {icount+=8;}    
   packet_counter[num_type]+=1;  //count packet number for each type
   if ((print_flag>0) && (fmod((float)(tmpP),(float)(1000.))==0.)&&(tmpP!=0)) cout<<"verifying "<<tmpP<<"\n";
   if (N1!=1) 
   {
/**************PAMELA PREVIOUS PACKET END DISPLACEMENT*************/
    if ((icount-8-adr_first[2]+1-LenP[2]-16)<=Limit) {adr_last[2]=icount-8;}               //end byte for previous packet
    else if ((icount-8-adr_first[2]+1-LenP[2]-16)>Limit) {adr_last[2]=adr_first[2]+Limit;} //end byte for previous packet
/**************PAMELA PACKET LENGTH ERROR ANALYSIS*****************/
	 if ( (flag_CRC_H[2]==1)&&(GLOBAL_LEN_ER>0)&&
		  ( ((adr_last[2]-adr_first[2]+1)<(LenP[2]+16))||((adr_last[2]-adr_first[2]+1-LenP[2]-16)>64) ) )
	 {flag_len[2]=1;
	  er_pack_Len+=1;
	  if (print_flag>0) 
	  {cout<<"pack num "<<numP[2]<<" len="<<LenP[2]<<" true len="<<adr_last[2]-adr_first[2]-16<<" Type="<<num_type<<"\n";}
	  fo_work<<"Length error : packet number="<<numP[2]<<" : length in header="<<LenP[2]<<
	           " : length till next header="<<adr_last[2]-adr_first[2]-16<<" : packet type="<<hex<<
		   packet_type[num_type]<<dec<<"\n";
         }
	 if ( (flag_CRC_H[2]==0)&&(GLOBAL_LEN_ER>0)&&
		  ( ((adr_last[2]-adr_first[2]+1)<(LenP[2]+16))||((adr_last[2]-adr_first[2]+1-LenP[2]-16)>64) ) )
	 {flag_len_weak[2]=1;
	  if (print_flag>0) 
	  {cout<<"weak length error; pack num "<<numP[2]<<" len="<<LenP[2]<<" true len="<<adr_last[2]-adr_first[2]-16
	       <<" Type="<<num_type<<"\n";}
	  fo_work<<"Weak length error : packet number="<<numP[2]<<" : length in header="<<LenP[2]<<
	           " : length till next header="<<adr_last[2]-adr_first[2]-16<<" : packet type="<<num_type<<"\n";
         }

    if ((GLOBAL_LEN_ER>0)&&(flag_len[2]==1))
    {
//Write data in file "nnnnn_mmm_xxx_cln2.pam"
     for (i=0;i<adr_last[2]-adr_first[2]+1;i++)
     {fo_pam<< buffer[i];}
    }
    else if ((GLOBAL_LEN_ER>0)&&(flag_len[2]!=1)&&(flag_len_weak[2]!=1))
    {
//Write data in file "nnnnn_mmm_xxx_cln2.pam"
     for (i=0;i<LenP[2]+16;i++)
     {fo_pam<< buffer[i];}
     adr_last[2]=adr_first[2]+LenP[2]+15;
     icount=adr_first[2]+15+LenP[2]+8;
    }
    else if ((GLOBAL_LEN_ER>0)&&(flag_len[2]!=1)&&(flag_len_weak[2]==1))
    {
//Write data in file "nnnnn_mmm_xxx_cln2.pam"
     for (i=0;i<adr_last[2]-adr_first[2]+1;i++)
     {fo_pam<< buffer[i];}
    }    
    else if ((GLOBAL_LEN_ER==0)&&(adr_last[2]-adr_first[2]+1-LenP[2]-16>=0))
    {
     for (i=0;i<LenP[2]+16;i++)
     {fo_pam<< buffer[i];}
     adr_last[2]=adr_first[2]+LenP[2]+15;
     icount=adr_first[2]+15+LenP[2]+8;
    }
    delete [] buffer;
    buffer=new char[Nmax];
    for (i=0;i<8;i++) buffer[i]=Name_Buf[i]; 

    temp[1]=adr_last[2];
    for (i=0;i<4;i++) Byte_adr2[i]=0;
    for (i=0;i<4;i++)
    {if (temp[1]/(long int)(pow(256.,3-i))>=1) 
     {Byte_adr2[3-i]=(int)(temp[1]/(long int)(pow(256.,3-i)));
      temp[1]=temp[1]-(long int)pow(256.,3-i)*
      (int)(temp[1]/(long int)(pow(256.,3-i)));}}	
      //Write previous packet end displacement in nnnnn_mmm_xxx_cln2.log file
     for (i=0;i<4;i++) fprintf(fo_log,"%1c",char(Byte_adr2[i]));
    }
/*********************reading remaining header********************/
   fin.read(Name_Buf_add, 8);
   if (fin.eof()) break;
   icount+=8; icount_total+=8;
   for (i=0;i<8;i++) buffer[i+8]=Name_Buf_add[i];

//packet length
   pack_length=(long int)(unsigned char)(Name_Buf_add[6])+
           256*(long int)(unsigned char)(Name_Buf_add[5])+
       256*256*(long int)(unsigned char)(Name_Buf_add[4]);
/***********PACKET HEADER DATA ANALYSIS****************************/
   switch (N1)
   { 
    case 1:{
            LenP[2]=pack_length;
		    numP[2]=(long int)(unsigned char)(Name_Buf[7])+
   	            256*(long int)(unsigned char)(Name_Buf[6])+
            256*256*(long int)(unsigned char)(Name_Buf[5]);	            
		    time_p[2]=(long int)(unsigned char)(Name_Buf_add[3])+
   	                      256*(long int)(unsigned char)(Name_Buf_add[2])+
                         256*256*(long int)(unsigned char)(Name_Buf_add[1])+
		    256*256*256*(long int)(unsigned char)(Name_Buf_add[0]);
		    time_max=time_p[2];
		    time_min=time_p[2];

			CRC_Header[2]=(int)(unsigned char)(Name_Buf_add[7]);
			adr_first[2]=icount-15;
			CRC_bad_pack[2]=0;
            flag_CRC_H[2]=0;
			flag_pack[2]=0;
			flag_name[2]=0;
			flag_type[2]=0;
			flag_time[2]=0;
			flag_CRC_cor[2]=0;
            for (i=0; i<2; i++)
			{numP[i]=numP[2]; time_p[i]=time_p[2];
			 CRC_Header[i]=CRC_Header[2]; 
			 LenP[i]=LenP[2];
			 adr_first[i]=adr_first[2];
			 CRC_bad_pack[i]=CRC_bad_pack[2];
			 flag_CRC_H[i]=flag_CRC_H[2];
			 flag_pack[i]=flag_pack[2];
             flag_name[i]=flag_name[2];
             flag_type[i]=flag_type[2];
             flag_time[i]=flag_time[2];
             flag_CRC_cor[i]=flag_CRC_cor[2];}
			break;
		   }
    default:{
             for (i=0; i<2; i++)
			 {numP[i]=numP[i+1];
			  time_p[i]=time_p[i+1];
			  CRC_Header[i]=CRC_Header[i+1];
			  CRC_bad_pack[i]=CRC_bad_pack[i+1];
			  LenP[i]=LenP[i+1];
			  adr_first[i]=adr_first[i+1];
                          adr_last[i]=adr_last[i+1];
			  flag_CRC_H[i]=flag_CRC_H[i+1];
                          flag_len[i]=flag_len[i+1];
                          flag_len_weak[i]=flag_len_weak[i+1];
			  flag_pack[i]=flag_pack[i+1];
			  flag_name[i]=flag_name[i+1];
			  flag_type[i]=flag_type[i+1];
			  flag_time[i]=flag_time[i+1];
			  flag_CRC_cor[i]=flag_CRC_cor[i+1];}
		          numP[2]=(long int)(unsigned char)(Name_Buf[7])+
   	                      256*(long int)(unsigned char)(Name_Buf[6])+
                          256*256*(long int)(unsigned char)(Name_Buf[5]);
		          time_p[2]=(long int)(unsigned char)(Name_Buf_add[3])+
   	                         256*(long int)(unsigned char)(Name_Buf_add[2])+
                             256*256*(long int)(unsigned char)(Name_Buf_add[1])+
			 256*256*256*(long int)(unsigned char)(Name_Buf_add[0]);			
                           
			 CRC_Header[2]=(int)(unsigned char)(Name_Buf_add[7]);
			 CRC_bad_pack[2]=0;
			 adr_first[2]=icount-15;
			 flag_CRC_H[2]=0;			 
                         flag_len[2]=0;
                         flag_len_weak[2]=0;
			 flag_pack[2]=0;
			 flag_name[2]=0;
			 flag_type[2]=0;
			 flag_time[2]=0;
			 flag_CRC_cor[2]=0;
                         LenP[2]=pack_length;
			}
   }
/********packet header CRC error analysis**************************/
//   CalcCheckSum=CRC_H(Name_Buf, Name_Buf_add);
  
   for (i=0; i<8; i++)
   {buff[i]=Name_Buf[i];
    buff[i+8]=Name_Buf_add[i];}

   CalcCheckSum=(short int)(unsigned char)CM_Compute_CRC16(0, (unsigned char*)&buff, 15); 
   if (CalcCheckSum!=(short int)(unsigned char)(Name_Buf_add[7]))
   {er_pack_CRC+=1;
    CRC_bad_pack[2]=numP[2];
    flag_CRC_H[2]=1;
    fo_work<<"CRC error : PAMELA packet="<<numP[2]<<" : real CRC="<<bitset<8>(CRC_Header[2])<<"("<<CRC_Header[2]<<
             ") : calculated CRC="<<bitset<8>(CalcCheckSum)<<"("<<CalcCheckSum<<")\n";
   }
   if ((time_max<time_p[2])&&(flag_CRC_H[2]!=1)) time_max=time_p[2];
/*********packet counter error analysis****************************/
   if ((numP[2]!=(numP[1]+1))&&(N1!=1)&&(flag_len[1]==0)&&(CRC_bad_pack[2]==numP[2])) 
   {
    fo_work<<"Packet number error : packet="<<numP[2]<<" : previous="<<numP[1]<<" "<<numP[0]<<"\n";
    flag_pack[2]=1;
   }

   if ((numP[1]!=(numP[0]+1))&&(numP[1]!=(numP[2]-1))&&(N1!=1)&&(CRC_bad_pack[1]==numP[1])) 
   {
    fo_work<<"Packet number error : packet="<<numP[1]<<" : previous="<<numP[0]<<" : next="<<numP[2]<<"\n";

    P_er=N1-1; flag_pack[1]=1;

//Correct error packet number//
   tmpP=numP[0]+1;
    for (i=0;i<3;i++) Hex[i]=0;
    for (i=0;i<3;i++)
    {if (tmpP/(long int) (pow(256.,2-i))>=1) 
     {Hex[2-i]=(int)(tmpP/(long int)(pow(256.,2-i)));
      tmpP=tmpP-(long int)(pow(256.,2-i)*(int)(tmpP/(long int)(pow(256.,2-i)) ));
     }
    }
    for (i=5;i<8;i++) (Name_Buf_old[i]=(char)(Hex[7-i]));
    for (i=0; i<8; i++)
    {buff[i]=Name_Buf_old[i];
     buff[i+8]=Name_Buf_add_old[i];}	
     if (CRC_Header[1]==(short int)(unsigned char)CM_Compute_CRC16(0, (unsigned char*)&buff, 15))
     {flag_CRC_cor[1]=1;}
//Correct error packet number//
    tmpP=numP[2]-1;
    for (i=0;i<3;i++) Hex[i]=0;
    for (i=0;i<3;i++)
    {if (tmpP/(long int)(pow(256.,2-i))>=1) 
     {Hex[2-i]=(int)(tmpP/(long int)(pow(256.,2-i)));
      tmpP=tmpP-(long int)pow(256.,2-i)*(int)(tmpP/(long int)(pow(256.,2-i)));
     }
    }
    for (i=5;i<8;i++) (Name_Buf_old[i]=(char)(Hex[7-i]));
    for (i=0; i<8; i++)
    {buff[i]=Name_Buf_old[i];
     buff[i+8]=Name_Buf_add_old[i];}	    
    if (CRC_Header[1]==(short int)(unsigned char)CM_Compute_CRC16(0, (unsigned char*)&buff, 15))
    {flag_CRC_cor[1]=1;}
   }
/*********************Missing packet analysis**********************/
/**packet numbers:  21 (22 17 18) 19 ******************************/
/***missed packets: 21  20 19 18  17 ******************************/  
   if ((numP[1]<numP[0])&&(numP[1]==numP[2]-1)&&(flag_pack[0]==0)&&(flag_pack[1]==0)&&(flag_pack[2]==0))
   {Mis_group+=1;
    count_sec_miss+=numP[0]-1-numP[1];
    fprintf(fo_miss,"secondary transmitted packets may be %9i -%9i number of packets=%9i\n",
                    numP[1],numP[0],numP[0]-1-numP[1]);
   } 
/**packet numbers: 10 11 (12 15 19) 20 ****************************/
/***********missed packets: 16 17 18 ********************************/
   else if ((numP[1]>numP[0]+1)&&(numP[1]<numP[2]-1)&&(flag_pack[0]==0)&&(flag_pack[1]==0)&&(flag_pack[2]==0))
   {Mis_group+=1;
    fprintf(fo_miss,"1) %9i%9i\n",numP[1]+1,numP[2]-1);
    count_miss+=numP[2]-numP[1]-1;}
/**packet numbers: 10 (11 12 15) 19 20 ****************************/
/***********missed packets: 13 14 ***********************************/
   else if ((numP[1]>=numP[0]+1)&&(numP[1]<numP[2]-1)&&(flag_pack[2]==0)&&(flag_pack[1]==0)&&(flag_pack[0]==0))
   {Mis_group+=1;
    fprintf(fo_miss,"2) %9i%9i\n",numP[0]+2,numP[2]-1);
    count_miss+=numP[2]-numP[1]-1;}
/**packet numbers: 10 (11 20 15) 16 17 ****************************/
/********missed packets number =2 ***********************************/
   else if ((numP[0]<numP[2]-2)&&(flag_pack[2]==0)&&(flag_pack[1]==1)&&(flag_pack[0]==0))   
   {Mis_group+=1;
    fprintf(fo_miss,"3) %9i%9i\n",numP[0]+1, numP[2]-1);
    count_miss+=numP[2]-numP[0]-2;}

/**************Time error******************************************/
   if ((numP[2]>=numP[0]+2)&&(flag_pack[2]==0)&&(flag_pack[0]==0)&&
	   (flag_time[0]==0)&&(time_p[2]>time_p[0])&&
	   ((time_p[1]<time_p[0])||(time_p[1]>time_p[2]))) 
   {flag_time[1]=1;
    fo_work<<"Time error : PAMELA packet="<<numP[2]<<" : time="<<time_p[1]<<" : time previous="<<time_p[1]
           <<" : time_next="<<time_p[2]<<"\n";   
   }
   
/**************Error number calculation****************************/
   if (N1!=1)
   {flags=(short int)((float)(flag_len_weak[1])+
                      pow((float)(2*flag_CRC_H[1]),1)+
 	              pow((float)(2*flag_len[1]),2)+
                      pow((float)(2*flag_pack[1]),3)+
		      pow((float)(2*flag_name[1]),4)+
                      pow((float)(2*flag_type[1]),5)+
                      pow((float)(2*flag_time[1]),6)+
		      pow((float)(2*flag_CRC_cor[1]),7));
    if (flags!=0) er_pack_count+=1;
//Write error number in log file
    fprintf(fo_log,"%1c",char(flags));
//Write error number in work.dat file
    if (flags!=0) fo_work<<"PAMELA packet="<<numP[1]<<" : error flag="<<flags<<"\n";
   }
/***********************NAME ERROR*********************************/
   if (((int)(unsigned char)Name_Buf[0]!=CODE_BYTE1)||
       ((int)(unsigned char)Name_Buf[1]!=CODE_BYTE2)||
       ((int)(unsigned char)Name_Buf[2]!=CODE_BYTE3))
   {
    fo_work<<"Name error : packet number="<<numP[2]<<" : ";
    fo_work<<hex<<(int)(unsigned char)Name_Buf[0]<<" : "<<(int)(unsigned char)Name_Buf[1]
           <<" : "<<(int)(unsigned char)Name_Buf[2]<<dec<<"\n";
    Name_Buf[0]=(char)(CODE_BYTE1);
    Name_Buf[1]=(char)(CODE_BYTE2);
    Name_Buf[2]=(char)(CODE_BYTE3);
    flag_name[2]=1;

    for (i=0; i<8; i++)
    {buff[i]=Name_Buf[i];
     buff[i+8]=Name_Buf_add[i];}    
    if (CRC_Header[2]==(short int)(unsigned char)CM_Compute_CRC16(0, (unsigned char*)&buff, 15))
    {
     count_name_cor+=1;
     buffer[0]=(char)(CODE_BYTE1);
     buffer[1]=(char)(CODE_BYTE2);
     buffer[2]=(char)(CODE_BYTE3);     
     flag_CRC_cor[2]=1;
    }
   }

/***********************PACKET TYPE ERROR**************************/
   if ((num_type==0)||(num_type1==0)||(num_type2==0))
   {
    flag_type[2]=1;
    fo_work<<"Packet type error : packet number="<<numP[2]<<" : packet type="<<hex<<packet_type[num_type]<<dec<<
             " : "<<num_type<<" : "<<num_type1<<" : "<<num_type2<<"\n";
   }
/******************PAMELA BEGIN PACKET DISPLACEMENT****************/
   temp[0]=adr_first[2];
   for (i=0;i<4;i++) Byte_adr1[i]=0;
   for (i=0;i<4;i++)
   {if (temp[0]/(long int)(pow(256.,3-i))>=1) 
	{Byte_adr1[3-i]=(int)(temp[0]/(long int)(pow(256.,3-i)));
	 temp[0]=temp[0]-(long int)pow(256.,3-i)*
			   (int)(temp[0]/(long int)(pow(256.,3-i)));}
   }   

//Write packet type and begin displacement in log file
   fprintf(fo_log,"%1c",char(num_type));
   for (i=0;i<4;i++) fprintf(fo_log,"%1c",char(Byte_adr1[i]));

/**************PAMELA PACKET OLD BUFFER***************************/
   for (i=0;i<8;i++)
   {Name_Buf_old[i]=Name_Buf[i]; Name_Buf_add_old[i]=Name_Buf_add[i];}

/**********************data reading********************************/
   fin.seekg(-1,ios::cur); icount-=1; icount_total-=1;
   fin.read(Name_Buf,8); 
   if (fin.eof()) break;
   icount+=8; icount_total+=8;
   for (i=0;i<7;i++) buffer[i+16]=Name_Buf[i+1]; 
  }
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
/**********************data reading********************************/
  fin.get(ch);
  if (fin.eof()) {break;}
  icount_total+=1;
  if ((print_flag>0) && fmod( double(icount_total), double(100000000.))==double(0.)) {cout<<"Mbytes="<<icount_total/1000000.<<"\n";}
  if ((N1>0)&&(FLAG_BIG==0)) icount+=1; 
  for (i=0;i<7;i++) Name_Buf[i]=Name_Buf[i+1];
  Name_Buf[7]=ch; /*for(i=0;i<8;i++) printf("%x",(int)(unsigned char)Name_Buf[i]); printf("\n");*/
  if ((icount-adr_first[2]-1)<Nmax)
  {buffer[icount-adr_first[2]]=ch;}
  else
  {flag_len[2]=1;
   er_pack_Len+=1;
   fo_work<<"Length error (too long packet) : packet number="<<numP[2]<<" : length in header="<<LenP[2]<<
	    " : length (> Nmax="<<Nmax<<")="<<icount-adr_first[2]<<" : packet type="<<hex<<
            packet_type[num_type]<<dec<<"\n";
   if (print_flag>0 && FLAG_BIG==0) {cout<<"too long packet "<<numP[2]<<"\n";}
   FLAG_BIG=1;
  }
 }
/******************************************************************/
/********************MAIN READING CYCLE END************************/
/******************************************************************/

/*************PAMELA LAST PACKET END DISPLACEMENT******************/
  if (FLAG_BIG==0) {adr_last[2]=icount;}
  else {adr_last[2]=adr_first[2]+Limit;}
  if ((adr_last[2]-adr_first[2]+1-LenP[2]-16>64)||(adr_last[2]-adr_first[2]+1-LenP[2]-16<0)) 
  {
   flag_len[2]=1;
   er_pack_Len+=1;
   fo_work<<"Length error : packet number="<<numP[2]<<" : length in header="<<LenP[2]<<
            " : length till next header="<<adr_last[2]-adr_first[2]-16<<" : packet type="<<hex<<
	    packet_type[num_type]<<dec<<"\n";   
  }
  if ( (flag_len[2]!=1)&&(flag_CRC_H[2]!=1) )
  {adr_last[2]=adr_first[2]+LenP[2]+15;}
  temp[1]=adr_last[2];
  for (i=0;i<4;i++) Byte_adr2[i]=0;
  for (i=0;i<4;i++)
  {if (temp[1]/(long int)(pow(256.,3-i))>=1) 
	{Byte_adr2[3-i]=(int)(temp[1]/(long int)(pow(256.,3-i)));
     temp[1]=temp[1]-(long int)pow(256.,3-i)*
     (int)(temp[1]/(long int)(pow(256.,3-i)));}}
  
/*********PAMELA LAST PACKET LENGTH ERROR ANALYSIS*****************/

//Write last packet end displacement in log file
 for (i=0;i<4;i++) fprintf(fo_log,"%1c",char(Byte_adr2[i]));

//Write error number in log and work.dat files
 flags=(short int)(pow((float)(2*flag_CRC_H[2]),1)+
 	           pow((float)(2*flag_len[2]),2)+
                   pow((float)(2*flag_pack[2]),3)+
                   pow((float)(2*flag_name[2]),4)+
		   pow((float)(2*flag_type[2]),5)+
                   pow((float)(2*flag_time[2]),6)+
				   pow((float)(2*flag_CRC_cor[2]),7));
 if ((flags!=0)&&(N1>0)) er_pack_count+=1; 
 fprintf(fo_log,"%1c",char(flags));
//Write error number in work.dat file
 if (flags!=0) fo_work<<"PAMELA packet="<<numP[2]<<" : error flag="<<flags<<"\n"; 


//Write inf file
 fprintf(fo_inf,"%lli bytes in initial file %s\n", icount_total, name_in);
 icount_int = (int)(icount_total/1024);
 icount_frac= icount_total%1024;
 fprintf(fo_inf,"file size= %i,%i kByte \n", icount_int, icount_frac);
 strcpy(ttt,name_in); 
 ttt[17]='2';
 fprintf(fo_inf,"%lli bytes in cln2 file %s\n", icount, ttt);
 ratioCln1Cln2=(float)icount/icount_total;
 fprintf(fo_inf,"ratio of sizes of Cln1 and Cln2 files %f\n", ratioCln1Cln2);

 fprintf(fo_inf,"number of identified packets= %i\n", N1);
 fprintf(fo_inf,"number of PAMELA missed packets= %i\n",count_miss);
 fprintf(fo_inf,"number of PAMELA double transmitted packets = %i\n",count_sec_miss);		     
 fprintf(fo_inf,"number of PAMELA packets with CRC error= %i\n",er_pack_CRC);		     
 fprintf(fo_inf,"number of PAMELA packets with length error= %i\n",er_pack_Len);		     		     
 fprintf(fo_inf,"Overall number of PAMELA packets with error= %i\n",er_pack_count);
 fprintf(fo_inf,"number of PAMELA packets with corrected name= %i\n",count_name_cor);
 fprintf(fo_inf,"number of PAMELA packets with non identified type =%i\n",packet_counter[0]);

 for (i=1;i<NumberPack;i++)
 {
  fprintf(fo_inf,"Type %x  number of PAMELA packets= %i\n",
	             packet_type[i-1], packet_counter[i]);
 }
//Write data in file "nnnnn_mmm_xxx_cln2.pam"
 for (i=0;i<adr_last[2]-adr_first[2]+1;i++)
 {fo_pam<<buffer[i];}

 time(&seconds);
 timeinfo=localtime(&seconds);
 strftime(time_year,5,"%Y",timeinfo);
 strftime(time_month,5,"%m",timeinfo);
 strftime(time_day,5,"%d",timeinfo);
 strcat( strcat(strcat(strcat(strcpy(data_time,time_year),"-"),time_month),"-"),time_day);

 for (i=strlen(pam)-22;i<=strlen(pam);i++)
 {name_cln2[i-(strlen(pam)-22)]=pam[i];}
 for (i=0;i<strlen(pam)-22;i++)
 {name_path[i]=pam[i];}
 name_path[strlen(pam)-22]='\0';
 for (i=0;i<5;i++)
 {cirquit_no[i]=name_cln2[i];}
 cirquit_no[5]='\0';
 for (i=6;i<9;i++)
 {route_no[i-6]=name_cln2[i];}
 route_no[3]='\0';
 file_ready=1;
 file_deleted=0;
 file_archived=0;
 file_proces=0;
 if (IndexQ>YODA_flag) file_proces=1;

 if (IndexQ>transmit_flag) {flag_to_transmit=1;}
 else {flag_to_transmit=0;}
 oss.str("");
 oss << "INSERT INTO FILES (cirquit_n, route_n, counter, qual_index, ready, deleted, date,"
     << " archived, path, cln2_name, proces_flag, transit_flag)"
     << " VALUES ('"<< cirquit_no << "','"<< route_no << "','"<<r_counter<<"','"<< IndexQ << "','"
     << file_ready << "','"<< file_deleted << "','"<< data_time << "','"<< file_archived << "','"
     << name_path << "', '" << name_cln2 << "','"<< file_proces << "','"
     << flag_to_transmit << "')";
 if (print_flag>0) cout<<oss.str().c_str()<<" table FILES\n";      
 TSQLResult *res=sqlServer->Query(oss.str().c_str()); 

 flag_sec_trans=0;
 
 oss1.str("");
 oss1 <<"INSERT INTO FILES1 (my_route, raw_filename, pam_size, ratioCln1Cln2, cirquit_n, route_n, route_real,"
      <<" counter, board_time, Num_all, Num_macro, flag_twice, OBT_first, OBT_last)"
      <<" VALUES ('"<<current_route<<"','"<<name_ini<<"','"<<icount<<"','"<<ratioCln1Cln2<<"','"<<cirquit_no<<"','"
      <<route_no<<"','"<<real_route[r_counter]<<"','"<<r_counter
      <<"','"<<time_p[2]<<"','"<<N1<<"','"<<packet_counter[28]<<"','"<<flag_sec_trans<<"','"<<time_min<<"','"<<time_max<<"')";
 if (print_flag>0) cout<<oss1.str().c_str()<<" table FILES1\n";      
 TSQLResult *res3=sqlServer->Query(oss1.str().c_str()); 

// if (conn->Query(oss.str().c_str()) == 0)
// {
//  printf("ERROR!!!");
// }

 delete [] buffer;

/***************************************************************/
  fin.close();
  fo_work.close();
  fclose(fo_log);
  fclose(fo_inf);
  fo_pam.close();
  fclose(fo_miss);
}

