forked from acbecker/hotpants
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alard.c
1552 lines (1269 loc) · 50.4 KB
/
alard.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
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
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include<stdio.h>
#include<string.h>
#include<math.h>
#if !defined(__MACH__)
#include <malloc.h>
#endif
#if defined(__MACH__)
#include <stdlib.h>
#endif
#include<stdlib.h>
#include<fitsio.h>
#include "defaults.h"
#include "globals.h"
#include "functions.h"
/*
Several of these subroutines appear originally in code created by
Cristophe Alard for ISIS, but have been modified and/or rewritten
for the current software package. In particular, the construction
of the least squares matrices have been taken directly from the ISIS
code.
08/20/01 [email protected]
*/
void getKernelVec() {
/*****************************************************
* Fills kernel_vec with kernel weight filter, called only once
*****************************************************/
int ig, idegx, idegy, nvec;
int ren;
nvec = 0;
for (ig = 0; ig < ngauss; ig++) {
for (idegx = 0; idegx <= deg_fixe[ig]; idegx++) {
for (idegy = 0; idegy <= deg_fixe[ig]-idegx; idegy++) {
/* stores kernel weight mask for each order */
kernel_vec[nvec] = kernel_vector(nvec, idegx, idegy, ig, &ren);
nvec++;
}
}
}
}
int fillStamp(stamp_struct *stamp, float *imConv, float *imRef) {
/*****************************************************
* Fills stamp->vectors with convolved images, and
* pixel indices multiplied by each other for background fit
*****************************************************/
int ren = 0;
int i,j,xi,yi,dx,dy,idegx,idegy,di,dj,nv,ig,nvec;
double ax,ay,xf,yf;
double *im;
float rPixX2, rPixY2;
rPixX2 = 0.5 * rPixX;
rPixY2 = 0.5 * rPixY;
if (verbose >= 1)
fprintf(stderr, " xs : %4i ys : %4i sig: %6.3f sscnt: %4i nss: %4i \n",
stamp->x, stamp->y, stamp->chi2, stamp->sscnt, stamp->nss);
if (stamp->sscnt >= stamp->nss) {
/* have gone through all the good substamps, reject this stamp */
/*if (verbose >= 2) fprintf(stderr, " ******** REJECT stamp (out of substamps)\n");*/
if (verbose >= 1)
fprintf(stderr, " Reject stamp\n");
return 1;
}
nvec = 0;
for (ig = 0; ig < ngauss; ig++) {
for (idegx = 0; idegx <= deg_fixe[ig]; idegx++) {
for (idegy = 0; idegy <= deg_fixe[ig]-idegx; idegy++) {
ren = 0;
dx = (idegx / 2) * 2 - idegx;
dy = (idegy / 2) * 2 - idegy;
if (dx == 0 && dy == 0 && nvec > 0)
ren = 1;
/* fill stamp->vectors[nvec] with convolved image */
/* image is convolved with functional form of kernel, fit later for amplitude */
xy_conv_stamp(stamp, imConv, nvec, ren);
++nvec;
}
}
}
/* get the krefArea data */
if (cutSStamp(stamp, imRef))
return 1;
/* fill stamp->vectors[nvec+++] with x^(bg) * y^(bg) for background fit */
xi = stamp->xss[stamp->sscnt];
yi = stamp->yss[stamp->sscnt];
di = xi - hwKSStamp;
dj = yi - hwKSStamp;
for (i = xi - hwKSStamp; i <= xi + hwKSStamp; i++) {
xf = (i - rPixX2) / rPixX2;
for (j = yi - hwKSStamp; j <= yi + hwKSStamp; j++) {
/* fprintf(stderr, "%d %d %d %d %d %d\n", k, xi, yi,i, j, fwKSStamp); */
yf = (j - rPixY2) / rPixY2;
ax = 1.0;
nv = nvec;
for (idegx = 0; idegx <= bgOrder; idegx++) {
ay = 1.0;
for (idegy = 0; idegy <= bgOrder - idegx; idegy++) {
im = stamp->vectors[nv];
im[i-di+fwKSStamp*(j-dj)] = ax * ay;
ay *= yf;
++nv;
}
ax *= xf;
}
}
}
/* build stamp->mat from stamp->vectors */
build_matrix0(stamp);
/* build stamp->scprod from stamp->vectors and imRef */
build_scprod0(stamp, imRef);
return 0;
}
double *kernel_vector(int n, int deg_x, int deg_y, int ig, int *ren) {
/*****************************************************
* Creates kernel sized entry for kernel_vec for each kernel degree
* Mask of filter_x * filter_y, filter = exp(-x**2 sig) * x^deg
* Subtract off kernel_vec[0] if n > 0
* NOTE: this does not use any image
******************************************************/
double *vector=NULL,*kernel0=NULL;
int i,j,k,dx,dy,ix;
double sum_x,sum_y,x,qe;
if (usePCA) {
return kernel_vector_PCA(n, deg_x, deg_y, ig, ren);
}
vector = (double *)malloc(fwKernel*fwKernel*sizeof(double));
dx = (deg_x / 2) * 2 - deg_x;
dy = (deg_y / 2) * 2 - deg_y;
sum_x = sum_y = 0.0;
*ren = 0;
for (ix = 0; ix < fwKernel; ix++) {
x = (double)(ix - hwKernel);
k = ix+n*fwKernel;
qe = exp(-x * x * sigma_gauss[ig]);
filter_x[k] = qe * pow(x, deg_x);
filter_y[k] = qe * pow(x, deg_y);
sum_x += filter_x[k];
sum_y += filter_y[k];
}
if (n > 0)
kernel0 = kernel_vec[0];
sum_x = 1. / sum_x;
sum_y = 1. / sum_y;
if (dx == 0 && dy == 0) {
for (ix = 0; ix < fwKernel; ix++) {
filter_x[ix+n*fwKernel] *= sum_x;
filter_y[ix+n*fwKernel] *= sum_y;
}
for (i = 0; i < fwKernel; i++) {
for (j = 0; j < fwKernel; j++) {
vector[i+fwKernel*j] = filter_x[i+n*fwKernel] * filter_y[j+n*fwKernel];
}
}
if (n > 0) {
for (i = 0; i < fwKernel * fwKernel; i++) {
vector[i] -= kernel0[i];
}
*ren = 1;
}
} else {
for (i = 0; i < fwKernel; i++) {
for (j = 0; j < fwKernel; j++) {
vector[i+fwKernel*j] = filter_x[i+n*fwKernel] * filter_y[j+n*fwKernel];
}
}
}
return vector;
}
double *kernel_vector_PCA(int n, int deg_x, int deg_y, int ig, int *ren) {
/*****************************************************
* Creates kernel sized entry for kernel_vec for each kernel degree
* Mask of filter_x * filter_y, filter = exp(-x**2 sig) * x^deg
* Subtract off kernel_vec[0] if n > 0
* NOTE: this does not use any image
******************************************************/
double *vector=NULL,*kernel0=NULL;
int i,j;
vector = (double *)malloc(fwKernel*fwKernel*sizeof(double));
for (i = 0; i < fwKernel; i++) {
for (j = 0; j < fwKernel; j++) {
vector[i+fwKernel*j] = PCA[n][i+fwKernel*j];
}
}
if (n > 0)
kernel0 = kernel_vec[0];
if (n > 0) {
for (i = 0; i < fwKernel * fwKernel; i++) {
vector[i] -= kernel0[i];
}
*ren = 1;
}
return vector;
}
void xy_conv_stamp(stamp_struct *stamp, float *image, int n, int ren) {
/*****************************************************
* Called for each degree of convolution, ngauss by deg_gauss
* Each convolution is stored in stamp->vectors[n], imc here
******************************************************/
int i,j,xc,yc,xij,sub_width,xi,yi;
double *v0,*imc;
if (usePCA) {
xy_conv_stamp_PCA(stamp, image, n, ren);
return;
}
xi = stamp->xss[stamp->sscnt];
yi = stamp->yss[stamp->sscnt];
imc = stamp->vectors[n];
sub_width = fwKSStamp + fwKernel - 1;
/* pull area to convolve out of full reference image region */
/* convolve with y filter */
for(i = xi - hwKSStamp - hwKernel; i <= xi + hwKSStamp + hwKernel; i++) {
for(j = yi - hwKSStamp; j <= yi + hwKSStamp; j++) {
xij = i - xi + sub_width / 2 + sub_width * (j - yi + hwKSStamp);
temp[xij] = 0.0;
for(yc = -hwKernel; yc <= hwKernel; yc++) {
temp[xij] += image[i+rPixX*(j+yc)] * filter_y[hwKernel-yc+n*fwKernel];
}
}
}
/* convolve with x filter */
for(j = -hwKSStamp; j <= hwKSStamp; j++) {
for(i = -hwKSStamp; i <= hwKSStamp;i++) {
xij = i + hwKSStamp + fwKSStamp * (j + hwKSStamp);
imc[xij] = 0.0;
for(xc = -hwKernel; xc <= hwKernel; xc++) {
imc[xij] += temp[i+xc+sub_width/2+sub_width*(j+hwKSStamp)] * filter_x[hwKernel-xc+n*fwKernel];
}
}
}
if (ren) {
v0 = stamp->vectors[0];
for(i = 0; i < fwKSStamp * fwKSStamp; i++) imc[i] -= v0[i];
}
return;
}
void xy_conv_stamp_PCA(stamp_struct *stamp, float *image, int n, int ren) {
/*****************************************************
* Called for each degree of convolution, ngauss by deg_gauss
* Each convolution is stored in stamp->vectors[n], imc here
******************************************************/
int i,j,xc,yc,xij,xi,yi;
double *v0,*imc;
xi = stamp->xss[stamp->sscnt];
yi = stamp->yss[stamp->sscnt];
imc = stamp->vectors[n];
/* pull area to convolve out of full reference image region */
for(j = yi - hwKSStamp; j <= yi + hwKSStamp; j++) {
for(i = xi - hwKSStamp; i <= xi + hwKSStamp; i++) {
xij = i - (xi - hwKSStamp) + fwKSStamp * (j - (yi - hwKSStamp));
imc[xij] = 0.;
for(yc = -hwKernel; yc <= hwKernel; yc++) {
for(xc = -hwKernel; xc <= hwKernel; xc++) {
imc[xij] += image[(i+xc)+rPixX*(j+yc)] * PCA[n][(xc+hwKernel) + fwKernel*(yc+hwKernel)];
}
}
}
}
if (ren) {
v0 = stamp->vectors[0];
for(i = 0; i < fwKSStamp * fwKSStamp; i++) imc[i] -= v0[i];
}
return;
}
void fitKernel(stamp_struct *stamps, float *imRef, float *imConv, float *imNoise, double *kernelSol,
double *meansigSubstamps, double *scatterSubstamps, int *NskippedSubstamps) {
/*****************************************************
* Complete fit for kernel solution
*****************************************************/
double d, **matrix;
char check;
int i,mat_size;
int ncomp1, ncomp2, ncomp, nbg_vec;
ncomp1 = nCompKer - 1;
ncomp2 = ((kerOrder + 1) * (kerOrder + 2)) / 2;
ncomp = ncomp1 * ncomp2;
nbg_vec = ((bgOrder + 1) * (bgOrder + 2)) / 2;
mat_size = ncomp1 * ncomp2 + nbg_vec + 1;
if (verbose >= 2) fprintf(stderr, " Mat_size: %i ncomp2: %i ncomp1: %i nbg_vec: %i \n",
mat_size, ncomp2, ncomp1, nbg_vec);
/* allocate fitting matrix */
matrix = (double **)malloc((mat_size + 1)*sizeof(double *));
for (i = 0; i <= mat_size; i++)
matrix[i] = (double *)malloc((mat_size + 1)*sizeof(double));
/* allocate weight matrix */
wxy = (double **)malloc(nS*sizeof(double *));
for (i = 0; i < nS; i++)
wxy[i] = (double *)malloc(ncomp2*sizeof(double));
if (verbose>=2) fprintf(stderr, " Expanding Matrix For Full Fit\n");
build_matrix(stamps, nS, matrix);
build_scprod(stamps, nS, imRef, kernelSol);
ludcmp(matrix, mat_size, indx, &d);
lubksb(matrix, mat_size, indx, kernelSol);
if (verbose>=2) fprintf(stderr, " Checking again\n");
check = check_again(stamps, kernelSol, imConv, imRef, imNoise, meansigSubstamps, scatterSubstamps, NskippedSubstamps);
while(check) {
fprintf(stderr, "\n Re-Expanding Matrix\n");
build_matrix(stamps, nS, matrix);
build_scprod(stamps, nS, imRef, kernelSol);
ludcmp(matrix, mat_size, indx, &d);
lubksb(matrix, mat_size, indx, kernelSol);
fprintf(stderr, " Checking again\n");
check = check_again(stamps, kernelSol, imConv, imRef, imNoise, meansigSubstamps, scatterSubstamps, NskippedSubstamps);
}
fprintf(stderr, " Sigma clipping of bad stamps converged, kernel determined\n");
for (i = 0; i <= mat_size; i++)
free(matrix[i]);
for (i = 0; i < nS; i++)
free(wxy[i]);
free(matrix);
free(wxy);
return;
}
void build_matrix0(stamp_struct *stamp) {
/*****************************************************
* Build least squares matrix for each stamp
*****************************************************/
int i,j,pixStamp,k,i1,ivecbg=0;
int ncomp1, ncomp2, ncomp, nbg_vec;
double p0,q;
double **vec;
ncomp1 = nCompKer;
ncomp2 = ((kerOrder + 1) * (kerOrder + 2)) / 2;
ncomp = ncomp1 * ncomp2;
nbg_vec = ((bgOrder + 1) * (bgOrder + 2)) / 2;
pixStamp = fwKSStamp * fwKSStamp;
vec = stamp->vectors;
/* loop over the convolved images created by xy_conv_stamp() */
/* each level represents ngauss and deg_gauss */
for (i = 0; i < ncomp1; i++) {
for (j = 0; j <= i; j++) {
q = 0.0;
/* integrate W_m1 and W_m2 (sum over all pixels) */
for (k = 0; k < pixStamp; k++)
q += vec[i][k] * vec[j][k];
/* Q from Eqn 3. in Alard */
stamp->mat[i+1][j+1] = q;
}
}
for (i1 = 0; i1 < ncomp1; i1++) {
ivecbg = ncomp1;
p0 = 0.0;
/* integrate convolved images and first order background (equals 1 everywhere!)*/
for (k = 0; k < pixStamp; k++)
p0 += vec[i1][k] * vec[ivecbg][k];
stamp->mat[ncomp1+1][i1+1] = p0;
}
/* integrate first order background with itself */
/* NOTE : DON'T MASK K HERE - BACKGROUND! */
for (k = 0, q = 0.0; k < pixStamp; k++)
q += vec[ivecbg][k] * vec[ncomp1][k];
stamp->mat[ncomp1+1][ncomp1+1] = q;
return;
}
void build_scprod0(stamp_struct *stamp, float *image) {
/*****************************************************
* Build the right side of each stamp's least squares matrix
* stamp.scprod = degree of kernel fit + 1 bg term
*****************************************************/
int xc,yc,xi,yi,i1,k;
int ncomp1, ncomp2, ncomp, nbg_vec;
double p0,q;
double **vec;
ncomp1 = nCompKer;
ncomp2 = ((kerOrder + 1) * (kerOrder + 2)) / 2;
ncomp = ncomp1 * ncomp2;
nbg_vec = ((bgOrder + 1) * (bgOrder + 2)) / 2;
vec = stamp->vectors;
xi = stamp->xss[stamp->sscnt];
yi = stamp->yss[stamp->sscnt];
/* Do eqn 4. in Alard */
/* Multiply each order's convolved image with reference image */
for (i1 = 0; i1 < ncomp1; i1++) {
p0 = 0.0;
for (xc = -hwKSStamp; xc <= hwKSStamp; xc++) {
for (yc = -hwKSStamp; yc <= hwKSStamp; yc++) {
k = xc + hwKSStamp + fwKSStamp * (yc + hwKSStamp);
p0 += vec[i1][k] * image[xc+xi+rPixX*(yc+yi)];
}
}
stamp->scprod[i1+1] = p0;
}
/* Multiply first order background model with reference image */
q = 0.0;
for (xc = -hwKSStamp; xc <= hwKSStamp; xc++) {
for (yc = -hwKSStamp; yc <= hwKSStamp; yc++) {
k = xc + hwKSStamp + fwKSStamp * (yc + hwKSStamp);
q += vec[ncomp1][k] * image[xc+xi+rPixX*(yc+yi)];
}
}
stamp->scprod[ncomp1+1] = q;
return;
}
double check_stamps(stamp_struct *stamps, int nS, float *imRef, float *imNoise) {
/*****************************************************
* Fit each stamp independently, reject significant outliers
* Next fit good stamps globally
* Returns a merit statistic, smaller for better fits
*****************************************************/
int nComps,i,im,jm,mcnt1,mcnt2,mcnt3;
double d,sum=0,kmean,kstdev;
double merit1,merit2,merit3,sig1,sig2,sig3;
float *m1,*m2,*m3,*ks;
int xc, yc, nks;
double **matrix;
int mat_size;
int ncomp1, ncomp2, ncomp, nbg_vec;
int ntestStamps;
double *testKerSol = NULL;
stamp_struct *testStamps = NULL;
/* kernel sum */
ks = (float *)calloc(nS, sizeof(float));
nks = 0;
ncomp1 = nCompKer - 1;
ncomp2 = ((kerOrder + 1) * (kerOrder + 2)) / 2;
ncomp = ncomp1 * ncomp2;
nbg_vec = ((bgOrder + 1) * (bgOrder + 2)) / 2;
mat_size = ncomp1 * ncomp2 + nbg_vec + 1;
if (verbose>=2) fprintf(stderr, " Mat_size0: %i ncomp2: %i ncomp1: %i nbg_vec: %i \n"
, mat_size, ncomp2, ncomp1, nbg_vec);
/* for inital fit */
nComps = nCompKer + 1;
for (i = 0; i < nS; i++) {
xc = stamps[i].xss[stamps[i].sscnt];
yc = stamps[i].yss[stamps[i].sscnt];
/* extract check_mat to solve one particular stamp */
for (im = 1; im <= nComps; im++) {
check_vec[im] = stamps[i].scprod[im];
for (jm = 1; jm <= im; jm++) {
check_mat[im][jm] = stamps[i].mat[im][jm];
check_mat[jm][im] = check_mat[im][jm];
}
}
/* fit stamp, the constant kernel coefficients end up in check_vec */
ludcmp(check_mat,nComps,indx,&d);
lubksb(check_mat,nComps,indx,check_vec);
/* find kernel sum */
sum = check_vec[1];
check_stack[i] = sum;
stamps[i].norm = sum;
ks[nks++] = sum;
if (verbose >= 2) fprintf(stderr, " # %d xss: %4i yss: %4i ksum: %f\n", i,
stamps[i].xss[stamps[i].sscnt],
stamps[i].yss[stamps[i].sscnt], sum);
}
sigma_clip(ks, nks, &kmean, &kstdev, 10);
fprintf(stderr, " %.1f sigma clipped mean ksum : %.3f, stdev : %.3f, n : %i\n",
kerSigReject, kmean, kstdev, nks);
/* so we need some way to reject bad stamps here in the first test,
we decided to use kernel sum. is there a better way? part of
the trick is that if some things are variable, you get different
kernel sums, but the subtraction itself should come out ok. */
/* stamps.diff : delta ksum in sigma */
/* here we want to reject high sigma points on the HIGH and LOW
side, since we want things with the same normalization */
for (i = 0; i < nS; i++) {
stamps[i].diff = fabs((stamps[i].norm - kmean) / kstdev);
}
/*****************************************************
* Global fit for kernel solution
*****************************************************/
/* do only if necessary */
if ((strncmp(forceConvolve, "b", 1)==0)) {
/* allocate fitting matrix */
matrix = (double **)calloc((mat_size + 1), sizeof(double *));
for (i = 0; i <= mat_size; i++)
matrix[i] = (double *)calloc((mat_size + 1), sizeof(double));
/* allocate weight matrix */
wxy = (double **)calloc(nS, sizeof(double *));
for (i = 0; i < nS; i++)
wxy[i] = (double *)calloc(ncomp2, sizeof(double));
/* first find out how many good stamps to allocate */
ntestStamps = 0;
for (i = 0; i < nS; i++)
if (stamps[i].diff < kerSigReject) {
ntestStamps++;
}
else {
if (verbose >= 2) fprintf(stderr, " # %d skipping xss: %4i yss: %4i ksum: %f sigma: %f\n", i,
stamps[i].xss[stamps[i].sscnt],
stamps[i].yss[stamps[i].sscnt],
stamps[i].norm, stamps[i].diff);
}
/* then allocate test stamp structure */
if(!(testStamps = (stamp_struct *)calloc(ntestStamps, sizeof(stamp_struct)))) {
printf("Cannot Allocate Test Stamp List\n");
exit (1);
}
testKerSol = (double *)calloc((nCompTotal+1), sizeof(double));
/* and point test stamp structure to good stamps */
ntestStamps = 0;
for (i = 0; i < nS; i++)
if (stamps[i].diff < kerSigReject)
testStamps[ntestStamps++] = stamps[i];
/* finally do fit */
if (verbose >= 2) fprintf(stderr, " Expanding Test Matrix For Fit\n");
build_matrix(testStamps, ntestStamps, matrix);
build_scprod(testStamps, ntestStamps, imRef, testKerSol);
ludcmp(matrix, mat_size, indx, &d);
lubksb(matrix, mat_size, indx, testKerSol);
/* get the kernel sum to normalize figures of merit! */
kmean = make_kernel(0, 0, testKerSol);
/* determine figure of merit from good stamps */
/* average of sum (diff**2 / value), ~variance */
m1 = (float *)calloc(ntestStamps, sizeof(float));
/* standard deviation of pixel distribution */
m2 = (float *)calloc(ntestStamps, sizeof(float));
/* noise sd based on histogram distribution width */
m3 = (float *)calloc(ntestStamps, sizeof(float));
mcnt1 = 0;
mcnt2 = 0;
mcnt3 = 0;
for (i = 0; i < ntestStamps; i++) {
getStampSig(&testStamps[i], testKerSol, imNoise, &sig1, &sig2, &sig3);
if ((sig1 != -1) && (sig1 <= MAXVAL)) {
m1[mcnt1++] = sig1;
}
if ((sig2 != -1) && (sig2 <= MAXVAL)) {
m2[mcnt2++] = sig2;
}
if ((sig3 != -1) && (sig3 <= MAXVAL)) {
m3[mcnt3++] = sig3;
}
}
sigma_clip(m1, mcnt1, &merit1, &sig1, 10);
sigma_clip(m2, mcnt2, &merit2, &sig2, 10);
sigma_clip(m3, mcnt3, &merit3, &sig3, 10);
/* normalize by kernel sum */
merit1 /= kmean;
merit2 /= kmean;
merit3 /= kmean;
/* clean up this mess */
if (testKerSol) free(testKerSol);
if (testStamps) free(testStamps);
for (i = 0; i <= mat_size; i++)
free(matrix[i]);
for (i = 0; i < nS; i++)
free(wxy[i]);
free(matrix);
free(wxy);
free(m1);
free(m2);
free(m3);
free(ks);
/* average value of figures of merit across stamps */
fprintf(stderr, " <var_merit> = %.3f, <sd_merit> = %.3f, <hist_merit> = %.3f\n", merit1, merit2, merit3);
/* return what is asked for if possible, if not use backup */
if (strncmp(figMerit, "v", 1)==0) {
if (mcnt1 > 0) {
return merit1;
}
else if (mcnt2 > 0) {
return merit2;
}
else if (mcnt3 > 0) {
return merit3;
}
else {
return 666;
}
}
else if (strncmp(figMerit, "s", 1)==0) {
if (mcnt2 > 0) {
return merit2;
}
else if (mcnt1 > 0) {
return merit1;
}
else if (mcnt3 > 0) {
return merit3;
}
else {
return 666;
}
}
else if (strncmp(figMerit, "h", 1)==0) {
if (mcnt3 > 0) {
return merit3;
}
else if (mcnt1 > 0) {
return merit1;
}
else if (mcnt2 > 0) {
return merit2;
}
else {
return 666;
}
}
}
else
return 0;
return 0;
}
void build_matrix_new(stamp_struct *stamps, int nS, double **matrix) {
/*****************************************************
* Build overall matrix including spatial variations
*****************************************************/
int mat_size,i,j,pixStamp,istamp,k,i1,i2,j1,j2,ibg,jbg,ivecbg,jj;
int ncomp1, ncomp2, ncomp, nbg_vec;
double **matrix0,p0,q,fx,fy;
double **vec;
int ideg1, ideg2, xstamp, ystamp;
double a1, a2;
ncomp1 = nCompKer - 1;
ncomp2 = ((kerOrder + 1) * (kerOrder + 2)) / 2;
ncomp = ncomp1 * ncomp2;
nbg_vec = ((bgOrder + 1) * (bgOrder + 2)) / 2;
pixStamp = fwKSStamp * fwKSStamp;
mat_size = ncomp1 * ncomp2 + nbg_vec + 1;
if (verbose >= 2) fprintf(stderr, " Mat_size: %i ncomp2: %i ncomp1: %i nbg_vec: %i \n",mat_size,ncomp2,ncomp1,nbg_vec);
for(i = 0; i <= mat_size; i++)
for(j = 0; j <= mat_size; j++)
matrix[i][j] = 0.0;
for(i = 0; i < nS; i++)
for(j = 0; j < ncomp2; j++)
wxy[i][j] = 0.0;
for (istamp = 0; istamp < nS; istamp++) {
/* skip over any bad stamps along the way */
while(stamps[istamp].sscnt >= stamps[istamp].nss) {
++istamp;
if (istamp >= nS) break;
}
if (istamp >= nS) break;
vec = stamps[istamp].vectors;
xstamp = stamps[istamp].xss[stamps[istamp].sscnt];
ystamp = stamps[istamp].yss[stamps[istamp].sscnt];
/* build weight function *HERE*, implicitly giving bad stamps zero weight */
/* because we skip them over above... */
k = 0;
fx = (xstamp - rPixX/2) / rPixX/2;
fy = (ystamp - rPixY/2) / rPixY/2;
for (ideg1 = 0, a1 = 1.0; ideg1 <= kerOrder; ideg1++, a1 *= fx)
for (ideg2 = 0, a2 = 1.0; ideg2 <= kerOrder - ideg1; ideg2++, a2 *= fy)
wxy[istamp][k++] = a1 * a2;
matrix0 = stamps[istamp].mat;
for (i = 0; i < ncomp; i++) {
i1 = i / ncomp2;
i2 = i - i1 * ncomp2;
for (j = 0; j <= i; j++) {
j1 = j / ncomp2;
j2 = j - j1 * ncomp2;
/* spatially weighted W_m1 and W_m2 integrals */
matrix[i+2][j+2] += wxy[istamp][i2] * wxy[istamp][j2] * matrix0[i1+2][j1+2];
}
}
matrix[1][1] += matrix0[1][1];
for (i = 0; i < ncomp; i++) {
i1 = i / ncomp2;
i2 = i - i1 * ncomp2;
matrix[i+2][1] += wxy[istamp][i2] * matrix0[i1+2][1];
}
for (ibg = 0; ibg < nbg_vec; ibg++) {
i = ncomp + ibg + 1;
ivecbg = ncomp1 + ibg + 1;
for (i1 = 1; i1 < ncomp1 + 1; i1++) {
p0 = 0.0;
/* integrate convolved images over all order backgrounds */
for (k = 0; k < pixStamp; k++)
p0 += vec[i1][k] * vec[ivecbg][k];
/* spatially weighted image * background terms */
for (i2 = 0; i2 < ncomp2; i2++) {
jj = (i1 - 1) * ncomp2 + i2 + 1;
matrix[i+1][jj+1] += p0 * wxy[istamp][i2];
}
}
p0 = 0.0;
for (k = 0; k < pixStamp; k++)
p0 += vec[0][k] * vec[ivecbg][k];
matrix[i+1][1] += p0;
/* background * background */
for (jbg = 0;jbg <= ibg; jbg++) {
for (k = 0, q = 0.0; k < pixStamp; k++)
q += vec[ivecbg][k] * vec[ncomp1+jbg+1][k];
matrix[i+1][ncomp+jbg+2] += q;
}
}
}
/* fill lower half of matrix */
for (i = 0; i < mat_size; i++) {
for (j = 0; j <= i; j++) {
matrix[j+1][i+1] = matrix[i+1][j+1];
/* fprintf(stderr, "matrix[%i][%i]: %lf\n", i,j,matrix[i+1][j+1]); */
}
}
return;
}
void build_matrix(stamp_struct *stamps, int nS, double **matrix) {
/*****************************************************
* Build overall matrix including spatial variations
*****************************************************/
int mat_size,i,j,pixStamp,istamp,k,i1,i2,j1,j2,ibg,jbg,ivecbg,jj;
int ncomp1, ncomp2, ncomp, nbg_vec;
double **matrix0,p0,q;
double **vec;
float rPixX2, rPixY2;
int ideg1, ideg2, xstamp, ystamp;
double a1, a2, fx, fy;
ncomp1 = nCompKer - 1;
ncomp2 = ((kerOrder + 1) * (kerOrder + 2)) / 2;
ncomp = ncomp1 * ncomp2;
nbg_vec = ((bgOrder + 1) * (bgOrder + 2)) / 2;
pixStamp = fwKSStamp * fwKSStamp;
rPixX2 = 0.5 * rPixX;
rPixY2 = 0.5 * rPixY;
mat_size = ncomp1 * ncomp2 + nbg_vec + 1;
if (verbose >= 2) fprintf(stderr, " Mat_size: %i ncomp2: %i ncomp1: %i nbg_vec: %i \n",mat_size,ncomp2,ncomp1,nbg_vec);
for(i = 0; i <= mat_size; i++)
for(j = 0; j <= mat_size; j++)
matrix[i][j] = 0.0;
for(i = 0; i < nS; i++)
for(j = 0; j < ncomp2; j++)
wxy[i][j] = 0.0;
for (istamp = 0; istamp < nS; istamp++) {
/* skip over any bad stamps along the way */
while(stamps[istamp].sscnt >= stamps[istamp].nss) {
++istamp;
if (istamp >= nS) break;
}
if (istamp >= nS) break;
vec = stamps[istamp].vectors;
xstamp = stamps[istamp].xss[stamps[istamp].sscnt];
ystamp = stamps[istamp].yss[stamps[istamp].sscnt];
/* RANGE FROM -1 to 1 */
fx = (xstamp - rPixX2) / rPixX2;
fy = (ystamp - rPixY2) / rPixY2;
/* build weight function *HERE* */
k = 0;
a1 = 1.0;
for (ideg1 = 0; ideg1 <= kerOrder; ideg1++) {
a2 = 1.0;
for (ideg2 = 0; ideg2 <= kerOrder - ideg1; ideg2++) {
wxy[istamp][k++] = a1 * a2;
a2 *= fy;
}
a1 *= fx;
}
matrix0 = stamps[istamp].mat;
for (i = 0; i < ncomp; i++) {
i1 = i / ncomp2;
i2 = i - i1 * ncomp2;
for (j = 0; j <= i; j++) {
j1 = j / ncomp2;
j2 = j - j1 * ncomp2;
/* spatially weighted W_m1 and W_m2 integrals */
matrix[i+2][j+2] += wxy[istamp][i2] * wxy[istamp][j2] * matrix0[i1+2][j1+2];
}
}
matrix[1][1] += matrix0[1][1];
for (i = 0; i < ncomp; i++) {
i1 = i / ncomp2;
i2 = i - i1 * ncomp2;
matrix[i+2][1] += wxy[istamp][i2] * matrix0[i1+2][1];
}
for (ibg = 0; ibg < nbg_vec; ibg++) {
i = ncomp + ibg + 1;
ivecbg = ncomp1 + ibg + 1;
for (i1 = 1; i1 < ncomp1 + 1; i1++) {
p0 = 0.0;
/* integrate convolved images over all order backgrounds */
for (k = 0; k < pixStamp; k++)
p0 += vec[i1][k] * vec[ivecbg][k];
/* spatially weighted image * background terms */
for (i2 = 0; i2 < ncomp2; i2++) {
jj = (i1 - 1) * ncomp2 + i2 + 1;
matrix[i+1][jj+1] += p0 * wxy[istamp][i2];
}
}
p0 = 0.0;
for (k = 0; k < pixStamp; k++)
p0 += vec[0][k] * vec[ivecbg][k];
matrix[i+1][1] += p0;
/* background * background */
for (jbg = 0;jbg <= ibg; jbg++) {
for (k = 0, q = 0.0; k < pixStamp; k++)
q += vec[ivecbg][k] * vec[ncomp1+jbg+1][k];
matrix[i+1][ncomp+jbg+2] += q;
}
}
}
/* fill lower half of matrix */
for (i = 0; i < mat_size; i++) {
for (j = 0; j <= i; j++) {
matrix[j+1][i+1] = matrix[i+1][j+1];
/* fprintf(stderr, "matrix[%i][%i]: %lf\n", i,j,matrix[i+1][j+1]); */
}
}
return;
}
void build_scprod(stamp_struct *stamps, int nS, float *image, double *kernelSol) {
/*****************************************************
* Build the right side of the complete least squares matrix
*****************************************************/
int istamp,xc,yc,xi,yi,i1,i2,k,ibg,i,ii;
int ncomp1, ncomp2, ncomp, nbg_vec;
double p0,q;
double **vec;
ncomp1 = nCompKer - 1;
ncomp2 = ((kerOrder + 1) * (kerOrder + 2)) / 2;
ncomp = ncomp1 * ncomp2;
nbg_vec = ((bgOrder + 1) * (bgOrder + 2)) / 2;
for (i = 0; i <= ncomp + nbg_vec + 1; i++)
kernelSol[i]=0.0;
for (istamp = 0; istamp < nS; istamp++) {
/* skip over any bad stamps along the way */
while(stamps[istamp].sscnt >= stamps[istamp].nss) {
++istamp;
if (istamp >= nS) break;
}
if (istamp >= nS) break;
vec= stamps[istamp].vectors;
xi = stamps[istamp].xss[stamps[istamp].sscnt];
yi = stamps[istamp].yss[stamps[istamp].sscnt];
p0 = stamps[istamp].scprod[1];
kernelSol[1] += p0;