[3] | 1 | #include <iostream> |
---|
| 2 | #include <sstream> |
---|
| 3 | #include <string> |
---|
| 4 | #include <wcs.h> |
---|
| 5 | #include <wcshdr.h> |
---|
[66] | 6 | #include <fitsio.h> |
---|
[71] | 7 | #include <duchamp.hh> |
---|
[3] | 8 | #include <Cubes/cubes.hh> |
---|
| 9 | |
---|
[71] | 10 | void write_header_info(fitsfile *fptr, Param &par, string nature); |
---|
[86] | 11 | |
---|
[3] | 12 | void Cube::saveReconstructedCube() |
---|
| 13 | { |
---|
[86] | 14 | /** |
---|
| 15 | * Cube::saveReconstructedCube() |
---|
| 16 | * |
---|
| 17 | * A function to save the reconstructed and/or residual arrays. |
---|
| 18 | * A number of header keywords are written as well, indicating the nature of the |
---|
| 19 | * reconstruction that has been done. |
---|
| 20 | * The file is always written -- if the filename (as calculated based on the |
---|
| 21 | * recon parameters) exists, then it is overwritten. |
---|
| 22 | */ |
---|
[3] | 23 | |
---|
[71] | 24 | int newbitpix = FLOAT_IMG; |
---|
| 25 | |
---|
[3] | 26 | float *resid = new float[this->numPixels]; |
---|
| 27 | for(int i=0;i<this->numPixels;i++) resid[i] = this->array[i] - this->recon[i]; |
---|
| 28 | float blankval = this->par.getBlankPixVal(); |
---|
| 29 | |
---|
[71] | 30 | long *fpixel = new long[this->numDim]; |
---|
| 31 | for(int i=0;i<this->numDim;i++) fpixel[i]=1; |
---|
| 32 | int status = 0; /* MUST initialize status */ |
---|
[3] | 33 | fitsfile *fptrOld, *fptrNew; |
---|
| 34 | fits_open_file(&fptrOld,this->par.getImageFile().c_str(),READONLY,&status); |
---|
| 35 | if (status) fits_report_error(stderr, status); |
---|
| 36 | |
---|
| 37 | if(this->par.getFlagOutputRecon()){ |
---|
[71] | 38 | string fileout = "!" + this->par.getImageFile(); // ! so that it writes over an existing file. |
---|
| 39 | fileout = fileout.substr(0,fileout.size()-5); // remove the ".fits" on the end. |
---|
| 40 | std::stringstream ss; |
---|
| 41 | ss << ".RECON"<<this->par.getAtrousCut()<<".fits"; |
---|
| 42 | fileout += ss.str(); |
---|
[3] | 43 | |
---|
| 44 | status = 0; |
---|
[71] | 45 | fits_create_file(&fptrNew,fileout.c_str(),&status); |
---|
[3] | 46 | if (status) fits_report_error(stderr, status); |
---|
| 47 | status = 0; |
---|
| 48 | fits_movabs_hdu(fptrOld, 1, NULL, &status); |
---|
| 49 | if (status) fits_report_error(stderr, status); |
---|
| 50 | status = 0; |
---|
[71] | 51 | fits_copy_header(fptrOld, fptrNew, &status); |
---|
[3] | 52 | if (status) fits_report_error(stderr, status); |
---|
[71] | 53 | char *comment = new char[80]; |
---|
| 54 | long dud; |
---|
| 55 | status = 0; |
---|
| 56 | fits_update_key(fptrNew, TINT, "BITPIX", &newbitpix, "number of bits per data pixel", &status); |
---|
| 57 | if (status) fits_report_error(stderr, status); |
---|
| 58 | status = 0; |
---|
| 59 | float bscale=1., bzero=0.; |
---|
| 60 | fits_update_key(fptrNew, TFLOAT, "BSCALE", &bscale, "PHYSICAL = PIXEL*BSCALE + BZERO", &status); |
---|
| 61 | fits_update_key(fptrNew, TFLOAT, "BZERO", &bzero, "", &status); |
---|
| 62 | fits_set_bscale(fptrNew, 1., 0., &status); |
---|
| 63 | if (status) fits_report_error(stderr, status); |
---|
| 64 | // Need to correct the dimensions, if we have subsectioned the image... |
---|
| 65 | if(this->par.getFlagSubsection()){ |
---|
[46] | 66 | fits_read_key(fptrOld, TLONG, "NAXIS1", &dud, comment, &status); |
---|
| 67 | fits_update_key(fptrNew, TLONG, "NAXIS1", &(this->axisDim[0]), comment, &status); |
---|
| 68 | fits_read_key(fptrOld, TLONG, "NAXIS2", &dud, comment, &status); |
---|
[54] | 69 | fits_update_key(fptrNew, TLONG, "NAXIS2", &(this->axisDim[1]), comment, &status); |
---|
[46] | 70 | fits_read_key(fptrOld, TLONG, "NAXIS3", &dud, comment, &status); |
---|
[54] | 71 | fits_update_key(fptrNew, TLONG, "NAXIS3", &(this->axisDim[2]), comment, &status); |
---|
[46] | 72 | } |
---|
| 73 | |
---|
[71] | 74 | write_header_info(fptrNew, this->par, "recon"); |
---|
[3] | 75 | |
---|
| 76 | if(this->par.getFlagBlankPix()) |
---|
[71] | 77 | fits_write_pixnull(fptrNew, TFLOAT, fpixel, this->numPixels, this->recon, &blankval, &status); |
---|
| 78 | else fits_write_pix(fptrNew, TFLOAT, fpixel, this->numPixels, this->recon, &status); |
---|
[3] | 79 | |
---|
[71] | 80 | status = 0; |
---|
[3] | 81 | fits_close_file(fptrNew, &status); |
---|
[71] | 82 | if (status) fits_report_error(stderr, status); |
---|
[3] | 83 | } |
---|
| 84 | |
---|
| 85 | if(this->par.getFlagOutputResid()){ |
---|
[71] | 86 | string fileout = "!" + this->par.getImageFile(); // ! so that it writes over an existing file. |
---|
| 87 | fileout = fileout.substr(0,fileout.size()-5); |
---|
| 88 | std::stringstream ss; |
---|
| 89 | ss << ".RESID"<<this->par.getAtrousCut()<<".fits"; |
---|
| 90 | fileout += ss.str(); |
---|
[3] | 91 | |
---|
| 92 | status = 0; |
---|
[71] | 93 | fits_create_file(&fptrNew,fileout.c_str(),&status); |
---|
[3] | 94 | if (status) fits_report_error(stderr, status); |
---|
| 95 | status = 0; |
---|
| 96 | fits_movabs_hdu(fptrOld, 1, NULL, &status); |
---|
| 97 | if (status) fits_report_error(stderr, status); |
---|
| 98 | status = 0; |
---|
[71] | 99 | fits_copy_header(fptrOld, fptrNew, &status); |
---|
[3] | 100 | if (status) fits_report_error(stderr, status); |
---|
[46] | 101 | |
---|
[71] | 102 | status = 0; |
---|
| 103 | fits_update_key(fptrNew, TINT, "BITPIX", &newbitpix, "number of bits per data pixel", &status); |
---|
| 104 | if (status) fits_report_error(stderr, status); |
---|
| 105 | status = 0; |
---|
| 106 | float bscale=1., bzero=0.; |
---|
| 107 | fits_update_key(fptrNew, TFLOAT, "BSCALE", &bscale, "PHYSICAL = PIXEL*BSCALE + BZERO", &status); |
---|
| 108 | fits_update_key(fptrNew, TFLOAT, "BZERO", &bzero, "", &status); |
---|
| 109 | fits_set_bscale(fptrNew, 1., 0., &status); |
---|
| 110 | if (status) fits_report_error(stderr, status); |
---|
| 111 | |
---|
[46] | 112 | // Need to correct the dimensions, if we have subsectioned the image... |
---|
[71] | 113 | char *comment = new char[80]; |
---|
| 114 | long dud; |
---|
[46] | 115 | if(this->pars().getFlagSubsection()){ |
---|
| 116 | fits_read_key(fptrOld, TLONG, "NAXIS1", &dud, comment, &status); |
---|
| 117 | fits_update_key(fptrNew, TLONG, "NAXIS1", &(this->axisDim[0]), comment, &status); |
---|
| 118 | fits_read_key(fptrOld, TLONG, "NAXIS2", &dud, comment, &status); |
---|
[54] | 119 | fits_update_key(fptrNew, TLONG, "NAXIS2", &(this->axisDim[1]), comment, &status); |
---|
[46] | 120 | fits_read_key(fptrOld, TLONG, "NAXIS3", &dud, comment, &status); |
---|
[54] | 121 | fits_update_key(fptrNew, TLONG, "NAXIS3", &(this->axisDim[2]), comment, &status); |
---|
[71] | 122 | } |
---|
[3] | 123 | |
---|
[71] | 124 | write_header_info(fptrNew, this->par, "resid"); |
---|
| 125 | |
---|
[3] | 126 | if(this->par.getFlagBlankPix()) |
---|
| 127 | fits_write_pixnull(fptrNew, TFLOAT, fpixel, this->numPixels, resid, &blankval, &status); |
---|
| 128 | else fits_write_pix(fptrNew, TFLOAT, fpixel, this->numPixels, resid, &status); |
---|
| 129 | |
---|
| 130 | fits_close_file(fptrNew, &status); |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | delete [] recon; |
---|
| 134 | delete [] resid; |
---|
| 135 | delete [] fpixel; |
---|
| 136 | |
---|
| 137 | } |
---|
[71] | 138 | |
---|
| 139 | |
---|
| 140 | void write_header_info(fitsfile *fptr, Param &par, string nature) |
---|
| 141 | { |
---|
[86] | 142 | /** |
---|
| 143 | * write_header_info(fptr, par, nature) |
---|
| 144 | * |
---|
| 145 | * A simple function that writes all the necessary keywords and comments |
---|
| 146 | * to the FITS header pointed to by fptr. |
---|
| 147 | * The keyword names and comments are taken from duchamp.hh |
---|
| 148 | * The parameter "nature" indicates what type of file is being written: |
---|
| 149 | * should be either "recon" or "resid". |
---|
| 150 | */ |
---|
| 151 | |
---|
| 152 | |
---|
[71] | 153 | int status = 0; |
---|
| 154 | char *comment, *keyword; |
---|
| 155 | string explanation = "",ReconResid=""; |
---|
| 156 | |
---|
| 157 | fits_write_history(fptr, (char *)header_history1.c_str(), &status); |
---|
| 158 | |
---|
| 159 | fits_write_history(fptr, (char *)header_history2.c_str(), &status); |
---|
| 160 | |
---|
| 161 | fits_write_comment(fptr, (char *)header_comment.c_str(), &status); |
---|
| 162 | |
---|
| 163 | float valf = par.getAtrousCut(); |
---|
| 164 | fits_write_key(fptr, TFLOAT, (char *)keyword_snrRecon.c_str(), &valf, |
---|
| 165 | (char *)comment_snrRecon.c_str(), &status); |
---|
| 166 | |
---|
[96] | 167 | int vali = par.getReconDim(); |
---|
| 168 | fits_write_key(fptr, TINT, (char *)keyword_reconDim.c_str(), &vali, |
---|
| 169 | (char *)comment_reconDim.c_str(), &status); |
---|
| 170 | |
---|
| 171 | vali = par.getMinScale(); |
---|
[71] | 172 | fits_write_key(fptr, TINT, (char *)keyword_scaleMin.c_str(), &vali, |
---|
| 173 | (char *)comment_scaleMin.c_str(), &status); |
---|
| 174 | |
---|
| 175 | vali = par.getFilterCode(); |
---|
| 176 | fits_write_key(fptr, TINT, (char *)keyword_filterCode.c_str(), &vali, |
---|
| 177 | (char *)comment_filterCode.c_str(), &status); |
---|
| 178 | |
---|
[95] | 179 | vali = int(par.getFlagMW()); |
---|
| 180 | fits_write_key(fptr, TINT, (char *)keyword_flagMW.c_str(), &vali, |
---|
| 181 | (char *)comment_flagMW.c_str(), &status); |
---|
[84] | 182 | if(par.getFlagMW()){ |
---|
| 183 | fits_write_comment(fptr, (char *)flagMW_comment.c_str(), &status); |
---|
| 184 | vali = par.getMinMW(); |
---|
| 185 | fits_write_key(fptr, TINT, (char *)keyword_minMW.c_str(), &vali, |
---|
| 186 | (char *)comment_minMW.c_str(), &status); |
---|
| 187 | vali = par.getMaxMW(); |
---|
| 188 | fits_write_key(fptr, TINT, (char *)keyword_maxMW.c_str(), &vali, |
---|
| 189 | (char *)comment_maxMW.c_str(), &status); |
---|
| 190 | } |
---|
| 191 | |
---|
[71] | 192 | if(nature == "recon"){ |
---|
| 193 | explanation = "Duchamp: This is the RECONSTRUCTED cube"; |
---|
| 194 | ReconResid = "RECON"; |
---|
| 195 | } |
---|
| 196 | else if(nature == "resid"){ |
---|
| 197 | explanation = "Duchamp: This is the RESIDUAL cube"; |
---|
| 198 | ReconResid = "RESID"; |
---|
| 199 | } |
---|
| 200 | else std::cerr << "WARNING : write_header_info : explanation not present!\n"; |
---|
| 201 | fits_write_comment(fptr, (char *)explanation.c_str(), &status); |
---|
| 202 | fits_write_key(fptr, TSTRING, (char *)keyword_ReconResid.c_str(), |
---|
| 203 | (char *)ReconResid.c_str(), (char *)comment_ReconResid.c_str(), &status); |
---|
| 204 | |
---|
| 205 | } |
---|