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

Annotation of /rawreader/src/sort.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.5 - (hide annotations) (download)
Sun Jan 28 10:55:52 2007 UTC (17 years, 10 months ago) by cafagna
Branch: MAIN
CVS Tags: v1r15, HEAD
Changes since 1.4: +3 -2 lines
Main two features of this version are:
1)  the estimation of absolute time is stored  in DB and in timesync.dat
file
2) There is CLN2_flag which gives possibility:
 a) to avoid creation of cln2 files Cln2_flag=0,
 b) if Cln2_flag=1 cln2 file is created with all data in raw file
 c) if Cln2_flag=2 cln2 file is created with only new data in raw file

1 cafagna 1.1 ///////////////PAMELA PACKETS SORT///////////////////////////////////
2     #include "descript.h"
3 cafagna 1.2 #include "flag.h"
4    
5 cafagna 1.1 void RunHeaderReader(char *buf);
6     extern long int OBTtimesync;
7     extern long int Timesync;
8 cafagna 1.4 extern char MYSQL[120];
9     extern char database[120];
10     extern char arina[120];
11 cafagna 1.3 extern int print_flag;
12    
13 cafagna 1.1 struct Names
14     {
15 cafagna 1.4 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 cafagna 1.1 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 cafagna 1.4 FILE *fo_log, *fo_ql, *fo_work2;
25 cafagna 1.1
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 cafagna 1.3 char connection[80];
31     char query[500];
32 cafagna 1.4 long int orbit=0, route=0, count=0;
33 cafagna 1.1 // 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 cafagna 1.3 long int time_differ=0;
47     int flag_time=0;
48 cafagna 1.1 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 cafagna 1.2 Length=(int)(strlen(file_init));
60 cafagna 1.1 strncat(strcpy(tmp,"\0"),file_init,Length-8);
61    
62 cafagna 1.3 stringstream oss;
63    
64     strcpy(connection,MYSQL);
65 cafagna 1.4 strcat(connection,database);
66 cafagna 1.3 TSQLServer *sqlServer = TSQLServer::Connect(connection, "pamelaprod", "rawprod");
67 cafagna 1.4 if (sqlServer==0) {cerr<<"can not connect with MYSQL server\n"; exit(1);}
68 cafagna 1.3
69 cafagna 1.1 for (i=0;i<NumberPack+1;i++)
70     {
71 cafagna 1.4 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 cafagna 1.2 /*************Open files /home/pamelaprod/rawreader/level0/nnnnn/xxx_mmm/packets/nnnnn_mmm_xxx_AA.bin*******/
83 cafagna 1.1 strncat(strcpy(data[i].a,path3),file_init,Length-8);
84 cafagna 1.2 strcat(data[i].a,numer);
85 cafagna 1.1 strcat(data[i].a,".bin");
86 cafagna 1.2 //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 cafagna 1.1 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 cafagna 1.2 }
93 cafagna 1.1
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 cafagna 1.3 fo_pam.open(pam,ios::binary); //open input file "nnnnn_mmm_xxx_cln2.pam"
103 cafagna 1.1 if(fo_pam.good()==0) {cerr<<"can not open pam file"; exit(1);}
104    
105     ofstream fo_arina;
106 cafagna 1.4 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 cafagna 1.1 if(fo_arina.good()==0) {cerr<<"can not open arina file"; exit(1);}
110    
111 cafagna 1.3 fo_log=fopen(log, "rb"); //open input file "nnnnn_mmm_xxx_cln2.log"
112 cafagna 1.1 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 cafagna 1.2 strcat(fno2,"work2.dat");
117 cafagna 1.3 fo_work2=fopen(fno2, "wb"); //open output help file "nnnnn_mmm_xxx_work2.dat"
118 cafagna 1.1 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 cafagna 1.3 fo_ql=fopen(ql, "wb"); //open output file "nnnnn_mmm_xxx_ql.log"
123 cafagna 1.1 if(fo_ql==NULL) {cerr<<"can not open ql file"; exit(1);}
124    
125     /******************************************************************/
126     /******************************************************************/
127     while(feof(fo_log)==0)
128     {
129 cafagna 1.3 //read PAMELA packet information from nnnnn_mmm_xxx_cln2.log
130 cafagna 1.1 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 cafagna 1.3 /*Place file pointer in nnnnn_mmm_xxx_cln2.pam on packet begin*/
146 cafagna 1.1 fo_pam.seekg(adr1-1,ios::beg);
147 cafagna 1.3 //read PAMELA packet from nnnnn_mmm_xxx_cln2.pam
148 cafagna 1.1 fo_pam.read(packet,adr2-adr1+1);
149    
150     /*OBT of Pamela*/
151     time=(long int)(unsigned char)(packet[11])+
152 cafagna 1.3 256*(long int)(unsigned char)(packet[10])+
153 cafagna 1.1 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 cafagna 1.4 temp=(long int)(unsigned char)(packet[7])+256*(long int)(unsigned char)(packet[6])+
162     256*256*(long int)(unsigned char)(packet[5]);
163 cafagna 1.3 if ((print_flag>0)&&(fmod((float)(temp),(float)(10000.))==0.)&&(temp!=0)) cout<<"sorting "<<temp<<"\n";
164    
165 cafagna 1.1 /*unpacking several packets.... mikhailov
166 cafagna 1.3 unpackingPamelaPacket(packet,type)*/
167 cafagna 1.5 if( type == 16)
168     {cout<<"RunHeader is calling\n";
169 cafagna 1.3 RunHeaderReader(packet);
170 cafagna 1.5 cout<<"RunHeader is finishing\n";
171 cafagna 1.3 if (flag_time==0) time_differ=Timesync-OBTtimesync;
172     flag_time=1;
173     }
174 cafagna 1.1 /*******************Packet with type "type"***********************/
175     counter[type]+=1;
176 cafagna 1.3 //write PAMELA packet in nnnnn_mmm_xxx_AA.bin
177 cafagna 1.1 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 cafagna 1.3 //write information about PAMELA packet in nnnnn_mmm_xxx_ql.log
198 cafagna 1.1 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 cafagna 1.3 /*Place file pointer in nnnnn_mmm_xxx_cln2.pam on error packet begin*/
219 cafagna 1.1 fo_pam.seekg(adr1-1,ios::beg);
220 cafagna 1.3 //read PAMELA error packet from nnnnn_mmm_xxx_cln2.pam
221 cafagna 1.1 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 cafagna 1.3 //write PAMELA packet in nnnnn_mmm_xxx_00.bin
228 cafagna 1.1 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 cafagna 1.3 {//write information about PAMELA packet in nnnnn_mmm_xxx_ql.log
249 cafagna 1.1 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 cafagna 1.4 if (print_flag>0) cout<<file_init<<"\n";
268     for(i=0;i<5;i++) {tmp1[i]=file_init[i];} tmp1[5]='\0';
269     sscanf(tmp1,"%5u",&orbit);
270     for(i=6;i<9;i++) {tmp1[i-6]=file_init[i];} tmp1[3]='\0';
271     sscanf(tmp1,"%3u",&route);
272     for(i=10;i<13;i++) {tmp1[i-10]=file_init[i];} tmp1[3]='\0';
273     sscanf(tmp1,"%3u",&count);
274 cafagna 1.3 oss.str("");
275     oss << "UPDATE FILES1 set time_dif="<<time_differ<<" where cirquit_n="<<orbit<<" and route_n="<<route<<
276     " and counter="<<count;
277     if (print_flag>0) cout<<oss.str().c_str()<<" table FILES1\n";
278     TSQLResult *res=sqlServer->Query(oss.str().c_str());
279    
280     // Form the query string to write PAMELA packets number in Packets
281     query[0]='\0';
282     for(i=0;i<NumberPack;i++)
283     {
284     sprintf(tmp,"%i",counter[i]);
285     strcat(tmp,"','");
286     strcat(query,tmp);
287     }
288     sprintf(tmp,"%i",counter[i]);
289     strcat(tmp,"')");
290     strcat(query,tmp);
291    
292     oss.str("");
293     oss << "INSERT INTO PACKETS (cirquit_n, route_n, counter, P00, P07, P08, P09, P10, P11, P12, P13, P16, P17, P18, P19, P1A,"
294     << " P1B, P1C, P1D, P20, P21, P22, P23, P24, P25, P30, P50, P51, P52, P53, P54, P55, P60, P70, P71, P72, P73, P74,"
295     << " P75, P76, P77, P81, P82, P83, P84, P85, P86, P88, P89, P8A, PA1, PAB, PF0, PF1, PF2, PF3, PF4)"
296     << " VALUES ('"<< orbit << "','"<< route << "','"<<count<<"','"<<query;
297     if (print_flag>0) cout<<oss.str().c_str()<<" table PACKETS\n";
298     TSQLResult *res1=sqlServer->Query(oss.str().c_str());
299    
300 cafagna 1.1 fo_pam.close();
301     fo_arina.close();
302     fclose(fo_log);
303     fclose(fo_work2);
304    
305     for(i=0;i<NumberPack+1;i++) fclose(data[i].fo_a);
306     fclose(fo_ql);
307     delete [] packet;
308     }

  ViewVC Help
Powered by ViewVC 1.1.23