forked from cbassa/sattools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalibrate.c
More file actions
684 lines (586 loc) · 14.2 KB
/
calibrate.c
File metadata and controls
684 lines (586 loc) · 14.2 KB
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include "qfits.h"
#include <cpgplot.h>
#include <wcslib/cel.h>
#include <gsl/gsl_multifit.h>
#include <getopt.h>
#define LIM 256
#define NMAX 8192
#define D2R M_PI/180.0
#define R2D 180.0/M_PI
struct star {
double ra,de;
float pmra,pmde;
float mag;
};
struct image {
int naxis1,naxis2,naxis3;
float *z;
float zmin,zmax;
double ra0,de0;
float x0,y0;
float a[3],b[3];
double mjd;
} img;
struct catalog {
int n;
float x[NMAX],y[NMAX],mag[NMAX];
double ra[NMAX],de[NMAX],rx[NMAX],ry[NMAX];
int select[NMAX];
};
struct image read_fits(char *filename,int pnum);
struct catalog read_pixel_catalog(char *filename,float,float,float);
int fgetline(FILE *file,char *s,int lim);
struct catalog read_astrometric_catalog(char *filename,float mmin,float sx,float sy,float angle);
void forward(double ra0,double de0,double ra,double de,double *x,double *y);
int select_nearest(struct catalog c,float x,float y);
void lfit2d(float *x,float *y,float *z,int n,float *a);
void fit_transformation(struct catalog cat,struct catalog ast,int nselect);
struct catalog reread_astrometric_catalog(char *filename,float mmin);
int match_catalogs(struct catalog *cat,struct catalog *ast,float rmax);
double sex2dec(char *s);
void usage(void)
{
printf("-f <file> FITS file to calibrate\n");
printf("-R <hh:mm:ss> RA of center\n");
printf("-D <ddd:mm:ss> Decl of center\n");
printf("-s <scale> Pixel scale (arcseconds) [10.0]\n");
printf("-q <parang> Parallactic angle (degrees) [0.0]\n");
printf("-m <limmag> Magnitude limit [9.0]\n");
printf("-r <radius> Matching radius (pixels) [10]\n");
return;
}
int main(int argc,char *argv[])
{
int i,j;
float xmin,xmax,ymin,ymax,zmin,zmax;
float tr[]={-0.5,1.0,0.0,-0.5,0.0,1.0};
float heat_l[] = {0.0, 0.2, 0.4, 0.6, 1.0};
float heat_r[] = {0.0, 0.5, 1.0, 1.0, 1.0};
float heat_g[] = {0.0, 0.0, 0.5, 1.0, 1.0};
float heat_b[] = {0.0, 0.0, 0.0, 0.3, 1.0};
int redraw=1,plotcat=0,click=0,nselect=0;
float x,y,width;
char c,pixcat[LIM];
struct catalog cat,ast;
float sx=-10.0,sy=10.0,q=0.0;
char *env,starfile[128];
float r,srmin=1.0,srmax=10.0,smmin=2.0,smmax=8.0,mag=9.0,rmax=10.0,rmask=-1;
int arg=0;
char *fitsfile=NULL;
char sra[16],sde[16];
double ra,de;
FILE *file;
// Environment variables
env=getenv("ST_DATADIR");
sprintf(starfile,"%s/data/tycho2.dat",env);
// Decode options
if (argc>1) {
while ((arg=getopt(argc,argv,"f:R:D:s:q:m:r:M:"))!=-1) {
switch(arg) {
case 'f':
fitsfile=optarg;
break;
case 'R':
strcpy(sra,optarg);
if (strchr(sra,':')!=NULL)
ra=15.0*sex2dec(sra);
else
ra=atof(sra);
break;
case 'D':
strcpy(sde,optarg);
if (strchr(sde,':')!=NULL)
de=sex2dec(sde);
else
de=atof(sde);
break;
case 's':
if (strchr(optarg,',')!=NULL) {
sscanf(optarg,"%f,%f",&sx,&sy);
} else {
sx=-atof(optarg);
sy=-sx;
}
break;
case 'q':
q=atof(optarg);
break;
case 'm':
mag=atof(optarg);
break;
case 'M':
rmask=atof(optarg);
break;
case 'r':
rmax=atof(optarg);
break;
case 'h':
usage();
return 0;
break;
default:
usage();
return 0;
}
}
} else {
usage();
return 0;
}
// Read image
img=read_fits(fitsfile,0);
// Set variables
img.ra0=ra;
img.de0=de;
img.x0=0.5*(float) img.naxis1;
img.y0=0.5*(float) img.naxis2;
// Read pixel catalog
sprintf(pixcat,"%s.cat",fitsfile);
cat=read_pixel_catalog(pixcat,img.x0,img.y0,rmask);
// Read astrometric catalog
ast=read_astrometric_catalog(starfile,mag,sx,sy,q);
// Open server
cpgopen("/xs");
// cpgpap(0.,1.0);
cpgask(0);
cpgsch(0.8);
// Default limits
width=(img.naxis1>img.naxis2) ? img.naxis1 : img.naxis2;
xmin=0.5*(img.naxis1-width);
xmax=0.5*(img.naxis1+width);
ymin=0.5*(img.naxis2-width);
ymax=0.5*(img.naxis2+width);
zmin=img.zmin;
zmax=img.zmax;
// Forever loop
for (;;) {
if (redraw==1) {
cpgeras();
cpgsvp(0.1,0.95,0.1,0.95);
cpgwnad(xmin,xmax,ymin,ymax);
cpglab("x (pix)","y (pix)"," ");
cpgsfs(2);
cpgctab (heat_l,heat_r,heat_g,heat_b,5,1.0,0.5);
cpgimag(img.z,img.naxis1,img.naxis2,1,img.naxis1,1,img.naxis2,zmin,zmax,tr);
cpgbox("BCTSNI",0.,0,"BCTSNI",0.,0);
// Plot catalog
if (plotcat==1) {
cpgsci(3);
for (i=0;i<cat.n;i++) {
if (cat.select[i]!=0)
cpgpt1(cat.x[i],cat.y[i],6);
else
cpgpt1(cat.x[i],cat.y[i],24);
}
cpgsci(1);
}
cpgsci(4);
for (i=0;i<ast.n;i++) {
r=srmax-(srmax-srmin)*(ast.mag[i]-smmin)/(smmax-smmin);
// Upscale for image size
r*=img.naxis1/752.0;
if (ast.select[i]!=0)
cpgpt1(ast.x[i],ast.y[i],6);
cpgcirc(ast.x[i],ast.y[i],r);
}
cpgsci(1);
redraw=0;
}
// Get cursor
cpgcurs(&x,&y,&c);
// Quit
if (c=='q')
break;
// Help
if (c=='h') {
printf("Calibrates astrometry. Initially requires manual matching of at least three stars. Use 'a' to select star on the image, then 'b' to select star from the catalog (blue circles). If at least three stars are matched, use 'f' and 'm' to converge the calibration.\n\n");
printf("q Quit\n");
printf("a Select star on image\n");
printf("b Select star from catalog\n");
printf("c Center image on pixel\n");
printf("f Fit calibration\n");
printf("m Match stars using current calibration\n");
printf("z/+ Zoom in on cursor\n");
printf("x/- Zoom out on cursor\n");
printf("p Plot sextractor catalog\n");
printf("r Reset zoom\n");
}
// Select pixel catalog
if (c=='a' && click==0) {
i=select_nearest(cat,x,y);
cat.select[i]=nselect+1;
redraw=1;
click=1;
}
// Select catalog
if (c=='b' && click==1) {
i=select_nearest(ast,x,y);
ast.select[i]=nselect+1;
redraw=1;
click=0;
nselect++;
}
// Center
if (c=='c') {
xmin=x-0.5*width;
xmax=x+0.5*width;
ymin=y-0.5*width;
ymax=y+0.5*width;
redraw=1;
continue;
}
// Fit
if (c=='f' && nselect>=3) {
fit_transformation(cat,ast,nselect);
ast=reread_astrometric_catalog(starfile,mag+1);
redraw=1;
}
// Zoom
if (c=='z' || c=='+' || c=='=') {
width/=1.25;
xmin=x-0.5*width;
xmax=x+0.5*width;
ymin=y-0.5*width;
ymax=y+0.5*width;
redraw=1;
continue;
}
// Match catalogs
if (c=='m') {
nselect=match_catalogs(&cat,&ast,rmax);
redraw=1;
}
// Unzoom
if (c=='x' || c=='-') {
width*=1.25;
xmin=x-0.5*width;
xmax=x+0.5*width;
ymin=y-0.5*width;
ymax=y+0.5*width;
redraw=1;
continue;
}
// Plot catalog
if (c=='p') {
plotcat = (plotcat==1) ? 0 : 1;
redraw=1;
}
// Reset
if (c=='r') {
width=(img.naxis1>img.naxis2) ? img.naxis1 : img.naxis2;
xmin=0.5*(img.naxis1-width);
xmax=0.5*(img.naxis1+width);
ymin=0.5*(img.naxis2-width);
ymax=0.5*(img.naxis2+width);
redraw=1;
continue;
}
// Dump
if (c=='d') {
file=fopen("dump.dat","w");
for (i=0;i<nselect;i++) {
for (j=0;j<cat.n;j++)
if (cat.select[j]==i+1)
fprintf(file,"%lf %lf ",cat.x[j]-img.x0,cat.y[j]-img.y0);
for (j=0;j<ast.n;j++)
if (ast.select[j]==i+1)
fprintf(file,"%lf %lf\n",ast.ra[j],ast.de[j]);
}
fclose(file);
}
}
cpgend();
return 0;
}
// Read fits image
struct image read_fits(char *filename,int pnum)
{
int i,j,k,l,m;
qfitsloader ql;
char key[FITS_LINESZ+1] ;
struct image img;
float s1,s2,avg,std;
// Set plane
ql.xtnum = 0;
ql.pnum = pnum;
// Set loadtype
ql.ptype = PTYPE_FLOAT;
// Set filename
ql.filename=filename;
// Image size
img.naxis1=atoi(qfits_query_hdr(filename,"NAXIS1"));
img.naxis2=atoi(qfits_query_hdr(filename,"NAXIS2"));
// img.mjd=atof(qfits_query_hdr(filename,"MJD-OBS"));
// Initialize load
if (qfitsloader_init(&ql) != 0)
printf("Error initializing data loading\n");
// Test load
if (qfits_loadpix(&ql) != 0)
printf("Error loading actual data\n");
// Allocate image memory
img.z=(float *) malloc(sizeof(float) * img.naxis1*img.naxis2);
// Fill z array
for (i=0,l=0,m=0;i<img.naxis1;i++) {
for (j=0;j<img.naxis2;j++) {
img.z[l]=ql.fbuf[l];
l++;
}
}
// Get levels
for (i=0,s1=0.0;i<img.naxis1*img.naxis2;i++)
s1+=img.z[i];
avg=s1/(float) (img.naxis1*img.naxis2);
for (i=0,s2=0.0;i<img.naxis1*img.naxis2;i++)
s2+=pow(img.z[i]-avg,2);
std=sqrt(s2/(float) (img.naxis1*img.naxis2));
img.zmin=avg-4.0*std;
img.zmax=avg+16.0*std;
return img;
}
// Read pixel catalog
struct catalog read_pixel_catalog(char *filename,float x0,float y0,float rmin)
{
int i=0;
FILE *file;
char line[LIM];
struct catalog c;
float dx,dy,r;
// Read catalog
file=fopen(filename,"r");
if (file==NULL) {
fprintf(stderr,"%s not found!\n",filename);
exit(0);
}
while (fgetline(file,line,LIM)>0) {
if (i>=NMAX)
break;
if (strstr(line,"#")!=NULL)
continue;
sscanf(line,"%f %f %f",&c.x[i],&c.y[i],&c.mag[i]);
dx=c.x[i]-x0;
dy=c.y[i]-y0;
r=sqrt(dx*dx+dy*dy);
if (r>rmin && rmin>0.0)
continue;
c.select[i]=0;
i++;
}
fclose(file);
c.n=i;
return c;
}
// Read a line of maximum length int lim from file FILE into string s
int fgetline(FILE *file,char *s,int lim)
{
int c,i=0;
while (--lim > 0 && (c=fgetc(file)) != EOF && c != '\n')
s[i++] = c;
if (c == '\n')
s[i++] = c;
s[i] = '\0';
return i;
}
struct catalog read_astrometric_catalog(char *filename,float mmin,float sx,float sy,float angle)
{
int i=0;
FILE *file;
char line[LIM];
struct catalog c;
double rx,ry,x,y,ra,de;
struct star s;
double d,dx,dy;
double mjd0=51544.5;
file=fopen(filename,"rb");
if (file==NULL) {
fprintf(stderr,"%s not found!\n",filename);
exit(0);
}
while (!feof(file)) {
fread(&s,sizeof(struct star),1,file);
if (s.mag>mmin)
continue;
forward(img.ra0,img.de0,s.ra,s.de,&rx,&ry);
x=img.x0+1.0/sx*(cos(angle*D2R)*rx+sin(angle*D2R)*ry);
y=img.y0+1.0/sy*(-sin(angle*D2R)*rx+cos(angle*D2R)*ry);
if (x>0.0 && x<(float) img.naxis1 && y>0.0 && y<(float) img.naxis2) {
c.x[i]=x;
c.y[i]=y;
c.rx[i]=rx;
c.ry[i]=ry;
c.ra[i]=s.ra;
c.de[i]=s.de;
c.mag[i]=s.mag;
c.select[i]=0;
if (i>=NMAX)
break;
i++;
}
}
fclose(file);
c.n=i;
return c;
}
// Select nearest object
int select_nearest(struct catalog c,float x,float y)
{
int i,imin;
float r,rmin;
for (i=0;i<c.n;i++) {
r=sqrt(pow(x-c.x[i],2)+pow(y-c.y[i],2));
if (i==0 || r<rmin) {
imin=i;
rmin=r;
}
}
return imin;
}
// Fit transformation
void fit_transformation(struct catalog cat,struct catalog ast,int nselect)
{
int i,j;
float *x,*y,*rx,*ry;
x=(float *) malloc(sizeof(float)*nselect);
y=(float *) malloc(sizeof(float)*nselect);
rx=(float *) malloc(sizeof(float)*nselect);
ry=(float *) malloc(sizeof(float)*nselect);
for (i=0;i<nselect;i++) {
for (j=0;j<cat.n;j++) {
if (cat.select[j]==i+1) {
x[i]=cat.x[j]-img.x0;
y[i]=cat.y[j]-img.y0;
}
}
for (j=0;j<ast.n;j++) {
if (ast.select[j]==i+1) {
rx[i]=ast.rx[j];
ry[i]=ast.ry[j];
}
}
}
lfit2d(x,y,rx,nselect,img.a);
lfit2d(x,y,ry,nselect,img.b);
return;
}
// Linear 2D fit
void lfit2d(float *x,float *y,float *z,int n,float *a)
{
int i;
double chisq;
gsl_matrix *X,*cov;
gsl_vector *yy,*w,*c;
X=gsl_matrix_alloc(n,3);
yy=gsl_vector_alloc(n);
w=gsl_vector_alloc(n);
c=gsl_vector_alloc(3);
cov=gsl_matrix_alloc(3,3);
// Fill matrices
for(i=0;i<n;i++) {
gsl_matrix_set(X,i,0,1.0);
gsl_matrix_set(X,i,1,x[i]);
gsl_matrix_set(X,i,2,y[i]);
gsl_vector_set(yy,i,z[i]);
gsl_vector_set(w,i,1.0);
}
// Do fit
gsl_multifit_linear_workspace *work=gsl_multifit_linear_alloc(n,3);
gsl_multifit_wlinear(X,w,yy,c,cov,&chisq,work);
gsl_multifit_linear_free(work);
// Save parameters
for (i=0;i<3;i++)
a[i]=gsl_vector_get(c,(i));
gsl_matrix_free(X);
gsl_vector_free(yy);
gsl_vector_free(w);
gsl_vector_free(c);
gsl_matrix_free(cov);
return;
}
// Read astrometric catalog
struct catalog reread_astrometric_catalog(char *filename,float mmin)
{
int i=0;
FILE *file;
char line[LIM];
struct catalog c;
double rx,ry,x,y;
struct star s;
double d,dx,dy,ra,de;
double mjd0=51544.5;
file=fopen(filename,"rb");
while (!feof(file)) {
fread(&s,sizeof(struct star),1,file);
if (s.mag>mmin)
continue;
forward(img.ra0,img.de0,s.ra,s.de,&rx,&ry);
dx=rx-img.a[0];
dy=ry-img.b[0];
d=img.a[1]*img.b[2]-img.a[2]*img.b[1];
x=(img.b[2]*dx-img.a[2]*dy)/d+img.x0;
y=(img.a[1]*dy-img.b[1]*dx)/d+img.y0;
if (x>0.0 && x<img.naxis1 && y>0.0 && y<img.naxis2) {
c.x[i]=x;
c.y[i]=y;
c.rx[i]=rx;
c.ry[i]=ry;
c.ra[i]=s.ra;
c.de[i]=s.de;
c.mag[i]=s.mag;
c.select[i]=0;
i++;
}
}
fclose(file);
c.n=i;
return c;
}
int match_catalogs(struct catalog *cat,struct catalog *ast,float rmax)
{
int i,j,jmin,n,flag=0;
float r,rmin;
FILE *file;
// Reset
for (i=0;i<cat->n;i++)
cat->select[i]=0;
for (i=0;i<ast->n;i++)
ast->select[i]=0;
file=fopen("out.dat","w");
for (i=0,n=0;i<cat->n;i++) {
for (j=0,flag=0;j<ast->n;j++) {
if (ast->select[j]!=0)
continue;
r=sqrt(pow(cat->x[i]-ast->x[j],2)+pow(cat->y[i]-ast->y[j],2));
if (flag==0 || r<rmin) {
rmin=r;
jmin=j;
flag=1;
}
}
if (rmin<rmax) {
fprintf(file,"%10.4f %10.4f %10.6f %10.6f\n",cat->x[i]-img.x0,cat->y[i]-img.y0,ast->ra[jmin],ast->de[jmin]);
cat->select[i]=n+1;
ast->select[jmin]=n+1;
n++;
}
}
fclose(file);
printf("%d stars matched\n",n);
return n;
}
// Convert Sexagesimal into Decimal
double sex2dec(char *s)
{
double x;
float deg,min,sec;
char t[LIM];
strcpy(t,s);
deg=fabs(atof(strtok(t," :")));
min=fabs(atof(strtok(NULL," :")));
sec=fabs(atof(strtok(NULL," :")));
x=(double) deg+(double) min/60.+(double) sec/3600.;
if (s[0]=='-') x= -x;
return x;
}