-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmulticube2raw.c
166 lines (133 loc) · 3.68 KB
/
multicube2raw.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
#include<math.h>
void usage() {
printf("This generates volumetric raw files for blender from\n");
printf("Gaussian Cube Files!\n");
printf("cube2raw [file1.cube] [file2.cube] ... [filen.cube] [outfile.raw]\n");
}
int main(int argc, char** argv) {
double *bigtable;
char *smalltable;
int sizex, sizey, sizez, sizexBYsizey;
int sizex_old, sizey_old, sizez_old;
int index_x,index_y, index_z, index;
int num_of_cubes = argc-2;
char buffer[200];
int num_of_atoms;
int i,j;
double new;
// negative infinity
double max= -1./0.;
// positive infinity
double min= -1.*max;
double rescalefactor;
int shift;
FILE* cubefile;
int rawfile;
// process input
if(argc<3) {
printf("This program requires at least 3 arguments\n");
usage();
return 1;
}
rawfile = open(argv[argc-1],O_CREAT|O_WRONLY,000644);
printf("Number of files to be processed: %i \n", num_of_cubes);
if(rawfile == -1) {
printf("Error in opening raw file!");
usage();
return 1;
}
for(i=0;i<num_of_cubes;i++) {
printf(" INPUT: %s\n", argv[i+1]);
}
printf("OUTPUT: %s\n", argv[num_of_cubes+1]);
for(i=0;i<num_of_cubes;i++) {
cubefile = fopen(argv[i+1],"r");
if(cubefile == NULL) {
printf("Error in opening cube file!");
usage();
return 1;
}
// process cube files header
//skip the first two lines
for(j=0;j<2;j++){
fgets(buffer, 200, cubefile);
}
fgets(buffer,200, cubefile);
sscanf(buffer, "%i", &num_of_atoms);
fgets(buffer, 200, cubefile);
sscanf(buffer, "%i", &sizex);
fgets(buffer, 200, cubefile);
sscanf(buffer, "%i", &sizey);
fgets(buffer, 200, cubefile);
sscanf(buffer, "%i", &sizez);
// do some inter cube file consistency checks
if(i>0) {
if (sizex != sizex_old || sizey != sizey_old || sizez != sizez_old) {
printf("Cube file dimenstions are inconsistent @ cube %i \n",i);
usage();
return 1;
}
}
for(j=0;j<num_of_atoms;j++){
fgets(buffer, 200, cubefile);
}
printf("%lf %lf \n",max,min);
// allocate some conversion memory in the first run;
if(i==0) {
smalltable = (char*)malloc(sizex*sizey*sizez);
}
sizexBYsizey = sizex*sizey;
// get the minimum and maximum value in order to calculate some
// the rescalefactor
for(index_x=0;index_x<sizex;index_x++) {
for(index_y=0;index_y<sizey;index_y++) {
for(index_z=0;index_z<sizez;index_z++) {
fscanf(cubefile,"%lf",&new);
new = atan(new);
if(new > max) {
max = new;
}
if(new < min) {
min = new;
}
}
}
}
sizex_old = sizex;
sizey_old = sizey;
sizez_old = sizez;
fclose(cubefile);
}
// rescalefactor from minimum and maximum value in all cubes
rescalefactor = 254/(max-min);
printf("%lf %lf \n",max,min);
// actual conversion taking place
for (i=0;i<num_of_cubes;i++) {
cubefile = fopen(argv[i+1],"r");
for(j=0;j<2;j++){
fgets(buffer, 200, cubefile);
}
fgets(buffer,200, cubefile);
sscanf(buffer, "%i", &num_of_atoms);
for(j=0;j<num_of_atoms+3;j++){
fgets(buffer, 200, cubefile);
}
for(index_x=0;index_x<sizex;index_x++) {
for(index_y=0;index_y<sizey;index_y++) {
for(index_z=0;index_z<sizez;index_z++) {
fscanf(cubefile,"%lf",&new);
new = atan(new);
index = index_x+index_y*sizex+index_z*sizexBYsizey;
smalltable[index] = (char)rint((new-min)*rescalefactor);
}
}
}
write(rawfile,smalltable,sizex*sizey*sizez);
fclose(cubefile);
printf("Processed File: %i \n",i);
}
close(rawfile);
}