source: trunk/src/FitsIO/ReadAndSearch.cc @ 1213

Last change on this file since 1213 was 1123, checked in by MatthewWhiting, 12 years ago

Moving the code that reads from and writes to FITS files containing reconstructed, momentmap, mask etc arrays to the FitsIO directory, away from Cubes. Updating all include statements as well.

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    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->removeMW(); // only works if flagMW is true
180        spectrum->setStats(median,sigma,par.getCut());
181        if(par.getFlagFDR()) spectrum->setupFDR();
182        spectrum->setMinSize(par.getMinChannels());
183        spectrum->findSources1D();
184      }
185      else{
186        sigma  = findMADFM(array,dimAxes[2])/correctionFactor;
187        spectrum->saveArray(array,dimAxes[2]);
188        spectrum->removeMW(); // only works if flagMW is true
189        spectrum->setStats(median,sigma,par.getCut());
190        if(par.getFlagFDR()) spectrum->setupFDR();
191        spectrum->setMinSize(par.getMinChannels());
192        spectrum->findSources1D();
193      }
194
195      numDetected += spectrum->getNumObj();
196      for(int obj=0;obj<spectrum->getNumObj();obj++){
197        Detection *object = new Detection;
198        *object = spectrum->getObject(obj);
199        for(int pix=0;pix<object->getSize();pix++) {
200          // Fix up coordinates of each pixel to match original array
201          object->setZ(pix, object->getX(pix));
202          object->setX(pix, x);
203          object->setY(pix, y);
204          object->setF(pix, array[object->getZ(pix)]);
205          // NB: set F to the original value, not the recon value.
206        }
207        object->setOffsets(par);
208        object->calcParams();
209        mergeIntoList(*object,outputList,par);
210        delete object;
211      }
212   
213   
214    } // end of loop over y
215
216    printBackSpace(6);
217    std::cout << std::flush;
218  }   // end of loop over x
219
220  delete spectrum;
221  delete [] bigarray;
222  delete [] array;
223  delete [] fpixel;
224  delete [] lpixel;
225  delete [] inc;
226  delete [] dimAxes;
227  delete [] specdim;
228
229  return outputList;
230
231}
Note: See TracBrowser for help on using the repository browser.