[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> |
---|
[3] | 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; |
---|
[95] | 16 | using std::string; |
---|
[3] | 17 | |
---|
| 18 | Filter reconFilter; |
---|
| 19 | |
---|
| 20 | int main(int argc, char * argv[]) |
---|
| 21 | { |
---|
| 22 | |
---|
[85] | 23 | string paramFile,fitsfile; |
---|
| 24 | Cube *cube = new Cube; |
---|
| 25 | Param *par = new Param; |
---|
[3] | 26 | |
---|
| 27 | if(argc==1){ |
---|
[57] | 28 | std::cout << ERR_USAGE_MSG; |
---|
[85] | 29 | return 1; |
---|
[3] | 30 | } |
---|
| 31 | else{ |
---|
| 32 | char c; |
---|
[85] | 33 | while( ( c = getopt(argc,argv,"p:f:hv") )!=-1){ |
---|
[3] | 34 | switch(c) { |
---|
| 35 | case 'p': |
---|
| 36 | paramFile = optarg; |
---|
[85] | 37 | cube->readParam(paramFile); |
---|
[3] | 38 | break; |
---|
[85] | 39 | case 'f': |
---|
| 40 | fitsfile = optarg; |
---|
| 41 | par->setImageFile(fitsfile); |
---|
| 42 | cube->saveParam(*par); |
---|
| 43 | break; |
---|
[57] | 44 | case 'v': |
---|
| 45 | std::cout << PROGNAME << " " << VERSION << std::endl; |
---|
[85] | 46 | return 1; |
---|
[57] | 47 | break; |
---|
[3] | 48 | case 'h': |
---|
| 49 | default : |
---|
[57] | 50 | std::cout << ERR_USAGE_MSG; |
---|
[85] | 51 | return 1; |
---|
[3] | 52 | break; |
---|
| 53 | } |
---|
| 54 | } |
---|
| 55 | } |
---|
| 56 | |
---|
[85] | 57 | delete par; |
---|
| 58 | |
---|
[3] | 59 | if(cube->pars().getImageFile().empty()){ |
---|
[85] | 60 | std::cerr << "ERROR : No input image has been given!\n"; |
---|
| 61 | std::cerr << " Use the imageFile parameter in "<< paramFile << " to specify the FITS file.\n"; |
---|
| 62 | std::cerr << "Exiting...\n\n"; |
---|
| 63 | return 1; |
---|
[3] | 64 | } |
---|
| 65 | |
---|
| 66 | string fname = cube->pars().getImageFile(); |
---|
| 67 | if(cube->pars().getFlagSubsection()){ |
---|
| 68 | cube->pars().parseSubsection(); |
---|
| 69 | fname+=cube->pars().getSubsection(); |
---|
| 70 | } |
---|
[95] | 71 | std::cout << "Opening image: " << fname << std::endl; |
---|
[71] | 72 | if( cube->getCube(fname) == FAILURE){ |
---|
[95] | 73 | std::cerr << "ERROR : Unable to open image file : " << fname << std::endl; |
---|
[85] | 74 | std::cerr << "Exiting...\n\n"; |
---|
| 75 | return 1; |
---|
[3] | 76 | } |
---|
[95] | 77 | else std::cout << "Opened successfully." << std::endl; |
---|
[3] | 78 | |
---|
[71] | 79 | // If the reconstructed array is to be read in from disk |
---|
[76] | 80 | if( cube->pars().getFlagReconExists() && cube->pars().getFlagATrous() ){ |
---|
[95] | 81 | std::cout << "Reading reconstructed array: "<<std::endl; |
---|
[71] | 82 | if( cube->readReconCube() == FAILURE){ |
---|
[85] | 83 | std::cerr << "WARNING : Could not read in existing reconstructed array.\n" |
---|
[71] | 84 | << " Will perform reconstruction using assigned parameters.\n"; |
---|
| 85 | cube->pars().setFlagReconExists(false); |
---|
| 86 | } |
---|
[95] | 87 | else std::cout << "Reconstructed array available.\n"; |
---|
[71] | 88 | } |
---|
| 89 | |
---|
[95] | 90 | // Filter needed for baseline removal and a trous reconstruction |
---|
| 91 | if(cube->pars().getFlagATrous() || cube->pars().getFlagBaseline()){ |
---|
| 92 | reconFilter.define(cube->pars().getFilterCode()); |
---|
| 93 | cube->pars().setFilterName(reconFilter.getName()); |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | // Write the parameters to screen. |
---|
[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(); |
---|
[95] | 111 | std::cout<<"Done."<<std::endl; |
---|
[3] | 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(); |
---|
[95] | 115 | std::cout << std::endl; |
---|
[3] | 116 | } |
---|
| 117 | |
---|
[96] | 118 | // if(cube->pars().getFlagMW()){ |
---|
| 119 | // std::cout<<"Removing the Milky Way channels... "<<std::flush; |
---|
| 120 | // cube->removeMW(); |
---|
| 121 | // std::cout<<"Done."<<std::endl; |
---|
| 122 | // } |
---|
[3] | 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()){ |
---|
[95] | 139 | std::cout<<"Commencing search in reconstructed cube..."<<std::endl; |
---|
[96] | 140 | cube->ReconSearch(); |
---|
[95] | 141 | std::cout<<"Done. Found "<<cube->getNumObj()<<" objects."<<std::endl; |
---|
[3] | 142 | } |
---|
| 143 | else{ |
---|
[95] | 144 | std::cout<<"Commencing search in cube..."<<std::endl; |
---|
[96] | 145 | cube->CubicSearch(); |
---|
[95] | 146 | std::cout<<"Done. Found "<<cube->getNumObj()<<" objects."<<std::endl; |
---|
[3] | 147 | } |
---|
| 148 | |
---|
| 149 | std::cout<<"Merging lists... "<<std::flush; |
---|
| 150 | cube->ObjectMerger(); |
---|
[95] | 151 | std::cout<<"Done. "<<std::endl; |
---|
| 152 | std::cout<<"Final object count = "<<cube->getNumObj()<<std::endl; |
---|
[3] | 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(); |
---|
[95] | 165 | std::cout<<"Done."<<std::endl; |
---|
[3] | 166 | } |
---|
| 167 | |
---|
| 168 | if(cube->getNumObj()>0){ |
---|
| 169 | |
---|
| 170 | cube->calcObjectWCSparams(); |
---|
[87] | 171 | |
---|
| 172 | cube->setObjectFlags(); |
---|
[3] | 173 | |
---|
| 174 | cube->sortDetections(); |
---|
| 175 | |
---|
| 176 | cube->outputDetectionList(); |
---|
| 177 | } |
---|
| 178 | |
---|
[87] | 179 | std::cout<<"Creating the maps... "<<std::flush; |
---|
| 180 | cube->plotMomentMap("/xs"); |
---|
[3] | 181 | if(cube->pars().getFlagMaps()){ |
---|
| 182 | cube->plotMomentMap(cube->pars().getMomentMap()+"/vcps"); |
---|
| 183 | cube->plotDetectionMap(cube->pars().getDetectionMap()+"/vcps"); |
---|
| 184 | } |
---|
[87] | 185 | std::cout << "done.\n"; |
---|
[3] | 186 | |
---|
[49] | 187 | if((cube->getDimZ()>1) && (cube->getNumObj()>0)){ |
---|
[3] | 188 | std::cout << "Plotting the individual spectra... " << std::flush; |
---|
| 189 | cube->outputSpectra(); |
---|
| 190 | std::cout << "done.\n"; |
---|
| 191 | } |
---|
[87] | 192 | else if(cube->getDimZ()<=1) std::cerr << "WARNING : Not plotting any spectra -- no third dimension.\n"; |
---|
[3] | 193 | |
---|
| 194 | cpgend(); |
---|
| 195 | |
---|
| 196 | if(cube->pars().getFlagATrous()&& |
---|
| 197 | (cube->pars().getFlagOutputRecon()||cube->pars().getFlagOutputResid()) ){ |
---|
| 198 | std::cout << "Saving reconstructed cube... "<<std::flush; |
---|
| 199 | cube->saveReconstructedCube(); |
---|
| 200 | std::cout << "done.\n"; |
---|
| 201 | } |
---|
| 202 | |
---|
| 203 | if(cube->pars().getFlagLog() && (cube->getNumObj()>0)){ |
---|
| 204 | ofstream logfile(cube->pars().getLogFile().c_str(),std::ios::app); |
---|
| 205 | logfile << "=-=-=-=-=-=-=-\nCube summary\n=-=-=-=-=-=-=-\n"; |
---|
| 206 | logfile << *cube; |
---|
| 207 | logfile.close(); |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | if(cube->pars().getFlagVOT()){ |
---|
| 211 | ofstream votfile(cube->pars().getVOTFile().c_str()); |
---|
| 212 | cube->outputDetectionsVOTable(votfile); |
---|
| 213 | votfile.close(); |
---|
| 214 | } |
---|
| 215 | |
---|
[20] | 216 | if(cube->pars().getFlagKarma()){ |
---|
| 217 | ofstream karmafile(cube->pars().getKarmaFile().c_str()); |
---|
| 218 | cube->outputDetectionsKarma(karmafile); |
---|
| 219 | karmafile.close(); |
---|
| 220 | } |
---|
| 221 | |
---|
[3] | 222 | delete cube; |
---|
| 223 | |
---|
| 224 | return 0; |
---|
| 225 | } |
---|
| 226 | |
---|