source: tags/release-1.0.5/src/FitsIO/subsection.cc @ 1455

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

Minor typo fixed in FitsIO/subsection.cc.

File size: 4.7 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(wcsprm *wcs)
13{
14  if(this->flagSubsection){ // if so, then the offsets array is defined.
15    this->xSubOffset = this->offsets[wcs->lng];
16    this->ySubOffset = this->offsets[wcs->lat];
17    this->zSubOffset = this->offsets[wcs->spec];
18  }
19  else{// else they should be 0
20    this->xSubOffset = this->ySubOffset = this->zSubOffset = 0;
21  }
22}
23
24int Param::verifySubsection()
25{
26  /**
27   *  Param::verifySubsection()
28   *
29   *   Checks that the subsection string is in the appropriate format, with
30   *    the correct number of entries (one for each axis).
31   *   This involves reading the individual substrings and converting to
32   *    integers, and storing in the offsets array.
33   *   Steps in the subsection string are not dealt with -- a warning message
34   *    is written to the screen, and the step values are removed from the
35   *    subsection string.
36   */
37
38  // First open the requested FITS file and check its existence and
39  //  number of axes.
40  int numAxes,status = 0;  /* MUST initialize status */
41  fitsfile *fptr;         
42
43  // Make sure the FITS file exists
44  int exists;
45  fits_file_exists(this->imageFile.c_str(),&exists,&status);
46  if(exists<=0){
47    fits_report_error(stderr, status);
48    duchampWarning("verifySubsection", "Requested image does not exist!\n");
49    return FAILURE;
50  }
51  // Open the FITS file
52  if( fits_open_file(&fptr,this->imageFile.c_str(),READONLY,&status) ){
53    fits_report_error(stderr, status);
54    return FAILURE;
55  }
56  // Read the size information -- number of axes.
57  status = 0;
58  if(fits_get_img_dim(fptr, &numAxes, &status)){
59    fits_report_error(stderr, status);
60    return FAILURE;
61  }
62  // Close the FITS file.
63  status = 0;
64  fits_close_file(fptr, &status);
65  if (status){
66    duchampWarning("verifySubsection","Error closing file: ");
67    fits_report_error(stderr, status);
68  }
69
70  ///////////////////
71
72  // Make sure subsection has [ and ] at ends
73  if((this->subsection[0]!='[') ||
74     (this->subsection[this->subsection.size()-1]!=']')){
75    std::stringstream errmsg;
76    errmsg << "Subsection needs to be delimited by square brackets\n"
77           << "You provided: " << this->subsection << std::endl;
78    duchampError("verifySubsection",errmsg.str());
79    return FAILURE;
80  }
81
82  ///////////////////
83
84  // Make sure subsection has number of sections matching number of axes
85
86  int numSections=1;
87  for(int i=0;i<this->subsection.size();i++)
88    if(this->subsection[i]==',') numSections++;
89
90  if(numSections!=numAxes){
91    std::stringstream errmsg;
92    errmsg << "Subsection has "<<numSections
93           <<" sections, whereas the FITS file has " << numAxes << " axes\n"
94           << "Subsection provided was: " << this->subsection << std::endl;
95    duchampError("verifySubsection",errmsg.str());
96    return FAILURE;
97  }
98   
99  ///////////////////
100
101  // If we get to here, should have correct number of entries.
102
103  this->offsets = new long[numAxes];
104
105  vector<string> sections(numSections); // this will hold the individual
106                                        //   section strings
107
108  std::stringstream ss;
109  ss.str(this->subsection);
110  bool removeStep = false;
111  string temp;
112
113  bool atEnd = false;
114  getline(ss,temp,'[');
115  for(int i=0;i<numSections-1;i++){
116    getline(ss,temp,',');
117    sections[i]=temp;
118  }
119  getline(ss,temp,']');
120  sections[numSections-1]=temp;
121
122  for(int str=0;str<numSections;str++){
123    int a = sections[str].find(':');     // first occurence of ':' in section
124    int b = sections[str].find(':',a+1); // location of second ':' -- will be
125                                         //  -1 if there is no second occurence
126    this->offsets[str] = atoi( sections[str].substr(0,a).c_str() ) - 1;
127    // store the minimum pixel value in offsets array
128    if(b>0){ 
129      // if there is a step component, rewrite section string without
130      //  the step part.
131      sections[str] = sections[str].substr(0,b);
132      removeStep = true;
133    }
134  }
135
136  if(removeStep){  // if there was a step present
137    std::stringstream errmsg;
138    errmsg << "The subsection given is " << this->subsection <<".\n"
139           << "Duchamp is currently unable to deal with pixel steps"
140           << " in the subsection.\n"
141           << "These have been ignored, and so the subection used is ";
142    // rewrite subsection without any step sizes.
143   this->subsection = "[" + sections[0];
144   for(int str=1;str<numSections;str++)
145     this->subsection += ',' + sections[str];
146   this->subsection += "]";
147   errmsg << this->subsection << std::endl;
148   duchampWarning("verifySubsection", errmsg.str());
149  }
150
151}
Note: See TracBrowser for help on using the repository browser.