#include "descript.h"

void filename(char [], char [], char [], char[], int, char[]);
void FindRoute(int[], int*);
short int CRC(int []);
int restrict(int [], long long int[], int);

void read(char fni[], char fno[], char INF[],
		  char protocol[], char path[], char name[], char nnnnn_TMP[], float quality[], int real_route[], int &Rcount)
{
 FILE *fo, *fo1, *fout, *foRout, *foMiss, *fo_log_kadr, *fo_route;
 ifstream fin;
 ofstream fo_cln1, fokadr_cln1;
 
 char buf[1024];
 //  char stop1;
 char ch1,ch2,ch3, ch, file_name_prot[80];
 char file_route[80];  /*protocol binary filesm mmm_counter_nnnnn_cln1Er.log*/
 char miss_file[80];   /*protocol files mmm_counter_nnnnn_mis_cln1.dat*/
 char cln1_file[80];   /*route binary data files mmm_counter_nnnnn_cln1.pam*/
 char cln1_kadr[80];   /*route binary data files mmm_counter_nnnnn_cln1.bin*/
 char log_kadr[80];    /*log binary data files mmm_counter_nnnnn_cln1.log*/
 char log_route[80];   /*log binary file for number of routes*/
 char Short_Buf[8], Name_Buf[4]; 
 short int CalcCheckSum;

 int LenthBuf=1015, flag_help=0;
 int i, j, k, file_length=0, Hex[3];
 int N_er=0;              /*Number of kadr with name error*/
 int CRC0_er=0;           /*Number of kadr with CRC0 error*/
 int P_er=0;              /*Number of kadr with packet error number*/
 int M_er=0;              /*Number of kadr with rout error number*/
 int end=0;               /*End of last pamela packet*/
 // Buffers for Kadr analysis
 int buffer1[1024], buffer2[1024], buffer3[1024]; 
 int rr=0;

 int header1, header2, header3, CRC_0[3], CRC_1[3];
 long long int icount_int;
 long long int icount_frac, tmpP, numP[3];	      
 long int N1=0;                  /* Number of identified kadrs*/
 long long int icount=0;         /* Number of bytes in identified kadrs*/ 
 long long int icount_total=0;   /* Total number of bytes*/
 long long int packet_first[100];/*First kadr number for each route*/
 long long int packet_last[100]; /*Second kadr number for each route*/
 long int count_missed[100];     /* Number of missed kadrs in each route*/
 long int count_sec_missed[100];
 unsigned long long int count_ident[100]; /* Number of good bytes in each rout*/
 unsigned long long int count_tot[100];   /* Total number of bytes in each rout*/
 int NumRout[1000];              /* Number of Route*/
 long long int Num_kadrs[1000];  /* Number of identified kadrs in each rout*/
 long int Num_good_kadrs[1000];  /* Number of normal kadrs in each rout*/
 long long int Num_bad_kadrs[1000]; /* Bad kadrs number in each rout*/
// float quality[100];           /* File quality pointer*/
 int counter=0;                  /* Last byte position in truncated kadr*/
 int adr1=0;                     /* Initial byte of kadr in cln1.bin*/
 int adr2=0;                     /* Last byte of kadr in cln2.bin*/
 int tmp=0;
 int numPack[4];                 /* Number of kadr in given route*/
 int adress[4];
 int R_er[100];           /* Rout errors number in given rout*/
 int R_er_interf1[100];   /* Interface Rout error at begin of rout*/
 int R_er_interf2[100];   /* Interface Rout error at end of rout*/
 int Mis_group[1000];     /* Number of missed kadrs group
                             for each rout*/
 int Pack_Er[1000];       /* Number of packets with packet number
                             error for each rout*/
 int CRC_bad_kadr[3];     /* Current CRC bad kadr number*/
 int flag_pack[3];        /* Flag for error in packet number*/
 int flag_rout[3];        /* Flag for error in rout number*/
 int flag_name[3];        /* Flag for name error*/
 int flag_CRC0[3];        /* Flag for CRC0 error*/
 int flag_CRC[3];         /* Flag for CRC error*/
 int flag_CRC_cor[3];     /* Flag for CRC error correction*/
 int flag_tr[3];          /* Truncate flag*/
 short int flags;         /* Number for all errors*/

 for (j=0; j<4; j++) {adress[j]=0; numPack[j]=0;}
 for (j=0; j<1024; j++) 
 {buffer1[j]=0; buffer2[j]=0; buffer3[j]=0;}
 for (i=0; i<100; i++) 
 {Num_kadrs[i]=0;
  NumRout[i]=0;
  packet_first[i]=0;
  packet_last[i]=0;
  count_missed[i]=0;
  count_sec_missed[i]=0;
  count_ident[i]=0;
  count_tot[i]=0;
  R_er[i]=0;
  R_er_interf1[i]=0;
  Num_bad_kadrs[i]=0;
  Num_good_kadrs[i]=0;
  quality[i]=0; Mis_group[i]=0; Pack_Er[i]=0;
  R_er_interf2[i]=0;}
 for (i=0; i<3; i++)
 {numP[i]=0; CRC_bad_kadr[i]=0; Hex[i]=0;
  flag_pack[i]=0; flag_rout[i]=0;
  flag_name[i]=0; flag_CRC0[i]=0; flag_tr[i]=0;
  flag_CRC[i]=0; flag_CRC_cor[i]=0;} 
 // open input file "nnnnnmmm.pam"
 fin.open(fni, ifstream::binary);
 if(fin.good()==0) {cerr<<"can not open input file"; exit(0);}
 //open output file "nnnnnmmm_work.dat"
 fout=fopen(fno, "wb");
 if(fout==0) {cerr<<"can not open outputfile"; exit(0);}
 //open output file "nnnnnmmm_protocol.txt"
 fo=fopen(protocol, "wb");
 if(fo==0) {cerr<<"can not open protocol file"; exit(0);}
 //open output file "nnnnnmmm_cln1.inf"
 fo1=fopen(INF, "wb");
 if(fo1==0) {cerr<<"can not open inf file"; exit(0);}
 //open output file "nnnnnmmm_route.log"
 strncpy(log_route,INF,strlen(INF)-9);
 log_route[strlen(INF)-9]='\0';
 strcat(log_route,"_route.log");
 fo_route=fopen(log_route, "wb");
 if(fo_route==0) {cerr<<"can not open file for routes number"; exit(0);}
 /////////////////////////////////////////////////////////////////
 // cin>>stop1;
 k=0;
 while(fin.eof()==0)
 {
  counter=0;          //pounter for position of kadr truncation
  N_er=0;           
  M_er=0;
  P_er=0;
  CRC0_er=0;
  if (N1==0) Rcount=1;
/**********************data reading********************************/
  fin.read(Name_Buf,3);
  if (fin.eof()) break;
  fin.read(Short_Buf,5);	
  if (fin.eof()) break;
  ch1=Name_Buf[0];
  ch2=Name_Buf[1];
  ch3=Name_Buf[2];
  tmpP=(long int)(unsigned char)(Short_Buf[1])+
     	256*(long int)(unsigned char)(Short_Buf[2])+
        256*256*(long int)(unsigned char)(Short_Buf[3]);
  icount+=8; icount_total+=8;
  count_ident[Rcount-1]+=8; count_tot[Rcount-1]+=8;

  while((ch1!=(char)(CODE1)&&ch2!=(char)(CODE2)&&
  (N1==0||buffer3[1023]!=(int)(unsigned char)(Short_Buf[4])||buffer3[3]!=(int)(unsigned char)(Short_Buf[0])))
	     ||
  (ch2!=(char)(CODE2)&&ch3!=(char)(CODE3)&&(buffer3[1023]!=(int)(unsigned char)(Short_Buf[4])||(numP[2]+1)!=tmpP))
	     ||
  (ch3!=(char)(CODE3)&&ch1!=(char)(CODE1)&&((numP[2]+1)!=tmpP||buffer3[1023]!=(int)(unsigned char)(Short_Buf[4])))
	     ||
  (ch1!=(char)(CODE1)&&
   (buffer3[1023]!=(int)(unsigned char)(Short_Buf[4])||buffer3[3]!=(int)(unsigned char)(Short_Buf[0])||(numP[2]+1)!=tmpP))
  	     ||
  (ch2!=(char)(CODE2)&&
   (buffer3[1023]!=(int)(unsigned char)(Short_Buf[4])||buffer3[3]!=(int)(unsigned char)(Short_Buf[0])||(numP[2]+1)!=tmpP))
	     ||
  (ch3!=(char)(CODE3)&&
   (buffer3[1023]!=(int)(unsigned char)(Short_Buf[4])||buffer3[3]!=(int)(unsigned char)(Short_Buf[0])||(numP[2]+1)!=tmpP))
	     ||
  (ch1!=(char)(CODE1)&&ch2!=(char)(CODE2)&&ch3!=(char)(CODE3)))
  {fin.get(ch);
   if (fin.eof()) {/*cerr<<"\n"<<"gopa";*/ break;}
   ch1=ch2; ch2=ch3; ch3=Short_Buf[0];
   for (i=0;i<4;i++) Short_Buf[i]=Short_Buf[i+1]; Short_Buf[4]=ch;
   icount_total+=1; count_tot[Rcount-1]+=1;
   tmpP=(long int)(unsigned char)(Short_Buf[1])+
  	    256*(long int)(unsigned char)(Short_Buf[2])+
        256*256*(long int)(unsigned char)(Short_Buf[3]);
  }
  if ((fin.eof())&&(N1==0)) 
  {
   fclose(fout);
   remove(fno);
   fclose(fo);
   remove(protocol);
   fclose(fo1);
   remove(INF);
   exit(0);
  }
//	cout<<"icount_total="<<icount_total<<" "<<N1<<"\n";
//	printf("%x %x %x \n",(int)(unsigned char)(ch1),(int)(unsigned char)(ch2),(int)(unsigned char)(ch3));
//	printf("%x %x %x %x %x\n",(int)(unsigned char)(Short_Buf[0]),(int)(unsigned char)(Short_Buf[1]),
//		                      (int)(unsigned char)(Short_Buf[2]),(int)(unsigned char)(Short_Buf[3]),
//							  (int)(unsigned char)(Short_Buf[4]));
//	printf("buffer3[3]= %x\n",(int)(unsigned char)buffer3[3]);
//	printf("numP[2]+1= %x tmpP= %x\n",numP[2]+1, tmpP);
//	printf("buffer3[1023]= %x\n",(int)(unsigned char)buffer3[1023], tmpP);

  /*****************************************************************/
  /*********************data reading********************************/
  fin.read(buf,LenthBuf); 
  if (fin.eof()) break;
  icount+=1015; icount_total+=1015;
  count_ident[Rcount-1]+=1015; count_tot[Rcount-1]+=1015;
  /****************************************************************/
  /********************data reading********************************/
  fin.get(ch);
  icount+=1; icount_total+=1;
  count_ident[Rcount-1]+=1; count_tot[Rcount-1]+=1;
  if (fin.eof()) break;
  /***************************************************************/
  N1+=1; Num_kadrs[Rcount-1]+=1;
 
  /**********************DATA ANALYSIS****************************/
  /***************************************************************/
  header1=(unsigned char)(ch1);
  header2=(unsigned char)(ch2);
  header3=(unsigned char)(ch3);
  switch (N1)
  { 
   case 1:{
   	   buffer3[0]=(int)(unsigned char)(ch1);
           buffer3[1]=(int)(unsigned char)(ch2);
           buffer3[2]=(int)(unsigned char)(ch3);
           buffer3[3]=(int)(unsigned char)(Short_Buf[0]);
           buffer3[4]=(int)(unsigned char)(Short_Buf[1]);
           buffer3[5]=(int)(unsigned char)(Short_Buf[2]);
           buffer3[6]=(int)(unsigned char)(Short_Buf[3]);
           buffer3[7]=(int)(unsigned char)(Short_Buf[4]);
           for (i=0; i<1015; i++)
		   {buffer3[i+8]=(int)(unsigned char)(buf[i]);}
           buffer3[1023]=(int)(unsigned char)(ch);			  
		   for (i=0; i<1024; i++)
		   {buffer1[i]=buffer3[i];buffer2[i]=buffer3[i];}

		   NumRout[Rcount-1]=1;
           numP[2]=(long int)(unsigned char)(Short_Buf[1])+
   	       256*(long int)(unsigned char)(Short_Buf[2])+
           256*256*(long int)(unsigned char)(Short_Buf[3]);
		   CRC_bad_kadr[2]=0;
		   flag_pack[2]=0; flag_rout[2]=0;
		   flag_name[2]=0; flag_CRC0[2]=0; flag_tr[2]=0;
		   flag_CRC[2]=0; flag_CRC_cor[2]=0;
           for (i=0; i<2; i++)
		   {numP[i]=numP[2];CRC_bad_kadr[i]=CRC_bad_kadr[2];
		    flag_pack[i]=flag_pack[2]; flag_rout[i]=flag_rout[2];
		    flag_name[i]=flag_name[2]; flag_CRC0[i]=flag_CRC0[2];
		    flag_CRC[i]=flag_CRC[2]; flag_tr[i]=flag_tr[2];
		    flag_CRC_cor[i]=flag_CRC_cor[2];}
           CRC_0[2]=(int)(unsigned char)(Short_Buf[4]);
		   for (i=0; i<2; i++)
		   {CRC_0[i]=CRC_0[2];}
           CRC_1[2]=(int)(unsigned char)(ch);
		   for (i=0; i<2; i++)
		   {CRC_1[i]=CRC_1[2];}
           packet_first[Rcount-1]=numP[2];
		   break;
          }
   default:{
   	    for (i=0; i<1024; i++)
	    {buffer1[i]=buffer2[i];buffer2[i]=buffer3[i];}
	    buffer3[0]=(int)(unsigned char)(ch1);
            buffer3[1]=(int)(unsigned char)(ch2);
            buffer3[2]=(int)(unsigned char)(ch3);
            buffer3[3]=(int)(unsigned char)(Short_Buf[0]);
            buffer3[4]=(int)(unsigned char)(Short_Buf[1]);
            buffer3[5]=(int)(unsigned char)(Short_Buf[2]);
            buffer3[6]=(int)(unsigned char)(Short_Buf[3]);
            buffer3[7]=(int)(unsigned char)(Short_Buf[4]);
            for (i=0; i<1015; i++)
		    {buffer3[i+8]=(int)(unsigned char)(buf[i]);}
            buffer3[1023]=(int)(unsigned char)(ch);

            for (i=0; i<2; i++)
		    {numP[i]=numP[i+1]; CRC_bad_kadr[i]=CRC_bad_kadr[i+1];
		     flag_pack[i]=flag_pack[i+1];
		     flag_rout[i]=flag_rout[i+1];
		     flag_name[i]=flag_name[i+1];
		     flag_CRC0[i]=flag_CRC0[i+1];
		     flag_tr[i]=flag_tr[i+1];
		     flag_CRC[i]=flag_CRC[i+1];
		     flag_CRC_cor[i]=flag_CRC_cor[i+1];}
            numP[2]=(long int)(unsigned char)(Short_Buf[1])+
   	        256*(long int)(unsigned char)(Short_Buf[2])+
            256*256*(long int)(unsigned char)(Short_Buf[3]);
		    CRC_bad_kadr[2]=0;
		    flag_pack[2]=0; flag_rout[2]=0;
		    flag_name[2]=0; flag_CRC0[2]=0;
		    flag_CRC[2]=0; flag_CRC_cor[2]=0; flag_tr[2]=0;
		    for (i=0; i<2; i++) CRC_0[i]=CRC_0[i+1];
            CRC_0[2]=(int)(unsigned char)(Short_Buf[4]);
		    for (i=0; i<2; i++) {CRC_1[i]=CRC_1[i+1];}
            CRC_1[2]=(int)(unsigned char)(ch);    
           }
  }  
  //  if (fmod(N1,10000)==0.) cout<<N1<<"\n";	  
// Caf mod on 31 Aug 2006
  if ((N1%10000)==0 && N1>0) cout<<N1<<"\n";	  
  // end mod

/******************CRC ERROR ANALYSIS***************************/
  CalcCheckSum=CRC(buffer3);
  if (CalcCheckSum!=(short int)(buffer3[1023]))
  {
   /*if (numP[2]==1026) 
   {for (i=0;i<8;i++) printf(" %x ",buffer3[i]); printf("\n");
    for (i=8;i<16;i++) printf(" %x ",buffer3[i]); printf("\n");
    for (i=1000;i<1008;i++) printf(" %x ",buffer3[i]); printf("\n");
	printf("%x %x %x %x",buffer3[1020],buffer3[1021],buffer3[1022],buffer3[1023]);
    cin>>i;}*/
   fprintf(fout,"CRC = %x (%x) error in kadr %i\n", buffer3[1023],CalcCheckSum,numP[2]);     //write in file nnnnn_work.dat
//	cerr<<"checksum error "<<Rcount<<" Cadre="<<numP[2]<<
//		  " "<<buffer3[3]<<"\n";
   flag_CRC[2]=1;
   Num_bad_kadrs[Rcount-1]+=1;
   CRC_bad_kadr[2]=numP[2];
   counter=restrict(buffer3,numP,buffer2[3]);
   if (counter!=0)
   {fprintf(fout," truncate byte=%i",counter);
    cout<<" truncate counter="<<counter<<" route="<<buffer2[3]<<" "<<buffer3[3]<<" kadr="<<numP[2]<<" "<<numP[1]<<" "<<numP[0]<<"\n";
    fin.seekg(-(1024-counter),ios::cur);
    icount-=1024;
    count_ident[Rcount-1]-=1024;
    count_tot[Rcount-1]-=1024-counter;
    icount_total-=1024-counter;
    N1-=1;
    CRC_bad_kadr[2]=0;
    flag_tr[2]=1;
    Num_kadrs[Rcount-1]-=1;
    Num_bad_kadrs[Rcount-1]-=1;}
  }

  /******************HEADER ANALYSIS*******************************/
  if ((header1!=(int)(CODE1))||(header2!=(int)(CODE2))||(header3!=(int)(CODE3))) 
  {flag_name[2]=1; N_er=N1;}
  /****************************************************************/

  /******************CRC0 ANALYSIS*********************************/
  if ((buffer2[7]!=buffer1[1023])&&
      ((numP[1]==numP[0]+1)||(numP[1]==numP[2]-1))&&(flag_tr[1]!=1)&&(flag_tr[0]!=1)&&(N1>2))
  {flag_CRC0[1]=1; CRC0_er=N1-1;}
  /****************************************************************/

  /******************KADR NUMBER ANALYSIS**************************/
  if ((numP[1]!=(numP[0]+1))&&(numP[1]!=(numP[2]-1))&&(N1!=1)&&(CRC_bad_kadr[1]==numP[1])&&(numP[1]!=0)) 
  {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=4;i<7;i++) (buffer2[i]=Hex[i-4]);
   if (buffer2[1023]==(int)(CRC(buffer2))) flag_CRC_cor[1]=1;

   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=4;i<7;i++) (buffer2[i]=Hex[i-4]);
   if (buffer2[1023]==(int)(CRC(buffer2))) flag_CRC_cor[1]=1;
  }
  /////////////////////////////////////////////////////////////////////
  if ((numP[2]!=(numP[1]+1))&&(N1!=1)&&(buffer3[3]==buffer2[3])&&(CRC_bad_kadr[2]==numP[2])&&(numP[2]!=0)) flag_pack[2]=1;
  if (flag_pack[2]!=1) packet_last[Rcount-1]=numP[2];
  ////////Missing kadr analysis////////////////////////////////////
  /**route number: 10 (10 19 10) 10 ******************************/
  /**kadr number:  21 (22 17 18) 19 ******************************/
  /**missed kadrs: 21 20 19 18 17   ******************************/  
  if ((numP[1]<numP[0])&&(numP[1]==numP[2]-1)&&(buffer1[3]==buffer3[3])&&(flag_tr[1]==0)&&
 	  (flag_pack[0]==0)&&(flag_pack[1]==0)&&(flag_pack[2]==0))
  {Mis_group[Rcount-1]+=1;
   fprintf(foMiss,"secondary transmitted kadres may be %9i%9i\n",numP[1]+1,numP[0]-1);
   count_sec_missed[Rcount-1]+=numP[0]-1-numP[1]-1;}  
  /**route number: 10 (10 19 10) 10 ******************************/
  /**kadr number:  11 (12 15 20) 21 ******************************/
  /**missed kadrs:    16 17 18 19   ******************************/  
  if ((numP[1]>numP[0]+1)&&(numP[1]<numP[2]-1)&&(buffer1[3]==buffer3[3])&&(flag_tr[1]==0)&&
 	  (flag_pack[0]==0)&&(flag_pack[1]==0)&&(flag_pack[2]==0))
  {Mis_group[Rcount-1]+=1;
   fprintf(foMiss,"%9i%9i\n",numP[1]+1,numP[2]-1);
   count_missed[Rcount-1]+=numP[2]-numP[1]-1;}
  /**route number: (10 10 10) 10 10 ******************************/
  /**kadr number:  (11 12 15) 20 21 ******************************/
  /**missed kadrs:     13 14        ******************************/  
  else if ((numP[1]>=numP[0]+1)&&(numP[1]<numP[2]-1)&&
	       (buffer1[3]==buffer3[3])&&(buffer1[3]==buffer2[3])&&
		   (flag_tr[1]==0)&&(flag_pack[2]==0)&&(flag_pack[1]==0)&&(flag_pack[0]==0))
  {Mis_group[Rcount-1]+=1;
   fprintf(foMiss,"%9i%9i\n",numP[0]+2,numP[2]-1);
   count_missed[Rcount-1]+=numP[2]-numP[0]-1-1;}
  /**route number: (10 10 10) 10 10 ******************************/
  /**kadr number:  (11 29 15) 14 15 ******************************/
  /**number of missed kadrs:2       ******************************/  
  else if (((numP[1]<numP[0]+1)||(numP[1]>numP[2]-1))&&(numP[2]-numP[0]>2)&&
           (buffer1[3]==buffer3[3])&&(buffer1[3]==buffer2[3])&&(flag_tr[1]==0)&&
     	   (flag_pack[2]==0)&&(flag_pack[1]==1)&&(flag_pack[0]==0))
  {Mis_group[Rcount-1]+=1;
   fprintf(foMiss,"%9i%9i -1\n",numP[0]+1,numP[2]-1);
   count_missed[Rcount-1]+=numP[2]-numP[0]-1-1;}
  //truncate kadr
  /**route number: 10 (10 10 10) 10 ******************************/
  /**kadr number:  11 (12 13 14) 15 ******************************/
  /**missed truncate kadr:13        ******************************/  
  else if ((numP[0]<numP[2]-1)&&(buffer1[3]==buffer3[3])&&
	       (flag_tr[0]==0)&&(flag_tr[1]==1)&&(flag_tr[2]==0)&&
		   (flag_pack[2]==0)&&(flag_pack[1]==0)&&(flag_pack[0]==0)) 
  {Mis_group[Rcount-1]+=1;
   fprintf(foMiss,"%9i%9i\n",numP[0]+1,numP[2]-1);
   count_missed[Rcount-1]+=numP[2]-numP[0]-1;}
  /***************************************************************/

  /******************ROUTE NUMBER ANALYSIS**************************/
  if ((buffer2[3]!=buffer1[3])&&(buffer2[3]!=buffer3[3])&&(buffer1[3]!=buffer3[3])&&
      (flag_pack[2]==0)&&(flag_pack[1]==0)&&(flag_pack[0]==0))
  {
   /******************Interface route error 1***********************/
   /**route number:7   7  ( 7   4  8) 8 8 **************************/
   /**kadr number:197 198 (199 200 1) 2 3 **************************/
   /*if ((numP[1]==numP[0]+1)&&(numP[2]<numP[0]))       
   {Rcount+=1; flag_rout[1]=1;
    packet_last[Rcount-2]=numP[1];
    packet_first[Rcount-1]=numP[2];
    Num_kadrs[Rcount-1]+=1;
    Num_kadrs[Rcount-2]-=1;			 
    R_er[Rcount-2]+=1;
    R_er_interf2[Rcount-2]+=1;
    M_er=N1-1;
    count_ident[Rcount-1]+=1024; count_ident[Rcount-2]-=1024;
    count_tot[Rcount-1]+=1024; count_tot[Rcount-2]-=1024;
    NumRout[Rcount-1]=Rcount;
    cout<<"\n interface1 "<<NumRout[Rcount-1]<<" "<<Rcount;
    //	  cin>>stop1;
    if ((CRC_bad_kadr[1]==numP[1])&&(numP[1]!=0))
    {
    //Correct error route number//////////////////////////////////////////
	 buffer2[3]=buffer1[3];
	 if (buffer2[1023]==(int)(CRC(buffer2))) flag_CRC_cor[1]=1;
     ////////////////////////////////////////////////////////////////////
	 flags=(short int)(pow((float)(2*flag_name[1]),4)+
	       pow((float)(2*flag_rout[1]),1)+
	       pow((float)(2*flag_pack[1]),2)+
	       pow((float)(2*flag_CRC0[1]),3)+
		   pow((float)(2*flag_CRC[1]),5)+
		   pow((float)(2*flag_CRC_cor[1]),6));
     fprintf(foRout,"%1c%1c%1c%1c",(char)(buffer2[4]),
	         (char)(buffer2[5]),(char)(buffer2[6]),(char)(flags));
     fprintf(fout,"%i Interface route error 1 %i\n",CRC_bad_kadr[1],flags);
     CRC_bad_kadr[1]=0;
    }
    // CREATE FILE NAMES	  
   	filename(file_route, miss_file, cln1_file, path,
	         NumRout[Rcount-1], name);			 
	strncpy(cln1_kadr,cln1_file,strlen(cln1_file)-3);
	cln1_kadr[strlen(cln1_file)-3]='\0';
	strcat(cln1_kadr,"bin");
	strncpy(log_kadr,file_route,strlen(file_route)-6);
	log_kadr[strlen(file_route)-6]='\0';
	strcat(log_kadr,"_bin.log");
	fclose(foRout);                    //Close files 
	fo_cln1.close();                   //for old
	fclose(foMiss);                    //route
	fokadr_cln1.close();
	fclose(fo_log_kadr);
    //Open files for new route
	foRout=fopen(file_route, "wb");          //mmm_nnnnn_cln1Er.log
	foMiss=fopen(miss_file, "wb");           //mmm_nnnnn_mis_cln1.dat
	fo_cln1.open(cln1_file, ios::binary);    //mmm_nnnnn_cln1.pam   
	fokadr_cln1.open(cln1_kadr,ios::binary); //mmm_nnnnn_cln1.bin
	fo_log_kadr=fopen(log_kadr,"wb");        //mmm_nnnnn_cln1_bin.log
   }*/
   /******************Interface route error 2***********************/
   /**route number:7   7  ( 7  4 8) 8 ******************************/
   /**kadr number:198 199 (200 1 2) 3 ******************************/
   /*if ((numP[1]==numP[2]-1)&&(numP[1]<numP[0])&&(buffer2[3]!=0))
   {Rcount+=1; flag_rout[1]=1;
    packet_last[Rcount-2]=numP[0];
    packet_first[Rcount-1]=numP[1];
    Num_kadrs[Rcount-1]+=2;
    Num_kadrs[Rcount-2]-=2;
    R_er[Rcount-1]+=1;
    R_er_interf1[Rcount-1]+=1;
    M_er=N1-1;
    count_ident[Rcount-1]+=2048; count_ident[Rcount-2]-=2048;
    count_tot[Rcount-1]+=2048; count_tot[Rcount-2]-=2048;
    NumRout[Rcount-1]=Rcount;
    cout<<"\n interface2 "<<NumRout[Rcount-1]<<" "<<Rcount;
//	  cin>>stop1;
    // CREATE FILE NAMES
   	filename(file_route, miss_file, cln1_file, path,
	         NumRout[Rcount-1], name);	
	strncpy(cln1_kadr,cln1_file,strlen(cln1_file)-3);
	cln1_kadr[strlen(cln1_file)-3]='\0';
	strcat(cln1_kadr,"bin");
	strncpy(log_kadr,file_route,strlen(file_route)-6);
	log_kadr[strlen(file_route)-6]='\0';
	strcat(log_kadr,"_bin.log");
	fclose(foRout);                    //Close files 
	fo_cln1.close();                   //for old
	fclose(foMiss);                    //route
	fokadr_cln1.close();
	fclose(fo_log_kadr);
    //Open files for new route
	foRout=fopen(file_route, "wb");          //mmm_nnnnn_cln1Er.log
	foMiss=fopen(miss_file, "wb");           //mmm_nnnnn_mis_cln1.dat
	fo_cln1.open(cln1_file, ios::binary);    //mmm_nnnnn_cln1.pam
	fokadr_cln1.open(cln1_kadr,ios::binary); //mmm_nnnnn_cln1.bin
	fo_log_kadr=fopen(log_kadr,"wb");        //mmm_nnnnn_cln1_bin.log
	if ((CRC_bad_kadr[1]==numP[1])&&(numP[1]!=0))
	{Num_bad_kadrs[Rcount-1]+=1; Num_bad_kadrs[Rcount-2]-=1;
     //Correct error rout number//////////////////////////////////////////
	 buffer2[3]=buffer3[3];
	 if (buffer2[1023]==(int)(CRC(buffer2))) flag_CRC_cor[1]=1;
     ////////////////////////////////////////////////////////////////////
	 flags=(short int)(pow((float)(2*flag_name[1]),4)+
	        pow((float)(2*flag_rout[1]),1)+
	        pow((float)(2*flag_pack[1]),2)+
	        pow((float)(2*flag_CRC0[1]),3)+
		pow((float)(2*flag_CRC[1]),5)+
		pow((float)(2*flag_CRC_cor[1]),6));
         fprintf(foRout,"%1c%1c%1c%1c",(char)(buffer2[4]),(char)(buffer2[5]),(char)(buffer2[6]),(char)(flags));
         fprintf(fout,"%i Interface route error 2 %i\n",CRC_bad_kadr[1],flags);
         CRC_bad_kadr[1]=0;}
   }*/
   /******************Interface route error 3************************/
   /**route number:7   7  ( 7  8 4) 8 8******************************/
   /**kadr number:198 199 (200 1 2) 3 4******************************/
/*   if ((numP[1]==numP[2]-1)&&(flag_CRC[1]==0)&&(flag_CRC[2]==1))
   {Rcount+=1;
    packet_last[Rcount-2]=numP[0];
    packet_first[Rcount-1]=numP[1];
    NumRout[Rcount-1]=Rcount;
    Num_kadrs[Rcount-2]-=2;
    Num_kadrs[Rcount-1]+=2;
    count_ident[Rcount-1]+=2048; count_ident[Rcount-2]-=2048;
    count_tot[Rcount-1]+=2048; count_tot[Rcount-2]-=2048;
    Num_bad_kadrs[Rcount-1]+=1; Num_bad_kadrs[Rcount-2]-=1;
    cout<<"\n interface3 "<<NumRout[Rcount-1]<<" "<<Rcount;
//	  cin>>stop1;
    // CREATE FILE NAMES
   	filename(file_route, miss_file, cln1_file, path,
	         NumRout[Rcount-1], name);			 
	strncpy(cln1_kadr,cln1_file,strlen(cln1_file)-3);
	cln1_kadr[strlen(cln1_file)-3]='\0';
	strcat(cln1_kadr,"bin");
	strncpy(log_kadr,file_route,strlen(file_route)-6);
	log_kadr[strlen(file_route)-6]='\0';
	strcat(log_kadr,"_bin.log");
	fclose(foRout);                    //Close files 
	fo_cln1.close();                   //for old
	fclose(foMiss);                    //route
	fokadr_cln1.close();
	fclose(fo_log_kadr);
    //Open files for new route
	foRout=fopen(file_route, "wb");          //mmm_nnnnn_cln1Er.log 
	foMiss=fopen(miss_file, "wb");           //mmm_nnnnn_mis_cln1.dat  
	fo_cln1.open(cln1_file, ios::binary);    //mmm_nnnnn_cln1.pam
	fokadr_cln1.open(cln1_kadr,ios::binary); //mmm_nnnnn_cln1.bin
	fo_log_kadr=fopen(log_kadr,"wb");        //mmm_nnnnn_cln1_bin.log  
	flags=(short int)(pow((float)(2*flag_name[1]),4)+
	       pow((float)(2*flag_rout[1]),1)+
	       pow((float)(2*flag_pack[1]),2)+
	       pow((float)(2*flag_CRC0[1]),3)+
	       pow((float)(2*flag_CRC[1]),5)+
	       pow((float)(2*flag_CRC_cor[1]),6));
        fprintf(foRout,"%1c%1c%1c%1c",(char)(buffer2[4]),(char)(buffer2[5]),(char)(buffer2[6]),(char)(flags));
	fprintf(fout,"%i Interface route error 3 %i\n",CRC_bad_kadr[1],flags);
	CRC_bad_kadr[1]=0;
   }*/
  }
  /**************Return to old route*******************************/
  /**route number: 7 (8 4 8) 8*************************************/
  /*if ((buffer2[3]!=buffer1[3])&&(buffer2[3]!=buffer3[3])&&
      (buffer1[3]==buffer3[3])&&(buffer2[3]!=0)&&(N1>2))
  {		 
   R_er[Rcount-1]+=1; flag_rout[1]=1;
   M_er=N1-1;
   if ((CRC_bad_kadr[1]==numP[1])&&(numP[1]!=0))
   {
    //Correct error route number//////////////////////////////////////////
    buffer2[3]=buffer1[3];
    if (buffer2[1023]==(int)(CRC(buffer2))) flag_CRC_cor[1]=1;
    ////////////////////////////////////////////////////////////////////
    flags=(short int)(pow((float)(2*flag_name[1]),4)+
	       pow((float)(2*flag_rout[1]),1)+
	       pow((float)(2*flag_pack[1]),2)+
	       pow((float)(2*flag_CRC0[1]),3)+
   	       pow((float)(2*flag_CRC[1]),5)+
	       pow((float)(2*flag_CRC_cor[1]),6));
    fprintf(foRout,"%1c%1c%1c%1c",(char)(buffer2[4]),(char)(buffer2[5]),(char)(buffer2[6]),(char)(flags));
    fprintf(fout,"%i Old route error %i\n",CRC_bad_kadr[1],flags);
    CRC_bad_kadr[1]=0;}
   cout<<"\n"<<Rcount<<" old "<<NumRout[Rcount-1]<<" "<<buffer1[3]<<" "<<buffer2[3]<<" "<<buffer3[3]<<" N1="<<N1-1<<"\n";
  }*/
  /**************First route***************************************/
  /**route number: (0 4) 4*****************************************/
  /**kadr number:  (0 1) 2*****************************************/
  if (N1==2)
  {packet_first[Rcount-1]=numP[1];
   packet_last[Rcount-1]=numP[2];
   NumRout[Rcount-1]=1;
   // CREATE FILE NAMES
   filename(file_route, miss_file, cln1_file, path,
            NumRout[Rcount-1], name);			 
   strncpy(cln1_kadr,cln1_file,strlen(cln1_file)-3);
   cln1_kadr[strlen(cln1_file)-3]='\0';
   strcat(cln1_kadr,"bin");
   strncpy(log_kadr,file_route,strlen(file_route)-6);
   log_kadr[strlen(file_route)-6]='\0';
   strcat(log_kadr,"_bin.log");
   //Open files for new route
   foRout=fopen(file_route, "wb");          //mmm_nnnnn_cln1Er.log
   foMiss=fopen(miss_file, "wb");           //mmm_nnnnn_mis_cln1.dat 
   fo_cln1.open(cln1_file, ios::binary);    //mmm_nnnnn_cln1.pam
   if (!fo_cln1) cout<<"can not open "<<cln1_file<<"\n";
   fokadr_cln1.open(cln1_kadr,ios::binary); //mmm_nnnnn_cln1.bin
   if (!fokadr_cln1) cout<<"can not open "<<cln1_kadr<<"\n";
   fo_log_kadr=fopen(log_kadr,"wb");        //mmm_nnnnn_cln1_bin.log
   //  cout<<"\n"<<Rcount<<" old "<<NumRout[Rcount-1]<<" "<<numP[1]<<" N1="<<N1-1;
  } 
  /***************New route***************************************/
  /**route number: 7  ( 7  0 8) 8*********************************/
  /**kadr number: 199 (200 0 1) 2*********************************/
  /**route number: 7  ( 7  8 8) 8*********************************/
  /**kadr number: 199 (200 0 1) 2*********************************/
  if ( ((buffer1[3]!=buffer3[3])&&(buffer2[3]==0)&&(N1>3)&&(numP[1]!=numP[0]+1)&&(numP[1]<10))||
       ((buffer1[3]!=buffer2[3])&&(buffer2[3]==buffer3[3])&&(buffer1[3]!=0)&&(numP[1]!=numP[0]+1)&&(numP[1]<10))||
       ((numP[2]==numP[1]+1)&&(numP[1]==0)&&(Num_kadrs[Rcount-1]>1000000)) )
  {Rcount+=1;
   packet_last[Rcount-2]=numP[0]; 
   packet_first[Rcount-1]=numP[1]; 
   NumRout[Rcount-1]=Rcount;
   cout<<"New route="<<Rcount<<" "<<buffer1[3]<<" "<<buffer2[3]<<" "<<buffer3[3]<<"\n";
   cout<<numP[0]<<" "<<numP[1]<<" "<<numP[2]<<"\n";cin>>i;
   if (flag_tr[1]==0)
   {Num_kadrs[Rcount-2]-=2;
    Num_kadrs[Rcount-1]+=2;
    count_ident[Rcount-1]+=2048; count_ident[Rcount-2]-=2048;
    count_tot[Rcount-1]+=2048; count_tot[Rcount-2]-=2048;}
   else
   {Num_kadrs[Rcount-2]-=1;
    Num_kadrs[Rcount-1]+=1;
    count_ident[Rcount-1]+=1024; count_ident[Rcount-2]-=1024;
    count_tot[Rcount-1]+=1024; count_tot[Rcount-2]-=1024;}
   // CREATE FILE NAMES
   filename(file_route, miss_file, cln1_file, path,
            NumRout[Rcount-1], name);			 
   strncpy(cln1_kadr,cln1_file,strlen(cln1_file)-3);
   cln1_kadr[strlen(cln1_file)-3]='\0';
   strcat(cln1_kadr,"bin");
   strncpy(log_kadr,file_route,strlen(file_route)-6);
   log_kadr[strlen(file_route)-6]='\0';
   strcat(log_kadr,"_bin.log");
   fclose(foRout);                    //Close files 
   fo_cln1.close();                   //for old
   fclose(foMiss);                    //route
   fokadr_cln1.close();
   fclose(fo_log_kadr);
   //Open files for new route
   foRout=fopen(file_route, "wb");          //mmm_nnnnn_cln1Er.log
   foMiss=fopen(miss_file, "wb");           //mmm_nnnnn_mis_cln1.dat    
   fo_cln1.open(cln1_file, ios::binary);    //mmm_nnnnn_cln1.pam
   fokadr_cln1.open(cln1_kadr,ios::binary); //mmm_nnnnn_cln1.bin
   fo_log_kadr=fopen(log_kadr,"wb");        //mmm_nnnnn_cln1_bin.log
   if ((CRC_bad_kadr[2]==numP[2])&&(numP[2]!=0)) {Num_bad_kadrs[Rcount-1]+=1; Num_bad_kadrs[Rcount-2]-=1;}
   if ((CRC_bad_kadr[1]==numP[1])&&(numP[1]!=0))
   {Num_bad_kadrs[Rcount-1]+=1; Num_bad_kadrs[Rcount-2]-=1;
    flags=(short int)(pow((float)(2*flag_name[1]),4)+
	       pow((float)(2*flag_rout[1]),1)+
	       pow((float)(2*flag_pack[1]),2)+
	       pow((float)(2*flag_CRC0[1]),3)+
	       pow((float)(2*flag_CRC[1]),5)+
	       pow((float)(2*flag_CRC_cor[1]),6));
    fprintf(foRout,"%1c%1c%1c%1c",(char)(buffer2[4]),(char)(buffer2[5]),(char)(buffer2[6]),(char)(flags));
    fprintf(fout,"%i New route %i\n",CRC_bad_kadr[1],flags);
    CRC_bad_kadr[1]=-1;}
  // cout<<Rcount<<" New "<<NumRout[Rcount-1]<<" "<<numP[1]<<"\n"; 
  }
  
  if ((numP[1]==0)&&(N1>1))
  {rr=0;
   FindRoute(buffer2, &rr);
   if (rr>0)
   {
    real_route[Rcount]=rr;
   }
   else
   {
    real_route[Rcount]=0;
   }
  }   
  /***************************************************************/
  if (CRC_bad_kadr[1]==numP[1])
  {
   //Correct CRC0 error/////////////////////////////////////////////////
   buffer2[7]=buffer1[1023];
   if (buffer2[1023]==(int)(CRC(buffer2))) flag_CRC_cor[1]=1;
   //Correct name error/////////////////////////////////////////////////
   buffer2[0]=(int)(CODE1);
   buffer2[1]=(int)(CODE2);
   buffer2[2]=(int)(CODE3);
   if (buffer2[1023]==(int)(CRC(buffer2))) flag_CRC_cor[1]=1;
  }
  ////////////////////////////////////////////////////////////////////
  /*******************************************************************/
  if ((CRC_bad_kadr[1]==numP[1])&&(numP[1]!=0)&&(N1>1))
  {
   flags=(short int)(pow((float)(2*flag_name[1]),4)+
    	             pow((float)(2*flag_rout[1]),1)+
	                 pow((float)(2*flag_pack[1]),2)+
		             pow((float)(2*flag_CRC0[1]),3)+
			         pow((float)(2*flag_CRC[1]),5)+
			         pow((float)(2*flag_CRC_cor[1]),6));
   fprintf(foRout,"%1c%1c%1c%1c",(char)(buffer2[4]),
           (char)(buffer2[5]),(char)(buffer2[6]),(char)(flags));
   fprintf(fout,"%i flags=%i\n",CRC_bad_kadr[1],flags);}
   
  /***************************************************************/
//   cout <<"\nCalcChekSum="<<dec <<right<<CalcCheckSum
//   <<"\npacket.CheckSum1="<<right<<packet.CheckSum1;
//   cout <<"\nPacket number="<<packet.Pckt_Num<<" "<<endl;
  //*Packet analysis**********************************************
  if (N_er!=0) fprintf(fout,"Name error %i in kadr %i\n",numP[2],N_er);
  if (M_er!=0) fprintf(fout,"Root number error %i in kadr %i\n",numP[1],M_er);
  if (P_er!=0) fprintf(fout,"Number packet error %i in kadr %i\n",numP[1], P_er);
  if (CRC0_er!=0) fprintf(fout,"CRC0 error %i in kadr %i\n",numP[1], CRC0_er);

  /***************PAMELA data extracting for each route***********/
//  if (N1!=1&&(flag_CRC[1]==0||(flag_rout[1]==0&&flag_pack[1]==0)||
//	  (flag_CRC[1]!=0&&flag_CRC_cor[1]==1))) 
  if (N1!=1)
  {end=0;
   if (buffer2[3]!=buffer1[3]||buffer2[3]!=buffer3[3])
   {for (i=8;i<1020;i++)
    {
//     if (fmodf(float((Num_kadrs[Rcount-1]-1)*1015+i-8),float(64))==0.&&buffer2[i]==0&&buffer2[i+1]==0&&
//   	     buffer2[i+2]==0&&buffer2[i+3]==0) end=1;
     if (end!=1) fo_cln1<<(unsigned char)buffer2[i];
    }
    if (end!=1) for(i=1020;i<1023;i++) {fo_cln1<<(unsigned char)buffer2[i];}
   }
   else {for(i=8;i<1023;i++) fo_cln1<<(unsigned char)buffer2[i];}
   for (i=0;i<1024;i++) {fokadr_cln1<<(unsigned char)buffer2[i];}
   //Write in file mmm_counter_nnnnn_cln1_bin.log
   tmp=numP[1]; 
   for (k=0;k<4;k++) numPack[k]=0;
   for (k=0;k<4;k++)
   {if (tmp/(long int)(pow(256.,3-k))>=1) 
	{numPack[3-k]=(int)(tmp/(long int)(pow(256.,3-k)));
	 tmp=tmp-(long int)pow(256.,3-k)*(int)(tmp/(long int)(pow(256.,3-k)));}}	
   for (i=0;i<4;i++) fputc((int)(unsigned char)(numPack[i]),fo_log_kadr);
   adr1=icount-1024*2; 
   for (k=0;k<4;k++) adress[k]=0;
   for (k=0;k<4;k++)
   {if (adr1/(long int)(pow(256.,3-k))>=1) 
	{adress[3-k]=(int)(adr1/(long int)(pow(256.,3-k)));
	 adr1=adr1-(long int)pow(256.,3-k)*
	 (int)(adr1/(long int)(pow(256.,3-k)));}}	
   for (i=0;i<4;i++) fputc((int)(unsigned char)(adress[i]),fo_log_kadr);
   adr2=icount-1024;
   for (k=0;k<4;k++) adress[k]=0;
   for (k=0;k<4;k++)
   {if (adr2/(long int)(pow(256.,3-k))>=1) 
    {adress[3-k]=(int)(adr2/(long int)(pow(256.,3-k)));
	 adr2=adr2-(long int)pow(256.,3-k)*
	 (int)(adr2/(long int)(pow(256.,3-k)));}}
   for (i=0;i<4;i++) fputc((int)(unsigned char)(adress[i]),fo_log_kadr);
   fputc(flags,fo_log_kadr);
  }
 }
/***************************************************************/
/***************************************************************/
 if (flag_CRC[2]==0||(flag_rout[2]==0&&flag_pack[2]==0)||
     (flag_CRC[2]!=0&&flag_CRC_cor[2]==1)) 
 {end=0;
  for (i=0;i<1024;i++) fokadr_cln1<<(unsigned char)buffer3[i];
  //Write in file mmm_counter_nnnnn_cln1_bin.log
  tmp=numP[2];
  for (k=0;k<4;k++) numPack[k]=0;
  for (k=0;k<4;k++)
   {if (tmp/(long int)(pow(256.,3-k))>=1) 
    {numPack[3-k]=(int)(tmp/(long int)(pow(256.,3-k)));
     tmp=tmp-(long int)pow(256.,3-k)*
     (int)(tmp/(long int)(pow(256.,3-k)));}}	
   for (i=0;i<4;i++) fputc((int)(unsigned char)(numPack[i]),fo_log_kadr);
   adr1=icount-1024; 
   for (k=0;k<4;k++) adress[k]=0;
   for (k=0;k<4;k++)
   {if (adr1/(long int)(pow(256.,3-k))>=1) 
    {adress[3-k]=(int)(adr1/(long int)(pow(256.,3-k)));
     adr1=adr1-(long int)pow(256.,3-k)*
     (int)(adr1/(long int)(pow(256.,3-k)));}}	
   for (i=0;i<4;i++) fputc((int)(unsigned char)(adress[i]),fo_log_kadr);
   adr2=icount;
   for (k=0;k<4;k++) adress[k]=0;
   for (k=0;k<4;k++)
   {if (adr2/(long int)(pow(256.,3-k))>=1) 
    {adress[3-k]=(int)(adr2/(long int)(pow(256.,3-k)));
     adr2=adr2-(long int)pow(256.,3-k)*
     (int)(adr2/(long int)(pow(256.,3-k)));}}
   for (i=0;i<4;i++) fputc((int)(unsigned char)(adress[i]),fo_log_kadr);
   fputc(flags,fo_log_kadr);

  for (i=8;i<1019;i++)
	{
//	 if (fmodf(float((Num_kadrs[Rcount-1]-1)*1015+i-8),
//		 float(64))==0.&&buffer3[i]==0&&buffer3[i+1]==0&&
//		 buffer3[i+2]==0&&buffer3[i+3]==0&&buffer3[i+4]==0) end=1;
         if (end!=1) fo_cln1<<(unsigned char)buffer3[i];
	}
  if (end!=1) {for(i=1019;i<1023;i++) {fo_cln1<<(unsigned char)buffer3[i];}}
 }

 icount_int = (int)(icount/1024);
 icount_frac= icount%1024;
// cout<<"\nfile size="<<icount_total<<" Byte   "<<N1
//     <<" identified cadrs   Number of routs="<<Rcount<<"\n";
// cout<<" size of identified cadrs="<<icount<<" bytes   "<<count_ident[0]<<"\n";

//***protocol file***********************************************
 strcpy(file_name_prot, path);
 strcat(file_name_prot, name);
 file_length=(int)(strlen(name));
 fprintf(fo,"file name=%s\n",file_name_prot);
 fprintf(fo,"file size= %i,%i kByte \n", icount_int,
		                                       icount_frac);
 for (i=0;i<Rcount;i++)
 {fprintf(fo,"route=%i;",NumRout[i]);
  fprintf(fo," size of identified cadrs =%lli bytes;",count_ident[i]);
  fprintf(fo," total size of route=%lli bytes;\n",count_tot[i]);
  fprintf(fo,"number of errors=%i;\n",Num_bad_kadrs[i]);
 }

 //***route's number log file**************************************
 for (i=0;i<Rcount;i++) {fprintf(fo_route,"%1c",(char)(NumRout[i]));}

//***cln1.inf file*************************************************
 fprintf(fo1,"file name=");
 fprintf(fo1,"%s%s",path,name);
 fprintf(fo1,"\nfile size= %i,%i kByte \n", icount_int,
		                                       icount_frac);
 fprintf(fo1,"number identified kadrs= %i\n", N1);
 fprintf(fo1,"number of routes= %i\n", Rcount);
 
 for (i=0;i<Rcount;i++)
 {
  Num_good_kadrs[i]=Num_kadrs[i]-Num_bad_kadrs[i];
  quality[i]=(float)(1.)-(float)(Num_bad_kadrs[i])/(float)(Num_kadrs[i]);
  fprintf(fo1,"\nRoute number=%i: number of normal kadrs= %i:\n",
		  NumRout[i],Num_good_kadrs[i]);
  fprintf(fo1,"first kadre number= %lli:", packet_first[i]);
  fprintf(fo1," last kadre number= %lli:", packet_last[i]);
  fprintf(fo1," Number of missed kadrs= %i:\n", count_missed[i]);
//  fprintf(fo1,"Number of double transmitted kadrs= %i:\n", count_sec_missed[i]);  
  fprintf(fo1,"number of identified kadres=%lli: quality index=%f\n", Num_kadrs[i], quality[i]);
 }	 
//***************************************************************
  fin.close();
  fclose(fout);
  fclose(fo);
  fclose(fo1);
  fclose(foRout);
  fclose(foMiss);
  fo_cln1.close();
  fclose(fo_log_kadr);
  fokadr_cln1.close();
  fclose(fo_route);
}

