source: trunk/src/mainDuchamp.cc @ 379

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

Added in functionality to output a mask cube FITS file. Cube has values 1 where there is a detected object, and 0 elsewhere. Works in similar way to the reconCube or smoothCube.

File size: 8.4 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
35#include <duchamp.hh>
[142]36#include <param.hh>
[317]37#include <pgheader.hh>
[3]38#include <Detection/detection.hh>
39#include <Cubes/cubes.hh>
40#include <Utils/utils.hh>
41#include <ATrous/atrous.hh>
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.
85  cube->readSavedArrays();
[71]86
[139]87  // special case for 2D images -- ignore the minChannels requirement
88  if(cube->getDimZ()==1) cube->pars().setMinChannels(0); 
89
[103]90  // Write the parameters to screen.
[3]91  std::cout << cube->pars();
92
93  if(cube->pars().getFlagLog()){
[106]94    // Open the logfile and write the time on the first line
[232]95    std::ofstream logfile(cube->pars().getLogFile().c_str());
[3]96    logfile << "New run of the Duchamp sourcefinder: ";
[80]97    time_t now = time(NULL);
98    logfile << asctime( localtime(&now) );
[120]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;
[3]103    logfile << cube->pars();
104    logfile.close();
105  }
106
[285]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
[3]111    std::cout<<"Trimming the Blank Pixels from the edges...  "<<std::flush;
112    cube->trimCube();
[103]113    std::cout<<"Done."<<std::endl;
[3]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();
[103]117    std::cout << std::endl;
[3]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
[60]126  if(cube->pars().getFlagNegative()){
127    std::cout<<"Inverting the Cube... "<<std::flush;
128    cube->invert();
129    std::cout<<" Done."<<std::endl;
130  }
131
[208]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()){
[275]137    std::cout<<"Commencing search in smoothed cube..."<<std::endl;
[201]138    cube->SmoothSearch();
139  }
[3]140  else{
[103]141    std::cout<<"Commencing search in cube..."<<std::endl;
142    cube->CubicSearch();
[3]143  }
[258]144  std::cout << "Done. Intermediate list has " << cube->getNumObj();
[201]145  if(cube->getNumObj()==1) std::cout << " object.\n";
146  else std::cout << " objects.\n";
[3]147
[146]148  if(cube->getNumObj() > 1){
[265]149    if(cube->pars().getFlagGrowth())
150      std::cout<<"Merging, Growing and Rejecting...  "<<std::flush;
151    else
152      std::cout<<"Merging and Rejecting...  "<<std::flush;
[146]153    cube->ObjectMerger();
154    std::cout<<"Done.                      "<<std::endl;
155  }
[103]156  std::cout<<"Final object count = "<<cube->getNumObj()<<std::endl;
[3]157
[60]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
[258]164  if(cube->pars().getFlagBaseline()){
165    std::cout<<"Replacing the baselines...  "<<std::flush;
166    cube->replaceBaseline();
167    std::cout<<"Done."<<std::endl;
168  }
[3]169
170  if(cube->pars().getFlagCubeTrimmed()){
171    std::cout<<"Replacing the trimmed pixels on the edges...  "<<std::flush;
172    cube->unTrimCube();
[103]173    std::cout<<"Done."<<std::endl;
[3]174  }
175
[275]176  cube->prepareOutputFile();
177
[3]178  if(cube->getNumObj()>0){
179
180    cube->calcObjectWCSparams();
[87]181
182    cube->setObjectFlags();
[3]183   
184    cube->sortDetections();
185  }
[312]186 
187  cube->outputDetectionList();
[3]188
[315]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");
[258]198    std::cout << "Done.\n";
[315]199   
[367]200//     if((cube->getDimZ()>1) && (cube->getNumObj()>0)){
201    if(cube->getNumObj()>0){
[315]202      std::cout << "Plotting the individual spectra... " << std::flush;
203      cube->outputSpectra();
204      std::cout << "Done.\n";
205    }
[367]206//     else if(cube->getDimZ()<=1)
207//       duchampWarning("Duchamp",
208//                   "Not plotting any spectra : no third dimension.\n");
[3]209  }
[315]210  else{
211    std::cout << "PGPLOT has not been enabled, so no graphical output.\n";
212  }
[3]213
214  if(cube->pars().getFlagATrous()&&
215     (cube->pars().getFlagOutputRecon()||cube->pars().getFlagOutputResid()) ){
[258]216    std::cout << "Saving reconstructed cube to "
[208]217              << cube->pars().outputReconFile() << "... "<<std::flush;
[3]218    cube->saveReconstructedCube();
219    std::cout << "done.\n";
220  }
[208]221  if(cube->pars().getFlagSmooth()&& cube->pars().getFlagOutputSmooth()){
[290]222    std::cout << "Saving smoothed cube to "
[208]223              << cube->pars().outputSmoothFile() << "... " <<std::flush;
224    cube->saveSmoothedCube();
225    std::cout << "done.\n";
226  }
[379]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  }
[3]233
234  if(cube->pars().getFlagLog() && (cube->getNumObj()>0)){
[232]235    std::ofstream logfile(cube->pars().getLogFile().c_str(),std::ios::app);
[3]236    logfile << "=-=-=-=-=-=-=-\nCube summary\n=-=-=-=-=-=-=-\n";
237    logfile << *cube;
238    logfile.close();
239  }
240
[349]241  if(cube->pars().getFlagVOT()){
[232]242    std::ofstream votfile(cube->pars().getVOTFile().c_str());
[3]243    cube->outputDetectionsVOTable(votfile);
244    votfile.close();
245  }
246
[349]247  if(cube->pars().getFlagKarma()){
[232]248    std::ofstream karmafile(cube->pars().getKarmaFile().c_str());
[20]249    cube->outputDetectionsKarma(karmafile);
250    karmafile.close();
251  }
252
[313]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
[3]263  delete cube;
264
[168]265  return SUCCESS;
[3]266}
267
Note: See TracBrowser for help on using the repository browser.