source: branches/OptimisedGrowerTesting/src/FitsIO/ReadAndSearch.cc @ 1441

Last change on this file since 1441 was 1246, checked in by MatthewWhiting, 11 years ago

Ticket #193 - Removing all the MW-related code. Most of it was commented out, but Param now no longer has anything referring to MW. The flag array in ObjectGrower? has also changed to FLAG from MW.

File size: 7.1 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    DUCHAMPWARN("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  size_t pos;
123  int frac;
124
125  Image *spectrum = new Image(specdim);
126  spectrum->saveParam(par);
127  spectrum->pars().setBeamSize(2.); // only neighbouring channels correlated
128
129  for(int y=0;y<dimAxes[1];y++){
130
131    std::cout << std::setw(4) << y << ": " << std::flush;
132
133    fpixel[1] = lpixel[1] = y+1; 
134 
135    status = 0;
136    if( fits_open_file(&fptr,fname.c_str(),READONLY,&status) ){
137      DUCHAMPWARN("readAndSearch","Error opening file: ");
138      fits_report_error(stderr, status);
139    }
140    status = 0;
141    fits_read_subset(fptr, TFLOAT, fpixel, lpixel, inc, NULL, bigarray, &anynul, &status);
142    if(status){
143      DUCHAMPERROR("readAndSearch","There was an error reading in the data array:");
144      fits_report_error(stderr, status);
145    }
146    status = 0;
147    fits_close_file(fptr, &status);
148    if (status){
149      DUCHAMPWARN("readAndSearch","Error closing file: ");
150      fits_report_error(stderr, status);
151    }
152
153    for(int x=0;x<dimAxes[0];x++){
154
155      pos = y*dimAxes[0]+x;
156      frac = 100*(pos+1)/(dimAxes[1]*dimAxes[0]);
157      if(frac%2 == 0){
158        std::cout << "|";
159        printHash(frac/2);
160        printSpace(50-frac/2);
161        std::cout << "| " << std::setw(5) << numDetected;
162        printBackSpace(58);
163        std::cout << std::flush;
164      }
165 
166      for(int z=0;z<dimAxes[2];z++) array[z] = bigarray[z*dimAxes[0] + x];
167 
168      median = findMedian(array,dimAxes[2]);
169      spectrum->clearDetectionList();
170      if(par.getFlagATrous()){
171        float *recon = new float[dimAxes[2]];
172        atrous1DReconstruct(dimAxes[2],array,recon,par);
173        float *resid = new float[dimAxes[2]];
174        for(int i=0;i<dimAxes[2];i++) resid[i] = array[i] - recon[i];
175        sigma  = findMADFM(recon,dimAxes[2])/correctionFactor;
176        spectrum->saveArray(recon,dimAxes[2]);
177        delete [] recon;
178        delete [] resid;
179        spectrum->setStats(median,sigma,par.getCut());
180        if(par.getFlagFDR()) spectrum->setupFDR();
181        spectrum->setMinSize(par.getMinChannels());
182        spectrum->findSources1D();
183      }
184      else{
185        sigma  = findMADFM(array,dimAxes[2])/correctionFactor;
186        spectrum->saveArray(array,dimAxes[2]);
187        spectrum->setStats(median,sigma,par.getCut());
188        if(par.getFlagFDR()) spectrum->setupFDR();
189        spectrum->setMinSize(par.getMinChannels());
190        spectrum->findSources1D();
191      }
192
193      numDetected += spectrum->getNumObj();
194      for(int obj=0;obj<spectrum->getNumObj();obj++){
195        Detection *object = new Detection;
196        *object = spectrum->getObject(obj);
197        for(int pix=0;pix<object->getSize();pix++) {
198          // Fix up coordinates of each pixel to match original array
199          object->setZ(pix, object->getX(pix));
200          object->setX(pix, x);
201          object->setY(pix, y);
202          object->setF(pix, array[object->getZ(pix)]);
203          // NB: set F to the original value, not the recon value.
204        }
205        object->setOffsets(par);
206        object->calcParams();
207        mergeIntoList(*object,outputList,par);
208        delete object;
209      }
210   
211   
212    } // end of loop over y
213
214    printBackSpace(6);
215    std::cout << std::flush;
216  }   // end of loop over x
217
218  delete spectrum;
219  delete [] bigarray;
220  delete [] array;
221  delete [] fpixel;
222  delete [] lpixel;
223  delete [] inc;
224  delete [] dimAxes;
225  delete [] specdim;
226
227  return outputList;
228
229}
Note: See TracBrowser for help on using the repository browser.