source: tags/release-1.0.5/src/Cubes/ReadAndSearch.cc

Last change on this file was 175, checked in by Matthew Whiting, 18 years ago

Introduced some simple inline functions to duchamp.hh to make the printing of progress bars simpler and of a more unified fashion.

File size: 5.6 KB
Line 
1#include <iostream>
2#include <iomanip>
3#include <string>
4#include <wcs.h>
5#include <wcshdr.h>
6#include <fitshdr.h>
7#include <wcsfix.h>
8#include <wcsunits.h>
9#define WCSLIB_GETWCSTAB // define this so that we don't try and redefine wtbarr
10                         // (this is a problem when using gcc v.4+
11#include <fitsio.h>
12#include <math.h>
13#include <duchamp.hh>
14#include <param.hh>
15#include <Cubes/cubes.hh>
16#include <Detection/detection.hh>
17#include <ATrous/atrous.hh>
18#include <Utils/utils.hh>
19
20string imageType[4] = {"point", "spectrum", "image", "cube"};
21
22vector<Detection> readAndSearch(Param &par)
23{
24  vector<Detection> outputList;
25
26  short int maxdim=3;
27  long *dimAxes = new long[maxdim];
28  for(int i=0;i<maxdim;i++) dimAxes[i]=1;
29  long nelements;
30  int bitpix,numAxes;
31  int status = 0,  nkeys;  /* MUST initialize status */
32  fitsfile *fptr;         
33
34  string fname = par.getImageFile();
35  if(par.getFlagSubsection()){
36    par.parseSubsection();
37    fname+=par.getSubsection();
38  }
39
40  if( fits_open_file(&fptr,fname.c_str(),READONLY,&status) ){
41    fits_report_error(stderr, status);
42  }
43
44  status = 0;
45  fits_get_img_param(fptr, maxdim, &bitpix, &numAxes, dimAxes, &status);
46  if(status){
47    fits_report_error(stderr, status);
48  }
49  status = 0;
50  fits_close_file(fptr, &status);
51  if (status){
52    duchampWarning("readAndSearch","Error closing file: ");
53    fits_report_error(stderr, status);
54  }
55
56  if(numAxes<=3) 
57    std::cout << "Dimensions of " << imageType[numAxes] << ": " << dimAxes[0];
58  else std::cout << "Dimensions of " << imageType[3] << ": " << dimAxes[0];
59  if(numAxes>1) std::cout << "x" << dimAxes[1];
60  if(numAxes>2) std::cout << "x" << dimAxes[2];
61  std::cout << std::endl;
62
63  int anynul;
64  long *fpixel = new long[numAxes];
65  long *lpixel = new long[numAxes];
66  long *inc = new long[numAxes];    // the data-sampling increment
67  for(int i=0;i<numAxes;i++) inc[i]=1;
68
69  //---------------------------------------------------------
70  //  Read in spectra one at a time, reconstruct if necessary
71  //   and then search, adding found objects to cube's object list.
72
73  fpixel[0] = 1;
74  for(int i=2;i<numAxes;i++) fpixel[i] = 1;
75
76  lpixel[0] = dimAxes[0];
77  lpixel[2] = dimAxes[2];
78  for(int i=3;i<numAxes;i++) lpixel[i] = 1;
79
80  int numDetected = 0;
81 
82  std::cout << "   0: |";
83  printSpace(50);
84  std::cout << "| " << std::setw(5) << numDetected << std::flush;
85  printBackSpace(64);
86  std::cout << std::flush;
87
88  float *bigarray = new float[dimAxes[0] * dimAxes[2]];
89  float *array = new float[dimAxes[2]];
90  long *specdim = new long[2];
91  specdim[0] = dimAxes[2]; specdim[1]=1;
92 
93  float median,sigma;
94  int pos,frac;
95
96  Image *spectrum = new Image(specdim);
97  spectrum->saveParam(par);
98  spectrum->pars().setBeamSize(2.); // only neighbouring channels correlated
99
100  for(int y=0;y<dimAxes[1];y++){
101
102    std::cout << std::setw(4) << y << ": " << std::flush;
103
104    fpixel[1] = lpixel[1] = y+1; 
105 
106    status = 0;
107    if( fits_open_file(&fptr,fname.c_str(),READONLY,&status) ){
108      duchampWarning("readAndSearch","Error opening file: ");
109      fits_report_error(stderr, status);
110    }
111    status = 0;
112    fits_read_subset(fptr, TFLOAT, fpixel, lpixel, inc, NULL, bigarray, &anynul, &status);
113    if(status){
114      duchampError("readAndSearch","There was an error reading in the data array:");
115      fits_report_error(stderr, status);
116    }
117    status = 0;
118    fits_close_file(fptr, &status);
119    if (status){
120      duchampWarning("readAndSearch","Error closing file: ");
121      fits_report_error(stderr, status);
122    }
123
124    for(int x=0;x<dimAxes[0];x++){
125
126      pos = y*dimAxes[0]+x;
127      frac = 100*(pos+1)/(dimAxes[1]*dimAxes[0]);
128      if(frac%2 == 0){
129        std::cout << "|";
130        printHash(frac/2);
131        printSpace(50-frac/2);
132        std::cout << "| " << std::setw(5) << numDetected;
133        printBackSpace(58);
134        std::cout << std::flush;
135      }
136 
137      for(int z=0;z<dimAxes[2];z++) array[z] = bigarray[z*dimAxes[0] + x];
138 
139      median = findMedian(array,dimAxes[2]);
140      spectrum->clearDetectionList();
141      if(par.getFlagATrous()){
142        float *recon = new float[dimAxes[2]];
143        atrous1DReconstruct(dimAxes[2],array,recon,par);
144        float *resid = new float[dimAxes[2]];
145        for(int i=0;i<dimAxes[2];i++) resid[i] = array[i] - recon[i];
146        sigma  = findMADFM(recon,dimAxes[2])/correctionFactor;
147        spectrum->saveArray(recon,dimAxes[2]);
148        delete [] recon, resid;
149        spectrum->removeMW(); // only works if flagMW is true
150        spectrum->setStats(median,sigma,par.getCut());
151        if(par.getFlagFDR()) spectrum->setupFDR();
152        spectrum->setMinSize(par.getMinChannels());
153        spectrum->spectrumDetect();
154      }
155      else{
156        sigma  = findMADFM(array,dimAxes[2])/correctionFactor;
157        spectrum->saveArray(array,dimAxes[2]);
158        spectrum->removeMW(); // only works if flagMW is true
159        spectrum->setStats(median,sigma,par.getCut());
160        if(par.getFlagFDR()) spectrum->setupFDR();
161        spectrum->setMinSize(par.getMinChannels());
162        spectrum->spectrumDetect();
163      }
164
165      numDetected += spectrum->getNumObj();
166      for(int obj=0;obj<spectrum->getNumObj();obj++){
167        Detection *object = new Detection;
168        *object = spectrum->getObject(obj);
169        for(int pix=0;pix<object->getSize();pix++) {
170          // Fix up coordinates of each pixel to match original array
171          object->setZ(pix, object->getX(pix));
172          object->setX(pix, x);
173          object->setY(pix, y);
174          object->setF(pix, array[object->getZ(pix)]);
175          // NB: set F to the original value, not the recon value.
176        }
177        object->addOffsets(par);
178        object->calcParams();
179        mergeIntoList(*object,outputList,par);
180        delete object;
181      }
182   
183   
184    } // end of loop over y
185
186    printBackSpace(6);
187    std::cout << std::flush;
188  }   // end of loop over x
189
190  delete spectrum;
191  delete [] bigarray, array, fpixel, lpixel, inc, dimAxes, specdim;
192
193  return outputList;
194
195}
Note: See TracBrowser for help on using the repository browser.