-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtipsy_xdr_2_tipsy_xdr.c
154 lines (147 loc) · 4.27 KB
/
tipsy_xdr_2_tipsy_xdr.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
/*
** tipsy_xdr_2_tipsy_xdr.c
**
** Written by Marcel Zemp
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>
#include <assert.h>
#include <iof.h>
void usage(void);
int main(int argc, char **argv) {
int i;
int positionprecision, verboselevel;
int writegas, writedark, writestar;
TIPSY_HEADER thin, thout;
TIPSY_GAS_PARTICLE tgp;
TIPSY_DARK_PARTICLE tdp;
TIPSY_STAR_PARTICLE tsp;
TIPSY_GAS_PARTICLE_DPP tgpdpp;
TIPSY_DARK_PARTICLE_DPP tdpdpp;
TIPSY_STAR_PARTICLE_DPP tspdpp;
XDR xdrsin, xdrsout;
positionprecision = 0;
verboselevel = 0;
writegas = 1;
writedark = 1;
writestar = 1;
i = 1;
while (i < argc) {
if ((strcmp(argv[i],"-h") == 0) || (strcmp(argv[i],"-help") == 0)) {
usage();
}
if (strcmp(argv[i],"-version") == 0) {
fprintf(stderr,"%s (%s)\n",NAME,VERSION);
exit(1);
}
if (strcmp(argv[i],"-spp") == 0) {
positionprecision = 0;
i++;
}
else if (strcmp(argv[i],"-dpp") == 0) {
positionprecision = 1;
i++;
}
else if (strcmp(argv[i],"-writegas") == 0) {
i++;
if (i >= argc) usage();
writegas = atoi(argv[i]);
i++;
}
else if (strcmp(argv[i],"-writedark") == 0) {
i++;
if (i >= argc) usage();
writedark = atoi(argv[i]);
i++;
}
else if (strcmp(argv[i],"-writestar") == 0) {
i++;
if (i >= argc) usage();
writestar = atoi(argv[i]);
i++;
}
else if (strcmp(argv[i],"-verbose") == 0) {
verboselevel = 1;
i++;
}
else {
usage();
}
}
xdrstdio_create(&xdrsin,stdin,XDR_DECODE);
read_tipsy_xdr_header(&xdrsin,&thin);
/*
** Recalculate header
*/
thout = thin;
if (writegas == 0) thout.ngas = 0;
if (writedark == 0) thout.ndark = 0;
if (writestar == 0) thout.nstar = 0;
thout.ntotal = thout.ngas+thout.ndark+thout.nstar;
/*
** Write new file
*/
xdrstdio_create(&xdrsout,stdout,XDR_ENCODE);
write_tipsy_xdr_header(&xdrsout,&thout);
if (positionprecision == 0) {
for (i = 0; i < thin.ngas; i++) {
read_tipsy_xdr_gas(&xdrsin,&tgp);
if (writegas == 1) write_tipsy_xdr_gas(&xdrsout,&tgp);
}
for (i = 0; i < thin.ndark; i++) {
read_tipsy_xdr_dark(&xdrsin,&tdp);
if (writedark == 1) write_tipsy_xdr_dark(&xdrsout,&tdp);
}
for (i = 0; i < thin.nstar; i++) {
read_tipsy_xdr_star(&xdrsin,&tsp);
if (writestar == 1) write_tipsy_xdr_star(&xdrsout,&tsp);
}
}
if (positionprecision == 1) {
for (i = 0; i < thin.ngas; i++) {
read_tipsy_xdr_gas_dpp(&xdrsin,&tgpdpp);
if (writegas == 1) write_tipsy_xdr_gas_dpp(&xdrsout,&tgpdpp);
}
for (i = 0; i < thin.ndark; i++) {
read_tipsy_xdr_dark_dpp(&xdrsin,&tdpdpp);
if (writedark == 1) write_tipsy_xdr_dark_dpp(&xdrsout,&tdpdpp);
}
for (i = 0; i < thin.nstar; i++) {
read_tipsy_xdr_star_dpp(&xdrsin,&tspdpp);
if (writestar == 1) write_tipsy_xdr_star_dpp(&xdrsout,&tspdpp);
}
}
xdr_destroy(&xdrsin);
xdr_destroy(&xdrsout);
if (verboselevel > 0) {
fprintf(stderr,"Time: %g Ntotal: %u Ngas: %u Ndark: %u Nstar: %u\n",
thout.time,thout.ntotal,thout.ngas,thout.ndark,thout.nstar);
}
exit(0);
}
void usage(void) {
fprintf(stderr,"\n");
fprintf(stderr,"%s (%s)\n",NAME,VERSION);
fprintf(stderr,"\n");
fprintf(stderr,"Program converts tipsy XDR format to tipsy XDR format.\n");
fprintf(stderr,"\n");
fprintf(stderr,"Please specify the following parameters:\n");
fprintf(stderr,"\n");
fprintf(stderr,"-spp : set this flag if input and output files have single precision positions (default)\n");
fprintf(stderr,"-dpp : set this flag if input and output files have double precision positions\n");
fprintf(stderr,"-writegas <value> : 0 = don't write out gas / 1 = write out gas (default: 1)\n");
fprintf(stderr,"-writedark <value> : 0 = don't write out dark matter / 1 = write out dark matter (default: 1)\n");
fprintf(stderr,"-writestar <value> : 0 = don't write out stars / 1 = write out stars (default: 1)\n");
fprintf(stderr,"-verbose : verbose\n");
fprintf(stderr,"< <name> : input file in tipsy XDR format\n");
fprintf(stderr,"> <name> : output file in tipsy XDR format\n");
fprintf(stderr,"\n");
fprintf(stderr,"Other options:\n");
fprintf(stderr,"\n");
fprintf(stderr,"-h or -help : display this help and exit\n");
fprintf(stderr,"-version : display version information and exit\n");
fprintf(stderr,"\n");
exit(1);
}