-
Notifications
You must be signed in to change notification settings - Fork 0
/
PCAObjects.cpp
1222 lines (963 loc) · 31.7 KB
/
PCAObjects.cpp
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 "PCAObjects.h"
#include <CCfits/CCfits>
#include "TMV.h"
#include <sstream>
#include "myTypeDef.h"
#include "myIO.h"
#include <cassert>
#include "Log.h"
#include "Image.h"
#include "NR.h"
#include <fstream>
#include <algorithm>
#include <cstdlib>
namespace PCA {
int myrandom (int i) { return std::rand()%i;}
using std::cout;
using std::endl;
// reeturn legendre polynomial of specified order at single point centered on image
static DVector definePXY(int order, float x, float xmin, float xmax)
{
DVector temp(order+1);
double newx = (2.*x-xmin-xmax)/(xmax-xmin);
temp[0] = 1.;
if(order>0) temp[1] = newx;
for(int i=2;i<=order;++i) {
temp[i] = ((2.*i-1.)*newx*temp[i-1] - (i-1.)*temp[i-2])/i;
}
return temp;
}
// return matrix of x,y points
static void setPRow(int fitorder, Position<float> pos, const Bounds<float>& bounds, DVectorView prow)
{
//Assert(int(prow.size()) == (fitorder+1)*(fitorder+2)/2);
DVector px =
definePXY(fitorder,pos.x,bounds.getXMin(),bounds.getXMax());
DVector py =
definePXY(fitorder,pos.y,bounds.getYMin(),bounds.getYMax());
int pq = 0;
for(int n=0;n<=fitorder;++n) {
for(int p=n,q=n-p;q<=n;--p,++q) {
//Assert(pq < int(prow.size()));
prow(pq) = px[p]*py[q];
++pq;
}
}
//Assert(pq == int(prow.size()));
}
template<class T>
int Cell<T>::getNGood() {
int ngood=0;
for(int i=0;i<dets.size();++i) {
if(!dets[i]->isClipped()) ngood++;
}
return ngood;
}
template<class T>
int Cell<T>::getNClip() {
int nclip=0;
for(int i=0;i<dets.size();++i) {
}
return nclip;
}
// Return the mean values of all the detections in a cell
// if no detections found it will return a zero
template<class T>
std::vector<T> Cell<T>::getMeanVals()
{
int ngood=this->getNGood();
std::vector<T> v(nvar,defaultVal);
if (ngood==0 ) {
FILE_LOG(logDEBUG1)<<" this cell does not have at least one detections. "<<endl;
this->setMissing(true);
return v;
}
if (this->isMissing() ) {
FILE_LOG(logDEBUG1)<<" this cell has been labeled missing. "<<endl;
return v;
}
for(int j=0;j<nvar;++j) v[j]=0;
for(int i=0;i<dets.size();++i) {
if(dets[i]->isClipped()) continue;
for(int j=0;j<nvar;++j) {
v[j]+=dets[i]->getVal(j);
}
}
for(int j=0;j<nvar;++j) {
v[j]/=dets.size();
FILE_LOG(logDEBUG1)<<" Mean Cell var :"<<j<<" "<<v[j]<<endl;
}
return v;
}
// Return the median values of all the detections in a cell
template<class T>
std::vector<T> Cell<T>::getMedianVals()
{
std::vector<T> v(nvar,defaultVal);
int ngood=this->getNGood();
if(ngood<3) {
FILE_LOG(logDEBUG1)<<" this cell does not have at least three detections. "<<endl;
this->setMissing(true);
return v;
}
for(int j=0;j<nvar;++j) v[j]=0;
for(int j=0;j<nvar;++j) {
std::vector<T> tmp;
for(int i=0;i<dets.size();++i) {
if(dets[i]->isClipped()) continue;
tmp.push_back(dets[i]->getVal(j));
}
v[j]=median<T>(tmp);
FILE_LOG(logDEBUG2)<<"Median Cell var "<<j<<" "<<v[j]<<endl;
}
return v;
}
// Return the median values of all the detections in a cell
template<class T>
std::vector<T> Cell<T>::getMeanClipVals(float clip)
{
std::vector<T> v(nvar,defaultVal);
FILE_LOG(logDEBUG1)<<" Meanclips with :"<<dets.size()<<endl;
int ngood=this->getNGood();
if (this->isMissing() ) {
FILE_LOG(logDEBUG1)<<" this cell has been labeled missing. "<<endl;
return v;
}
if(ngood<1) {
FILE_LOG(logDEBUG1)<<" this cell does not have at least one detections. "<<endl;
this->setMissing(1);
return v;
}
for(int j=0;j<nvar;++j) v[j]=0;
//iterate once through the variables to label outliers in any
// of the variables using the the median absolute deviation
int nclip=0;
for(int j=0;j<nvar;++j) {
FILE_LOG(logDEBUG1)<<" Getting variable: "<<j<<endl;
std::vector<T> tmp(ngood);
int cur=0;
for(int i=0;i<dets.size();++i) {
if(dets[i]->isClipped()) continue;
tmp[cur]=(dets[i]->getVal(j));
cur++;
}
double mad;
double median=median_mad(tmp,mad);
FILE_LOG(logDEBUG1)<<" Found "<<tmp.size()
<<" objects with median: "<<median
<<" mad: "<<mad<<endl;
cur=0;
int ngood=0;
for(int i=0;i<dets.size();++i) {
if(dets[i]->isClipped()) continue;
if( std::abs(dets[i]->getVal(j)-median) > clip*mad) {
FILE_LOG(logDEBUG2)<<" Clipping object "<<i
<<" diff from median: "
<< std::abs(dets[i]->getVal(j)-median)
<<" max allowed:"<<clip*mad<<endl;
nclip++;
}
else {
v[j]+=dets[i]->getVal(j);
FILE_LOG(logDEBUG2)<<" adding "<<dets[i]->getVal(j)<<endl;
ngood++;
}
}
FILE_LOG(logDEBUG1)<<" Clipped "<<nclip<<" detections"<<endl;
v[j]/=ngood;
}
std::stringstream str;
str<<" Final means :";
for(int j=0;j<nvar;++j) {
str<<v[j]<<" ";
}
FILE_LOG(logDEBUG1)<<str.str()<<endl;
return v;
}
// Return a polynomial fit of all the detections in a cell
template<class T>
std::vector<T> Cell<T>::getFitVals(int order,float clip)
{
fitorder=order;
int nfit=(fitorder+1)*(fitorder+2)/2;
int ndet=dets.size();//this->getNGood();
std::vector<T> v(nvar*nfit,defaultVal);
if (this->isMissing() ) {
FILE_LOG(logDEBUG1)<<" this cell has been labeled missing. "<<endl;
return v;
}
if(ndet<nfit) {
FILE_LOG(logDEBUG1)<<" this cell has more parameters than detections "
<<ndet<<" "<<nfit<<". Set as missing."<<endl;
this->setMissing(true);
return v;
}
FILE_LOG(logDEBUG)<<"getting fit vals for "<<ndet<<" detections "<<nfit<<endl;
// we need to calculate first which objects we may need
// to reject. We don't necesarily want to clip them because
// they may be useful in better iterations
std::vector<bool> use_det(ndet,true);
for(int j=0;j<nvar;++j) {
std::vector<T> tmp;
for(int i=0;i<ndet;++i) {
if(dets[i]->isClipped()) continue;
tmp.push_back(dets[i]->getVal(j));
}
double mad;
double median=median_mad(tmp,mad);
for(int i=0;i<ndet;++i) {
if(dets[i]->isClipped()) continue;
// check that is within clipped sigma sigma
if(std::fabs(dets[i]->getVal(j)-median)>clip*mad) {
FILE_LOG(logDEBUG)<<"skipping "<<i<<" var "<<j<<" value to large "<<dets[i]->getVal(j)
<<" "<<median<<" "
<<std::fabs(dets[i]->getVal(j)-median)/mad<<" sigma"<<endl;
use_det[i]=false;
}
}
}
int ndet_cur=0;
for(int i=0;i<ndet;++i) {
if(dets[i]->isClipped()) continue;
if(use_det[i]) ndet_cur++;
}
if(ndet_cur<nfit) {
FILE_LOG(logDEBUG1)<<" this cell has more parameters than detections "
<<ndet_cur<<" "<<nfit<<". "<<endl;
this->setMissing(true);
return v;
}
FILE_LOG(logDEBUG)<<"using "<<ndet_cur<<" objects "<<endl;
DMatrix b(ndet_cur,nvar);
DMatrix A(ndet_cur,nfit);
for(int j=0;j<nvar;++j) {
int cur=0;
for(int i=0;i<dets.size();++i) {
if(dets[i]->isClipped() || !use_det[i]) continue;
b(cur,j)=dets[i]->getVal(j);
setPRow(fitorder,dets[i]->getPos(),bounds,A.row(cur));
cur++;
}
}
FILE_LOG(logDEBUG)<<"data "<<b<<endl;
FILE_LOG(logDEBUG)<<"poses "<<A<<endl;
DMatrix x=b/A;//b.subMatrix(0,cur,0,nvar)/A.subMatrix(0,cur,0,ndet);
FILE_LOG(logDEBUG)<<"x "<<x<<endl;
int cur=0;
for(int i=0;i<x.nrows();++i) {
for(int j=0;j<x.ncols();++j) {
v[cur]=x(i,j);
cur++;
}
}
return v;
}
template<class T>
std::vector<T> Cell<T>::getVals(std::string type,std::vector<float> ¶ms)
{
if(getTypeFromString(type)==Mean) {
return getMeanVals();
}
else if(getTypeFromString(type)==MeanClip) {
assert(params.size()>=1);
float sigma_clip=params[0];
return getMeanClipVals(sigma_clip);
}
else if(getTypeFromString(type)==Median) {
return getMedianVals();
}
else if(getTypeFromString(type)==Fit) {
assert(params.size()>=1);
int order=static_cast<int>(params[0]);
float clip=params[1];
return getFitVals(order,clip);
}
}
template<class T>
int Cell<T>::getNVal(std::string type,std::vector<float> ¶ms)
{
if(getTypeFromString(type)==Mean) {
return nvar;
}
else if(getTypeFromString(type)==MeanClip) {
return nvar;
}
else if(getTypeFromString(type)==Median) {
return nvar;
}
else if(getTypeFromString(type)==Fit) {
assert(params.size()>=1);
int fitorder=static_cast<int>(params[0]);
return nvar*(fitorder+1)*(fitorder+2)/2;
}
return 0;
}
template<class T>
std::vector<std::vector<T> > Cell<T>::getDiff(tmv::ConstVectorView<T> &vals,std::string type,
std::vector<float> params,
const std::vector<double> &mean,
const std::vector<double> &sigma,
bool clip,
double nclip)
{
std::vector<std::vector<T> > vdiff(nvar);
if(this->isMissing()) return vdiff;
if(getTypeFromString(type)!=Fit) {
for(int j=0;j<nvar;++j) {
for(int i=0;i<dets.size();++i) {
if(dets[i]->isClipped()) continue;
if(!clip) {
FILE_LOG(logDEBUG1)<<" nvar "<<j<<" det "<<i<<" "
<<vals[j]<<" "<<dets[i]->getVal(j)<<" "
<<vals[j]-dets[i]->getVal(j)<<endl;
vdiff[j].push_back(vals[j]-dets[i]->getVal(j));
}
else {
double diff=std::fabs((vals[j]-dets[i]->getVal(j))-mean[j])/sigma[j];
if(diff>nclip) {
FILE_LOG(logDEBUG1)<<" setting var "<<j<<" det "<<i<<" as clipped "
<<diff<<" compared to "<<nclip<<endl;
dets[i]->setClip(true);
}
else vdiff[j].push_back(vals[j]-dets[i]->getVal(j));
}
}
}
}
else {
int fitorder=params[0];
int nfit=(fitorder+1)*(fitorder+2)/2;
int ndet=this->getNGood();
DMatrix br(ndet,nvar);
DMatrix A(ndet,nfit);
DMatrix x(nfit,nvar);
int cur=0;
for(int n=0;n<dets.size();++n) {
if(dets[n]->isClipped()) continue;
setPRow(fitorder,dets[n]->getPos(),bounds,A.row(cur));
cur++;
}
cur=0;
for(int i=0;i<nfit;++i) {
for(int j=0;j<nvar;++j) {
x(i,j)=vals[cur];
cur++;
}
}
FILE_LOG(logDEBUG1)<<"x "<<x<<endl;
FILE_LOG(logDEBUG1)<<"A "<<A<<endl;
br=A*x;
for(int j=0;j<nvar;++j) {
cur=0;
for(int i=0;i<dets.size();++i) {
if(dets[i]->isClipped()) continue;
if(!clip) {
FILE_LOG(logDEBUG1)<<" fit nvar "<<j<<" det "<<i<<" "
<<br(cur,j)<<" "<<dets[i]->getVal(j)
<<" "<<br(cur,j)-dets[i]->getVal(j)<<endl;
vdiff[j].push_back(br(cur,j)-dets[i]->getVal(j));
}
else {
double diff=std::fabs( (br(cur,j)-dets[i]->getVal(j))-mean[j])/sigma[j];
if(diff>nclip) {
dets[i]->setClip(true);
FILE_LOG(logDEBUG1)<<" setting var "<<j<<" det "<<i<<" as clipped "
<<diff<<" compared to "<<nclip<<endl;
}
else vdiff[j].push_back(br(cur,j)-dets[i]->getVal(j));
}
cur++;
}
}
}
return vdiff;
}
template<class T>
std::vector<std::valarray<T> > Cell<T>::getDetVals(tmv::ConstVectorView<T> &vals,
std::string type,std::vector<float> params)
{
std::vector<std::valarray<T> > vdet;
if(getTypeFromString(type)!=Fit) {
for(int i=0;i<dets.size();++i) {
std::valarray<T> sdet(nvar);
for(int j=0;j<nvar;++j) {
sdet[j]=vals[j];
}
vdet.push_back(sdet);
}
}
else {
int fitorder=params[0];
int nfit=(fitorder+1)*(fitorder+2)/2;
int ndet=this->getNDet();
DMatrix br(ndet,nvar);
DMatrix A(ndet,nfit);
DMatrix x(nfit,nvar);
for(int n=0;n<dets.size();++n) {
setPRow(fitorder,dets[n]->getPos(),bounds,A.row(n));
}
int cur=0;
for(int i=0;i<nfit;++i) {
for(int j=0;j<nvar;++j) {
x(i,j)=vals[cur];
cur++;
}
}
br=A*x;
for(int i=0;i<dets.size();++i) {
std::valarray<T> sdet(nvar);
for(int j=0;j<nvar;++j) {
sdet[j]=br(i,j);
}
vdet.push_back(sdet);
}
}
return vdet;
}
template<class T>
int Chip<T>::getNClip()
{
int nclip=0;
for(int i=0;i<cells.size();++i) nclip+=cells[i]->getNClip();
return nclip;
}
template<class T>
int Chip<T>::getNGood()
{
int ngood=0;
for(int i=0;i<cells.size();++i) ngood+=cells[i]->getNGood();
return ngood;
}
template<class T>
int Chip<T>::getNDet()
{
int ndet=0;
for(int i=0;i<cells.size();++i) ndet+=cells[i]->getNDet();
return ndet;
}
// Get the mean values of all the cells in a chip
// The ordering of the variables are
// Ce1l 1 var1..varN, Cell2 var1..varN, Cell3...
template<class T>
std::vector<T> Chip<T>::getVals(std::string type,std::vector<float> ¶ms)
{
// assume all cells have the same number
int ntotvar=cells[0]->getNVal(type,params);
std::vector<T> v(ntotvar*cells.size());
int cur_index=0;
for(int i=0;i<cells.size();++i) {
FILE_LOG(logDEBUG)<<" Get vals from cell "<<i<<endl;
std::vector<T> cv=cells[i]->getVals(type,params);
for(int j=0;j<ntotvar;++j) {
v[cur_index]=cv[j];
cur_index++;
}
}
return v;
}
template<class T>
std::vector<bool> Chip<T>::getMissing()
{
std::vector<bool> v(cells.size(),false);
for(int i=0;i<cells.size();++i) {
if(cells[i]->isMissing()) {
FILE_LOG(logDEBUG)<<"At chip "<<label<<" cell "<<i
<<" missing with "<<cells[i]->getNGood()
<<" "<<endl;
v[i]=true;
}
}
return v;
}
template<class T>
void Chip<T>::setMissing(float prob)
{
for(int i=0;i<cells.size();++i) {
if(ran01()<prob) cells[i]->setMissing(true);
}
}
template<class T>
void Chip<T>::divide(int nvar,int _nx,int _ny) {
nx=_nx;
ny=_ny;
std::vector<Bounds<float> > vb=bounds.divide(nx,ny);
for(int i=0;i<vb.size();++i) {
Cell<T> *cell=new Cell<T>(nvar,vb[i]);
cells.push_back(cell);
cbounds.push_back(vb[i]);
}
}
template<class T>
void Chip<T>::addDet(Detection<T> *det) {
// The bounds class has the y as the fast moving coordinate
int bin_x=static_cast<int>(det->getPos().x/(bounds.getXMax()/nx));
int bin_y=static_cast<int>(det->getPos().y/(bounds.getYMax()/ny));
int bin=bin_x*ny+bin_y;
cells[bin]->addDet(det);
}
template<class T>
Exposure<T>::Exposure (string _label,int _nchip, int _shapestart,double _ra,double _dec,float _airmass):
label(_label),nchip(_nchip),ra(_ra),dec(_dec),airmass(_airmass),
nx_chip(-1.),ny_chip(-1.),xmax_chip(-1.),ymax_chip(-1.),
shapeStart(_shapestart),outlier(0) {}
template<class T>
int Exposure<T>::getNClip()
{
int nclip=0;
typename std::map<int,Chip<T>*>::const_iterator iter=chips.begin();
for(; iter!=chips.end();++iter) nclip+=iter->second->getNClip();
return nclip;
}
template<class T>
int Exposure<T>::getNGood()
{
int ngood=0;
typename std::map<int,Chip<T>*>::const_iterator iter=chips.begin();
for(; iter!=chips.end();++iter) ngood+=iter->second->getNGood();
return ngood;
}
template<class T>
int Exposure<T>::getNDet()
{
int ndet=0;
typename std::map<int,Chip<T>*>::const_iterator iter=chips.begin();
for(; iter!=chips.end();++iter) ndet+=iter->second->getNDet();
return ndet;
}
template<class T>
bool Exposure<T>::readShapelet(std::string dir,int nvar,bool add_size,
bool include_miss,bool use_dash,string suffix,
std::string exp,float max,string used_dir,string cdir) {
if (exp.empty()) exp=label;
FILE_LOG(logINFO) << "Reading exposure " << exp<<endl;
for(int ichip=1;ichip<=nchip;++ichip) {
Chip<T> *chip=new Chip<T>(ichip,xmax_chip,ymax_chip);
chip->divide(nvar,nx_chip,ny_chip);
// check if this chip should be skipped
std::vector<int>::iterator iter=find(skip.begin(),skip.end(),ichip);
if(iter!=skip.end()) continue;
std::stringstream inputFile,usedFile;
if(!use_dash) {
inputFile << dir << "/" << exp << "_";
usedFile << used_dir<<"/" << exp << "_";
}
else {
inputFile << dir << "/" << exp << "-";
usedFile << exp << "-";
}
if(ichip<10) {
inputFile <<0;
usedFile <<0;
}
if(!use_dash) {
inputFile << ichip << "_"+suffix;
usedFile << ichip << "_used.fits";
}
else {
inputFile << ichip << "-"+suffix;
}
string outname=usedFile.str();
std::vector<Detection<T> *> dets;
try {
FILE_LOG(logDEBUG) << "opening file " << inputFile.str()<<endl;
std::auto_ptr<CCfits::FITS> pInfile(new CCfits::FITS(inputFile.str(),CCfits::Read));
std::vector<Position<double> > skypos;
if(!cdir.empty()) {
// erase extra zeros
string cexp(exp);
cexp.erase(6,2);
std::stringstream inputFile2;
inputFile2 << cdir << "/" << cexp<<"/"<<exp << "_";
if(ichip<10) inputFile2 <<0;
inputFile2 << ichip << "_cat.fits";
CCfits::FITS *catfile=new CCfits::FITS(inputFile2.str(),CCfits::Read);
CCfits::ExtHDU& cattable = catfile->extension(2);
long rows=cattable.rows();
std::vector<double> ra,dec;
cattable.column("ALPHAWIN_J2000").read(ra, 1, rows);
cattable.column("DELTAWIN_J2000").read(dec, 1, rows);
for(int i=0;i<rows;++i) {
skypos.push_back(Position<double>(ra[i],dec[i]));
}
}
CCfits::ExtHDU& table = pInfile->extension(1);
long nTabRows=table.rows();
FILE_LOG(logDEBUG) << "found " << int(nTabRows*max)<<" objects"<<endl;
// randomize index list because there is x,y dependence
std::vector<int> index(nTabRows);
for (int i=0; i<nTabRows; i++) index[i]=i;
std::random_shuffle(index.begin(),index.end(),myrandom);
long start=1;
long end=nTabRows;
std::vector<int> id;
std::vector<int> psf_flags;
std::vector<double> psf_size;
std::vector<double> xpos;
std::vector<double> ypos;
table.column("psf_flags").read(psf_flags, start, end);
table.column("sigma_p").read(psf_size, start, end);
table.column("x").read(xpos, start, end);
table.column("y").read(ypos, start, end);
if(!cdir.empty()) {
table.column("id").read(id, start, end);
}
std::vector<long> order; // shapelet order
table.column("psf_order").read(order, start, end);
int icount=0;
std::vector<int> used(nTabRows,0);
for (int i=0; i<nTabRows*max; i++) {
if (psf_flags[index[i]]==0) { // pass psf flags
used[index[i]]=1;
int row=index[i]+1;
Detection<T> *det=new Detection<T>(xpos[index[i]],ypos[index[i]],nvar);
int ncoeff=(order[index[i]]+1)*(order[index[i]]+2)/2; // psf values
std::valarray<double> coeffs;
table.column("shapelets").read(coeffs, row);
FILE_LOG(logDEBUG1)<<"adding object at row "<<i<<" chip: "<<ichip<<" "<<xpos[index[i]]<<" "
<<ypos[index[i]]<<" "<<coeffs[shapeStart]<<" "
<<coeffs[shapeStart+1]<<" "<<coeffs[shapeStart+2]<<" "<<endl;
int last_index=nvar;
int start_index=0;
if(add_size) {
FILE_LOG(logDEBUG1)<<"adding size "<<psf_size[index[i]]<<endl;
det->setVal(0,psf_size[index[i]]);
last_index--;
start_index++;
}
for(int j=0;j<last_index;++j) {
FILE_LOG(logDEBUG1)<<"adding index var "<<start_index+j<<" "
<<"from shapelet index "<<shapeStart+j<<" value:"
<<coeffs[shapeStart+j]<<endl;
det->setVal(start_index+j,coeffs[shapeStart+j]);
}
if(!cdir.empty()) {
det->setSky(skypos[id[i]-1]);
}
dets.push_back(det);
}
}
if(max<1) {
// Maybe this should be moved to the main program and then we only need to
// write out one file
FILE_LOG(logDEBUG)<<"Writing out used objects "<<endl;
FITS *fitfile=0;
long naxis = 2;
long naxes1[2] = { 1, 1 };
fitfile=new FITS("!"+usedFile.str(),USHORT_IMG , naxis , naxes1 );
// write the exposure information
int nvar=1;
int nrows=nTabRows;
std::vector<string> colName(nvar,"");
std::vector<string> colForm(nvar,"");
std::vector<string> colUnit(nvar,"");
colName[0] = "used";
colForm[0] = "1J";
colUnit[0] = "";
Table* newTable = fitfile->addTable("Used",nrows,colName,colForm,colUnit);
newTable->column(colName[0]).write(used,1);
delete fitfile;
}
}
catch (CCfits::FitsException& ) {
if(!include_miss) {
FILE_LOG(logERROR)<<"Can't open chip: "<<inputFile.str()
<<" from exposure "<<exp<<" skipping"<<endl;
return false;
}
else {
FILE_LOG(logERROR)<<"Can't open chip: "<<inputFile.str()
<<" from exposure "<<exp<<" will be set to missing"<<endl;
}
}
if(chips.find(ichip)==chips.end()) {
FILE_LOG(logDEBUG2)<<"Could not find chip "<<ichip<<" creating new one"<<endl;
Chip<T> *chip=new Chip<T>(ichip,xmax_chip,ymax_chip);
chip->divide(nvar,nx_chip,ny_chip);
addChip(ichip,chip);
}
for(int i=0;i<dets.size();++i) {
chips[ichip]->addDet(dets[i]);
}
FILE_LOG(logDEBUG2)<<"Chip "<<ichip<<" has "<<chips[ichip]->getNGood()<<" stars"<<endl;
}
return true;
}
template<class T>
bool Exposure<T>::readPixels(std::string dir,int npix, int nvar,std::string sdir,
bool use_dash,std::string exp) {
if (exp.empty()) exp=label;
FILE_LOG(logINFO) << "Reading exposure " << exp<<endl;
for(int ichip=1;ichip<=nchip;++ichip) {
Chip<T> *chip=new Chip<T>(ichip,xmax_chip,ymax_chip);
chip->divide(nvar,nx_chip,ny_chip);
// check if this chip should be skipped
std::vector<int>::iterator iter=find(skip.begin(),skip.end(),ichip);
if(iter!=skip.end()) continue;
std::stringstream inputFile;
inputFile << dir << "/" << exp << "_";
if(ichip<10) inputFile <<0;
inputFile << ichip << ".fits.fz";
string image_file=inputFile.str();
std::stringstream inputFile2;
if(!use_dash) {
inputFile2 << sdir << "/" << exp << "_";
}
else {
inputFile2 << sdir << "/" << exp << "-";
}
if(ichip<10) inputFile2 <<0;
if(!use_dash) {
inputFile2 << ichip << "_psf.fits";
}
else {
inputFile2 << ichip << "-psf.fits";
}
string psf_file=inputFile2.str();
FILE_LOG(logDEBUG) << "Reading image file " << image_file<<endl;
pca::Image<T> im (image_file,2); // main image
pca::Image<T> wim(image_file,4); // weight image
pca::Image<T> bpm(image_file,3); // bad pixel mask
FILE_LOG(logDEBUG) << "Reading psf file " << psf_file<<endl;
std::vector<Position<T> > pos;
std::vector<double> use_sky;
try {
std::auto_ptr<CCfits::FITS> pInfile(new CCfits::FITS(psf_file,CCfits::Read));
CCfits::ExtHDU& table = pInfile->extension(1);
long nTabRows=table.rows();
FILE_LOG(logDEBUG) << "found " << nTabRows<<" objects"<<endl;
long start=1;
long end=nTabRows;
std::vector<int> psf_flags;
std::vector<int> sky;
std::vector<double> xpos;
std::vector<double> ypos;
table.column("psf_flags").read(psf_flags, start, end);
table.column("x").read(xpos, start, end);
table.column("y").read(ypos, start, end);
table.column("sky").read(sky, start, end);
std::vector<long> order; // shapelet order
table.column("psf_order").read(order, start, end);
for (int i=0; i<nTabRows; i++) {
if (!psf_flags[i]) { // pass psf flags
pos.push_back(Position<T>(xpos[i],ypos[i]));
use_sky.push_back(sky[i]);
}
}
}
catch (CCfits::FitsException& ) {
FILE_LOG(logERROR)<<"Can't open chip: "<<inputFile.str()<<" from exposure "<<exp<<" skipping"<<endl;
return false;
}
int app=npix;
int npixu=4*(app+1)*(app+1);
assert(npixu==nvar);
std::vector<double> pixels(nvar),weight(nvar);
// Loop over centers of objects and get sky-subtracted pixel lists
for(int istar=0;istar<pos.size();++istar) {
double xcen=pos[istar].x;
double ycen=pos[istar].y;
FILE_LOG(logDEBUG1)<<"Getting pixels around star : "<<istar<<" "
<<pos[istar]<<" "<<endl;
int i1 = int(floor(xcen-app));
int i2 = int(ceil(xcen+app));
int j1 = int(floor(ycen-app));
int j2 = int(ceil(ycen+app));
if (i1 < 0) { i1 = 0; }
if (i1 > xmax_chip) { i1 = xmax_chip; }
if (j1 < 0) { j1 = 0; }
if (j1 > ymax_chip) { i1 = ymax_chip; }
double chipx = i1-xcen;
double peak=0;
int cpix=0;;
FILE_LOG(logDEBUG1)<<"Boundary x : "<<i1<<","<<i2<<" y: "<<j1<<","<<j2<<endl;
for(int i=i1;i<=i2;++i) {
double chipy = j1-ycen;
for(int j=j1;j<=j2;++j) {
double flux = im(i,j)-use_sky[istar];
double inverseVariance=wim(i,j);
double bp=bpm(i,j);
if(bp>0.0) {
inverseVariance=0;
flux=-999.0;
}
// FILE_LOG(logDEBUG2)<<"Values at pixel "<<i<<","<<j<<","<<cpix<<" "
// <<flux<<" "<<inverseVariance<<" "<<bp<<endl;
pixels[cpix]=flux;
weight[cpix]=inverseVariance;
cpix++;
}
}
Detection<T> *det=new Detection<T>(pos[istar].x,pos[istar].y,nvar);
for(int j=0;j<nvar;++j) {
det->setVal(j,pixels[j]);
}
chip->addDet(det);
}