[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] | 43 | using namespace duchamp; |
---|
| 44 | |
---|
[3] | 45 | int 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 | |
---|
[731] | 135 | cube->Search(true); |
---|
[475] | 136 | std::cout << "Done. Intermediate list has " << cube->getNumObj(); |
---|
| 137 | if(cube->getNumObj()==1) std::cout << " object.\n"; |
---|
| 138 | else std::cout << " objects.\n"; |
---|
[3] | 139 | |
---|
[715] | 140 | if(cube->getNumObj() > 0){ |
---|
[475] | 141 | if(cube->pars().getFlagGrowth()) |
---|
| 142 | std::cout<<"Merging, Growing and Rejecting... "<<std::flush; |
---|
| 143 | else |
---|
| 144 | std::cout<<"Merging and Rejecting... "<<std::flush; |
---|
| 145 | cube->ObjectMerger(); |
---|
| 146 | std::cout<<"Done. "<<std::endl; |
---|
| 147 | } |
---|
| 148 | std::cout<<"Final object count = "<<cube->getNumObj()<<std::endl; |
---|
[60] | 149 | |
---|
[475] | 150 | if(cube->pars().getFlagNegative()){ |
---|
| 151 | std::cout<<"Un-Inverting the Cube... "<<std::flush; |
---|
| 152 | cube->reInvert(); |
---|
| 153 | std::cout<<" Done."<<std::endl; |
---|
| 154 | } |
---|
[3] | 155 | |
---|
[475] | 156 | if(cube->pars().getFlagBaseline()){ |
---|
| 157 | std::cout<<"Replacing the baselines... "<<std::flush; |
---|
| 158 | cube->replaceBaseline(); |
---|
| 159 | std::cout<<"Done."<<std::endl; |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | if(cube->pars().getFlagCubeTrimmed()){ |
---|
| 163 | std::cout<<"Replacing the trimmed pixels on the edges... "<<std::flush; |
---|
| 164 | cube->unTrimCube(); |
---|
| 165 | std::cout<<"Done."<<std::endl; |
---|
| 166 | } |
---|
| 167 | |
---|
[3] | 168 | } |
---|
| 169 | |
---|
[275] | 170 | cube->prepareOutputFile(); |
---|
| 171 | |
---|
[3] | 172 | if(cube->getNumObj()>0){ |
---|
| 173 | |
---|
[748] | 174 | std::cout << "Calculating final parameters and setting flags... "<<std::flush; |
---|
[3] | 175 | cube->calcObjectWCSparams(); |
---|
[87] | 176 | |
---|
| 177 | cube->setObjectFlags(); |
---|
[3] | 178 | |
---|
| 179 | cube->sortDetections(); |
---|
[475] | 180 | |
---|
[748] | 181 | std::cout << "Done." << std::endl; |
---|
| 182 | |
---|
[3] | 183 | } |
---|
[312] | 184 | |
---|
| 185 | cube->outputDetectionList(); |
---|
[3] | 186 | |
---|
[315] | 187 | if(USE_PGPLOT){ |
---|
| 188 | std::cout<<"Creating the maps... "<<std::flush; |
---|
| 189 | std::vector<std::string> devices; |
---|
| 190 | if(cube->pars().getFlagXOutput()) devices.push_back("/xs"); |
---|
| 191 | if(cube->pars().getFlagMaps()) |
---|
| 192 | devices.push_back(cube->pars().getMomentMap()+"/vcps"); |
---|
| 193 | cube->plotMomentMap(devices); |
---|
| 194 | if(cube->pars().getFlagMaps()) |
---|
| 195 | cube->plotDetectionMap(cube->pars().getDetectionMap()+"/vcps"); |
---|
[258] | 196 | std::cout << "Done.\n"; |
---|
[315] | 197 | |
---|
[367] | 198 | if(cube->getNumObj()>0){ |
---|
[315] | 199 | std::cout << "Plotting the individual spectra... " << std::flush; |
---|
| 200 | cube->outputSpectra(); |
---|
| 201 | std::cout << "Done.\n"; |
---|
| 202 | } |
---|
[3] | 203 | } |
---|
[315] | 204 | else{ |
---|
| 205 | std::cout << "PGPLOT has not been enabled, so no graphical output.\n"; |
---|
| 206 | } |
---|
[3] | 207 | |
---|
[475] | 208 | if(!cube->pars().getFlagUsePrevious()){ |
---|
[3] | 209 | |
---|
[475] | 210 | if(cube->pars().getFlagATrous()&& |
---|
| 211 | (cube->pars().getFlagOutputRecon()||cube->pars().getFlagOutputResid()) ){ |
---|
| 212 | std::cout << "Saving reconstructed cube to " |
---|
| 213 | << cube->pars().outputReconFile() << "... "<<std::flush; |
---|
[700] | 214 | if(cube->saveReconstructedCube() == FAILURE) |
---|
| 215 | std::cout << "Failed!\n"; |
---|
| 216 | else |
---|
| 217 | std::cout << "done.\n"; |
---|
[475] | 218 | } |
---|
| 219 | if(cube->pars().getFlagSmooth()&& cube->pars().getFlagOutputSmooth()){ |
---|
| 220 | std::cout << "Saving smoothed cube to " |
---|
| 221 | << cube->pars().outputSmoothFile() << "... " <<std::flush; |
---|
[700] | 222 | if(cube->saveSmoothedCube() == FAILURE) |
---|
| 223 | std::cout << "Failed!\n"; |
---|
| 224 | else |
---|
| 225 | std::cout << "done.\n"; |
---|
[475] | 226 | } |
---|
| 227 | if(cube->pars().getFlagOutputMask()){ |
---|
| 228 | std::cout << "Saving mask cube to " |
---|
| 229 | << cube->pars().outputMaskFile() << "... " <<std::flush; |
---|
[700] | 230 | if(cube->saveMaskCube() == FAILURE) |
---|
| 231 | std::cout << "Failed!\n"; |
---|
| 232 | else |
---|
| 233 | std::cout << "done.\n"; |
---|
[475] | 234 | } |
---|
[670] | 235 | if(cube->pars().getFlagOutputMomentMap()){ |
---|
| 236 | std::cout << "Saving 0th moment map image to " |
---|
| 237 | << cube->pars().outputMomentMapFile() << "... " <<std::flush; |
---|
[700] | 238 | if(cube->saveMomentMapImage() == FAILURE) |
---|
| 239 | std::cout << "Failed!\n"; |
---|
| 240 | else |
---|
| 241 | std::cout << "done.\n"; |
---|
[670] | 242 | } |
---|
[475] | 243 | |
---|
| 244 | if(cube->pars().getFlagLog() && (cube->getNumObj()>0)){ |
---|
| 245 | std::ofstream logfile(cube->pars().getLogFile().c_str(),std::ios::app); |
---|
| 246 | logfile << "=-=-=-=-=-=-=-\nCube summary\n=-=-=-=-=-=-=-\n"; |
---|
| 247 | logfile << *cube; |
---|
| 248 | logfile.close(); |
---|
| 249 | } |
---|
| 250 | |
---|
[3] | 251 | } |
---|
| 252 | |
---|
[349] | 253 | if(cube->pars().getFlagVOT()){ |
---|
[232] | 254 | std::ofstream votfile(cube->pars().getVOTFile().c_str()); |
---|
[3] | 255 | cube->outputDetectionsVOTable(votfile); |
---|
| 256 | votfile.close(); |
---|
| 257 | } |
---|
| 258 | |
---|
[349] | 259 | if(cube->pars().getFlagKarma()){ |
---|
[232] | 260 | std::ofstream karmafile(cube->pars().getKarmaFile().c_str()); |
---|
[20] | 261 | cube->outputDetectionsKarma(karmafile); |
---|
| 262 | karmafile.close(); |
---|
| 263 | } |
---|
| 264 | |
---|
[783] | 265 | if(cube->pars().getFlagTextSpectra()){ |
---|
[832] | 266 | if(cube->pars().isVerbose()) std::cout << "Saving spectra to text file ... "; |
---|
[783] | 267 | cube->writeSpectralData(); |
---|
[832] | 268 | if(cube->pars().isVerbose()) std::cout << "done.\n"; |
---|
[783] | 269 | } |
---|
| 270 | |
---|
[475] | 271 | if(!cube->pars().getFlagUsePrevious() && cube->pars().getFlagLog()){ |
---|
[313] | 272 | // Open the logfile and write the time on the first line |
---|
| 273 | std::ofstream logfile(cube->pars().getLogFile().c_str(),std::ios::app); |
---|
| 274 | logfile << "Duchamp completed: "; |
---|
| 275 | time_t now = time(NULL); |
---|
| 276 | logfile << asctime( localtime(&now) ); |
---|
| 277 | logfile.close(); |
---|
| 278 | } |
---|
| 279 | |
---|
| 280 | |
---|
[3] | 281 | delete cube; |
---|
| 282 | |
---|
[168] | 283 | return SUCCESS; |
---|
[3] | 284 | } |
---|
| 285 | |
---|