source: tags/release-1.1.7/src/Cubes/ReadAndSearch.cc @ 1455

Last change on this file since 1455 was 400, checked in by MatthewWhiting, 16 years ago

Fixed references to fitshdr.h so that the wcslib path is included.

File size: 7.2 KB
Line 
1// -----------------------------------------------------------------------
2// ReadAndSearch.cc: Combining both the FITS reading and the searching.
3// -----------------------------------------------------------------------
4// Copyright (C) 2006, Matthew Whiting, ATNF
5//
6// This program is free software; you can redistribute it and/or modify it
7// under the terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 2 of the License, or (at your
9// option) any later version.
10//
11// Duchamp is distributed in the hope that it will be useful, but WITHOUT
12// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14// for more details.
15//
16// You should have received a copy of the GNU General Public License
17// along with Duchamp; if not, write to the Free Software Foundation,
18// Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
19//
20// Correspondence concerning Duchamp may be directed to:
21//    Internet email: Matthew.Whiting [at] atnf.csiro.au
22//    Postal address: Dr. Matthew Whiting
23//                    Australia Telescope National Facility, CSIRO
24//                    PO Box 76
25//                    Epping NSW 1710
26//                    AUSTRALIA
27// -----------------------------------------------------------------------
28#include <iostream>
29#include <iomanip>
30#include <string>
31#include <wcslib/wcs.h>
32#include <wcslib/wcshdr.h>
33#include <wcslib/fitshdr.h>
34#include <wcslib/wcsfix.h>
35#include <wcslib/wcsunits.h>
36#define WCSLIB_GETWCSTAB // define this so that we don't try and
37                         // redefine wtbarr (this is a problem when
38                         // using gcc v.4+
39#include <fitsio.h>
40#include <math.h>
41#include <duchamp/duchamp.hh>
42#include <duchamp/param.hh>
43#include <duchamp/Cubes/cubes.hh>
44#include <duchamp/Detection/detection.hh>
45#include <duchamp/ATrous/atrous.hh>
46#include <duchamp/Utils/utils.hh>
47
48std::string imageType[4] = {"point", "spectrum", "image", "cube"};
49
50vector<Detection> readAndSearch(Param &par)
51{
52  vector<Detection> outputList;
53
54  short int maxdim=3;
55  long *dimAxes = new long[maxdim];
56  for(int i=0;i<maxdim;i++) dimAxes[i]=1;
57  long nelements;
58  int bitpix,numAxes;
59  int status = 0,  nkeys;  /* MUST initialize status */
60  fitsfile *fptr;         
61
62  std::string fname = par.getImageFile();
63  if(par.getFlagSubsection()){
64    par.parseSubsection();
65    fname+=par.getSubsection();
66  }
67
68  if( fits_open_file(&fptr,fname.c_str(),READONLY,&status) ){
69    fits_report_error(stderr, status);
70  }
71
72  status = 0;
73  fits_get_img_param(fptr, maxdim, &bitpix, &numAxes, dimAxes, &status);
74  if(status){
75    fits_report_error(stderr, status);
76  }
77  status = 0;
78  fits_close_file(fptr, &status);
79  if (status){
80    duchampWarning("readAndSearch","Error closing file: ");
81    fits_report_error(stderr, status);
82  }
83
84  if(numAxes<=3) 
85    std::cout << "Dimensions of " << imageType[numAxes] << ": " << dimAxes[0];
86  else std::cout << "Dimensions of " << imageType[3] << ": " << dimAxes[0];
87  if(numAxes>1) std::cout << "x" << dimAxes[1];
88  if(numAxes>2) std::cout << "x" << dimAxes[2];
89  std::cout << std::endl;
90
91  int anynul;
92  long *fpixel = new long[numAxes];
93  long *lpixel = new long[numAxes];
94  long *inc = new long[numAxes];    // the data-sampling increment
95  for(int i=0;i<numAxes;i++) inc[i]=1;
96
97  //---------------------------------------------------------
98  //  Read in spectra one at a time, reconstruct if necessary
99  //   and then search, adding found objects to cube's object list.
100
101  fpixel[0] = 1;
102  for(int i=2;i<numAxes;i++) fpixel[i] = 1;
103
104  lpixel[0] = dimAxes[0];
105  lpixel[2] = dimAxes[2];
106  for(int i=3;i<numAxes;i++) lpixel[i] = 1;
107
108  int numDetected = 0;
109 
110  std::cout << "   0: |";
111  printSpace(50);
112  std::cout << "| " << std::setw(5) << numDetected << std::flush;
113  printBackSpace(64);
114  std::cout << std::flush;
115
116  float *bigarray = new float[dimAxes[0] * dimAxes[2]];
117  float *array = new float[dimAxes[2]];
118  long *specdim = new long[2];
119  specdim[0] = dimAxes[2]; specdim[1]=1;
120 
121  float median,sigma;
122  int pos,frac;
123
124  Image *spectrum = new Image(specdim);
125  spectrum->saveParam(par);
126  spectrum->pars().setBeamSize(2.); // only neighbouring channels correlated
127
128  for(int y=0;y<dimAxes[1];y++){
129
130    std::cout << std::setw(4) << y << ": " << std::flush;
131
132    fpixel[1] = lpixel[1] = y+1; 
133 
134    status = 0;
135    if( fits_open_file(&fptr,fname.c_str(),READONLY,&status) ){
136      duchampWarning("readAndSearch","Error opening file: ");
137      fits_report_error(stderr, status);
138    }
139    status = 0;
140    fits_read_subset(fptr, TFLOAT, fpixel, lpixel, inc, NULL, bigarray, &anynul, &status);
141    if(status){
142      duchampError("readAndSearch","There was an error reading in the data array:");
143      fits_report_error(stderr, status);
144    }
145    status = 0;
146    fits_close_file(fptr, &status);
147    if (status){
148      duchampWarning("readAndSearch","Error closing file: ");
149      fits_report_error(stderr, status);
150    }
151
152    for(int x=0;x<dimAxes[0];x++){
153
154      pos = y*dimAxes[0]+x;
155      frac = 100*(pos+1)/(dimAxes[1]*dimAxes[0]);
156      if(frac%2 == 0){
157        std::cout << "|";
158        printHash(frac/2);
159        printSpace(50-frac/2);
160        std::cout << "| " << std::setw(5) << numDetected;
161        printBackSpace(58);
162        std::cout << std::flush;
163      }
164 
165      for(int z=0;z<dimAxes[2];z++) array[z] = bigarray[z*dimAxes[0] + x];
166 
167      median = findMedian(array,dimAxes[2]);
168      spectrum->clearDetectionList();
169      if(par.getFlagATrous()){
170        float *recon = new float[dimAxes[2]];
171        atrous1DReconstruct(dimAxes[2],array,recon,par);
172        float *resid = new float[dimAxes[2]];
173        for(int i=0;i<dimAxes[2];i++) resid[i] = array[i] - recon[i];
174        sigma  = findMADFM(recon,dimAxes[2])/correctionFactor;
175        spectrum->saveArray(recon,dimAxes[2]);
176        delete [] recon;
177        delete [] resid;
178        spectrum->removeMW(); // only works if flagMW is true
179        spectrum->setStats(median,sigma,par.getCut());
180        if(par.getFlagFDR()) spectrum->setupFDR();
181        spectrum->setMinSize(par.getMinChannels());
182        spectrum->spectrumDetect();
183      }
184      else{
185        sigma  = findMADFM(array,dimAxes[2])/correctionFactor;
186        spectrum->saveArray(array,dimAxes[2]);
187        spectrum->removeMW(); // only works if flagMW is true
188        spectrum->setStats(median,sigma,par.getCut());
189        if(par.getFlagFDR()) spectrum->setupFDR();
190        spectrum->setMinSize(par.getMinChannels());
191        spectrum->spectrumDetect();
192      }
193
194      numDetected += spectrum->getNumObj();
195      for(int obj=0;obj<spectrum->getNumObj();obj++){
196        Detection *object = new Detection;
197        *object = spectrum->getObject(obj);
198        for(int pix=0;pix<object->getSize();pix++) {
199          // Fix up coordinates of each pixel to match original array
200          object->setZ(pix, object->getX(pix));
201          object->setX(pix, x);
202          object->setY(pix, y);
203          object->setF(pix, array[object->getZ(pix)]);
204          // NB: set F to the original value, not the recon value.
205        }
206        object->setOffsets(par);
207        object->calcParams();
208        mergeIntoList(*object,outputList,par);
209        delete object;
210      }
211   
212   
213    } // end of loop over y
214
215    printBackSpace(6);
216    std::cout << std::flush;
217  }   // end of loop over x
218
219  delete spectrum;
220  delete [] bigarray;
221  delete [] array;
222  delete [] fpixel;
223  delete [] lpixel;
224  delete [] inc;
225  delete [] dimAxes;
226  delete [] specdim;
227
228  return outputList;
229
230}
Note: See TracBrowser for help on using the repository browser.