1 | #include <iostream> |
---|
2 | #include <string> |
---|
3 | #include <string.h> |
---|
4 | #include <wcsunits.h> |
---|
5 | #define WCSLIB_GETWCSTAB // define this so that we don't try and redefine |
---|
6 | // wtbarr (this is a problem when using gcc v.4+ |
---|
7 | #include <fitsio.h> |
---|
8 | #include <longnam.h> |
---|
9 | #include <math.h> |
---|
10 | #include <duchamp.hh> |
---|
11 | #include <Cubes/cubes.hh> |
---|
12 | |
---|
13 | int FitsHeader::readHeaderInfo(string fname, Param &par) |
---|
14 | { |
---|
15 | /** |
---|
16 | * FitsHeader::readHeaderInfo |
---|
17 | * A simple front-end function to the three header access |
---|
18 | * functions defined below. |
---|
19 | * |
---|
20 | */ |
---|
21 | |
---|
22 | int returnValue = SUCCESS; |
---|
23 | |
---|
24 | if(this->readBUNIT(fname)==FAILURE) returnValue=FAILURE; |
---|
25 | |
---|
26 | if(this->readBLANKinfo(fname, par)==FAILURE) returnValue=FAILURE; |
---|
27 | |
---|
28 | if(this->readBeamInfo(fname, par)==FAILURE) returnValue=FAILURE; |
---|
29 | |
---|
30 | return returnValue; |
---|
31 | } |
---|
32 | |
---|
33 | |
---|
34 | ////////////////////////////////////////////////// |
---|
35 | |
---|
36 | int FitsHeader::readBUNIT(string fname) |
---|
37 | { |
---|
38 | /** |
---|
39 | * FitsHeader::readBUNIT(string fname) |
---|
40 | * Read the BUNIT header keyword, to store the units |
---|
41 | * of brightness (flux). |
---|
42 | */ |
---|
43 | fitsfile *fptr; |
---|
44 | char *comment = new char[FLEN_COMMENT]; |
---|
45 | char *unit = new char[FLEN_VALUE]; |
---|
46 | int returnStatus = 0, status = 0; |
---|
47 | |
---|
48 | // Open the FITS file |
---|
49 | status = 0; |
---|
50 | if( fits_open_file(&fptr,fname.c_str(),READONLY,&status) ){ |
---|
51 | fits_report_error(stderr, status); |
---|
52 | return FAILURE; |
---|
53 | } |
---|
54 | |
---|
55 | // Read the BUNIT keyword, and translate to standard unit format if needs be |
---|
56 | fits_read_key(fptr, TSTRING, "BUNIT", unit, comment, &returnStatus); |
---|
57 | if (returnStatus){ |
---|
58 | duchampWarning("readBUNIT","Error reading BUNIT keyword: "); |
---|
59 | fits_report_error(stderr, returnStatus); |
---|
60 | } |
---|
61 | else{ |
---|
62 | wcsutrn(0,unit); |
---|
63 | this->fluxUnits = unit; |
---|
64 | } |
---|
65 | |
---|
66 | // Close the FITS file |
---|
67 | status = 0; |
---|
68 | fits_close_file(fptr, &status); |
---|
69 | if (status){ |
---|
70 | duchampWarning("readBUNIT","Error closing file: "); |
---|
71 | fits_report_error(stderr, status); |
---|
72 | } |
---|
73 | |
---|
74 | delete [] comment, unit; |
---|
75 | |
---|
76 | return returnStatus; |
---|
77 | } |
---|
78 | |
---|
79 | ////////////////////////////////////////////////// |
---|
80 | |
---|
81 | int FitsHeader::readBLANKinfo(string fname, Param &par) |
---|
82 | { |
---|
83 | /** |
---|
84 | * FitsHeader::readBLANKinfo(string fname, Param &par) |
---|
85 | * Reading in the Blank pixel value keywords. |
---|
86 | * If the BLANK keyword is in the header, use that and store the relevant |
---|
87 | * values. Also copy them into the parameter set. |
---|
88 | * If not, use the default value (either the default from param.cc or |
---|
89 | * from the param file) and assume simple values for the keywords |
---|
90 | * --> the scale keyword is the same as the blank value, |
---|
91 | * the blank keyword (which is an int) is 1 and |
---|
92 | * the bzero (offset) is 0. |
---|
93 | */ |
---|
94 | int returnStatus = 0, status = 0; |
---|
95 | if(par.getFlagBlankPix()){ // Only do this if we want the blank pix value |
---|
96 | |
---|
97 | fitsfile *fptr; |
---|
98 | char *comment = new char[FLEN_COMMENT]; |
---|
99 | int blank; |
---|
100 | float bscale, bzero; |
---|
101 | |
---|
102 | // Open the FITS file. |
---|
103 | if( fits_open_file(&fptr,fname.c_str(),READONLY,&status) ){ |
---|
104 | fits_report_error(stderr, status); |
---|
105 | return FAILURE; |
---|
106 | } |
---|
107 | |
---|
108 | // Read the BLANK keyword. |
---|
109 | // If this isn't present, then report the error, and set to |
---|
110 | // the nominal values given in Param |
---|
111 | // If it is, read the other two necessary keywords, and then set |
---|
112 | // the values accordingly. |
---|
113 | if(fits_read_key(fptr, TINT, "BLANK", &blank, comment, &returnStatus)){ |
---|
114 | duchampWarning("readBLANKinfo","Error reading BLANK keyword: "); |
---|
115 | fits_report_error(stderr, returnStatus); |
---|
116 | std::stringstream errmsg; |
---|
117 | errmsg << "Using default BLANK value (" |
---|
118 | << par.getBlankPixVal() << ").\n"; |
---|
119 | duchampWarning("readBLANKinfo", errmsg.str()); |
---|
120 | this->blankKeyword = 1; |
---|
121 | this->bscaleKeyword = par.getBlankPixVal(); |
---|
122 | this->bzeroKeyword = 0; |
---|
123 | par.setBlankKeyword(1); |
---|
124 | par.setBzeroKeyword(0); |
---|
125 | par.setFlagUsingBlank(true); |
---|
126 | } |
---|
127 | else{ |
---|
128 | status = 0; |
---|
129 | fits_read_key(fptr, TFLOAT, "BZERO", &bzero, comment, &status); |
---|
130 | status = 0; |
---|
131 | fits_read_key(fptr, TFLOAT, "BSCALE", &bscale, NULL, &status); |
---|
132 | this->blankKeyword = blank; |
---|
133 | this->bscaleKeyword = bscale; |
---|
134 | this->bzeroKeyword = bzero; |
---|
135 | par.setBlankKeyword(blank); |
---|
136 | par.setBscaleKeyword(bscale); |
---|
137 | par.setBzeroKeyword(bzero); |
---|
138 | par.setBlankPixVal( blank*bscale + bzero ); |
---|
139 | } |
---|
140 | |
---|
141 | // Close the FITS file. |
---|
142 | status = 0; |
---|
143 | fits_close_file(fptr, &status); |
---|
144 | if (status){ |
---|
145 | duchampWarning("readBLANKinfo","Error closing file: "); |
---|
146 | fits_report_error(stderr, status); |
---|
147 | } |
---|
148 | |
---|
149 | delete [] comment; |
---|
150 | |
---|
151 | } |
---|
152 | |
---|
153 | return returnStatus; |
---|
154 | |
---|
155 | } |
---|
156 | |
---|
157 | ////////////////////////////////////////////////// |
---|
158 | |
---|
159 | int FitsHeader::readBeamInfo(string fname, Param &par) |
---|
160 | { |
---|
161 | /** |
---|
162 | * FitsHeader::readBeamInfo(string fname, Param &par) |
---|
163 | * Reading in the beam parameters from the header. |
---|
164 | * Use these, plus the basic WCS parameters to calculate the size of |
---|
165 | * the beam in pixels. Copy the beam size into the parameter set. |
---|
166 | * If information not present in FITS header, use the parameter |
---|
167 | * set to define the beam size. |
---|
168 | */ |
---|
169 | char *comment = new char[80]; |
---|
170 | string keyword[4]={"BMAJ","BMIN","CDELT1","CDELT2"}; |
---|
171 | float bmaj,bmin,cdelt1,cdelt2; |
---|
172 | int status[6]; |
---|
173 | fitsfile *fptr; |
---|
174 | |
---|
175 | for(int i=0;i<6;i++) status[i] = 0; |
---|
176 | |
---|
177 | // Open the FITS file |
---|
178 | if( fits_open_file(&fptr,fname.c_str(),READONLY,&status[0]) ){ |
---|
179 | fits_report_error(stderr, status[0]); |
---|
180 | return FAILURE; |
---|
181 | } |
---|
182 | |
---|
183 | // Read the Keywords -- first look for BMAJ. If it is present, read the |
---|
184 | // others, and calculate the beam size. |
---|
185 | // If it is not, give warning and set beam size to nominal value. |
---|
186 | fits_read_key(fptr, TFLOAT, (char *)keyword[0].c_str(), &bmaj, |
---|
187 | comment, &status[1]); |
---|
188 | fits_read_key(fptr, TFLOAT, (char *)keyword[1].c_str(), &bmin, |
---|
189 | comment, &status[2]); |
---|
190 | fits_read_key(fptr, TFLOAT, (char *)keyword[2].c_str(), &cdelt1, |
---|
191 | comment, &status[3]); |
---|
192 | fits_read_key(fptr, TFLOAT, (char *)keyword[3].c_str(), &cdelt2, |
---|
193 | comment, &status[4]); |
---|
194 | |
---|
195 | if(status[1]||status[2]||status[3]||status[4]){ // error |
---|
196 | stringstream errmsg; |
---|
197 | errmsg << "Header keywords not present: "; |
---|
198 | for(int i=0;i<4;i++) if(status[i+1]) errmsg<<keyword[i]<<" "; |
---|
199 | errmsg << "\nUsing parameter beamSize to determine size of beam.\n"; |
---|
200 | duchampWarning("readBeamInfo",errmsg.str()); |
---|
201 | this->setBeamSize(par.getBeamSize()); |
---|
202 | par.setFlagUsingBeam(true); |
---|
203 | } |
---|
204 | else{ // all keywords present |
---|
205 | this->setBeamSize( M_PI * (bmaj/2.) * (bmin/2.) / fabs(cdelt1*cdelt2) ); |
---|
206 | this->setBmajKeyword(bmaj); |
---|
207 | this->setBminKeyword(bmin); |
---|
208 | par.setBeamSize(this->beamSize); |
---|
209 | } |
---|
210 | |
---|
211 | // Close the FITS file. |
---|
212 | fits_close_file(fptr, &status[5]); |
---|
213 | if (status[5]){ |
---|
214 | duchampWarning("readBeamInfo","Error closing file: "); |
---|
215 | fits_report_error(stderr, status[5]); |
---|
216 | } |
---|
217 | |
---|
218 | delete [] comment; |
---|
219 | |
---|
220 | int returnStatus = status[0]; |
---|
221 | for(int i=1;i<6;i++) if(status[i]>returnStatus) returnStatus=status[i]; |
---|
222 | |
---|
223 | return returnStatus; |
---|
224 | } |
---|