[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 | |
---|
[103] | 82 | // Write the parameters to screen. |
---|
[1049] | 83 | std::cout << cube->pars() << "\n"; |
---|
[3] | 84 | |
---|
[475] | 85 | if(cube->pars().getFlagUsePrevious()){ |
---|
[1157] | 86 | std::cout << "Reading detections from binary catalogue... \n"; |
---|
[1146] | 87 | if(cube->readBinaryCatalogue() == FAILURE){ |
---|
[914] | 88 | DUCHAMPTHROW("Duchamp", "Could not read detections from log file"); |
---|
[475] | 89 | return FAILURE; |
---|
| 90 | } |
---|
| 91 | else std::cout << "Done.\n"; |
---|
[3] | 92 | } |
---|
[475] | 93 | else { |
---|
[3] | 94 | |
---|
[475] | 95 | if(cube->pars().getFlagLog()){ |
---|
| 96 | // Prepare the log file. |
---|
| 97 | cube->prepareLogFile(argc, argv); |
---|
| 98 | } |
---|
[3] | 99 | |
---|
[475] | 100 | //if(cube->pars().getFlagBlankPix()){ |
---|
| 101 | if(cube->pars().getFlagTrim()){ |
---|
| 102 | // Trim any blank pixels from the edges, and report the new size |
---|
| 103 | // of the cube |
---|
| 104 | std::cout<<"Trimming the Blank Pixels from the edges... "<<std::flush; |
---|
| 105 | cube->trimCube(); |
---|
| 106 | std::cout<<"Done."<<std::endl; |
---|
| 107 | std::cout << "New dimensions: " << cube->getDimX(); |
---|
| 108 | if(cube->getNumDim()>1) std::cout << "x" << cube->getDimY(); |
---|
| 109 | if(cube->getNumDim()>2) std::cout << "x" << cube->getDimZ(); |
---|
| 110 | std::cout << std::endl; |
---|
| 111 | } |
---|
[3] | 112 | |
---|
[475] | 113 | if(cube->pars().getFlagBaseline()){ |
---|
| 114 | std::cout<<"Removing the baselines... "<<std::flush; |
---|
| 115 | cube->removeBaseline(); |
---|
| 116 | std::cout<<" Done. \n"; |
---|
| 117 | } |
---|
[1361] | 118 | |
---|
[475] | 119 | if(cube->pars().getFlagNegative()){ |
---|
| 120 | std::cout<<"Inverting the Cube... "<<std::flush; |
---|
| 121 | cube->invert(); |
---|
| 122 | std::cout<<" Done.\n"; |
---|
| 123 | } |
---|
[3] | 124 | |
---|
[834] | 125 | cube->Search(); |
---|
[475] | 126 | std::cout << "Done. Intermediate list has " << cube->getNumObj(); |
---|
| 127 | if(cube->getNumObj()==1) std::cout << " object.\n"; |
---|
| 128 | else std::cout << " objects.\n"; |
---|
[3] | 129 | |
---|
[715] | 130 | if(cube->getNumObj() > 0){ |
---|
[475] | 131 | if(cube->pars().getFlagGrowth()) |
---|
| 132 | std::cout<<"Merging, Growing and Rejecting... "<<std::flush; |
---|
| 133 | else |
---|
| 134 | std::cout<<"Merging and Rejecting... "<<std::flush; |
---|
| 135 | cube->ObjectMerger(); |
---|
| 136 | std::cout<<"Done. "<<std::endl; |
---|
| 137 | } |
---|
| 138 | std::cout<<"Final object count = "<<cube->getNumObj()<<std::endl; |
---|
[60] | 139 | |
---|
[1361] | 140 | |
---|
[475] | 141 | if(cube->pars().getFlagNegative()){ |
---|
| 142 | std::cout<<"Un-Inverting the Cube... "<<std::flush; |
---|
[1361] | 143 | cube->invert(!cube->pars().getFlagBaseline(),true); |
---|
[475] | 144 | std::cout<<" Done."<<std::endl; |
---|
| 145 | } |
---|
[3] | 146 | |
---|
[1361] | 147 | if(cube->pars().getFlagBaseline() && !cube->pars().getFlagNegative()){ |
---|
[475] | 148 | std::cout<<"Replacing the baselines... "<<std::flush; |
---|
| 149 | cube->replaceBaseline(); |
---|
| 150 | std::cout<<"Done."<<std::endl; |
---|
| 151 | } |
---|
[1361] | 152 | |
---|
[475] | 153 | if(cube->pars().getFlagCubeTrimmed()){ |
---|
| 154 | std::cout<<"Replacing the trimmed pixels on the edges... "<<std::flush; |
---|
| 155 | cube->unTrimCube(); |
---|
| 156 | std::cout<<"Done."<<std::endl; |
---|
| 157 | } |
---|
[1361] | 158 | |
---|
[3] | 159 | } |
---|
| 160 | |
---|
| 161 | if(cube->getNumObj()>0){ |
---|
| 162 | |
---|
[748] | 163 | std::cout << "Calculating final parameters and setting flags... "<<std::flush; |
---|
[3] | 164 | cube->calcObjectWCSparams(); |
---|
[87] | 165 | |
---|
| 166 | cube->setObjectFlags(); |
---|
[3] | 167 | |
---|
| 168 | cube->sortDetections(); |
---|
[475] | 169 | |
---|
[748] | 170 | std::cout << "Done." << std::endl; |
---|
| 171 | |
---|
[3] | 172 | } |
---|
[312] | 173 | |
---|
[1361] | 174 | if(cube->pars().getFlagNegative() && cube->pars().getFlagBaseline()){ |
---|
| 175 | std::cout<<"Un-Inverting the Cube... "<<std::flush; |
---|
| 176 | cube->invert(true,true); |
---|
| 177 | std::cout<<" Done."<<std::endl; |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | if(cube->pars().getFlagBaseline() && cube->pars().getFlagNegative()){ |
---|
| 181 | std::cout<<"Replacing the baselines... "<<std::flush; |
---|
| 182 | cube->replaceBaseline(false); |
---|
| 183 | std::cout<<"Done."<<std::endl; |
---|
| 184 | } |
---|
| 185 | |
---|
[1049] | 186 | cube->outputCatalogue(); |
---|
[3] | 187 | |
---|
[315] | 188 | if(USE_PGPLOT){ |
---|
| 189 | std::cout<<"Creating the maps... "<<std::flush; |
---|
| 190 | std::vector<std::string> devices; |
---|
[985] | 191 | if(cube->nondegDim()>1){ |
---|
| 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 | } |
---|
| 197 | if(cube->pars().getFlagMaps()){ |
---|
| 198 | if(cube->nondegDim()==1) cube->plotDetectionMap("/xs"); |
---|
[315] | 199 | cube->plotDetectionMap(cube->pars().getDetectionMap()+"/vcps"); |
---|
[985] | 200 | } |
---|
[258] | 201 | std::cout << "Done.\n"; |
---|
[315] | 202 | |
---|
[994] | 203 | if(cube->getNumObj()>0 && cube->pars().getFlagPlotSpectra()){ |
---|
[1148] | 204 | std::cout << "Plotting the spectra of detected objects... " << std::flush; |
---|
[315] | 205 | cube->outputSpectra(); |
---|
| 206 | std::cout << "Done.\n"; |
---|
| 207 | } |
---|
[3] | 208 | } |
---|
[315] | 209 | else{ |
---|
| 210 | std::cout << "PGPLOT has not been enabled, so no graphical output.\n"; |
---|
| 211 | } |
---|
[3] | 212 | |
---|
[475] | 213 | if(!cube->pars().getFlagUsePrevious()){ |
---|
[1146] | 214 | |
---|
| 215 | if(cube->pars().getFlagWriteBinaryCatalogue() && (cube->getNumObj()>0)){ |
---|
| 216 | cube->writeBinaryCatalogue(); |
---|
[475] | 217 | } |
---|
| 218 | |
---|
[3] | 219 | } |
---|
| 220 | |
---|
[1142] | 221 | std::cout << "Writing to FITS files... \n"; |
---|
[1121] | 222 | cube->writeToFITS(); |
---|
[1142] | 223 | std::cout << "Done.\n"; |
---|
[946] | 224 | |
---|
[349] | 225 | if(cube->pars().getFlagVOT()){ |
---|
[1048] | 226 | cube->outputDetectionsVOTable(); |
---|
[3] | 227 | } |
---|
| 228 | |
---|
[1077] | 229 | // write annotation files, if required. |
---|
| 230 | cube->outputAnnotations(); |
---|
[20] | 231 | |
---|
[783] | 232 | if(cube->pars().getFlagTextSpectra()){ |
---|
[832] | 233 | if(cube->pars().isVerbose()) std::cout << "Saving spectra to text file ... "; |
---|
[783] | 234 | cube->writeSpectralData(); |
---|
[832] | 235 | if(cube->pars().isVerbose()) std::cout << "done.\n"; |
---|
[783] | 236 | } |
---|
| 237 | |
---|
[475] | 238 | if(!cube->pars().getFlagUsePrevious() && cube->pars().getFlagLog()){ |
---|
[313] | 239 | // Open the logfile and write the time on the first line |
---|
| 240 | std::ofstream logfile(cube->pars().getLogFile().c_str(),std::ios::app); |
---|
[1147] | 241 | logfile << "# Duchamp completed: "; |
---|
[313] | 242 | time_t now = time(NULL); |
---|
| 243 | logfile << asctime( localtime(&now) ); |
---|
| 244 | logfile.close(); |
---|
| 245 | } |
---|
| 246 | |
---|
| 247 | |
---|
[3] | 248 | delete cube; |
---|
| 249 | |
---|
[872] | 250 | } catch (const DuchampError &err) { |
---|
[873] | 251 | std::cout << "\nDuchamp failed with the following error:\n" << err.what() << "\n"; |
---|
[872] | 252 | exit(1); |
---|
| 253 | } |
---|
| 254 | |
---|
[168] | 255 | return SUCCESS; |
---|
[872] | 256 | |
---|
[3] | 257 | } |
---|
| 258 | |
---|