source: tags/release-1.0.2/src/mainDuchamp.cc @ 1455

Last change on this file since 1455 was 120, checked in by Matthew Whiting, 18 years ago

Introduced error and warning reporting functions, to normalise the format
of errors and warnings. Definitions of functions go in duchamp.cc.
Changed all code that reports errors/warnings to use these new functions.

Fixed a couple of bugs that affected the way 2D images were dealt with:
ReconSearch? now looks at how many dimensions there are in the data array
before choosing the dimension of the reconstruction, and the minChannels
test was improved for the case of minChannels=0.

Some minor additions made to the Guide as well.

File size: 6.2 KB
Line 
1#include <iostream>
2#include <fstream>
3#include <string>
4#include <cpgplot.h>
5#include <math.h>
6#include <unistd.h>
7#include <time.h>
8
9#include <duchamp.hh>
10#include <Detection/detection.hh>
11#include <Cubes/cubes.hh>
12#include <Utils/utils.hh>
13#include <ATrous/atrous.hh>
14
15using std::ofstream;
16using std::string;
17
18Filter reconFilter;
19
20int main(int argc, char * argv[])
21{
22
23  string paramFile,fitsfile;
24  Cube *cube = new Cube;
25  Param *par = new Param;
26
27  if(argc==1){
28    std::cout << ERR_USAGE_MSG;
29    return 1;
30  }
31  else{
32    char c;
33    while( ( c = getopt(argc,argv,"p:f:hv") )!=-1){
34      switch(c) {
35      case 'p':
36        paramFile = optarg;
37        cube->readParam(paramFile);
38        break;
39      case 'f':
40        fitsfile = optarg;
41        par->setImageFile(fitsfile);
42        cube->saveParam(*par);
43        break;
44      case 'v':
45        std::cout << PROGNAME << " version " << VERSION << std::endl;
46        return 1;
47        break;
48      case 'h':
49      default :
50        std::cout << ERR_USAGE_MSG;
51        return 1;
52        break;
53      }
54    }
55  }
56
57  delete par;
58
59  if(cube->pars().getImageFile().empty()){
60    duchampError("mainDuchamp",
61                 "No input image has been given!\nUse the imageFile parameter in "+
62                 paramFile + " to specify the FITS file.\nExiting...\n");
63    return 1;
64  }
65
66  std::cout << "Opening image: " << cube->pars().getFullImageFile() << std::endl;
67  if( cube->getCube() == FAILURE){
68    duchampError("mainDuchamp",
69                 "Unable to open image file "+cube->pars().getFullImageFile()+
70                 "\nExiting...\n");
71    return 1;
72  }
73  else std::cout << "Opened successfully." << std::endl;
74
75  // If the reconstructed array is to be read in from disk
76  if( cube->pars().getFlagReconExists() && cube->pars().getFlagATrous() ){
77    std::cout << "Reading reconstructed array: "<<std::endl;
78    if( cube->readReconCube() == FAILURE){
79      std::stringstream errmsg;
80      errmsg <<"Could not read in existing reconstructed array.\n"
81             <<"Will perform reconstruction using assigned parameters.\n";
82      duchampWarning("mainDuchamp", errmsg.str());
83      cube->pars().setFlagReconExists(false);
84    }
85    else std::cout << "Reconstructed array available.\n";
86  }
87
88  // Filter needed for baseline removal and a trous reconstruction
89  if(cube->pars().getFlagATrous() || cube->pars().getFlagBaseline()){
90    reconFilter.define(cube->pars().getFilterCode());
91    cube->pars().setFilterName(reconFilter.getName());
92  }
93
94  // Write the parameters to screen.
95  if(cube->getDimZ()==1) cube->pars().setMinChannels(0);  // special case for 2D images.
96  std::cout << cube->pars();
97
98  if(cube->pars().getFlagLog()){
99    // Open the logfile and write the time on the first line
100    ofstream logfile(cube->pars().getLogFile().c_str());
101    logfile << "New run of the Duchamp sourcefinder: ";
102    time_t now = time(NULL);
103    logfile << asctime( localtime(&now) );
104    // Write out the command-line statement
105    logfile << "Executing statement : ";
106    for(int i=0;i<argc;i++) logfile << argv[i] << " ";
107    logfile << std::endl;
108    logfile << cube->pars();
109    logfile.close();
110  }
111
112  if(cube->pars().getFlagBlankPix()){
113    // Trim any blank pixels from the edges, and report the new size 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.                 "<<std::endl;
127  }
128
129  if(cube->pars().getFlagNegative()){
130    std::cout<<"Inverting the Cube... "<<std::flush;
131    cube->invert();
132    std::cout<<" Done."<<std::endl;
133  }
134
135  if(cube->pars().getFlagATrous()){
136    std::cout<<"Commencing search in reconstructed cube..."<<std::endl;
137    cube->ReconSearch();
138    std::cout<<"Done. Found "<<cube->getNumObj()<<" objects."<<std::endl;
139  }
140  else{
141    std::cout<<"Commencing search in cube..."<<std::endl;
142    cube->CubicSearch();
143    std::cout<<"Done. Found "<<cube->getNumObj()<<" objects."<<std::endl;
144  }
145
146  std::cout<<"Merging lists...  "<<std::flush;
147  cube->ObjectMerger();
148  std::cout<<"Done.                      "<<std::endl;
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  cube->replaceBaseline();
158
159  if(cube->pars().getFlagCubeTrimmed()){
160    std::cout<<"Replacing the trimmed pixels on the edges...  "<<std::flush;
161    cube->unTrimCube();
162    std::cout<<"Done."<<std::endl;
163  }
164
165  if(cube->getNumObj()>0){
166
167    cube->calcObjectWCSparams();
168
169    cube->setObjectFlags();
170   
171    cube->sortDetections();
172   
173    cube->outputDetectionList();
174  }
175
176  std::cout<<"Creating the maps...  "<<std::flush;
177  cube->plotMomentMap("/xs");
178  if(cube->pars().getFlagMaps()){
179    cube->plotMomentMap(cube->pars().getMomentMap()+"/vcps");
180    cube->plotDetectionMap(cube->pars().getDetectionMap()+"/vcps");
181  }
182  std::cout << "done.\n";
183
184  if((cube->getDimZ()>1) && (cube->getNumObj()>0)){
185    std::cout << "Plotting the individual spectra... " << std::flush;
186    cube->outputSpectra();
187    std::cout << "done.\n";
188  }
189  else if(cube->getDimZ()<=1)
190    duchampWarning("mainDuchamp","Not plotting any spectra  -- no third dimension.\n");
191
192  cpgend();
193
194  if(cube->pars().getFlagATrous()&&
195     (cube->pars().getFlagOutputRecon()||cube->pars().getFlagOutputResid()) ){
196    std::cout << "Saving reconstructed cube... "<<std::flush;
197    cube->saveReconstructedCube();
198    std::cout << "done.\n";
199  }
200
201  if(cube->pars().getFlagLog() && (cube->getNumObj()>0)){
202    ofstream logfile(cube->pars().getLogFile().c_str(),std::ios::app);
203    logfile << "=-=-=-=-=-=-=-\nCube summary\n=-=-=-=-=-=-=-\n";
204    logfile << *cube;
205    logfile.close();
206  }
207
208  if(cube->pars().getFlagVOT()){
209    ofstream votfile(cube->pars().getVOTFile().c_str());
210    cube->outputDetectionsVOTable(votfile);
211    votfile.close();
212  }
213
214  if(cube->pars().getFlagKarma()){
215    ofstream karmafile(cube->pars().getKarmaFile().c_str());
216    cube->outputDetectionsKarma(karmafile);
217    karmafile.close();
218  }
219
220  delete cube;
221
222  return 0;
223}
224
Note: See TracBrowser for help on using the repository browser.