forked from scottransom/presto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdownsample.c
141 lines (122 loc) · 4.13 KB
/
downsample.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
#include "presto.h"
#include "downsample_cmd.h"
#ifdef USEDMALLOC
#include "dmalloc.h"
#endif
int main(int argc, char *argv[])
{
FILE *infile, *outfile;
int ii, jj, bufflen = 10000, numread;
long long N = 0;
float *inbuffer = NULL, *outbuffer = NULL;
short useshorts = 0, *sinbuffer = NULL, *soutbuffer = NULL;
char *rootfilenm, *outname;
infodata idata;
Cmdline *cmd;
/* Call usage() if we have no command line arguments */
if (argc == 1) {
Program = argv[0];
printf("\n");
usage();
exit(1);
}
/* Parse the command line using the excellent program Clig */
cmd = parseCmdline(argc, argv);
#ifdef DEBUG
showOptionValues();
#endif
printf("\n\n");
printf(" Time Series Downsampling Routine\n");
printf(" Sept, 2002\n\n");
{
int hassuffix = 0;
char *suffix;
hassuffix = split_root_suffix(cmd->argv[0], &rootfilenm, &suffix);
if (hassuffix) {
if (strcmp(suffix, "sdat") == 0)
useshorts = 1;
if (strcmp(suffix, "dat") != 0 && strcmp(suffix, "sdat") != 0) {
printf
("\nInput file ('%s') must be a time series ('.dat' or '.sdat')!\n\n",
cmd->argv[0]);
free(suffix);
exit(0);
}
free(suffix);
} else {
printf
("\nInput file ('%s') must be a time series ('.dat' or '.sdat')!\n\n",
cmd->argv[0]);
exit(0);
}
if (cmd->outfileP) {
outname = cmd->outfile;
} else {
outname = (char *) calloc(strlen(rootfilenm) + 11, sizeof(char));
if (useshorts)
sprintf(outname, "%s_D%d.sdat", rootfilenm, cmd->factor);
else
sprintf(outname, "%s_D%d.dat", rootfilenm, cmd->factor);
}
}
/* Read the info file */
readinf(&idata, rootfilenm);
if (strlen(remove_whitespace(idata.object)) > 0) {
printf("Downsampling %s data from '%s'.\n\n",
remove_whitespace(idata.object), cmd->argv[0]);
} else {
printf("Downsampling data from '%s'.\n\n", cmd->argv[0]);
}
/* Open files and create arrays */
infile = chkfopen(argv[1], "rb");
outfile = chkfopen(outname, "wb");
/* Read and downsample */
if (useshorts) {
sinbuffer = gen_svect(bufflen * cmd->factor);
soutbuffer = gen_svect(bufflen);
while ((numread =
chkfread(sinbuffer, sizeof(short), bufflen * cmd->factor, infile))) {
for (ii = 0; ii < numread / cmd->factor; ii++) {
soutbuffer[ii] = 0;
for (jj = 0; jj < cmd->factor; jj++)
soutbuffer[ii] += sinbuffer[cmd->factor * ii + jj];
}
chkfwrite(soutbuffer, sizeof(short), numread / cmd->factor, outfile);
N += numread / cmd->factor;
}
vect_free(sinbuffer);
vect_free(soutbuffer);
} else {
inbuffer = gen_fvect(bufflen * cmd->factor);
outbuffer = gen_fvect(bufflen);
while ((numread =
chkfread(inbuffer, sizeof(float), bufflen * cmd->factor, infile))) {
for (ii = 0; ii < numread / cmd->factor; ii++) {
outbuffer[ii] = 0;
for (jj = 0; jj < cmd->factor; jj++)
outbuffer[ii] += inbuffer[cmd->factor * ii + jj];
}
chkfwrite(outbuffer, sizeof(float), numread / cmd->factor, outfile);
N += numread / cmd->factor;
}
vect_free(inbuffer);
vect_free(outbuffer);
}
printf("Done. Wrote %lld points.\n\n", N);
/* Write the new info file */
idata.dt = idata.dt * cmd->factor;
idata.numonoff = 0;
idata.N = (double) N;
strncpy(idata.name, outname, strlen(outname) - 4);
if (useshorts)
idata.name[strlen(outname) - 5] = '\0';
else
idata.name[strlen(outname) - 4] = '\0';
writeinf(&idata);
fclose(infile);
fclose(outfile);
free(rootfilenm);
if (!cmd->outfileP)
free(outname);
exit(0);
}