1 | #include <iostream> |
---|
2 | #include <sstream> |
---|
3 | #include <string> |
---|
4 | #include <wcs.h> |
---|
5 | #include <wcshdr.h> |
---|
6 | #include <fitsio.h> |
---|
7 | #include <duchamp.hh> |
---|
8 | #include <Cubes/cubes.hh> |
---|
9 | |
---|
10 | void write_header_info(fitsfile *fptr, Param &par, string nature); |
---|
11 | |
---|
12 | void Cube::saveReconstructedCube() |
---|
13 | { |
---|
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 | */ |
---|
23 | |
---|
24 | int newbitpix = FLOAT_IMG; |
---|
25 | |
---|
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 | |
---|
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 */ |
---|
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()){ |
---|
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(); |
---|
43 | |
---|
44 | status = 0; |
---|
45 | fits_create_file(&fptrNew,fileout.c_str(),&status); |
---|
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; |
---|
51 | fits_copy_header(fptrOld, fptrNew, &status); |
---|
52 | if (status) fits_report_error(stderr, status); |
---|
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()){ |
---|
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); |
---|
69 | fits_update_key(fptrNew, TLONG, "NAXIS2", &(this->axisDim[1]), comment, &status); |
---|
70 | fits_read_key(fptrOld, TLONG, "NAXIS3", &dud, comment, &status); |
---|
71 | fits_update_key(fptrNew, TLONG, "NAXIS3", &(this->axisDim[2]), comment, &status); |
---|
72 | } |
---|
73 | |
---|
74 | write_header_info(fptrNew, this->par, "recon"); |
---|
75 | |
---|
76 | if(this->par.getFlagBlankPix()) |
---|
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); |
---|
79 | |
---|
80 | status = 0; |
---|
81 | fits_close_file(fptrNew, &status); |
---|
82 | if (status) fits_report_error(stderr, status); |
---|
83 | } |
---|
84 | |
---|
85 | if(this->par.getFlagOutputResid()){ |
---|
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(); |
---|
91 | |
---|
92 | status = 0; |
---|
93 | fits_create_file(&fptrNew,fileout.c_str(),&status); |
---|
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; |
---|
99 | fits_copy_header(fptrOld, fptrNew, &status); |
---|
100 | if (status) fits_report_error(stderr, status); |
---|
101 | |
---|
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 | |
---|
112 | // Need to correct the dimensions, if we have subsectioned the image... |
---|
113 | char *comment = new char[80]; |
---|
114 | long dud; |
---|
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); |
---|
119 | fits_update_key(fptrNew, TLONG, "NAXIS2", &(this->axisDim[1]), comment, &status); |
---|
120 | fits_read_key(fptrOld, TLONG, "NAXIS3", &dud, comment, &status); |
---|
121 | fits_update_key(fptrNew, TLONG, "NAXIS3", &(this->axisDim[2]), comment, &status); |
---|
122 | } |
---|
123 | |
---|
124 | write_header_info(fptrNew, this->par, "resid"); |
---|
125 | |
---|
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 | } |
---|
138 | |
---|
139 | |
---|
140 | void write_header_info(fitsfile *fptr, Param &par, string nature) |
---|
141 | { |
---|
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 | |
---|
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 | |
---|
167 | int vali = par.getMinScale(); |
---|
168 | fits_write_key(fptr, TINT, (char *)keyword_scaleMin.c_str(), &vali, |
---|
169 | (char *)comment_scaleMin.c_str(), &status); |
---|
170 | |
---|
171 | vali = par.getFilterCode(); |
---|
172 | fits_write_key(fptr, TINT, (char *)keyword_filterCode.c_str(), &vali, |
---|
173 | (char *)comment_filterCode.c_str(), &status); |
---|
174 | |
---|
175 | if(par.getFlagMW()){ |
---|
176 | fits_write_comment(fptr, (char *)flagMW_comment.c_str(), &status); |
---|
177 | vali = par.getMinMW(); |
---|
178 | fits_write_key(fptr, TINT, (char *)keyword_minMW.c_str(), &vali, |
---|
179 | (char *)comment_minMW.c_str(), &status); |
---|
180 | vali = par.getMaxMW(); |
---|
181 | fits_write_key(fptr, TINT, (char *)keyword_maxMW.c_str(), &vali, |
---|
182 | (char *)comment_maxMW.c_str(), &status); |
---|
183 | } |
---|
184 | |
---|
185 | if(nature == "recon"){ |
---|
186 | explanation = "Duchamp: This is the RECONSTRUCTED cube"; |
---|
187 | ReconResid = "RECON"; |
---|
188 | } |
---|
189 | else if(nature == "resid"){ |
---|
190 | explanation = "Duchamp: This is the RESIDUAL cube"; |
---|
191 | ReconResid = "RESID"; |
---|
192 | } |
---|
193 | else std::cerr << "WARNING : write_header_info : explanation not present!\n"; |
---|
194 | fits_write_comment(fptr, (char *)explanation.c_str(), &status); |
---|
195 | fits_write_key(fptr, TSTRING, (char *)keyword_ReconResid.c_str(), |
---|
196 | (char *)ReconResid.c_str(), (char *)comment_ReconResid.c_str(), &status); |
---|
197 | |
---|
198 | } |
---|