[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 | |
---|
[71] | 60 | // If the reconstructed array is to be read in from disk |
---|
[76] | 61 | if( cube->pars().getFlagReconExists() && cube->pars().getFlagATrous() ){ |
---|
[103] | 62 | std::cout << "Reading reconstructed array: "<<std::endl; |
---|
[71] | 63 | if( cube->readReconCube() == FAILURE){ |
---|
[120] | 64 | std::stringstream errmsg; |
---|
| 65 | errmsg <<"Could not read in existing reconstructed array.\n" |
---|
| 66 | <<"Will perform reconstruction using assigned parameters.\n"; |
---|
[168] | 67 | duchampWarning("Duchamp", errmsg.str()); |
---|
[71] | 68 | cube->pars().setFlagReconExists(false); |
---|
| 69 | } |
---|
[103] | 70 | else std::cout << "Reconstructed array available.\n"; |
---|
[71] | 71 | } |
---|
| 72 | |
---|
[103] | 73 | // Filter needed for baseline removal and a trous reconstruction |
---|
| 74 | if(cube->pars().getFlagATrous() || cube->pars().getFlagBaseline()){ |
---|
| 75 | reconFilter.define(cube->pars().getFilterCode()); |
---|
| 76 | cube->pars().setFilterName(reconFilter.getName()); |
---|
| 77 | } |
---|
| 78 | |
---|
[139] | 79 | // special case for 2D images -- ignore the minChannels requirement |
---|
| 80 | if(cube->getDimZ()==1) cube->pars().setMinChannels(0); |
---|
| 81 | |
---|
[103] | 82 | // Write the parameters to screen. |
---|
[3] | 83 | std::cout << cube->pars(); |
---|
| 84 | |
---|
| 85 | if(cube->pars().getFlagLog()){ |
---|
[106] | 86 | // Open the logfile and write the time on the first line |
---|
[3] | 87 | ofstream logfile(cube->pars().getLogFile().c_str()); |
---|
| 88 | logfile << "New run of the Duchamp sourcefinder: "; |
---|
[80] | 89 | time_t now = time(NULL); |
---|
| 90 | logfile << asctime( localtime(&now) ); |
---|
[120] | 91 | // Write out the command-line statement |
---|
| 92 | logfile << "Executing statement : "; |
---|
| 93 | for(int i=0;i<argc;i++) logfile << argv[i] << " "; |
---|
| 94 | logfile << std::endl; |
---|
[3] | 95 | logfile << cube->pars(); |
---|
| 96 | logfile.close(); |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | if(cube->pars().getFlagBlankPix()){ |
---|
[139] | 100 | // Trim any blank pixels from the edges, |
---|
| 101 | // and report the new size of the cube |
---|
[3] | 102 | std::cout<<"Trimming the Blank Pixels from the edges... "<<std::flush; |
---|
| 103 | cube->trimCube(); |
---|
[103] | 104 | std::cout<<"Done."<<std::endl; |
---|
[3] | 105 | std::cout << "New dimensions: " << cube->getDimX(); |
---|
| 106 | if(cube->getNumDim()>1) std::cout << "x" << cube->getDimY(); |
---|
| 107 | if(cube->getNumDim()>2) std::cout << "x" << cube->getDimZ(); |
---|
[103] | 108 | std::cout << std::endl; |
---|
[3] | 109 | } |
---|
| 110 | |
---|
| 111 | if(cube->pars().getFlagBaseline()){ |
---|
| 112 | std::cout<<"Removing the baselines... "<<std::flush; |
---|
| 113 | cube->removeBaseline(); |
---|
| 114 | std::cout<<" Done. "<<std::endl; |
---|
| 115 | } |
---|
| 116 | |
---|
[60] | 117 | if(cube->pars().getFlagNegative()){ |
---|
| 118 | std::cout<<"Inverting the Cube... "<<std::flush; |
---|
| 119 | cube->invert(); |
---|
| 120 | std::cout<<" Done."<<std::endl; |
---|
| 121 | } |
---|
| 122 | |
---|
[76] | 123 | if(cube->pars().getFlagATrous()){ |
---|
[103] | 124 | std::cout<<"Commencing search in reconstructed cube..."<<std::endl; |
---|
| 125 | cube->ReconSearch(); |
---|
[146] | 126 | std::cout << "Done. Intermediate list has " |
---|
| 127 | << cube->getNumObj(); |
---|
| 128 | if(cube->getNumObj()==1) std::cout << " object.\n"; |
---|
| 129 | else std::cout << " objects.\n"; |
---|
[3] | 130 | } |
---|
| 131 | else{ |
---|
[103] | 132 | std::cout<<"Commencing search in cube..."<<std::endl; |
---|
| 133 | cube->CubicSearch(); |
---|
[146] | 134 | std::cout << "Done. Intermediate list has " |
---|
| 135 | << cube->getNumObj(); |
---|
| 136 | if(cube->getNumObj()==1) std::cout << " object.\n"; |
---|
| 137 | else std::cout << " objects.\n"; |
---|
[3] | 138 | } |
---|
| 139 | |
---|
[146] | 140 | if(cube->getNumObj() > 1){ |
---|
| 141 | std::cout<<"Merging lists... "<<std::flush; |
---|
| 142 | cube->ObjectMerger(); |
---|
| 143 | std::cout<<"Done. "<<std::endl; |
---|
| 144 | } |
---|
[103] | 145 | std::cout<<"Final object count = "<<cube->getNumObj()<<std::endl; |
---|
[3] | 146 | |
---|
[60] | 147 | if(cube->pars().getFlagNegative()){ |
---|
| 148 | std::cout<<"Un-Inverting the Cube... "<<std::flush; |
---|
| 149 | cube->reInvert(); |
---|
| 150 | std::cout<<" Done."<<std::endl; |
---|
| 151 | } |
---|
| 152 | |
---|
[3] | 153 | cube->replaceBaseline(); |
---|
| 154 | |
---|
| 155 | if(cube->pars().getFlagCubeTrimmed()){ |
---|
| 156 | std::cout<<"Replacing the trimmed pixels on the edges... "<<std::flush; |
---|
| 157 | cube->unTrimCube(); |
---|
[103] | 158 | std::cout<<"Done."<<std::endl; |
---|
[3] | 159 | } |
---|
| 160 | |
---|
| 161 | if(cube->getNumObj()>0){ |
---|
| 162 | |
---|
| 163 | cube->calcObjectWCSparams(); |
---|
[87] | 164 | |
---|
| 165 | cube->setObjectFlags(); |
---|
[3] | 166 | |
---|
| 167 | cube->sortDetections(); |
---|
| 168 | |
---|
| 169 | cube->outputDetectionList(); |
---|
| 170 | } |
---|
| 171 | |
---|
[87] | 172 | std::cout<<"Creating the maps... "<<std::flush; |
---|
| 173 | cube->plotMomentMap("/xs"); |
---|
[3] | 174 | if(cube->pars().getFlagMaps()){ |
---|
| 175 | cube->plotMomentMap(cube->pars().getMomentMap()+"/vcps"); |
---|
| 176 | cube->plotDetectionMap(cube->pars().getDetectionMap()+"/vcps"); |
---|
| 177 | } |
---|
[87] | 178 | std::cout << "done.\n"; |
---|
[3] | 179 | |
---|
[49] | 180 | if((cube->getDimZ()>1) && (cube->getNumObj()>0)){ |
---|
[3] | 181 | std::cout << "Plotting the individual spectra... " << std::flush; |
---|
| 182 | cube->outputSpectra(); |
---|
| 183 | std::cout << "done.\n"; |
---|
| 184 | } |
---|
[120] | 185 | else if(cube->getDimZ()<=1) |
---|
[168] | 186 | duchampWarning("Duchamp", |
---|
| 187 | "Not plotting any spectra -- no third dimension.\n"); |
---|
[3] | 188 | |
---|
| 189 | cpgend(); |
---|
| 190 | |
---|
| 191 | if(cube->pars().getFlagATrous()&& |
---|
| 192 | (cube->pars().getFlagOutputRecon()||cube->pars().getFlagOutputResid()) ){ |
---|
| 193 | std::cout << "Saving reconstructed cube... "<<std::flush; |
---|
| 194 | cube->saveReconstructedCube(); |
---|
| 195 | std::cout << "done.\n"; |
---|
| 196 | } |
---|
| 197 | |
---|
| 198 | if(cube->pars().getFlagLog() && (cube->getNumObj()>0)){ |
---|
| 199 | ofstream logfile(cube->pars().getLogFile().c_str(),std::ios::app); |
---|
| 200 | logfile << "=-=-=-=-=-=-=-\nCube summary\n=-=-=-=-=-=-=-\n"; |
---|
| 201 | logfile << *cube; |
---|
| 202 | logfile.close(); |
---|
| 203 | } |
---|
| 204 | |
---|
| 205 | if(cube->pars().getFlagVOT()){ |
---|
| 206 | ofstream votfile(cube->pars().getVOTFile().c_str()); |
---|
| 207 | cube->outputDetectionsVOTable(votfile); |
---|
| 208 | votfile.close(); |
---|
| 209 | } |
---|
| 210 | |
---|
[20] | 211 | if(cube->pars().getFlagKarma()){ |
---|
| 212 | ofstream karmafile(cube->pars().getKarmaFile().c_str()); |
---|
| 213 | cube->outputDetectionsKarma(karmafile); |
---|
| 214 | karmafile.close(); |
---|
| 215 | } |
---|
| 216 | |
---|
[3] | 217 | delete cube; |
---|
| 218 | |
---|
[168] | 219 | return SUCCESS; |
---|
[3] | 220 | } |
---|
| 221 | |
---|