-
Notifications
You must be signed in to change notification settings - Fork 10
/
readfits.c
110 lines (106 loc) · 2.89 KB
/
readfits.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
#include"wcstools-3.9.2/libwcs/fitsfile.h"
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
void readfits(char* filename,double **buffer,int x0,int y0,int nx,int ny,double *date,int *xsize,int *ysize)
{
char *head;
char *image;
double *buf;
int imsize=0;
int nlog;
int naxis1,naxis2;
int bitpix;
float *fimage;
double *dimage;
char keyword[]="MJD-OBS";
int lhead,nbhead;
head=fitsrhead(filename,&lhead,&nbhead);
if(!hgetr8(head,keyword,date))
*date=NAN;
hgeti4(head,"NAXIS1",&naxis1);
hgeti4(head,"NAXIS2",&naxis2);
hgeti4(head,"BITPIX",&bitpix);
if ((x0<=0 || y0<=0 || nx<=0 || ny<=0 || x0+nx>naxis1 || y0+ny>naxis2) && (naxis1%2!=0 || naxis2%2!=0))
{
x0=1;
y0=1;
nx=naxis1-naxis1%2;
ny=naxis2-naxis2%2;
printf("Using the whole image %s, but size is odd. Fixing\n",filename);
}
if(x0<=0 || y0<=0 ||nx<=0 || ny<=0 || x0+nx>naxis1 || y0+ny>naxis2)
{
*xsize=naxis1;
*ysize=naxis2;
imsize=naxis1*naxis2;
image=fitsrimage(filename, nbhead, head);
if(bitpix!=-64)
fimage=(float *)image;
else
dimage=(double *)image;
}
else
{
*xsize=nx;
*ysize=ny;
imsize=nx*ny;
image=fitsrsect(filename,head,nbhead,x0,y0,nx,ny,nlog);
//printf("x0:%d y0: %d\n",x0,y0);
//printf("Input readfits: %d %d %d %d\n",x0,y0,nx,ny);
if(bitpix!=-64)
fimage=(float *)image;
else
dimage=(double *)image;
}
buf=calloc(imsize,sizeof(double));
if(bitpix!=-64)
for(int j=0;j<imsize;j++)
buf[j]=fmax(fimage[j],0);
else
for(int j=0;j<imsize;j++)
buf[j]=fmax(dimage[j],0);
free(head);
free(image);
*buffer=buf;
//printf("xsize: %d ysize: %d\n",*xsize,*ysize);
//write_matrix_file("/tmp/test.txt",*buffer,*ysize,*xsize);
//exit(1);
}
/*
void main()
{
char *head,test;
char *image;
float *fimage;
int xsize,ysize;
int lhead,nbhead,ival;
char filename[]="test.fits";
char keyword[]="MJD-OBS";
int imsize;
double date;
double *buffer;
double *buffer2;
//head=fitsrhead(filename,&lhead,&nbhead);
//printf("ldhead: %d nbhead: %d\n",lhead,nbhead);
//printf("%s\n",head);
//if(hgetr8(head,keyword,&date))
// printf("DATE: %f\n",date);
// image=fitsrimage(filename, nbhead, head);
// fimage=(float *)image;
// printf("\n %f \n",fimage[256*255+255]);
readfits(filename,&buffer,0,0,0,0,&date,&xsize,&ysize);
printf("xsize: %d ysize: %d\n",xsize,ysize);
printf("date: %f\n",date);
for(int j=0;j<10;j++)
printf(" %f ",buffer[j+2*256]);
date=0;
xsize=0;
ysize=0;
readfits(filename,&buffer2,0,0,10,10,&date,&xsize,&ysize);
printf("xsize: %d ysize: %d\n",xsize,ysize);
printf("date: %f\n",date);
for(int j=0;j<10;j++)
printf(" %f ",buffer2[j+2*10]);
}
*/