-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscaleas.c
145 lines (138 loc) · 3.46 KB
/
scaleas.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
/*
** scaleas.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 verboselevel;
int shiftint, factorint;
int integerindex, floatindex, doubleindex, index;
double shift, factor;
ARRAY_HEADER ah;
ARRAY_PARTICLE ap;
XDR xdrsin, xdrsout;
verboselevel = 0;
index = -1;
integerindex = 0;
floatindex = 0;
doubleindex = 0;
shiftint = 0;
factorint = 1;
shift = 0;
factor = 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],"-shift") == 0) {
i++;
if (i >= argc) usage();
shift = atof(argv[i]);
i++;
}
else if (strcmp(argv[i],"-factor") == 0) {
i++;
if (i >= argc) usage();
factor = atof(argv[i]);
i++;
}
else if (strcmp(argv[i],"-index") == 0) {
i++;
if (i >= argc) usage();
if (strcmp(argv[i],"i") == 0) {
integerindex = 1;
}
else if (strcmp(argv[i],"f") == 0) {
floatindex = 1;
}
else if (strcmp(argv[i],"d") == 0) {
doubleindex = 1;
}
else {
usage();
}
i++;
if (i >= argc) usage();
index = atoi(argv[i])-1;
i++;
}
else if (strcmp(argv[i],"-verbose") == 0) {
verboselevel = 1;
i++;
}
else {
usage();
}
}
assert(index != -1);
if (integerindex == 1) {
shiftint = (int) shift;
factorint = (int) factor;
}
xdrstdio_create(&xdrsin,stdin,XDR_DECODE);
xdrstdio_create(&xdrsout,stdout,XDR_ENCODE);
read_array_xdr_header(&xdrsin,&ah);
allocate_array_particle(&ah,&ap);
write_array_xdr_header(&xdrsout,&ah);
for (i = 0; i < ah.N[0]; i++) {
read_array_xdr_particle(&xdrsin,&ah,&ap);
if (integerindex == 1) {
ap.ia[index] = factorint*(ap.ia[index]+shiftint);
}
else if (floatindex == 1) {
ap.fa[index] = factor*(ap.fa[index]+shift);
}
else if (doubleindex == 1) {
ap.da[index] = factor*(ap.da[index]+shift);
}
write_array_xdr_particle(&xdrsout,&ah,&ap);
}
xdr_destroy(&xdrsin);
xdr_destroy(&xdrsout);
if (verboselevel > 0) {
fprintf(stderr,"Ntotal: %d Ni: %d Nf: %d Nd: %d\n",
ah.N[0],ah.N[1],ah.N[2],ah.N[3]);
if (integerindex == 1) {
fprintf(stderr,"shift: %d factor: %d\n",shiftint,factorint);
}
else {
fprintf(stderr,"shift: %g factor: %g\n",shift,factor);
}
}
exit(0);
}
void usage(void) {
fprintf(stderr,"\n");
fprintf(stderr,"%s (%s)\n",NAME,VERSION);
fprintf(stderr,"\n");
fprintf(stderr,"Program performes the following scaling on the array values v_old:\n");
fprintf(stderr,"v_new = factor * (v_old + shift)\n");
fprintf(stderr,"\n");
fprintf(stderr,"Please specify the following parameters:\n");
fprintf(stderr,"\n");
fprintf(stderr,"-shift <value> : value for shift (default: 0)\n");
fprintf(stderr,"-factor <value> : value for factor (default: 1)\n");
fprintf(stderr,"-index <type> <index> : <type>: i, f or d, index of array field\n");
fprintf(stderr,"< <name> : input file in standard binary format\n");
fprintf(stderr,"> <name> : output file in standard binary 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);
}