[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 | |
---|
[872] | 48 | try { |
---|
| 49 | |
---|
| 50 | |
---|
[232] | 51 | std::string paramFile,fitsfile; |
---|
[85] | 52 | Cube *cube = new Cube; |
---|
[3] | 53 | |
---|
[168] | 54 | if(cube->getopts(argc,argv)==FAILURE) return FAILURE; |
---|
[3] | 55 | |
---|
| 56 | if(cube->pars().getImageFile().empty()){ |
---|
[914] | 57 | DUCHAMPTHROW("Duchamp", "No input image has been given!. Use the imageFile parameter in the parameter file to specify the FITS file."); |
---|
[168] | 58 | return FAILURE; |
---|
[3] | 59 | } |
---|
| 60 | |
---|
[258] | 61 | if(cube->pars().getFlagSubsection() || cube->pars().getFlagStatSec()){ |
---|
[168] | 62 | // make sure the subsection is OK. |
---|
| 63 | if(cube->pars().verifySubsection() == FAILURE){ |
---|
[914] | 64 | DUCHAMPTHROW("Duchamp", "Unable to use the subsection provided."); |
---|
[168] | 65 | return FAILURE; |
---|
| 66 | } |
---|
| 67 | } |
---|
[164] | 68 | |
---|
[139] | 69 | std::cout << "Opening image: " |
---|
| 70 | << cube->pars().getFullImageFile() << std::endl; |
---|
| 71 | |
---|
[106] | 72 | if( cube->getCube() == FAILURE){ |
---|
[914] | 73 | DUCHAMPTHROW("Duchamp", "Unable to open image file "<< cube->pars().getFullImageFile()); |
---|
[168] | 74 | return FAILURE; |
---|
[3] | 75 | } |
---|
[103] | 76 | else std::cout << "Opened successfully." << std::endl; |
---|
[3] | 77 | |
---|
[208] | 78 | // Read in any saved arrays that are in FITS files on disk. |
---|
[475] | 79 | if(!cube->pars().getFlagUsePrevious()) |
---|
| 80 | cube->readSavedArrays(); |
---|
[71] | 81 | |
---|
[139] | 82 | // special case for 2D images -- ignore the minChannels requirement |
---|
| 83 | if(cube->getDimZ()==1) cube->pars().setMinChannels(0); |
---|
| 84 | |
---|
[103] | 85 | // Write the parameters to screen. |
---|
[3] | 86 | std::cout << cube->pars(); |
---|
| 87 | |
---|
[475] | 88 | if(cube->pars().getFlagUsePrevious()){ |
---|
| 89 | std::cout << "Reading detections from existing log file... \n"; |
---|
| 90 | if(cube->getExistingDetections() == FAILURE){ |
---|
[914] | 91 | DUCHAMPTHROW("Duchamp", "Could not read detections from log file"); |
---|
[475] | 92 | return FAILURE; |
---|
| 93 | } |
---|
| 94 | else std::cout << "Done.\n"; |
---|
[3] | 95 | } |
---|
[475] | 96 | else { |
---|
[3] | 97 | |
---|
[475] | 98 | if(cube->pars().getFlagLog()){ |
---|
| 99 | // Prepare the log file. |
---|
| 100 | cube->prepareLogFile(argc, argv); |
---|
| 101 | } |
---|
[3] | 102 | |
---|
[475] | 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 | } |
---|
[3] | 115 | |
---|
[475] | 116 | if(cube->pars().getFlagBaseline()){ |
---|
| 117 | std::cout<<"Removing the baselines... "<<std::flush; |
---|
| 118 | cube->removeBaseline(); |
---|
| 119 | std::cout<<" Done. \n"; |
---|
| 120 | } |
---|
[60] | 121 | |
---|
[475] | 122 | if(cube->pars().getFlagNegative()){ |
---|
| 123 | std::cout<<"Inverting the Cube... "<<std::flush; |
---|
| 124 | cube->invert(); |
---|
| 125 | std::cout<<" Done.\n"; |
---|
| 126 | } |
---|
[3] | 127 | |
---|
[834] | 128 | cube->Search(); |
---|
[475] | 129 | std::cout << "Done. Intermediate list has " << cube->getNumObj(); |
---|
| 130 | if(cube->getNumObj()==1) std::cout << " object.\n"; |
---|
| 131 | else std::cout << " objects.\n"; |
---|
[3] | 132 | |
---|
[715] | 133 | if(cube->getNumObj() > 0){ |
---|
[475] | 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; |
---|
[60] | 142 | |
---|
[475] | 143 | if(cube->pars().getFlagNegative()){ |
---|
| 144 | std::cout<<"Un-Inverting the Cube... "<<std::flush; |
---|
| 145 | cube->reInvert(); |
---|
| 146 | std::cout<<" Done."<<std::endl; |
---|
| 147 | } |
---|
[3] | 148 | |
---|
[475] | 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 | |
---|
[3] | 161 | } |
---|
| 162 | |
---|
[275] | 163 | cube->prepareOutputFile(); |
---|
| 164 | |
---|
[3] | 165 | if(cube->getNumObj()>0){ |
---|
| 166 | |
---|
[748] | 167 | std::cout << "Calculating final parameters and setting flags... "<<std::flush; |
---|
[3] | 168 | cube->calcObjectWCSparams(); |
---|
[87] | 169 | |
---|
| 170 | cube->setObjectFlags(); |
---|
[3] | 171 | |
---|
| 172 | cube->sortDetections(); |
---|
[475] | 173 | |
---|
[748] | 174 | std::cout << "Done." << std::endl; |
---|
| 175 | |
---|
[3] | 176 | } |
---|
[312] | 177 | |
---|
| 178 | cube->outputDetectionList(); |
---|
[3] | 179 | |
---|
[315] | 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"); |
---|
[258] | 189 | std::cout << "Done.\n"; |
---|
[315] | 190 | |
---|
[367] | 191 | if(cube->getNumObj()>0){ |
---|
[315] | 192 | std::cout << "Plotting the individual spectra... " << std::flush; |
---|
| 193 | cube->outputSpectra(); |
---|
| 194 | std::cout << "Done.\n"; |
---|
| 195 | } |
---|
[3] | 196 | } |
---|
[315] | 197 | else{ |
---|
| 198 | std::cout << "PGPLOT has not been enabled, so no graphical output.\n"; |
---|
| 199 | } |
---|
[3] | 200 | |
---|
[475] | 201 | if(!cube->pars().getFlagUsePrevious()){ |
---|
[3] | 202 | |
---|
[475] | 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; |
---|
[700] | 207 | if(cube->saveReconstructedCube() == FAILURE) |
---|
| 208 | std::cout << "Failed!\n"; |
---|
| 209 | else |
---|
| 210 | std::cout << "done.\n"; |
---|
[475] | 211 | } |
---|
| 212 | if(cube->pars().getFlagSmooth()&& cube->pars().getFlagOutputSmooth()){ |
---|
| 213 | std::cout << "Saving smoothed cube to " |
---|
| 214 | << cube->pars().outputSmoothFile() << "... " <<std::flush; |
---|
[700] | 215 | if(cube->saveSmoothedCube() == FAILURE) |
---|
| 216 | std::cout << "Failed!\n"; |
---|
| 217 | else |
---|
| 218 | std::cout << "done.\n"; |
---|
[475] | 219 | } |
---|
[670] | 220 | if(cube->pars().getFlagOutputMomentMap()){ |
---|
| 221 | std::cout << "Saving 0th moment map image to " |
---|
| 222 | << cube->pars().outputMomentMapFile() << "... " <<std::flush; |
---|
[700] | 223 | if(cube->saveMomentMapImage() == FAILURE) |
---|
| 224 | std::cout << "Failed!\n"; |
---|
| 225 | else |
---|
| 226 | std::cout << "done.\n"; |
---|
[670] | 227 | } |
---|
[475] | 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 | |
---|
[3] | 236 | } |
---|
| 237 | |
---|
[946] | 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 | |
---|
[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 | |
---|
[783] | 259 | if(cube->pars().getFlagTextSpectra()){ |
---|
[832] | 260 | if(cube->pars().isVerbose()) std::cout << "Saving spectra to text file ... "; |
---|
[783] | 261 | cube->writeSpectralData(); |
---|
[832] | 262 | if(cube->pars().isVerbose()) std::cout << "done.\n"; |
---|
[783] | 263 | } |
---|
| 264 | |
---|
[475] | 265 | if(!cube->pars().getFlagUsePrevious() && cube->pars().getFlagLog()){ |
---|
[313] | 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 | |
---|
[3] | 275 | delete cube; |
---|
| 276 | |
---|
[872] | 277 | } catch (const DuchampError &err) { |
---|
[873] | 278 | std::cout << "\nDuchamp failed with the following error:\n" << err.what() << "\n"; |
---|
[872] | 279 | exit(1); |
---|
| 280 | } |
---|
| 281 | |
---|
[168] | 282 | return SUCCESS; |
---|
[872] | 283 | |
---|
[3] | 284 | } |
---|
| 285 | |
---|