source: branches/fitshead-branch/Cubes/saveImage.cc @ 96

Last change on this file since 96 was 96, checked in by Matthew Whiting, 18 years ago

Two sets of large changes:
1) Added reconDim, to select which dimension of reconstruction to use.
2) Changed the way the MW channels are dealt with -- now not set to 0. but

simply ignored for searching purposes.

Summary of changes for each file:
duchamp.hh -- added keyword info for reconDim
param.hh -- Introduced reconDim and flagUsingBlank and isInMW.
param.cc -- Introduced reconDim and flagUsingBlank: initialisation etc commands
InputComplete? -- Added reconDim info
mainDuchamp.cc -- Removed the removeMW call. Changed search function names
docs/Guide.tex -- New code to deal with changes: reconDim, MW removal,

up-to-date output examples, better hints & notes section

Detection/thresholding_functions.cc -- minor typo correction

Cubes/cubes.hh -- added isBlank and removeMW functions in Image class, and

renamed search function prototypes

Cubes/cubes.cc -- added removeMW function for Image class, cleaned up

Cube::removeMW as well (although now not used)

Cubes/outputSpectra.cc -- Improved use of isBlank and isInMW functions: now

show MW channels but don't use them in calculating
the flux range

Cubes/getImage.cc -- added line to indicate whether the Param's blank value

is being used, rather than the FITS header.

Cubes/cubicSearch.cc -- renamed functions: front end is now CubicSearch?, and

searching function is search3DArray.

-- Improved way MW removal is dealt with. Changed

location of stats calculations.

Cubes/saveImage.cc -- added code for saving reconDim info
Cubes/readRecon.cc -- added code for reading reconDim info (and added status

intialisations before all cfitsio commands)

ATrous/ReconSearch.cc -- renamed functions: front end is now ReconSearch?, and

searching function is searchReconArray.
Using reconDim to decide which reconstruction to use.

-- Improved way MW removal is dealt with. Changed

location of stats calculations.

ATrous/atrous_1d_reconstruct.cc -- using Median stats
ATrous/atrous_2d_reconstruct.cc -- made code up to date, to conform with 1- &

3-d code. Removed boundary conditions.

ATrous/atrous_3d_reconstruct.cc -- corrected typo (avGapY /= float(xdim)).

Using median stats.

Cubes/cubicSearchNMerge.cc -- Deleted. (not used)
Cubes/readParams.cc -- Deleted. (not used)

File size: 7.8 KB
Line 
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
10void write_header_info(fitsfile *fptr, Param &par, string nature);
11
12void 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
140void 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.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();
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
179  vali = int(par.getFlagMW());
180  fits_write_key(fptr, TINT, (char *)keyword_flagMW.c_str(), &vali,
181                 (char *)comment_flagMW.c_str(), &status);
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
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}
Note: See TracBrowser for help on using the repository browser.