/[PAMELA software]/rawreader/src/sort.cpp
ViewVC logotype

Contents of /rawreader/src/sort.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.4 - (show annotations) (download)
Sun Dec 10 13:06:03 2006 UTC (17 years, 11 months ago) by cafagna
Branch: MAIN
CVS Tags: v1r14
Changes since 1.3: +32 -36 lines
5th of December version

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

  ViewVC Help
Powered by ViewVC 1.1.23