-
[physik/nlsop.git] / parse_trim_collision.c
1 /*
2  * parse srim 2003.26 collision data 
3  *
4  * author: hackbard@hackdaworld.dyndns.org
5  *
6  * this may just work with srim 2003.26 and 180keV c+ -> si,
7  * not quite sure though! :)
8  *
9  */
10
11 #define _GNU_SOURCE
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <unistd.h>
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #include <fcntl.h>
19
20 #define MAXION 5000
21 #define MAXZ 233
22
23 typedef struct s_z {
24         int cell_hits_from_ions;
25         int total_hits;
26         double energy;
27 } t_z;
28
29 int main(int argc,char **argv) {
30
31         int fd,ion,i,j;
32         int z,skipped;
33         double en;
34         double max_e;
35         int max_chfi,max_th;
36         t_z Z[MAXZ];
37         unsigned char hit[MAXZ];
38         char buf[256],*p,value[6],value2[10];
39
40         i=0;
41
42         if(argc!=2) return 0;
43         
44         printf("opening file %s ...\n",argv[1]);
45         if((fd=open(argv[1],O_RDONLY))<0) {
46                 printf("unable to open file %s\n",argv[1]);
47                 return 0;
48         }
49
50         ion=1;
51         skipped=0;
52         for(i=0;i<MAXZ;i++) {
53                 Z[i].cell_hits_from_ions=0;
54                 Z[i].total_hits=0;
55                 Z[i].energy=0;
56         }
57         memset(hit,0,MAXZ);
58
59         while(1) {
60
61                 /* read line */
62                 i=0;
63                 memset(buf,0,256);
64                 while(1) {
65                         j=read(fd,&buf[i],1);
66                         if(j<=0) {
67                                 close(fd);
68                                 /* norm */
69                                 max_e=Z[0].energy;
70                                 max_th=Z[0].total_hits;
71                                 max_chfi=Z[0].cell_hits_from_ions;
72                                 for(i=0;i<MAXZ;i++) {
73                                         if(Z[i].energy>max_e) max_e=Z[i].energy;
74                                         if(Z[i].total_hits>max_th) max_th=Z[i].total_hits;
75                                         if(Z[i].cell_hits_from_ions>max_chfi) max_chfi=Z[i].cell_hits_from_ions;
76                                 }
77                                 for(i=0;i<MAXZ;i++) 
78                                         printf("%d %f %f %f\n",i*3,1.0*Z[i].total_hits/max_th,
79                                                                 Z[i].energy/max_e,
80                                                                 1.0*Z[i].cell_hits_from_ions/max_chfi);
81                                 printf("skipped = %d\n",skipped);
82                                 return 1;
83                         }
84                         if(buf[i]=='\n') break;
85                         i++;
86                 }
87
88                 /* parse line */
89                 if((buf[0]=='³')&&(buf[1]!='=')) {
90                         p=strtok(buf,"³");
91                         value[0]=p[0];
92                         value[1]=p[1];
93                         value[2]=p[2];
94                         value[3]=p[3];
95                         value[4]=p[4];
96                         value[5]='\0';
97                         p=strtok(NULL,"³");
98                         value2[0]=p[0];
99                         value2[1]=p[1];
100                         value2[2]='.';
101                         value2[3]=p[3];
102                         value2[4]=p[4];
103                         value2[5]=p[5];
104                         value2[6]=p[6];
105                         value2[7]=p[7];
106                         value2[8]=p[8];
107                         value2[9]='\0';
108                         en=atof(value2);
109                         p=strtok(NULL,"³");
110                         value2[0]=p[0];
111                         value2[1]=p[1];
112                         value2[2]=p[2];
113                         value2[3]=p[3];
114                         value2[4]=p[4];
115                         value2[5]='.';
116                         value2[6]=p[6];
117                         value2[7]=p[7];
118                         value2[8]=p[8];
119                         value2[9]=p[9];
120                         value2[10]='\0';
121                         z=(int)(atof(value2)/30.);
122                         if(z>232) skipped+=1;
123                         else {
124                                 Z[z].energy+=en;
125                                 Z[z].total_hits+=1;
126                                 hit[z]=1;
127                         }
128                         if(ion!=atoi(value)) {
129                                 /* new ion */
130                                 for(i=0;i<MAXZ;i++) Z[i].cell_hits_from_ions+=hit[i];
131                                 memset(hit,0,MAXZ);
132                                 ion=atoi(value);
133                         }
134                         // printf("%d %d %f %d\n",z*3,Z[z].total_hits,
135                         //      Z[z].energy,Z[z].cell_hits_from_ions);
136                         // return 0;
137                 }
138
139
140         }
141         return 0;
142 }