source: branches/pixel-map-branch/src/Utils/Section.cc @ 1339

Last change on this file since 1339 was 254, checked in by Matthew Whiting, 17 years ago

Added functionality to use only a subsection in the statistics calculations. This includes:

  • A new Section class.
  • New input parameters.
  • Altering the setCubeStats() function to test for this.
  • Some documentation on the new parameters.
File size: 4.5 KB
Line 
1#include <string>
2#include <vector>
3#include <iostream>
4#include <sstream>
5#include <Utils/Section.hh>
6#include <duchamp.hh>
7
8Section::Section(const Section& s)
9{
10  this->subsection  = s.subsection;
11  this->numSections = s.numSections;
12  this->starts      = s.starts;
13  this->dims        = s.dims;
14}
15//--------------------------------------------
16 
17Section& Section::operator= (const Section& s)
18{
19  this->subsection  = s.subsection;
20  this->numSections = s.numSections;
21  this->starts      = s.starts;
22  this->dims        = s.dims;
23}
24//--------------------------------------------
25
26int Section::parse(std::vector<long> dimAxes)
27{
28  /**
29   * This function reads the subsection string, and registers the
30   * starting values and lengths for each dimension. The array of axis
31   * dimensions is needed to know how long each axis really is, and
32   * whether we have strayed over the limit or not.
33   *
34   * Note that steps in the subsection string are not dealt with -- a
35   * warning message is written to the screen, and the step values are
36   * removed from the subsection string.
37   *
38   * The function also does basic checks to make sure it is of the
39   * correct format (ie. has square brackets delimiting it) and the
40   * correct number of sections, returning a FAILURE if either of
41   * these checks fail.
42   *
43   * \param dimAxes The array of axis dimensions, of which the
44   *                 Section is a subsection.
45   * \return SUCCESS/FAILURE (from duchamp.hh)
46   */
47
48  std::stringstream errmsg;
49
50  // First Make sure subsection has [ and ] at ends
51  if((this->subsection[0]!='[') ||
52     (this->subsection[this->subsection.size()-1]!=']')){
53    errmsg.str();
54    errmsg << "Subsection needs to be delimited by square brackets\n"
55           << "You provided: " << this->subsection << std::endl;
56    duchampError("Section::parse",errmsg.str());
57    return FAILURE;
58  }
59
60  this->starts.clear();
61  this->dims.clear();
62 
63  this->numSections=1;
64  for(int i=0;i<this->subsection.size();i++)
65    if(this->subsection[i]==',') this->numSections++;
66
67  if(numSections!=dimAxes.size()){
68    errmsg.str();
69    errmsg << "Subsection has "<<numSections
70           <<" sections, compared to a cube with "
71           << dimAxes.size() << " axes\n"
72           << "Subsection provided was: " << this->subsection << std::endl;
73    duchampError("Section::parse",errmsg.str());
74    return FAILURE;
75  }
76
77  this->starts.resize(this->numSections);
78  this->dims.resize(this->numSections);
79
80  std::vector<std::string> sections(numSections);
81  // this will hold the section strings for each dimension
82  std::stringstream ss;
83  ss.str(this->subsection);
84  bool removeStep = false;
85  bool doingBorders = false;
86  std::string temp;
87
88  bool atEnd = false;
89  getline(ss,temp,'[');
90  for(int i=0;i<numSections-1;i++){
91    getline(ss,temp,',');
92    sections[i]=temp;
93  }
94  getline(ss,temp,']');
95  sections[numSections-1]=temp;
96
97  for(int str=0;str<numSections;str++){
98    if(sections[str]=="*"){
99      this->starts[str] = 0;
100      this->dims[str]= dimAxes[str];
101    }
102    else{
103      int numColon=0;
104      for(int i=0;i<sections[str].size();i++){
105        if(sections[str][i]==':'){
106          sections[str][i]=' ';
107          numColon++;
108        }
109      }
110      int a,b,c;
111      std::stringstream temp;
112      temp.str(sections[str]);
113      switch(numColon){
114      case 1: // usual case
115        temp >> a >> b;
116        this->starts[str] = a-1;
117        this->dims[str] = b-a+1;
118        break;
119      case 0: // borders case -- so many off each border
120        temp >> a;
121        this->starts[str] = a-1;
122        this->dims[str] = dimAxes[str]-2*a;
123        temp.str();
124        temp << a << ":" << dimAxes[str]-a;
125        sections[str] = temp.str();
126        doingBorders=true;
127        break;
128      case 2:
129      default:
130        temp >> a >> b >> c;
131        this->starts[str] = a-1;
132        this->dims[str] = b-a+1;
133        temp.str();
134        temp << a << ":" << b;
135        sections[str] = temp.str();
136        removeStep=true;
137        break;
138      }
139
140    }
141  }
142
143  if(removeStep){  // if there was a step present
144    errmsg.str();
145    errmsg << "The subsection given is " << this->subsection <<".\n"
146           << "Duchamp is currently unable to deal with pixel steps"
147           << " in the subsection.\n"
148           << "These have been ignored, and so the subection used is ";
149  }
150
151  if(removeStep || doingBorders){
152    // rewrite subsection without any step sizes and with correct borders.
153   this->subsection = "[" + sections[0];
154   for(int str=1;str<numSections;str++)
155     this->subsection += ',' + sections[str];
156   this->subsection += "]";
157  }
158
159  if(removeStep){
160    errmsg << this->subsection << std::endl;
161    duchampWarning("Section::parse", errmsg.str());
162  }
163
164  return SUCCESS;
165 
166}
167//--------------------------------------------
Note: See TracBrowser for help on using the repository browser.