-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patharrayascii2bin.c
334 lines (308 loc) · 9.13 KB
/
arrayascii2bin.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
arrayascii2bin
Utility for examining contents of tipsy binary files
(c) 2023 James Wadsley
Usage: arrayascii2bin filename (see below under usage())
Make: gcc arrayascii2bin.c -lm -o arrayascii2bin
Note: May need tirpc to get xdr functions:
Make2: gcc -I/usr/include/tirpc arrayascii2bin.c -ltirpc -lm -o arrayascii2bin
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <rpc/xdr.h>
#include <rpc/rpc.h>
#include <assert.h>
#include <unistd.h>
#define MAXDIM 3
typedef float Real;
#define Real float
int nSph=-1,nDark=-1,nStar=-1,bReadAux=0;
// ReadArray( file, filew, bDark,bGas,bStar, bReplace, iWriteFormat, bInt, bCreate, iReplace, fReplace);
int ReadArray(char *file, char *filew, int bDark,int bGas,int bStar,int bReplace,int iWriteFormat,int bInt, int bCreate,int iReplace, float fReplace)
{
int i,iReadFormat=0;
long offset;
XDR xdrread,xdrwrite;
FILE *fpread,*fpwrite;
struct stat fileinfo;
int fildes,status;
long long size;
int npart;
fildes = open(file, O_RDONLY);
if (fildes < 0) {
fprintf(stderr,"Can't open file: %s\n",file);
exit(1);
}
status = fstat(fildes, &fileinfo);
size = fileinfo.st_size;
close(fildes);
fpread = fopen( file, "r" );
if (fpread == NULL) {
fprintf(stderr,"Can't open file: %s\n",file);
exit(1);
}
fread(&npart,4,1,fpread);
#ifdef VERBOSE
fprintf(stderr,"TEST: %d Particles in Native Binary array file %s\n",npart,file);
#endif
if (npart == (size-4)/4) {
fprintf(stderr,"%d Particles in Native Binary array file %s\n",npart,file);
iReadFormat=0;
}
else {
rewind(fpread);
xdrstdio_create(&xdrread, fpread, XDR_DECODE);
xdr_int( &xdrread, &npart );
#ifdef VERBOSE
fprintf(stderr,"TEST: %d Particles in STD Binary array file %s\n",npart,file);
#endif
if (npart == (size-4)/4) {
fprintf(stderr,"%d Particles in STD Binary array file %s\n",npart,file);
iReadFormat=1;
}
else {
const int nChar=20;
if (size<nChar) {
fprintf(stderr,"File is very short -- truncated? Aborting.\n");
exit(2);
}
rewind(fpread);
unsigned char readtest[nChar+1];
fscanf(fpread, "%20c", readtest);
int bNumber=0,nOK=0;
// Check for whitespace or numbers in 1st chars
for (i=0;i<nChar;i++) {
switch(readtest[i]) {
case 48: // 0
case 49:
case 50:
case 51:
case 52:
case 53:
case 54:
case 55:
case 56:
case 57: // 9
bNumber=1;
case 9: // tab
case 10: // linefeed
case 13: // return
case 32: // space
case 43: // +
case 44: // ,
case 45: // -
case 46: // .
case 101: // e
case 69: // E
nOK++;
}
#ifdef VERBOSE
printf("%i %i ]%c[ %d %d\n",i,readtest[i],readtest[i],nOK,bNumber);
#endif
}
#ifdef VERBOSE
fprintf(stderr,"TEST: nOK %d bNumber %d\n",nOK,bNumber);
#endif
if (nOK==nChar && bNumber) { // ascii text numbers
rewind(fpread);
fscanf(fpread, "%d", &npart);
fprintf(stderr,"%d Particles in ascii text array file %s\n",npart,file);
iReadFormat=-1;
}
else {
fprintf(stderr,"Unknown format. Aborting\n");
exit(1);
}
}
}
fpwrite = fopen( filew, "w" );
if (fpwrite == NULL) {
fprintf(stderr,"Can't open file for writing: %s\n",filew);
exit(1);
}
// Write new headers
switch(iWriteFormat) {
case 0:
fwrite(&npart,4,1,fpwrite);
break;
case 1:
xdrstdio_create(&xdrwrite, fpwrite, XDR_ENCODE);
xdr_int( &xdrwrite, &npart );
break;
case -1:
fprintf(fpwrite,"%d\n",npart);
break;
}
int DataSize=sizeof(float);
int iRead=0,nChunk0=1024,nChunk;
nChunk=nChunk0;
union { float f; int i; } Data[nChunk0];
if (bReplace) {
iWriteFormat=-2;
if (bInt) {
for (i=0;i<nChunk;i++) {
Data[i].i = iReplace;
}
}
else {
for (i=0;i<nChunk;i++) {
Data[i].f = fReplace;
}
}
}
for (;;) {
if (iRead+nChunk0 > npart) {
nChunk=npart-iRead;
}
switch(iReadFormat) {
case 0:
fread(&Data[0],DataSize*nChunk,1,fpread);
break;
case 1:
if (bInt) {
for (i=0;i<nChunk;i++) {
xdr_int(&xdrread,&(Data[i].i));
}
}
else {
for (i=0;i<nChunk;i++) {
xdr_float(&xdrread,&(Data[i].f));
}
}
break;
case -1:
if (bInt) {
for (i=0;i<nChunk;i++) {
fscanf(fpread,"%d",&(Data[i].i));
}
}
else {
for (i=0;i<nChunk;i++) {
fscanf(fpread,"%f",&(Data[i].f));
}
}
case -2:
break;
default:
exit(1);
}
switch(iWriteFormat) {
case 0:
fwrite(&Data[0],DataSize*nChunk,1,fpwrite);
break;
case 1:
if (bInt) {
for (i=0;i<nChunk;i++) {
xdr_int(&xdrwrite,&(Data[i].i));
}
}
else {
for (i=0;i<nChunk;i++) {
xdr_float(&xdrwrite,&(Data[i].f));
}
}
break;
case -1:
if (bInt) {
for (i=0;i<nChunk;i++) {
fprintf(fpwrite,"%d",(Data[i].i));
}
}
else {
for (i=0;i<nChunk;i++) {
fprintf(fpwrite,"%f",(Data[i].f));
}
}
break;
default:
exit(1);
}
iRead+=nChunk;
if (iRead >= npart) break;
}
fclose(fpread);
return npart;
}
void usage() {
fprintf(stderr,"Usage: arrayascii2binary [-i] [-r replaceval] [-std] [-a] [-w filewrite] fileread\n"
"This utility converts between ascii, native and xdr format for auxilliary files.\n"
"fileread input format auto detected except for float vs int\n"
"-i read and write int\n"
"-r replace contents with replaceval (will not read past header)\n"
"-std xdr/standard format\n"
"-a ascii/text format\n"
"-w write to filewrite (default array.out)\n");
exit(1);
}
int main(int argc, char **argv) {
int i,bGas=0,bDark=0,bStar=0;
char defaultw[]="array.out", *filew=defaultw;
char *pch, *file=NULL;
float fReplace;
int iReplace;
int bReplace=0,bCreate=0,iWriteFormat=0,bInt=0;
i=1;
while(i < argc) {
pch = argv[i];
if (!strcmp(argv[i],"-r")) {
++i;
bReplace=1;
if (i >= argc) usage();
if (bInt)
iReplace = atoi(argv[i]);
else
fReplace = atof(argv[i]);
++i;
}
if (!strcmp(argv[i],"-w")) {
++i;
if (i >= argc) usage();
filew = argv[i];
++i;
}
else if (!strcmp(argv[i],"-std")) {
iWriteFormat=1; // xdr
++i;
}
else if (!strcmp(argv[i],"-a")) {
iWriteFormat=-1; // ascii
++i;
}
else if (!strcmp(argv[i],"-i")) {
bInt=1;
++i;
if (bReplace) {
fprintf(stderr,"set int mode before setting replace value\n");
exit(1);
}
}
else if (*pch == '-') {
pch++;
while (*pch) {
if (*pch == 'g') bGas = 1;
else if (*pch == 'd') bDark = 1;
else if (*pch == 's') bStar = 1;
pch++;
}
i++;
}
else if (file == NULL) {
file = argv[i];
i++;
}
else {
usage();
}
}
if (file == NULL) usage();
if (!bGas && !bStar && !bDark) {
bGas = 1; bStar = 1; bDark = 1;
}
ReadArray( file, filew, bDark,bGas,bStar, bReplace, iWriteFormat, bInt, bCreate, iReplace, fReplace);
return 0;
}