source: trunk/src/Cubes/getImage.cc @ 159

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

Trying to improve the FITS-I/O, to be more robust about the ordering of the
axes. Not quite there yet, but do a commit to make sure the work is saved.

File size: 4.3 KB
Line 
1#include <iostream>
2#include <string>
3#include <string.h>
4#include <wcs.h>
5#include <wcshdr.h>
6#include <fitshdr.h>
7#include <wcsfix.h>
8#include <wcsunits.h>
9#define WCSLIB_GETWCSTAB // define this so that we don't try and redefine
10                         //  wtbarr (this is a problem when using gcc v.4+
11#include <fitsio.h>
12#include <math.h>
13#include <duchamp.hh>
14#include <Cubes/cubes.hh>
15
16string imageType[4] = {"point", "spectrum", "image", "cube"};
17
18int Cube::getCube(string fname)
19{
20  /**
21   * Cube::getCube(string )
22   *  Read in a cube from the file fname (assumed to be in FITS format).
23   *  Function reads in the following:
24   *      - axis dimensions
25   *      - pixel array
26   *      - WCS information (in form of WCSLIB wcsprm structure)
27   *      - Header keywords: BUNIT (brightness unit),
28   *                         BLANK, BZERO, BSCALE (for blank pixel value)
29   *                         BMAJ, BMIN, CDELT1, CDELT2 (for beam size)
30   */
31
32
33//   short int maxdim=3;
34//   long *dimAxes = new long[maxdim];
35//   for(int i=0;i<maxdim;i++) dimAxes[i]=1;
36  long nelements;
37  int bitpix,numAxes;
38  int status = 0,  nkeys;  /* MUST initialize status */
39  fitsfile *fptr;         
40
41  status = 0;
42  if( fits_open_file(&fptr,fname.c_str(),READONLY,&status) ){
43    fits_report_error(stderr, status);
44    return FAILURE;
45  }
46
47  status = 0;
48  if(fits_get_img_dim(fptr, &numAxes, &status)){
49    fits_report_error(stderr, status);
50    return FAILURE;
51  }
52  long *dimAxes = new long[numAxes];
53  for(int i=0;i<numAxes;i++) dimAxes[i]=1;
54  status = 0;
55  if(fits_get_img_size(fptr, numAxes, dimAxes, &status)){
56    fits_report_error(stderr, status);
57    return FAILURE;
58  }
59//   status = 0;
60//   fits_get_img_param(fptr, maxdim, &bitpix, &numAxes, dimAxes, &status);
61//   if(status){
62//     fits_report_error(stderr, status);
63//     return FAILURE;
64//   }
65
66  long *fpixel = new long[numAxes];
67  for(int i=0;i<numAxes;i++) fpixel[i]=1;
68
69  if(numAxes<=3)  std::cout << "Dimensions of " << imageType[numAxes];
70  else std::cout << "Dimensions of " << imageType[3];
71  int dim = 0;
72  std::cout << ": " << dimAxes[dim];
73  while(dim+1<numAxes) std::cout << "x" << dimAxes[++dim];
74  std::cout << std::endl;
75
76  int npix = dimAxes[0]*dimAxes[1]*dimAxes[2];
77  float *array = new float[npix];
78  int anynul;
79  char *nullarray = new char[npix];
80  for(int i=0;i<npix;i++) nullarray[i] = 0;
81
82  //-------------------------------------------------------------
83  // Reading in the pixel array.
84  //  The location of any blank pixels (as determined by the header keywords)
85  //   is stored in nullarray (set to 1). Also anynul will be set to 1 to
86  //   indicate the presence of blank pixels.
87  //   If anynul==1 then all pixels are non-blank.
88
89  status = 0;
90  fits_read_pixnull(fptr, TFLOAT, fpixel, npix, array,
91                    nullarray, &anynul, &status);
92//   fits_read_pix(fptr, TFLOAT, fpixel, npix, NULL, array, &anynul, &status);
93  if(status){
94    duchampError("getCube","There was an error reading in the data array:");
95    fits_report_error(stderr, status);
96    return FAILURE;
97  }
98
99  if(anynul==0){   
100    // no blank pixels, so don't bother with any trimming or checking...
101    if(this->par.getFlagBlankPix()) { 
102      // if user requested fixing, inform them of change.
103      duchampWarning("getCube",
104                     "No blank pixels, so setting flagBlankPix to false.\n");
105    }
106    this->par.setFlagBlankPix(false);
107  }
108 
109  status = 0;
110  fits_close_file(fptr, &status);
111  if (status){
112    duchampWarning("getCube","Error closing file: ");
113    fits_report_error(stderr, status);
114  }
115
116  FitsHeader newHead;
117
118  newHead.readHeaderInfo(fname, this->par);
119  this->par.copyHeaderInfo(newHead);
120  this->setHead(newHead);
121
122
123  this->initialiseCube(dimAxes);
124  this->saveArray(array,npix);
125  //-------------------------------------------------------------
126  // Once the array is saved, change the value of the blank pixels from
127  // 0 (as they are set by fits_read_pixnull) to the correct blank value
128  // as determined by the above code.
129  for(int i=0; i<npix;i++){
130//     if(nullarray[i]==1) this->array[i] = blank*bscale + bzero; 
131    if(isnan(array[i])) this->array[i] = par.getBlankPixVal();
132    if(nullarray[i]==1) this->array[i] = this->par.getBlankPixVal(); 
133  }
134
135
136  if(!newHead.isWCS())
137    duchampWarning("getCube","WCS is not good enough to be used.\n");
138
139  //  delete hdr;
140  delete [] array;
141  delete [] fpixel;
142  delete [] dimAxes;
143
144  return SUCCESS;
145
146}
147
Note: See TracBrowser for help on using the repository browser.