source: trunk/src/mainDuchamp.cc @ 312

Last change on this file since 312 was 312, checked in by Matthew Whiting, 17 years ago

Make sure we print out something to the results file when there are zero detections.

File size: 7.7 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 <cpgplot.h>
32#include <math.h>
33#include <unistd.h>
34#include <time.h>
35
36#include <duchamp.hh>
37#include <param.hh>
38#include <Detection/detection.hh>
39#include <Cubes/cubes.hh>
40#include <Utils/utils.hh>
41#include <ATrous/atrous.hh>
42
43int main(int argc, char * argv[])
44{
45
46  std::string paramFile,fitsfile;
47  Cube *cube = new Cube;
48
49  if(cube->getopts(argc,argv)==FAILURE) return FAILURE;
50
51  if(cube->pars().getImageFile().empty()){
52    std::stringstream errmsg;
53    errmsg << "No input image has been given!\n"
54           << "Use the imageFile parameter in "
55           << paramFile << " to specify the FITS file.\nExiting...\n";
56    duchampError("Duchamp", errmsg.str());
57    return FAILURE;
58  }
59
60  if(cube->pars().getFlagSubsection() || cube->pars().getFlagStatSec()){
61    // make sure the subsection is OK.
62    if(cube->pars().verifySubsection() == FAILURE){
63      duchampError("Duchamp",
64                   "Unable to use the subsection provided.\nExiting...\n");
65      return FAILURE;
66    }
67  }     
68
69  std::cout << "Opening image: "
70            << cube->pars().getFullImageFile() << std::endl;
71
72  if( cube->getCube() == FAILURE){
73    std::stringstream errmsg;
74    errmsg << "Unable to open image file "
75           << cube->pars().getFullImageFile()
76           << "\nExiting...\n";
77    duchampError("Duchamp", errmsg.str());
78    return FAILURE;
79  }
80  else std::cout << "Opened successfully." << std::endl;
81
82  // Read in any saved arrays that are in FITS files on disk.
83  cube->readSavedArrays();
84
85  // special case for 2D images -- ignore the minChannels requirement
86  if(cube->getDimZ()==1) cube->pars().setMinChannels(0); 
87
88  // Write the parameters to screen.
89  std::cout << cube->pars();
90
91  if(cube->pars().getFlagLog()){
92    // Open the logfile and write the time on the first line
93    std::ofstream logfile(cube->pars().getLogFile().c_str());
94    logfile << "New run of the Duchamp sourcefinder: ";
95    time_t now = time(NULL);
96    logfile << asctime( localtime(&now) );
97    // Write out the command-line statement
98    logfile << "Executing statement : ";
99    for(int i=0;i<argc;i++) logfile << argv[i] << " ";
100    logfile << std::endl;
101    logfile << cube->pars();
102    logfile.close();
103  }
104
105  //if(cube->pars().getFlagBlankPix()){
106  if(cube->pars().getFlagTrim()){
107    // Trim any blank pixels from the edges, and report the new size
108    // of the cube
109    std::cout<<"Trimming the Blank Pixels from the edges...  "<<std::flush;
110    cube->trimCube();
111    std::cout<<"Done."<<std::endl;
112    std::cout << "New dimensions:  " << cube->getDimX();
113    if(cube->getNumDim()>1) std::cout << "x" << cube->getDimY();
114    if(cube->getNumDim()>2) std::cout << "x" << cube->getDimZ();
115    std::cout << std::endl;
116  }
117
118  if(cube->pars().getFlagBaseline()){
119    std::cout<<"Removing the baselines... "<<std::flush;
120    cube->removeBaseline();
121    std::cout<<" Done.                 "<<std::endl;
122  }
123
124  if(cube->pars().getFlagNegative()){
125    std::cout<<"Inverting the Cube... "<<std::flush;
126    cube->invert();
127    std::cout<<" Done."<<std::endl;
128  }
129
130  if(cube->pars().getFlagATrous()){
131    std::cout<<"Commencing search in reconstructed cube..."<<std::endl;
132    cube->ReconSearch();
133  } 
134  else if(cube->pars().getFlagSmooth()){
135    std::cout<<"Commencing search in smoothed cube..."<<std::endl;
136    cube->SmoothSearch();
137  }
138  else{
139    std::cout<<"Commencing search in cube..."<<std::endl;
140    cube->CubicSearch();
141  }
142  std::cout << "Done. Intermediate list has " << cube->getNumObj();
143  if(cube->getNumObj()==1) std::cout << " object.\n";
144  else std::cout << " objects.\n";
145
146  if(cube->getNumObj() > 1){
147    if(cube->pars().getFlagGrowth())
148      std::cout<<"Merging, Growing and Rejecting...  "<<std::flush;
149    else
150      std::cout<<"Merging and Rejecting...  "<<std::flush;
151    cube->ObjectMerger();
152    std::cout<<"Done.                      "<<std::endl;
153  }
154  std::cout<<"Final object count = "<<cube->getNumObj()<<std::endl;
155
156  if(cube->pars().getFlagNegative()){
157    std::cout<<"Un-Inverting the Cube... "<<std::flush;
158    cube->reInvert();
159    std::cout<<" Done."<<std::endl;
160  }
161
162  if(cube->pars().getFlagBaseline()){
163    std::cout<<"Replacing the baselines...  "<<std::flush;
164    cube->replaceBaseline();
165    std::cout<<"Done."<<std::endl;
166  }
167
168  if(cube->pars().getFlagCubeTrimmed()){
169    std::cout<<"Replacing the trimmed pixels on the edges...  "<<std::flush;
170    cube->unTrimCube();
171    std::cout<<"Done."<<std::endl;
172  }
173
174  cube->prepareOutputFile();
175
176  if(cube->getNumObj()>0){
177
178    cube->calcObjectWCSparams();
179
180    cube->setObjectFlags();
181   
182    cube->sortDetections();
183  }
184 
185  cube->outputDetectionList();
186
187
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->getDimZ()>1) && (cube->getNumObj()>0)){
199    std::cout << "Plotting the individual spectra... " << std::flush;
200    cube->outputSpectra();
201    std::cout << "Done.\n";
202  }
203  else if(cube->getDimZ()<=1)
204    duchampWarning("Duchamp",
205                   "Not plotting any spectra : no third dimension.\n");
206
207  cpgend();
208
209  if(cube->pars().getFlagATrous()&&
210     (cube->pars().getFlagOutputRecon()||cube->pars().getFlagOutputResid()) ){
211    std::cout << "Saving reconstructed cube to "
212              << cube->pars().outputReconFile() << "... "<<std::flush;
213    cube->saveReconstructedCube();
214    std::cout << "done.\n";
215  }
216  if(cube->pars().getFlagSmooth()&& cube->pars().getFlagOutputSmooth()){
217    std::cout << "Saving smoothed cube to "
218              << cube->pars().outputSmoothFile() << "... " <<std::flush;
219    cube->saveSmoothedCube();
220    std::cout << "done.\n";
221  }
222
223  if(cube->pars().getFlagLog() && (cube->getNumObj()>0)){
224    std::ofstream logfile(cube->pars().getLogFile().c_str(),std::ios::app);
225    logfile << "=-=-=-=-=-=-=-\nCube summary\n=-=-=-=-=-=-=-\n";
226    logfile << *cube;
227    logfile.close();
228  }
229
230  if(cube->pars().getFlagVOT()){
231    std::ofstream votfile(cube->pars().getVOTFile().c_str());
232    cube->outputDetectionsVOTable(votfile);
233    votfile.close();
234  }
235
236  if(cube->pars().getFlagKarma()){
237    std::ofstream karmafile(cube->pars().getKarmaFile().c_str());
238    cube->outputDetectionsKarma(karmafile);
239    karmafile.close();
240  }
241
242  delete cube;
243
244  return SUCCESS;
245}
246
Note: See TracBrowser for help on using the repository browser.