[476] | 1 | // ----------------------------------------------------------------------- |
---|
| 2 | // existingDetections.cc: Functions to read in previously-made |
---|
| 3 | // detections from an existing log file |
---|
| 4 | // ----------------------------------------------------------------------- |
---|
| 5 | // Copyright (C) 2006, Matthew Whiting, ATNF |
---|
| 6 | // |
---|
| 7 | // This program is free software; you can redistribute it and/or modify it |
---|
| 8 | // under the terms of the GNU General Public License as published by the |
---|
| 9 | // Free Software Foundation; either version 2 of the License, or (at your |
---|
| 10 | // option) any later version. |
---|
| 11 | // |
---|
| 12 | // Duchamp is distributed in the hope that it will be useful, but WITHOUT |
---|
| 13 | // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
---|
| 14 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
---|
| 15 | // for more details. |
---|
| 16 | // |
---|
| 17 | // You should have received a copy of the GNU General Public License |
---|
| 18 | // along with Duchamp; if not, write to the Free Software Foundation, |
---|
| 19 | // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA |
---|
| 20 | // |
---|
| 21 | // Correspondence concerning Duchamp may be directed to: |
---|
| 22 | // Internet email: Matthew.Whiting [at] atnf.csiro.au |
---|
| 23 | // Postal address: Dr. Matthew Whiting |
---|
| 24 | // Australia Telescope National Facility, CSIRO |
---|
| 25 | // PO Box 76 |
---|
| 26 | // Epping NSW 1710 |
---|
| 27 | // AUSTRALIA |
---|
| 28 | // ----------------------------------------------------------------------- |
---|
| 29 | #include <iostream> |
---|
| 30 | #include <fstream> |
---|
| 31 | #include <sstream> |
---|
| 32 | #include <string> |
---|
| 33 | |
---|
| 34 | #include <duchamp/Cubes/cubes.hh> |
---|
| 35 | #include <duchamp/param.hh> |
---|
| 36 | #include <duchamp/PixelMap/Scan.hh> |
---|
| 37 | #include <duchamp/Detection/detection.hh> |
---|
[611] | 38 | #include <duchamp/Utils/feedback.hh> |
---|
[476] | 39 | |
---|
| 40 | using namespace PixelInfo; |
---|
| 41 | |
---|
| 42 | namespace duchamp |
---|
| 43 | { |
---|
| 44 | |
---|
[698] | 45 | OUTCOME Cube::getExistingDetections() |
---|
[476] | 46 | { |
---|
| 47 | |
---|
| 48 | std::ifstream logfile(this->par.getLogFile().c_str()); |
---|
| 49 | |
---|
| 50 | if(!logfile.is_open()){ |
---|
[913] | 51 | DUCHAMPERROR("getExistingDetections","Unable to open log file " << this->par.getLogFile()); |
---|
[476] | 52 | return FAILURE; |
---|
| 53 | } |
---|
| 54 | |
---|
[611] | 55 | if(this->par.isVerbose()) std::cout << " Reading from logfile : " << this->par.getLogFile() << "\n"; |
---|
[476] | 56 | |
---|
| 57 | std::string temp,filename; |
---|
| 58 | std::stringstream ss; |
---|
| 59 | |
---|
| 60 | // first check filename, just to be sure |
---|
[1146] | 61 | while(getline(logfile,temp), (temp.size()<11 || temp.substr(0,13)!="# Image to be")){} |
---|
[476] | 62 | ss.str(temp); |
---|
[1146] | 63 | ss >> temp >> temp >> temp >> temp >> temp>> temp >> filename; |
---|
[476] | 64 | if(filename != this->par.getFullImageFile()){ |
---|
[913] | 65 | DUCHAMPERROR("getExistingDetections", "The image file given the log file (" << filename <<") is different to that in the parameter file (" << this->par.getFullImageFile()<<")."); |
---|
[476] | 66 | return FAILURE; |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | // read down until first Detection # line |
---|
[583] | 70 | while(getline(logfile,temp), (temp.size()<9 || temp.substr(0,9)!="Threshold")){ |
---|
| 71 | } |
---|
[476] | 72 | float threshold,middle,spread; |
---|
| 73 | bool robust; |
---|
[583] | 74 | getline(logfile,temp); |
---|
| 75 | std::stringstream dataline; |
---|
| 76 | dataline.str(temp); |
---|
| 77 | dataline >> threshold >> middle >> spread >> robust; |
---|
[611] | 78 | if(this->par.isVerbose()) std::cout << " Detection threshold used was " << threshold << "\n"; |
---|
[476] | 79 | this->Stats.setRobust(robust); |
---|
| 80 | this->Stats.setThreshold(threshold); |
---|
| 81 | this->Stats.setMiddle(middle); |
---|
| 82 | this->Stats.setSpread(spread); |
---|
| 83 | getline(logfile,temp); |
---|
[589] | 84 | std::stringstream numline; |
---|
| 85 | numline.str(temp); |
---|
| 86 | int numDets; |
---|
| 87 | std::string null; |
---|
| 88 | numline >> numDets >> null; |
---|
[611] | 89 | if(this->par.isVerbose()) std::cout << " Number of detections in logfile = " << numDets << "\n"; |
---|
| 90 | ProgressBar bar; |
---|
| 91 | if(this->par.isVerbose()){ |
---|
| 92 | std::cout << " Reading detections... ";// << std::flush; |
---|
| 93 | bar.init(numDets); |
---|
| 94 | } |
---|
[476] | 95 | getline(logfile,temp); |
---|
| 96 | int x1,x2, ypix, zpix; |
---|
[589] | 97 | for(int d=0; d<numDets; d++){ |
---|
| 98 | getline(logfile,temp); // This should be Detection #X: |
---|
[611] | 99 | if(this->par.isVerbose()) bar.update(d+1); |
---|
[1003] | 100 | if(temp.substr(0,11)!="Detection #") DUCHAMPTHROW("existingDetections","Format of Log file is wrong - \""<<temp.substr(0,11)<<"\" should be \"Detection #\"!"); |
---|
[476] | 101 | Detection obj; |
---|
[1003] | 102 | getline(logfile,temp); |
---|
| 103 | while(temp.size()>0){ |
---|
| 104 | for(size_t i=0;i<temp.size();i++) |
---|
| 105 | if(temp[i]=='-' || temp[i]==',') temp[i] = ' '; |
---|
| 106 | std::stringstream ss; |
---|
| 107 | ss.str(temp); |
---|
| 108 | ss >> x1 >> x2 >> ypix >> zpix; |
---|
| 109 | Scan scn(ypix-this->par.getYOffset(),x1-this->par.getXOffset(),x2-x1+1); |
---|
| 110 | obj.addScan(scn,zpix-this->par.getZOffset()); |
---|
| 111 | getline(logfile,temp); |
---|
[476] | 112 | } |
---|
[1003] | 113 | |
---|
[476] | 114 | obj.setOffsets(this->par); |
---|
[583] | 115 | if(obj.getSize()>0){ |
---|
| 116 | obj.calcParams(); |
---|
| 117 | this->addObject(obj); |
---|
| 118 | } |
---|
[1003] | 119 | |
---|
[476] | 120 | } |
---|
[589] | 121 | logfile.close(); |
---|
[476] | 122 | |
---|
[1003] | 123 | this->updateDetectMap(); |
---|
| 124 | |
---|
[611] | 125 | if(this->par.isVerbose()){ |
---|
| 126 | bar.remove(); |
---|
| 127 | std::cout << "Done.\n"; |
---|
| 128 | } |
---|
| 129 | |
---|
[476] | 130 | return SUCCESS; |
---|
| 131 | |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | |
---|
| 135 | } |
---|