-
Notifications
You must be signed in to change notification settings - Fork 19
/
polyco.c
359 lines (320 loc) · 10.8 KB
/
polyco.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/* polyco.c
* routines to read/use polyco.dat
*/
#include "polyco.h"
#include "psrfits.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <math.h>
int read_one_pc(FILE *f, struct polyco *pc) {
int i, j;
char *rv;
int ret;
char buf[90];
/* Read in polyco chunk */
rv = fgets(buf, 90, f);
if (rv==NULL) { return(-1); }
strncpy(pc->psr, &buf[0], 10); pc->psr[10] = '\0';
pc->mjd = atoi(&buf[31]);
pc->fmjd = atof(&buf[39]);
if ((rv=strchr(pc->psr, ' '))!=NULL) { *rv='\0'; }
rv = fgets(buf,90,f);
if (rv==NULL) { return(-1); }
pc->rphase_int = atoll(&buf[0]);
pc->rphase = fmod(atof(&buf[0]),1.0);
pc->f0 = atof(&buf[20]);
pc->nsite = atoi(&buf[42]);
pc->nmin = atoi(&buf[43]);
pc->nc = atoi(&buf[50]);
pc->rf = atof(&buf[55]);
pc->used = 0;
for (i=0; i<pc->nc/3 + (pc->nc%3)?1:0; i++) {
rv=fgets(buf, 90, f);
if (rv==NULL) { return(-1); }
for (j=0; j<90; j++) { if (buf[j]=='D' || buf[j]=='d') buf[j]='e'; }
ret=sscanf(buf, "%lf %lf %lf",
&(pc->c[3*i]), &(pc->c[3*i+1]), &(pc->c[3*i+2]));
if (ret!=3) { return(-1); }
}
return(0);
}
int read_pc(FILE *f, struct polyco *pc, const char *psr, int mjd, double fmjd) {
/* Read through until we get to right psr, mjd */
int done=0, nomatch=0;
int i, j;
char *rv;
int ret;
char buf[90];
float tdiff;
while (!done) {
/* Read in polyco chunk */
rv = fgets(buf, 90, f);
if (rv==NULL) { done=1; nomatch=1; continue; }
strncpy(pc->psr, &buf[0], 10); pc->psr[10] = '\0';
pc->mjd = atoi(&buf[31]);
pc->fmjd = atof(&buf[39]);
if ((rv=strchr(pc->psr, ' '))!=NULL) { *rv='\0'; }
rv = fgets(buf,90,f);
pc->rphase = fmod(atof(&buf[0]),1.0);
pc->f0 = atof(&buf[20]);
pc->nsite = atoi(&buf[42]);
pc->nmin = atoi(&buf[43]);
pc->nc = atoi(&buf[50]);
pc->rf = atof(&buf[55]);
for (i=0; i<pc->nc/3 + (pc->nc%3)?1:0; i++) {
rv=fgets(buf, 90, f);
if (rv==NULL) { return(-1); }
for (j=0; j<90; j++) { if (buf[j]=='D' || buf[j]=='d') buf[j]='e'; }
ret=sscanf(buf, "%lf %lf %lf",
&(pc->c[3*i]), &(pc->c[3*i+1]), &(pc->c[3*i+2]));
if (ret!=3) { return(-1); }
}
/* check for correct psr - null psrname matches any */
if (psr!=NULL) { if (strcmp(pc->psr, psr)!=0) { continue; } }
tdiff = 1440.0*((double)(mjd-pc->mjd) + (fmjd-pc->fmjd));
if (fabs(tdiff) > (float)pc->nmin/2.0) { continue; }
done=1;
}
return(-1*nomatch);
}
/* Reads all polycos in a file, mallocs space for them, returns
* number found
*/
int read_all_pc(FILE *f, struct polyco **pc) {
int rv, npc=0;
do {
*pc = (struct polyco *)realloc(*pc, sizeof(struct polyco) * (npc+1));
rv = read_one_pc(f, &((*pc)[npc]));
npc++;
} while (rv==0);
npc--; // Final "read" is really a error or EOF.
return(npc);
}
/* Select appropriate polyco set */
int select_pc(const struct polyco *pc, int npc, const char *psr,
int imjd, double fmjd) {
int ipc;
const char *tmp = psr;
if (psr!=NULL)
if (tmp[0]=='J' || tmp[0]=='B') tmp++;
// Verbose
//fprintf(stderr, "Looking for polycos with src='%s' imjd=%d fmjd=%f\n",
// tmp, imjd, fmjd);
for (ipc=0; ipc<npc; ipc++) {
//fprintf(stderr, " read src='%s' imjd=%d fmjd=%f span=%d\n",
// pc[ipc].psr, pc[ipc].mjd, pc[ipc].fmjd, pc[ipc].nmin);
if (psr!=NULL) { if (strcmp(pc[ipc].psr,tmp)!=0) { continue; } }
if (pc_out_of_range(&pc[ipc],imjd,fmjd)==0) { break; }
}
if (ipc<npc) { return(ipc); }
return(-1);
}
/* Compute pulsar phase given polyco struct and mjd */
double psr_phase(const struct polyco *pc, int mjd, double fmjd, double *freq,
long long *pulsenum) {
double dt = 1440.0*((double)(mjd-pc->mjd)+(fmjd-pc->fmjd));
int i;
double phase = pc->c[pc->nc-1];
double f = 0.0;
if (fabs(dt)>(double)pc->nmin/2.0) { return(-1.0); }
for (i=pc->nc-1; i>0; i--) {
phase = dt*(phase) + pc->c[i-1];
f = dt*(f) + (double)i*pc->c[i];
}
f = pc->f0 + (1.0/60.0)*f;
phase += pc->rphase + dt*60.0*pc->f0;
if (freq!=NULL) { *freq = f; }
if (pulsenum!=NULL) {
long long n = pc->rphase_int;
n += (long long)(phase - fmod(phase,1.0));
phase = fmod(phase,1.0);
if (phase<0.0) { phase += 1.0; n--; }
*pulsenum = n;
}
return(phase);
}
double psr_fdot(const struct polyco *pc, int mjd, double fmjd, double *fdot) {
double dt = 1440.0*((double)(mjd-pc->mjd)+(fmjd-pc->fmjd));
if (fabs(dt)>(double)pc->nmin/2.0) { return(-1.0); }
double fd=0.0;
int i;
for (i=pc->nc-1; i>1; i--) {
fd = dt*fd + ((double)i)*((double)i-1.0)*pc->c[i];
}
fd /= 60.0;
if (fdot!=NULL) { *fdot=fd; }
return(fd);
}
double psr_phase_avg(const struct polyco *pc, int mjd,
double fmjd1, double fmjd2) {
double dt1 = 1440.0*((double)(mjd-pc->mjd)+(fmjd1-pc->fmjd));
double dt2 = 1440.0*((double)(mjd-pc->mjd)+(fmjd2-pc->fmjd));
if (fabs(dt1)>(double)pc->nmin/2.0) { return(-1.0); }
if (fabs(dt2)>(double)pc->nmin/2.0) { return(-1.0); }
double pavg;
int i;
double tmp1=0.0, tmp2=0.0;
for (i=pc->nc-1; i>=0; i--) {
tmp1 = dt1*tmp1 + pc->c[i]/((double)i+1.0);
tmp2 = dt2*tmp2 + pc->c[i]/((double)i+1.0);
}
tmp1 *= dt1; tmp2 *= dt2;
pavg = (tmp2-tmp1)/(dt2-dt1) + pc->rphase + (dt1+dt2)*60.0*pc->f0/2.0;
return(pavg);
}
int pc_range_check(const struct polyco *pc, int mjd, double fmjd) {
double dt;
dt = (double)(mjd - pc->mjd) + (fmjd - pc->fmjd);
dt *= 1440.0;
if (dt < -1.0*(double)pc->nmin/2.0) { return(-1); }
else if (dt > (double)pc->nmin/2.0) { return(1); }
else { return(0); }
}
int pc_out_of_range(const struct polyco *pc, int mjd, double fmjd) {
double dt;
dt = (double)(mjd - pc->mjd) + (fmjd - pc->fmjd);
dt *= 1440.0;
if (fabs(dt)>(double)pc->nmin/2.0) { return(1); }
return(0);
}
int pc_out_of_range_sloppy(const struct polyco *pc, int mjd, double fmjd,
double slop) {
double dt;
dt = (double)(mjd - pc->mjd) + (fmjd - pc->fmjd);
dt *= 1440.0;
if (fabs(dt)>slop*(double)pc->nmin/2.0) { return(1); }
return(0);
}
/* Check whether or not two polyco structs are the same */
int polycos_differ(const struct polyco *p1, const struct polyco *p2) {
// Could add more tests as needed
if (strncmp(p1->psr, p2->psr,15)!=0) return(1);
if (p1->mjd!=p2->mjd) return(1);
if (p1->fmjd!=p2->fmjd) return(1);
if (p1->rf!=p2->rf) return(1);
if (p1->nsite!=p2->nsite) return(1);
if (p1->nmin!=p2->nmin) return(1);
if (p1->nc!=p2->nc) return(1);
return(0);
}
/* Convert telescope name to tempo code */
char telescope_name_to_code(const char *name) {
/* Assume a 1-char input is already a code */
if (strlen(name)==1) { return(name[0]); }
/* Add to these as needed .. */
if (strcasecmp(name, "GBT")==0) return('1');
if (strcasecmp(name, "GB43m")==0) return('a');
if (strcasecmp(name, "GB 43m")==0) return('a');
if (strncasecmp(name, "GB140",5)==0) return('a');
if (strncasecmp(name, "GB 140",6)==0) return('a');
if (strncasecmp(name, "NRAO20",6)==0) return('a');
if (strncasecmp(name, "Arecibo",7)==0) return('3');
if (strcasecmp(name, "AO")==0) return('3');
if (strcasecmp(name, "SHAO")==0) return('s');
if (strcasecmp(name, "VLA")==0) return('6');
/* Not found, return null */
return('\0');
}
/* Generate polycos from a parfile */
#define make_polycos_cleanup() do {\
unlink("pulsar.par");\
unlink("polyco.dat");\
unlink("tz.in");\
chdir(origdir);\
free(origdir);\
rmdir(tmpdir);\
} while (0)
int make_polycos(const char *parfile, struct hdrinfo *hdr,
char *src, struct polyco **pc) {
/* Open parfile */
FILE *pf = fopen(parfile, "r");
if (pf==NULL) {
fprintf(stderr, "make_polycos: Error opening parfile %s\n", parfile);
return(-1);
}
/* Generate temp directory */
char tmpdir[] = "/tmp/polycoXXXXXX";
if (mkdtemp(tmpdir)==NULL) {
fprintf(stderr, "make_polycos: Error generating temp dir.\n");
fclose(pf);
return(-1);
}
/* change to temp dir */
char *origdir = getcwd(NULL,0);
chdir(tmpdir);
/* Open temp dir */
char fname[256];
sprintf(fname, "%s/pulsar.par", tmpdir);
FILE *fout = fopen(fname, "w");
if (fout==NULL) {
fprintf(stderr, "make_polycos: Error writing to temp dir.\n");
fclose(pf);
make_polycos_cleanup();
return(-1);
}
/* Get source name, copy file */
char line[256], parsrc[32]="", *key, *val, *saveptr, *ptr;
while (fgets(line,256,pf)!=NULL) {
fprintf(fout, "%s", line);
while ((ptr=strchr(line,'\t'))!=NULL) *ptr=' ';
if ((ptr=strrchr(line,'\n')) != NULL) *ptr='\0';
key = strtok_r(line, " ", &saveptr);
val = strtok_r(NULL, " ", &saveptr);
if (key==NULL || val==NULL) continue;
if (strncmp(key, "PSR", 3)==0) {
// J or B is bad here?
if (val[0]=='J' || val[0]=='B') val++;
strcpy(parsrc, val);
}
}
fclose(pf);
fclose(fout);
if (parsrc[0]=='\0') {
fprintf(stderr, "make_polycos: Couldn't find source name in %s\n",
parfile);
make_polycos_cleanup();
return(-1);
}
if (src!=NULL) { strcpy(src,parsrc); }
/* Get telescope character */
char tcode = telescope_name_to_code(hdr->telescope);
if (tcode=='\0') {
fprintf(stderr, "make_polycos: Unrecognized telescope name (%s)\n",
hdr->telescope);
make_polycos_cleanup();
return(-1);
}
/* Write tz.in */
sprintf(fname, "%s/tz.in", tmpdir);
fout = fopen(fname, "w");
if (fout==NULL) {
fprintf(stderr, "make_polycos: Error opening tz.in for write.\n");
make_polycos_cleanup();
return(-1);
}
fprintf(fout, "%c 12 30 15 %.5f\n\n\n%s\n",
tcode, hdr->fctr, parsrc);
fclose(fout);
/* Call tempo */
int mjd0, mjd1;
mjd0 = (int)hdr->MJD_epoch;
mjd1 = (int)(hdr->MJD_epoch + hdr->scanlen/86400.0 + 0.5);
if (mjd1==mjd0) mjd1++;
sprintf(line, "echo %d %d | tempo -z -f pulsar.par > /dev/null",
mjd0-1, mjd1);
system(line);
/* Read polyco file */
FILE *pcfile = fopen("polyco.dat", "r");
if (pcfile==NULL) {
fprintf(stderr, "make_polycos: Error reading polyco.dat\n");
make_polycos_cleanup();
return(-1);
}
int npc = read_all_pc(pcfile, pc);
fclose(pcfile);
/* Clean up */
make_polycos_cleanup();
return(npc);
}