| 1 |
///////////////PAMELA PACKETS SORT///////////////////////////////////
|
| 2 |
#include "descript.h"
|
| 3 |
#include "flag.h"
|
| 4 |
|
| 5 |
void RunHeaderReader(char *buf);
|
| 6 |
extern long int OBTtimesync;
|
| 7 |
extern long int Timesync;
|
| 8 |
extern char MYSQL[80];
|
| 9 |
extern char database[80];
|
| 10 |
extern int print_flag;
|
| 11 |
|
| 12 |
struct Names
|
| 13 |
{
|
| 14 |
char a[80]; /*Full name for nnnnn_mmm_xxx_AA.bin*/
|
| 15 |
char a_short[80]; /*Short name for nnnnn_mmm_xxx_AA.bin*/
|
| 16 |
FILE *fo_a;
|
| 17 |
};
|
| 18 |
|
| 19 |
void sort(char log[], char pam[], char path[], char path3[],
|
| 20 |
char file_init[], int packet_type[])
|
| 21 |
|
| 22 |
{
|
| 23 |
FILE *fo_log, *fo_ql, *fo_work2, *f_temp;
|
| 24 |
|
| 25 |
Names data[NumberPack+1];
|
| 26 |
char fno2[80]; /* Out help file mm_nnnnn_work2.dat*/
|
| 27 |
char ql[80];
|
| 28 |
char tmp[80], tmp1[80], numer[10];
|
| 29 |
char arina[80];
|
| 30 |
char connection[80];
|
| 31 |
char query[500];
|
| 32 |
long int orbit, route, count;
|
| 33 |
// char buffer[10];
|
| 34 |
char *packet=new char[Nmax];
|
| 35 |
unsigned int type=0; /*PAMELA packet type*/
|
| 36 |
unsigned int error=0; /*PAMELA packet error flag*/
|
| 37 |
int Length=0; /*Length of filename*/
|
| 38 |
long int counter[NumberPack+1];/*Counter for PAMELA packets in each dat file*/
|
| 39 |
unsigned long int adr1=0; /*First byte of PAMELA packet in _cln2.pam*/
|
| 40 |
unsigned long int adr2=0; /*Last byte of PAMELA packet in _cln2.pam*/
|
| 41 |
long int time=0; /*PAMELA packet time*/
|
| 42 |
long int moscowtime=0; /*moscow time of packets*/
|
| 43 |
long int first[NumberPack+1]; /*First byte for PAMELA packets of given type*/
|
| 44 |
long int last[NumberPack+1]; /*Last byte for PAMELA packets of given type*/
|
| 45 |
long int temp=0;
|
| 46 |
long int time_differ=0;
|
| 47 |
int flag_time=0;
|
| 48 |
int Hex_first[4];
|
| 49 |
int Hex_last[4];
|
| 50 |
int i=0, j=0, stop=0;
|
| 51 |
|
| 52 |
for (i=0;i<4;i++)
|
| 53 |
{Hex_first[i]=0;
|
| 54 |
Hex_last[i]=0;}
|
| 55 |
|
| 56 |
for (i=0;i<NumberPack+1;i++)
|
| 57 |
{counter[i]=0;
|
| 58 |
first[i]=0;
|
| 59 |
last[i]=0;}
|
| 60 |
|
| 61 |
Length=(int)(strlen(file_init));
|
| 62 |
strncat(strcpy(tmp,"\0"),file_init,Length-8);
|
| 63 |
strcat(strcpy(tmp1,path),"tmp.dat");
|
| 64 |
f_temp=fopen(tmp1,"wb+");
|
| 65 |
|
| 66 |
stringstream oss;
|
| 67 |
|
| 68 |
strcpy(connection,MYSQL);
|
| 69 |
strcat(connection,database);
|
| 70 |
TSQLServer *sqlServer = TSQLServer::Connect(connection, "pamelaprod", "rawprod");
|
| 71 |
|
| 72 |
for (i=0;i<NumberPack+1;i++)
|
| 73 |
{
|
| 74 |
if (i==0) {fprintf(f_temp,"%02i",i);}
|
| 75 |
else {fprintf(f_temp,"%02X",packet_type[i-1]);}
|
| 76 |
fseek(f_temp,0,0);
|
| 77 |
fgets(numer,10,f_temp);
|
| 78 |
fseek(f_temp,0,0);
|
| 79 |
/*************Open files /home/pamelaprod/rawreader/level0/nnnnn/xxx_mmm/packets/nnnnn_mmm_xxx_AA.bin*******/
|
| 80 |
strncat(strcpy(data[i].a,path3),file_init,Length-8);
|
| 81 |
strcat(data[i].a,numer);
|
| 82 |
strcat(data[i].a,".bin");
|
| 83 |
//form the 'nnnnn_mmm_xxx_AA.bin'
|
| 84 |
for (j=0;j<20;j++) tmp[j]=data[i].a[strlen(data[i].a)-(20-j)];
|
| 85 |
/*********************Create names nnnnn_mmm_xxx_AA.bin********************/
|
| 86 |
for (j=0;j<20;j++)
|
| 87 |
{data[i].a_short[j]=tmp[j]; data[i].a_short[20]='\0';
|
| 88 |
data[i].a[strlen(data[i].a)-(20-j)]=tmp[j];
|
| 89 |
}
|
| 90 |
|
| 91 |
data[i].fo_a=fopen(data[i].a,"wb");
|
| 92 |
if(data[i].fo_a==NULL)
|
| 93 |
{cerr<<"can not open _%s.dat file",numer; exit(1);}
|
| 94 |
|
| 95 |
}
|
| 96 |
fclose(f_temp);
|
| 97 |
remove(tmp1);
|
| 98 |
/**********************OPEN NECESSARY FILES************************/
|
| 99 |
/******************************************************************/
|
| 100 |
ifstream fo_pam;
|
| 101 |
fo_pam.open(pam,ios::binary); //open input file "nnnnn_mmm_xxx_cln2.pam"
|
| 102 |
if(fo_pam.good()==0) {cerr<<"can not open pam file"; exit(1);}
|
| 103 |
|
| 104 |
ofstream fo_arina;
|
| 105 |
strcpy(arina,"/home/arina/orientation/");
|
| 106 |
strcat(arina,data[28].a_short);
|
| 107 |
if (print_flag>0) cout<<arina<<"\n";
|
| 108 |
fo_arina.open(arina,ios::binary);
|
| 109 |
if(fo_arina.good()==0) {cerr<<"can not open arina file"; exit(1);}
|
| 110 |
|
| 111 |
fo_log=fopen(log, "rb"); //open input file "nnnnn_mmm_xxx_cln2.log"
|
| 112 |
if(fo_log==NULL) {cerr<<"can not open log file"; exit(1);}
|
| 113 |
|
| 114 |
strcpy(fno2,path3);
|
| 115 |
strncat(fno2,file_init,strlen(file_init)-8);
|
| 116 |
strcat(fno2,"work2.dat");
|
| 117 |
fo_work2=fopen(fno2, "wb"); //open output help file "nnnnn_mmm_xxx_work2.dat"
|
| 118 |
if(fo_work2==NULL) {cerr<<"can not open outputfile"; exit(1);}
|
| 119 |
|
| 120 |
strncat(strcpy(ql,path3),file_init,Length-8);
|
| 121 |
strcat(ql,"ql.log");
|
| 122 |
fo_ql=fopen(ql, "wb"); //open output file "nnnnn_mmm_xxx_ql.log"
|
| 123 |
if(fo_ql==NULL) {cerr<<"can not open ql file"; exit(1);}
|
| 124 |
|
| 125 |
/******************************************************************/
|
| 126 |
/******************************************************************/
|
| 127 |
while(feof(fo_log)==0)
|
| 128 |
{
|
| 129 |
//read PAMELA packet information from nnnnn_mmm_xxx_cln2.log
|
| 130 |
fscanf(fo_log,"%1c",&type);
|
| 131 |
if (feof(fo_log)!=0) break;
|
| 132 |
fscanf(fo_log,"%4c",&adr1);
|
| 133 |
if (feof(fo_log)!=0) break;
|
| 134 |
fscanf(fo_log,"%4c",&adr2);
|
| 135 |
if (feof(fo_log)!=0) break;
|
| 136 |
fscanf(fo_log,"%1c",&error);
|
| 137 |
if (feof(fo_log)!=0) break;
|
| 138 |
|
| 139 |
if ( ( (bitset<8>(error).test(7))||(error==0)||
|
| 140 |
((bitset<8>(error).test(0))&&(!bitset<8>(error).test(1))&&(!bitset<8>(error).test(2))&&
|
| 141 |
(!bitset<8>(error).test(3))&&(!bitset<8>(error).test(4))&&(!bitset<8>(error).test(5))&&
|
| 142 |
(!bitset<8>(error).test(6))) )&&
|
| 143 |
(type>0)&&(type<NumberPack+1) )
|
| 144 |
{
|
| 145 |
/*Place file pointer in nnnnn_mmm_xxx_cln2.pam on packet begin*/
|
| 146 |
fo_pam.seekg(adr1-1,ios::beg);
|
| 147 |
//read PAMELA packet from nnnnn_mmm_xxx_cln2.pam
|
| 148 |
fo_pam.read(packet,adr2-adr1+1);
|
| 149 |
|
| 150 |
/*OBT of Pamela*/
|
| 151 |
time=(long int)(unsigned char)(packet[11])+
|
| 152 |
256*(long int)(unsigned char)(packet[10])+
|
| 153 |
256*256*(long int)(unsigned char)(packet[9])+
|
| 154 |
256*256*256*(long int)(unsigned char)(packet[8]);
|
| 155 |
|
| 156 |
/* mikhailov time goes to moscow time 08 12 2004*/
|
| 157 |
|
| 158 |
moscowtime=time-(OBTtimesync-Timesync);
|
| 159 |
|
| 160 |
/*temp is packet number*/
|
| 161 |
temp=(long int)(unsigned char)(packet[7])+
|
| 162 |
256*(long int)(unsigned char)(packet[6])+
|
| 163 |
256*256*(long int)(unsigned char)(packet[5]);
|
| 164 |
if ((print_flag>0)&&(fmod((float)(temp),(float)(10000.))==0.)&&(temp!=0)) cout<<"sorting "<<temp<<"\n";
|
| 165 |
|
| 166 |
/*unpacking several packets.... mikhailov
|
| 167 |
unpackingPamelaPacket(packet,type)*/
|
| 168 |
if( type == 11)
|
| 169 |
{
|
| 170 |
RunHeaderReader(packet);
|
| 171 |
if (flag_time==0) time_differ=Timesync-OBTtimesync;
|
| 172 |
flag_time=1;
|
| 173 |
}
|
| 174 |
/*******************Packet with type "type"***********************/
|
| 175 |
counter[type]+=1;
|
| 176 |
//write PAMELA packet in nnnnn_mmm_xxx_AA.bin
|
| 177 |
if (type!=4) fwrite(packet,adr2-adr1+1,1,data[type].fo_a);
|
| 178 |
//write for ARINA
|
| 179 |
if (type==28) fo_arina.write(packet,adr2-adr1+1);
|
| 180 |
first[type]=last[type]+1;
|
| 181 |
last[type]+=adr2-adr1+1;
|
| 182 |
temp=first[type];
|
| 183 |
for (i=0;i<4;i++) Hex_first[i]=0;
|
| 184 |
for (i=0;i<4;i++)
|
| 185 |
{if (temp/(long int)(pow(256.,3-i))>=1)
|
| 186 |
{Hex_first[3-i]=(int)(temp/(long int)(pow(256.,3-i)));
|
| 187 |
temp=temp-(long int)pow(256.,3-i)*
|
| 188 |
(int)(temp/(long int)(pow(256.,3-i)));}}
|
| 189 |
temp=last[type];
|
| 190 |
for (i=0;i<4;i++) Hex_last[i]=0;
|
| 191 |
for (i=0;i<4;i++)
|
| 192 |
{if (temp/(long int)(pow(256.,3-i))>=1)
|
| 193 |
{Hex_last[3-i]=(int)(temp/(long int)(pow(256.,3-i)));
|
| 194 |
temp=temp-(long int)pow(256.,3-i)*
|
| 195 |
(int)(temp/(long int)(pow(256.,3-i)));}
|
| 196 |
}
|
| 197 |
//write information about PAMELA packet in nnnnn_mmm_xxx_ql.log
|
| 198 |
if (type!=4)
|
| 199 |
{
|
| 200 |
fprintf(fo_ql,"%1c",char(type));
|
| 201 |
for (i=0;i<4;i++) fprintf(fo_ql,"%1c",char(Hex_first[i]));
|
| 202 |
// for (i=0;i<4;i++) fprintf(fo_ql,"%1c",char(Hex_last[i]));
|
| 203 |
fprintf(fo_ql,"%1c",char(error));
|
| 204 |
// for (i=8;i<12;i++) fprintf(fo_ql,"%1c",packet[i]); //!!!!temporary
|
| 205 |
|
| 206 |
|
| 207 |
/* mikhailov time goes to moscow time* 08 12 2004*/
|
| 208 |
for (i=0;i<4;i++) fprintf(fo_ql,"%1c",char(time>>((3-i)*8)&0XFF));
|
| 209 |
for (i=0;i<4;i++) fprintf(fo_ql,"%1c",char(moscowtime>>((3-i)*8)&0XFF));
|
| 210 |
for (i=0;i<3;i++) fprintf(fo_ql,"%1c",packet[i+5]);
|
| 211 |
for (i=0;i<15;i++) fprintf(fo_ql,"%1c",char(0XFF)); //added to fill 32 bytes
|
| 212 |
}
|
| 213 |
}
|
| 214 |
else
|
| 215 |
{
|
| 216 |
/*******************Packets with errors***************************/
|
| 217 |
char *er_pack=new char [adr2-adr1+1];
|
| 218 |
/*Place file pointer in nnnnn_mmm_xxx_cln2.pam on error packet begin*/
|
| 219 |
fo_pam.seekg(adr1-1,ios::beg);
|
| 220 |
//read PAMELA error packet from nnnnn_mmm_xxx_cln2.pam
|
| 221 |
fo_pam.read(er_pack,adr2-adr1+1);
|
| 222 |
time=(long int)(unsigned char)(er_pack[11])+
|
| 223 |
256*(long int)(unsigned char)(er_pack[10])+
|
| 224 |
256*256*(long int)(unsigned char)(er_pack[9])+
|
| 225 |
256*256*256*(long int)(unsigned char)(er_pack[8]);
|
| 226 |
counter[0]+=1;
|
| 227 |
//write PAMELA packet in nnnnn_mmm_xxx_00.bin
|
| 228 |
fwrite(er_pack,adr2-adr1+1,1,data[0].fo_a);
|
| 229 |
first[0]=last[0]+1;
|
| 230 |
last[0]+=adr2-adr1+1;
|
| 231 |
temp=first[0];
|
| 232 |
for (i=0;i<4;i++) Hex_first[i]=0;
|
| 233 |
for (i=0;i<4;i++)
|
| 234 |
{if (temp/(long int)(pow(256.,3-i))>=1)
|
| 235 |
{Hex_first[3-i]=(int)(temp/(long int)(pow(256.,3-i)));
|
| 236 |
temp=temp-(long int)pow(256.,3-i)*
|
| 237 |
(int)(temp/(long int)(pow(256.,3-i)));}
|
| 238 |
}
|
| 239 |
temp=last[0];
|
| 240 |
for (i=0;i<4;i++) Hex_last[i]=0;
|
| 241 |
for (i=0;i<4;i++)
|
| 242 |
{if (temp/(long int)(pow(256.,3-i))>=1)
|
| 243 |
{Hex_last[3-i]=(int)(temp/(long int)(pow(256.,3-i)));
|
| 244 |
temp=temp-(long int)pow(256.,3-i)*
|
| 245 |
(int)(temp/(long int)(pow(256.,3-i)));}
|
| 246 |
}
|
| 247 |
if (type!=4)
|
| 248 |
{//write information about PAMELA packet in nnnnn_mmm_xxx_ql.log
|
| 249 |
fprintf(fo_ql,"%1c",(char)(type));
|
| 250 |
for (i=0;i<4;i++) fprintf(fo_ql,"%1c",char(Hex_first[i]));
|
| 251 |
// for (i=0;i<4;i++) fprintf(fo_ql,"%1c",char(Hex_last[i])); //temporary
|
| 252 |
fprintf(fo_ql,"%1c",char(error));
|
| 253 |
// for (i=8;i<12;i++) fprintf(fo_ql,"%1c",er_pack[i]);
|
| 254 |
/* mikhailov time goes to moscow time 08 12 2004*/
|
| 255 |
for (i=0;i<4;i++) fprintf(fo_ql,"%1c",char(time>>((3-i)*8)&0XFF));
|
| 256 |
for (i=0;i<4;i++) fprintf(fo_ql,"%1c",char(moscowtime>>((3-i)*8)&0XFF));
|
| 257 |
for (i=0;i<3;i++) fprintf(fo_ql,"%1c",er_pack[i+5]);
|
| 258 |
for (i=0;i<15;i++) fprintf(fo_ql,"%1c",char(0XFF)); //added to fill 32 bytes
|
| 259 |
|
| 260 |
delete [] er_pack;
|
| 261 |
}
|
| 262 |
}
|
| 263 |
}
|
| 264 |
|
| 265 |
for (i=0;i<NumberPack+1;i++)
|
| 266 |
fprintf(fo_work2,"Number of packets in %s file = %i\n",data[i].a_short,counter[i]);
|
| 267 |
|
| 268 |
for(i=0;i<5;i++) {tmp1[i]=file_init[i];} tmp1[5]=' ';
|
| 269 |
sscanf(tmp1,"%5d",&orbit);
|
| 270 |
|
| 271 |
for(i=6;i<9;i++) {tmp1[i-6]=file_init[i];} tmp1[3]=' ';
|
| 272 |
sscanf(tmp1,"%3d",&route);
|
| 273 |
|
| 274 |
for(i=10;i<13;i++) {tmp1[i-10]=file_init[i];} tmp1[3]=' ';
|
| 275 |
sscanf(tmp1,"%3d",&count);
|
| 276 |
|
| 277 |
oss.str("");
|
| 278 |
oss << "UPDATE FILES1 set time_dif="<<time_differ<<" where cirquit_n="<<orbit<<" and route_n="<<route<<
|
| 279 |
" and counter="<<count;
|
| 280 |
if (print_flag>0) cout<<oss.str().c_str()<<" table FILES1\n";
|
| 281 |
TSQLResult *res=sqlServer->Query(oss.str().c_str());
|
| 282 |
|
| 283 |
// Form the query string to write PAMELA packets number in Packets
|
| 284 |
query[0]='\0';
|
| 285 |
for(i=0;i<NumberPack;i++)
|
| 286 |
{
|
| 287 |
sprintf(tmp,"%i",counter[i]);
|
| 288 |
strcat(tmp,"','");
|
| 289 |
strcat(query,tmp);
|
| 290 |
}
|
| 291 |
sprintf(tmp,"%i",counter[i]);
|
| 292 |
strcat(tmp,"')");
|
| 293 |
strcat(query,tmp);
|
| 294 |
|
| 295 |
oss.str("");
|
| 296 |
oss << "INSERT INTO PACKETS (cirquit_n, route_n, counter, P00, P07, P08, P09, P10, P11, P12, P13, P16, P17, P18, P19, P1A,"
|
| 297 |
<< " P1B, P1C, P1D, P20, P21, P22, P23, P24, P25, P30, P50, P51, P52, P53, P54, P55, P60, P70, P71, P72, P73, P74,"
|
| 298 |
<< " P75, P76, P77, P81, P82, P83, P84, P85, P86, P88, P89, P8A, PA1, PAB, PF0, PF1, PF2, PF3, PF4)"
|
| 299 |
<< " VALUES ('"<< orbit << "','"<< route << "','"<<count<<"','"<<query;
|
| 300 |
if (print_flag>0) cout<<oss.str().c_str()<<" table PACKETS\n";
|
| 301 |
TSQLResult *res1=sqlServer->Query(oss.str().c_str());
|
| 302 |
|
| 303 |
fo_pam.close();
|
| 304 |
fo_arina.close();
|
| 305 |
fclose(fo_log);
|
| 306 |
fclose(fo_work2);
|
| 307 |
|
| 308 |
for(i=0;i<NumberPack+1;i++) fclose(data[i].fo_a);
|
| 309 |
fclose(fo_ql);
|
| 310 |
delete [] packet;
|
| 311 |
}
|