[3] | 1 | #include <iostream> |
---|
| 2 | #include <iomanip> |
---|
| 3 | #include <sstream> |
---|
| 4 | #include <fstream> |
---|
| 5 | #include <vector> |
---|
| 6 | #include <string> |
---|
| 7 | #include <cpgplot.h> |
---|
| 8 | #include <math.h> |
---|
| 9 | #include <unistd.h> |
---|
| 10 | #include <Detection/detection.hh> |
---|
| 11 | #include <Cubes/cubes.hh> |
---|
| 12 | #include <Utils/utils.hh> |
---|
| 13 | #include <ATrous/atrous.hh> |
---|
| 14 | |
---|
| 15 | using std::ofstream; |
---|
| 16 | using std::endl; |
---|
| 17 | |
---|
| 18 | Filter reconFilter; |
---|
| 19 | |
---|
| 20 | int main(int argc, char * argv[]) |
---|
| 21 | { |
---|
| 22 | |
---|
| 23 | string paramFile; |
---|
| 24 | string err_usage_msg("Usage:: Duchamp.x -p [parameter file]\n"); |
---|
| 25 | |
---|
| 26 | if(argc==1){ |
---|
| 27 | std::cout << err_usage_msg; |
---|
| 28 | exit(1); |
---|
| 29 | } |
---|
| 30 | else{ |
---|
| 31 | char c; |
---|
| 32 | while( ( c = getopt(argc,argv,"p:h") )!=-1){ |
---|
| 33 | switch(c) { |
---|
| 34 | case 'p': |
---|
| 35 | paramFile = optarg; |
---|
| 36 | break; |
---|
| 37 | case 'h': |
---|
| 38 | default : |
---|
| 39 | std::cout << err_usage_msg; |
---|
| 40 | exit(1); |
---|
| 41 | break; |
---|
| 42 | } |
---|
| 43 | } |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | Cube *cube = new Cube; |
---|
| 47 | cube->readParam(paramFile); |
---|
| 48 | if(cube->pars().getImageFile().empty()){ |
---|
| 49 | std::cerr << "Error: No input image has been given!"<<endl; |
---|
| 50 | std::cerr << "Use the imageFile parameter in "<< paramFile << " to specify the FITS file."<<endl; |
---|
| 51 | std::cerr << "Exiting..." << endl; |
---|
| 52 | exit(1); |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | // Filter needed for baseline removal and a trous reconstruction |
---|
| 56 | if(cube->pars().getFlagATrous() || cube->pars().getFlagBaseline()){ |
---|
| 57 | reconFilter.define(cube->pars().getFilterCode()); |
---|
| 58 | cube->pars().setFilterName(reconFilter.getName()); |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | string fname = cube->pars().getImageFile(); |
---|
| 62 | if(cube->pars().getFlagSubsection()){ |
---|
| 63 | cube->pars().parseSubsection(); |
---|
| 64 | fname+=cube->pars().getSubsection(); |
---|
| 65 | } |
---|
| 66 | std::cout << "Opening image: " << fname << endl; |
---|
| 67 | if( cube->getCube(fname) ){ |
---|
| 68 | std::cerr << "Error opening file : " << fname << endl; |
---|
| 69 | std::cerr << "Exiting..." << endl; |
---|
| 70 | exit(1); |
---|
| 71 | } |
---|
| 72 | else std::cout << "Opened successfully." << endl; |
---|
| 73 | if(!cube->isWCS()) std::cerr << "Warning! WCS is not good enough to be used.\n"; |
---|
| 74 | |
---|
| 75 | std::cout << cube->pars(); |
---|
| 76 | |
---|
| 77 | if(cube->pars().getFlagLog()){ |
---|
| 78 | ofstream logfile(cube->pars().getLogFile().c_str()); |
---|
| 79 | logfile << "New run of the Duchamp sourcefinder: "; |
---|
| 80 | logfile.close(); |
---|
| 81 | string syscall = "date >> " + cube->pars().getLogFile(); |
---|
| 82 | system(syscall.c_str()); |
---|
| 83 | logfile.open(cube->pars().getLogFile().c_str(),std::ios::app); |
---|
| 84 | logfile << cube->pars(); |
---|
| 85 | logfile.close(); |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | if(cube->pars().getFlagBlankPix()){ |
---|
| 89 | std::cout<<"Trimming the Blank Pixels from the edges... "<<std::flush; |
---|
| 90 | cube->trimCube(); |
---|
| 91 | std::cout<<"Done."<<endl; |
---|
| 92 | std::cout << "New dimensions: " << cube->getDimX(); |
---|
| 93 | if(cube->getNumDim()>1) std::cout << "x" << cube->getDimY(); |
---|
| 94 | if(cube->getNumDim()>2) std::cout << "x" << cube->getDimZ(); |
---|
| 95 | std::cout << endl; |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | if(cube->pars().getFlagMW()){ |
---|
| 99 | std::cout<<"Removing the Milky Way channels... "; |
---|
| 100 | cube->removeMW(); |
---|
| 101 | std::cout<<"Done."<<endl; |
---|
| 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 | |
---|
| 110 | if(cube->getDimZ()==1) cube->pars().setMinChannels(0); // special case for 2D images. |
---|
| 111 | |
---|
| 112 | if(cube->pars().getFlagATrous()){ |
---|
| 113 | std::cout<<"Commencing search in reconstructed cube..."<<endl; |
---|
| 114 | cube->ReconSearch3D(); |
---|
| 115 | std::cout<<"Done. Found "<<cube->getNumObj()<<" objects."<<endl; |
---|
| 116 | } |
---|
| 117 | else{ |
---|
| 118 | std::cout<<"Commencing search in cube..."<<endl; |
---|
| 119 | cube->SimpleSearch3D(); |
---|
| 120 | std::cout<<"Done. Found "<<cube->getNumObj()<<" objects."<<endl; |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | std::cout<<"Merging lists... "<<std::flush; |
---|
| 124 | cube->ObjectMerger(); |
---|
| 125 | std::cout<<"Done. "<<endl; |
---|
| 126 | std::cout<<"Final object count = "<<cube->getNumObj()<<endl; |
---|
| 127 | |
---|
| 128 | cube->replaceBaseline(); |
---|
| 129 | |
---|
| 130 | if(cube->pars().getFlagCubeTrimmed()){ |
---|
| 131 | std::cout<<"Replacing the trimmed pixels on the edges... "<<std::flush; |
---|
| 132 | cube->unTrimCube(); |
---|
| 133 | std::cout<<"Done."<<endl; |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | if(cube->getNumObj()>0){ |
---|
| 137 | |
---|
| 138 | cube->calcObjectWCSparams(); |
---|
| 139 | |
---|
| 140 | cube->sortDetections(); |
---|
| 141 | |
---|
| 142 | cube->outputDetectionList(); |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | // cube->plotDetectionMap("/xs"); |
---|
| 146 | cube->plotMomentMap("/xs"); |
---|
| 147 | |
---|
| 148 | if(cube->pars().getFlagMaps()){ |
---|
[12] | 149 | std::cout<<"Creating the maps... "<<std::flush; |
---|
[3] | 150 | cube->plotMomentMap(cube->pars().getMomentMap()+"/vcps"); |
---|
| 151 | cube->plotDetectionMap(cube->pars().getDetectionMap()+"/vcps"); |
---|
[12] | 152 | std::cout << "done.\n"; |
---|
[3] | 153 | } |
---|
| 154 | |
---|
| 155 | if(cube->isWCS() && (cube->getNumObj()>0)){ |
---|
| 156 | std::cout << "Plotting the individual spectra... " << std::flush; |
---|
| 157 | cube->outputSpectra(); |
---|
| 158 | std::cout << "done.\n"; |
---|
| 159 | } |
---|
| 160 | else if(!(cube->isWCS())) std::cout << "Not plotting any spectra due to bad WCS.\n"; |
---|
| 161 | |
---|
| 162 | cpgend(); |
---|
| 163 | |
---|
| 164 | if(cube->pars().getFlagATrous()&& |
---|
| 165 | (cube->pars().getFlagOutputRecon()||cube->pars().getFlagOutputResid()) ){ |
---|
| 166 | std::cout << "Saving reconstructed cube... "<<std::flush; |
---|
| 167 | cube->saveReconstructedCube(); |
---|
| 168 | std::cout << "done.\n"; |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | if(cube->pars().getFlagLog() && (cube->getNumObj()>0)){ |
---|
| 172 | ofstream logfile(cube->pars().getLogFile().c_str(),std::ios::app); |
---|
| 173 | logfile << "=-=-=-=-=-=-=-\nCube summary\n=-=-=-=-=-=-=-\n"; |
---|
| 174 | logfile << *cube; |
---|
| 175 | logfile.close(); |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | if(cube->pars().getFlagVOT()){ |
---|
| 179 | ofstream votfile(cube->pars().getVOTFile().c_str()); |
---|
| 180 | cube->outputDetectionsVOTable(votfile); |
---|
| 181 | votfile.close(); |
---|
| 182 | } |
---|
| 183 | |
---|
[20] | 184 | if(cube->pars().getFlagKarma()){ |
---|
| 185 | ofstream karmafile(cube->pars().getKarmaFile().c_str()); |
---|
| 186 | cube->outputDetectionsKarma(karmafile); |
---|
| 187 | karmafile.close(); |
---|
| 188 | } |
---|
| 189 | |
---|
[3] | 190 | delete cube; |
---|
| 191 | |
---|
| 192 | return 0; |
---|
| 193 | } |
---|
| 194 | |
---|