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

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

A large commit, based on improving memory usage, allocation, etc:

  • src/param.hh :
    • Added a delete command for the offsets array in Param. Keep track of size via new sizeOffsets variable.
    • Changed "wcsprm" to "struct wcsprm", for clarity.
    • Put wcsvfree in the FitsHeader? destructor so that memory is deallocated correctly.
  • src/param.cc :
    • Improved the FitsHeader? constructor functions, so that memory for the wcsprm structures is allocated appropriately.
    • Other wcsprm-related tweaks.
    • Included code for sizeOffsets -- the size of the offsets array in Param, so that we can properly deallocate its memory in the destructor function.
  • src/FitsIO/subsection.cc : Changed "wcsprm" to "struct wcsprm", for clarity, and added a sizeOffsets to track the memory allocation for offsets.
  • src/FitsIO/dataIO.cc : renamed the local variable array to pixarray so that there is no confusion. Added delete[] commands for local arrays.
  • src/FitsIO/wcsIO.cc : Improved the struct wcsprm memory allocation. Now using a local wcs variable so that we don't get confusion with the FitsHeader? one.
  • src/Utils/wcsFunctions.cc : changed "wcsprm" to "struct wcsprm", for clarity.
  • src/Cubes/CubicSearch.cc : removed two allocation calls (to new) that were not needed, as well as unused commented-out code.
  • src/Cubes/plotting.cc :
    • Corrected the way the detection map is worked out and the scale bar range calculated.
    • Changed "wcsprm" to "struct wcsprm", for clarity.
  • src/duchamp.hh : better implementation of the rewind() and remove() functions for ProgressBar?
  • src/Utils/getStats.cc : minor diffs
  • src/Utils/utils.hh : changed prototypes
  • src/Cubes/cubes.cc : Changed the way the setCubeStats() function works, so that stats aren't needlessly calculated if the threshold has already been specified.
  • src/Cubes/cubes.hh : minor presentation changes
  • src/Cubes/drawMomentCutout.cc : Tried to improve the scale-bar drawing function, to cope with very high angular resolution data. Not quite working properly yet.
  • src/Cubes/outputSpectra.cc : Corrected the flux labels so that the appropriate units are used, and not just Jy or Jy/beam.
File size: 4.9 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  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  this->sizeOffsets = numAxes;
105
106  vector<string> sections(numSections); // this will hold the individual
107                                        //   section strings
108
109  std::stringstream ss;
110  ss.str(this->subsection);
111  bool removeStep = false;
112  string temp;
113
114  bool atEnd = false;
115  getline(ss,temp,'[');
116  for(int i=0;i<numSections-1;i++){
117    getline(ss,temp,',');
118    sections[i]=temp;
119  }
120  getline(ss,temp,']');
121  sections[numSections-1]=temp;
122
123  for(int str=0;str<numSections;str++){
124    if(sections[str]=="*") this->offsets[str] = 0;
125    else{
126      // if it is a genuine subsection and not everything.
127      int a = sections[str].find(':');     // first occurence of ':' in section
128      int b = sections[str].find(':',a+1); // location of second ':' - will be
129                                           //  -1 if there is no second ':'
130      this->offsets[str] = atoi( sections[str].substr(0,a).c_str() ) - 1;
131      // store the minimum pixel value in offsets array
132      if(b>0){ 
133        // if there is a step component, rewrite section string without
134        //  the step part.
135        sections[str] = sections[str].substr(0,b);
136        removeStep = true;
137      }
138    }
139  }
140
141  if(removeStep){  // if there was a step present
142    std::stringstream errmsg;
143    errmsg << "The subsection given is " << this->subsection <<".\n"
144           << "Duchamp is currently unable to deal with pixel steps"
145           << " in the subsection.\n"
146           << "These have been ignored, and so the subection used is ";
147    // rewrite subsection without any step sizes.
148   this->subsection = "[" + sections[0];
149   for(int str=1;str<numSections;str++)
150     this->subsection += ',' + sections[str];
151   this->subsection += "]";
152   errmsg << this->subsection << std::endl;
153   duchampWarning("verifySubsection", errmsg.str());
154  }
155
156}
Note: See TracBrowser for help on using the repository browser.