-
Notifications
You must be signed in to change notification settings - Fork 0
/
pfits_merge.c
132 lines (117 loc) · 4.02 KB
/
pfits_merge.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
// Copyright (C) 2015,2016 George Hobbs
// This file is part of the pfits software package
//
/* pfits is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* pfits is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with pfits. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "fitsio.h"
// In order to merge multiple psrfits files we:
// 1) make a copy of the first file
int main(int argc,char *argv[])
{
char outfile[1024],oname[1024];
int i;
fitsfile *outfptr,*infptr;
char fname[100][1024];
int status=0;
int nFiles=0;
int ii=1;
long nSubint_out,nSubint_in;
int colnum;
int tableWidth;
unsigned char *loadBytes;
double *offsSub;
double nulldouble = 0.0;
int initflag = 0;
double stepSize;
printf("Starting\n");
// Set up some defaults
strcpy(outfile,"pfitsMerge.sf");
for (i=1;i<argc;i++)
{
if (strcmp(argv[i],"-o")==0)
strcpy(outfile,argv[++i]);
else
strcpy(fname[nFiles++],argv[i]);
}
printf("Starting\n");
sprintf(oname,"!%s",outfile);
printf("Creating new PSRFITS file\n");
if ( !fits_create_file(&outfptr, oname, &status) )
{
// Copy the first file
if ( !fits_open_file(&infptr, fname[0], READONLY, &status) )
{
/* Copy every HDU until we get an error */
while( !fits_movabs_hdu(infptr, ii++, NULL, &status) )
{
fits_copy_hdu(infptr, outfptr, 0, &status);
}
/* Reset status after normal error */
if (status == END_OF_FILE) status = 0;
fits_close_file(infptr, &status);
}
else
{
printf("Unable to open input file %s\n",fname[0]);
exit(1);
}
}
else
printf("Unable to open the output file: %s\n",outfile);
// Now combine this file with the other files
printf("combining: nFiles = %d\n",nFiles);
for (i=1;i<nFiles;i++)
{
printf("Merging file: %s\n",fname[i]);
if ( !fits_open_file(&infptr, fname[i], READONLY, &status) )
{
fits_movnam_hdu(outfptr,BINARY_TBL,"SUBINT",1,&status);
fits_get_num_rows(outfptr,&nSubint_out,&status);
printf("nSubint_out = %ld\n",nSubint_out);
fits_movnam_hdu(infptr,BINARY_TBL,"SUBINT",1,&status);
fits_get_num_rows(infptr,&nSubint_in,&status);
// Make more space in the output file
fits_insert_rows(outfptr,nSubint_out,nSubint_in,&status);
// Determine the width of the SUBINT table in bytes
fits_read_key(infptr,TINT,"NAXIS1",&tableWidth,NULL,&status);
printf("nSubint_in = %ld %d\n",nSubint_in,tableWidth);
// Allocate enough memory
loadBytes = (unsigned char *)malloc(sizeof(unsigned char)*tableWidth*nSubint_in);
fits_read_tblbytes(infptr,1,1,tableWidth*nSubint_in,&loadBytes[0],&status);
fits_write_tblbytes(outfptr,nSubint_out+1,1,tableWidth*nSubint_in,&loadBytes[0],&status);
free(loadBytes);
fits_close_file(infptr,&status);
}
else
{
printf("Unable to open file %s for merging\n",fname[i]);
exit(1);
}
}
// Must now fix the OFFS_SUB column to ensure continuity throughout the PSRFITS file
printf("Updating OFFS_SUB column\n");
fits_get_num_rows(outfptr,&nSubint_out,&status);
fits_get_colnum(outfptr,CASEINSEN,"OFFS_SUB",&colnum,&status);
offsSub = (double *)malloc(sizeof(double)*nSubint_out);
fits_read_col(outfptr,TDOUBLE,colnum,1,1,nSubint_out,&nulldouble,&(offsSub[0]),&initflag,&status);
stepSize = offsSub[0]*2;
for (i=1;i<nSubint_out;i++)
offsSub[i] = offsSub[i-1]+stepSize;
fits_write_col(outfptr,TDOUBLE,colnum,1,1,nSubint_out,&(offsSub[0]),&status);
free(offsSub);
printf("Closing file\n");
fits_close_file(outfptr, &status);
printf("Ending\n");
}