source: tags/release-1.1.12/src/mainDuchamp.cc

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

Removing default verbosity flags for cube::Search()

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