-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrite_fits.c
219 lines (158 loc) · 6.16 KB
/
write_fits.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 <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "chealpix.h"
#include "fitsio.h"
// used by memory allocation
#define NR_END 1
#define FREE_ARG char*
// compile gcc -o write_fits write_fits.c -lm -lcfitsio
// usage: ./write_fits sky.dat
// output will be called sky.fits
void setCoordSysHP(char *,char *);
float *vector(long nl, long nh);
void free_vector(float *v, long nl, long nh);
void nrerror(char error_text[]);
int main(int argc, char *argv[])
{
int i, j, ND, nside;
double x;
float *data;
FILE *in;
if (argc!=2) nrerror("usage: ./write_fits sky.dat");
ND = 0;
in = fopen(argv[1],"r");
while(!feof(in))
{
fscanf(in,"%lf", &x);
ND++;
}
rewind(in);
ND -= 1;
nside = (int)(sqrt((double)(ND)/12.0));
printf("NSIDE = %d\n", nside);
data = vector(0,ND-1);
// Read in Data
for(j=0; j< ND; j++)
{
fscanf(in,"%f", &data[j]);
}
fclose(in);
write_healpix_map(data, nside, "sky.fits", 0, "C");
}
void nrerror(char error_text[])
/* Numerical Recipes standard error handler */
{
int rx;
fprintf(stderr,"Numerical Recipes run-time error...\n");
fprintf(stderr,"%s\n",error_text);
fprintf(stderr,"...now exiting to system...\n");
exit(1);
}
void free_vector(float *v, long nl, long nh)
/* free a double vector allocated with dvector() */
{
free((FREE_ARG) (v+nl-NR_END));
}
float *vector(long nl, long nh)
/* allocate a double vector with subscript range v[nl..nh] */
{
float *v;
v=(float *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(float)));
if (!v) nrerror("allocation failure in dvector()");
return v-nl+NR_END;
}
void write_healpix_map( float *signal, long nside, char *filename, char nest, char *coordsys) {
/*******************************************************************/
/* Create a binary table extension */
/*******************************************************************/
fitsfile *fptr; /* pointer to the FITS file, defined in fitsio.h */
int status, hdutype;
long firstrow, firstelem;
int bitpix = SHORT_IMG;
long naxis = 0;
long naxes[] = {0,0};
int tfields = 1;
long nrows;
char order[9]; /* HEALPix ordering */
char extname[] = "BINTABLE"; /* extension name */
char *ttype[] = { "SIGNAL" };
char *tform[] = { "1E" };
char *tunit[] = { " " };
char coordsys9[9];
/* Calculate the number of pixels in the full map */
nrows = 12L*nside*nside;
/* initialize status before calling fitsio routines */
status = 0;
/* create new FITS file */
if (fits_create_file(&fptr, filename, &status))
fprintf(stderr, "%s (%d): Could not create new fits file.\n",
__FILE__, __LINE__);
if ( fits_create_img(fptr, bitpix, naxis, naxes, &status) )
fprintf(stderr, "%s (%d): Could not create new image file.\n",
__FILE__, __LINE__);
if ( fits_write_date(fptr, &status) )
fprintf(stderr, "%s (%d): Could not add date.\n",
__FILE__, __LINE__);
/* move to 1nd HDU */
if ( fits_movabs_hdu(fptr, 1, &hdutype, &status) )
fprintf(stderr, "%s (%d): Could not move to first HDU.\n",
__FILE__, __LINE__);
/* append a new empty binary table onto the FITS file */
if ( fits_create_tbl( fptr, BINARY_TBL, nrows, tfields, ttype, tform,
tunit, extname, &status) )
fprintf(stderr, "%s (%d): Could not create new binary table.\n",
__FILE__, __LINE__);
if (fits_write_key(fptr, TSTRING, "PIXTYPE", "HEALPIX",
"HEALPIX Pixelisation", &status))
fprintf(stderr, "%s (%d): Could not write PIXTYPE keyword.\n",
__FILE__, __LINE__);
if (nest) strcpy(order, "NESTED ");
else strcpy(order, "RING ");
if (fits_write_key(fptr, TSTRING, "ORDERING", order,
"Pixel ordering scheme, either RING or NESTED", &status))
fprintf(stderr, "%s (%d): Could not write ORDERING keyword.\n",
__FILE__, __LINE__);
if (fits_write_key(fptr, TLONG, "NSIDE", &nside,
"Resolution parameter for HEALPIX", &status))
fprintf(stderr, "%s (%d): Could not write NSIDE keyword.\n",
__FILE__, __LINE__);
/*****NEW*************/
setCoordSysHP(coordsys,coordsys9);
if (fits_write_key(fptr, TSTRING, "COORDSYS", coordsys9,"Pixelisation coordinate system", &status))
fprintf(stderr, "%s (%d): Could not write COORDSYS keyword.\n",__FILE__, __LINE__);
/*****END**NEW*******/
if (fits_write_comment(fptr," G = Galactic, E = ecliptic, C = celestial = equatorial ", &status))
fprintf(stderr, "%s (%d): Could not write COORDSYS explanation keyword.\n",
__FILE__, __LINE__);
firstrow = 1; /* first row in table to write */
firstelem = 1; /* first element in row (ignored in ASCII tables) */
if (fits_write_col(fptr, TFLOAT, 1, firstrow, firstelem, nrows, signal,
&status))
fprintf(stderr, "%s (%d): Could not write signal.\n", __FILE__, __LINE__);
/*fits_write_col(fptr, TLONG, 2, firstrow, firstelem, nrows, pixel, &status);*/
/*fits_write_col(fptr, TLONG, 3, firstrow, firstelem, nrows, n_obs, &status);*/
/*fits_write_col(fptr, TFLOAT, 4, firstrow, firstelem, nrows, serror,&status);*/
if ( fits_close_file(fptr, &status) ) /* close the FITS file */
fprintf(stderr, "%s (%d): Could not close file.\n",
__FILE__, __LINE__);
return;
}
void setCoordSysHP(char *coordsys,char *coordsys9){
strcpy(coordsys9,"C ");
if(strncmp(coordsys,"G",1)!=0 && strncmp(coordsys,"E",1)!=0 && strncmp(coordsys,"C",1)!=0 && strncmp(coordsys,"Q",1)!=0)
fprintf(stderr, "%s (%d): System Cordinates is not correct (Galactic,Ecliptic,Celestial=Equatorial). Celestial system was set.\n", __FILE__, __LINE__);
/* if(strncmp(coordsys,"GAL",3)==0) */
/* strcpy(coordsys9,"G "); */
/* else if(strncmp(coordsys,"ECL",3)==0) */
/* strcpy(coordsys9,"E "); */
/* else if(strncmp(coordsys,"EQU",3)==0 || strncmp(coordsys,"CEL",3)==0) */
/* strcpy(coordsys9,"C "); */
if(strncmp(coordsys,"G",1)==0)
strcpy(coordsys9,"G ");
else if(strncmp(coordsys,"E",1)==0)
strcpy(coordsys9,"E ");
else if(strncmp(coordsys,"C",1)==0 || strncmp(coordsys,"Q",1)==0)
strcpy(coordsys9,"C ");
}