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 | |
---|
8 | namespace 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 | } |
---|