-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtash
4218 lines (4218 loc) · 216 KB
/
tash
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
[1mdiff --git a/+ImUtil/+Im/background_image.m b/+ImUtil/+Im/background_image.m[m
[1mindex e76c90c..11b447b 100755[m
[1m--- a/+ImUtil/+Im/background_image.m[m
[1m+++ b/+ImUtil/+Im/background_image.m[m
[36m@@ -139,7 +139,7 @@[m [mfor Ilist=1:1:Nlist[m
% is mode_fit requested also for the std:[m
switch lower(InPar.MethodStD)[m
case 'mode_fit'[m
[31m- [SubBack(Ilist),SubStd(Ilist)] = Util.stat.mode_fit(SubImage,InPar.FunBackPar{:});[m
[32m+[m[32m [SubBack(Ilist),SubStd(Ilist)] = Util.stat.mode_fit(SubImage(~isnan(SubImage)),InPar.FunBackPar{:});[m
CalcStd = false;[m
otherwise[m
SubBack(Ilist) = Util.stat.mode_fit(SubImage,InPar.FunBackPar{:});[m
[1mdiff --git a/+ImUtil/+pattern/match_pattern_shift_rot.m~ b/+ImUtil/+pattern/match_pattern_shift_rot.m~[m
[1mdeleted file mode 100644[m
[1mindex d4e762b..0000000[m
[1m--- a/+ImUtil/+pattern/match_pattern_shift_rot.m~[m
[1m+++ /dev/null[m
[36m@@ -1,409 +0,0 @@[m
[31m-function [Res,IndBest,H2]=match_pattern_shift_rot(Cat,Ref,Rotation,varargin)[m
[31m-% Match X/Y coordinates in two shifted and rotated lists.[m
[31m-% Description: ImUtil.pattern[m
[31m-% Input : - Catalog list, containing at least two columns of [X,Y].[m
[31m-% Should be sorted by Y.[m
[31m-% If this is an AstCat object then AstCat/pattern_match_shift[m
[31m-% will be called.[m
[31m-% - Reference list, containing at least two columns of [X,Y].[m
[31m-% Sorted by Y.[m
[31m-% - Vector of rotations to check [deg].[m
[31m-% * Arbitrary number of pairs of arguments: ...,keyword,value,...[m
[31m-% where keyword are one of the followings:[m
[31m-% 'ColXc' - Column name or column index of the X coordinate in[m
[31m-% the catalog. Default is 1.[m
[31m-% 'ColYc' - Column name or column index of the Y coordinate in[m
[31m-% the catalog. Default is 2.[m
[31m-% 'ColXr' - Column name or column index of the X coordinate in[m
[31m-% the reference. Default is 1.[m
[31m-% 'ColYr' - Column name or column index of the Y coordinate in[m
[31m-% the reference. Default is 2.[m
[31m-% 'FunCatSelect' - Function handle for selecting good sources in[m
[31m-% input Cat. Flag=fun(Cat,additional_arg), where Flag[m
[31m-% is a logical vector indicating good sources.[m
[31m-% Default is [].[m
[31m-% 'FunCatSelectPar' - Additional parameters to pass to the[m
[31m-% FunCatSelect function. Default is {}.[m
[31m-% 'FunRefSelect' - Function handle for selecting good sources in[m
[31m-% input Ref. Flag=fun(Ref,additional_arg), where Flag[m
[31m-% is a logical vector indicating good sources.[m
[31m-% Default is [].[m
[31m-% 'FunRefSelectPar' - Additional parameters to pass to the[m
[31m-% FunRefSelect function. Default is {}.[m
[31m-% 'SearchRangeX' - X shift search range (in coordinate units).[m
[31m-% If empty then set to half the max X coordinates in[m
[31m-% the input Cat.[m
[31m-% Default is []. [m
[31m-% 'SearchRangeY' - Y shift search range (in coordinate units).[m
[31m-% If empty then set to half the max X coordinates in[m
[31m-% the input Cat.[m
[31m-% Default is [].[m
[31m-% 'SearchStepX' - X shift search step size (in coordinate units).[m
[31m-% Default is 2.[m
[31m-% 'SearchStepY' - Y shift search step size (in coordinate units).[m
[31m-% Default is 2.[m
[31m-% 'MaxMethod' - Method by which to use the best match[m
[31m-% solution/s. Options are:[m
[31m-% 'sort' - sort by number of matches and return the[m
[31m-% best solutions. Default.[m
[31m-% 'max1' - Return the soltion with the largest[m
[31m-% number of matches.[m
[31m-% 'MinNinPeak' - Minimum number of histogram matching for a[m
[31m-% valid solution. Default is 5.[m
[31m-% If value is <1, then this is the fraction of matches[m
[31m-% out of the total number of sources in the catalog.[m
[31m-% 'MaxPeaks' - Maximum number of histogram peaks (matches) to[m
[31m-% return. Default is 10.[m
[31m-% 'MaxPeaksCheck' - Out of the returned peaks, this is the[m
[31m-% maximum nuber of peaks to check. Default is 10.[m
[31m-% Checking includes trying to use the transformation[m
[31m-% to match all the sources.[m
[31m-% If 0 then 'SelectBest' will use the MaxHistMatch[m
[31m-% value.[m
[31m-% 'CooType' - Coordinate type {'plane'|'sphere'}.[m
[31m-% Default is 'plane'.[m
[31m-% 'SearchRad' - Search radius for final matching. Default is 2.[m
[31m-% 'SearchMethod' - Catalg matching search method.[m
[31m-% See search_cat.m for options. Default is 'binms'.[m
[31m-% 'SelectBest' - How to select the best solution. Options are:[m
[31m-% 'meanerr' - Choose the solution with the minimal[m
[31m-% error on the mean.[m
[31m-% 'N' - Choose the solution with the largest[m
[31m-% number of matched sources.[m
[31m-% 'std' - Choose the solution with the minmum[m
[31m-% std.[m
[31m-% 'comb' - Use both meanerr and N. Default.[m
[31m-% 'FalseAlarm' - In the adaptive peak detection option,[m
[31m-% this is the false alarm probability that will be[m
[31m-% used in order to calculate the detection threshold.[m
[31m-% Default is 1e-4.[m
[31m-% 'RegionMaxConn' - connectivity for imregionalmax. Default is 8.[m
[31m-% Output : - Structure array of all the possible shift solutions.[m
[31m-% The following fields are available:[m
[31m-% - Index of the best shift solution in the structure of all[m
[31m-% possible solutions.[m
[31m-% - The 2D histogram of all possible combination of X and Y[m
[31m-% distances. [m
[31m-% License: GNU general public license version 3[m
[31m-% By : Eran O. Ofek Jan 2018[m
[31m-% URL : http://weizmann.ac.il/home/eofek/matlab/[m
[31m-% Example: [Res,IndBest,H2]=ImUtil.pattern.match_pattern_shift_rot[m
[31m-% Reliable: 2[m
[31m-%--------------------------------------------------------------------------[m
[31m-[m
[31m-RAD = 180./pi;[m
[31m-[m
[31m-if (nargin==0)[m
[31m- % simulation mode[m
[31m- Rotation = (-1:0.1:1)';[m
[31m- Nstar = 1000;[m
[31m- Ref = rand(Nstar,2).*2048;[m
[31m- Ref = sortrows(Ref,2);[m
[31m- Noverlap = 50;[m
[31m- Cat = [Ref(1:Noverlap,1), Ref(1:Noverlap,2)];[m
[31m- Cat = [Cat; rand(Nstar-Noverlap,2).*2048];[m
[31m- Cat(:,1) = Cat(:,1) + 20 + randn(Nstar,1).*0.3;[m
[31m- Cat(:,2) = Cat(:,2) + 30 + randn(Nstar,1).*0.3;[m
[31m- Cat = sortrows(Cat,2);[m
[31m-end[m
[31m-[m
[31m-[m
[31m-[m
[31m-DefV.Radius = 2;[m
[31m-DefV.Flip = [1 1];[m
[31m-DefV.ColXc = 1; % or string[m
[31m-DefV.ColYc = 2;[m
[31m-DefV.ColXr = 1;[m
[31m-DefV.ColYr = 2;[m
[31m-DefV.FunCatSelect = [];[m
[31m-DefV.FunCatSelectPar = {};[m
[31m-DefV.FunRefSelect = [];[m
[31m-DefV.FunRefSelectPar = {};[m
[31m-DefV.CutRefCat = true;[m
[31m-DefV.SearchRangeX = [-1000 1000];[m
[31m-DefV.SearchRangeY = [-1000 1000];[m
[31m-DefV.SearchRangeFactor= 0.5;[m
[31m-DefV.SearchStepX = 3;[m
[31m-DefV.SearchStepY = 3;[m
[31m-DefV.MaxMethod = 'adaptive'; %'sort'; % {'max1'|'sort'|'[m
[31m-DefV.FalseAlarm = 1e-2;[m
[31m-DefV.MinNinPeak = 5; % min number of matches in peak - if <1 then fraction of ListCat[m
[31m-DefV.RetSolFracMax = 0.5; % return only solutions that their HistNmatch is larger than this fraction*max(HistNmatch)[m
[31m-DefV.MaxPeaks = 10; % maximum number of peaks to return in individual rotations[m
[31m-DefV.MaxPeaksCheck = 10; % out of the returned peaks - this is the maximum nuber of peaks to check[m
[31m-%DefV.SearchRad = 2;[m
[31m-DefV.SortResBy = 'N';[m
[31m-DefV.SelectBest = 'comb'; % {'N','std','meanerr','comb'}[m
[31m-%DefV.RegionMaxConn = 8;[m
[31m-[m
[31m-if (~isempty(varargin))[m
[31m- InPar = InArg.populate_keyval(DefV,varargin,mfilename);[m
[31m-else[m
[31m- InPar = DefV;[m
[31m-end[m
[31m-[m
[31m-[m
[31m-% set SearchRangeX/Y[m
[31m-if (isempty(InPar.SearchRangeX))[m
[31m- SizeX = max(Cat(:,InPar.ColXc)).*InPar.SearchRangeFactor;[m
[31m- InPar.SearchRangeX = [-SizeX +SizeX];[m
[31m-end[m
[31m-if (isempty(InPar.SearchRangeY))[m
[31m- SizeY = max(Cat(:,InPar.ColYc)).*InPar.SearchRangeFactor;[m
[31m- InPar.SearchRangeY = [-SizeY +SizeY];[m
[31m-end[m
[31m-[m
[31m-% If SearchRange is a scalar then assume the range is symmetric[m
[31m-if (numel(InPar.SearchRangeX)==1)[m
[31m- InPar.SearchRangeX = [-InPar.SearchRangeX InPar.SearchRangeX];[m
[31m-end[m
[31m-if (numel(InPar.SearchRangeY)==1)[m
[31m- InPar.SearchRangeY = [-InPar.SearchRangeY InPar.SearchRangeY];[m
[31m-end[m
[31m-[m
[31m-% select best targets from List and Cat for matching[m
[31m-if (~isempty(InPar.FunCatSelect))[m
[31m- FlagCat = InPar.FunCatSelect(Cat,InPar.FunCatSelectPar{:});[m
[31m- Cat = Cat(FlagCat,:);[m
[31m-end[m
[31m-if (~isempty(InPar.FunRefSelect))[m
[31m- FlagRef = InPar.FunRefSelect(Ref,InPar.FunRefSelectPar{:});[m
[31m- Ref = Cat(FlagRef,:);[m
[31m-end[m
[31m-[m
[31m-% % select best targets from List and Cat for matching[m
[31m-% % select targets if columns are in required range[m
[31m-% FlagSelCat = Util.array.array_select(Cat,InPar.CatSelectOp,InPar.CatSelect{:});[m
[31m-% FlagSelRef = Util.array.array_select(Ref,InPar.RefSelectOp,InPar.RefSelect{:});[m
[31m-% SubCat = Cat(FlagSelCat,:);[m
[31m-% SubRef = Ref(FlagSelRef,:);[m
[31m-[m
[31m- [m
[31m-% number of elements in Ref and Cat[m
[31m-Ncat = size(Cat,1);[m
[31m-Nref = size(Ref,1);[m
[31m-[m
[31m-% if InPar.MinNinPeak<1 then use it as the fraction of matches for Ncat[m
[31m-if (InPar.MinNinPeak<1)[m
[31m- InPar.MinNinPeak = ceil(InPar.MinNinPeak.*Ncat);[m
[31m-end[m
[31m-[m
[31m-[m
[31m-%Xref = Ref(:,InPar.ColXr);[m
[31m-%Yref = Ref(:,InPar.ColYr);[m
[31m-[m
[31m-% if (InPar.MaxPeaksCheck>0 && ~issorted(Ycat))[m
[31m-% error('Input catalog is not sorted');[m
[31m-% end[m
[31m-[m
[31m-Nflip = size(InPar.Flip,1);[m
[31m-Res = [];[m
[31m-for Iflip=1:1:Nflip[m
[31m- % Vectors of X/Y coordinates[m
[31m- Xcat = Cat(:,InPar.ColXc);[m
[31m- Ycat = Cat(:,InPar.ColYc);[m
[31m-[m
[31m- % Flip coordinates[m
[31m- % Note this assumes that the coordinates are specified relative to[m
[31m- % image center.[m
[31m- Xcat = InPar.Flip(Iflip,1).*Xcat;[m
[31m- Ycat = InPar.Flip(Iflip,2).*Ycat;[m
[31m- [m
[31m-[m
[31m- Nrot = numel(Rotation);[m
[31m- for Irot=1:1:Nrot[m
[31m- % rotate Cat - rotation in deg[m
[31m- % note the rotation is applied after flip![m
[31m- XcatRot = Xcat.*cosd(Rotation(Irot)) - Ycat.*sind(Rotation(Irot));[m
[31m- YcatRot = Xcat.*sind(Rotation(Irot)) + Ycat.*cosd(Rotation(Irot));[m
[31m- [m
[31m- if (InPar.CutRefCat)[m
[31m- MinX = min(XcatRot) - max(abs(InPar.SearchRangeX));[m
[31m- MaxX = max(XcatRot) + max(abs(InPar.SearchRangeX));[m
[31m- MinY = min(YcatRot) - max(abs(InPar.SearchRangeY));[m
[31m- MaxY = max(YcatRot) + max(abs(InPar.SearchRangeY));[m
[31m-[m
[31m- % use only Ref sources in SubCat region[m
[31m- FinCut = Ref(:,InPar.ColXr)>MinX & ...[m
[31m- Ref(:,InPar.ColXr)<MaxX & ...[m
[31m- Ref(:,InPar.ColYr)>MinY & ...[m
[31m- Ref(:,InPar.ColYr)<MaxY;[m
[31m- [m
[31m- RefSub = Ref(FinCut,:);[m
[31m- else[m
[31m- RefSub = Ref;[m
[31m- end[m
[31m-[m
[31m- [ResT,IndBest,H2]=ImUtil.pattern.match_pattern_shift([XcatRot,YcatRot],RefSub,...[m
[31m- 'ColXc',1,...[m
[31m- 'ColYc',2,...[m
[31m- 'ColXr',InPar.ColXr,...[m
[31m- 'ColYr',InPar.ColYr,...[m
[31m- 'SearchRangeX',InPar.SearchRangeX,...[m
[31m- 'SearchRangeY',InPar.SearchRangeY,...[m
[31m- 'SearchRangeFactor',InPar.SearchRangeFactor,...[m
[31m- 'SearchStepX',InPar.SearchStepX,...[m
[31m- 'SearchStepY',InPar.SearchStepY,...[m
[31m- 'MaxMethod',InPar.MaxMethod,...[m
[31m- 'FalseAlarm',InPar.FalseAlarm,...[m
[31m- 'RetSolFracMax',InPar.RetSolFracMax,...[m
[31m- 'FunCatSelect',[],...[m
[31m- 'FunCatSelectPar',{},...[m
[31m- 'FunRefSelect',[],...[m
[31m- 'FunRefSelectPar',{},...[m
[31m- 'RetSolFracMax',InPar.RetSolFracMax,...[m
[31m- 'MaxPeaks',InPar.MaxPeaks,...[m
[31m- 'Radius',InPar.Radius,...[m
[31m- 'SelectBest',InPar.SelectBest,...[m
[31m- 'MaxPeaksCheck',0,...[m
[31m- 'MinNinPeak',InPar.MinNinPeak);[m
[31m- [m
[31m- if (~isempty(IndBest))[m
[31m- Npeak = numel(ResT);[m
[31m- CellRot = num2cell(Rotation(Irot).*ones(Npeak,1));[m
[31m- [ResT(1:1:Npeak).Rot] = deal(CellRot{:});[m
[31m- CellFlipX = num2cell(InPar.Flip(Iflip,1).*ones(Npeak,1));[m
[31m- CellFlipY = num2cell(InPar.Flip(Iflip,2).*ones(Npeak,1));[m
[31m- [ResT(1:1:Npeak).FlipX] = deal(CellFlipX{:});[m
[31m- [ResT(1:1:Npeak).FlipY] = deal(CellFlipY{:});[m
[31m- if (isempty(Res))[m
[31m- Res = ResT;[m
[31m- else[m
[31m- Res = [Res(:); ResT(:)]; [m
[31m- end[m
[31m- end[m
[31m- end[m
[31m-end[m
[31m-[m
[31m-% return only solutions that their HistNmatch> MaxHistNmatch.*InPar.RetSolFracMax[m
[31m-if (~isempty(Res))[m
[31m- % sort by HistNmatch[m
[31m- [~,SI] = sort([Res.HistNmatch],2,'descend');[m
[31m- Res = Res(SI);[m
[31m- [m
[31m- MaxHistNmatch = max([Res.HistNmatch]);[m
[31m- FlagFracMax = [Res.HistNmatch]>(MaxHistNmatch.*InPar.RetSolFracMax);[m
[31m- Res = Res(FlagFracMax);[m
[31m- [m
[31m- [m
[31m-end[m
[31m-[m
[31m-[m
[31m-% Check if Res contains the multiple candidates of the same solution[m
[31m-if (numel(Res)>1)[m
[31m- DD = sqrt(([Res.HistShiftX] - [Res.HistShiftX]').^2 + ([Res.HistShiftY] - [Res.HistShiftY]').^2 );[m
[31m- CritDist = sqrt(InPar.SearchStepX.^2 + InPar.SearchStepY.^2).*1.5;[m
[31m- [DI,DJ] = find(triu(DD)>CritDist);[m
[31m- % keep only one solution [m
[31m- Res = Res([1;DI]);[m
[31m-end[m
[31m-[m
[31m-Npeak = numel(Res);[m
[31m-NpeakCheck = min(InPar.MaxPeaksCheck,Npeak); % max. number of peaks to check[m
[31m-%Res = struct_def({'IndRef','IndCat','MatchedCat','MatchedRef','MatchedResid','StdResid','Std','MeanErr'},Npeak,1);[m
[31m-for Ipeak=1:1:NpeakCheck[m
[31m- % given possible shift[m
[31m- % matche Cat and Ref [m
[31m- [m
[31m- Xcat = Cat(:,InPar.ColXc);[m
[31m- Ycat = Cat(:,InPar.ColYc);[m
[31m-% Xcat = Res(Ipeak).FlipX.*Xcat;[m
[31m-% Ycat = Res(Ipeak).FlipY.*Ycat;[m
[31m-[m
[31m- %!!!!!!!!!!!!!!!!!!!-----------------------!!!!!!!!!!!!!!!!!!!!![m
[31m- %[ResMatch,OutCat] = ImUtil.pattern.test_match(Cat,Ref,[Res(Ipeak).ShiftX, Res(Ipeak).ShiftY, Res(Ipeak).Rot./RAD],'Radius',InPar.Radius);[m
[31m- %[ResMatch,OutCat,CatS]= ImUtil.pattern.test_match([Xcat, Ycat],Ref,...[m
[31m- % [Res(Ipeak).ShiftX, Res(Ipeak).ShiftY, Res(Ipeak).Rot./RAD],...[m
[31m- % 'Radius',InPar.Radius,...[m
[31m- % 'Flip',[Res(Ipeak).FlipX, Res(Ipeak).FlipY]);[m
[31m- [m
[31m- %inserting the whole catalog instead of only X-Y columns[m
[31m- [ResMatch,OutCat,CatS]= ImUtil.pattern.test_match(Cat,Ref,...[m
[31m- 'ColXc',InPar.ColXc,...[m
[31m- 'ColYc',InPar.ColYc,...[m
[31m- [Res(Ipeak).ShiftX, Res(Ipeak).ShiftY, Res(Ipeak).Rot./RAD],...[m
[31m- 'Radius',InPar.Radius,...[m
[31m- 'Flip',[Res(Ipeak).FlipX, Res(Ipeak).FlipY]);[m
[31m- %!!!!!!!!!!!!!!!!!!!-----------------------!!!!!!!!!!!!!!!!!!!!![m
[31m- [m
[31m- Res(Ipeak).Nmatch = numel(ResMatch.IndCat);[m
[31m- %Res(Ipeak).MatchedCat = OutCat(ResMatch.IndCat,:);[m
[31m- Res(Ipeak).MatchedCat = CatS(ResMatch.IndCat,:);[m
[31m- %Res(Ipeak).MatchedCat = Cat(ResMatch.IndCat,1:2);[m
[31m- Res(Ipeak).MatchedRef = Ref(ResMatch.IndRef,:);[m
[31m- Res(Ipeak).ResidX = ResMatch.ResidX;[m
[31m- Res(Ipeak).ResidY = ResMatch.ResidY;[m
[31m- Res(Ipeak).Resid = sqrt(ResMatch.ResidX.^2 + ResMatch.ResidY.^2);[m
[31m- Res(Ipeak).StdX = ResMatch.StdX;[m
[31m- Res(Ipeak).StdY = ResMatch.StdY;[m
[31m- Res(Ipeak).Std = std(Res(Ipeak).Resid);[m
[31m- Res(Ipeak).Rstd = Util.stat.rstd(Res(Ipeak).Resid);[m
[31m- Res(Ipeak).MeanErr = Res(Ipeak).Rstd./sqrt(Res(Ipeak).Nmatch);[m
[31m-end[m
[31m-[m
[31m- [m
[31m-if (Npeak==0)[m
[31m- IndBest = [];[m
[31m-else[m
[31m- if (NpeakCheck==0)[m
[31m- % can use only MaxHistMatch[m
[31m- [~,IndBest] = min([Res.HistNmatch]);[m
[31m- else[m
[31m-[m
[31m- switch lower(InPar.SelectBest)[m
[31m- case 'meanerr'[m
[31m- [~,IndBest] = min([Res.MeanErr]);[m
[31m- case 'std'[m
[31m- [~,IndBest] = min([Res.Std]);[m
[31m- case 'rstd'[m
[31m- [~,IndBest] = min([Res.Rstd]);[m
[31m- case 'n'[m
[31m- [~,IndBest] = max([Res.Nmatch]);[m
[31m- case 'comb'[m
[31m- MaxNmatch = max([Res.Nmatch]);[m
[31m- IndGood = find([Res.Nmatch]>(MaxNmatch.*0.8));[m
[31m- [~,IndBest] = min([Res(IndGood).MeanErr]);[m
[31m- IndBest = IndGood(IndBest);[m
[31m- otherwise[m
[31m- error('Unknown SelectBest option');[m
[31m- end[m
[31m- end[m
[31m-end[m
[31m-[m
[31m-[m
[31m-[m
[31m-% % order Res by quality of solution[m
[31m-% switch lower(InPar.SortResBy)[m
[31m-% case 'n'[m
[31m-% [~,SI] = sort([Res.Nmatch]);[m
[31m-% otherwise[m
[31m-% error('Unknown SortResBy option');[m
[31m-% end[m
[31m-% Res = Res(SI);[m
[31m-[m
[31m-% go over all possible peaks in the 2D histogram of X/Y distances[m
[31m-% and for each possibility search for matches in the complete[m
[31m-% catalogs[m
[31m-%Npeak = numel(Res);[m
[31m-%NpeakCheck = min(InPar.MaxPeaksCheckInd,Npeak); % max. number of peaks to check[m
[31m-%Res = struct_def({'IndRef','IndCat','MatchedCat','MatchedRef','MatchedResid','StdResid','Std','MeanErr'},Npeak,1);[m
[31m-[m
[31m-% % select best[m
[31m-% Res = Res(1:NpeakCheck);[m
[31m-% for Ipeak=1:1:NpeakCheck[m
[31m-% % given possible shift[m
[31m-% % matche Cat and Ref [m
[31m-% [m
[31m-% [ResMatch,OutCat] = ImUtil.pattern.test_match(Cat,Ref,[Res(Ipeak).ShiftX, Res(Ipeak).ShiftY],'Radius',InPar.Radius);[m
[31m-% Res(Ipeak).Nmatch = numel(ResMatch.IndCat);[m
[31m-% Res(Ipeak).MatchedCat = OutCat(ResMatch.IndCat,:);[m
[31m-% Res(Ipeak).MatchedRef = Ref(ResMatch.IndRef,:);[m
[31m-% Res(Ipeak).ResidX = ResMatch.ResidX;[m
[31m-% Res(Ipeak).ResidY = ResMatch.ResidY;[m
[31m-% Res(Ipeak).Resid = sqrt(ResMatch.ResidX.^2 + ResMatch.ResidY.^2);[m
[31m-% Res(Ipeak).StdX = ResMatch.StdX;[m
[31m-% Res(Ipeak).StdY = ResMatch.StdY;[m
[31m-% Res(Ipeak).Std = std(Res(Ipeak).Resid);[m
[31m-% Res(Ipeak).Rstd = Util.stat.rstd(Res(Ipeak).Resid);[m
[31m-% Res(Ipeak).MeanErr = Res(Ipeak).Std./sqrt(Res(Ipeak).Nmatch);[m
[31m-% end[m
[1mdiff --git a/@AstCat/astrometry.m b/@AstCat/astrometry.m[m
[1mindex 2b45ee8..08a00a2 100644[m
[1m--- a/@AstCat/astrometry.m[m
[1m+++ b/@AstCat/astrometry.m[m
[36m@@ -295,7 +295,7 @@[m [mfor Isim=1:1:Nsim[m
[m
%apply the limit on the PM error (if given by the user)[m
if (~isempty(InPar.MaxPMerr))[m
[31m- IndForPMerr= RefCat.Cat(:,9)<InPar.MaxPMerr;[m
[32m+[m[32m IndForPMerr= RefCat.Cat(:,RefCat.Col.ErrPMRA)<InPar.MaxPMerr;[m
RefCat.Cat=RefCat.Cat(IndForPMerr,:);[m
end[m
%%% ----- !!!!!!!!!!! ----- !!!!!!!!!!!![m
[1mdiff --git a/@AstCat/astrometry.m~ b/@AstCat/astrometry.m~[m
[1mdeleted file mode 100644[m
[1mindex 21feae1..0000000[m
[1m--- a/@AstCat/astrometry.m~[m
[1m+++ /dev/null[m
[36m@@ -1,674 +0,0 @@[m
[31m-function [ResAst,OrigSim]=astrometry(Sim,varargin)[m
[31m-% Search astrometric solution to an image or catalog.[m
[31m-% Package: @AstCat[m
[31m-% Description: Search astrometric solution to an image or catalog.[m
[31m-% Given a guess coordinates to an image or catalogs (of X/Y[m
[31m-% positions), the image plate scale, and rough rotation,[m
[31m-% the program attemt to identify the stars in the image[m
[31m-% relative to stars in a reference catalog.[m
[31m-% The matched stars are than used to solve for a[m
[31m-% transformation between the X/Y coordinates and the RA/Dec.[m
[31m-% The program is designed to be robust against failures. As[m
[31m-% such the matching is doing in sub sections of the main image[m
[31m-% so if some are failed there is still a chance to recover.[m
[31m-% The fitting is done, by default, using orthogonal[m
[31m-% polynomilas that makes the solution more stable.[m
[31m-% Input : - An AstCat object or a SIM object.[m
[31m-% An AstCat object that contains a catalog of sources in the[m
[31m-% image, or a SIM object that contains an image and optionaly a[m
[31m-% catalog of sources. If the catalog of sources in a SIM object[m
[31m-% is not provided, then the sources will be extracted.[m
[31m-% * Arbitrary number of pairs of arguments: ...,keyword,value,...[m
[31m-% where keyword are one of the followings:[m
[31m-% '[m
[31m-% Output : - A structure array of astrometric solutions and quality[m
[31m-% parameters for each image.[m
[31m-% - [m
[31m-% License: GNU general public license version 3[m
[31m-% By : Eran O. Ofek Jan 2018[m
[31m-% URL : http://weizmann.ac.il/home/eofek/matlab/[m
[31m-% Example: ResAst=astrometry(Sim);[m
[31m-% Reliable: 2[m
[31m-%--------------------------------------------------------------------------[m
[31m-[m
[31m-RAD = 180./pi;[m
[31m-[m
[31m-CatField = AstCat.CatField;[m
[31m-OrigColXY = {'dup_X','dup_Y'};[m
[31m-[m
[31m-[m
[31m-[m
[31m-%--- Source extractor parameters ---[m
[31m-DefV.RePopCat = false;[m
[31m-DefV.SrcExtractorProg = @mextractor;[m
[31m-DefV.SrcExtractorPar = {};[m
[31m-%--- Approximate guess position ---[m
[31m-DefV.Scale = 1.01; % arcsec/pix[m
[31m-DefV.RA = []; % rad,...[m
[31m-DefV.Dec = []; % rad,...[m
[31m-DefV.Equinox = 2000.0;[m
[31m-DefV.KeyScale = 'SCALE'; %[m
[31m-DefV.KeyRA = {'RA','OBJRA','OBJRAD','CRVAL1'};[m
[31m-DefV.KeyDec = {'DEC','OBJDEC','OBJDECD','CRVAL2'};[m
[31m-DefV.KeyEquinox = {'EQUINOX'};[m
[31m-DefV.UnitsRA = []; % [], 'deg','r'[m
[31m-DefV.UnitsDec = []; % [], 'deg','r'[m
[31m-%--- Reference catalog ---[m
[31m-DefV.RefCat = 'GAIADR2'; %@get_ucac4; %@wget_ucac4; % string, function, struct[m
[31m-DefV.RCrad = 0.8./RAD; %0.8/RAD; % [radian][m
[31m-DefV.RefCatMagRange = [12 19.0]; %19.0];[m
[31m-DefV.UseMagRangeCat = false;[m
[31m-DefV.MaxRefStars = 15000;[m
[31m-DefV.Shape = 'box';[m
[31m-DefV.RC_ColRA = 'RA';[m
[31m-DefV.RC_ColDec = 'Dec';[m
[31m-DefV.RC_ColMag = 'Mag_G'; %'MagModel'; %'ModelMag';[m
[31m-DefV.RC_ColColor = 'Mag_BP-Mag_RP';[m
[31m-DefV.ApplyPM = false; %true; %true;[m
[31m-DefV.RC_ColPM_RA = 'PMRA';[m
[31m-DefV.RC_ColPM_Dec = 'PMDec';[m
[31m-DefV.RC_ColPlx = 'Plx';[m
[31m-DefV.RC_ColRV = 'RV';[m
[31m-DefV.RC_EpochInRA = 'Epoch';[m
[31m-DefV.RC_EpochInDec = 'Epoch';[m
[31m-DefV.RC_EpochInUnits = 'yr';[m
[31m-DefV.CutRefCat = true; %false;[m
[31m-%--- Catalog ---[m
[31m-DefV.ImSize = []; % [x,y][m
[31m-DefV.ColXc = {'XWIN_IMAGE','X','xpos'};[m
[31m-DefV.ColYc = {'YWIN_IMAGE','Y','ypos'};[m
[31m-DefV.CatColMag = 'MAG_PSF'; % Mag column name in SIM[m
[31m-%--- Cleaning ---[m
[31m-DefV.CleanLines = true;[m
[31m-DefV.FlagLinesPar = {};[m
[31m-DefV.CleanOverDense = true;[m
[31m-DefV.FlagOverdensePar = {};[m
[31m-%--- Matching ---[m
[31m-DefV.Flip = [1 -1]; %1 1];[m
[31m-DefV.MatchRotMethod = 'xcrot'; % 'scan' | 'xcrot'[m
[31m-DefV.HistDistEdges = (12:3:300)';[m
[31m-DefV.MinRot = -15;[m
[31m-DefV.MaxRot = +15;[m
[31m-DefV.StepRot = 0.5;[m
[31m-DefV.ReFindMatches = true;[m
[31m-DefV.SearchRangeX = [-1000 1000]; %[-1000 1000];[m
[31m-DefV.SearchRangeY = [-1000 1000]; %[-1000 1000];[m
[31m-DefV.SearchRangeFactor = 0.5;[m
[31m-DefV.SearchStepX = 4; %3;[m
[31m-DefV.SearchStepY = 4; %3;[m
[31m-DefV.SearchRad = 4; %3; % pix[m
[31m-DefV.MaxMethod = 'adaptive'; %'sort'; % {'max1'|'sort'|'[m
[31m-DefV.MinNinPeak = 5; % min number of matches in peak - if <1 then fraction of ListCat[m
[31m-DefV.MaxPeaks = 10; % maximum number of peaks to return[m
[31m-DefV.MaxPeaksCheckI = 5; % 10 % out of the returned peaks - this is the maximum nuber of peaks to check[m
[31m-DefV.MaxPeaksCheckF = 5; % 10 % out of the returned peaks - this is the maximum nuber of peaks to check[m
[31m-DefV.SelectBest = 'comb'; % {'N','std','meanerr','comb'}[m
[31m-DefV.FalseAlarm = 1e-4;[m
[31m-DefV.RegionMaxConn = 8;[m
[31m-DefV.AdjustStarDensity = true;[m
[31m-DefV.MaxDensityAdjust = 0.5; % 0.5;[m
[31m-DefV.NstarsRescaleBlock = 9000; % if Nstars> then shrink blocksize by x2 and increase StepRot by x2[m
[31m-DefV.ReScaleFactor = 2;[m
[31m-DefV.BlockSize = [1024 1024]; %[512 512]; %[1024 1024]; %'full';[m
[31m-DefV.BufferSize = 200;[m
[31m-[m
[31m-%--- Fitting ---[m
[31m-%DefV.UseCase_TranC = {'affine', 5}; %{'affine_tt_cheby2_4', 100; 'affine_tt_cheby2_3', 70; 'affine_tt', 20; 'affine', 5};[m
[31m-DefV.UseCase_TranC = {'affine_tt_cheby2_4', 100; 'affine_tt_cheby2_3', 70; 'affine_tt', 10; 'affine', 5};[m
[31m-DefV.Niter = 1;[m
[31m-DefV.SigClip = 5;[m
[31m-DefV.MaxResid = 1./3600; % assuming coordinates in deg[m
[31m-%--- Analysis ---[m
[31m-DefV.AnalysisBlockSize = [512 512];[m
[31m-%--- Verbose/plot ---[m
[31m-DefV.Verbose = true;[m
[31m-DefV.Plot = false;[m
[31m-[m
[31m-%maximum Excess noise in the reference catalog (GAIA)[m
[31m-DefV.MaxExcessNoise = 10;[m
[31m-%threshold for proper motion errors(GAIA)[m
[31m-DefV.MaxPMerr = [];[m
[31m-%Logical for applying parallax correction (barycentric)[m
[31m-DefV.ApplyParallax = false;[m
[31m-[m
[31m-InPar = InArg.populate_keyval(DefV,varargin,mfilename);[m
[31m-[m
[31m-% number of images[m
[31m-Nsim = numel(Sim);[m
[31m-[m
[31m-%------------------------[m
[31m-%--- source extractor ---[m
[31m-%------------------------[m
[31m-if (SIM.issim(Sim))[m
[31m- IsCatPop = isfield_populated(Sim, CatField);[m
[31m- if (~any(IsCatPop))[m
[31m- Sim(~IsCatPop) = InPar.SrcExtractorProg(Sim(~IsCatPop),InPar.SrcExtractorPar{:});[m
[31m- else[m
[31m- if (InPar.RePopCat)[m
[31m- Sim(~IsCatPop) = InPar.SrcExtractorProg(Sim(~IsCatPop),InPar.SrcExtractorPar{:});[m
[31m- end[m
[31m- end[m
[31m-else[m
[31m- % assume SIM is an AstCat object with populated catalog[m
[31m-end[m
[31m-[m
[31m-%-----------------------------------------------[m
[31m-%--- Convert user supplied coo user to J2000 ---[m
[31m-%-----------------------------------------------[m
[31m-if (~isempty(InPar.RA) && ~isempty(InPar.Dec))[m
[31m- % User supplied RA/Dec[m
[31m- RA = celestial.coo.convertdms(InPar.RA,'gH','r');[m
[31m- Dec = celestial.coo.convertdms(InPar.Dec,'gD','R');[m
[31m- if (InPar.Equinox~=2000)[m
[31m- Coo2000 = celestial.coo.coco([RA,Dec],sprintf('j%06.1f',InPar.Equinox),'j2000.0'); % Na'ama, 20180524[m
[31m- RA = Coo2000(:,1);[m
[31m- Dec = Coo2000(:,2);[m
[31m- end [m
[31m-else[m
[31m- % Read RA/Dec from header[m
[31m- Out = getcoo(Sim,'KeyRA',InPar.KeyRA,'KeyDec',InPar.KeyDec,'KeyEquinox',InPar.KeyEquinox,'OutUnits','rad');[m
[31m- Coo2000 = celestial.coo.coco([[Out.RA].',[Out.Dec].'],sprintf('j%06.1f',Out(1).Equinox),'j2000.0');[m
[31m- RA = Coo2000(:,1);[m
[31m- Dec = Coo2000(:,2);[m
[31m-end[m
[31m-[m
[31m-%--- Read scale ---[m
[31m-if (isempty(InPar.Scale))[m
[31m- Val = cell2mat(mgetkey(Sim,InPar.KeyScale));[m
[31m- if any(isnan(Val))[m
[31m- error('Some header scale keyword values are not available');[m
[31m- end[m
[31m- [m
[31m- InPar.Scale = Val;[m
[31m-else[m
[31m- InPar.Scale = InPar.Scale(:).*ones(Nsim,1);[m
[31m-end[m
[31m-[m
[31m-% Debuging [m
[31m-%Dec = Dec - 10./3600./RAD;[m
[31m-% [RA,Dec]=xy2coo(S,[1; 1024],[1; 2048]); [RA1,Dec1]=xy2coo(S1,[1; 1024],[1; 2048]); (RA-RA1).*RAD.*3600, (Dec-Dec1).*RAD.*360[m
[31m-[m
[31m-%------------------[m
[31m-%--- Image size ---[m
[31m-%------------------[m
[31m-if (~isempty(InPar.ImSize))[m
[31m- ImSize = InPar.ImSize; % [X, Y][m
[31m-else[m
[31m- ImSize = imagesize(Sim); % [X, Y][m
[31m-end[m
[31m-[m
[31m-[m
[31m-OrigSim = Sim;[m
[31m-[m
[31m-% astrometry for each image[m
[31m-FitRes = Util.struct.struct_def({'Par','ParErr','Resid','ResidX','ResidY','ResidT',...[m
[31m- 'Rrms','Chi2','Nobs','Npar','Ndof','rms','AstT','Flag','rmsAll','RrmsAll','Wrms','MagRef','Xref','Yref'},Nsim,1);[m
[31m-Log = Util.struct.struct_def({'PatternMatch','FitMerge','FitFinal','Analysis'},Nsim,1);[m
[31m-ReturnFlag = false(Nsim,1);[m
[31m-for Isim=1:1:Nsim[m
[31m-[m
[31m- [m
[31m- Scale = InPar.Scale(Isim);[m
[31m- [m
[31m- %!!!!!!!!!!!!!!!!!!!-----------------------!!!!!!!!!!!!!!!!!!!!![m
[31m- %add column with the order of the objects in the catalog [m
[31m- %Y location sorted in order find the objects that used in the fit[m
[31m- [m
[31m- [m
[31m- [~,ColXc] = select_exist_colnames(Sim(Isim),InPar.ColXc(:));[m
[31m- [~,ColYc] = select_exist_colnames(Sim(Isim),InPar.ColYc(:));[m
[31m- [m
[31m- Sim(Isim).Cat(:,end+1)=(1:1:length(Sim(Isim).Cat(:,ColXc))).'; [m
[31m-[m
[31m- Sim(Isim) = col_insert(Sim(Isim),...[m
[31m- (1:1:length(Sim(Isim).Cat(:,ColXc))).', [m
[31m- [m
[31m- Sim(Isim).Col.IndexSimYsorted=length(Sim(Isim).Cat(1,:)); [m
[31m- [m
[31m- Sim(Isim).ColCell{end+1}='IndexSimYsorted'; [m
[31m- [m
[31m- [m
[31m- %!!!!!!!!!!!!!!!!!!!-----------------------!!!!!!!!!!!!!!!!!!!!![m
[31m- %--------------------------[m
[31m- %--- Reference Catalaog ---[m
[31m- %--------------------------[m
[31m- if (AstCat.isastcat(InPar.RefCat) || isstruct(InPar.RefCat))[m
[31m- % RefCat was provided[m
[31m- RefCat = InPar.RefCat;[m
[31m-[m
[31m- % convert to AstCat object[m
[31m- RefCat = AstCat.struct2astcat(RefCat);[m
[31m- else[m
[31m- % External catalog was not provided[m
[31m- % try to retrieve[m
[31m- RefCat = VO.search.cat_cone(InPar.RefCat,RA(1),Dec(1),InPar.RCrad,'RadiusUnits','rad','OutType','astcat');[m
[31m- end[m
[31m- [m
[31m- % what to do if RefCat is empty[m
[31m- if (isempty(RefCat.Cat))[m
[31m- % No reference catalog found - astrometric solution failed[m
[31m- %ResAst(Isim) = [];[m
[31m- warning('Reference catalog is empty - no solution found');[m
[31m- else[m
[31m- % RefCat is not empty[m
[31m- [m
[31m- % clean the GAIA catalog[m
[31m- switch lower(InPar.RefCat)[m
[31m- case 'gaiadr1'[m
[31m- % remove sources with excess noise >5 sigma and outside the[m
[31m- % mag range[m
[31m- InPar.RC_ColMag = 'MagG'; % override mag column[m
[31m- F = RefCat.(CatField)(:,8) < 80 & RefCat.(CatField)(:,5)> InPar.RefCatMagRange(1) & RefCat.(CatField)(:,5)< InPar.RefCatMagRange(2);[m
[31m- RefCat.(CatField) = RefCat.(CatField)(F,:);[m
[31m- [m
[31m- case 'gaiadr2'[m
[31m- MagG = col_get(RefCat,{InPar.RC_ColMag});[m
[31m- ExcessNoise = col_get(RefCat,{'ExcessNoise'});[m
[31m- F = ExcessNoise<InPar.MaxExcessNoise & MagG> InPar.RefCatMagRange(1) & MagG< InPar.RefCatMagRange(2);[m
[31m- RefCat.(CatField) = RefCat.(CatField)(F,:);[m
[31m- [m
[31m- [m
[31m- end[m
[31m- [m
[31m- %---------------------------[m
[31m- %--- apply proper motion ---[m
[31m- %---------------------------[m
[31m- if (InPar.ApplyPM)[m
[31m- % applay proper motion, RV and parallax to star positions[m
[31m- EpochOut = julday(Sim); % get JD of image - (image epoch)[m
[31m- %%% ----- !!!!!!!!!!! ----- !!!!!!!!!!!![m
[31m- [m
[31m- %The field 'ApplyParallax' added to the apply proper motion[m
[31m- %call[m
[31m- RefCat = apply_proper_motion(RefCat,'EpochInRA',InPar.RC_EpochInRA,...[m
[31m- 'EpochInDec',InPar.RC_EpochInDec,...[m
[31m- 'EpochInUnits',InPar.RC_EpochInUnits,...[m
[31m- 'EpochOut',EpochOut,...[m
[31m- 'EpochOutUnits','JD',...[m
[31m- 'ColPM_RA',InPar.RC_ColPM_RA,...[m
[31m- 'ColPM_Dec',InPar.RC_ColPM_Dec,...[m
[31m- 'ColPlx',InPar.RC_ColPlx,...[m
[31m- 'ColRV',InPar.RC_ColRV, ...[m
[31m- 'ApplyParallax', InPar.ApplyParallax);[m
[31m- [m
[31m- %apply the limit on the PM error (if given by the user)[m
[31m- if (~isempty(InPar.MaxPMerr))[m
[31m- IndForPMerr= RefCat.Cat(:,9)<InPar.MaxPMerr;[m
[31m- RefCat.Cat=RefCat.Cat(IndForPMerr,:);[m
[31m- end[m
[31m- %%% ----- !!!!!!!!!!! ----- !!!!!!!!!!!![m
[31m- end[m
[31m-[m
[31m- % Generate a version of the reference catalog with only selected columns[m
[31m- % RA, Dec, Mag, Color[m
[31m- [RC_ColRA, RC_ColDec, RC_ColMag] = colname2ind(RefCat,{InPar.RC_ColRA, InPar.RC_ColDec, InPar.RC_ColMag});[m
[31m- RC = col_arith(RefCat,{InPar.RC_ColRA,InPar.RC_ColDec,...[m
[31m- InPar.RC_ColMag,InPar.RC_ColColor},...[m
[31m- 'astcat',true);[m
[31m- RC.ColCell = {InPar.RC_ColRA,InPar.RC_ColDec,'Mag','Color'};[m
[31m- RC_ColMag = 3; % Mag in 3rd column[m
[31m- RC = colcell2col(RC);[m
[31m-[m
[31m- % applay MaxRefStars[m
[31m- % If Ref catalogs have too many starts select only the brightest[m
[31m- NrefStars = size(RC.(CatField),1);[m
[31m- if (NrefStars>InPar.MaxRefStars)[m
[31m- [~,IsM] = sort(RC.(CatField)(:,RC_ColMag));[m
[31m- MaxMag = RC.(CatField)(IsM(InPar.MaxRefStars),RC_ColMag);[m
[31m- FlagMM = RC.(CatField)(:,RC_ColMag)<MaxMag;[m
[31m- RC.(CatField) = RC.(CatField)(FlagMM,:);[m
[31m- [m
[31m- end[m
[31m- [m
[31m- [m
[31m- % calculate average density of sources in RefCat (RC):[m
[31m- % this is used to adjust the number of stars in the catalog and reference.[m
[31m- DensityRef = size(RC.Cat,1)./(pi.*(InPar.RCrad.*RAD).^2); % stars/deg^2[m
[31m-[m
[31m-[m
[31m- % project catalog coordinates from celestial sphere to plane[m
[31m- % X,Y in units of pixels (using first guess pixel scale).[m
[31m- % Note that the X=0,Y=0 position is the position defined[m
[31m- % by the guess RA/Dec (RA, Dec parameters).[m
[31m- % This are the "projection-plan coordinates"[m
[31m- % relative to the native long and lat of the fiducial point[m
[31m- [X,Y]=projection(RC,'tan',[RC_ColRA RC_ColDec],[RAD.*3600./Scale RA Dec],'rad');[m
[31m- % add the projected X/Y as the 1st and 2nd columns in the reference catalog[m
[31m- ColRef = {'X','Y'};[m
[31m- RC = col_insert(RC,X,1,'X');[m
[31m- RC = col_insert(RC,Y,2,'Y');[m
[31m- RC_ColMag = 5; % Mag is now in the 5th column![m
[31m- % sort the reference catalog by the Y position[m
[31m- RC = sortrows(RC,'Y');[m
[31m- % plot(RC.Cat(:,1),RC.Cat(:,2),'.')[m
[31m-[m
[31m- %------------------[m
[31m- %--- Prep image ---[m
[31m- %------------------[m
[31m- CatColMagInd = colname2ind(Sim(Isim),InPar.CatColMag);[m
[31m- [m
[31m- % mag range[m
[31m- if (InPar.UseMagRangeCat)[m
[31m- FlagM = Sim(Isim).(CatField)(:,CatColMagInd)>InPar.RefCatMagRange(1) & ...[m
[31m- Sim(Isim).(CatField)(:,CatColMagInd)<InPar.RefCatMagRange(2);[m
[31m- Sim(Isim).(CatField) = Sim(Isim).(CatField)(FlagM,:);[m
[31m- [m
[31m- end[m
[31m- [m
[31m- % clean sources out of image boundries[m
[31m- [~,ColXc] = select_exist_colnames(Sim(Isim),InPar.ColXc(:));[m
[31m- [~,ColYc] = select_exist_colnames(Sim(Isim),InPar.ColYc(:));[m
[31m- [m
[31m- FlagIn = Sim(Isim).(CatField)(:,ColXc)>0 & ...[m
[31m- Sim(Isim).(CatField)(:,ColXc)<ImSize(1) & ...[m
[31m- Sim(Isim).(CatField)(:,ColYc)>0 & ...[m
[31m- Sim(Isim).(CatField)(:,ColYc)<ImSize(2);[m
[31m- Sim(Isim).(CatField) = Sim(Isim).(CatField)(FlagIn,:);[m
[31m- [m
[31m- % clean lines/rows[m
[31m- % search for excess of sources on the same line/row and remove[m
[31m- if (InPar.CleanLines)[m
[31m- FlagCat = flag_bleeding_src(Sim(Isim),InPar.FlagLinesPar{:});[m
[31m- Sim(Isim).(CatField) = Sim(Isim).(CatField)(~FlagCat.Flag,:);[m
[31m- end[m
[31m-[m
[31m- % clean overdensity regions[m
[31m- % search for excess f sources in some regions and remove[m
[31m- if (InPar.CleanOverDense)[m
[31m- FlagCat = flag_overdense_src(Sim,InPar.FlagOverdensePar{:});[m
[31m- Sim(Isim).(CatField) = Sim(Isim).(CatField)(~FlagCat.Flag,:);[m
[31m- end[m
[31m- [m
[31m- % adjust number of stars in Sim such that it will be roughly equal to[m
[31m- % the star density in the reference image[m
[31m- [m
[31m- [m
[31m- % Note that density adjustment can be done only if stars magnitudes[m
[31m- % are provided[m
[31m- % Check if magnitude is ok[m
[31m- if (~isempty(CatColMagInd))[m
[31m- if (numel(unique(Sim(Isim).(CatField)(:,CatColMagInd)))>1)[m
[31m- ValidMag = true;[m
[31m- else[m
[31m- ValidMag = false;[m
[31m- end[m
[31m- else[m
[31m- ValidMag = false;[m
[31m- end[m
[31m- if (~ValidMag && InPar.Verbose)[m
[31m- warning('Invalid magnitudes - avoid using');[m
[31m- end[m
[31m-[m
[31m- if (InPar.AdjustStarDensity && ValidMag)[m
[31m- DensityCat = size(Sim(Isim).Cat,1)./prod(ImSize(Isim,:).*Scale./3600);[m
[31m- [m
[31m- DensityRatio = DensityRef./DensityCat;[m
[31m- [m
[31m- if (DensityRatio<1)[m
[31m- [m
[31m- % adjust the number of sources in the Sim image[m
[31m- Fraction = max(DensityRatio, InPar.MaxDensityAdjust);[m
[31m- Quant = quantile(Sim(Isim).Cat(:,CatColMagInd),Fraction);[m
[31m- Flag = Sim(Isim).Cat(:,CatColMagInd)<Quant;[m
[31m- Sim(Isim).Cat = Sim(Isim).Cat(Flag,:);[m
[31m- [m
[31m- [m
[31m- else[m
[31m- DensityRatio = 1./DensityRatio;[m
[31m- [m
[31m- % adjust the number of sources in the Ref [m
[31m- Fraction = max(DensityRatio, InPar.MaxDensityAdjust);[m
[31m- Quant = quantile(RC.Cat(:,RC_ColMag),Fraction);[m
[31m- Flag = RC.Cat(:,RC_ColMag)<Quant;[m
[31m- RC.Cat = RC.Cat(Flag,:);[m
[31m- end[m
[31m- [m
[31m- end[m
[31m-[m
[31m- [m
[31m- [m
[31m- [m
[31m- % define VecRot[m
[31m- % define BlockSize and step size based on number of stars[m
[31m- Nsrc = size(Sim(Isim).(CatField),1);[m
[31m- if (Nsrc>InPar.NstarsRescaleBlock)[m
[31m- if (InPar.Verbose)[m
[31m- fprintf('ReScale BlockSize and StepRot by %f\n',InPar.ReScaleFactor);[m
[31m- end[m
[31m- BlockSize = InPar.BlockSize./InPar.ReScaleFactor;[m
[31m- VecRot = (InPar.MinRot:InPar.ReScaleFactor.*InPar.StepRot:InPar.MaxRot).';[m
[31m- else[m
[31m- BlockSize = InPar.BlockSize;[m
[31m- VecRot = (InPar.MinRot:InPar.StepRot:InPar.MaxRot).';[m
[31m- end[m
[31m-[m
[31m- % get X and Y column indices in SIM[m
[31m- %[ColXc,ColYc] = colname2ind(Sim(Isim),{InPar.ColXc,InPar.ColYc}); [m
[31m- %[~,ColXc] = select_exist_colnames(Sim(Isim),InPar.ColXc(:));[m
[31m- %[~,ColYc] = select_exist_colnames(Sim(Isim),InPar.ColYc(:));[m
[31m- [m
[31m-[m
[31m- % Construct a table with [X,Y] columns only[m
[31m-[m
[31m- SimCat = AstCat.sim2astcat(Sim(Isim));[m
[31m-[m
[31m- % Duplicate X/Y columns in SimCat[m
[31m- % Original X/Y are named: 'dup_X' and 'dup_Y'[m
[31m- % transformed X/Y are '*WIN_IMAGE'...[m
[31m- SimCat = col_duplicate(SimCat,[ColXc,ColYc],OrigColXY);[m
[31m-[m
[31m- % select sources in sub image [m
[31m- % If the solution is done in sub image then this generate[m
[31m- % a catalog for each sub image (block).[m
[31m- [m
[31m- SubCat = subcat_regional(SimCat,ImSize(Isim,:),BlockSize,InPar.BufferSize,[ColXc,ColYc]);[m
[31m- Nsub = numel(SubCat);[m
[31m- [m
[31m- ResBest = Util.struct.struct_def({'MaxHistMatch','MaxHistShiftX','MaxHistShiftY',...[m
[31m- 'ShiftX','ShiftY','Tran','Nmatch','IndRef','IndCat',...[m
[31m- 'MatchedCat','MatchedRef','MatchedResid','StdResid',...[m
[31m- 'Std','MeanErr','BestRot','BestFlip'},Nsub,1);[m
[31m- [m
[31m- ShiftRes = nan(Nsub,2);[m
[31m- ImCenter = ImSize(Isim,:).*0.5;[m
[31m- for Isub=1:1:Nsub[m
[31m- %Isub[m
[31m- % for each sub region[m
[31m- [m
[31m- % match ref catalog with image catalog[m
[31m- % find also the correct image flip and rotation.[m
[31m- % The origin of the reference catalog (RC) is [RA, Dec].[m
[31m- % The origin of the image is its geometric center.[m
[31m- % Note that the output MatchedCat[m
[31m- % contains the columns: {'XWIN_IMAGE' 'YWIN_IMAGE' 'dup_X' 'dup_Y'}[m
[31m- [m
[31m- [m
[31m- % SubCat contains sources in a sub image region[m
[31m- % next line transform its coordinates from image corner to[m
[31m- % image center (i.e., similar to that of the reference catalog)[m
[31m- SubCat(Isub).(CatField)(:,[ColXc, ColYc]) = SubCat(Isub).(CatField)(:,[ColXc, ColYc]) - ImCenter; % +[50 50];[m
[31m- [m
[31m- switch lower(InPar.MatchRotMethod)[m
[31m- case 'xcrot'[m
[31m- % cross match the rotation using histogram of[m
[31m- % distances/angles[m
[31m- ResRot = ImUtil.pattern.match_pattern_rot(SubCat(Isub).(CatField),RC.(CatField),...[m
[31m- 'CatColX',ColXc,...[m
[31m- 'CatColY',ColYc,...[m
[31m- 'HistDistEdges',InPar.HistDistEdges,...[m
[31m- 'CutRefCat',InPar.CutRefCat,...[m
[31m- 'SearchRangeX',InPar.SearchRangeX,...[m
[31m- 'SearchRangeY',InPar.SearchRangeY);[m
[31m-[m
[31m- % go over all rotational possibilities[m
[31m- Nrot = size(ResRot.MatchedRot,1);[m
[31m- K = 0;[m
[31m- VecRotSel = [];[m
[31m- for Irot=1:1:Nrot[m
[31m- if (any(all(ResRot.MatchedRot(Irot,3:4)==InPar.Flip,2)))[m
[31m- K = K + 1;[m
[31m- VecRotSel(K) = ResRot.MatchedRot(Irot,1);[m
[31m- end[m
[31m- end[m
[31m- [m
[31m- if (isempty(VecRotSel))[m
[31m- % No rotation candidate solution found[m
[31m- % go back to scanning[m
[31m- VecRotSel = VecRot;[m
[31m- else[m
[31m- [m
[31m- Fvrs = VecRotSel<InPar.MaxRot | (VecRotSel>InPar.MinRot | VecRotSel>(360+InPar.MinRot));[m
[31m- VecRotSel = VecRotSel(Fvrs);[m
[31m- %ImUtil.pattern.match_pattern_rot finds minus the rotation[m
[31m- VecRotSel = -VecRotSel;[m
[31m- end[m
[31m- case 'scan'[m
[31m- % match rotation by scanning all possible rotations[m
[31m- VecRotSel = VecRot;[m
[31m- otherwise[m
[31m- error('Unknown MatchRotMethod option');[m
[31m- end[m
[31m- [m
[31m- [Res,IndBest,H] = ImUtil.pattern.match_pattern_shift_rot(SubCat(Isub).(CatField),RC.(CatField),...[m
[31m- VecRotSel,...[m
[31m- 'ColXc',ColXc,...[m
[31m- 'ColYc',ColYc,...[m
[31m- 'Flip',InPar.Flip,...[m
[31m- 'CutRefCat',InPar.CutRefCat,...[m
[31m- 'SearchRangeX',InPar.SearchRangeX,...[m
[31m- 'SearchRangeY',InPar.SearchRangeY,...[m
[31m- 'SearchRangeFactor',InPar.SearchRangeFactor,...[m
[31m- 'SearchStepX',InPar.SearchStepX,...[m
[31m- 'SearchStepY',InPar.SearchStepY,...[m
[31m- 'Radius',InPar.SearchRad);[m
[31m- [m
[31m- [m
[31m- if (~isempty(Res) && ~isempty(IndBest)) [m
[31m- ShiftRes(Isub,:) = [Res(IndBest).ShiftX, Res(IndBest).ShiftY];[m
[31m- [m
[31m- ResSub(Isub) = Res(IndBest); [m
[31m- end[m
[31m- [m
[31m- end[m
[31m- [m