source: tags/release-1.1.2/src/mainDuchamp.cc @ 1441

Last change on this file since 1441 was 393, checked in by MatthewWhiting, 17 years ago

Fixed up headers for trunk as well.

File size: 8.5 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  cube->readSavedArrays();
86
87  // special case for 2D images -- ignore the minChannels requirement
88  if(cube->getDimZ()==1) cube->pars().setMinChannels(0); 
89
90  // Write the parameters to screen.
91  std::cout << cube->pars();
92
93  if(cube->pars().getFlagLog()){
94    // Open the logfile and write the time on the first line
95    std::ofstream logfile(cube->pars().getLogFile().c_str());
96    logfile << "New run of the Duchamp sourcefinder: ";
97    time_t now = time(NULL);
98    logfile << asctime( localtime(&now) );
99    // Write out the command-line statement
100    logfile << "Executing statement : ";
101    for(int i=0;i<argc;i++) logfile << argv[i] << " ";
102    logfile << std::endl;
103    logfile << cube->pars();
104    logfile.close();
105  }
106
107  //if(cube->pars().getFlagBlankPix()){
108  if(cube->pars().getFlagTrim()){
109    // Trim any blank pixels from the edges, and report the new size
110    // of the cube
111    std::cout<<"Trimming the Blank Pixels from the edges...  "<<std::flush;
112    cube->trimCube();
113    std::cout<<"Done."<<std::endl;
114    std::cout << "New dimensions:  " << cube->getDimX();
115    if(cube->getNumDim()>1) std::cout << "x" << cube->getDimY();
116    if(cube->getNumDim()>2) std::cout << "x" << cube->getDimZ();
117    std::cout << std::endl;
118  }
119
120  if(cube->pars().getFlagBaseline()){
121    std::cout<<"Removing the baselines... "<<std::flush;
122    cube->removeBaseline();
123    std::cout<<" Done.                 "<<std::endl;
124  }
125
126  if(cube->pars().getFlagNegative()){
127    std::cout<<"Inverting the Cube... "<<std::flush;
128    cube->invert();
129    std::cout<<" Done."<<std::endl;
130  }
131
132  if(cube->pars().getFlagATrous()){
133    std::cout<<"Commencing search in reconstructed cube..."<<std::endl;
134    cube->ReconSearch();
135  } 
136  else if(cube->pars().getFlagSmooth()){
137    std::cout<<"Commencing search in smoothed cube..."<<std::endl;
138    cube->SmoothSearch();
139  }
140  else{
141    std::cout<<"Commencing search in cube..."<<std::endl;
142    cube->CubicSearch();
143  }
144  std::cout << "Done. Intermediate list has " << cube->getNumObj();
145  if(cube->getNumObj()==1) std::cout << " object.\n";
146  else std::cout << " objects.\n";
147
148  if(cube->getNumObj() > 1){
149    if(cube->pars().getFlagGrowth())
150      std::cout<<"Merging, Growing and Rejecting...  "<<std::flush;
151    else
152      std::cout<<"Merging and Rejecting...  "<<std::flush;
153    cube->ObjectMerger();
154    std::cout<<"Done.                      "<<std::endl;
155  }
156  std::cout<<"Final object count = "<<cube->getNumObj()<<std::endl;
157
158  if(cube->pars().getFlagNegative()){
159    std::cout<<"Un-Inverting the Cube... "<<std::flush;
160    cube->reInvert();
161    std::cout<<" Done."<<std::endl;
162  }
163
164  if(cube->pars().getFlagBaseline()){
165    std::cout<<"Replacing the baselines...  "<<std::flush;
166    cube->replaceBaseline();
167    std::cout<<"Done."<<std::endl;
168  }
169
170  if(cube->pars().getFlagCubeTrimmed()){
171    std::cout<<"Replacing the trimmed pixels on the edges...  "<<std::flush;
172    cube->unTrimCube();
173    std::cout<<"Done."<<std::endl;
174  }
175
176  cube->prepareOutputFile();
177
178  if(cube->getNumObj()>0){
179
180    cube->calcObjectWCSparams();
181
182    cube->setObjectFlags();
183   
184    cube->sortDetections();
185  }
186 
187  cube->outputDetectionList();
188
189  if(USE_PGPLOT){
190    std::cout<<"Creating the maps...  "<<std::flush;
191    std::vector<std::string> devices;
192    if(cube->pars().getFlagXOutput()) devices.push_back("/xs");
193    if(cube->pars().getFlagMaps())
194      devices.push_back(cube->pars().getMomentMap()+"/vcps");
195    cube->plotMomentMap(devices);
196    if(cube->pars().getFlagMaps())
197      cube->plotDetectionMap(cube->pars().getDetectionMap()+"/vcps");
198    std::cout << "Done.\n";
199   
200//     if((cube->getDimZ()>1) && (cube->getNumObj()>0)){
201    if(cube->getNumObj()>0){
202      std::cout << "Plotting the individual spectra... " << std::flush;
203      cube->outputSpectra();
204      std::cout << "Done.\n";
205    }
206//     else if(cube->getDimZ()<=1)
207//       duchampWarning("Duchamp",
208//                   "Not plotting any spectra : no third dimension.\n");
209  }
210  else{
211    std::cout << "PGPLOT has not been enabled, so no graphical output.\n";
212  }
213
214  if(cube->pars().getFlagATrous()&&
215     (cube->pars().getFlagOutputRecon()||cube->pars().getFlagOutputResid()) ){
216    std::cout << "Saving reconstructed cube to "
217              << cube->pars().outputReconFile() << "... "<<std::flush;
218    cube->saveReconstructedCube();
219    std::cout << "done.\n";
220  }
221  if(cube->pars().getFlagSmooth()&& cube->pars().getFlagOutputSmooth()){
222    std::cout << "Saving smoothed cube to "
223              << cube->pars().outputSmoothFile() << "... " <<std::flush;
224    cube->saveSmoothedCube();
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    cube->saveMaskCube();
231    std::cout << "done.\n";
232  }
233
234  if(cube->pars().getFlagLog() && (cube->getNumObj()>0)){
235    std::ofstream logfile(cube->pars().getLogFile().c_str(),std::ios::app);
236    logfile << "=-=-=-=-=-=-=-\nCube summary\n=-=-=-=-=-=-=-\n";
237    logfile << *cube;
238    logfile.close();
239  }
240
241  if(cube->pars().getFlagVOT()){
242    std::ofstream votfile(cube->pars().getVOTFile().c_str());
243    cube->outputDetectionsVOTable(votfile);
244    votfile.close();
245  }
246
247  if(cube->pars().getFlagKarma()){
248    std::ofstream karmafile(cube->pars().getKarmaFile().c_str());
249    cube->outputDetectionsKarma(karmafile);
250    karmafile.close();
251  }
252
253  if(cube->pars().getFlagLog()){
254    // Open the logfile and write the time on the first line
255    std::ofstream logfile(cube->pars().getLogFile().c_str(),std::ios::app);
256    logfile << "Duchamp completed: ";
257    time_t now = time(NULL);
258    logfile << asctime( localtime(&now) );
259    logfile.close();
260  }
261
262
263  delete cube;
264
265  return SUCCESS;
266}
267
Note: See TracBrowser for help on using the repository browser.