forked from scottransom/presto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoas2dat.c
219 lines (179 loc) · 6.14 KB
/
toas2dat.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include "toas2dat_cmd.h"
#define WORKLEN 65536
#define SECPERDAY 86400
#define MAXREAD 32768
/* #define DEBUG */
#ifdef USEDMALLOC
#include "dmalloc.h"
#endif
unsigned long getfilelen(FILE * file, size_t size)
{
int filenum, rt;
struct stat buf;
filenum = fileno(file);
rt = fstat(filenum, &buf);
if (rt == -1) {
perror("\nError in getfilelen()");
printf("\n");
exit(-1);
}
return (unsigned long) (buf.st_size / size);
}
int compare_doubles(const void *a, const void *b)
/* qsort comparison function for doubles */
{
const double *da = (const double *) a;
const double *db = (const double *) b;
return (*da > *db) - (*da < *db);
}
int read_toas(FILE *infile, double **toas)
/* Read a text file containing ASCII text TOAs. */
/* The number of TOAs read is returned. */
/* Lines beginning with '#' are ignored. */
{
double dtmp;
char line[80], *sptr = NULL;
int ii=0, numtoa=0;
/* Read the input file once to count TOAs */
while (1) {
sptr = fgets(line, 80, infile);
if (!feof(infile) && sptr != NULL && sptr[0] != '\n') {
if (line[0] != '#' && sscanf(line, "%lf", &dtmp) == 1)
numtoa++;
} else {
break;
}
}
*toas = (double *) malloc(sizeof(double) * numtoa);
/* Rewind and read the TOAs for real */
rewind(infile);
while (1) {
sptr = fgets(line, 80, infile);
if (!feof(infile) && sptr != NULL && sptr[0] != '\n') {
if (line[0] != '#' && sscanf(line, "%lf", &(*toas)[ii]) == 1)
ii++;
} else {
break;
}
}
return numtoa;
}
int main(int argc, char *argv[])
/* Convert a file of TOAs in either text or binary format */
/* into a floating point time series. The time series will */
/* have 'cmd->numout' points with each bin of length */
/* 'cmd->dt' seconds. */
{
long ii, jj, ntoas, numwrites, numtowrite, numplaced = 0;
double To, toa, *toaptr, *ddata, lotime, hitime, dtfract, blockt;
float *fdata;
FILE *infile, *outfile;
Cmdline *cmd;
/* Call usage() if we have no command line arguments */
if (argc == 1) {
Program = argv[0];
usage();
exit(0);
}
/* Parse the command line using the excellent program Clig */
cmd = parseCmdline(argc, argv);
#ifdef DEBUG
showOptionValues();
#endif
fprintf(stderr, "\n\n TOA to Time Series Converter\n");
fprintf(stderr, " by Scott M. Ransom\n");
fprintf(stderr, " 17 October 2000\n\n");
/* Open our files and read the TOAs */
printf("\nReading TOAs from '%s'.\n", cmd->argv[0]);
if (cmd->textP) { /* Text data */
infile = fopen(cmd->argv[0], "r");
ntoas = read_toas(infile, &ddata);
printf(" Found %ld TOAs.\n", ntoas);
} else { /* Binary data */
infile = fopen(cmd->argv[0], "rb");
if (cmd->floatP) { /* Floating point data */
ntoas = getfilelen(infile, sizeof(float));
printf(" Found %ld TOAs.\n", ntoas);
ddata = (double *) malloc(sizeof(double) * ntoas);
fdata = (float *) malloc(sizeof(float) * ntoas);
jj = fread(fdata, sizeof(float), ntoas, infile);
if (jj != ntoas) {
printf("\nError reading TOA file. Only %ld points read.\n\n", jj);
exit(-1);
}
for (jj = 0; jj < ntoas; jj++)
ddata[jj] = (double) fdata[jj];
free(fdata);
} else { /* Double precision data */
ntoas = getfilelen(infile, sizeof(double));
printf(" Found %ld TOAs.\n", ntoas);
ddata = (double *) malloc(sizeof(double) * ntoas);
jj = fread(ddata, sizeof(double), ntoas, infile);
if (jj != ntoas) {
printf("\nError reading TOA file. Only %ld points read.\n\n", jj);
exit(-1);
}
}
}
fclose(infile);
outfile = fopen(cmd->outfile, "wb");
/* Allocate our output array */
fdata = (float *) malloc(sizeof(float) * WORKLEN);
printf("\nWriting time series of %ld points of\n", cmd->numout);
printf("length %f seconds to '%s'.\n\n", cmd->dt, cmd->outfile);
/* Sort the TOAs */
qsort(ddata, ntoas, sizeof(double), compare_doubles);
/* Convert the TOAs to seconds offset from the first TOA */
if (cmd->t0P)
To = cmd->t0;
else
To = ddata[0];
if (cmd->secP)
for (ii = 0; ii < ntoas; ii++)
ddata[ii] = (ddata[ii] - To);
else
for (ii = 0; ii < ntoas; ii++)
ddata[ii] = (ddata[ii] - To) * SECPERDAY;
toaptr = ddata;
toa = *toaptr;
/* Determine the number of output writes we need */
numwrites = (cmd->numout % WORKLEN) ?
cmd->numout / WORKLEN + 1 : cmd->numout / WORKLEN;
dtfract = 1.0 / cmd->dt;
blockt = WORKLEN * cmd->dt;
/* Loop over the number of writes */
for (ii = 0; ii < numwrites; ii++) {
/* Determine the beginning and ending times of the output array */
lotime = ii * blockt;
hitime = (ii + 1) * blockt;
numtowrite = ((cmd->numout % WORKLEN) && (ii == (numwrites - 1))) ?
cmd->numout % WORKLEN : WORKLEN;
/* Initialize the output data array to all zeros */
for (jj = 0; jj < WORKLEN; jj++)
fdata[jj] = 0.0;
/* Place any TOAs we need to in the current output array */
while ((toaptr - ddata) < ntoas) {
if (toa >= hitime)
break;
else if (toa >= lotime) {
fdata[(int) ((toa - lotime) * dtfract)] += 1.0;
numplaced++;
}
toaptr++;
toa = *toaptr;
}
/* Write the output data */
fwrite(fdata, sizeof(float), numtowrite, outfile);
}
/* Cleanup */
printf("Done.\n Placed %ld TOAs.\n\n", numplaced);
fclose(outfile);
free(fdata);
free(ddata);
exit(0);
}