source: tags/release-0.9.2/Cubes/cubicSearch.cc @ 1323

Last change on this file since 1323 was 86, checked in by Matthew Whiting, 18 years ago

Large commit, mostly removing dud commented code, and adding comments and
documentation to the existing code. No new features.

File size: 6.0 KB
Line 
1#include <iostream>
2#include <iomanip>
3#include <fstream>
4#include <vector>
5#include <Cubes/cubes.hh>
6#include <Utils/utils.hh>
7
8void Cube::SimpleSearch3D()
9{
10  /**
11   * Cube::SimpleSearch3D()
12   *  A front end to the cubic searching routine that does not
13   *  involve any wavelet reconstruction.
14   *  Although if baseline-removal is required that is done prior to searching.
15   *  Once searching is complete, the detection map is updated and
16   *  the intermediate detections are logged in the log file.
17   */
18
19  this->objectList = cubicSearch(this->axisDim,this->array,this->par);
20   
21  this->updateDetectMap();
22  if(this->par.getFlagLog()) this->logDetectionList();
23
24}
25
26
27vector <Detection> cubicSearch(long *dim, float *Array, Param &par)
28{
29  /**
30   * cubicSearch
31   *  Takes a dimension array and data array as input (and Parameter set)
32   *  and searches for detections in a combination of 1D and 2D searches.
33   *  Returns a vector list of Detections.
34   *  No reconstruction is assumed to have taken place, so statistics are
35   *  calculated (using robust methods) from the data array itself.
36   */
37
38  vector <Detection> outputList;
39  int zdim = dim[2];
40  int xySize = dim[0] * dim[1];
41  int fullSize = zdim * xySize;
42  int num = 0;
43
44  float blankPixValue = par.getBlankPixVal();
45  bool *isGood = new bool[fullSize];
46  for(int pos=0;pos<fullSize;pos++)
47    isGood[pos] = !par.isBlank(Array[pos]);
48 
49  float dud;
50
51  // FIRST SEARCH --  IN EACH SPECTRUM.
52  if(zdim>1){
53    if(par.isVerbose()) std::cout << "  1D: |                    |" << std::flush;
54    float *specMedian = new float[xySize];
55    float *specSigma = new float[xySize];
56
57    for(int npix=0; npix<xySize; npix++){
58      float *spec = new float[zdim];
59      int goodSize=0;
60      for(int z=0;z<zdim;z++) if(isGood[z*xySize+npix]) spec[goodSize++] = Array[z*xySize+npix];
61      if(goodSize>0) findMedianStats(spec,goodSize,specMedian[npix],dud);
62      else specMedian[npix] = blankPixValue;
63      if(goodSize>0){
64        findMedianStats(spec,goodSize,dud,specSigma[npix]);
65        specSigma[npix] /= correctionFactor;
66      }
67      else specSigma[npix] = 1.;
68      delete spec;
69    }
70
71    int numSearches = xySize + zdim;
72    for(int npix=0; npix<xySize; npix++){
73
74      if( par.isVerbose() && ((100*(npix+1)/xySize)%5 == 0) ){
75        std::cout << "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b|";
76        for(int i=0;i<(100*(npix+1)/xySize)/5;i++) std::cout << "#";
77        for(int i=(100*(npix+1)/xySize)/5;i<20;i++) std::cout << " ";
78        std::cout << "|" << std::flush;
79      }
80
81      long *specdim = new long[2];
82      specdim[0] = zdim; specdim[1]=1;
83      Image *spectrum = new Image(specdim);
84      delete specdim;
85      spectrum->saveParam(par);
86      spectrum->pars().setBeamSize(2.); // for spectrum, only neighbouring channels correlated
87      spectrum->extractSpectrum(Array,dim,npix);
88      spectrum->setStats(specMedian[npix],specSigma[npix],par.getCut());
89      if(par.getFlagFDR()) spectrum->setupFDR();
90      spectrum->setMinSize(par.getMinChannels());
91      spectrum->spectrumDetect();
92      for(int obj=0;obj<spectrum->getNumObj();obj++){
93        Detection *object = new Detection;
94        *object = spectrum->getObject(obj);
95        for(int pix=0;pix<object->getSize();pix++) {
96          // Fix up coordinates of each pixel to match original array
97          object->setZ(pix, object->getX(pix));
98          object->setX(pix, npix%dim[0]);
99          object->setY(pix, npix/dim[0]);
100        }
101        object->addOffsets(par);
102        object->calcParams();
103        mergeIntoList(*object,outputList,par);
104        delete object;
105      }
106      delete spectrum;
107     }
108
109    delete [] specMedian;
110    delete [] specSigma;
111
112    num = outputList.size();
113    if(par.isVerbose())
114      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;
115
116  }
117
118  // SECOND SEARCH --  IN EACH CHANNEL
119  // FIRST, GET STATS
120  if(par.isVerbose()) std::cout << "  2D: |                    |" << std::flush;
121  float *imageMedian = new float[zdim];
122  float *imageSigma = new float[zdim];
123  for(int z=0; z<zdim; z++){
124    float *image = new float[xySize];
125    int goodSize=0;
126    for(int npix=0; npix<xySize; npix++)
127      if(isGood[z*xySize + npix]) image[goodSize++] = Array[z*xySize + npix];
128    if(goodSize>0){
129      findMedianStats(image,goodSize,imageMedian[z],imageSigma[z]);
130      imageSigma[z] /= correctionFactor;
131    }
132    else{
133      imageMedian[z] = blankPixValue;
134      imageSigma[z] = 1.;
135    }
136    delete image;
137  }
138  // NEXT, DO SOURCE FINDING
139  bool *doChannel = new bool[zdim];
140  for(int z=0;z<zdim;z++)
141    doChannel[z] = !( par.getFlagMW() && (z>=par.getMinMW()) && (z<=par.getMaxMW()) );
142
143  for(int z=0; z<zdim; z++){
144
145    if( par.isVerbose() && ((100*(z+1)/zdim)%5 == 0) ){
146      std::cout << "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b|";
147      for(int i=0;i<(100*(z+1)/zdim)/5;i++) std::cout << "#";
148      for(int i=(100*(z+1)/zdim)/5;i<20;i++) std::cout << " ";
149      std::cout << "|" << std::flush;
150    }
151
152    if( doChannel[z] ){
153
154      long *imdim = new long[2];
155      imdim[0] = dim[0]; imdim[1] = dim[1];
156      Image *channelImage = new Image(imdim);
157      delete imdim;
158      channelImage->saveParam(par);
159      channelImage->extractImage(Array,dim,z);
160      channelImage->setStats(imageMedian[z],imageSigma[z],par.getCut());
161      if(par.getFlagFDR()) channelImage->setupFDR();
162      channelImage->setMinSize(par.getMinPix());
163      channelImage->lutz_detect();
164      for(int obj=0;obj<channelImage->getNumObj();obj++){
165        Detection *object = new Detection;
166        *object = channelImage->getObject(obj);
167        // Fix up coordinates of each pixel to match original array
168        for(int pix=0;pix<object->getSize();pix++) object->setZ(pix, z);
169        object->addOffsets(par);
170        object->calcParams();
171        mergeIntoList(*object,outputList,par);
172        delete object;
173      }
174      delete channelImage;
175    }
176
177  }
178
179  if(par.isVerbose())
180    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
181              << ".                                           " << std::endl << std::flush;
182
183  delete [] imageMedian;
184  delete [] imageSigma;
185
186  delete [] isGood;
187  delete [] doChannel;
188 
189  return outputList;
190}
Note: See TracBrowser for help on using the repository browser.