source: tags/release-1.1.11/src/Cubes/existingDetections.cc @ 1441

Last change on this file since 1441 was 718, checked in by MatthewWhiting, 14 years ago

A rogue uint that was missed.

File size: 4.6 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#include <duchamp/Utils/feedback.hh>
39
40using namespace PixelInfo;
41
42namespace duchamp
43{
44
45  OUTCOME Cube::getExistingDetections()
46  {
47
48    std::ifstream logfile(this->par.getLogFile().c_str());
49 
50    if(!logfile.is_open()){
51      std::stringstream errmsg;
52      errmsg << "Unable to open log file " << this->par.getLogFile();
53      duchampError("getExistingDetections",errmsg.str());
54      return FAILURE;
55    }
56
57    if(this->par.isVerbose()) std::cout << "  Reading from logfile : " << this->par.getLogFile() << "\n";
58
59    std::string temp,filename;
60    std::stringstream ss;
61
62    // first check filename, just to be sure
63    while(getline(logfile,temp), (temp.size()<11 || temp.substr(0,11)!="Image to be")){}
64    ss.str(temp);
65    ss >> temp >> temp >> temp >> temp>> temp >> filename;
66    if(filename != this->par.getFullImageFile()){
67      std::stringstream errmsg;
68      errmsg << "The image file given the log file (" << filename
69             <<") is different to that in the parameter file (" << this->par.getFullImageFile()<<").";
70      duchampError("getExistingDetections", errmsg.str());
71      return FAILURE;
72    }
73
74    // read down until first Detection # line
75    while(getline(logfile,temp), (temp.size()<9 || temp.substr(0,9)!="Threshold")){
76    }
77    float threshold,middle,spread;
78    bool robust;
79    getline(logfile,temp);
80    std::stringstream dataline;
81    dataline.str(temp);
82    dataline >> threshold >> middle >> spread >> robust;
83    if(this->par.isVerbose()) std::cout << "  Detection threshold used was " << threshold << "\n";
84    this->Stats.setRobust(robust);
85    this->Stats.setThreshold(threshold);
86    this->Stats.setMiddle(middle);
87    this->Stats.setSpread(spread);
88    getline(logfile,temp);
89    std::stringstream numline;
90    numline.str(temp);
91    int numDets;
92    std::string null;
93    numline >> numDets >> null;
94    if(this->par.isVerbose()) std::cout << "  Number of detections in logfile = " << numDets << "\n";
95    ProgressBar bar;
96    if(this->par.isVerbose()){
97      std::cout << "  Reading detections... ";// << std::flush;
98      bar.init(numDets);
99    }
100    getline(logfile,temp);
101    int x1,x2, ypix, zpix;
102    for(int d=0; d<numDets; d++){
103      getline(logfile,temp);   // This should be Detection #X:
104      if(this->par.isVerbose()) bar.update(d+1);
105      if(temp.substr(0,11)!="Detection #") duchampError("existingDetections","Format of Log file is wrong!");
106      Detection obj;
107      while(getline(logfile,temp), temp.size()>0){
108          for(size_t i=0;i<temp.size();i++)
109            if(temp[i]=='-' || temp[i]==',') temp[i] = ' ';
110          std::stringstream ss;
111          ss.str(temp);
112          ss >> x1 >> x2 >> ypix >> zpix;
113          Scan scn(ypix-this->par.getYOffset(),x1-this->par.getXOffset(),x2-x1+1);
114          obj.addScan(scn,zpix-this->par.getZOffset());
115      }
116      obj.setOffsets(this->par);
117      if(obj.getSize()>0){
118        obj.calcParams();
119        this->addObject(obj);
120      }
121      getline(logfile,temp);
122    }
123    logfile.close();
124
125    if(this->par.isVerbose()){
126      bar.remove();
127      std::cout << "Done.\n";
128    }
129
130    return SUCCESS;
131
132  }
133
134
135}
Note: See TracBrowser for help on using the repository browser.