source: trunk/Cubes/saveImage.cc @ 53

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

saveImage.cc : Added code to write the correct axis lengths to the headers

of the new fits files (in the case of a subsection being used, so that
it doesn't assume the file is the same size as the entire input file).

getImage.cc : If there is no WCS in the header of the fits file, the code now

does not try and set the WCS in the Cube class. Now able to run on fake
cubes with no associated WCS.

File size: 4.4 KB
Line 
1#include <iostream>
2#include <sstream>
3#include <string>
4#include <fitsio.h>
5#include <wcs.h>
6#include <wcshdr.h>
7#include <Cubes/cubes.hh>
8
9void Cube::saveReconstructedCube()
10{
11 
12  float *resid = new float[this->numPixels];
13  for(int i=0;i<this->numPixels;i++) resid[i] = this->array[i] - this->recon[i];
14  float blankval = this->par.getBlankPixVal();
15
16  short int maxdim=3;
17  long *fpixel = new long[maxdim];
18  for(int i=0;i<maxdim;i++) fpixel[i]=1;
19  long *dimAxes = new long[maxdim];
20  for(int i=0;i<maxdim;i++) dimAxes[i]=1;
21  long nelements;
22  int bitpix,numAxes,anynul;
23  int status = 0,  nkeys, ct=1;  /* MUST initialize status */
24  fitsfile *fptrOld, *fptrNew;         
25  fits_open_file(&fptrOld,this->par.getImageFile().c_str(),READONLY,&status);
26  if (status) fits_report_error(stderr, status);
27 
28  if(this->par.getFlagOutputRecon()){
29    string fileout1 = "!" + this->par.getImageFile(); // ! so that it writes over an existing file.
30    fileout1 = fileout1.substr(0,fileout1.size()-5); // remove the ".fits" on the end.
31    std::stringstream ss1;
32    ss1 << ".RECON"<<this->par.getAtrousCut()<<".fits";
33    fileout1 += ss1.str();
34    string syscall = "rm -f " + fileout1;
35    system(syscall.c_str());
36
37    status = 0;
38    fits_create_file(&fptrNew,fileout1.c_str(),&status);
39    if (status) fits_report_error(stderr, status);
40    status = 0;
41    fits_movabs_hdu(fptrOld, 1, NULL, &status);
42    if (status) fits_report_error(stderr, status);
43    status = 0;
44    fits_copy_hdu(fptrOld, fptrNew, 0, &status);
45    if (status) fits_report_error(stderr, status);
46    // Need to correct the dimensions, if we have subsectioned the image...
47    if(this->pars().getFlagSubsection()){
48      char *comment = new char[80];
49      long dud;
50      fits_read_key(fptrOld, TLONG, "NAXIS1", &dud, comment, &status);
51      fits_update_key(fptrNew, TLONG, "NAXIS1", &(this->axisDim[0]), comment, &status);
52      fits_read_key(fptrOld, TLONG, "NAXIS2", &dud, comment, &status);
53      fits_update_key(fptrNew, TLONG, "NAXIS2", &(this->axisDim[2]), comment, &status);
54      fits_read_key(fptrOld, TLONG, "NAXIS3", &dud, comment, &status);
55      fits_update_key(fptrNew, TLONG, "NAXIS3", &(this->axisDim[1]), comment, &status);
56    }
57
58    //////////////////////////////
59    ////// NEED TO ADD NEW HEADER KEYS
60    ////// INDICATING WHAT THE FILE IS...
61    //////////////////////////////
62
63    if(this->par.getFlagBlankPix())
64      fits_write_pixnull(fptrNew, TFLOAT, fpixel, this->numPixels, recon, &blankval, &status);
65    else  fits_write_pix(fptrNew, TFLOAT, fpixel, this->numPixels, recon, &status);
66
67    fits_close_file(fptrNew, &status);
68  }
69
70  if(this->par.getFlagOutputResid()){
71    string fileout2 = "!" + this->par.getImageFile(); // ! so that it writes over an existing file.
72    fileout2 = fileout2.substr(0,fileout2.size()-5);
73    std::stringstream ss2;
74    ss2 << ".RESID"<<this->par.getAtrousCut()<<".fits";
75    fileout2 += ss2.str();
76    string syscall = "rm -f " + fileout2;
77    system(syscall.c_str()); 
78
79    status = 0;
80    fits_create_file(&fptrNew,fileout2.c_str(),&status);
81    if (status) fits_report_error(stderr, status);
82    status = 0;
83    fits_movabs_hdu(fptrOld, 1, NULL, &status);
84    if (status) fits_report_error(stderr, status);
85    status = 0;
86    fits_copy_hdu(fptrOld, fptrNew, 0, &status);
87    if (status) fits_report_error(stderr, status);
88
89    // Need to correct the dimensions, if we have subsectioned the image...
90    if(this->pars().getFlagSubsection()){
91      char *comment = new char[80];
92      long dud;
93      fits_read_key(fptrOld, TLONG, "NAXIS1", &dud, comment, &status);
94      fits_update_key(fptrNew, TLONG, "NAXIS1", &(this->axisDim[0]), comment, &status);
95      fits_read_key(fptrOld, TLONG, "NAXIS2", &dud, comment, &status);
96      fits_update_key(fptrNew, TLONG, "NAXIS2", &(this->axisDim[2]), comment, &status);
97      fits_read_key(fptrOld, TLONG, "NAXIS3", &dud, comment, &status);
98      fits_update_key(fptrNew, TLONG, "NAXIS3", &(this->axisDim[1]), comment, &status);
99   }
100    //////////////////////////////
101    ////// NEED TO ADD NEW HEADER KEYS
102    ////// INDICATING WHAT THE FILE IS...
103    //////////////////////////////
104
105    if(this->par.getFlagBlankPix())
106      fits_write_pixnull(fptrNew, TFLOAT, fpixel, this->numPixels, resid, &blankval, &status);
107    else  fits_write_pix(fptrNew, TFLOAT, fpixel, this->numPixels, resid, &status);
108
109    fits_close_file(fptrNew, &status);
110  }
111
112  delete [] recon;
113  delete [] resid;
114  delete [] fpixel;
115  delete [] dimAxes;
116
117}
Note: See TracBrowser for help on using the repository browser.