source: tags/release-1.1/src/Utils/Section.cc @ 1323

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

Mostly adding the distribution text to the start of files, with a few additional comments added too.

File size: 6.3 KB
Line 
1// -----------------------------------------------------------------------
2// Section.cc: Member functions for the Section class, particularly
3//             how to interpret the subsection string.
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 <string>
30#include <vector>
31#include <iostream>
32#include <sstream>
33#include <Utils/Section.hh>
34#include <duchamp.hh>
35
36Section::Section(const Section& s)
37{
38  this->subsection  = s.subsection;
39  this->numSections = s.numSections;
40  this->starts      = s.starts;
41  this->dims        = s.dims;
42}
43//--------------------------------------------
44 
45Section& Section::operator= (const Section& s)
46{
47  if(this == &s) return *this;
48  this->subsection  = s.subsection;
49  this->numSections = s.numSections;
50  this->starts      = s.starts;
51  this->dims        = s.dims;
52  return *this;
53}
54//--------------------------------------------
55
56int Section::parse(std::vector<long> dimAxes)
57{
58  /**
59   * This function reads the subsection string, and registers the
60   * starting values and lengths for each dimension. The array of axis
61   * dimensions is needed to know how long each axis really is, and
62   * whether we have strayed over the limit or not.
63   *
64   * Note that steps in the subsection string are not dealt with -- a
65   * warning message is written to the screen, and the step values are
66   * removed from the subsection string.
67   *
68   * The function also does basic checks to make sure it is of the
69   * correct format (ie. has square brackets delimiting it) and the
70   * correct number of sections, returning a FAILURE if either of
71   * these checks fail.
72   *
73   * \param dimAxes The array of axis dimensions, of which the
74   *                 Section is a subsection.
75   * \return SUCCESS/FAILURE (from duchamp.hh)
76   */
77
78  std::stringstream errmsg;
79
80  // First Make sure subsection has [ and ] at ends
81  if((this->subsection[0]!='[') ||
82     (this->subsection[this->subsection.size()-1]!=']')){
83    errmsg.str("");
84    errmsg << "Subsection needs to be delimited by square brackets\n"
85           << "You provided: " << this->subsection << std::endl;
86    duchampError("Section parsing",errmsg.str());
87    return FAILURE;
88  }
89
90  this->starts.clear();
91  this->dims.clear();
92 
93  this->numSections=1;
94  for(int i=0;i<this->subsection.size();i++)
95    if(this->subsection[i]==',') this->numSections++;
96
97  if(numSections!=dimAxes.size()){
98    errmsg.str("");
99    errmsg << "Subsection has "<<numSections
100           <<" sections, compared to a cube with "
101           << dimAxes.size() << " axes\n"
102           << "Subsection provided was: " << this->subsection << std::endl;
103    duchampError("Section parsing",errmsg.str());
104    return FAILURE;
105  }
106
107  this->starts.resize(this->numSections);
108  this->dims.resize(this->numSections);
109
110  std::vector<std::string> sections(numSections);
111  // this will hold the section strings for each dimension
112  std::stringstream ss;
113  ss.str(this->subsection);
114  bool removeStep = false;
115  bool doingBorders = false;
116  std::string temp;
117
118  getline(ss,temp,'[');
119  for(int i=0;i<numSections-1;i++){
120    getline(ss,temp,',');
121    sections[i]=temp;
122  }
123  getline(ss,temp,']');
124  sections[numSections-1]=temp;
125
126  for(int str=0;str<numSections;str++){
127    if(sections[str]=="*"){
128      this->starts[str] = 0;
129      this->dims[str]= dimAxes[str];
130    }
131    else{
132      int numColon=0;
133      for(int i=0;i<sections[str].size();i++){
134        if(sections[str][i]==':'){
135          sections[str][i]=' ';
136          numColon++;
137        }
138      }
139      int a,b,c;
140      std::stringstream readString,fixedString;
141      readString.str(sections[str]);
142      switch(numColon){
143      case 1: // usual case
144        readString >> a >> b;
145        this->starts[str] = a-1;
146        this->dims[str] = b-a+1;
147        break;
148      case 0: // borders case -- so many off each border
149        readString >> a;
150        if(a>=dimAxes[str]/2){
151          errmsg.str("");
152          errmsg<< "You requested the subsection " << this->subsection
153                << " but axis #" << str+1
154                <<" has zero size, since its dimension is " << dimAxes[str]
155                <<".\nI'm not going to parse this! Go and fix it.\n";
156          duchampError("Section parsing", errmsg.str());
157          return FAILURE;
158        }
159        this->starts[str] = a;
160        this->dims[str] = dimAxes[str]-2*a;
161        fixedString << this->starts[str]+1 << ":"
162                    << this->getEnd(str)+1;
163        sections[str] = fixedString.str();
164        doingBorders=true;
165        break;
166      case 2: // subsection involves a step
167      default:
168        readString>> a >> b >> c;
169        this->starts[str] = a-1;
170        this->dims[str] = b-a+1;
171        fixedString << a << ":" << b;
172        sections[str] = fixedString.str();
173        removeStep=true;
174        break;
175      }
176
177    }
178  }
179
180  if(removeStep){  // if there was a step present
181    errmsg.str("");
182    errmsg << "The subsection given is " << this->subsection <<".\n"
183           << "Duchamp is currently unable to deal with pixel steps"
184           << " in the subsection.\n"
185           << "These have been ignored, and so the subection used is ";
186  }
187
188  if(removeStep || doingBorders){
189    // rewrite subsection without any step sizes and with correct borders.
190   this->subsection = "[" + sections[0];
191   for(int str=1;str<numSections;str++)
192     this->subsection += ',' + sections[str];
193   this->subsection += "]";
194  }
195
196  if(removeStep){
197    errmsg << this->subsection << std::endl;
198    duchampWarning("Section parsing", errmsg.str());
199  }
200
201  return SUCCESS;
202 
203}
204//--------------------------------------------
Note: See TracBrowser for help on using the repository browser.