forked from demorest/psrfits_utils
-
Notifications
You must be signed in to change notification settings - Fork 7
/
fix_psrfits_polyco.c
108 lines (94 loc) · 3.02 KB
/
fix_psrfits_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
/* fix_psrfits_polyco.c
*
* Install missing polycos into a psrfits file.
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <fitsio.h>
#include <getopt.h>
#include "polyco.h"
#include "psrfits.h"
int main(int argc, char *argv[]) {
/* cmd line */
static struct option long_opts[] = {
{"parfile", 1, NULL, 'P'},
{0,0,0,0}
};
int opt, opti;
char parfile[256]="";
while ((opt=getopt_long(argc,argv,"P:",long_opts,&opti))!=-1) {
switch (opt) {
case 'P':
strncpy(parfile, optarg, 255);
parfile[255]='\0';
break;
default:
exit(0);
break;
}
}
if (optind==argc) {
fprintf(stderr, "No files given.\n");
exit(1);
}
if (parfile[0]=='\0') {
fprintf(stderr, "No .par file given.\n");
exit(1);
}
int i, rv;
struct psrfits pf;
struct polyco *pc=NULL;
for (i=optind; i<argc; i++) {
// Open file
strcpy(pf.filename, argv[i]);
pf.status=0;
rv = psrfits_open(&pf);
if (rv) { fits_report_error(stderr, rv); exit(1); }
// Basic info
int nsubint = pf.rows_per_file;
printf("Found %d subints.\n", nsubint);
// Make polycos
char source[32];
int npc = make_polycos(parfile, &pf.hdr, source, &pc);
if (npc<=0) {
fprintf(stderr, "Error generating polycos.\n");
exit(1);
}
// Decide which ones are needed
int isub, col;
fits_movnam_hdu(pf.fptr, BINARY_TBL, "SUBINT", 0, &pf.status);
fits_get_colnum(pf.fptr, CASEINSEN, "OFFS_SUB", &col, &pf.status);
for (isub=0; isub<nsubint; isub++) {
double offset, fmjd;
fits_read_col(pf.fptr, TDOUBLE, col, isub+1, 1, 1, NULL, &offset,
NULL, &pf.status);
fmjd = (pf.hdr.start_sec + offset)/86400.0;
int ipc = select_pc(pc, npc, source, pf.hdr.start_day, fmjd);
if (ipc<0) {
fprintf(stderr, "Polycos do not span observation range.\n");
exit(1);
}
printf("Polyco set %d was used (isub=%d offset=%f)\n",
ipc, isub, offset);
pc[ipc].used = 1;
}
// Close file
psrfits_close(&pf);
// Reopen for writing
rv = fits_open_file(&pf.fptr, pf.filename, READWRITE, &pf.status);
if (rv) { fits_report_error(stderr, rv); exit(1); }
// Delete all old polycos??????
long nrows;
fits_movnam_hdu(pf.fptr, BINARY_TBL, "POLYCO", 0, &pf.status);
fits_get_num_rows(pf.fptr, &nrows, &pf.status);
fits_delete_rows(pf.fptr, 1, nrows, &pf.status);
// Fill in new polycos
rv = psrfits_write_polycos(&pf, pc, npc);
if (rv) { fits_report_error(stderr, pf.status); pf.status=0; }
// Close file, all done
fits_close_file(pf.fptr, &pf.status);
}
exit(0);
}