| 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 | struct Names | 
| 9 | { | 
| 10 | char a[80];                     /*Full name for mmm_nnnnn_counter_xx.bin*/ | 
| 11 | char a_short[80];               /*Short name for mmm_nnnnn_counter_xx.bin*/ | 
| 12 | FILE *fo_a; | 
| 13 | }; | 
| 14 |  | 
| 15 | void sort(char log[], char pam[], char path[], char path3[], | 
| 16 | char file_init[], int packet_type[]) | 
| 17 |  | 
| 18 | { | 
| 19 | FILE *fo_log, *fo_ql, *fo_work2, *f_temp; | 
| 20 |  | 
| 21 | Names data[NumberPack+1]; | 
| 22 | char fno2[80];       /* Out help file mm_nnnnn_work2.dat*/ | 
| 23 | char ql[80]; | 
| 24 | char tmp[80], tmp1[80], numer[10]; | 
| 25 | char arina[80]; | 
| 26 | // char buffer[10]; | 
| 27 | char *packet=new char[Nmax]; | 
| 28 | unsigned int type=0;           /*PAMELA packet type*/ | 
| 29 | unsigned int error=0;          /*PAMELA packet error flag*/ | 
| 30 | int Length=0;                  /*Length of filename*/ | 
| 31 | long int counter[NumberPack+1];/*Counter for PAMELA packets in each dat file*/ | 
| 32 | unsigned long int adr1=0;      /*First byte of PAMELA packet in _cln2.pam*/ | 
| 33 | unsigned long int adr2=0;      /*Last byte of PAMELA packet in _cln2.pam*/ | 
| 34 | long int time=0;               /*PAMELA packet time*/ | 
| 35 | long int moscowtime=0;         /*moscow time of packets*/ | 
| 36 | long int first[NumberPack+1];  /*First byte for PAMELA packets of given type*/ | 
| 37 | long int last[NumberPack+1];   /*Last byte for PAMELA packets of given type*/ | 
| 38 | long int temp=0; | 
| 39 | int Hex_first[4]; | 
| 40 | int Hex_last[4]; | 
| 41 | int i=0, j=0, stop=0; | 
| 42 |  | 
| 43 | for (i=0;i<4;i++) | 
| 44 | {Hex_first[i]=0; | 
| 45 | Hex_last[i]=0;} | 
| 46 |  | 
| 47 | for (i=0;i<NumberPack+1;i++) | 
| 48 | {counter[i]=0; | 
| 49 | first[i]=0; | 
| 50 | last[i]=0;} | 
| 51 |  | 
| 52 | Length=(int)(strlen(file_init)); | 
| 53 | strncat(strcpy(tmp,"\0"),file_init,Length-8); | 
| 54 | strcat(strcpy(tmp1,path),"tmp.dat"); | 
| 55 | f_temp=fopen(tmp1,"wb+"); | 
| 56 |  | 
| 57 | for (i=0;i<NumberPack+1;i++) | 
| 58 | { | 
| 59 | if (i==0) {fprintf(f_temp,"%02i",i);} | 
| 60 | else {fprintf(f_temp,"%02X",packet_type[i-1]);} | 
| 61 | fseek(f_temp,0,0); | 
| 62 | fgets(numer,10,f_temp); | 
| 63 | fseek(f_temp,0,0); | 
| 64 | /*************Open files /home/pamelaprod/rawreader/level0/nnnnn/xxx_mmm/packets/nnnnn_mmm_xxx_AA.bin*******/ | 
| 65 | strncat(strcpy(data[i].a,path3),file_init,Length-8); | 
| 66 | strcat(data[i].a,numer); | 
| 67 | strcat(data[i].a,".bin"); | 
| 68 | //form the 'nnnnn_mmm_xxx_AA.bin' | 
| 69 | for (j=0;j<20;j++) tmp[j]=data[i].a[strlen(data[i].a)-(20-j)]; | 
| 70 | /*********************Create names nnnnn_mmm_xxx_AA.bin********************/ | 
| 71 | for (j=0;j<20;j++) | 
| 72 | {data[i].a_short[j]=tmp[j]; data[i].a_short[20]='\0'; | 
| 73 | data[i].a[strlen(data[i].a)-(20-j)]=tmp[j]; | 
| 74 | } | 
| 75 |  | 
| 76 | data[i].fo_a=fopen(data[i].a,"wb"); | 
| 77 | if(data[i].fo_a==NULL) | 
| 78 | {cerr<<"can not open _%s.dat file",numer; exit(1);} | 
| 79 |  | 
| 80 | } | 
| 81 | fclose(f_temp); | 
| 82 | remove(tmp1); | 
| 83 | /**********************OPEN NECESSARY FILES************************/ | 
| 84 | /******************************************************************/ | 
| 85 | ifstream fo_pam; | 
| 86 | fo_pam.open(pam,ios::binary);      //open input file "xxx_mmm_nnnnn_cln2.pam" | 
| 87 | if(fo_pam.good()==0) {cerr<<"can not open pam file"; exit(1);} | 
| 88 |  | 
| 89 | ofstream fo_arina; | 
| 90 | strcpy(arina,"/home/arina/orientation/"); | 
| 91 | strcat(arina,data[28].a_short); //cout<<arina<<"\n"; | 
| 92 | fo_arina.open(arina,ios::binary); | 
| 93 | if(fo_arina.good()==0) {cerr<<"can not open arina file"; exit(1);} | 
| 94 |  | 
| 95 | fo_log=fopen(log, "rb");      //open input file "xxx_mmm_nnnnn_cln2.log" | 
| 96 | if(fo_log==NULL) {cerr<<"can not open log file"; exit(1);} | 
| 97 |  | 
| 98 | strcpy(fno2,path3); | 
| 99 | strncat(fno2,file_init,strlen(file_init)-8); | 
| 100 | strcat(fno2,"work2.dat"); | 
| 101 | fo_work2=fopen(fno2, "wb");   //open output help file "xxx_mmm_nnnnn_work2.dat" | 
| 102 | if(fo_work2==NULL) {cerr<<"can not open outputfile"; exit(1);} | 
| 103 |  | 
| 104 | strncat(strcpy(ql,path3),file_init,Length-8); | 
| 105 | strcat(ql,"ql.log"); | 
| 106 | fo_ql=fopen(ql, "wb");        //open output file "xxx_mmm_nnnnn_ql.log" | 
| 107 | if(fo_ql==NULL) {cerr<<"can not open ql file"; exit(1);} | 
| 108 |  | 
| 109 | /******************************************************************/ | 
| 110 | /******************************************************************/ | 
| 111 | while(feof(fo_log)==0) | 
| 112 | { | 
| 113 | //read PAMELA packet information from xxx_mmm_nnnnn__cln2.log | 
| 114 | fscanf(fo_log,"%1c",&type); | 
| 115 | if (feof(fo_log)!=0) break; | 
| 116 | fscanf(fo_log,"%4c",&adr1); | 
| 117 | if (feof(fo_log)!=0) break; | 
| 118 | fscanf(fo_log,"%4c",&adr2); | 
| 119 | if (feof(fo_log)!=0) break; | 
| 120 | fscanf(fo_log,"%1c",&error); | 
| 121 | if (feof(fo_log)!=0) break; | 
| 122 |  | 
| 123 | if ( ( (bitset<8>(error).test(7))||(error==0)|| | 
| 124 | ((bitset<8>(error).test(0))&&(!bitset<8>(error).test(1))&&(!bitset<8>(error).test(2))&& | 
| 125 | (!bitset<8>(error).test(3))&&(!bitset<8>(error).test(4))&&(!bitset<8>(error).test(5))&& | 
| 126 | (!bitset<8>(error).test(6))) )&& | 
| 127 | (type>0)&&(type<NumberPack+1) ) | 
| 128 | { | 
| 129 | /*Place file pointer in xxx_mmm_nnnnn_cln2.pam on packet begin*/ | 
| 130 | fo_pam.seekg(adr1-1,ios::beg); | 
| 131 | //read PAMELA packet from xxx_mmm_nnnnn_cln2.pam | 
| 132 | fo_pam.read(packet,adr2-adr1+1); | 
| 133 |  | 
| 134 | /*OBT of Pamela*/ | 
| 135 | time=(long int)(unsigned char)(packet[11])+ | 
| 136 | 256*(long int)(unsigned char)(packet[10])+ | 
| 137 | 256*256*(long int)(unsigned char)(packet[9])+ | 
| 138 | 256*256*256*(long int)(unsigned char)(packet[8]); | 
| 139 |  | 
| 140 | /* mikhailov     time goes to moscow time 08 12 2004*/ | 
| 141 |  | 
| 142 | moscowtime=time-(OBTtimesync-Timesync); | 
| 143 |  | 
| 144 | //   if(type == 11)  cout<<"time ="<<time<<" moscowtime ="<<moscowtime<<"\n"; | 
| 145 | /*temp is packet number*/ | 
| 146 | temp=(long int)(unsigned char)(packet[7])+ | 
| 147 | 256*(long int)(unsigned char)(packet[6])+ | 
| 148 | 256*256*(long int)(unsigned char)(packet[5]); | 
| 149 | //   if ((fmod((float)(temp),(float)(10000.))==0.)&&(temp!=0)) cout<<"sorting "<<temp<<"\n"; | 
| 150 | /*unpacking several packets....   mikhailov | 
| 151 | unpackingPamelaPacket(packet,type)*/ | 
| 152 |  | 
| 153 | // if (type == 10) RunHeaderReader(packet); | 
| 154 | if( type == 11){RunHeaderReader(packet);} | 
| 155 | /*******************Packet with type "type"***********************/ | 
| 156 | counter[type]+=1; | 
| 157 | //write PAMELA packet in mmm_nnnnn_xxx_AA.bin | 
| 158 | if (type!=4) fwrite(packet,adr2-adr1+1,1,data[type].fo_a); | 
| 159 | //write for ARINA | 
| 160 | if (type==28) fo_arina.write(packet,adr2-adr1+1); | 
| 161 | first[type]=last[type]+1; | 
| 162 | last[type]+=adr2-adr1+1; | 
| 163 | temp=first[type]; | 
| 164 | for (i=0;i<4;i++) Hex_first[i]=0; | 
| 165 | for (i=0;i<4;i++) | 
| 166 | {if (temp/(long int)(pow(256.,3-i))>=1) | 
| 167 | {Hex_first[3-i]=(int)(temp/(long int)(pow(256.,3-i))); | 
| 168 | temp=temp-(long int)pow(256.,3-i)* | 
| 169 | (int)(temp/(long int)(pow(256.,3-i)));}} | 
| 170 | temp=last[type]; | 
| 171 | for (i=0;i<4;i++) Hex_last[i]=0; | 
| 172 | for (i=0;i<4;i++) | 
| 173 | {if (temp/(long int)(pow(256.,3-i))>=1) | 
| 174 | {Hex_last[3-i]=(int)(temp/(long int)(pow(256.,3-i))); | 
| 175 | temp=temp-(long int)pow(256.,3-i)* | 
| 176 | (int)(temp/(long int)(pow(256.,3-i)));} | 
| 177 | } | 
| 178 | //write information about PAMELA packet in xxx_mmm_nnnnn_ql.log | 
| 179 | if (type!=4) | 
| 180 | { | 
| 181 | fprintf(fo_ql,"%1c",char(type)); | 
| 182 | for (i=0;i<4;i++) fprintf(fo_ql,"%1c",char(Hex_first[i])); | 
| 183 | //   for (i=0;i<4;i++) fprintf(fo_ql,"%1c",char(Hex_last[i])); | 
| 184 | fprintf(fo_ql,"%1c",char(error)); | 
| 185 | //  for (i=8;i<12;i++) fprintf(fo_ql,"%1c",packet[i]);     //!!!!temporary | 
| 186 |  | 
| 187 |  | 
| 188 | /* mikhailov     time goes to moscow time*  08 12 2004*/ | 
| 189 | for (i=0;i<4;i++) fprintf(fo_ql,"%1c",char(time>>((3-i)*8)&0XFF)); | 
| 190 | for (i=0;i<4;i++) fprintf(fo_ql,"%1c",char(moscowtime>>((3-i)*8)&0XFF)); | 
| 191 | for (i=0;i<3;i++) fprintf(fo_ql,"%1c",packet[i+5]); | 
| 192 | for (i=0;i<15;i++) fprintf(fo_ql,"%1c",char(0XFF));  //added to fill 32 bytes | 
| 193 | } | 
| 194 | } | 
| 195 | else | 
| 196 | { | 
| 197 | /*******************Packets with errors***************************/ | 
| 198 | char *er_pack=new char [adr2-adr1+1]; | 
| 199 | /*Place file pointer in mmm_counter_nnnnn_cln2.pam on error packet begin*/ | 
| 200 | fo_pam.seekg(adr1-1,ios::beg); | 
| 201 | //read PAMELA error packet from mmm_counter_nnnnn_cln2.pam | 
| 202 | fo_pam.read(er_pack,adr2-adr1+1); | 
| 203 | time=(long int)(unsigned char)(er_pack[11])+ | 
| 204 | 256*(long int)(unsigned char)(er_pack[10])+ | 
| 205 | 256*256*(long int)(unsigned char)(er_pack[9])+ | 
| 206 | 256*256*256*(long int)(unsigned char)(er_pack[8]); | 
| 207 | counter[0]+=1; | 
| 208 | //write PAMELA packet in mmm_nnnnn_conter_00.bin | 
| 209 | fwrite(er_pack,adr2-adr1+1,1,data[0].fo_a); | 
| 210 | first[0]=last[0]+1; | 
| 211 | last[0]+=adr2-adr1+1; | 
| 212 | temp=first[0]; | 
| 213 | for (i=0;i<4;i++) Hex_first[i]=0; | 
| 214 | for (i=0;i<4;i++) | 
| 215 | {if (temp/(long int)(pow(256.,3-i))>=1) | 
| 216 | {Hex_first[3-i]=(int)(temp/(long int)(pow(256.,3-i))); | 
| 217 | temp=temp-(long int)pow(256.,3-i)* | 
| 218 | (int)(temp/(long int)(pow(256.,3-i)));} | 
| 219 | } | 
| 220 | temp=last[0]; | 
| 221 | for (i=0;i<4;i++) Hex_last[i]=0; | 
| 222 | for (i=0;i<4;i++) | 
| 223 | {if (temp/(long int)(pow(256.,3-i))>=1) | 
| 224 | {Hex_last[3-i]=(int)(temp/(long int)(pow(256.,3-i))); | 
| 225 | temp=temp-(long int)pow(256.,3-i)* | 
| 226 | (int)(temp/(long int)(pow(256.,3-i)));} | 
| 227 | } | 
| 228 | if (type!=4) | 
| 229 | {//write information about PAMELA packet in xxx_mmm_nnnnn_ql.log | 
| 230 | fprintf(fo_ql,"%1c",(char)(type)); | 
| 231 | for (i=0;i<4;i++) fprintf(fo_ql,"%1c",char(Hex_first[i])); | 
| 232 | //  for (i=0;i<4;i++) fprintf(fo_ql,"%1c",char(Hex_last[i]));  //temporary | 
| 233 | fprintf(fo_ql,"%1c",char(error)); | 
| 234 | //   for (i=8;i<12;i++) fprintf(fo_ql,"%1c",er_pack[i]); | 
| 235 | /* mikhailov     time goes to moscow time 08 12 2004*/ | 
| 236 | for (i=0;i<4;i++) fprintf(fo_ql,"%1c",char(time>>((3-i)*8)&0XFF)); | 
| 237 | for (i=0;i<4;i++) fprintf(fo_ql,"%1c",char(moscowtime>>((3-i)*8)&0XFF)); | 
| 238 | for (i=0;i<3;i++) fprintf(fo_ql,"%1c",er_pack[i+5]); | 
| 239 | for (i=0;i<15;i++) fprintf(fo_ql,"%1c",char(0XFF));  //added to fill 32 bytes | 
| 240 |  | 
| 241 | delete [] er_pack; | 
| 242 | } | 
| 243 | } | 
| 244 | } | 
| 245 |  | 
| 246 | for (i=0;i<NumberPack+1;i++) | 
| 247 | fprintf(fo_work2,"Number of packets in %s file = %i\n",data[i].a_short,counter[i]); | 
| 248 |  | 
| 249 | fo_pam.close(); | 
| 250 | fo_arina.close(); | 
| 251 | fclose(fo_log); | 
| 252 | fclose(fo_work2); | 
| 253 |  | 
| 254 | for(i=0;i<NumberPack+1;i++) fclose(data[i].fo_a); | 
| 255 | fclose(fo_ql); | 
| 256 | delete [] packet; | 
| 257 | } |