forked from acbecker/hotpants
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
2001 lines (1699 loc) · 84.9 KB
/
main.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<ctype.h>
#include<time.h>
#include<unistd.h>
#include "defaults.h"
#include "globals.h"
#include "functions.h"
int main(int argc,char *argv[]) {
int i,j,k,l,m; /* generic indices */
char scrStr[SCRLEN]; /* scratch string */
double sumKernel; /* photometric normalization */
double meansigSubstamps,scatterSubstamps; /* mean sigma and scatter of substamps, for fits header */
double meansigSubstampsF,scatterSubstampsF; /* mean sigma and scatter of substamps, using final noise */
double meanksumSubstamps,scatterksumSubstamps; /* mean and scatter of ksum */
int NskippedSubstamps; /* Number of skipped substamps, for fits header */
/*
NOTE : The .fits images are 1-indexed, while these arrays contained within are
0 indexed. Thus anything you find below at x,y you will find displayed at
position x+1,y+1.
*/
/* template image */
fitsfile *tPtr;
int tBitpix, tNaxis;
long tNaxes[MAXDIM];
float *tRData = NULL;
double tMerit=0;
double *tKerSol = NULL;
stamp_struct *ctStamps = NULL;
/* comparison image */
fitsfile *iPtr;
int iBitpix, iNaxis;
long iNaxes[MAXDIM];
float *iRData = NULL;
double iMerit=0;
double *iKerSol = NULL;
stamp_struct *ciStamps = NULL;
/* output image */
fitsfile *oPtr;
int oBitpix, oNaxis;
long oNaxes[MAXDIM];
float *oRData = NULL;
/* extraneous image; eRData now defined in globals.h */
fitsfile *ePtr;
float *eRData = NULL;
/* extraneous output regions */
int *misRData = NULL; /* image mask */
int *mtsRData = NULL; /* template mask */
int xMin, yMin, xMax, yMax; /* whole image */
int *rXMins, *rYMins, *rXMaxs, *rYMaxs; /* each region, good data */
int rXMin, rYMin, rXMax, rYMax; /* each region, good data */
int rXBMin, rYBMin, rXBMax, rYBMax; /* each region, including buffer */
int xBufLo, xBufHi, yBufLo, yBufHi; /* buffer */
int nR; /* region counter */
int sXMin, sYMin, sXMax, sYMax; /* each stamp */
int niS, ntS; /* stamp counter */
int convTmpl; /* which way the convolution goes */
/* cfitsio */
long pixMin[2], pixMax[2];
long inc[2] = {1, 1};
int fpixelOutX, fpixelOutY, lpixelOutX, lpixelOutY;
int anynul;
int status = 0, kInfoNum, nx2norm;
char **tform=NULL, **ttype=NULL, **tunit=NULL;
char hKeyword[1024], hInfo[2048];
double sum, mean, median, mode, sd, fwhm, lfwhm, diffrat, x2norm;
double summ, meanm, medianm, modem, sdm, fwhmm, lfwhmm;
double nsum, nmean, nmedian, nmode, nsd, nfwhm, nlfwhm;
double nsumm, nmeanm, nmedianm, nmodem, nsdm, nfwhmm, nlfwhmm;
float iSFrac, tSFrac;
float fitThresh;
int flag;
/* misc */
FILE *rFile;
double inv1;
char *pstr;
int sBorder; /*armin*/
struct tm *tm;
time_t thetime;
/* SET VERSION */
sprintf(version, "5.1.11");
/* set global vars, and grab command line args */
/* return image names in fnames */
vargs(argc, argv);
/******/
/* access input images, create output image shell */
/******/
/* open up, get template bitpix, # dimensions, image size */
if (fits_open_file(&tPtr, template, 0, &status))
printError(status);
if (fits_get_img_param(tPtr, MAXDIM, &tBitpix, &tNaxis, tNaxes, &status))
printError(status);
/* if input noise image, open and check to make sure its the right size */
/* use the ePtr fitsfile temporarily */
if (tNoiseIm) {
if (fits_open_file(&ePtr, tNoiseIm, 0, &status))
printError(status);
if (fits_get_img_param(ePtr, MAXDIM, &oBitpix, &oNaxis, oNaxes, &status))
printError(status);
if ( (oNaxes[0] != tNaxes[0]) || (oNaxes[1] != tNaxes[1]) ) {
fprintf(stderr, "WARNING : Input template noise array not same size as template, ignoring...\n");
tNoiseIm = NULL;
}
if ( fits_close_file(ePtr, &status) )
printError(status);
}
/* open up, get comparison image bitpix, # dimensions, image size */
if (fits_open_file(&iPtr, image, 0, &status))
printError(status);
if (fits_get_img_param(iPtr, MAXDIM, &iBitpix, &iNaxis, iNaxes, &status))
printError(status);
/* if input noise image, open and check to make sure its the right size */
/* use the ePtr fitsfile temporarily */
if (iNoiseIm) {
if (fits_open_file(&ePtr, iNoiseIm, 0, &status))
printError(status);
if (fits_get_img_param(ePtr, MAXDIM, &oBitpix, &oNaxis, oNaxes, &status))
printError(status);
if ( (oNaxes[0] != iNaxes[0]) || (oNaxes[1] != iNaxes[1]) ) {
fprintf(stderr, "WARNING : Input image noise array not same size as image, ignoring...\n");
iNoiseIm = NULL;
}
if ( fits_close_file(ePtr, &status) )
printError(status);
}
if (tNoiseIm) {
if (fits_open_file(&ePtr, tNoiseIm, 0, &status))
printError(status);
if (fits_get_img_param(ePtr, MAXDIM, &oBitpix, &oNaxis, oNaxes, &status))
printError(status);
if ( (oNaxes[0] != iNaxes[0]) || (oNaxes[1] != iNaxes[1]) ) {
fprintf(stderr, "WARNING : Input template noise array not same size as image, ignoring...\n");
tNoiseIm = NULL;
}
if ( fits_close_file(ePtr, &status) )
printError(status);
}
/* same with mask images */
if (iMaskIm) {
if (fits_open_file(&ePtr, iMaskIm, 0, &status))
printError(status);
if (fits_get_img_param(ePtr, MAXDIM, &oBitpix, &oNaxis, oNaxes, &status))
printError(status);
if ( (oNaxes[0] != iNaxes[0]) || (oNaxes[1] != iNaxes[1]) ) {
fprintf(stderr, "WARNING : Input image mask array not same size as image, ignoring...\n");
iMaskIm = NULL;
}
if ( fits_close_file(ePtr, &status) )
printError(status);
}
if (tMaskIm) {
if (fits_open_file(&ePtr, tMaskIm, 0, &status))
printError(status);
if (fits_get_img_param(ePtr, MAXDIM, &oBitpix, &oNaxis, oNaxes, &status))
printError(status);
if ( (oNaxes[0] != iNaxes[0]) || (oNaxes[1] != iNaxes[1]) ) {
fprintf(stderr, "WARNING : Input template mask array not same size as image, ignoring...\n");
tMaskIm = NULL;
}
if ( fits_close_file(ePtr, &status) )
printError(status);
}
/* let em know whats going on... */
fprintf(stderr, "Doing : %s -\n", image);
fprintf(stderr, " %s =\n", template);
fprintf(stderr, " %s\n", outim);
fprintf(stderr, " Good templ data : %.1f -> %.1f\n", tLThresh, tUThresh);
fprintf(stderr, " Good image data : %.1f -> %.1f\n", iLThresh, iUThresh);
/* ADU pedestal? */
tUThresh -= tPedestal;
tUKThresh -= tPedestal;
tLThresh -= tPedestal;
iUThresh -= iPedestal;
iUKThresh -= iPedestal;
iLThresh -= iPedestal;
/* overwrite output image? */
if (!noClobber) { sprintf(scrStr, "!%s", outim); }
else { sprintf(scrStr, "%s", outim); }
oNaxes[0] = imax(tNaxes[0], iNaxes[0]);
oNaxes[1] = imax(tNaxes[1], iNaxes[1]);
oNaxis = 2;
if (outShort)
oBitpix = SHORT_IMG;
else
oBitpix = FLOAT_IMG;
/* create and open new empty output FITS file */
if ( fits_create_file(&oPtr, scrStr, &status) ||
fits_create_img(oPtr, oBitpix, oNaxis, oNaxes, &status) )
printError(status);
/* copy over the fits keywords */
if ( hp_fits_copy_header(iPtr, oPtr, &status) )
printError(status);
/* add info; format taken from SWARP */
fits_write_key_str(oPtr, "COMMENT", "", "", &status);
fits_write_key_str(oPtr, "SOFTNAME", "HOTPanTS", "The software that differenced this image", &status);
fits_write_key_str(oPtr, "SOFTVERS", version, "Version", &status);
fits_write_key_str(oPtr, "SOFTAUTH", "Andrew Becker <[email protected]>", "Maintainer", &status);
fits_write_key_str(oPtr, "SOFTINST", "University of Washington", "Institute", &status);
fits_write_key_str(oPtr, "COMMENT", "", "", &status);
/* user name */
if (!(pstr=getenv("USERNAME"))) /* Cygwin,... */
if ((pstr=getenv("LOGNAME"))) /* Linux,... */
fits_write_key_str(oPtr, "AUTHOR", pstr, "Who ran the software", &status);
if (!pstr)
fits_write_key_str(oPtr, "AUTHOR", "unknown", "Who ran the software", &status);
/* host name */
if (!gethostname(hInfo, 80))
fits_write_key_str(oPtr, "ORIGIN", hInfo, "Where it was done", &status);
/* what was it fired up */
thetime = time(NULL);
tm = gmtime(&thetime);
sprintf(hInfo,"%04d-%02d-%02dT%02d:%02d:%02d",
tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
tm->tm_hour, tm->tm_min, tm->tm_sec);
fits_write_key_str(oPtr, "DATE", hInfo, "When it was started (GMT)", &status);
fits_write_key_str(oPtr, "COMMENT", "", "", &status);
/* don't let a failure here kill us */
status = 0;
/* change bscale/bzero AFTER transfering input image header */
if ( fits_update_key_flt(oPtr, "BZERO", outBzero, -5, "", &status) ||
fits_update_key_flt(oPtr, "BSCALE", outBscale, -5, "", &status) )
printError(status);
/* make new image extensions for any extraneous output data */
if (inclConvImage) {
if ( fits_insert_img(oPtr, oBitpix, oNaxis, oNaxes, &status) ||
fits_update_key(oPtr, TSTRING, "OBJECT", "Convolved Image", "", &status) )
printError(status);
}
if (convImage) {
if (!noClobber) { sprintf(scrStr, "!%s", convImage); }
else { sprintf(scrStr, "%s", convImage); }
if (fits_create_file(&ePtr, scrStr, &status) ||
fits_create_img(ePtr, oBitpix, oNaxis, oNaxes, &status) ||
hp_fits_copy_header(iPtr, ePtr, &status) ||
fits_update_key(ePtr, TSTRING, "OBJECT", "Convolved Image", "", &status) ||
fits_close_file(ePtr, &status) )
printError(status);
}
if (inclSigmaImage) {
if ( fits_insert_img(oPtr, oBitpix, 2, oNaxes, &status) ||
fits_update_key(oPtr, TSTRING, "OBJECT", "Noise-scaled (sigma) Difference Image", "", &status) )
printError(status);
}
if (sigmaImage) {
if (!noClobber) { sprintf(scrStr, "!%s", sigmaImage); }
else { sprintf(scrStr, "%s", sigmaImage); }
if (fits_create_file(&ePtr, scrStr, &status) ||
fits_create_img(ePtr, oBitpix, oNaxis, oNaxes, &status) ||
hp_fits_copy_header(iPtr, ePtr, &status) ||
fits_update_key(ePtr, TSTRING, "OBJECT", "Noise-scaled (sigma) Difference Image", "", &status) ||
fits_close_file(ePtr, &status) )
printError(status);
}
if (inclNoiseImage) {
if (outNShort) {
if (fits_insert_img(oPtr, SHORT_IMG, 2, oNaxes, &status))
printError(status);
}
else {
if (fits_insert_img(oPtr, FLOAT_IMG, 2, oNaxes, &status))
printError(status);
}
if (fits_update_key(oPtr, TSTRING, "OBJECT", "HOTPanTS Noise Image", "", &status))
printError(status);
/* manually set bscale and bzero for noise image layer */
if (outNShort) {
if ( fits_update_key_flt(oPtr, "BZERO", outNiBzero, -5, "", &status) ||
fits_update_key_flt(oPtr, "BSCALE", outNiBscale, -5, "", &status) )
printError(status);
}
}
if (noiseImage) {
if (!noClobber) { sprintf(scrStr, "!%s", noiseImage); }
else { sprintf(scrStr, "%s", noiseImage); }
if (fits_create_file(&ePtr, scrStr, &status))
printError(status);
if (outNShort) {
if (fits_create_img(ePtr, SHORT_IMG, oNaxis, oNaxes, &status))
printError(status);
}
else {
if (fits_create_img(ePtr, FLOAT_IMG, oNaxis, oNaxes, &status))
printError(status);
}
if (hp_fits_copy_header(iPtr, ePtr, &status) ||
fits_update_key(ePtr, TSTRING, "OBJECT", "HOTPanTS Noise Image", "", &status) ||
fits_update_key_flt(ePtr, "GAIN", 1., -1, "No gain in noise image", &status) ||
fits_update_key_flt(ePtr, "RDNOISE", 0., -1, "No rdnoise in noise image", &status) ||
fits_write_key_flt(ePtr, "MASKVAL", fillValNoise, -6, "Value of Masked Pixels", &status))
printError(status);
if (outNShort)
if (fits_update_key_flt(ePtr, "BZERO", outNiBzero, -5, "", &status) ||
fits_update_key_flt(ePtr, "BSCALE", outNiBscale, -5, "", &status))
printError(status);
if (fits_close_file(ePtr, &status))
printError(status);
}
if (outMask) {
if (!noClobber) { sprintf(scrStr, "!%s", outMask); }
else { sprintf(scrStr, "%s", outMask); }
if (fits_create_file(&ePtr, scrStr, &status) ||
fits_create_img(ePtr, SHORT_IMG, oNaxis, oNaxes, &status) ||
hp_fits_copy_header(iPtr, ePtr, &status) ||
fits_update_key(ePtr, TSTRING, "OBJECT", "Hotpants output Mask Image", "", &status) ||
fits_update_key_flt(ePtr, "BZERO", 32768, -5, "", &status) ||
fits_update_key_flt(ePtr, "BSCALE", 1, -5, "", &status) ||
fits_close_file(ePtr, &status) )
printError(status);
}
if (kernelImIn) {
/* possibly override defaults with info in kernel image */
getKernelInfo(kernelImIn);
fprintf(stderr, " received kernel info\n");
}
/* determine the size of the data structures */
nCompKer = 0;
for(i = 0; i < ngauss; i++)
nCompKer += ((deg_fixe[i] + 1) * (deg_fixe[i] + 2)) / 2;
nComp = ((kerOrder + 1) * (kerOrder + 2)) / 2;
nC = nCompKer + 2;
nCompBG = (nCompKer - 1) * nComp + 1;
nBGVectors = ((bgOrder + 1) * (bgOrder + 2)) / 2;
nCompTotal = nCompKer * nComp + nBGVectors;
fwKernel = hwKernel * 2 + 1; /* kernel size */
if (useFullSS) {
fwKSStamp = fwKernel; /* substamp size */
fwStamp = fwKernel;
nStampX = (int)(imin(tNaxes[0], iNaxes[0]) / nRegX / fwStamp);
nStampY = (int)(imin(tNaxes[1], iNaxes[1]) / nRegY / fwStamp);
fprintf(stderr, "Using maximial number of stamps : %d x %d\n", nStampX, nStampY);
}
else {
fwKSStamp = hwKSStamp * 2 + 1; /* substamp size */
/* estimate of fwStamp for all these mallocs... */
/* smallest dimension, minus kernel width */
fwStamp = imin( imin(tNaxes[0], iNaxes[0]) / nRegX / nStampX,
imin(tNaxes[1], iNaxes[1]) / nRegY / nStampY );
fwStamp -= fwKernel;
fwStamp -= fwStamp % 2 == 0 ? 1 : 0; /* hmmm, an odd shape... */
/* insanity checking */
if (fwStamp < fwKSStamp) {
fwStamp = fwKSStamp+fwKernel;
fwStamp -= fwStamp % 2 == 0 ? 1 : 0;
nStampX = imin(tNaxes[0], iNaxes[0]) / nRegX / fwStamp;
nStampY = imin(tNaxes[1], iNaxes[1]) / nRegY / fwStamp;
fprintf(stderr, "WARNING : too many stamps requested\n");
fprintf(stderr, " using nsx = %d, nsy = %d\n", nStampX, nStampY);
}
}
kcStep = kcStep ? kcStep : fwKernel; /* size of step in spatial_convolve */
nStamps = nStampX * nStampY;
sBorder = hwKSStamp + hwKernel;
fprintf(stderr, "Mallocing massive amounts of memory...\n");
/* malloc data structures */
if ( !(temp = (float *)calloc((fwKSStamp+fwKernel)*fwKSStamp, sizeof(float))) ||
!(indx = (int *)calloc((nCompTotal+1+100), sizeof(int))) ||
!(kernel = (double *)calloc(fwKernel*fwKernel, sizeof(double))) ||
!(kernel_coeffs = (double *)calloc(nCompKer, sizeof(double))) ||
!(kernel_vec = (double **)calloc(nCompKer, sizeof(double *))) ||
!(filter_x = (double *)calloc(nCompKer*fwKernel, sizeof(double))) ||
!(filter_y = (double *)calloc(nCompKer*fwKernel, sizeof(double))) ||
!(check_mat = (double **)calloc(nC, sizeof(double *))) ||
!(check_vec = (double *)calloc(nC, sizeof(double))) ||
!(check_stack = (double *)calloc(nStamps, sizeof(double))) ) {
exit(1);
}
for (i = 0; i < nC; i++)
if ( !(check_mat[i] = (double *)calloc(nC, sizeof(double))) ) {
exit (1);
}
/******/
/* determine pixel limits of regions, and of stamps in region */
/******/
/* maximum limit of overlap between images */
/* to be used in newest version, which deals with borders later... */
xMin = (gdXmin ? imax(0, gdXmin) : 0);
yMin = (gdYmin ? imax(0, gdYmin) : 0);
xMax = -1 + (gdXmax ? imin(imin(tNaxes[0], iNaxes[0]), gdXmax) : imin(tNaxes[0], iNaxes[0]));
yMax = -1 + (gdYmax ? imin(imin(tNaxes[1], iNaxes[1]), gdYmax) : imin(tNaxes[1], iNaxes[1]));
nR = 0;
if (regFile) {
rFile = fopen(regFile, "r");
while (fscanf(rFile, "%s\n", scrStr) != EOF)
nR++;
rewind(rFile);
if ( !(rXMins = (int *)calloc(nR, sizeof(int))) ||
!(rXMaxs = (int *)calloc(nR, sizeof(int))) ||
!(rYMins = (int *)calloc(nR, sizeof(int))) ||
!(rYMaxs = (int *)calloc(nR, sizeof(int))) )
exit(1);
nR = 0;
while (fscanf(rFile, "%d:%d,%d:%d", &rXMin, &rXMax, &rYMin, &rYMax) != EOF) {
/* range of good data for the region */
rXMins[nR] = imax(rXMin, xMin);
rYMins[nR] = imax(rYMin, yMin);
rXMaxs[nR] = imin(rXMax, xMax);
rYMaxs[nR] = imin(rYMax, yMax);
nR++;
}
fclose(rFile);
}
else if (regKeyWord) {
if ( !(rXMins = (int *)calloc(numRegKeyWord, sizeof(int))) ||
!(rXMaxs = (int *)calloc(numRegKeyWord, sizeof(int))) ||
!(rYMins = (int *)calloc(numRegKeyWord, sizeof(int))) ||
!(rYMaxs = (int *)calloc(numRegKeyWord, sizeof(int))) )
exit(1);
for (nR = 0; nR < numRegKeyWord; nR++){
sprintf(hKeyword, "%s%d", regKeyWord, nR);
if (fits_read_key_str(tPtr, hKeyword, hInfo, scrStr, &status)) {
rXMins[nR] = xMin;
rXMaxs[nR] = xMax;
rYMins[nR] = yMin;
rYMaxs[nR] = yMax;
status = 0;
}
else
sscanf(hInfo, "[%d:%d,%d:%d]", &rXMins[nR], &rXMaxs[nR], &rYMins[nR], &rYMaxs[nR]);
if (fits_read_key_str(iPtr, hKeyword, hInfo, scrStr, &status)) {
rXMin = xMin;
rXMax = xMax;
rYMin = yMin;
rYMax = yMax;
status = 0;
}
else
sscanf(hInfo, "[%d:%d,%d:%d]", &rXMin, &rXMax, &rYMin, &rYMax);
rXMins[nR] = imax(rXMin, rXMins[nR]);
rYMins[nR] = imax(rYMin, rYMins[nR]);
rXMaxs[nR] = imin(rXMax, rXMaxs[nR]);
rYMaxs[nR] = imin(rYMax, rYMaxs[nR]);
rXMins[nR] = imax(xMin, rXMins[nR]);
rYMins[nR] = imax(yMin, rYMins[nR]);
rXMaxs[nR] = imin(xMax, rXMaxs[nR]);
rYMaxs[nR] = imin(yMax, rYMaxs[nR]);
/* fprintf(stderr, "%d %d %d %d\n", rXMins[nR], rYMins[nR], rXMaxs[nR], rYMaxs[nR]); */
}
}
else {
if ( !(rXMins = (int *)calloc(nRegX*nRegY, sizeof(int))) ||
!(rXMaxs = (int *)calloc(nRegX*nRegY, sizeof(int))) ||
!(rYMins = (int *)calloc(nRegX*nRegY, sizeof(int))) ||
!(rYMaxs = (int *)calloc(nRegX*nRegY, sizeof(int))) )
exit(1);
/* in cfitsio, data are striped along x dimen, thus all loops
are y outer, x inner. or at least they should be... */
for (j = 0; j < nRegY; j++) {
for (i = 0; i < nRegX; i++) {
/* range of good data for the region */
rXMins[nR] = xMin + i * xMax / nRegX;
rYMins[nR] = yMin + j * yMax / nRegY;
rXMaxs[nR] = imin( (i+1) * xMax / nRegX, xMax );
rYMaxs[nR] = imin( (j+1) * yMax / nRegY, yMax );
nR++;
}
}
}
/* now that we know the number of regions, if we want a fits
binary table for the kernel info, create it here */
if ((doKerInfo) || (kernelImOut)){
tform = (char **)calloc(nR, sizeof(char *));
ttype = (char **)calloc(nR, sizeof(char *));
tunit = (char **)calloc(nR, sizeof(char *));
for (k = 0; k < nR; k++) {
tform[k] = (char *)malloc(10*sizeof(char));
ttype[k] = (char *)malloc(10*sizeof(char));
tunit[k] = (char *)malloc(sizeof(char));
strcpy(tform[k], "D10.8");
sprintf(ttype[k], "Region%d", k);
strcpy(tunit[k], "");
}
if (doKerInfo) {
/* lets try it as a layer at the **bottom** of the output difference image */
if ( fits_insert_btbl(oPtr, (nCompTotal+1), nR, ttype, tform, tunit, "Convolution Kernel Information", 0L, &status) ||
fits_update_key(oPtr, TSTRING, "OBJECT", "Convolution Kernel Information", "", &status) ||
fits_get_hdu_num(oPtr, &kInfoNum) )
printError(status);
}
if (kernelImOut) {
if (!noClobber) { sprintf(scrStr, "!%s", kernelImOut); }
else { sprintf(scrStr, "%s", kernelImOut); }
if (fits_create_file(&ePtr, scrStr, &status) ||
fits_insert_btbl(ePtr, (nCompTotal+1), nR, ttype, tform, tunit, "Convolution Kernel Information", 0L, &status) ||
fits_update_key(ePtr, TSTRING, "OBJECT", "Convolution Kernel Information", "", &status) ||
fits_get_hdu_num(ePtr, &kInfoNum) ||
fits_close_file(ePtr, &status))
printError(status);
}
}
xcmp = ycmp = 0;
if (sstampFile) {
/* downsize for speed */
/* armin: this screws things up! */
/*fwStamp = fwKSStamp + fwKernel;*/
/* armin: call function to load x,y from file */
loadxyfile(sstampFile, cmpFile);
}
/* initialize to requested value in case things change later on */
fitThresh = kerFitThresh;
/* cycle through all regions */
for (i = 0; i < nR; i++) {
if (kernelImIn) {
/* grab region info from kernelImIn */
readKernel(kernelImIn, i, &tKerSol, &iKerSol, &rXMin, &rXMax, &rYMin, &rYMax,
&meansigSubstamps, &scatterSubstamps,
&meansigSubstampsF, &scatterSubstampsF,
&diffrat, &NskippedSubstamps);
}
else {
rXMin = rXMins[i];
rXMax = rXMaxs[i];
rYMin = rYMins[i];
rYMax = rYMaxs[i];
meansigSubstamps = scatterSubstamps = 0.0;
NskippedSubstamps = 0;
}
if (nR > 1) {
/* buffered - add an extra half-stamp for merging regions */
rXBMin = imax(xMin, rXMin - fwStamp/2);
rYBMin = imax(yMin, rYMin - fwStamp/2);
rXBMax = imin(xMax, rXMax + fwStamp/2);
rYBMax = imin(yMax, rYMax + fwStamp/2);
}
else {
rXBMin = imax(xMin, rXMin - hwKernel);
rYBMin = imax(yMin, rYMin - hwKernel);
rXBMax = imin(xMax, rXMax + hwKernel);
rYBMax = imin(yMax, rYMax + hwKernel);
}
/* size of the buffering, used when merging output image sections */
xBufLo = rXMin - rXBMin;
xBufHi = rXBMax - rXMax;
yBufLo = rYMin - rYBMin;
yBufHi = rYBMax - rYMax;
/* size of buffered region, in pixels */
rPixX = rXBMax - rXBMin + 1;
rPixY = rYBMax - rYBMin + 1;
/* detemine the limits of the input images */
/* NOTE: cfitsio wants first pixel = 1, not 0 */
pixMin[0] = rXBMin + 1;
pixMin[1] = rYBMin + 1;
pixMax[0] = rXBMax + 1;
pixMax[1] = rYBMax + 1;
/* determine the limits of the output images */
fpixelOutX = rXBMin + xBufLo + 1;
fpixelOutY = rYBMin + yBufLo + 1;
lpixelOutX = fpixelOutX + (rPixX - xBufHi - xBufLo - 1);
lpixelOutY = fpixelOutY + (rPixY - yBufHi - yBufLo - 1);
fprintf(stderr, "Region %d pixels : %ld:%ld,%ld:%ld\n"
, i, pixMin[0], pixMax[0], pixMin[1], pixMax[1]);
fprintf(stderr, " Vector Indices (buffered) : %d:%d,%d:%d\n"
, rXBMin, rXBMax, rYBMin, rYBMax);
fprintf(stderr, " Vector Indices (good data): %d:%d,%d:%d\n"
, rXMin, rXMax, rYMin, rYMax);
/* malloc standard input and output arrays */
tRData = (float *)calloc(rPixX*rPixY, sizeof(float));
iRData = (float *)calloc(rPixX*rPixY, sizeof(float));
oRData = (float *)calloc(rPixX*rPixY, sizeof(float));
eRData = (float *)calloc(rPixX*rPixY, sizeof(float));
if (tRData == NULL || iRData == NULL || oRData == NULL || eRData == NULL) {
fprintf(stderr, "Cannot Allocate Standard Data Arrays\n");
exit (1);
}
/* set them to fillVal */
fset(tRData, fillVal, rPixX, rPixY);
fset(iRData, fillVal, rPixX, rPixY);
/* set eRData and oRData to fillValNoise */
fset(oRData, fillValNoise, rPixX, rPixY);
fset(eRData, fillValNoise, rPixX, rPixY);
/* malloc mask arrays */
mRData = (int *)calloc(rPixX*rPixY, sizeof(int));
misRData = (int *)calloc(rPixX*rPixY, sizeof(int));
mtsRData = (int *)calloc(rPixX*rPixY, sizeof(int));
if (mRData == NULL || misRData == NULL || mtsRData == NULL ) {
fprintf(stderr, "Cannot Allocate Mask Arrays\n");
exit (1);
}
/* dont bother if info already from kernel image */
if (!(kernelImIn)) {
/* ciStamps contains stamp information about convolving image */
/* ctStamps contains stamp information about convolving template */
if (!(strncmp(forceConvolve, "i", 1)==0)) {
if (verbose>=2) fprintf(stderr,"Allocating stamps...\n");
if(!(ctStamps = (stamp_struct *)calloc(nStamps, sizeof(stamp_struct)))) {
printf("Cannot Allocate Stamp List\n");
exit (1);
}
if (allocateStamps(ctStamps, nStamps)) {
fprintf(stderr,"Cannot Allocate Stamp Vector\n");
exit (1);
}
tKerSol = (double *)calloc((nCompTotal+1), sizeof(double));
}
if (!(strncmp(forceConvolve, "t", 1)==0)) {
if(!(ciStamps = (stamp_struct *)calloc(nStamps, sizeof(stamp_struct)))) {
printf("Cannot Allocate Stamp List\n");
exit (1);
}
if (allocateStamps(ciStamps, nStamps)) {
fprintf(stderr,"Cannot Allocate Stamp Vector\n");
exit(1);
}
iKerSol = (double *)calloc((nCompTotal+1), sizeof(double));
}
}
/* read image region from fits files into input arrays */
if (fits_read_subset_flt(tPtr, 0, tNaxis, tNaxes, pixMin, pixMax, inc, 0, tRData, &anynul, &status) ||
fits_read_subset_flt(iPtr, 0, iNaxis, iNaxes, pixMin, pixMax, inc, 0, iRData, &anynul, &status)) {
printError(status);
}
/* take off possible pedestal */
if (tPedestal != 0. || iPedestal != 0.) {
for (l = rPixX*rPixY; l--; ) {
tRData[l] -= tPedestal;
iRData[l] -= iPedestal;
}
}
/* we will use eRData and oRData here to make the APPROXIMATE
noise image - approximate because you don't know which way to
convolve yet */
if (iNoiseIm) {
if (fits_open_file(&ePtr, iNoiseIm, 0, &status))
printError(status);
if (fits_read_subset_flt(ePtr, 0, iNaxis, iNaxes, pixMin, pixMax, inc, 0, oRData, &anynul, &status) ||
fits_close_file(ePtr, &status))
printError(status);
for (l = rPixX*rPixY; l--; )
oRData[l] *= oRData[l];
}
else {
oRData = makeNoiseImage4(iRData, 1./iGain, iRdnoise/iGain);
}
if (tNoiseIm) {
if (fits_open_file(&ePtr, tNoiseIm, 0, &status))
printError(status);
if (fits_read_subset_flt(ePtr, 0, tNaxis, tNaxes, pixMin, pixMax, inc, 0, eRData, &anynul, &status) ||
fits_close_file(ePtr, &status))
printError(status);
for (l = rPixX*rPixY; l--; )
eRData[l] *= eRData[l];
}
else {
eRData = makeNoiseImage4(tRData, 1./tGain, tRdnoise/tGain);
}
/* add noise portions, NOT SQRT HERE
this is only used for getStampSig() */
for (l = rPixX*rPixY; l--; )
oRData[l] += eRData[l];
/* NOTE : oRData is (temporarily) the approximate noise squared per pixel */
/* and eRData gets realloced later */
/*
mask out bad input pixels, spread by hwKernel * kfSpreadMask1
*/
if (iMaskIm) {
if (fits_open_file(&ePtr, iMaskIm, 0, &status))
printError(status);
if (fits_read_subset_int(ePtr, 0, tNaxis, tNaxes, pixMin, pixMax, inc, 0, misRData, &anynul, &status) ||
fits_close_file(ePtr, &status))
printError(status);
/* register as read in from input mask */
for (l = rPixX*rPixY; l--; ) {
misRData[l] |= FLAG_INPUT_MASK * (misRData[l] > 0);
mRData[l] |= misRData[l];
}
}
if (tMaskIm) {
if (fits_open_file(&ePtr, tMaskIm, 0, &status))
printError(status);
if (fits_read_subset_int(ePtr, 0, tNaxis, tNaxes, pixMin, pixMax, inc, 0, mtsRData, &anynul, &status) ||
fits_close_file(ePtr, &status))
printError(status);
/* register as read in from input mask */
for (l = rPixX*rPixY; l--; ) {
mtsRData[l] |= FLAG_INPUT_MASK * (mtsRData[l] > 0);
mRData[l] |= mtsRData[l];
}
}
/* add own bits and spread */
makeInputMask(tRData, iRData, mRData);
/* make sure program can't find a valid stamp center around the edge of the image */
/* mask with 0x100 & 0x400 */
for (l = 0; l < rPixY; l++) {
for (k = 0; k < sBorder; k++) {
mRData[k+rPixX*l] |= (FLAG_T_BAD | FLAG_I_BAD);
}
for (k = rPixX-sBorder; k < rPixX; k++) {
mRData[k+rPixX*l] |= (FLAG_T_BAD | FLAG_I_BAD);
}
}
for (l = 0; l < sBorder; l++)
for (k = sBorder; k < rPixX-sBorder; k++)
mRData[k+rPixX*l] |= (FLAG_T_BAD | FLAG_I_BAD);
for (l = rPixY-sBorder; l < rPixY; l++)
for (k = sBorder; k < rPixX-sBorder; k++)
mRData[k+rPixX*l] |= (FLAG_T_BAD | FLAG_I_BAD);
status = 1;
flag = 1;
kerFitThresh = fitThresh;
if (!(kernelImIn)) {
/* only do a single loop for now... */
while ((status <= 2) && (flag)) {
flag = 0;
niS = 0;
ntS = 0;
for (l = 0; l < nStampY; l++) {
for (k = 0; k < nStampX; k++) {
fprintf(stderr, "Build stamp : t %4d i %4d (grid coord %2d %2d)\n", ntS, niS, k, l);
/* coordinates in the image, not the region */
/* NOTE : we keep float-valued 'rPixX / nStampX' in
here to not exclude a full stamps' width of space on
the right and upper sides of the image. Allows the
stamps to be separated by a little bit but fills the
region more evenly. */
sXMin = rXBMin + k * rPixX / nStampX;
sYMin = rYBMin + l * rPixY / nStampY;
sXMax = imin(sXMin + fwStamp - 1, rXBMax);
sYMax = imin(sYMin + fwStamp - 1, rYBMax);
/* armin: reinitializing sscnt and nss? */
if (!(strncmp(forceConvolve, "i", 1)==0)) {
ctStamps[ntS].sscnt = ctStamps[ntS].nss = 0;
ctStamps[ntS].chi2 = 0.0;
}
if (!(strncmp(forceConvolve, "t", 1)==0)) {
ciStamps[niS].sscnt = ciStamps[niS].nss = 0;
ciStamps[niS].chi2 = 0.0;
}
if (sstampFile) {
if (verbose >= 2) fprintf(stderr, "Adding centers manually\n");
for (m = 0; m < Ncmp; m++) {
if ((xcmp[m] > sXMin + hwKernel + 1) && (xcmp[m] < sXMax - hwKernel - 1) &&
(ycmp[m] > sYMin + hwKernel + 1) && (ycmp[m] < sYMax - hwKernel - 1)) {
buildStamps(sXMin, sXMax, sYMin, sYMax, &niS, &ntS, 0, rXBMin, rYBMin,
ciStamps, ctStamps, iRData, tRData,
xcmp[m] - rXBMin, ycmp[m] - rYBMin);
}
}
if (findSSC) {
if (verbose >= 2) fprintf(stderr, "Automatically finding additional centers\n");
/* and then choose enough stamps manually to fill the array */
buildStamps(sXMin, sXMax, sYMin, sYMax, &niS, &ntS, 1, rXBMin, rYBMin,
ciStamps, ctStamps, iRData, tRData, 0, 0);
}
}
else {
if (useFullSS) {
/* don't try and find centers, just use center of stamp */
buildStamps(sXMin, sXMax, sYMin, sYMax, &niS, &ntS, 0, rXBMin, rYBMin,
ciStamps, ctStamps, iRData, tRData, 0, 0);
}
else {
buildStamps(sXMin, sXMax, sYMin, sYMax, &niS, &ntS, 1, rXBMin, rYBMin,
ciStamps, ctStamps, iRData, tRData, 0, 0);
}
}
if (!(strncmp(forceConvolve, "i", 1)==0)) {
if (verbose>=2) fprintf(stderr," templ: %d substamps\n",ctStamps[ntS].nss);
if (ctStamps[ntS].nss > 0) ntS += 1;
}
if (!(strncmp(forceConvolve, "t", 1)==0)) {
if (verbose>=2) fprintf(stderr," image: %d substamps\n",ciStamps[niS].nss);
if (ciStamps[niS].nss > 0) niS += 1;
}
}
}
iSFrac = niS / (float) nStamps;
tSFrac = ntS / (float) nStamps;
if (strncmp(forceConvolve, "i", 1)==0) {
fprintf(stderr, "%d stamps built (%.2f%s)\n\n", niS, iSFrac, "%");
if (iSFrac < minFracGoodStamps)
flag = 1;
}
else if (strncmp(forceConvolve, "t", 1)==0) {
fprintf(stderr, "%d stamps built (%.2f%s)\n\n", ntS, tSFrac, "%");
if (tSFrac < minFracGoodStamps)
flag = 1;
}
else if ((iSFrac < minFracGoodStamps) || (tSFrac < minFracGoodStamps)) {
fprintf(stderr, "%d and %d stamps built (%.2f%s, %.2f%s)\n\n", ntS, niS, tSFrac, "%", iSFrac, "%");
flag = 1;
}
else {
fprintf(stderr, "%d and %d stamps built (%.2f%s, %.2f%s)\n\n", ntS, niS, tSFrac, "%", iSFrac, "%");
break;
}
/*
success here requires that you have chosen the number of
stamps such that you expect 1 object at good S/N in each
one. if too few stamps have been chosen, either
A : asking for too many stamps
B : too few objects at high S/N
i think B is the more likely scenario, so we lower the
fitting threshold. do it only once now to be safe,
since this is a new feature brought on by lack on sleep
during Chilean obsreving runs...
*/
if ((flag) && (status <= 1) && (scaleFitThresh < 1.)) {
/* start over... */
kerFitThresh *= scaleFitThresh;
fprintf(stderr, "Too few stamps were fit, scaling down fitting threshold to %.2f\n", kerFitThresh);
if (ctStamps) {
freeStampMem(ctStamps, nStamps);
free(ctStamps);
ctStamps = NULL;
}
if (ciStamps) {
freeStampMem(ciStamps, nStamps);
free(ciStamps);
ciStamps = NULL;
}
/* sheesh, reallocate stamps ... */
if (!(strncmp(forceConvolve, "i", 1)==0)) {
if(!(ctStamps = (stamp_struct *)calloc(nStamps, sizeof(stamp_struct)))) {
printf("Cannot Allocate Stamp List\n");
exit (1);
}
if (allocateStamps(ctStamps, nStamps)) {
fprintf(stderr,"Cannot Allocate Stamp Vector\n");
exit (1);
}
}
if (!(strncmp(forceConvolve, "t", 1)==0)) {
if(!(ciStamps = (stamp_struct *)calloc(nStamps, sizeof(stamp_struct)))) {
printf("Cannot Allocate Stamp List\n");
exit (1);
}
if (allocateStamps(ciStamps, nStamps)) {
fprintf(stderr,"Cannot Allocate Stamp Vector\n");
exit(1);
}
}
/* allow getpsfcenter to use pixels again */
for (l = rPixX*rPixY; l--; )
mRData[l] &= ~0xa00;
}
/* loop */
status += 1;
}
status = 0;