source: trunk/src/Cubes/ReadAndSearch.cc @ 155

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

Again, a large number of changes to commit:
Cubes/cubes.cc -- added destructors for DataArray?, Image and Cube.

fixed typo in Image::saveArray.

Cubes/cubes.hh -- destructors changed.
Cubes/drawMomentCutout.cc -- Added call to drawBlankEdges, which required

changing how the image array was set up. Now make
use of an isGood array.

Cubes/Merger?.cc -- made new functions mergeList and finaliseList that do a lot

of the work, acting just on the Detection vector.
Cube::Merger() now is mostly a front-end to these primitive
functions.

Cubes/ReadAndSearch?.cc -- new function that reads in FITS array piecemeal and

searches in each piece. For large files that are too
big to read in all at once.

param.cc -- added an explicit copy constructor to Param class.
Detection/spectrumDetect.cc -- fixed typos (pix. instead of pix->)
Detection/outputDetection.cc -- added test to outputDetectionText for number

of columns in ColSet?

Detection/columns.cc -- added push_back statements that had been left out.
Detection/detection.hh -- added prototypes for new merging functions
param.hh -- prototype for copy constructor of Param.

File size: 5.7 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];
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  for(int i=0;i<50;i++) std::cout << " ";
84  std::cout << "| " << std::setw(5) << numDetected << std::flush;
85  for(int i=0;i<64;i++) std::cout << "\b" << std::flush;
86
87  float *bigarray = new float[dimAxes[0] * dimAxes[2]];
88  float *array = new float[dimAxes[2]];
89  long *specdim = new long[2];
90  specdim[0] = dimAxes[2]; specdim[1]=1;
91 
92  float median,sigma;
93  int pos,frac;
94
95  Image *spectrum = new Image(specdim);
96  spectrum->saveParam(par);
97  spectrum->pars().setBeamSize(2.); // only neighbouring channels correlated
98
99  for(int y=0;y<dimAxes[1];y++){
100
101    std::cout << std::setw(4) << y << ": " << std::flush;
102
103    fpixel[1] = lpixel[1] = y+1; 
104 
105    status = 0;
106    if( fits_open_file(&fptr,fname.c_str(),READONLY,&status) ){
107      duchampWarning("readAndSearch","Error opening file: ");
108      fits_report_error(stderr, status);
109    }
110    status = 0;
111    fits_read_subset(fptr, TFLOAT, fpixel, lpixel, inc, NULL, bigarray, &anynul, &status);
112    if(status){
113      duchampError("readAndSearch","There was an error reading in the data array:");
114      fits_report_error(stderr, status);
115    }
116    status = 0;
117    fits_close_file(fptr, &status);
118    if (status){
119      duchampWarning("readAndSearch","Error closing file: ");
120      fits_report_error(stderr, status);
121    }
122
123    for(int x=0;x<dimAxes[0];x++){
124
125      pos = y*dimAxes[0]+x;
126      frac = 100*(pos+1)/(dimAxes[1]*dimAxes[0]);
127      if(frac%2 == 0){
128        std::cout << "|";
129        for(int i=0;i<frac/2;i++) std::cout << "#";
130        for(int i=frac/2;i<50;i++) std::cout << " ";
131        std::cout << "| " << std::setw(5) << numDetected;
132        for(int i=0;i<58;i++) std::cout << "\b";
133        std::cout << std::flush;
134      }
135 
136      for(int z=0;z<dimAxes[2];z++) array[z] = bigarray[z*dimAxes[0] + x];
137 
138      median = findMedian(array,dimAxes[2]);
139      spectrum->clearDetectionList();
140      if(par.getFlagATrous()){
141        float *recon = new float[dimAxes[2]];
142        atrous1DReconstruct(dimAxes[2],array,recon,par);
143        float *resid = new float[dimAxes[2]];
144        for(int i=0;i<dimAxes[2];i++) resid[i] = array[i] - recon[i];
145        sigma  = findMADFM(recon,dimAxes[2])/correctionFactor;
146        spectrum->saveArray(recon,dimAxes[2]);
147        delete [] recon, resid;
148        spectrum->removeMW(); // only works if flagMW is true
149        spectrum->setStats(median,sigma,par.getCut());
150        if(par.getFlagFDR()) spectrum->setupFDR();
151        spectrum->setMinSize(par.getMinChannels());
152        spectrum->spectrumDetect();
153      }
154      else{
155        sigma  = findMADFM(array,dimAxes[2])/correctionFactor;
156        spectrum->saveArray(array,dimAxes[2]);
157        spectrum->removeMW(); // only works if flagMW is true
158        spectrum->setStats(median,sigma,par.getCut());
159        if(par.getFlagFDR()) spectrum->setupFDR();
160        spectrum->setMinSize(par.getMinChannels());
161        spectrum->spectrumDetect();
162      }
163
164      numDetected += spectrum->getNumObj();
165      for(int obj=0;obj<spectrum->getNumObj();obj++){
166        Detection *object = new Detection;
167        *object = spectrum->getObject(obj);
168        for(int pix=0;pix<object->getSize();pix++) {
169          // Fix up coordinates of each pixel to match original array
170          object->setZ(pix, object->getX(pix));
171          object->setX(pix, x);
172          object->setY(pix, y);
173          object->setF(pix, array[object->getZ(pix)]);
174          // NB: set F to the original value, not the recon value.
175        }
176        object->addOffsets(par);
177        object->calcParams();
178        mergeIntoList(*object,outputList,par);
179        delete object;
180      }
181   
182   
183    } // end of loop over y
184
185    std::cout << "\b\b\b\b\b\b" << std::flush;
186  }   // end of loop over x
187
188  delete spectrum;
189  delete [] bigarray, array, fpixel, lpixel, inc, dimAxes, specdim;
190
191  return outputList;
192
193}
Note: See TracBrowser for help on using the repository browser.