-
Notifications
You must be signed in to change notification settings - Fork 3
/
snapshot.c
116 lines (110 loc) · 3.11 KB
/
snapshot.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
#include <stdio.h>
#include "tipsydefs.h"
int
main(argc,argv)
char *argv[];
int argc;
{
struct file {
char name[50];
FILE *ptr ;
int pipe;
} ;
struct file binaryfile ;
double time ;
static double currtime = 0.0;
static long currpos = 0L ;
static long lastpos = 0L ;
struct gas_particle *gas_particles;
struct dark_particle *dark_particles;
struct star_particle *star_particles;
sscanf(argv[1],"%s",binaryfile.name) ;
sscanf(argv[2],"%lf",&time) ;
if( (binaryfile.ptr = fopen(binaryfile.name,"r"))== NULL) {
fprintf(stderr,"<sorry, binary file %s does not exist>\n",
binaryfile.name) ;
}
else{
if ((float)currtime > (float)time){
fseek(binaryfile.ptr,0L,0);
currtime=0.0;
currpos=0;
}
forever {
if(fread((char *)&header,sizeof(header),1,binaryfile.ptr)
!= 1) {
fprintf(stderr,"<sorry time too large, using %f>\n",
(float)currtime) ;
break ;
}
currtime = header.time ;
currpos = ftell(binaryfile.ptr) - sizeof(header);
if ( (float)header.time >= (float)time )
break ;
fseek(binaryfile.ptr,
sizeof(gas_particles[0])*header.nsph +
sizeof(dark_particles[0])*header.ndark +
sizeof(star_particles[0])*header.nstar,
1) ;
}
fseek(binaryfile.ptr,currpos,0) ;
lastpos = currpos ;
fread((char *)&header,sizeof(header),1,binaryfile.ptr) ;
if(gas_particles != NULL) free(gas_particles);
if(header.nsph != 0) {
gas_particles = (struct gas_particle *)
malloc(header.nsph*sizeof(*gas_particles));
if(gas_particles == NULL) {
fprintf(stderr,
"<sorry, no memory for gas particles>\n") ;
return -1;
}
}
else
gas_particles = NULL;
if(dark_particles != NULL) free(dark_particles);
if(header.ndark != 0) {
dark_particles = (struct dark_particle *)
malloc(header.ndark*sizeof(*dark_particles));
if(dark_particles == NULL) {
fprintf(stderr,
"<sorry, no memory for dark particles>\n") ;
return -1;
}
}
else
dark_particles = NULL;
if(star_particles != NULL) free(star_particles);
if(header.nstar != 0) {
star_particles = (struct star_particle *)
malloc(header.nstar*sizeof(*star_particles));
if(star_particles == NULL) {
fprintf(stderr,
"<sorry, no memory for star particles>\n") ;
return -1;
}
}
else
star_particles = NULL;
fread((char *)gas_particles,sizeof(struct gas_particle),
header.nsph,binaryfile.ptr) ;
fread((char *)dark_particles,sizeof(struct dark_particle),
header.ndark,binaryfile.ptr) ;
fread((char *)star_particles,sizeof(struct star_particle),
header.nstar,binaryfile.ptr) ;
currpos = lastpos ;
fseek(binaryfile.ptr,currpos,0) ;
currtime = header.time ;
if ((float)time != (float)currtime){
fprintf(stderr,"<used time %f, hope you don't mind>\n",
(float)currtime);
}
fwrite((char *)&header,sizeof(header),1,stdout) ;
fwrite((char *)gas_particles,sizeof(struct gas_particle),header.nsph,stdout) ;
fwrite((char *)dark_particles,sizeof(struct dark_particle),
header.ndark,stdout) ;
fwrite((char *)star_particles,sizeof(struct star_particle),
header.nstar,stdout) ;
}
return 0;
}