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

Last change on this file since 913 was 913, checked in by MatthewWhiting, 12 years ago

A large swathe of changes aimed at improving warning/error/exception handling. Now make use of macros and streams. Also, there is now a distinction between DUCHAMPERROR and DUCHAMPTHROW.

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