[164] | 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 | |
---|
[204] | 12 | void Param::setOffsets(struct wcsprm *wcs) |
---|
[164] | 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 | |
---|
| 24 | int 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 | |
---|
[168] | 43 | // Make sure the FITS file exists |
---|
| 44 | int exists; |
---|
[169] | 45 | fits_file_exists(this->imageFile.c_str(),&exists,&status); |
---|
[168] | 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 |
---|
[164] | 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){ |
---|
[168] | 66 | duchampWarning("verifySubsection","Error closing file: "); |
---|
[164] | 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 |
---|
[168] | 93 | <<" sections, whereas the FITS file has " << numAxes << " axes\n" |
---|
[164] | 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]; |
---|
[204] | 104 | this->sizeOffsets = numAxes; |
---|
[164] | 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++){ |
---|
[192] | 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 | } |
---|
[164] | 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; |
---|
[168] | 153 | duchampWarning("verifySubsection", errmsg.str()); |
---|
[164] | 154 | } |
---|
| 155 | |
---|
| 156 | } |
---|