source: trunk/src/mainDuchamp.cc @ 890

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

Cleaning up the formatting of error reporting a little - removing line breaks from the error string and removing some needless "Exiting" comments.

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