source: trunk/src/mainDuchamp.cc @ 541

Last change on this file since 541 was 475, checked in by MatthewWhiting, 16 years ago

Implemented changes to Duchamp to allow the re-plotting of object spectra. This incorporates the functionality of Selavy into Duchamp, so that you only need a single executable.

File size: 8.5 KB
RevLine 
[299]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// -----------------------------------------------------------------------
[3]28#include <iostream>
29#include <fstream>
30#include <string>
31#include <math.h>
32#include <unistd.h>
[80]33#include <time.h>
[57]34
[393]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>
[3]42
[378]43using namespace duchamp;
44
[3]45int main(int argc, char * argv[])
46{
47
[232]48  std::string paramFile,fitsfile;
[85]49  Cube *cube = new Cube;
[3]50
[168]51  if(cube->getopts(argc,argv)==FAILURE) return FAILURE;
[3]52
53  if(cube->pars().getImageFile().empty()){
[139]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";
[168]58    duchampError("Duchamp", errmsg.str());
59    return FAILURE;
[3]60  }
61
[258]62  if(cube->pars().getFlagSubsection() || cube->pars().getFlagStatSec()){
[168]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  }     
[164]70
[139]71  std::cout << "Opening image: "
72            << cube->pars().getFullImageFile() << std::endl;
73
[106]74  if( cube->getCube() == FAILURE){
[139]75    std::stringstream errmsg;
76    errmsg << "Unable to open image file "
77           << cube->pars().getFullImageFile()
78           << "\nExiting...\n";
[168]79    duchampError("Duchamp", errmsg.str());
80    return FAILURE;
[3]81  }
[103]82  else std::cout << "Opened successfully." << std::endl;
[3]83
[208]84  // Read in any saved arrays that are in FITS files on disk.
[475]85  if(!cube->pars().getFlagUsePrevious())
86    cube->readSavedArrays();
[71]87
[139]88  // special case for 2D images -- ignore the minChannels requirement
89  if(cube->getDimZ()==1) cube->pars().setMinChannels(0); 
90
[103]91  // Write the parameters to screen.
[3]92  std::cout << cube->pars();
93
[475]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";
[3]102  }
[475]103  else {
[3]104
[475]105    if(cube->pars().getFlagLog()){
106      // Prepare the log file.
107      cube->prepareLogFile(argc, argv);
108    }
[3]109
[475]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    }
[3]122
[475]123    if(cube->pars().getFlagBaseline()){
124      std::cout<<"Removing the baselines... "<<std::flush;
125      cube->removeBaseline();
126      std::cout<<" Done.                 \n";
127    }
[60]128
[475]129    if(cube->pars().getFlagNegative()){
130      std::cout<<"Inverting the Cube... "<<std::flush;
131      cube->invert();
132      std::cout<<" Done.\n";
133    }
[3]134
[475]135    if(cube->pars().getFlagATrous()){
136      std::cout<<"Commencing search in reconstructed cube..."<<std::endl;
137      cube->ReconSearch();
138    } 
139    else if(cube->pars().getFlagSmooth()){
140      std::cout<<"Commencing search in smoothed cube..."<<std::endl;
141      cube->SmoothSearch();
142    }
143    else{
144      std::cout<<"Commencing search in cube..."<<std::endl;
145      cube->CubicSearch();
146    }
147    std::cout << "Done. Intermediate list has " << cube->getNumObj();
148    if(cube->getNumObj()==1) std::cout << " object.\n";
149    else std::cout << " objects.\n";
[3]150
[475]151    if(cube->getNumObj() > 1){
152      if(cube->pars().getFlagGrowth())
153        std::cout<<"Merging, Growing and Rejecting...  "<<std::flush;
154      else
155        std::cout<<"Merging and Rejecting...  "<<std::flush;
156      cube->ObjectMerger();
157      std::cout<<"Done.                      "<<std::endl;
158    }
159    std::cout<<"Final object count = "<<cube->getNumObj()<<std::endl;
[60]160
[475]161    if(cube->pars().getFlagNegative()){
162      std::cout<<"Un-Inverting the Cube... "<<std::flush;
163      cube->reInvert();
164      std::cout<<" Done."<<std::endl;
165    }
[3]166
[475]167    if(cube->pars().getFlagBaseline()){
168      std::cout<<"Replacing the baselines...  "<<std::flush;
169      cube->replaceBaseline();
170      std::cout<<"Done."<<std::endl;
171    }
172
173    if(cube->pars().getFlagCubeTrimmed()){
174      std::cout<<"Replacing the trimmed pixels on the edges...  "<<std::flush;
175      cube->unTrimCube();
176      std::cout<<"Done."<<std::endl;
177    }
178
[3]179  }
180
[275]181  cube->prepareOutputFile();
182
[3]183  if(cube->getNumObj()>0){
184
185    cube->calcObjectWCSparams();
[87]186
187    cube->setObjectFlags();
[3]188   
189    cube->sortDetections();
[475]190
[3]191  }
[312]192 
193  cube->outputDetectionList();
[3]194
[315]195  if(USE_PGPLOT){
196    std::cout<<"Creating the maps...  "<<std::flush;
197    std::vector<std::string> devices;
198    if(cube->pars().getFlagXOutput()) devices.push_back("/xs");
199    if(cube->pars().getFlagMaps())
200      devices.push_back(cube->pars().getMomentMap()+"/vcps");
201    cube->plotMomentMap(devices);
202    if(cube->pars().getFlagMaps())
203      cube->plotDetectionMap(cube->pars().getDetectionMap()+"/vcps");
[258]204    std::cout << "Done.\n";
[315]205   
[367]206    if(cube->getNumObj()>0){
[315]207      std::cout << "Plotting the individual spectra... " << std::flush;
208      cube->outputSpectra();
209      std::cout << "Done.\n";
210    }
[3]211  }
[315]212  else{
213    std::cout << "PGPLOT has not been enabled, so no graphical output.\n";
214  }
[3]215
[475]216  if(!cube->pars().getFlagUsePrevious()){
[3]217
[475]218    if(cube->pars().getFlagATrous()&&
219       (cube->pars().getFlagOutputRecon()||cube->pars().getFlagOutputResid()) ){
220      std::cout << "Saving reconstructed cube to "
221                << cube->pars().outputReconFile() << "... "<<std::flush;
222      cube->saveReconstructedCube();
223      std::cout << "done.\n";
224    }
225    if(cube->pars().getFlagSmooth()&& cube->pars().getFlagOutputSmooth()){
226      std::cout << "Saving smoothed cube to "
227                << cube->pars().outputSmoothFile() << "... " <<std::flush;
228      cube->saveSmoothedCube();
229      std::cout << "done.\n";
230    }
231    if(cube->pars().getFlagOutputMask()){
232      std::cout << "Saving mask cube to "
233                << cube->pars().outputMaskFile() << "... " <<std::flush;
234      cube->saveMaskCube();
235      std::cout << "done.\n";
236    }
237
238    if(cube->pars().getFlagLog() && (cube->getNumObj()>0)){
239      std::ofstream logfile(cube->pars().getLogFile().c_str(),std::ios::app);
240      logfile << "=-=-=-=-=-=-=-\nCube summary\n=-=-=-=-=-=-=-\n";
241      logfile << *cube;
242      logfile.close();
243    }
244
[3]245  }
246
[349]247  if(cube->pars().getFlagVOT()){
[232]248    std::ofstream votfile(cube->pars().getVOTFile().c_str());
[3]249    cube->outputDetectionsVOTable(votfile);
250    votfile.close();
251  }
252
[349]253  if(cube->pars().getFlagKarma()){
[232]254    std::ofstream karmafile(cube->pars().getKarmaFile().c_str());
[20]255    cube->outputDetectionsKarma(karmafile);
256    karmafile.close();
257  }
258
[475]259  if(!cube->pars().getFlagUsePrevious() && cube->pars().getFlagLog()){
[313]260    // Open the logfile and write the time on the first line
261    std::ofstream logfile(cube->pars().getLogFile().c_str(),std::ios::app);
262    logfile << "Duchamp completed: ";
263    time_t now = time(NULL);
264    logfile << asctime( localtime(&now) );
265    logfile.close();
266  }
267
268
[3]269  delete cube;
270
[168]271  return SUCCESS;
[3]272}
273
Note: See TracBrowser for help on using the repository browser.