source: trunk/src/FitsIO/subsection.cc @ 224

Last change on this file since 224 was 222, checked in by Matthew Whiting, 18 years ago

Large commit, but mostly documentation-oriented.

Only non-doc-related changes are:

  • To remove two deprecated files and any declarations of the functions in them
  • To move drawBlankEdges to the Cubes/ directory
  • Some small changes to the implementation of the StatsContainer? functions.
  • Creation of Utils/devel.hh to hide functions not used in Duchamp
  • To move the trimmedHist stats functions to their own file, again to hide them.
File size: 5.2 KB
Line 
1#include <iostream>
2#include <sstream>
3#include <string>
4#include <math.h>
5#include <wcs.h>
6#define WCSLIB_GETWCSTAB // define this so that we don't try and redefine
7                         //  wtbarr (this is a problem when using gcc v.4+
8#include <fitsio.h>
9#include <param.hh>
10#include <duchamp.hh>
11
12void Param::setOffsets(struct wcsprm *wcs)
13{
14  /**
15   * If there is a subsection being used, set the offset values according to the
16   * correct dimensions given by the WCS struct.
17   * If not, set the offsets to zero.
18   * \param wcs The WCSLIB wcsprm struct that defines which axis is which.
19   */
20  if(this->flagSubsection){ // if so, then the offsets array is defined.
21    this->xSubOffset = this->offsets[wcs->lng];
22    this->ySubOffset = this->offsets[wcs->lat];
23    this->zSubOffset = this->offsets[wcs->spec];
24  }
25  else{// else they should be 0
26    this->xSubOffset = this->ySubOffset = this->zSubOffset = 0;
27  }
28}
29
30int Param::verifySubsection()
31{
32  /**
33   *   Checks that the subsection string is in the appropriate format, with
34   *    the correct number of entries (one for each axis).
35   *   This involves reading the individual substrings and converting to
36   *    integers, and storing in the offsets array.
37   *   Steps in the subsection string are not dealt with -- a warning message
38   *    is written to the screen, and the step values are removed from the
39   *    subsection string.
40   */
41
42  // First open the requested FITS file and check its existence and
43  //  number of axes.
44  int numAxes,status = 0;  /* MUST initialize status */
45  fitsfile *fptr;         
46
47  // Make sure the FITS file exists
48  int exists;
49  fits_file_exists(this->imageFile.c_str(),&exists,&status);
50  if(exists<=0){
51    fits_report_error(stderr, status);
52    duchampWarning("verifySubsection", "Requested image does not exist!\n");
53    return FAILURE;
54  }
55  // Open the FITS file
56  if( fits_open_file(&fptr,this->imageFile.c_str(),READONLY,&status) ){
57    fits_report_error(stderr, status);
58    return FAILURE;
59  }
60  // Read the size information -- number of axes.
61  status = 0;
62  if(fits_get_img_dim(fptr, &numAxes, &status)){
63    fits_report_error(stderr, status);
64    return FAILURE;
65  }
66  // Close the FITS file.
67  status = 0;
68  fits_close_file(fptr, &status);
69  if (status){
70    duchampWarning("verifySubsection","Error closing file: ");
71    fits_report_error(stderr, status);
72  }
73
74  ///////////////////
75
76  // Make sure subsection has [ and ] at ends
77  if((this->subsection[0]!='[') ||
78     (this->subsection[this->subsection.size()-1]!=']')){
79    std::stringstream errmsg;
80    errmsg << "Subsection needs to be delimited by square brackets\n"
81           << "You provided: " << this->subsection << std::endl;
82    duchampError("verifySubsection",errmsg.str());
83    return FAILURE;
84  }
85
86  ///////////////////
87
88  // Make sure subsection has number of sections matching number of axes
89
90  int numSections=1;
91  for(int i=0;i<this->subsection.size();i++)
92    if(this->subsection[i]==',') numSections++;
93
94  if(numSections!=numAxes){
95    std::stringstream errmsg;
96    errmsg << "Subsection has "<<numSections
97           <<" sections, whereas the FITS file has " << numAxes << " axes\n"
98           << "Subsection provided was: " << this->subsection << std::endl;
99    duchampError("verifySubsection",errmsg.str());
100    return FAILURE;
101  }
102   
103  ///////////////////
104
105  // If we get to here, should have correct number of entries.
106
107  if(this->sizeOffsets>0) delete [] this->offsets;
108  this->offsets = new long[numAxes];
109  this->sizeOffsets = numAxes;
110
111  vector<string> sections(numSections); // this will hold the individual
112                                        //   section strings
113
114  std::stringstream ss;
115  ss.str(this->subsection);
116  bool removeStep = false;
117  string temp;
118
119  bool atEnd = false;
120  getline(ss,temp,'[');
121  for(int i=0;i<numSections-1;i++){
122    getline(ss,temp,',');
123    sections[i]=temp;
124  }
125  getline(ss,temp,']');
126  sections[numSections-1]=temp;
127
128  for(int str=0;str<numSections;str++){
129    if(sections[str]=="*") this->offsets[str] = 0;
130    else{
131      // if it is a genuine subsection and not everything.
132      int a = sections[str].find(':');     // first occurence of ':' in section
133      int b = sections[str].find(':',a+1); // location of second ':' - will be
134                                           //  -1 if there is no second ':'
135      this->offsets[str] = atoi( sections[str].substr(0,a).c_str() ) - 1;
136      // store the minimum pixel value in offsets array
137      if(b>0){ 
138        // if there is a step component, rewrite section string without
139        //  the step part.
140        sections[str] = sections[str].substr(0,b);
141        removeStep = true;
142      }
143    }
144  }
145
146  if(removeStep){  // if there was a step present
147    std::stringstream errmsg;
148    errmsg << "The subsection given is " << this->subsection <<".\n"
149           << "Duchamp is currently unable to deal with pixel steps"
150           << " in the subsection.\n"
151           << "These have been ignored, and so the subection used is ";
152    // rewrite subsection without any step sizes.
153   this->subsection = "[" + sections[0];
154   for(int str=1;str<numSections;str++)
155     this->subsection += ',' + sections[str];
156   this->subsection += "]";
157   errmsg << this->subsection << std::endl;
158   duchampWarning("verifySubsection", errmsg.str());
159  }
160
161}
Note: See TracBrowser for help on using the repository browser.