[299] | 1 | // ----------------------------------------------------------------------- |
---|
| 2 | // saveImage.cc: Write a wavelet-reconstructed or smoothed array to a |
---|
| 3 | // FITS file. |
---|
| 4 | // ----------------------------------------------------------------------- |
---|
| 5 | // Copyright (C) 2006, Matthew Whiting, ATNF |
---|
| 6 | // |
---|
| 7 | // This program is free software; you can redistribute it and/or modify it |
---|
| 8 | // under the terms of the GNU General Public License as published by the |
---|
| 9 | // Free Software Foundation; either version 2 of the License, or (at your |
---|
| 10 | // option) any later version. |
---|
| 11 | // |
---|
| 12 | // Duchamp is distributed in the hope that it will be useful, but WITHOUT |
---|
| 13 | // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
---|
| 14 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
---|
| 15 | // for more details. |
---|
| 16 | // |
---|
| 17 | // You should have received a copy of the GNU General Public License |
---|
| 18 | // along with Duchamp; if not, write to the Free Software Foundation, |
---|
| 19 | // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA |
---|
| 20 | // |
---|
| 21 | // Correspondence concerning Duchamp may be directed to: |
---|
| 22 | // Internet email: Matthew.Whiting [at] atnf.csiro.au |
---|
| 23 | // Postal address: Dr. Matthew Whiting |
---|
| 24 | // Australia Telescope National Facility, CSIRO |
---|
| 25 | // PO Box 76 |
---|
| 26 | // Epping NSW 1710 |
---|
| 27 | // AUSTRALIA |
---|
| 28 | // ----------------------------------------------------------------------- |
---|
[3] | 29 | #include <iostream> |
---|
| 30 | #include <sstream> |
---|
| 31 | #include <string> |
---|
[643] | 32 | #include <string.h> |
---|
[394] | 33 | #include <wcslib/wcs.h> |
---|
| 34 | #include <wcslib/wcshdr.h> |
---|
[757] | 35 | #include <wcslib/wcsunits.h> |
---|
[146] | 36 | #define WCSLIB_GETWCSTAB |
---|
| 37 | // define this so that we don't try and redefine wtbarr |
---|
| 38 | // (this is a problem when using cfitsio v.3 and g++ v.4) |
---|
| 39 | |
---|
[66] | 40 | #include <fitsio.h> |
---|
[393] | 41 | #include <duchamp/duchamp.hh> |
---|
| 42 | #include <duchamp/Cubes/cubes.hh> |
---|
| 43 | #include <duchamp/PixelMap/Voxel.hh> |
---|
| 44 | #include <duchamp/PixelMap/Object3D.hh> |
---|
[3] | 45 | |
---|
[378] | 46 | namespace duchamp |
---|
[208] | 47 | { |
---|
| 48 | |
---|
[528] | 49 | /// @brief Write FITS headers in correct format for reconstructed array output |
---|
[378] | 50 | void writeReconHeaderInfo(fitsfile *fptr, Param &par, std::string nature); |
---|
[208] | 51 | |
---|
[528] | 52 | /// @brief Write FITS headers in correct format for smoothed array output |
---|
[378] | 53 | void writeSmoothHeaderInfo(fitsfile *fptr, Param &par); |
---|
[208] | 54 | |
---|
[528] | 55 | /// @brief Write FITS headers in correct format for mask array output |
---|
[379] | 56 | void writeMaskHeaderInfo(fitsfile *fptr, Param &par); |
---|
[670] | 57 | |
---|
| 58 | /// @brief Write FITS headers in correct format for moment-0 array output |
---|
| 59 | void writeMomentMapHeaderInfo(fitsfile *fptr, Param &par); |
---|
| 60 | |
---|
[379] | 61 | //--------------------------------------------------------------------------- |
---|
| 62 | |
---|
[670] | 63 | char *numerateKeyword(std::string key, int num) |
---|
| 64 | { |
---|
| 65 | /// @details A utility function to combine a keyword and a |
---|
| 66 | /// value, to produce a relevant FITS keyword for a given |
---|
| 67 | /// axis. For example numerateKeyword(CRPIX,1) returns CRPIX1. |
---|
| 68 | std::stringstream ss; |
---|
| 69 | ss << key << num; |
---|
| 70 | return (char *)ss.str().c_str(); |
---|
| 71 | } |
---|
| 72 | |
---|
[695] | 73 | void duchampFITSerror(int status, std::string subroutine, std::string error) |
---|
[670] | 74 | { |
---|
[695] | 75 | if(status){ |
---|
| 76 | duchampError(subroutine,error); |
---|
| 77 | fits_report_error(stderr, status); |
---|
| 78 | } |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | |
---|
| 82 | OUTCOME Cube::saveMomentMapImage() |
---|
| 83 | { |
---|
[670] | 84 | /// @details |
---|
| 85 | /// A function to save the moment-0 map to a FITS file. |
---|
| 86 | |
---|
| 87 | int newbitpix = FLOAT_IMG; |
---|
| 88 | char *comment = new char[FLEN_COMMENT]; |
---|
| 89 | strcpy(comment,""); |
---|
| 90 | long *fpixel = new long[2]; |
---|
| 91 | for(int i=0;i<2;i++) fpixel[i]=1; |
---|
| 92 | int status = 0; /* MUST initialize status */ |
---|
| 93 | fitsfile *fptr; |
---|
| 94 | |
---|
| 95 | std::string fileout = "!" + this->par.outputMomentMapFile(); |
---|
| 96 | // the ! is there so that it writes over an existing file. |
---|
| 97 | char *keyword = new char[FLEN_KEYWORD]; |
---|
| 98 | |
---|
| 99 | status = 0; |
---|
[695] | 100 | if(fits_create_file(&fptr,fileout.c_str(),&status)){ |
---|
| 101 | duchampFITSerror(status,"saveMomentMapImage","Error creating file:"); |
---|
| 102 | return FAILURE; |
---|
[670] | 103 | } |
---|
| 104 | else { |
---|
| 105 | status = 0; |
---|
| 106 | long *dim = new long[2]; |
---|
| 107 | for (uint i = 0; i < 2; i++) dim[i] = this->axisDim[i]; |
---|
| 108 | if (fits_create_img(fptr, newbitpix, 2, dim, &status)) { |
---|
[695] | 109 | duchampFITSerror(status,"saveMomentMapImage","Error creating image:"); |
---|
[670] | 110 | } |
---|
| 111 | |
---|
[788] | 112 | this->head.beam().writeToFITS(fptr); |
---|
| 113 | |
---|
[670] | 114 | status = 0; |
---|
| 115 | strcpy(keyword,"EQUINOX"); |
---|
| 116 | if (fits_update_key(fptr, TFLOAT, keyword, &this->head.WCS().equinox, NULL, &status)){ |
---|
[695] | 117 | duchampFITSerror(status,"saveMomentMapImage","Error saving equinox:"); |
---|
[670] | 118 | } |
---|
| 119 | status = 0; |
---|
| 120 | strcpy(keyword,"BUNIT"); |
---|
[694] | 121 | if (fits_update_key(fptr, TSTRING, keyword, (char *)this->head.getIntFluxUnits().c_str(), NULL, &status)){ |
---|
[695] | 122 | duchampFITSerror(status,"saveMomentMapImage","Error saving BUNIT:"); |
---|
[670] | 123 | } |
---|
| 124 | float val; |
---|
| 125 | int axisNumbers[2]; |
---|
| 126 | axisNumbers[0] = this->head.WCS().lng; |
---|
| 127 | axisNumbers[1] = this->head.WCS().lat; |
---|
| 128 | for (uint d = 0; d < 2; d++) { |
---|
| 129 | int i = axisNumbers[d]; |
---|
| 130 | status = 0; |
---|
| 131 | if (fits_update_key(fptr, TSTRING, numerateKeyword("CTYPE", d + 1), this->head.WCS().ctype[i], NULL, &status)){ |
---|
[695] | 132 | duchampFITSerror(status,"saveMomentMapImage","Error saving CTYPE:"); |
---|
[670] | 133 | } |
---|
| 134 | status = 0; |
---|
| 135 | if (fits_update_key(fptr, TSTRING, numerateKeyword("CUNIT", d + 1), this->head.WCS().cunit[i], NULL, &status)){ |
---|
[695] | 136 | duchampFITSerror(status,"saveMomentMapImage","Error saving CUNIT:"); |
---|
[670] | 137 | } |
---|
| 138 | status = 0; |
---|
| 139 | val = this->head.WCS().crval[i]; |
---|
| 140 | if (fits_update_key(fptr, TFLOAT, numerateKeyword("CRVAL", d + 1), &val, NULL, &status)){ |
---|
[695] | 141 | duchampFITSerror(status,"saveMomentMapImage","Error saving CRVAL:"); |
---|
[670] | 142 | } |
---|
| 143 | val = this->head.WCS().cdelt[i]; |
---|
| 144 | status = 0; |
---|
| 145 | if (fits_update_key(fptr, TFLOAT, numerateKeyword("CDELT", d + 1), &val, NULL, &status)){ |
---|
[695] | 146 | duchampFITSerror(status,"saveMomentMapImage","Error saving CDELT:"); |
---|
[670] | 147 | } |
---|
| 148 | val = this->head.WCS().crpix[i]; |
---|
| 149 | status = 0; |
---|
| 150 | if (fits_update_key(fptr, TFLOAT, numerateKeyword("CRPIX", d + 1), &val, NULL, &status)){ |
---|
[695] | 151 | duchampFITSerror(status,"saveMomentMapImage","Error saving CRPIX:"); |
---|
[670] | 152 | } |
---|
| 153 | val = this->head.WCS().crota[i]; |
---|
| 154 | status = 0; |
---|
| 155 | if (fits_update_key(fptr, TFLOAT, numerateKeyword("CROTA", d + 1), &val, NULL, &status)){ |
---|
[695] | 156 | duchampFITSerror(status,"saveMomentMapImage","Error saving CROTA:"); |
---|
[670] | 157 | } |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | delete [] comment; |
---|
| 161 | delete [] keyword; |
---|
| 162 | |
---|
| 163 | writeMomentMapHeaderInfo(fptr, this->par); |
---|
| 164 | |
---|
| 165 | long size = this->axisDim[0] * this->axisDim[1]; |
---|
| 166 | float *momentMap = new float[size]; |
---|
| 167 | std::vector<bool> detectionMap; |
---|
| 168 | this->getMomentMap(momentMap,detectionMap); |
---|
| 169 | status=0; |
---|
| 170 | if(fits_write_pix(fptr, TFLOAT, fpixel, size, momentMap, &status)){ |
---|
[695] | 171 | duchampFITSerror(status,"saveMomentMapImage","Error writing data"); |
---|
[670] | 172 | } |
---|
| 173 | status = 0; |
---|
| 174 | if(fits_close_file(fptr, &status)){ |
---|
[695] | 175 | duchampFITSerror(status,"saveMomentMapImage","Error closing file"); |
---|
[670] | 176 | } |
---|
| 177 | |
---|
| 178 | delete [] momentMap; |
---|
| 179 | |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | delete [] fpixel; |
---|
| 183 | |
---|
[695] | 184 | return SUCCESS; |
---|
[670] | 185 | |
---|
| 186 | } |
---|
| 187 | |
---|
[695] | 188 | OUTCOME Cube::saveMaskCube() |
---|
[379] | 189 | { |
---|
[528] | 190 | /// @details |
---|
| 191 | /// A function to save a mask to a FITS file, indicating where the |
---|
| 192 | /// detections where made. The value of the detected pixels is |
---|
| 193 | /// determined by the flagMaskWithObjectNum parameter: if true, |
---|
| 194 | /// the value of the pixels is given by the corresponding object |
---|
| 195 | /// ID number; if false, they take the value 1 for all |
---|
| 196 | /// objects. Pixels not in a detected object have the value 0. |
---|
[379] | 197 | |
---|
| 198 | int newbitpix = SHORT_IMG; |
---|
[643] | 199 | char *comment = new char[FLEN_COMMENT]; |
---|
[644] | 200 | strcpy(comment,""); |
---|
[661] | 201 | long *fpixel = new long[this->header().WCS().naxis]; |
---|
[385] | 202 | for(int i=0;i<this->header().WCS().naxis;i++) fpixel[i]=1; |
---|
[379] | 203 | int status = 0; /* MUST initialize status */ |
---|
| 204 | fitsfile *fptrOld, *fptrNew; |
---|
[695] | 205 | if(fits_open_file(&fptrOld,this->par.getImageFile().c_str(),READONLY,&status)){ |
---|
| 206 | duchampFITSerror(status,"saveMask","Error opening existing file:"); |
---|
| 207 | return FAILURE; |
---|
[379] | 208 | } |
---|
| 209 | |
---|
| 210 | std::string fileout = "!" + this->par.outputMaskFile(); |
---|
| 211 | // the ! is there so that it writes over an existing file. |
---|
| 212 | |
---|
| 213 | status = 0; |
---|
[695] | 214 | if(fits_create_file(&fptrNew,fileout.c_str(),&status)){ |
---|
| 215 | duchampFITSerror(status,"saveMask","Error creating file:"); |
---|
| 216 | return FAILURE; |
---|
[379] | 217 | } |
---|
| 218 | else { |
---|
| 219 | status = 0; |
---|
[695] | 220 | if(fits_movabs_hdu(fptrOld, 1, NULL, &status)){ |
---|
| 221 | duchampFITSerror(status,"saveMask","Error accessing existing file's header:"); |
---|
| 222 | return FAILURE; |
---|
[379] | 223 | } |
---|
| 224 | status = 0; |
---|
| 225 | fits_copy_header(fptrOld, fptrNew, &status); |
---|
| 226 | if (status){ |
---|
[695] | 227 | duchampFITSerror(status,"saveMask","Error copying header to new file:"); |
---|
[379] | 228 | fits_report_error(stderr, status); |
---|
[695] | 229 | return FAILURE; |
---|
[379] | 230 | } |
---|
| 231 | status = 0; |
---|
[644] | 232 | char *keyword = new char[FLEN_KEYWORD]; |
---|
| 233 | strcpy(keyword,"BITPIX"); |
---|
| 234 | strcpy(comment,"number of bits per data pixel"); |
---|
[695] | 235 | if(fits_update_key(fptrNew, TINT, keyword, &newbitpix, comment, &status)){ |
---|
| 236 | duchampFITSerror(status,"saveMask","Error writing BITPIX header:"); |
---|
[379] | 237 | } |
---|
| 238 | float bscale=1., bzero=0.; |
---|
[644] | 239 | strcpy(comment,""); |
---|
| 240 | strcpy(keyword,"BSCALE"); |
---|
[695] | 241 | if(fits_update_key(fptrNew, TFLOAT, keyword, &bscale, comment, &status)){ |
---|
| 242 | duchampFITSerror(status,"saveMask","Error writing BSCALE header:"); |
---|
[379] | 243 | } |
---|
[644] | 244 | strcpy(keyword,"BZERO"); |
---|
[695] | 245 | if(fits_update_key(fptrNew, TFLOAT, keyword, &bzero, comment, &status)){ |
---|
| 246 | duchampFITSerror(status,"saveMask","Error writing BZERO header:"); |
---|
[644] | 247 | } |
---|
[695] | 248 | if(fits_set_bscale(fptrNew, 1, 0, &status)){ |
---|
| 249 | duchampFITSerror(status,"saveMask","Error setting scale:"); |
---|
[644] | 250 | } |
---|
[521] | 251 | std::string newunits; |
---|
| 252 | if(this->par.getFlagMaskWithObjectNum()) |
---|
| 253 | newunits = "Object ID"; |
---|
| 254 | else |
---|
| 255 | newunits = "Detection flag"; |
---|
[644] | 256 | strcpy(keyword,"BUNIT"); |
---|
[695] | 257 | if(fits_update_key(fptrNew, TSTRING, keyword, (char *)newunits.c_str(), comment, &status)){ |
---|
| 258 | duchampFITSerror(status,"saveMask","Error writing BUNIT header:"); |
---|
[521] | 259 | } |
---|
[379] | 260 | long dud; |
---|
| 261 | // Need to correct the dimensions, if we have subsectioned the image |
---|
| 262 | if(this->par.getFlagSubsection()){ |
---|
[385] | 263 | std::stringstream naxis; |
---|
| 264 | fits_read_key(fptrOld, TLONG, (char *)naxis.str().c_str(), &dud, comment, &status); |
---|
| 265 | fits_update_key(fptrNew, TLONG, (char *)naxis.str().c_str(), |
---|
[379] | 266 | &(this->axisDim[0]), comment, &status); |
---|
[385] | 267 | naxis.str(""); |
---|
| 268 | naxis << "NAXIS" << this->head.WCS().lat; |
---|
| 269 | fits_read_key(fptrOld, TLONG, (char *)naxis.str().c_str(), &dud, comment, &status); |
---|
| 270 | fits_update_key(fptrNew, TLONG, (char *)naxis.str().c_str(), |
---|
[379] | 271 | &(this->axisDim[1]), comment, &status); |
---|
[385] | 272 | naxis.str(""); |
---|
| 273 | naxis << "NAXIS" << this->head.WCS().spec; |
---|
| 274 | fits_read_key(fptrOld, TLONG, (char *)naxis.str().c_str(), &dud, comment, &status); |
---|
| 275 | fits_update_key(fptrNew, TLONG, (char *)naxis.str().c_str(), |
---|
[379] | 276 | &(this->axisDim[2]), comment, &status); |
---|
| 277 | } |
---|
| 278 | |
---|
| 279 | delete [] comment; |
---|
[661] | 280 | delete [] keyword; |
---|
[379] | 281 | |
---|
| 282 | writeMaskHeaderInfo(fptrNew, this->par); |
---|
| 283 | |
---|
| 284 | short *mask = new short[this->numPixels]; |
---|
| 285 | for(int i=0;i<this->numPixels;i++) mask[i]=0; |
---|
[623] | 286 | std::vector<Detection>::iterator obj; |
---|
| 287 | for(obj=this->objectList->begin();obj<this->objectList->end();obj++){ |
---|
| 288 | std::vector<PixelInfo::Voxel> voxlist = obj->getPixelSet(); |
---|
| 289 | std::vector<PixelInfo::Voxel>::iterator vox; |
---|
| 290 | for(vox=voxlist.begin();vox<voxlist.end();vox++){ |
---|
| 291 | int pixelpos = vox->getX() + this->axisDim[0]*vox->getY() + |
---|
| 292 | this->axisDim[0]*this->axisDim[1]*vox->getZ(); |
---|
| 293 | if(this->par.getFlagMaskWithObjectNum()) mask[pixelpos] = obj->getID(); |
---|
[521] | 294 | else mask[pixelpos] = 1; |
---|
[379] | 295 | } |
---|
| 296 | } |
---|
| 297 | status=0; |
---|
[695] | 298 | if(fits_write_pix(fptrNew, TSHORT, fpixel, this->numPixels, mask, &status)){ |
---|
| 299 | duchampFITSerror(status,"saveMask","Error writing mask array:"); |
---|
[379] | 300 | } |
---|
| 301 | status = 0; |
---|
[695] | 302 | if(fits_close_file(fptrNew, &status)){ |
---|
| 303 | duchampFITSerror(status,"saveMask","Error closing file:"); |
---|
[379] | 304 | } |
---|
| 305 | |
---|
| 306 | delete [] mask; |
---|
| 307 | |
---|
| 308 | } |
---|
| 309 | |
---|
| 310 | delete [] fpixel; |
---|
| 311 | |
---|
[695] | 312 | return SUCCESS; |
---|
[379] | 313 | } |
---|
| 314 | |
---|
| 315 | //--------------------------------------------------------------------------- |
---|
| 316 | |
---|
[695] | 317 | OUTCOME Cube::saveSmoothedCube() |
---|
[378] | 318 | { |
---|
[528] | 319 | /// @brief |
---|
| 320 | /// A function to save the smoothed arrays to a FITS file. |
---|
| 321 | /// Additional header keywords are written as well, indicating the |
---|
| 322 | /// width of the Hanning filter or the dimensions of the Gaussian |
---|
| 323 | /// kernel. |
---|
| 324 | /// The file is always written -- if the filename (as calculated |
---|
| 325 | /// based on the parameters) exists, then it is overwritten. |
---|
[3] | 326 | |
---|
[378] | 327 | float blankval = this->par.getBlankPixVal(); |
---|
[3] | 328 | |
---|
[378] | 329 | int status = 0; /* MUST initialize status */ |
---|
| 330 | fitsfile *fptrOld, *fptrNew; |
---|
[695] | 331 | if(fits_open_file(&fptrOld,this->par.getFullImageFile().c_str(),READONLY,&status)){ |
---|
| 332 | duchampFITSerror(status,"saveSmoothedCube", "Error opening existing file:"); |
---|
| 333 | return FAILURE; |
---|
| 334 | } |
---|
| 335 | |
---|
[378] | 336 | if(this->par.getFlagOutputSmooth()){ |
---|
| 337 | std::string fileout = "!" + this->par.outputSmoothFile(); |
---|
| 338 | // the ! is there so that it writes over an existing file. |
---|
[3] | 339 | |
---|
[378] | 340 | status = 0; |
---|
[695] | 341 | if(fits_create_file(&fptrNew,fileout.c_str(),&status)){ |
---|
| 342 | duchampFITSerror(status,"saveSmoothedCube","Error creating smoothed FITS file:"); |
---|
| 343 | return FAILURE; |
---|
| 344 | } |
---|
[378] | 345 | else { |
---|
[103] | 346 | status = 0; |
---|
[695] | 347 | if(fits_movabs_hdu(fptrOld, 1, NULL, &status)){ |
---|
| 348 | duchampFITSerror(status,"saveSmoothedCube","Error accessing existing file's header:"); |
---|
| 349 | return FAILURE; |
---|
| 350 | } |
---|
[103] | 351 | status = 0; |
---|
[695] | 352 | if(fits_copy_header(fptrOld, fptrNew, &status)){ |
---|
| 353 | duchampFITSerror(status,"saveSmoothedCube","Error copying header info:"); |
---|
| 354 | return FAILURE; |
---|
| 355 | } |
---|
[46] | 356 | |
---|
[378] | 357 | writeSmoothHeaderInfo(fptrNew, this->par); |
---|
[3] | 358 | |
---|
[103] | 359 | if(this->par.getFlagBlankPix()) |
---|
[578] | 360 | fits_write_imgnull(fptrNew, TFLOAT, 1, this->numPixels, this->recon, &blankval, &status); |
---|
| 361 | else |
---|
| 362 | fits_write_img(fptrNew, TFLOAT, 1, this->numPixels, this->recon, &status); |
---|
[695] | 363 | if(status){ |
---|
| 364 | duchampFITSerror(status,"saveSmothedCube","Error writing smoothed array:"); |
---|
| 365 | return FAILURE; |
---|
| 366 | } |
---|
[3] | 367 | |
---|
[103] | 368 | status = 0; |
---|
[695] | 369 | if(fits_close_file(fptrNew, &status)){ |
---|
| 370 | duchampFITSerror(status,"saveSmoothedCube","Error closing file:"); |
---|
| 371 | } |
---|
[103] | 372 | } |
---|
[378] | 373 | } |
---|
| 374 | |
---|
[695] | 375 | return SUCCESS; |
---|
| 376 | |
---|
[3] | 377 | } |
---|
| 378 | |
---|
[379] | 379 | //--------------------------------------------------------------------------- |
---|
[103] | 380 | |
---|
[695] | 381 | OUTCOME Cube::saveReconstructedCube() |
---|
[378] | 382 | { |
---|
[528] | 383 | /// @details |
---|
| 384 | /// A function to save the reconstructed and/or residual arrays. |
---|
| 385 | /// A number of header keywords are written as well, indicating the |
---|
| 386 | /// nature of the reconstruction that has been done. |
---|
| 387 | /// The file is always written -- if the filename (as calculated |
---|
| 388 | /// based on the recon parameters) exists, then it is overwritten. |
---|
[378] | 389 | |
---|
| 390 | float blankval = this->par.getBlankPixVal(); |
---|
[71] | 391 | |
---|
[757] | 392 | if(!this->reconAllocated){ |
---|
| 393 | duchampError("saveReconCube","Have not allocated reconstructed array, so cannot save"); |
---|
| 394 | return FAILURE; |
---|
| 395 | } |
---|
| 396 | |
---|
[378] | 397 | int status = 0; /* MUST initialize status */ |
---|
| 398 | fitsfile *fptrOld, *fptrNew; |
---|
[695] | 399 | if(fits_open_file(&fptrOld,this->par.getFullImageFile().c_str(),READONLY,&status)){ |
---|
| 400 | duchampFITSerror(status,"saveReconCube","Error opening existing file:"); |
---|
| 401 | return FAILURE; |
---|
| 402 | } |
---|
[757] | 403 | |
---|
| 404 | if(this->head.getFluxUnits()!=this->head.getOrigFluxUnits()){ |
---|
| 405 | // we have changed the flux units - need to change back |
---|
| 406 | double scale,offset,power; |
---|
| 407 | int status = wcsunits(this->head.getFluxUnits().c_str(), this->head.getOrigFluxUnits().c_str(), &scale, &offset, &power); |
---|
| 408 | if(status==0){ |
---|
| 409 | for(int i=0;i<this->numPixels;i++) |
---|
| 410 | if(!this->isBlank(i)){ |
---|
| 411 | this->array[i] = pow(scale * this->array[i] + offset, power); |
---|
| 412 | this->recon[i] = pow(scale * this->recon[i] + offset, power); |
---|
| 413 | } |
---|
| 414 | } |
---|
| 415 | } |
---|
[378] | 416 | |
---|
| 417 | if(this->par.getFlagOutputRecon()){ |
---|
| 418 | std::string fileout = "!" + this->par.outputReconFile(); |
---|
| 419 | // the ! is there so that it writes over an existing file. |
---|
| 420 | |
---|
| 421 | status = 0; |
---|
[695] | 422 | if(fits_create_file(&fptrNew,fileout.c_str(),&status)){ |
---|
| 423 | duchampFITSerror(status,"saveReconCube","Error creating file:"); |
---|
| 424 | return FAILURE; |
---|
| 425 | } |
---|
[378] | 426 | else |
---|
| 427 | { |
---|
| 428 | status = 0; |
---|
[695] | 429 | if(fits_movabs_hdu(fptrOld, 1, NULL, &status)){ |
---|
| 430 | duchampFITSerror(status,"saveReconCube","Error accessing existing file's header:"); |
---|
| 431 | return FAILURE; |
---|
| 432 | } |
---|
[378] | 433 | status = 0; |
---|
[695] | 434 | if(fits_copy_header(fptrOld, fptrNew, &status)){ |
---|
| 435 | duchampFITSerror(status,"saveReconCube","Error copying header:"); |
---|
| 436 | return FAILURE; |
---|
| 437 | } |
---|
[378] | 438 | |
---|
| 439 | writeReconHeaderInfo(fptrNew, this->par, "recon"); |
---|
| 440 | |
---|
[757] | 441 | status=0; |
---|
[767] | 442 | long *fpixel = new long[this->header().WCS().naxis]; |
---|
[757] | 443 | for(int i=0;i<this->numDim;i++) fpixel[i]=1; |
---|
| 444 | long group=0; |
---|
[378] | 445 | if(this->par.getFlagBlankPix()) |
---|
[757] | 446 | // fits_write_imgnull(fptrNew, TFLOAT, 1, this->numPixels, this->recon, &blankval, &status); |
---|
| 447 | fits_write_imgnull_flt(fptrNew, group, 1, this->numPixels, this->recon, blankval, &status); |
---|
| 448 | // fits_write_pixnull(fptrNew, TFLOAT, fpixel, this->numPixels, this->recon, &blankval, &status); |
---|
[578] | 449 | else |
---|
[757] | 450 | // fits_write_img(fptrNew, TFLOAT, 1, this->numPixels, this->recon, &status); |
---|
| 451 | fits_write_img_flt(fptrNew, group, 1, this->numPixels, this->recon, &status); |
---|
| 452 | // fits_write_pix(fptrNew, TFLOAT, fpixel, this->numPixels, this->recon, &status); |
---|
[695] | 453 | if(status){ |
---|
| 454 | duchampFITSerror(status,"saveReconCube","Error writing reconstructed array:"); |
---|
| 455 | return FAILURE; |
---|
| 456 | } |
---|
[757] | 457 | delete [] fpixel; |
---|
[378] | 458 | |
---|
| 459 | status = 0; |
---|
[695] | 460 | if(fits_close_file(fptrNew, &status)){ |
---|
| 461 | duchampFITSerror(status,"saveReconCube","Error closing file:"); |
---|
| 462 | } |
---|
[103] | 463 | } |
---|
[378] | 464 | } |
---|
[3] | 465 | |
---|
[71] | 466 | |
---|
[378] | 467 | if(this->par.getFlagOutputResid()){ |
---|
[578] | 468 | float *resid = new float[this->numPixels]; |
---|
| 469 | for(int i=0;i<this->numPixels;i++) |
---|
| 470 | resid[i] = this->array[i] - this->recon[i]; |
---|
| 471 | |
---|
[378] | 472 | std::string fileout = "!" + this->par.outputResidFile(); |
---|
| 473 | // the ! is there so that it writes over an existing file. |
---|
| 474 | status = 0; |
---|
[695] | 475 | if(fits_create_file(&fptrNew,fileout.c_str(),&status)){ |
---|
| 476 | duchampFITSerror(status,"saveResidualCube","Error creating new file:"); |
---|
| 477 | return FAILURE; |
---|
| 478 | } |
---|
[378] | 479 | else |
---|
| 480 | { |
---|
| 481 | status = 0; |
---|
[695] | 482 | if(fits_movabs_hdu(fptrOld, 1, NULL, &status)){ |
---|
| 483 | duchampFITSerror(status,"saveResidualCube","Error accessing existing file's header:"); |
---|
| 484 | return FAILURE; |
---|
| 485 | } |
---|
[378] | 486 | status = 0; |
---|
[695] | 487 | if(fits_copy_header(fptrOld, fptrNew, &status)){ |
---|
| 488 | duchampFITSerror(status,"saveResidualCube","Error copying header:"); |
---|
| 489 | return FAILURE; |
---|
| 490 | } |
---|
[378] | 491 | writeReconHeaderInfo(fptrNew, this->par, "resid"); |
---|
[71] | 492 | |
---|
[378] | 493 | if(this->par.getFlagBlankPix()) |
---|
[578] | 494 | fits_write_imgnull(fptrNew, TFLOAT, 1, this->numPixels, resid, &blankval, &status); |
---|
| 495 | else |
---|
| 496 | fits_write_img(fptrNew, TFLOAT, 1, this->numPixels, resid, &status); |
---|
[695] | 497 | if(status){ |
---|
| 498 | duchampFITSerror(status,"saveResidualCube","Error writing reconstructed array:"); |
---|
| 499 | return FAILURE; |
---|
| 500 | } |
---|
[71] | 501 | |
---|
[695] | 502 | status=0; |
---|
| 503 | if(fits_close_file(fptrNew, &status)){ |
---|
| 504 | duchampFITSerror(status,"saveResidualCube","Error closing file:"); |
---|
| 505 | } |
---|
[378] | 506 | } |
---|
[578] | 507 | delete [] resid; |
---|
[378] | 508 | } |
---|
[86] | 509 | |
---|
[695] | 510 | return SUCCESS; |
---|
[378] | 511 | } |
---|
[71] | 512 | |
---|
[379] | 513 | //--------------------------------------------------------------------------- |
---|
[378] | 514 | |
---|
| 515 | void writeReconHeaderInfo(fitsfile *fptr, Param &par, std::string nature) |
---|
| 516 | { |
---|
[528] | 517 | /// @details |
---|
| 518 | /// A simple function that writes all the necessary keywords and comments |
---|
| 519 | /// to the FITS header pointed to by fptr. |
---|
| 520 | /// The keyword names and comments are taken from duchamp.hh |
---|
| 521 | /// The parameter "nature" indicates what type of file is being written: |
---|
| 522 | /// should be either "recon" or "resid". |
---|
[378] | 523 | |
---|
| 524 | int status = 0; |
---|
| 525 | std::string explanation = "",ReconResid=""; |
---|
| 526 | |
---|
| 527 | fits_write_history(fptr, (char *)header_reconHistory1.c_str(), &status); |
---|
[71] | 528 | |
---|
[378] | 529 | fits_write_history(fptr, (char *)header_reconHistory2.c_str(), &status); |
---|
[71] | 530 | |
---|
[378] | 531 | fits_write_history(fptr, (char *)header_reconHistory_input.c_str(), &status); |
---|
[71] | 532 | |
---|
[378] | 533 | fits_write_history(fptr, (char *)par.getImageFile().c_str(), &status); |
---|
[105] | 534 | |
---|
[378] | 535 | if(par.getFlagSubsection()){ |
---|
| 536 | fits_write_comment(fptr,(char *)header_reconSubsection_comment.c_str(), |
---|
| 537 | &status); |
---|
| 538 | fits_write_key(fptr, TSTRING, (char *)keyword_subsection.c_str(), |
---|
| 539 | (char *)par.getSubsection().c_str(), |
---|
| 540 | (char *)comment_subsection.c_str(), &status); |
---|
| 541 | } |
---|
[105] | 542 | |
---|
[378] | 543 | fits_write_comment(fptr, (char *)header_atrous_comment.c_str(), &status); |
---|
[105] | 544 | |
---|
[378] | 545 | float valf = par.getAtrousCut(); |
---|
| 546 | fits_write_key(fptr, TFLOAT, (char *)keyword_snrRecon.c_str(), &valf, |
---|
| 547 | (char *)comment_snrRecon.c_str(), &status); |
---|
[71] | 548 | |
---|
[378] | 549 | int vali = par.getReconDim(); |
---|
| 550 | fits_write_key(fptr, TINT, (char *)keyword_reconDim.c_str(), &vali, |
---|
| 551 | (char *)comment_reconDim.c_str(), &status); |
---|
[103] | 552 | |
---|
[378] | 553 | vali = par.getMinScale(); |
---|
| 554 | fits_write_key(fptr, TINT, (char *)keyword_scaleMin.c_str(), &vali, |
---|
| 555 | (char *)comment_scaleMin.c_str(), &status); |
---|
[71] | 556 | |
---|
[378] | 557 | vali = par.getFilterCode(); |
---|
| 558 | fits_write_key(fptr, TINT, (char *)keyword_filterCode.c_str(), &vali, |
---|
| 559 | (char *)comment_filterCode.c_str(), &status); |
---|
[71] | 560 | |
---|
[378] | 561 | if(nature == "recon"){ |
---|
| 562 | explanation = "Duchamp: This is the RECONSTRUCTED cube"; |
---|
| 563 | ReconResid = "RECON"; |
---|
| 564 | } |
---|
| 565 | else if(nature == "resid"){ |
---|
| 566 | explanation = "Duchamp: This is the RESIDUAL cube"; |
---|
| 567 | ReconResid = "RESID"; |
---|
| 568 | } |
---|
| 569 | else duchampWarning("write_header_info","explanation not present!\n"); |
---|
| 570 | fits_write_comment(fptr, (char *)explanation.c_str(), &status); |
---|
| 571 | fits_write_key(fptr, TSTRING, (char *)keyword_ReconResid.c_str(), |
---|
| 572 | (char *)ReconResid.c_str(), |
---|
| 573 | (char *)comment_ReconResid.c_str(), &status); |
---|
| 574 | |
---|
[71] | 575 | } |
---|
| 576 | |
---|
[379] | 577 | //--------------------------------------------------------------------------- |
---|
| 578 | |
---|
[378] | 579 | void writeSmoothHeaderInfo(fitsfile *fptr, Param &par) |
---|
| 580 | { |
---|
[528] | 581 | /// @details |
---|
| 582 | /// A simple function that writes all the necessary keywords and comments |
---|
| 583 | /// to the FITS header pointed to by fptr. |
---|
| 584 | /// The keyword names and comments are taken from duchamp.hh |
---|
[208] | 585 | |
---|
[378] | 586 | int status = 0; |
---|
[208] | 587 | |
---|
[378] | 588 | fits_write_history(fptr, (char *)header_smoothHistory.c_str(), &status); |
---|
[208] | 589 | status = 0; |
---|
[378] | 590 | fits_write_history(fptr, (char *)header_smoothHistory_input.c_str(),&status); |
---|
[208] | 591 | status = 0; |
---|
[378] | 592 | fits_write_history(fptr, (char *)par.getImageFile().c_str(), &status); |
---|
| 593 | |
---|
| 594 | if(par.getFlagSubsection()){ |
---|
| 595 | status = 0; |
---|
| 596 | fits_write_comment(fptr,(char *)header_smoothSubsection_comment.c_str(), |
---|
| 597 | &status); |
---|
| 598 | status = 0; |
---|
| 599 | fits_write_key(fptr, TSTRING, (char *)keyword_subsection.c_str(), |
---|
| 600 | (char *)par.getSubsection().c_str(), |
---|
| 601 | (char *)comment_subsection.c_str(), &status); |
---|
| 602 | } |
---|
[208] | 603 | |
---|
[378] | 604 | if(par.getSmoothType()=="spatial"){ |
---|
| 605 | // if kernMin is negative (not defined), make it equal to kernMaj |
---|
| 606 | if(par.getKernMin() < 0) par.setKernMin(par.getKernMaj()); |
---|
[285] | 607 | |
---|
[378] | 608 | fits_write_key(fptr, TSTRING, (char *)keyword_smoothtype.c_str(), |
---|
| 609 | (char *)header_smoothSpatial.c_str(), |
---|
| 610 | (char *)comment_smoothtype.c_str(), &status); |
---|
| 611 | float valf = par.getKernMaj(); |
---|
| 612 | fits_write_key(fptr, TFLOAT, (char *)keyword_kernmaj.c_str(), &valf, |
---|
| 613 | (char *)comment_kernmaj.c_str(), &status); |
---|
| 614 | valf = par.getKernMin(); |
---|
| 615 | fits_write_key(fptr, TFLOAT, (char *)keyword_kernmin.c_str(), &valf, |
---|
| 616 | (char *)comment_kernmin.c_str(), &status); |
---|
| 617 | valf = par.getKernPA(); |
---|
| 618 | fits_write_key(fptr, TFLOAT, (char *)keyword_kernpa.c_str(), &valf, |
---|
| 619 | (char *)comment_kernpa.c_str(), &status); |
---|
| 620 | } |
---|
| 621 | else if(par.getSmoothType()=="spectral"){ |
---|
| 622 | fits_write_key(fptr, TSTRING, (char *)keyword_smoothtype.c_str(), |
---|
| 623 | (char *)header_smoothSpectral.c_str(), |
---|
| 624 | (char *)comment_smoothtype.c_str(), &status); |
---|
| 625 | int vali = par.getHanningWidth(); |
---|
| 626 | fits_write_key(fptr, TINT, (char *)keyword_hanningwidth.c_str(), &vali, |
---|
| 627 | (char *)comment_hanningwidth.c_str(), &status); |
---|
| 628 | } |
---|
[277] | 629 | } |
---|
[378] | 630 | |
---|
[379] | 631 | //--------------------------------------------------------------------------- |
---|
| 632 | |
---|
| 633 | void writeMaskHeaderInfo(fitsfile *fptr, Param &par) |
---|
| 634 | { |
---|
[528] | 635 | /// @details |
---|
| 636 | /// A simple function that writes all the necessary keywords and comments |
---|
| 637 | /// to the FITS header pointed to by fptr. |
---|
| 638 | /// The keyword names and comments are taken from duchamp.hh |
---|
[379] | 639 | |
---|
| 640 | int status = 0; |
---|
| 641 | |
---|
| 642 | fits_write_history(fptr, (char *)header_maskHistory.c_str(), &status); |
---|
| 643 | status = 0; |
---|
| 644 | fits_write_history(fptr, (char *)header_maskHistory_input.c_str(),&status); |
---|
| 645 | status = 0; |
---|
| 646 | fits_write_history(fptr, (char *)par.getImageFile().c_str(), &status); |
---|
| 647 | |
---|
| 648 | if(par.getFlagSubsection()){ |
---|
| 649 | status = 0; |
---|
| 650 | fits_write_comment(fptr,(char *)header_maskSubsection_comment.c_str(), |
---|
| 651 | &status); |
---|
| 652 | status = 0; |
---|
| 653 | fits_write_key(fptr, TSTRING, (char *)keyword_subsection.c_str(), |
---|
| 654 | (char *)par.getSubsection().c_str(), |
---|
| 655 | (char *)comment_subsection.c_str(), &status); |
---|
| 656 | } |
---|
| 657 | } |
---|
| 658 | |
---|
[670] | 659 | //--------------------------------------------------------------------------- |
---|
| 660 | |
---|
| 661 | void writeMomentMapHeaderInfo(fitsfile *fptr, Param &par) |
---|
| 662 | { |
---|
| 663 | /// @details |
---|
| 664 | /// A simple function that writes all the necessary keywords and comments |
---|
| 665 | /// to the FITS header pointed to by fptr. |
---|
| 666 | /// The keyword names and comments are taken from duchamp.hh |
---|
| 667 | |
---|
| 668 | int status = 0; |
---|
| 669 | |
---|
| 670 | fits_write_history(fptr, (char *)header_moment0History.c_str(), &status); |
---|
| 671 | status = 0; |
---|
| 672 | fits_write_history(fptr, (char *)header_moment0History_input.c_str(),&status); |
---|
| 673 | status = 0; |
---|
| 674 | fits_write_history(fptr, (char *)par.getImageFile().c_str(), &status); |
---|
| 675 | |
---|
| 676 | if(par.getFlagSubsection()){ |
---|
| 677 | status = 0; |
---|
| 678 | fits_write_comment(fptr,(char *)header_moment0Subsection_comment.c_str(), |
---|
| 679 | &status); |
---|
| 680 | status = 0; |
---|
| 681 | fits_write_key(fptr, TSTRING, (char *)keyword_subsection.c_str(), |
---|
| 682 | (char *)par.getSubsection().c_str(), |
---|
| 683 | (char *)comment_subsection.c_str(), &status); |
---|
| 684 | } |
---|
| 685 | } |
---|
| 686 | |
---|
[208] | 687 | } |
---|