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