1 | // ----------------------------------------------------------------------- |
---|
2 | // cubicSearchNMerge.cc: Combining both the searching and the merging |
---|
3 | // functions. |
---|
4 | // ----------------------------------------------------------------------- |
---|
5 | // Copyright (C) 2006, Matthew Whiting, ATNF |
---|
6 | // |
---|
7 | // This program is free software; you can redistribute it and/or modify it |
---|
8 | // under the terms of the GNU General Public License as published by the |
---|
9 | // Free Software Foundation; either version 2 of the License, or (at your |
---|
10 | // option) any later version. |
---|
11 | // |
---|
12 | // Duchamp is distributed in the hope that it will be useful, but WITHOUT |
---|
13 | // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
---|
14 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
---|
15 | // for more details. |
---|
16 | // |
---|
17 | // You should have received a copy of the GNU General Public License |
---|
18 | // along with Duchamp; if not, write to the Free Software Foundation, |
---|
19 | // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA |
---|
20 | // |
---|
21 | // Correspondence concerning Duchamp may be directed to: |
---|
22 | // Internet email: Matthew.Whiting [at] atnf.csiro.au |
---|
23 | // Postal address: Dr. Matthew Whiting |
---|
24 | // Australia Telescope National Facility, CSIRO |
---|
25 | // PO Box 76 |
---|
26 | // Epping NSW 1710 |
---|
27 | // AUSTRALIA |
---|
28 | // ----------------------------------------------------------------------- |
---|
29 | #include <iostream> |
---|
30 | #include <iomanip> |
---|
31 | #include <fstream> |
---|
32 | #include <vector> |
---|
33 | #include <duchamp/Cubes/cubes.hh> |
---|
34 | #include <duchamp/Utils/utils.hh> |
---|
35 | using std::endl; |
---|
36 | using std::setw; |
---|
37 | |
---|
38 | vector <Detection> cubicSearchNMerge(long *dim, float *Array, Param &par) |
---|
39 | { |
---|
40 | /** |
---|
41 | * cubicSearch |
---|
42 | * Takes a dimension array and data array as input (and Parameter set) |
---|
43 | * and searches for detections in a combination of 1D and 2D searches. |
---|
44 | * Returns a vector list of Detections. |
---|
45 | * No reconstruction is assumed to have taken place, so statistics are |
---|
46 | * calculated (using robust methods) from the data array itself. |
---|
47 | */ |
---|
48 | |
---|
49 | vector <Detection> outputList; |
---|
50 | int zdim = dim[2]; |
---|
51 | int xySize = dim[0] * dim[1]; |
---|
52 | int fullSize = zdim * xySize; |
---|
53 | int num=0; |
---|
54 | |
---|
55 | // bool flagBlank=par.getFlagBlankPix(); |
---|
56 | float blankPixValue = par.getBlankPixVal(); |
---|
57 | bool *isGood = new bool[fullSize]; |
---|
58 | for(int pos=0;pos<fullSize;pos++) |
---|
59 | isGood[pos] = !par.isBlank(Array[pos]); |
---|
60 | // isGood[pos] = (!flagBlank) || (Array[pos]!=blankPixValue); |
---|
61 | |
---|
62 | float dud; |
---|
63 | |
---|
64 | // FIRST SEARCH -- IN EACH SPECTRUM. |
---|
65 | // FIRST, GET STATS |
---|
66 | if(zdim>1){ |
---|
67 | if(par.isVerbose()) std::cout << " 1D: | |" << std::flush; |
---|
68 | // if(par.isVerbose()) std::cout << "Done 0%" << "\b\b\b\b\b\b\b\b" << std::flush; |
---|
69 | float *specMedian = new float[xySize]; |
---|
70 | float *specSigma = new float[xySize]; |
---|
71 | |
---|
72 | for(int npix=0; npix<xySize; npix++){ |
---|
73 | float *spec = new float[zdim]; |
---|
74 | int goodSize=0; |
---|
75 | for(int z=0;z<zdim;z++) if(isGood[z*xySize+npix]) spec[goodSize++] = Array[z*xySize+npix]; |
---|
76 | if(goodSize>0) findMedianStats(spec,goodSize,specMedian[npix],dud); |
---|
77 | else specMedian[npix] = blankPixValue; |
---|
78 | // if(goodSize>0) findNormalStats(spec,goodSize,dud,specSigma[npix]); |
---|
79 | if(goodSize>0){ |
---|
80 | findMedianStats(spec,goodSize,dud,specSigma[npix]); |
---|
81 | specSigma[npix] /= correctionFactor; |
---|
82 | } |
---|
83 | else specSigma[npix] = 1.; |
---|
84 | delete spec; |
---|
85 | } |
---|
86 | // NEXT, DO SOURCE FINDING |
---|
87 | int numSearches = xySize + zdim; |
---|
88 | for(int npix=0; npix<xySize; npix++){ |
---|
89 | |
---|
90 | // if(par.isVerbose() && ((1000*npix/xySize)%10==0) ) |
---|
91 | // std::cout << "Done " << setw(2) << 100*npix/xySize << "%\b\b\b\b\b\b\b\b" << std::flush; |
---|
92 | if( par.isVerbose() && ((100*(npix+1)/xySize)%5 == 0) ){ |
---|
93 | std::cout << "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b|"; |
---|
94 | for(int i=0;i<(100*(npix+1)/xySize)/5;i++) std::cout << "#"; |
---|
95 | for(int i=(100*(npix+1)/xySize)/5;i<20;i++) std::cout << " "; |
---|
96 | std::cout << "|" << std::flush; |
---|
97 | } |
---|
98 | |
---|
99 | float *spec = new float[zdim]; |
---|
100 | for(int z=0;z<zdim;z++) spec[z] = Array[z*xySize + npix]; |
---|
101 | long *specdim = new long[2]; |
---|
102 | specdim[0] = zdim; specdim[1]=1; |
---|
103 | Image *spectrum = new Image(specdim); |
---|
104 | spectrum->saveParam(par); |
---|
105 | spectrum->pars().setBeamSize(2.); // for spectrum, only neighbouring channels correlated |
---|
106 | spectrum->saveArray(spec,zdim); |
---|
107 | spectrum->setStats(specMedian[npix],specSigma[npix],par.getCut()); |
---|
108 | if(par.getFlagFDR()) spectrum->setupFDR(); |
---|
109 | spectrum->lutz_detect(); |
---|
110 | for(int obj=0;obj<spectrum->getNumObj();obj++){ |
---|
111 | Detection *object = new Detection; |
---|
112 | *object = spectrum->getObject(obj); |
---|
113 | // if(par.getFlagGrowth()) growObject(*object,*spectrum); |
---|
114 | for(int pix=0;pix<object->getSize();pix++) { |
---|
115 | // Fix up coordinates of each pixel to match original array |
---|
116 | object->setZ(pix, object->getX(pix)); |
---|
117 | object->setX(pix, npix%dim[0]); |
---|
118 | object->setY(pix, npix/dim[0]); |
---|
119 | } |
---|
120 | object->addOffsets(par); |
---|
121 | object->calcParams(); |
---|
122 | // outputList.push_back(*object); |
---|
123 | mergeIntoList(*object,outputList,par); |
---|
124 | delete object; |
---|
125 | } |
---|
126 | delete spectrum; |
---|
127 | delete spec; |
---|
128 | delete specdim; |
---|
129 | } |
---|
130 | |
---|
131 | delete [] specMedian; |
---|
132 | delete [] specSigma; |
---|
133 | |
---|
134 | num = outputList.size(); |
---|
135 | if(par.isVerbose()) |
---|
136 | std::cout <<"\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\bFound " << num <<";" << std::flush; |
---|
137 | |
---|
138 | } |
---|
139 | |
---|
140 | // SECOND SEARCH -- IN EACH CHANNEL |
---|
141 | // FIRST, GET STATS |
---|
142 | if(par.isVerbose()) std::cout << " 2D: | |" << std::flush; |
---|
143 | // if(par.isVerbose()) std::cout << "Done 0%" << "\b\b\b\b\b\b\b\b" << std::flush; |
---|
144 | float *imageMedian = new float[zdim]; |
---|
145 | float *imageSigma = new float[zdim]; |
---|
146 | for(int z=0; z<zdim; z++){ |
---|
147 | float *image = new float[xySize]; |
---|
148 | int goodSize=0; |
---|
149 | for(int npix=0; npix<xySize; npix++) |
---|
150 | if(isGood[z*xySize + npix]) image[goodSize++] = Array[z*xySize + npix]; |
---|
151 | if(goodSize>0) findMedianStats(image,goodSize,imageMedian[z],dud); |
---|
152 | else imageMedian[z] = blankPixValue; |
---|
153 | if(goodSize>0) findNormalStats(image,goodSize,dud,imageSigma[z]); |
---|
154 | else imageSigma[z] = 1.; |
---|
155 | delete image; |
---|
156 | } |
---|
157 | // NEXT, DO SOURCE FINDING |
---|
158 | bool *doChannel = new bool[zdim]; |
---|
159 | for(int z=0;z<zdim;z++) |
---|
160 | doChannel[z] = !( par.getFlagMW() && (z>=par.getMinMW()) && (z<=par.getMaxMW()) ); |
---|
161 | |
---|
162 | for(int z=0; z<zdim; z++){ |
---|
163 | |
---|
164 | // if(par.isVerbose() && ((1000*z/zdim)%10==0) ) |
---|
165 | // std::cout << "Done " << setw(2) << 100*z/zdim << "%\b\b\b\b\b\b\b\b" << std::flush; |
---|
166 | if( par.isVerbose() && ((100*(z+1)/zdim)%5 == 0) ){ |
---|
167 | std::cout << "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b|"; |
---|
168 | for(int i=0;i<(100*(z+1)/zdim)/5;i++) std::cout << "#"; |
---|
169 | for(int i=(100*(z+1)/zdim)/5;i<20;i++) std::cout << " "; |
---|
170 | std::cout << "|" << std::flush; |
---|
171 | } |
---|
172 | |
---|
173 | if( doChannel[z] ){ |
---|
174 | |
---|
175 | float *image = new float[xySize]; |
---|
176 | for(int npix=0; npix<xySize; npix++) image[npix] = Array[z*xySize + npix]; |
---|
177 | long *imdim = new long[2]; |
---|
178 | imdim[0] = dim[0]; imdim[1] = dim[1]; |
---|
179 | Image *channelImage = new Image(imdim); |
---|
180 | channelImage->saveParam(par); |
---|
181 | channelImage->saveArray(image,xySize); |
---|
182 | channelImage->setStats(imageMedian[z],imageSigma[z],par.getCut()); |
---|
183 | if(par.getFlagFDR()) channelImage->setupFDR(); |
---|
184 | channelImage->lutz_detect(); |
---|
185 | for(int obj=0;obj<channelImage->getNumObj();obj++){ |
---|
186 | Detection *object = new Detection; |
---|
187 | *object = channelImage->getObject(obj); |
---|
188 | // if(par.getFlagGrowth()) growObject(*object,*channelImage); |
---|
189 | // Fix up coordinates of each pixel to match original array |
---|
190 | for(int pix=0;pix<object->getSize();pix++) object->setZ(pix, z); |
---|
191 | object->addOffsets(par); |
---|
192 | object->calcParams(); |
---|
193 | mergeIntoList(*object,outputList,par); |
---|
194 | // outputList.push_back(*object); |
---|
195 | delete object; |
---|
196 | } |
---|
197 | delete image; |
---|
198 | delete channelImage; |
---|
199 | delete imdim; |
---|
200 | } |
---|
201 | |
---|
202 | } |
---|
203 | |
---|
204 | if(par.isVerbose()) |
---|
205 | std::cout << "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\bFound " << outputList.size() - num |
---|
206 | << ". " << std::endl << std::flush; |
---|
207 | |
---|
208 | delete [] imageMedian; |
---|
209 | delete [] imageSigma; |
---|
210 | delete [] isGood; |
---|
211 | delete [] doChannel; |
---|
212 | |
---|
213 | return outputList; |
---|
214 | } |
---|
215 | |
---|