-
Notifications
You must be signed in to change notification settings - Fork 0
/
pfits_setup.c
154 lines (130 loc) · 6.07 KB
/
pfits_setup.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
// 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/>.
*/
// Standard routines for pfits
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "pfits.h"
#include "fitsio.h"
int initialise(dSetStruct **dSet,int debugFlag)
{
if (debugFlag==1) printf("Entering initialise\n");
// Allocate memory for the main structure
if (!(*dSet = (dSetStruct *)malloc(sizeof(dSetStruct))))
errorStop("Unable to allocate enough memory to allocate the data structure\n",*dSet,debugFlag);
// Allocate memory for the header information
//
(*dSet)->headerMemorySet=0;
if (!((*dSet)->head = (headerStruct *)malloc(sizeof(headerStruct))))
errorStop("Unable to allocate enough memory to allocate the header structure\n",(*dSet),debugFlag);
(*dSet)->headerMemorySet=1;
(*dSet)->head->headerSet=0;
// Set some defaults
strcpy((*dSet)->fileName,"UNSET");
(*dSet)->fileOpen = 0;
(*dSet)->fileSet = 0;
if (debugFlag==1) printf("Complete initialise\n");
return 0;
}
int deallocateMemory(dSetStruct **dSet,int debugFlag)
{
if (debugFlag==1) printf("Entering deallocateMemory\n");
if ((*dSet)->headerMemorySet==1)
free((*dSet)->head);
free(*dSet);
if (debugFlag==1) printf("Complete deallocateMemory\n");
return 0;
}
void errorStop(char *err,dSetStruct *dSet,int debugFlag)
{
printf("%s",err);
deallocateMemory(&dSet,debugFlag);
exit(1);
}
void pfitsLoadHeader(dSetStruct *dSet,int debug)
{
int status=0;
int colnum=0;
float nval=0;
int initFlag=0;
if (debug==1)
printf("Entering loadHeader\n");
if (dSet->headerMemorySet==0)
errorStop("Trying to load the header information before allocating memory\n",dSet,debug);
// Read primary header information
if (status) {printf("pos 0\n"); fits_report_error(stderr,status); exit(1);}
fits_read_key(dSet->fp,TFLOAT,"OBSFREQ",&(dSet->head->freq),NULL,&status);
if (status) {printf("pos 1 OBSFREQ\n"); fits_report_error(stderr,status); exit(1);}
fits_read_key(dSet->fp,TFLOAT,"OBSBW",&(dSet->head->bw),NULL,&status);
if (status) {printf("pos 1 OBSBW\n"); fits_report_error(stderr,status); exit(1);}
fits_read_key(dSet->fp,TINT,"STT_IMJD",&(dSet->head->stt_imjd),NULL,&status);
if (status) {printf("pos 1 STT_IMJD\n"); fits_report_error(stderr,status); exit(1);}
fits_read_key(dSet->fp,TFLOAT,"STT_SMJD",&(dSet->head->stt_smjd),NULL,&status);
if (status) {printf("pos 1 STT_SMJD\n"); fits_report_error(stderr,status); exit(1);}
fits_read_key(dSet->fp,TFLOAT,"STT_OFFS",&(dSet->head->stt_offs),NULL,&status);
if (status) {printf("pos 1 STT_OFFS\n"); fits_report_error(stderr,status); exit(1);}
fits_read_key(dSet->fp,TSTRING,"SRC_NAME",(dSet->head->source),NULL,&status);
fits_read_key(dSet->fp,TSTRING,"RA",(dSet->head->ra),NULL,&status);
if (status) {printf("pos 1 RA\n"); fits_report_error(stderr,status); exit(1);}
fits_read_key(dSet->fp,TSTRING,"DEC",(dSet->head->dec),NULL,&status);
if (status) {printf("pos 1 DEC\n"); fits_report_error(stderr,status); exit(1);}
// Read SUBINT header information
fits_movnam_hdu(dSet->fp,BINARY_TBL,"SUBINT",1,&status);
if (status) {printf("pos 1.1\n"); fits_report_error(stderr,status); exit(1);}
fits_read_key(dSet->fp,TINT,"NAXIS2",&(dSet->head->nsub),NULL,&status);
if (status) {printf("pos 1.2\n"); fits_report_error(stderr,status); exit(1);}
fits_read_key(dSet->fp,TINT,"NCHAN",&(dSet->head->nchan),NULL,&status);
if (status) {printf("pos 1.3\n"); fits_report_error(stderr,status); exit(1);}
fits_read_key(dSet->fp,TINT,"NBITS",&(dSet->head->nbits),NULL,&status);
if (status) {printf("Error reading nbits ... trying to continue\n"); fits_report_error(stderr,status); status=0;}
fits_read_key(dSet->fp,TINT,"NPOL",&(dSet->head->npol),NULL,&status);
if (status) {printf("pos 1.5\n"); fits_report_error(stderr,status); exit(1);}
fits_read_key(dSet->fp,TINT,"NSBLK",&(dSet->head->nsblk),NULL,&status);
if (status) {printf("pos 1.6\n"); fits_report_error(stderr,status); exit(1);}
fits_read_key(dSet->fp,TFLOAT,"TBIN",&(dSet->head->tsamp),NULL,&status);
if (status) {printf("Error reading tbin ... trying to continue\n"); fits_report_error(stderr,status); status=0;}
fits_read_key(dSet->fp,TFLOAT,"CHAN_BW",&(dSet->head->chanbw),NULL,&status);
if (status) {printf("pos 2\n"); fits_report_error(stderr,status); exit(1);}
fits_read_key(dSet->fp,TINT,"NBIN",&(dSet->head->nbin),NULL,&status);
if (status) {printf("error with nbin\n"); fits_report_error(stderr,status); exit(1);}
// Read the frequency channels
fits_get_colnum(dSet->fp,CASEINSEN,"DAT_FREQ",&colnum,&status);
printf("Reading\n");
fits_read_col_flt(dSet->fp,colnum,1,1,dSet->head->nchan,nval,dSet->head->chanFreq,&initFlag,&status);
if (status) {printf("pos 3\n"); fits_report_error(stderr,status); exit(1);}
if (debug==1)
printf("Leaving loadHeader\n");
}
void pfitsOpenFile(dSetStruct *dSet,int debug)
{
int status=0;
if (debug==1)
printf("Entering pfitsOpenFile\n");
if (dSet->fileSet==0)
errorStop("Trying to open a file before setting the filename\n",dSet,debug);
fits_open_file(&(dSet->fp),dSet->fileName,READONLY,&status);
fits_report_error(stderr,status);
dSet->fileOpen = 1;
if (debug==1)
printf("Leaving pfitsOpenFile\n");
}
void setFilename(char *str,dSetStruct *dSet,int debug)
{
if (debug==1) printf("Entering setFilename\n");
strcpy(dSet->fileName,str);
dSet->fileSet = 1;
if (debug==1) printf("Leaving setFilename\n");
}