source: tags/release-1.6.1/src/FitsIO/WriteArray.cc @ 1441

Last change on this file since 1441 was 1354, checked in by MatthewWhiting, 10 years ago

#209 - Changing default bitpix for masks to SHORT_IMG. Also making front-end functions to the writing of arrays. These reside in WriteArray?, and cover float (with blank pixel handling), int and long. Masks can use long if there are lots (>32K) of objects.

File size: 5.4 KB
Line 
1#include <duchamp/FitsIO/WriteArray.hh>
2#include <duchamp/duchamp.hh>
3#include <duchamp/Cubes/cubes.hh>
4#include <fitsio.h>
5#include <string.h>
6#include <wcslib/wcsunits.h>
7
8namespace duchamp {
9
10  WriteArray::WriteArray():
11    itsCube(0),itsFilename(""),itsBitpix(-32),itsFlag2D(false),itsFptr(0)
12  {
13  }
14
15  WriteArray::WriteArray(Cube *cube):
16    itsCube(cube),itsFilename(""),itsBitpix(-32),itsFlag2D(false),itsFptr(0)
17  {
18  }
19
20  WriteArray::WriteArray(Cube *cube, int bitpix):
21    itsCube(cube),itsFilename(""),itsBitpix(bitpix),itsFlag2D(false),itsFptr(0)
22  {
23  }
24
25  WriteArray::WriteArray(const WriteArray &other)
26  {
27    this->operator=(other);
28  }
29
30  WriteArray& WriteArray::operator= (const WriteArray& other)
31  {
32    if(this==&other) return *this;
33    this->itsCube = other.itsCube;
34    this->itsBitpix = other.itsBitpix;
35    this->itsFilename = other.itsFilename;
36    this->itsFptr = other.itsFptr;
37    this->itsFlag2D = other.itsFlag2D;
38    return *this;
39  }
40
41  OUTCOME WriteArray::write()
42  {
43    if(this->openFile()==FAILURE) return FAILURE;
44    if(this->writeBasicHeader()==FAILURE) return FAILURE;
45    if(this->writeHeader()==FAILURE) return FAILURE;
46    if(this->writeData()==FAILURE) return FAILURE;
47    if(this->closeFile()==FAILURE) return FAILURE;
48    return SUCCESS;
49  }
50
51  OUTCOME WriteArray::openFile()
52  {
53    OUTCOME result=SUCCESS;
54    int status=0;
55    this->itsFilename = "!"+this->itsFilename;
56    fits_create_file(&this->itsFptr,this->itsFilename.c_str(),&status);
57    if(status){
58      DUCHAMPWARN("Reading Cube", "Error creating file " << this->itsFilename);
59      fits_report_error(stderr, status);
60      result = FAILURE;
61    }
62   
63    return result;
64  }
65
66  OUTCOME WriteArray::writeBasicHeader()
67  {
68    char *header, *hptr, keyname[9];
69    int  i, nkeyrec, status = 0;
70   
71    size_t naxis=this->itsCube->getNumDim();
72    long* naxes = new long[this->itsCube->getNumDim()];
73    for(size_t i=0;i<naxis;i++) naxes[i]=this->itsCube->getDimArray()[i];
74    if(this->itsFlag2D){
75      naxes[this->itsCube->header().WCS().spec]=1;
76      naxis=2;
77    }
78    // write the required header keywords
79    fits_write_imghdr(this->itsFptr, this->itsBitpix, naxis, naxes,  &status);
80
81    // Write beam information
82    this->itsCube->header().beam().writeToFITS(this->itsFptr);
83
84    // Write bunit information
85    status = 0;
86    strcpy(keyname,"BUNIT");
87    if (fits_update_key(this->itsFptr, TSTRING, keyname, (char *)this->itsCube->header().getFluxUnits().c_str(), NULL, &status)){
88      DUCHAMPWARN("saveImage","Error writing bunit info:");
89      fits_report_error(stderr, status);
90      return FAILURE;
91    }
92
93    // convert the wcsprm struct to a set of 80-char keys
94    int oldnaxis = this->itsCube->header().WCS().naxis;
95    if(this->itsFlag2D) this->itsCube->header().WCS().naxis=2;
96    if ((status = wcshdo(WCSHDO_all, this->itsCube->header().getWCS(), &nkeyrec, &header))) {
97      DUCHAMPWARN("saveImage","Could not convert WCS information to FITS header. WCS Error Code = "<<status<<": "<<wcs_errmsg[status]);
98      return FAILURE;
99    }
100    if(this->itsFlag2D) this->itsCube->header().WCS().naxis = oldnaxis;
101
102    hptr = header;
103    strncpy(keyname,hptr,8);
104    for (i = 0; i < nkeyrec; i++, hptr += 80) {
105      status=0;
106      if(fits_update_card(this->itsFptr,keyname,hptr,&status)){
107        DUCHAMPWARN("saveImage","Error writing header card");
108        fits_report_error(stderr,status);
109        return FAILURE;
110      }
111    }
112
113    delete [] naxes;
114
115    return SUCCESS;
116
117  }
118
119  OUTCOME WriteArray::closeFile()
120  {
121    OUTCOME result=SUCCESS;
122    int status = 0;
123    if(this->itsFptr!=0){
124      fits_close_file(this->itsFptr, &status);
125      if (status){
126        DUCHAMPWARN("Reading Cube", "Error closing file " << this->itsFilename);
127        fits_report_error(stderr, status);
128        result=FAILURE;
129      }
130    }
131    return result;
132  }
133   
134    OUTCOME WriteArray::writeToFITS_flt(size_t size, float *array)
135    {
136        OUTCOME result = SUCCESS;
137       
138        long group=0;
139        int status=0;
140        LONGLONG first=1;
141        LONGLONG nelem = LONGLONG(size);
142        if(this->itsCube->pars().getFlagBlankPix()){
143            float nulval = this->itsCube->pars().getBlankPixVal();
144            fits_write_imgnull_flt(this->itsFptr, group, first, nelem, array, nulval, &status);
145        }
146        else
147        fits_write_img_flt(this->itsFptr, group, first, nelem, array, &status);
148       
149        if(status){
150            duchampFITSerror(status,"writeArray","Error writing array:");
151            result = FAILURE;
152        }
153       
154        return result;
155       
156    }
157
158    OUTCOME WriteArray::writeToFITS_int(size_t size, int *array)
159    {
160        OUTCOME result = SUCCESS;
161       
162        long group=0;
163        int status=0;
164        LONGLONG first=1;
165        LONGLONG nelem = LONGLONG(size);
166        fits_write_img_int(this->itsFptr, group, first, nelem, array, &status);
167       
168        if(status){
169            duchampFITSerror(status,"writeArray","Error writing array:");
170            result = FAILURE;
171        }
172       
173        return result;
174       
175    }
176
177    OUTCOME WriteArray::writeToFITS_long(size_t size, long *array)
178    {
179        OUTCOME result = SUCCESS;
180       
181        long group=0;
182        int status=0;
183        LONGLONG first=1;
184        LONGLONG nelem = LONGLONG(size);
185        fits_write_img_lng(this->itsFptr, group, first, nelem, array, &status);
186       
187        if(status){
188            duchampFITSerror(status,"writeArray","Error writing array:");
189            result = FAILURE;
190        }
191       
192        return result;
193       
194    }
195
196}
Note: See TracBrowser for help on using the repository browser.