[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> |
---|
[80] | 10 | #include <time.h> |
---|
[57] | 11 | |
---|
| 12 | #include <duchamp.hh> |
---|
[3] | 13 | #include <Detection/detection.hh> |
---|
| 14 | #include <Cubes/cubes.hh> |
---|
| 15 | #include <Utils/utils.hh> |
---|
| 16 | #include <ATrous/atrous.hh> |
---|
| 17 | |
---|
| 18 | using std::ofstream; |
---|
| 19 | using std::endl; |
---|
| 20 | |
---|
| 21 | Filter reconFilter; |
---|
| 22 | |
---|
| 23 | int main(int argc, char * argv[]) |
---|
| 24 | { |
---|
| 25 | |
---|
[85] | 26 | string paramFile,fitsfile; |
---|
| 27 | Cube *cube = new Cube; |
---|
| 28 | Param *par = new Param; |
---|
[3] | 29 | |
---|
| 30 | if(argc==1){ |
---|
[57] | 31 | std::cout << ERR_USAGE_MSG; |
---|
[85] | 32 | return 1; |
---|
[3] | 33 | } |
---|
| 34 | else{ |
---|
| 35 | char c; |
---|
[85] | 36 | while( ( c = getopt(argc,argv,"p:f:hv") )!=-1){ |
---|
[3] | 37 | switch(c) { |
---|
| 38 | case 'p': |
---|
| 39 | paramFile = optarg; |
---|
[85] | 40 | cube->readParam(paramFile); |
---|
[3] | 41 | break; |
---|
[85] | 42 | case 'f': |
---|
| 43 | fitsfile = optarg; |
---|
| 44 | par->setImageFile(fitsfile); |
---|
| 45 | cube->saveParam(*par); |
---|
| 46 | break; |
---|
[57] | 47 | case 'v': |
---|
| 48 | std::cout << PROGNAME << " " << VERSION << std::endl; |
---|
[85] | 49 | return 1; |
---|
[57] | 50 | break; |
---|
[3] | 51 | case 'h': |
---|
| 52 | default : |
---|
[57] | 53 | std::cout << ERR_USAGE_MSG; |
---|
[85] | 54 | return 1; |
---|
[3] | 55 | break; |
---|
| 56 | } |
---|
| 57 | } |
---|
| 58 | } |
---|
| 59 | |
---|
[85] | 60 | delete par; |
---|
| 61 | |
---|
[3] | 62 | if(cube->pars().getImageFile().empty()){ |
---|
[85] | 63 | std::cerr << "ERROR : No input image has been given!\n"; |
---|
| 64 | std::cerr << " Use the imageFile parameter in "<< paramFile << " to specify the FITS file.\n"; |
---|
| 65 | std::cerr << "Exiting...\n\n"; |
---|
| 66 | return 1; |
---|
[3] | 67 | } |
---|
| 68 | |
---|
| 69 | // Filter needed for baseline removal and a trous reconstruction |
---|
| 70 | if(cube->pars().getFlagATrous() || cube->pars().getFlagBaseline()){ |
---|
| 71 | reconFilter.define(cube->pars().getFilterCode()); |
---|
| 72 | cube->pars().setFilterName(reconFilter.getName()); |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | string fname = cube->pars().getImageFile(); |
---|
| 76 | if(cube->pars().getFlagSubsection()){ |
---|
| 77 | cube->pars().parseSubsection(); |
---|
| 78 | fname+=cube->pars().getSubsection(); |
---|
| 79 | } |
---|
| 80 | std::cout << "Opening image: " << fname << endl; |
---|
[71] | 81 | if( cube->getCube(fname) == FAILURE){ |
---|
[85] | 82 | std::cerr << "ERROR : Unable to open image file : " << fname << endl; |
---|
| 83 | std::cerr << "Exiting...\n\n"; |
---|
| 84 | return 1; |
---|
[3] | 85 | } |
---|
| 86 | else std::cout << "Opened successfully." << endl; |
---|
| 87 | |
---|
[71] | 88 | // If the reconstructed array is to be read in from disk |
---|
[76] | 89 | if( cube->pars().getFlagReconExists() && cube->pars().getFlagATrous() ){ |
---|
[71] | 90 | if( cube->readReconCube() == FAILURE){ |
---|
[85] | 91 | std::cerr << "WARNING : Could not read in existing reconstructed array.\n" |
---|
[71] | 92 | << " Will perform reconstruction using assigned parameters.\n"; |
---|
| 93 | cube->pars().setFlagReconExists(false); |
---|
| 94 | } |
---|
| 95 | } |
---|
| 96 | |
---|
[3] | 97 | std::cout << cube->pars(); |
---|
| 98 | |
---|
| 99 | if(cube->pars().getFlagLog()){ |
---|
| 100 | ofstream logfile(cube->pars().getLogFile().c_str()); |
---|
| 101 | logfile << "New run of the Duchamp sourcefinder: "; |
---|
[80] | 102 | time_t now = time(NULL); |
---|
| 103 | logfile << asctime( localtime(&now) ); |
---|
[3] | 104 | logfile << cube->pars(); |
---|
| 105 | logfile.close(); |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | if(cube->pars().getFlagBlankPix()){ |
---|
| 109 | std::cout<<"Trimming the Blank Pixels from the edges... "<<std::flush; |
---|
| 110 | cube->trimCube(); |
---|
| 111 | std::cout<<"Done."<<endl; |
---|
| 112 | std::cout << "New dimensions: " << cube->getDimX(); |
---|
| 113 | if(cube->getNumDim()>1) std::cout << "x" << cube->getDimY(); |
---|
| 114 | if(cube->getNumDim()>2) std::cout << "x" << cube->getDimZ(); |
---|
| 115 | std::cout << endl; |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | if(cube->pars().getFlagMW()){ |
---|
| 119 | std::cout<<"Removing the Milky Way channels... "; |
---|
| 120 | cube->removeMW(); |
---|
| 121 | std::cout<<"Done."<<endl; |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | if(cube->pars().getFlagBaseline()){ |
---|
| 125 | std::cout<<"Removing the baselines... "<<std::flush; |
---|
| 126 | cube->removeBaseline(); |
---|
| 127 | std::cout<<" Done. "<<std::endl; |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | if(cube->getDimZ()==1) cube->pars().setMinChannels(0); // special case for 2D images. |
---|
| 131 | |
---|
[60] | 132 | if(cube->pars().getFlagNegative()){ |
---|
| 133 | std::cout<<"Inverting the Cube... "<<std::flush; |
---|
| 134 | cube->invert(); |
---|
| 135 | std::cout<<" Done."<<std::endl; |
---|
| 136 | } |
---|
| 137 | |
---|
[76] | 138 | if(cube->pars().getFlagATrous()){ |
---|
[3] | 139 | std::cout<<"Commencing search in reconstructed cube..."<<endl; |
---|
| 140 | cube->ReconSearch3D(); |
---|
| 141 | std::cout<<"Done. Found "<<cube->getNumObj()<<" objects."<<endl; |
---|
| 142 | } |
---|
| 143 | else{ |
---|
| 144 | std::cout<<"Commencing search in cube..."<<endl; |
---|
| 145 | cube->SimpleSearch3D(); |
---|
| 146 | std::cout<<"Done. Found "<<cube->getNumObj()<<" objects."<<endl; |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | std::cout<<"Merging lists... "<<std::flush; |
---|
| 150 | cube->ObjectMerger(); |
---|
| 151 | std::cout<<"Done. "<<endl; |
---|
| 152 | std::cout<<"Final object count = "<<cube->getNumObj()<<endl; |
---|
| 153 | |
---|
[60] | 154 | if(cube->pars().getFlagNegative()){ |
---|
| 155 | std::cout<<"Un-Inverting the Cube... "<<std::flush; |
---|
| 156 | cube->reInvert(); |
---|
| 157 | std::cout<<" Done."<<std::endl; |
---|
| 158 | } |
---|
| 159 | |
---|
[3] | 160 | cube->replaceBaseline(); |
---|
| 161 | |
---|
| 162 | if(cube->pars().getFlagCubeTrimmed()){ |
---|
| 163 | std::cout<<"Replacing the trimmed pixels on the edges... "<<std::flush; |
---|
| 164 | cube->unTrimCube(); |
---|
| 165 | std::cout<<"Done."<<endl; |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | if(cube->getNumObj()>0){ |
---|
| 169 | |
---|
| 170 | cube->calcObjectWCSparams(); |
---|
| 171 | |
---|
| 172 | cube->sortDetections(); |
---|
| 173 | |
---|
| 174 | cube->outputDetectionList(); |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | // cube->plotDetectionMap("/xs"); |
---|
| 178 | cube->plotMomentMap("/xs"); |
---|
| 179 | |
---|
| 180 | if(cube->pars().getFlagMaps()){ |
---|
[12] | 181 | std::cout<<"Creating the maps... "<<std::flush; |
---|
[3] | 182 | cube->plotMomentMap(cube->pars().getMomentMap()+"/vcps"); |
---|
| 183 | cube->plotDetectionMap(cube->pars().getDetectionMap()+"/vcps"); |
---|
[12] | 184 | std::cout << "done.\n"; |
---|
[3] | 185 | } |
---|
| 186 | |
---|
[49] | 187 | // if(cube->isWCS() && (cube->getNumObj()>0)){ |
---|
| 188 | if((cube->getDimZ()>1) && (cube->getNumObj()>0)){ |
---|
[3] | 189 | std::cout << "Plotting the individual spectra... " << std::flush; |
---|
| 190 | cube->outputSpectra(); |
---|
| 191 | std::cout << "done.\n"; |
---|
| 192 | } |
---|
[49] | 193 | else if(cube->getDimZ()<=1) std::cout << "Not plotting any spectra -- no third dimension.\n"; |
---|
[3] | 194 | |
---|
| 195 | cpgend(); |
---|
| 196 | |
---|
| 197 | if(cube->pars().getFlagATrous()&& |
---|
| 198 | (cube->pars().getFlagOutputRecon()||cube->pars().getFlagOutputResid()) ){ |
---|
| 199 | std::cout << "Saving reconstructed cube... "<<std::flush; |
---|
| 200 | cube->saveReconstructedCube(); |
---|
| 201 | std::cout << "done.\n"; |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | if(cube->pars().getFlagLog() && (cube->getNumObj()>0)){ |
---|
| 205 | ofstream logfile(cube->pars().getLogFile().c_str(),std::ios::app); |
---|
| 206 | logfile << "=-=-=-=-=-=-=-\nCube summary\n=-=-=-=-=-=-=-\n"; |
---|
| 207 | logfile << *cube; |
---|
| 208 | logfile.close(); |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | if(cube->pars().getFlagVOT()){ |
---|
| 212 | ofstream votfile(cube->pars().getVOTFile().c_str()); |
---|
| 213 | cube->outputDetectionsVOTable(votfile); |
---|
| 214 | votfile.close(); |
---|
| 215 | } |
---|
| 216 | |
---|
[20] | 217 | if(cube->pars().getFlagKarma()){ |
---|
| 218 | ofstream karmafile(cube->pars().getKarmaFile().c_str()); |
---|
| 219 | cube->outputDetectionsKarma(karmafile); |
---|
| 220 | karmafile.close(); |
---|
| 221 | } |
---|
| 222 | |
---|
[3] | 223 | delete cube; |
---|
| 224 | |
---|
| 225 | return 0; |
---|
| 226 | } |
---|
| 227 | |
---|