[3] | 1 | #include <iostream> |
---|
| 2 | #include <fstream> |
---|
| 3 | #include <string> |
---|
| 4 | #include <cpgplot.h> |
---|
| 5 | #include <math.h> |
---|
| 6 | #include <unistd.h> |
---|
[80] | 7 | #include <time.h> |
---|
[57] | 8 | |
---|
| 9 | #include <duchamp.hh> |
---|
[142] | 10 | #include <param.hh> |
---|
[3] | 11 | #include <Detection/detection.hh> |
---|
| 12 | #include <Cubes/cubes.hh> |
---|
| 13 | #include <Utils/utils.hh> |
---|
| 14 | #include <ATrous/atrous.hh> |
---|
| 15 | |
---|
| 16 | using std::ofstream; |
---|
[103] | 17 | using std::string; |
---|
[3] | 18 | |
---|
| 19 | Filter reconFilter; |
---|
| 20 | |
---|
| 21 | int main(int argc, char * argv[]) |
---|
| 22 | { |
---|
| 23 | |
---|
[85] | 24 | string paramFile,fitsfile; |
---|
| 25 | Cube *cube = new Cube; |
---|
[3] | 26 | |
---|
[168] | 27 | if(cube->getopts(argc,argv)==FAILURE) return FAILURE; |
---|
[3] | 28 | |
---|
| 29 | if(cube->pars().getImageFile().empty()){ |
---|
[139] | 30 | std::stringstream errmsg; |
---|
| 31 | errmsg << "No input image has been given!\n" |
---|
| 32 | << "Use the imageFile parameter in " |
---|
| 33 | << paramFile << " to specify the FITS file.\nExiting...\n"; |
---|
[168] | 34 | duchampError("Duchamp", errmsg.str()); |
---|
| 35 | return FAILURE; |
---|
[3] | 36 | } |
---|
| 37 | |
---|
[168] | 38 | if(cube->pars().getFlagSubsection()){ |
---|
| 39 | // make sure the subsection is OK. |
---|
| 40 | if(cube->pars().verifySubsection() == FAILURE){ |
---|
| 41 | duchampError("Duchamp", |
---|
| 42 | "Unable to use the subsection provided.\nExiting...\n"); |
---|
| 43 | return FAILURE; |
---|
| 44 | } |
---|
| 45 | } |
---|
[164] | 46 | |
---|
[139] | 47 | std::cout << "Opening image: " |
---|
| 48 | << cube->pars().getFullImageFile() << std::endl; |
---|
| 49 | |
---|
[106] | 50 | if( cube->getCube() == FAILURE){ |
---|
[139] | 51 | std::stringstream errmsg; |
---|
| 52 | errmsg << "Unable to open image file " |
---|
| 53 | << cube->pars().getFullImageFile() |
---|
| 54 | << "\nExiting...\n"; |
---|
[168] | 55 | duchampError("Duchamp", errmsg.str()); |
---|
| 56 | return FAILURE; |
---|
[3] | 57 | } |
---|
[103] | 58 | else std::cout << "Opened successfully." << std::endl; |
---|
[3] | 59 | |
---|
[208] | 60 | // Read in any saved arrays that are in FITS files on disk. |
---|
| 61 | cube->readSavedArrays(); |
---|
[71] | 62 | |
---|
[103] | 63 | // Filter needed for baseline removal and a trous reconstruction |
---|
| 64 | if(cube->pars().getFlagATrous() || cube->pars().getFlagBaseline()){ |
---|
| 65 | reconFilter.define(cube->pars().getFilterCode()); |
---|
| 66 | cube->pars().setFilterName(reconFilter.getName()); |
---|
| 67 | } |
---|
| 68 | |
---|
[139] | 69 | // special case for 2D images -- ignore the minChannels requirement |
---|
| 70 | if(cube->getDimZ()==1) cube->pars().setMinChannels(0); |
---|
| 71 | |
---|
[103] | 72 | // Write the parameters to screen. |
---|
[3] | 73 | std::cout << cube->pars(); |
---|
| 74 | |
---|
| 75 | if(cube->pars().getFlagLog()){ |
---|
[106] | 76 | // Open the logfile and write the time on the first line |
---|
[3] | 77 | ofstream logfile(cube->pars().getLogFile().c_str()); |
---|
| 78 | logfile << "New run of the Duchamp sourcefinder: "; |
---|
[80] | 79 | time_t now = time(NULL); |
---|
| 80 | logfile << asctime( localtime(&now) ); |
---|
[120] | 81 | // Write out the command-line statement |
---|
| 82 | logfile << "Executing statement : "; |
---|
| 83 | for(int i=0;i<argc;i++) logfile << argv[i] << " "; |
---|
| 84 | logfile << std::endl; |
---|
[3] | 85 | logfile << cube->pars(); |
---|
| 86 | logfile.close(); |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | if(cube->pars().getFlagBlankPix()){ |
---|
[139] | 90 | // Trim any blank pixels from the edges, |
---|
| 91 | // and report the new size of the cube |
---|
[3] | 92 | std::cout<<"Trimming the Blank Pixels from the edges... "<<std::flush; |
---|
| 93 | cube->trimCube(); |
---|
[103] | 94 | std::cout<<"Done."<<std::endl; |
---|
[3] | 95 | std::cout << "New dimensions: " << cube->getDimX(); |
---|
| 96 | if(cube->getNumDim()>1) std::cout << "x" << cube->getDimY(); |
---|
| 97 | if(cube->getNumDim()>2) std::cout << "x" << cube->getDimZ(); |
---|
[103] | 98 | std::cout << std::endl; |
---|
[3] | 99 | } |
---|
| 100 | |
---|
| 101 | if(cube->pars().getFlagBaseline()){ |
---|
| 102 | std::cout<<"Removing the baselines... "<<std::flush; |
---|
| 103 | cube->removeBaseline(); |
---|
| 104 | std::cout<<" Done. "<<std::endl; |
---|
| 105 | } |
---|
| 106 | |
---|
[60] | 107 | if(cube->pars().getFlagNegative()){ |
---|
| 108 | std::cout<<"Inverting the Cube... "<<std::flush; |
---|
| 109 | cube->invert(); |
---|
| 110 | std::cout<<" Done."<<std::endl; |
---|
| 111 | } |
---|
| 112 | |
---|
[208] | 113 | if(cube->pars().getFlagATrous()){ |
---|
| 114 | std::cout<<"Commencing search in reconstructed cube..."<<std::endl; |
---|
| 115 | cube->ReconSearch(); |
---|
| 116 | } |
---|
| 117 | else if(cube->pars().getFlagSmooth()){ |
---|
[201] | 118 | std::cout<<"Commencing search in hanning smoothed cube..."<<std::endl; |
---|
| 119 | cube->SmoothSearch(); |
---|
| 120 | } |
---|
[3] | 121 | else{ |
---|
[103] | 122 | std::cout<<"Commencing search in cube..."<<std::endl; |
---|
| 123 | cube->CubicSearch(); |
---|
[3] | 124 | } |
---|
[201] | 125 | std::cout << "Done. Intermediate list has " |
---|
| 126 | << cube->getNumObj(); |
---|
| 127 | if(cube->getNumObj()==1) std::cout << " object.\n"; |
---|
| 128 | else std::cout << " objects.\n"; |
---|
[3] | 129 | |
---|
[146] | 130 | if(cube->getNumObj() > 1){ |
---|
| 131 | std::cout<<"Merging lists... "<<std::flush; |
---|
| 132 | cube->ObjectMerger(); |
---|
| 133 | std::cout<<"Done. "<<std::endl; |
---|
| 134 | } |
---|
[103] | 135 | std::cout<<"Final object count = "<<cube->getNumObj()<<std::endl; |
---|
[3] | 136 | |
---|
[60] | 137 | if(cube->pars().getFlagNegative()){ |
---|
| 138 | std::cout<<"Un-Inverting the Cube... "<<std::flush; |
---|
| 139 | cube->reInvert(); |
---|
| 140 | std::cout<<" Done."<<std::endl; |
---|
| 141 | } |
---|
| 142 | |
---|
[3] | 143 | cube->replaceBaseline(); |
---|
| 144 | |
---|
| 145 | if(cube->pars().getFlagCubeTrimmed()){ |
---|
| 146 | std::cout<<"Replacing the trimmed pixels on the edges... "<<std::flush; |
---|
| 147 | cube->unTrimCube(); |
---|
[103] | 148 | std::cout<<"Done."<<std::endl; |
---|
[3] | 149 | } |
---|
| 150 | |
---|
| 151 | if(cube->getNumObj()>0){ |
---|
| 152 | |
---|
| 153 | cube->calcObjectWCSparams(); |
---|
[87] | 154 | |
---|
| 155 | cube->setObjectFlags(); |
---|
[3] | 156 | |
---|
| 157 | cube->sortDetections(); |
---|
| 158 | |
---|
| 159 | cube->outputDetectionList(); |
---|
| 160 | } |
---|
| 161 | |
---|
[87] | 162 | std::cout<<"Creating the maps... "<<std::flush; |
---|
[203] | 163 | // if(cube->pars().getFlagXOutput()) cube->plotMomentMap("/xs"); |
---|
| 164 | // if(cube->pars().getFlagMaps()){ |
---|
| 165 | // cube->plotMomentMap(cube->pars().getMomentMap()+"/vcps"); |
---|
| 166 | // cube->plotDetectionMap(cube->pars().getDetectionMap()+"/vcps"); |
---|
| 167 | // } |
---|
| 168 | vector<string> devices; |
---|
| 169 | if(cube->pars().getFlagXOutput()) devices.push_back("/xs"); |
---|
| 170 | if(cube->pars().getFlagMaps()) |
---|
| 171 | devices.push_back(cube->pars().getMomentMap()+"/vcps"); |
---|
| 172 | cube->plotMomentMap(devices); |
---|
| 173 | if(cube->pars().getFlagMaps()) |
---|
[3] | 174 | cube->plotDetectionMap(cube->pars().getDetectionMap()+"/vcps"); |
---|
[87] | 175 | std::cout << "done.\n"; |
---|
[3] | 176 | |
---|
[49] | 177 | if((cube->getDimZ()>1) && (cube->getNumObj()>0)){ |
---|
[3] | 178 | std::cout << "Plotting the individual spectra... " << std::flush; |
---|
| 179 | cube->outputSpectra(); |
---|
| 180 | std::cout << "done.\n"; |
---|
| 181 | } |
---|
[120] | 182 | else if(cube->getDimZ()<=1) |
---|
[168] | 183 | duchampWarning("Duchamp", |
---|
| 184 | "Not plotting any spectra -- no third dimension.\n"); |
---|
[3] | 185 | |
---|
| 186 | cpgend(); |
---|
| 187 | |
---|
| 188 | if(cube->pars().getFlagATrous()&& |
---|
| 189 | (cube->pars().getFlagOutputRecon()||cube->pars().getFlagOutputResid()) ){ |
---|
[208] | 190 | std::cout << "Saving reconstructed cube" |
---|
| 191 | << cube->pars().outputReconFile() << "... "<<std::flush; |
---|
[3] | 192 | cube->saveReconstructedCube(); |
---|
| 193 | std::cout << "done.\n"; |
---|
| 194 | } |
---|
[208] | 195 | if(cube->pars().getFlagSmooth()&& cube->pars().getFlagOutputSmooth()){ |
---|
| 196 | std::cout << "Saving Hanning-smoothed cube to" |
---|
| 197 | << cube->pars().outputSmoothFile() << "... " <<std::flush; |
---|
| 198 | cube->saveSmoothedCube(); |
---|
| 199 | std::cout << "done.\n"; |
---|
| 200 | } |
---|
[3] | 201 | |
---|
| 202 | if(cube->pars().getFlagLog() && (cube->getNumObj()>0)){ |
---|
| 203 | ofstream logfile(cube->pars().getLogFile().c_str(),std::ios::app); |
---|
| 204 | logfile << "=-=-=-=-=-=-=-\nCube summary\n=-=-=-=-=-=-=-\n"; |
---|
| 205 | logfile << *cube; |
---|
| 206 | logfile.close(); |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | if(cube->pars().getFlagVOT()){ |
---|
| 210 | ofstream votfile(cube->pars().getVOTFile().c_str()); |
---|
| 211 | cube->outputDetectionsVOTable(votfile); |
---|
| 212 | votfile.close(); |
---|
| 213 | } |
---|
| 214 | |
---|
[20] | 215 | if(cube->pars().getFlagKarma()){ |
---|
| 216 | ofstream karmafile(cube->pars().getKarmaFile().c_str()); |
---|
| 217 | cube->outputDetectionsKarma(karmafile); |
---|
| 218 | karmafile.close(); |
---|
| 219 | } |
---|
| 220 | |
---|
[3] | 221 | delete cube; |
---|
| 222 | |
---|
[168] | 223 | return SUCCESS; |
---|
[3] | 224 | } |
---|
| 225 | |
---|