-
Notifications
You must be signed in to change notification settings - Fork 23
/
functions.c
1726 lines (1387 loc) · 57 KB
/
functions.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>
#include<malloc.h>
#include<stdlib.h>
#include<fitsio.h>
#include<ctype.h>
#include "defaults.h"
#include "globals.h"
#include "functions.h"
/*
Some of these subroutines appear originally in code created by Gary
Bernstein for psfmatch, but have been modified and/or rewritten for
the current software package. In particular, the determination of
image noise statistics has been taken directly from the psfmatch
code.
08/20/01 [email protected]
*/
/* armin */
void loadxyfile(char *filename, int cmpfileflag){
FILE *xyfile;
int Nalloc,c;
char line[SCRLEN];
xyfile = fopen(filename, "r");
fprintf(stderr, "WARNING : INPUT FORMAT HARDCODED : X=1, Y=2; 1-indexed coordinates\n");
if (cmpfileflag) {
for (;;){
c=getc(xyfile);
if (c== EOF) break;
if (c=='\n') break;
}
}
Nalloc=100;
if ( !(xcmp = (float *)malloc(Nalloc*sizeof(float))) ||
!(ycmp = (float *)malloc(Nalloc*sizeof(float))) )
exit(1);
Ncmp=0;
while(fgets(line, SCRLEN, xyfile) != NULL) {
/* reallocate if necessary */
if (Ncmp==Nalloc){
Nalloc+=100;
xcmp = (float *)realloc(xcmp, Nalloc *sizeof(float));
ycmp = (float *)realloc(ycmp, Nalloc *sizeof(float));
}
/* skip poorly formatted lines... */
if (isalpha(line[0])) continue;
if (sscanf(line, "%f %f", &xcmp[Ncmp], &ycmp[Ncmp]) == 2) {
Ncmp++;
}
}
fclose(xyfile);
/* take care of 1-indexing, our arrays are 0-indexed */
for (c = 0; c < Ncmp; c++) {
xcmp[c] -= 1;
ycmp[c] -= 1;
}
}
/*armin*/
void savexy(stamp_struct *stamps, int nStamps, long xmin, long ymin, int regioncounter){
int sscnt,istamp;
FILE *xyfileused,*xyfileall,*xyfileskipped;
char xyfilenameall[1000];
char xyfilenameskipped[1000];
sprintf(xyfilenameall,"%s.all",xyfilename);
sprintf(xyfilenameskipped,"%s.skipped",xyfilename);
if (regioncounter==0) {
xyfileused = fopen(xyfilename, "w");
xyfileall = fopen(xyfilenameall, "w");
xyfileskipped = fopen(xyfilenameskipped, "w");
} else {
xyfileused = fopen(xyfilename, "a");
xyfileall = fopen(xyfilenameall, "a");
xyfileskipped = fopen(xyfilenameskipped, "a");
}
fprintf(xyfileall, "#%4s %4s\n", "X", "Y");
fprintf(xyfileused, "#%4s %4s\n", "X", "Y");
fprintf(xyfileskipped,"#%4s %4s\n", "X", "Y");
/* note single pixel offset for ds9 display compared to 0-index array */
for (istamp = 0; istamp < nStamps; istamp++) {
for (sscnt = 0; sscnt < stamps[istamp].nss; sscnt++) {
/*fprintf(xyfileall, " %4ld %4ld\n", stamps[istamp].xss[sscnt] + xmin, stamps[istamp].yss[sscnt] + ymin);*/
fprintf(xyfileall, "image;box(%4ld,%4ld,%d,%d) # color=yellow\n",
stamps[istamp].xss[sscnt] + xmin + 1,
stamps[istamp].yss[sscnt] + ymin + 1, fwKSStamp, fwKSStamp);
if (sscnt == stamps[istamp].sscnt)
/*fprintf(xyfileused, " %4ld %4ld\n", stamps[istamp].xss[sscnt] + xmin, stamps[istamp].yss[sscnt] + ymin);*/
fprintf(xyfileused, "image;box(%4ld,%4ld,%d,%d) # color=green\n",
stamps[istamp].xss[sscnt] + xmin + 1,
stamps[istamp].yss[sscnt] + ymin + 1, fwKSStamp, fwKSStamp);
if (sscnt < stamps[istamp].sscnt)
/*fprintf(xyfileskipped, " %4ld %4ld\n", stamps[istamp].xss[sscnt] + xmin, stamps[istamp].yss[sscnt] + ymin);*/
fprintf(xyfileskipped, "image;box(%4ld,%4ld,%d,%d) # color=red\n",
stamps[istamp].xss[sscnt] + xmin + 1,
stamps[istamp].yss[sscnt] + ymin + 1, fwKSStamp, fwKSStamp);
}
}
fclose(xyfileskipped);
fclose(xyfileall);
fclose(xyfileused);
}
int allocateStamps(stamp_struct *stamps, int nStamps) {
int i,j;
int nbgVectors;
nbgVectors = ((bgOrder + 1) * (bgOrder + 2)) / 2;
if (stamps) {
for (i = 0; i < nStamps; i++) {
/* **************************** */
if(!(stamps[i].vectors = (double **)calloc((nCompKer+nbgVectors), sizeof(double *))))
return 1;
for (j = 0; j < nCompKer + nbgVectors; j++) {
if(!(stamps[i].vectors[j] = (double *)calloc(fwKSStamp*fwKSStamp, sizeof(double))))
return 1;
}
/* **************************** */
if (!(stamps[i].krefArea = (double *)calloc(fwKSStamp*fwKSStamp, sizeof(double))))
return 1;
/* **************************** */
if (!(stamps[i].mat = (double **)calloc(nC, sizeof(double *))))
return 1;
for (j = 0; j < nC; j++)
if ( !(stamps[i].mat[j] = (double *)calloc(nC, sizeof(double))) )
return 1;
/* **************************** */
if (!(stamps[i].xss = (int *)calloc(nKSStamps, sizeof(int))))
return 1;
/* **************************** */
if (!(stamps[i].yss = (int *)calloc(nKSStamps, sizeof(int))))
return 1;
/* **************************** */
if (!(stamps[i].scprod = (double *)calloc(nC, sizeof(double))))
return 1;
/* **************************** */
stamps[i].x0 = stamps[i].y0 = stamps[i].x = stamps[i].y = 0;
stamps[i].nss = stamps[i].sscnt = 0;
stamps[i].nx = stamps[i].ny = 0;
stamps[i].sum = stamps[i].mean = stamps[i].median = 0;
stamps[i].mode = stamps[i].sd = stamps[i].fwhm = 0;
stamps[i].lfwhm = stamps[i].chi2 = 0;
stamps[i].norm = stamps[i].diff = 0;
}
}
return 0;
}
void buildStamps(int sXMin, int sXMax, int sYMin, int sYMax, int *niS, int *ntS,
int getCenters, int rXBMin, int rYBMin, stamp_struct *ciStamps, stamp_struct *ctStamps,
float *iRData, float *tRData, float hardX, float hardY) {
int sPixX, sPixY;
int xmax, ymax, k, l, nr2;
int nss;
/*int bbitt1=0x100, bbitt2=0x200, bbiti1=0x400, bbiti2=0x800;*/
int bbitt1=FLAG_T_BAD, bbitt2=FLAG_T_SKIP, bbiti1=FLAG_I_BAD, bbiti2=FLAG_I_SKIP;
float *refArea=NULL;
double check;
if (verbose >= 1)
fprintf(stderr, " Stamp in region : %d:%d,%d:%d\n",
sXMin, sXMax, sYMin, sYMax);
/* global vars */
sPixX = sXMax - sXMin + 1;
sPixY = sYMax - sYMin + 1;
if (!(strncmp(forceConvolve, "i", 1)==0)) {
if (ctStamps[*ntS].nss == 0) {
refArea = (float *)calloc(sPixX*sPixY, sizeof(float));
/* temp : store the whole stamp in refArea */
cutStamp(tRData, refArea, rPixX, sXMin-rXBMin, sYMin-rYBMin,
sXMax-rXBMin, sYMax-rYBMin, &ctStamps[*ntS]);
if ( !( getStampStats3(refArea, ctStamps[*ntS].x0, ctStamps[*ntS].y0, sPixX, sPixY,
&ctStamps[*ntS].sum, &ctStamps[*ntS].mean, &ctStamps[*ntS].median,
&ctStamps[*ntS].mode, &ctStamps[*ntS].sd, &ctStamps[*ntS].fwhm,
&ctStamps[*ntS].lfwhm, 0x0, 0xffff, 3))) {
/* pointless */
if (verbose >= 1)
fprintf(stderr, " Tmpl xs : %4i ys : %4i (sky,dsky = %.1f,%.1f)\n",
ctStamps[*ntS].x, ctStamps[*ntS].y, ctStamps[*ntS].mode, ctStamps[*ntS].fwhm);
}
free(refArea);
}
}
if (!(strncmp(forceConvolve, "t", 1)==0)) {
if (ciStamps[*niS].nss == 0) {
refArea = (float *)calloc(sPixX*sPixY, sizeof(float));
/* store the whole of the stamp in .refArea */
cutStamp(iRData, refArea, rPixX, sXMin-rXBMin, sYMin-rYBMin,
sXMax-rXBMin, sYMax-rYBMin, &ciStamps[*niS]);
if ( !( getStampStats3(refArea, ciStamps[*niS].x0, ciStamps[*niS].y0, sPixX, sPixY,
&ciStamps[*niS].sum, &ciStamps[*niS].mean, &ciStamps[*niS].median,
&ciStamps[*niS].mode, &ciStamps[*niS].sd, &ciStamps[*niS].fwhm,
&ciStamps[*niS].lfwhm, 0x0, 0xffff, 3))) {
/* pointless */
/* buildSigMask(&ciStamps[*niS], sPixX, sPixY, misRData); */
if (verbose >= 1)
fprintf(stderr, " Image xs : %4i ys : %4i (sky,dsky = %.1f,%.1f)\n",
ciStamps[*niS].x, ciStamps[*niS].y, ciStamps[*niS].mode, ciStamps[*niS].fwhm);
}
free(refArea);
}
}
if (!(strncmp(forceConvolve, "i", 1)==0)) {
nss = ctStamps[*ntS].nss;
if (getCenters) {
/* get potential centers for the kernel fit */
getPsfCenters(&ctStamps[*ntS], tRData, sPixX, sPixY, tUKThresh, bbitt1, bbitt2);
if (verbose >= 1)
fprintf(stderr, " Tmpl : scnt = %2i nss = %2i\n",
ctStamps[*ntS].sscnt, ctStamps[*ntS].nss);
} else {
/* don't increment ntS inside subroutine, BUT MAKE SURE YOU DO IT OUTSIDE! */
if (nss < nKSStamps) {
if (hardX)
xmax = (int)(hardX);
else
xmax = sXMin + (int)(fwStamp/2);
if (hardY)
ymax = (int)(hardY);
else
ymax = sYMin + (int)(fwStamp/2);
check = checkPsfCenter(tRData, xmax-ctStamps[*ntS].x0, ymax-ctStamps[*ntS].y0, sPixX, sPixY,
ctStamps[*ntS].x0, ctStamps[*ntS].y0, tUKThresh,
ctStamps[*ntS].mode, 1. / ctStamps[*ntS].fwhm,
0, 0, bbitt1 | bbitt2 | 0xbf, bbitt1);
/* its ok? */
if (check != 0.) {
/* globally mask out the region around this guy */
for (l = ymax-hwKSStamp; l <= ymax+hwKSStamp; l++) {
/*yr2 = l + ctStamps[*ntS].y0;*/
for (k = xmax-hwKSStamp; k <= xmax+hwKSStamp; k++) {
/*xr2 = k + ctStamps[*ntS].x0;*/
nr2 = l+rPixX*k;
/*if ((k > 0) && (k < sPixX) && (l > 0) && (l < sPixY))*/
if (nr2 >= 0 && nr2 < rPixX*rPixY)
mRData[nr2] |= bbitt2;
}
}
ctStamps[*ntS].xss[nss] = xmax;
ctStamps[*ntS].yss[nss] = ymax;
ctStamps[*ntS].nss += 1;
if (verbose >= 2) fprintf(stderr," #%d @ %4d,%4d\n", nss, xmax, ymax);
}
}
}
}
if (!(strncmp(forceConvolve, "t", 1)==0)) {
nss = ciStamps[*niS].nss;
if (getCenters) {
/* get potential centers for the kernel fit */
getPsfCenters(&ciStamps[*niS], iRData, sPixX, sPixY, iUKThresh, bbiti1, bbiti2);
if (verbose >= 1)
fprintf(stderr, " Image : scnt = %2i nss = %2i\n",
ciStamps[*niS].sscnt, ciStamps[*niS].nss);
} else {
/* don't increment niS inside subroutine, BUT MAKE SURE YOU DO IT OUTSIDE! */
if (nss < nKSStamps) {
if (hardX)
xmax = (int)(hardX);
else
xmax = sXMin + (int)(fwStamp/2);
if (hardY)
ymax = (int)(hardY);
else
ymax = sYMin + (int)(fwStamp/2);
check = checkPsfCenter(iRData, xmax-ciStamps[*niS].x0, ymax-ciStamps[*niS].y0, sPixX, sPixY,
ciStamps[*niS].x0, ciStamps[*niS].y0, iUKThresh,
ciStamps[*niS].mode, 1. / ciStamps[*niS].fwhm,
0, 0, bbiti1 | bbiti2 | 0xbf, bbiti1);
/* its ok? */
if (check != 0.) {
/* globally mask out the region around this guy */
for (l = ymax-hwKSStamp; l <= ymax+hwKSStamp; l++) {
/*yr2 = l + ciStamps[*niS].y0;*/
for (k = xmax-hwKSStamp; k <= xmax+hwKSStamp; k++) {
/*xr2 = k + ciStamps[*niS].x0;*/
/*nr2 = xr2+rPixX*yr2;*/
nr2 = l+rPixX*k;
/*if ((k > 0) && (k < sPixX) && (l > 0) && (l < sPixY))*/
if (nr2 >= 0 && nr2 < rPixX*rPixY)
mRData[nr2] |= bbiti2;
}
}
ciStamps[*niS].xss[nss] = xmax;
ciStamps[*niS].yss[nss] = ymax;
ciStamps[*niS].nss += 1;
if (verbose >= 2) fprintf(stderr," #%d @ %4d,%4d\n", nss, xmax, ymax);
}
}
}
}
return;
}
void cutStamp(float *data, float *refArea, int dxLen, int xMin, int yMin,
int xMax, int yMax, stamp_struct *stamp) {
int i, j;
int x, y;
int sxLen;
sxLen = xMax - xMin + 1;
/* NOTE, use j <= yMax here, not j < yMax, include yMax point */
for (j = yMin; j <= yMax; j++) {
y = j - yMin;
for (i = xMin; i <= xMax; i++) {
x = i - xMin;
refArea[x+y*sxLen] = data[i+j*dxLen];
/*fprintf(stderr, "%d %d %d %d %f\n", x, y, i, j, data[i+j*dxLen]); */
}
}
stamp->x0 = xMin;
stamp->y0 = yMin;
stamp->x = xMin + (xMax - xMin) / 2;
stamp->y = yMin + (yMax - yMin) / 2;
return;
}
int cutSStamp(stamp_struct *stamp, float *iData) {
/*****************************************************
* NOTE: The Centers here are in the regions' coords
* To grab them from the stamp, adjust using stamp->x0,y0
*****************************************************/
int i, j, k, nss, sscnt;
int x, y, dy, xStamp, yStamp;
float dpt;
double sum = 0.;
/*stamp->krefArea = (double *)realloc(stamp->krefArea, fwKSStamp*fwKSStamp*sizeof(double));*/
dfset(stamp->krefArea, fillVal, fwKSStamp, fwKSStamp);
nss = stamp->nss;
sscnt = stamp->sscnt;
xStamp = stamp->xss[sscnt] - stamp->x0;
yStamp = stamp->yss[sscnt] - stamp->y0;
/* have gone through all the good substamps, reject this stamp */
if (sscnt >= nss) {
if (verbose >= 2)
fprintf(stderr, " xs : %4i ys : %4i sig: %6.3f sscnt: %4i nss: %4i ******** REJECT stamp (out of substamps)\n",
stamp->x, stamp->y, stamp->chi2, sscnt, nss);
else
if (verbose >= 1)
fprintf(stderr, " Reject stamp\n");
return 1;
}
/*
fprintf(stderr, " xs : %4i ys : %4i substamp (sscnt=%d, nss=%d) xss: %4i yss: %4i\n",
stamp->x, stamp->y, sscnt, nss, stamp->xss[sscnt], stamp->yss[sscnt]);
*/
if (verbose >= 1)
fprintf(stderr, " xss : %4i yss : %4i\n", stamp->xss[sscnt], stamp->yss[sscnt]);
for (j = yStamp - hwKSStamp; j <= yStamp + hwKSStamp; j++) {
y = j - (yStamp - hwKSStamp);
dy = j + stamp->y0;
for (i = xStamp - hwKSStamp; i <= xStamp + hwKSStamp; i++) {
x = i - (xStamp - hwKSStamp);
k = i+stamp->x0 + rPixX*dy;
dpt = iData[k];
stamp->krefArea[x+y*fwKSStamp] = dpt;
sum += (mRData[k] & FLAG_INPUT_ISBAD) ? 0 : fabs(dpt);
}
}
stamp->sum = sum;
return 0;
}
double checkPsfCenter(float *iData, int imax, int jmax, int xLen, int yLen,
int sx0, int sy0,
double hiThresh, float sky, float invdsky,
int xbuffer, int ybuffer, int bbit, int bbit1) {
/* note the imax and jmax are in the stamp coordinate system */
int brk, l, k;
int xr2, yr2, nr2;
double dmax2, dpt2;
/* (5). after centroiding, re-check for fill/sat values and overlap (a little inefficient) */
/* zero tolerance for bad pixels! */
brk = 0;
/* since we have zero tolerance for bad pixels, the sum
of all the pixels in this box can be compared to the
sum of all the pixels in the other boxes. rank on
this! well, sum of high sigma pixels anyways...*/
dmax2 = 0.;
for (l = jmax-hwKSStamp; l <= jmax+hwKSStamp; l++) {
if ((l < ybuffer) || (l >= yLen-ybuffer))
continue; /* continue l loop */
yr2 = l + sy0;
for (k = imax-hwKSStamp; k <= imax+hwKSStamp; k++) {
if ((k < xbuffer) || (k >= xLen-xbuffer))
continue; /* continue k loop */
xr2 = k + sx0;
nr2 = xr2+rPixX*yr2;
if (mRData[nr2] & bbit) {
brk = 1;
dmax2 = 0;
break; /* exit k loop */
}
dpt2 = iData[nr2];
if (dpt2 >= hiThresh) {
mRData[nr2] |= bbit1;
brk = 1;
dmax2 = 0;
break; /* exit k loop */
}
if (( (dpt2 - sky) * invdsky) > kerFitThresh)
dmax2 += dpt2;
}
if (brk == 1)
break; /* exit l loop */
}
return dmax2;
}
int getPsfCenters(stamp_struct *stamp, float *iData, int xLen, int yLen, double hiThresh, int bbit1, int bbit2) {
/*****************************************************
* Find the X highest independent maxima in the stamp
* Subject to some stringent cuts
* NOTE : THERE ARE SOME HARDWIRED THINGS IN HERE
*****************************************************/
int i, j, k, l, nr, nr2, imax, jmax, xr, yr, xr2, yr2, sy0, sx0, xbuffer, ybuffer;
double dmax, dmax2, dpt, dpt2, loPsf, floor;
int *xloc, *yloc, *qs, pcnt, brk, bbit, fcnt;
double *peaks;
double sky, invdsky;
float dfrac = 0.9;
if (stamp->nss >= nKSStamps) {
fprintf(stderr," no need for automatic substamp search...\n");
return(0);
}
/* 0xff, but 0x40 is OK = 0xbf */
bbit = bbit1 | bbit2 | 0xbf;
sky = stamp->mode;
invdsky = 1. / stamp->fwhm;
/* the highest peak in the image, period, in case all else fails */
/* default to center of stamp */
sx0 = stamp->x0;
sy0 = stamp->y0;
/*
we can go with no buffer here, but, we need to allocate extra size in the
stamp->refArea to allow this to happen. too much room. removed
refArea from stamp structure, just use actual array.
*/
xbuffer = ybuffer = 0;
/* as low as we will go */
/* was a 2 here before version 4.1.6 */
floor = sky + kerFitThresh * stamp->fwhm;
qs = (int *)calloc(xLen*yLen/(hwKSStamp), sizeof(int));
xloc = (int *)calloc(xLen*yLen/(hwKSStamp), sizeof(int));
yloc = (int *)calloc(xLen*yLen/(hwKSStamp), sizeof(int));
peaks = (double *)calloc(xLen*yLen/(hwKSStamp), sizeof(double));
brk = 0;
pcnt = 0;
fcnt = 2 * nKSStamps; /* maximum number of stamps to find */
while (pcnt < fcnt) {
/* NOTE : we keep the previous iteration's matches if they were good */
loPsf = sky + (hiThresh - sky) * dfrac;
loPsf = (loPsf > floor) ? loPsf : floor; /* last ditch attempt to get some usable pixels */
/* (0). ignore near the edges */
for (j = ybuffer; j < yLen-ybuffer; j++) {
yr = j + sy0;
for (i = xbuffer; i < xLen-xbuffer; i++) {
xr = i + sx0;
nr = xr+rPixX*yr;
/* (1). pixel already included in another stamp, or exceeds hiThresh */
if (mRData[nr] & bbit)
continue;
dpt = iData[nr];
/* (2a). skip masked out value */
if (dpt >= hiThresh) {
mRData[nr] |= bbit1;
continue;
}
/* (2b). don't want low sigma point here, sometimes thats all that passes first test */
if (( (dpt - sky) * invdsky) < kerFitThresh)
continue;
/* finally, a good candidate! */
if (dpt > loPsf) {
dmax = dpt;
imax = i;
jmax = j;
/* center this candidate on the peak flux */
for (l = j-hwKSStamp; l <= j+hwKSStamp; l++) {
yr2 = l + sy0;
if ((l < ybuffer) || (l >= yLen-ybuffer))
continue; /* continue l loop */
for (k = i-hwKSStamp; k <= i+hwKSStamp; k++) {
xr2 = k + sx0;
nr2 = xr2+rPixX*yr2;
if ((k < xbuffer) || (k >= xLen-xbuffer))
continue; /* continue k loop */
/* (3). no fill/sat values within checked region, and not overlapping other stamp */
if (mRData[nr2] & bbit)
continue; /* continue k loop */
dpt2 = iData[nr2];
/* (4a). no fill/sat values within checked region, and not overlapping other stamp */
if (dpt2 >= hiThresh) {
mRData[nr2] |= bbit1;
continue; /* continue k loop */
}
/* (4b). not as strong a problem, just don't want low sigma peak */
if (( (dpt2 - sky) * invdsky) < kerFitThresh)
continue;
/* record position and amp of good point centroid in i,j,dmax */
if (dpt2 > dmax) {
dmax = dpt2;
imax = k;
jmax = l;
}
}
}
/* (5). after centroiding, re-check for fill/sat values and overlap (a little inefficient) */
/* zero tolerance for bad pixels! */
/* since we have zero tolerance for bad pixels, the sum
of all the pixels in this box can be compared to the
sum of all the pixels in the other boxes. rank on
this! well, sum of high sigma pixels anyways...*/
dmax2 = checkPsfCenter(iData, imax, jmax, xLen, yLen, sx0, sy0,
hiThresh, sky, invdsky, xbuffer, ybuffer, bbit, bbit1);
if (dmax2 == 0.)
continue; /* continue i loop */
/* made it! - a valid peak */
xloc[pcnt] = imax;
yloc[pcnt] = jmax;
peaks[pcnt++] = dmax2;
/* globally mask out the region around this guy */
for (l = jmax-hwKSStamp; l <= jmax+hwKSStamp; l++) {
yr2 = l + sy0;
for (k = imax-hwKSStamp; k <= imax+hwKSStamp; k++) {
xr2 = k + sx0;
nr2 = xr2+rPixX*yr2;
if ((k > 0) && (k < xLen) && (l > 0) && (l < yLen))
mRData[nr2] |= bbit2;
}
}
/* found enough stamps, get out! */
if (pcnt >= fcnt)
brk = 2;
}
if (brk == 2)
break; /* exit x loop */
}
if (brk == 2)
break; /* exit y loop */
}
if (loPsf == floor)
break; /* have hit the floor, get out */
dfrac -= 0.2;
}
if (pcnt+stamp->nss < nKSStamps) {
if (verbose >= 2) fprintf(stderr, " ...only found %d good substamps by autosearch\n", pcnt);
}
else {
if (verbose >= 2) fprintf(stderr, " ...found %d good substamps by autosearch\n", pcnt);
}
/* coords are in region's pixels */
/* no good full stamps */
if (pcnt == 0) {
/* changed in v4.1.6, don't accept center pixel, could be bad, worse than not having one! */
if (verbose >= 2) fprintf(stderr, " NO good pixels, skipping...\n");
free(qs);
free(xloc);
free(yloc);
free(peaks);
return 1;
}
else {
quick_sort (peaks, qs, pcnt);
if (verbose >= 2) fprintf(stderr, " Adding %d substamps found by autosearch\n", imin(pcnt, nKSStamps-stamp->nss));
for (i = stamp->nss, j = 0; j < pcnt && i < nKSStamps; i++,j++) {
stamp->xss[i] = xloc[qs[pcnt-j-1]] + sx0;
stamp->yss[i] = yloc[qs[pcnt-j-1]] + sy0;
if (verbose >= 2) fprintf(stderr," #%d @ %4d,%4d,%8.1f\n", i, stamp->xss[i], stamp->yss[i], peaks[qs[pcnt-j-1]]);
stamp->nss++;
}
}
free(qs);
free(xloc);
free(yloc);
free(peaks);
return 0;
}
void getNoiseStats3(float *data, float *noise, double *nnorm, int *nncount, int umask, int smask) {
/*
good pixels : umask=0, smask=0xffff
ok pixels : umask=0xff, smask=0x8000
bad pixels : umask=0x8000, smask=0
*/
double nsum=0;
int i, n=0, mdat;
float ddat=0, ndat=0;
for (i = rPixX*rPixY; i--; ) {
ddat = data[i];
mdat = mRData[i];
/*
fprintf(stderr, "CAW %d %f %f %f %d %d %d %d\n", n, nsum, ddat, ndat, mdat,
((umask > 0) && (!(mdat & umask))),
((smask > 0) && (mdat & smask)),
(ddat == fillVal) || (fabs(ddat) <= ZEROVAL));
*/
if (((umask > 0) && (!(mdat & umask))) ||
((smask > 0) && (mdat & smask)) ||
(fabs(ddat) <= ZEROVAL))
continue;
ndat = 1. / noise[i];
n += 1;
nsum += ddat*ddat * ndat*ndat;
}
if (n > 1) {
*nncount = n;
*nnorm = nsum / (float)n;
}
else {
*nncount = n;
*nnorm = MAXVAL;
}
return;
}
int getStampStats3(float *data,
int x0Reg, int y0Reg, int nPixX, int nPixY,
double *sum, double *mean, double *median,
double *mode, double *sd, double *fwhm, double *lfwhm,
int umask, int smask, int maxiter) {
/*****************************************************
* Given an input image, return stats on the pixel distribution
* This should be a masked distribution
*****************************************************/
/*
good pixels : umask=0, smask=0xffff
ok pixels : umask=0xff, smask=0x8000
bad pixels : umask=0x8000, smask=0
this version 3 does sigma clipping
*/
/* this came primarily from Gary Bernstein */
extern int flcomp();
double bin1,binsize,maxdens,moden;
double sumx,sumxx,isd;
double lower,upper,mode_bin, rdat;
int mdat,npts;
int bins[256],i,j,xr,yr;
int index,imax=0,tries,ilower,iupper, repeat;
double ssum;
int goodcnt;
int idum;
float *sdat;
double *work;
int nstat=100;
float ufstat=0.9;
float mfstat=0.5;
npts = nPixX * nPixY;
if (npts < nstat)
return 4;
/* fprintf(stderr, "DOINK! %d %d %f\n", x0Reg, y0Reg, data[0]); */
if ( !(sdat = (float *)calloc(npts, sizeof(float))) ||
!(work = (double *)calloc(nstat, sizeof(double)))) {
return (1);
}
idum = -666; /* initialize random number generator with the devil's seed */
tries = 0; /* attempts at the histogram */
goodcnt = 0;
/* pull 100 random enough values from array to estimate required bin sizes */
/* only do as many calls as there are points in the section! */
/* NOTE : ignore anything with fillVal or zero */
for (i = 0; (i < nstat) && (goodcnt < npts); i++, goodcnt++) {
xr = (int)floor(ran1(&idum)*nPixX);
yr = (int)floor(ran1(&idum)*nPixY);
/* here data is size rPixX, rPixY */
rdat = data[xr+yr*nPixX];
/* region is rPixX, rPixY */
mdat = mRData[(xr+x0Reg)+(yr+y0Reg)*rPixX];
if (((umask > 0) && (!(mdat & umask))) ||
((smask > 0) && (mdat & smask)) ||
(fabs(rdat) <= ZEROVAL))
i--;
else
work[i] = rdat;
}
qsort(work, i, sizeof(double), flcomp);
npts = i;
binsize = (work[(int)(ufstat*npts)] - work[(int)(mfstat*npts)]) / (float)nstat; /* good estimate */
bin1 = work[(int)(mfstat*npts)] - 128. * binsize; /* we use 256 bins */
/*** DO ONLY ONCE! ***/
goodcnt = 0;
for (j = 0; j < nPixY; j++) {
for (i = 0; i < nPixX; i++) {
rdat = data[i+j*nPixX];
mdat = mRData[(i+x0Reg)+(j+y0Reg)*rPixX];
/* fprintf(stderr, "BIGT %d %d %f : %d %d %d\n", i, j, rdat, i+x0Reg, j+y0Reg, mdat); */
/* looks like it works. that is, the image pixel values
i+x0Reg, j+y0Reg are the same as printed pixel values
i,j,rdat. mask should work similarly */
if (((umask > 0) && (!(mdat & umask))) ||
((smask > 0) && (mdat & smask)) ||
(fabs(rdat) <= ZEROVAL))
continue;
if (rdat*0.0 != 0.0) {
mRData[(i+x0Reg)+(j+y0Reg)*rPixX] |= (FLAG_INPUT_ISBAD | FLAG_ISNAN);
/* fprintf(stderr, "OUCH %d %d %f %d\n", i+x0Reg, j+y0Reg, rdat, mdat); */
continue;
}
/* good pixels pass, so sigma clipping part here */
sdat[goodcnt++] = rdat;
}
}
if (sigma_clip(sdat, goodcnt, mean, sd, maxiter)) {
free(sdat);
free(work);
return 5;
}
free(sdat);
/* save some speed */
isd = 1. / (*sd);
/*** DO ONLY ONCE! ***/
repeat = 1;
while (repeat) {
if (tries >= 5) {
/* too many attempts here - print message and exit*/
/* DD fprintf(stderr, " WARNING: 5 failed iterations in getStampStats2\n"); */
free(work);
return 1;
}
for (i=0; i<256; bins[i++]=0);
/* rezero sums if repeating */
ssum = sumx = sumxx = 0.;
goodcnt = 0;
for (j = 0; j < nPixY; j++) {
for (i = 0; i < nPixX; i++) {
rdat = data[i+j*nPixX];
mdat = mRData[(i+x0Reg)+(j+y0Reg)*rPixX];
if (((umask > 0) && (!(mdat & umask))) ||
((smask > 0) && (mdat & smask)) ||
(fabs(rdat) <= ZEROVAL))
continue;
if (rdat*0.0 != 0.0) {
mRData[(i+x0Reg)+(j+y0Reg)*rPixX] |= (FLAG_INPUT_ISBAD | FLAG_ISNAN);
continue;
}
/* final sigma cut */
/* reject both high and low here */
if ((fabs(rdat - (*mean)) * isd) > statSig)
continue;
index = floor( (rdat-bin1)/binsize ) + 1;
index = (index < 0 ? 0 : index);
index = (index > 255 ? 255 : index);
/* NOTE : the zero index is way overweighted since it contains everything
below bin1.
*/
bins[index]++;
/* ssum is the sum of absolute value of pixels in the image */
ssum += fabs(rdat);
/*
This is the number of pixels we are working with, not npts which
includes any zeroed or masked pixels.
*/
goodcnt += 1;
}
}
/* get out of dodge! */
if (goodcnt == 0) {
/* DD fprintf(stderr, " WARNING: no stampStats2 data\n"); */
*mode = *median = work[(int)(mfstat*npts)];
*fwhm = *lfwhm = 0.;
free(work);
return 2;
}
/* quit here if the bins are degenerate */
if (binsize == 0.) {
/* DD fprintf(stderr, " WARNING: no variation in stampStats2 data\n"); */
*mode = *median = work[(int)(mfstat*npts)];
*fwhm = *lfwhm = 0.;
free(work);
return 3;
}
/* find the mode - find narrowest region which holds ~10% of points*/
sumx = maxdens = 0.;
for (ilower = iupper = 1; iupper < 255; sumx -= bins[ilower++]) {
while ( (sumx < goodcnt/10.) && (iupper < 255) )
sumx += bins[iupper++];
if (sumx / (iupper-ilower) > maxdens) {
maxdens = sumx / (iupper-ilower);
imax = ilower;
}
}
/* if it never got assigned... */
if (imax < 0 || imax > 255)
imax = 0;
/* try to interpolate between bins somewhat by finding weighted
mean of the bins in the peak*/
/* NOTE : need <= here and above in case goodcnt is small (< 10) */
sumxx = sumx = 0.;
for (i = imax; (sumx < goodcnt/10.) && (i < 255); i++) {
sumx += bins[i];
sumxx += i*bins[i];
}
mode_bin = sumxx / sumx + 0.5; /*add 0.5 to give middle of bin*/
*mode = bin1 + binsize * (mode_bin - 1.);
/*find the percentile of the mode*/
imax = floor(mode_bin);
for (i = 0, sumx = 0.; i < imax; sumx += bins[i++]);
sumx += bins[imax] * (mode_bin-imax); /*interpolate fractional bin*/
sumx /= goodcnt;
moden=sumx;
/* find the region around mode containing half the "noise" points,
e.g. assume that the mode is 50th percentile of the noise */
lower = goodcnt * 0.25;
upper = goodcnt * 0.75;
sumx = 0.;
for (i = 0; sumx < lower; sumx += bins[i++]);
lower = i - (sumx - lower) / bins[i-1];
for ( ; sumx < upper; sumx += bins[i++]);
upper = i - (sumx - upper) / bins[i-1];
/*now a few checks to make sure the histogram bins were chosen well*/
if ( (lower < 1.) || (upper > 255.) ) {
/*make the bins wider, about same center*/
bin1 -= 128. * binsize;
binsize *= 2.;
tries++;
repeat = 1;
} else if ( (upper-lower) < 40. ) {
/*make bins narrower for better precision*/
binsize /= 3.;
bin1 = *mode - 128. * binsize;