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

Last change on this file since 1441 was 1146, checked in by MatthewWhiting, 11 years ago

Ticket #181 - Implementing the new catalogue writing, with new functions in Cube to handle the overall reading & writing. Briefly tested and seems to work OK. A couple of minor bug fixes as well, one in particular with the baseline-writing.

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      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,13)!="# Image to be")){}
62    ss.str(temp);
63    ss >> temp >> 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 #") DUCHAMPTHROW("existingDetections","Format of Log file is wrong - \""<<temp.substr(0,11)<<"\" should be \"Detection #\"!");
101      Detection obj;
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);
112      }
113     
114      obj.setOffsets(this->par);
115      if(obj.getSize()>0){
116        obj.calcParams();
117        this->addObject(obj);
118      }
119
120    }
121    logfile.close();
122
123    this->updateDetectMap();
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.