source: trunk/src/mainDuchamp.cc @ 971

Last change on this file since 971 was 946, checked in by MatthewWhiting, 12 years ago

Moving the writing of the mask cube to outside the if(!cube->pars().getFlagUsePrevious()) loop. This way we can change the specification of the mask without needing to
re-do the preprocessing & searching. Also changed the interface to getMomenMap, so that it returns the bool mask rather than taking it as a parameter.

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