source: trunk/src/Cubes/existingDetections.cc @ 583

Last change on this file since 583 was 583, checked in by MatthewWhiting, 15 years ago

Fixes for ticket #60, that make the reading of previous detections more robust.

File size: 4.1 KB
Line 
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>
38
39using namespace PixelInfo;
40
41namespace duchamp
42{
43
44  int Cube::getExistingDetections()
45  {
46
47    std::ifstream logfile(this->par.getLogFile().c_str());
48 
49    if(!logfile.is_open()){
50      std::stringstream errmsg;
51      errmsg << "Unable to open log file " << this->par.getLogFile();
52      duchampError("getExistingDetections",errmsg.str());
53      return FAILURE;
54    }
55
56    std::cout << "  Reading from logfile : " << this->par.getLogFile() << "\n";
57
58    std::string temp,filename;
59    std::stringstream ss;
60
61    // first check filename, just to be sure
62    while(getline(logfile,temp), (temp.size()<11 || temp.substr(0,11)!="Image to be")){}
63    ss.str(temp);
64    ss >> temp >> temp >> temp >> temp>> temp >> filename;
65    if(filename != this->par.getFullImageFile()){
66      std::stringstream errmsg;
67      errmsg << "The image file given the log file (" << filename
68             <<") is different to that in the parameter file (" << this->par.getFullImageFile()<<").";
69      duchampError("getExistingDetections", errmsg.str());
70      return FAILURE;
71    }
72
73    // read down until first Detection # line
74    while(getline(logfile,temp), (temp.size()<9 || temp.substr(0,9)!="Threshold")){
75    }
76    float threshold,middle,spread;
77    bool robust;
78    getline(logfile,temp);
79    std::stringstream dataline;
80    dataline.str(temp);
81    dataline >> threshold >> middle >> spread >> robust;
82    std::cout << "  Detection threshold used was " << threshold << "\n";
83    this->Stats.setRobust(robust);
84    this->Stats.setThreshold(threshold);
85    this->Stats.setMiddle(middle);
86    this->Stats.setSpread(spread);
87    getline(logfile,temp);
88    getline(logfile,temp);
89    int x1,x2, ypix, zpix;
90    while(!logfile.eof()){
91      Detection obj;
92      while(getline(logfile,temp), temp.substr(0,3)!="---"){
93        if(temp.substr(0,9)!="Detection"){
94          for(uint i=0;i<temp.size();i++)
95            if(temp[i]=='-' || temp[i]==',') temp[i] = ' ';
96          std::stringstream ss;
97          ss.str(temp);
98          ss >> x1 >> x2 >> ypix >> zpix;
99          Scan scn(ypix,x1,x2-x1+1);
100          obj.pixels().addScan(scn,zpix);
101        }
102      }
103      obj.setOffsets(this->par);
104      if(obj.getSize()>0){
105        obj.calcParams();
106        this->addObject(obj);
107      }
108      getline(logfile,temp); // reads next line -- should be Detection #...
109      if(temp.substr(0,11)!="Detection #"){
110        // if it is, then read two lines to finish off the file. This should trigger the eof flag above.
111        getline(logfile,temp);
112        getline(logfile,temp);
113      }
114    }
115
116    std::cout<<"  Final object count = "<<this->objectList->size()<<std::endl;
117   
118    return SUCCESS;
119
120  }
121
122
123}
Note: See TracBrowser for help on using the repository browser.