source: trunk/src/mainDuchamp.cc @ 872

Last change on this file since 872 was 872, checked in by MatthewWhiting, 13 years ago

Adding basic exception handling, with a new DuchampError? class and a try-catch loop in the main program.

File size: 9.0 KB
Line 
1// -----------------------------------------------------------------------
2// mainDuchamp.cc: Main program for Duchamp source finder.
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 <fstream>
30#include <string>
31#include <math.h>
32#include <unistd.h>
33#include <time.h>
34
35#include <duchamp/duchamp.hh>
36#include <duchamp/param.hh>
37#include <duchamp/pgheader.hh>
38#include <duchamp/Detection/detection.hh>
39#include <duchamp/Cubes/cubes.hh>
40#include <duchamp/Utils/utils.hh>
41#include <duchamp/ATrous/atrous.hh>
42
43using namespace duchamp;
44
45int main(int argc, char * argv[])
46{
47
48  try {
49   
50
51  std::string paramFile,fitsfile;
52  Cube *cube = new Cube;
53
54  if(cube->getopts(argc,argv)==FAILURE) return FAILURE;
55
56  if(cube->pars().getImageFile().empty()){
57    std::stringstream errmsg;
58    errmsg << "No input image has been given!\n"
59           << "Use the imageFile parameter in "
60           << paramFile << " to specify the FITS file.\nExiting...\n";
61    duchampError("Duchamp", errmsg.str());
62    return FAILURE;
63  }
64
65  if(cube->pars().getFlagSubsection() || cube->pars().getFlagStatSec()){
66    // make sure the subsection is OK.
67    if(cube->pars().verifySubsection() == FAILURE){
68      duchampError("Duchamp",
69                   "Unable to use the subsection provided.\nExiting...\n");
70      return FAILURE;
71    }
72  }     
73
74  std::cout << "Opening image: "
75            << cube->pars().getFullImageFile() << std::endl;
76
77  if( cube->getCube() == FAILURE){
78    std::stringstream errmsg;
79    errmsg << "Unable to open image file "
80           << cube->pars().getFullImageFile()
81           << "\nExiting...\n";
82    duchampError("Duchamp", errmsg.str());
83    return FAILURE;
84  }
85  else std::cout << "Opened successfully." << std::endl;
86
87  // Read in any saved arrays that are in FITS files on disk.
88  if(!cube->pars().getFlagUsePrevious())
89    cube->readSavedArrays();
90
91  // special case for 2D images -- ignore the minChannels requirement
92  if(cube->getDimZ()==1) cube->pars().setMinChannels(0); 
93
94  // Write the parameters to screen.
95  std::cout << cube->pars();
96
97  if(cube->pars().getFlagUsePrevious()){
98    std::cout << "Reading detections from existing log file... \n";
99    if(cube->getExistingDetections() == FAILURE){
100      duchampError("Duchamp",
101                   "Could not read detections from log file\nExiting...\n");
102      return FAILURE;
103    }
104    else std::cout << "Done.\n";
105  }
106  else {
107
108    if(cube->pars().getFlagLog()){
109      // Prepare the log file.
110      cube->prepareLogFile(argc, argv);
111    }
112
113    //if(cube->pars().getFlagBlankPix()){
114    if(cube->pars().getFlagTrim()){
115      // Trim any blank pixels from the edges, and report the new size
116      // of the cube
117      std::cout<<"Trimming the Blank Pixels from the edges...  "<<std::flush;
118      cube->trimCube();
119      std::cout<<"Done."<<std::endl;
120      std::cout << "New dimensions:  " << cube->getDimX();
121      if(cube->getNumDim()>1) std::cout << "x" << cube->getDimY();
122      if(cube->getNumDim()>2) std::cout << "x" << cube->getDimZ();
123      std::cout << std::endl;
124    }
125
126    if(cube->pars().getFlagBaseline()){
127      std::cout<<"Removing the baselines... "<<std::flush;
128      cube->removeBaseline();
129      std::cout<<" Done.                 \n";
130    }
131
132    if(cube->pars().getFlagNegative()){
133      std::cout<<"Inverting the Cube... "<<std::flush;
134      cube->invert();
135      std::cout<<" Done.\n";
136    }
137
138    cube->Search();
139    std::cout << "Done. Intermediate list has " << cube->getNumObj();
140    if(cube->getNumObj()==1) std::cout << " object.\n";
141    else std::cout << " objects.\n";
142
143    if(cube->getNumObj() > 0){
144      if(cube->pars().getFlagGrowth())
145        std::cout<<"Merging, Growing and Rejecting...  "<<std::flush;
146      else
147        std::cout<<"Merging and Rejecting...  "<<std::flush;
148      cube->ObjectMerger();
149      std::cout<<"Done.                      "<<std::endl;
150    }
151    std::cout<<"Final object count = "<<cube->getNumObj()<<std::endl;
152
153    if(cube->pars().getFlagNegative()){
154      std::cout<<"Un-Inverting the Cube... "<<std::flush;
155      cube->reInvert();
156      std::cout<<" Done."<<std::endl;
157    }
158
159    if(cube->pars().getFlagBaseline()){
160      std::cout<<"Replacing the baselines...  "<<std::flush;
161      cube->replaceBaseline();
162      std::cout<<"Done."<<std::endl;
163    }
164
165    if(cube->pars().getFlagCubeTrimmed()){
166      std::cout<<"Replacing the trimmed pixels on the edges...  "<<std::flush;
167      cube->unTrimCube();
168      std::cout<<"Done."<<std::endl;
169    }
170
171  }
172
173  cube->prepareOutputFile();
174
175  if(cube->getNumObj()>0){
176
177    std::cout << "Calculating final parameters and setting flags...  "<<std::flush;
178    cube->calcObjectWCSparams();
179
180    cube->setObjectFlags();
181   
182    cube->sortDetections();
183
184    std::cout << "Done." << std::endl;
185
186  }
187 
188  cube->outputDetectionList();
189
190  if(USE_PGPLOT){
191    std::cout<<"Creating the maps...  "<<std::flush;
192    std::vector<std::string> devices;
193    if(cube->pars().getFlagXOutput()) devices.push_back("/xs");
194    if(cube->pars().getFlagMaps())
195      devices.push_back(cube->pars().getMomentMap()+"/vcps");
196    cube->plotMomentMap(devices);
197    if(cube->pars().getFlagMaps())
198      cube->plotDetectionMap(cube->pars().getDetectionMap()+"/vcps");
199    std::cout << "Done.\n";
200   
201    if(cube->getNumObj()>0){
202      std::cout << "Plotting the individual spectra... " << std::flush;
203      cube->outputSpectra();
204      std::cout << "Done.\n";
205    }
206  }
207  else{
208    std::cout << "PGPLOT has not been enabled, so no graphical output.\n";
209  }
210
211  if(!cube->pars().getFlagUsePrevious()){
212
213    if(cube->pars().getFlagATrous()&&
214       (cube->pars().getFlagOutputRecon()||cube->pars().getFlagOutputResid()) ){
215      std::cout << "Saving reconstructed cube to "
216                << cube->pars().outputReconFile() << "... "<<std::flush;
217      if(cube->saveReconstructedCube() == FAILURE)
218        std::cout << "Failed!\n";
219      else
220        std::cout << "done.\n";
221    }
222    if(cube->pars().getFlagSmooth()&& cube->pars().getFlagOutputSmooth()){
223      std::cout << "Saving smoothed cube to "
224                << cube->pars().outputSmoothFile() << "... " <<std::flush;
225      if(cube->saveSmoothedCube() == FAILURE)
226        std::cout << "Failed!\n";
227      else
228        std::cout << "done.\n";
229    }
230    if(cube->pars().getFlagOutputMask()){
231      std::cout << "Saving mask cube to "
232                << cube->pars().outputMaskFile() << "... " <<std::flush;
233      if(cube->saveMaskCube() == FAILURE)
234        std::cout << "Failed!\n";
235      else
236        std::cout << "done.\n";
237    }
238    if(cube->pars().getFlagOutputMomentMap()){
239      std::cout << "Saving 0th moment map image to "
240                << cube->pars().outputMomentMapFile() << "... " <<std::flush;
241      if(cube->saveMomentMapImage() == FAILURE)
242        std::cout << "Failed!\n";
243      else
244        std::cout << "done.\n";
245    }
246
247    if(cube->pars().getFlagLog() && (cube->getNumObj()>0)){
248      std::ofstream logfile(cube->pars().getLogFile().c_str(),std::ios::app);
249      logfile << "=-=-=-=-=-=-=-\nCube summary\n=-=-=-=-=-=-=-\n";
250      logfile << *cube;
251      logfile.close();
252    }
253
254  }
255
256  if(cube->pars().getFlagVOT()){
257    std::ofstream votfile(cube->pars().getVOTFile().c_str());
258    cube->outputDetectionsVOTable(votfile);
259    votfile.close();
260  }
261
262  if(cube->pars().getFlagKarma()){
263    std::ofstream karmafile(cube->pars().getKarmaFile().c_str());
264    cube->outputDetectionsKarma(karmafile);
265    karmafile.close();
266  }
267
268  if(cube->pars().getFlagTextSpectra()){
269    if(cube->pars().isVerbose()) std::cout << "Saving spectra to text file ... ";
270    cube->writeSpectralData();
271    if(cube->pars().isVerbose()) std::cout << "done.\n";
272  }
273
274  if(!cube->pars().getFlagUsePrevious() && cube->pars().getFlagLog()){
275    // Open the logfile and write the time on the first line
276    std::ofstream logfile(cube->pars().getLogFile().c_str(),std::ios::app);
277    logfile << "Duchamp completed: ";
278    time_t now = time(NULL);
279    logfile << asctime( localtime(&now) );
280    logfile.close();
281  }
282
283
284  delete cube;
285
286  } catch (const DuchampError &err) {
287    std::cout << "\nDuchamp failed with error: " << err.what() << "\n";
288    exit(1);
289  }
290
291  return SUCCESS;
292
293}
294
Note: See TracBrowser for help on using the repository browser.