1 | #include <duchamp/Cubes/WriteArray.hh> |
---|
2 | #include <duchamp/duchamp.hh> |
---|
3 | #include <duchamp/Cubes/cubes.hh> |
---|
4 | #include <fitsio.h> |
---|
5 | #include <wcslib/wcsunits.h> |
---|
6 | |
---|
7 | namespace duchamp { |
---|
8 | |
---|
9 | WriteArray::WriteArray(): |
---|
10 | itsCube(0),itsFptr(0),itsBitpix(-32),itsFilename(""),itsFlag2D(false) |
---|
11 | { |
---|
12 | } |
---|
13 | |
---|
14 | WriteArray::WriteArray(Cube *cube): |
---|
15 | itsCube(cube),itsFptr(0),itsBitpix(-32),itsFilename(""),itsFlag2D(false) |
---|
16 | { |
---|
17 | } |
---|
18 | |
---|
19 | WriteArray::WriteArray(Cube *cube, int bitpix): |
---|
20 | itsCube(cube),itsFptr(0),itsBitpix(bitpix),itsFilename(""),itsFlag2D(false) |
---|
21 | { |
---|
22 | } |
---|
23 | |
---|
24 | WriteArray::WriteArray(const WriteArray &other) |
---|
25 | { |
---|
26 | this->operator=(other); |
---|
27 | } |
---|
28 | |
---|
29 | WriteArray& WriteArray::operator= (const WriteArray& other) |
---|
30 | { |
---|
31 | if(this==&other) return *this; |
---|
32 | this->itsCube = other.itsCube; |
---|
33 | this->itsBitpix = other.itsBitpix; |
---|
34 | this->itsFilename = other.itsFilename; |
---|
35 | this->itsFptr = other.itsFptr; |
---|
36 | this->itsFlag2D = other.itsFlag2D; |
---|
37 | return *this; |
---|
38 | } |
---|
39 | |
---|
40 | OUTCOME WriteArray::openFile() |
---|
41 | { |
---|
42 | OUTCOME result=SUCCESS; |
---|
43 | int status=0; |
---|
44 | fits_create_file(&this->itsFptr,this->itsFilename.c_str(),&status); |
---|
45 | if(status){ |
---|
46 | DUCHAMPWARN("Reading Cube", "Error creating file " << this->itsFilename); |
---|
47 | fits_report_error(stderr, status); |
---|
48 | result = FAILURE; |
---|
49 | } |
---|
50 | |
---|
51 | if(result == SUCCESS){ |
---|
52 | result = this->writeBasicHeader(); |
---|
53 | } |
---|
54 | |
---|
55 | return result; |
---|
56 | } |
---|
57 | |
---|
58 | OUTCOME WriteArray::writeBasicHeader() |
---|
59 | { |
---|
60 | char *header, *hptr, keyname[9]; |
---|
61 | int i, nkeyrec, status = 0; |
---|
62 | |
---|
63 | const size_t naxis=this->itsCube->getNumDim(); |
---|
64 | long* naxes = new long[this->itsCube->getNumDim()]; |
---|
65 | for(size_t i=0;i<naxis;i++) naxes[i]=this->itsCube->getDimArray()[i]; |
---|
66 | if(this->itsFlag2D) naxes[this->itsCube->header().WCS().spec]=1; |
---|
67 | // write the required header keywords |
---|
68 | fits_write_imghdr(this->itsFptr, this->itsBitpix, naxis, naxes, &status); |
---|
69 | |
---|
70 | // Write beam information |
---|
71 | this->itsCube->header().beam().writeToFITS(this->itsFptr); |
---|
72 | |
---|
73 | // Write bunit information |
---|
74 | status = 0; |
---|
75 | strcpy(keyname,"BUNIT"); |
---|
76 | if (fits_update_key(this->itsFptr, TSTRING, keyname, (char *)this->itsCube->header().getFluxUnits().c_str(), NULL, &status)){ |
---|
77 | DUCHAMPWARN("saveImage","Error writing bunit info:"); |
---|
78 | fits_report_error(stderr, status); |
---|
79 | return FAILURE; |
---|
80 | } |
---|
81 | |
---|
82 | // convert the wcsprm struct to a set of 80-char keys |
---|
83 | if ((status = wcshdo(WCSHDO_all, this->itsCube->header().getWCS(), &nkeyrec, &header))) { |
---|
84 | DUCHAMPWARN("saveImage","Could not convert WCS information to FITS header. WCS Error Code = "<<status<<": "<<wcs_errmsg[status]); |
---|
85 | return FAILURE; |
---|
86 | } |
---|
87 | |
---|
88 | hptr = header; |
---|
89 | strncpy(keyname,hptr,8); |
---|
90 | for (i = 0; i < nkeyrec; i++, hptr += 80) { |
---|
91 | status=0; |
---|
92 | if(fits_update_card(this->itsFptr,keyname,hptr,&status)){ |
---|
93 | DUCHAMPWARN("saveImage","Error writing header card"); |
---|
94 | fits_report_error(stderr,status); |
---|
95 | return FAILURE; |
---|
96 | } |
---|
97 | } |
---|
98 | |
---|
99 | if(this->itsBitpix>0){ |
---|
100 | if(this->itsCube->pars().getFlagBlankPix()){ |
---|
101 | strcpy(keyname,"BSCALE"); |
---|
102 | float bscale=this->itsCube->header().getBscaleKeyword(); |
---|
103 | if(fits_update_key(this->itsFptr, TFLOAT, keyname, &bscale, NULL, &status)){ |
---|
104 | duchampFITSerror(status,"saveImage","Error writing BSCALE header:"); |
---|
105 | } |
---|
106 | strcpy(keyname,"BZERO"); |
---|
107 | float bzero=this->itsCube->header().getBzeroKeyword(); |
---|
108 | if(fits_update_key(this->itsFptr, TFLOAT, keyname, &bzero, NULL, &status)){ |
---|
109 | duchampFITSerror(status,"saveImage","Error writing BZERO header:"); |
---|
110 | } |
---|
111 | strcpy(keyname,"BLANK"); |
---|
112 | int blank=this->itsCube->header().getBlankKeyword(); |
---|
113 | if(fits_update_key(this->itsFptr, TINT, keyname, &blank, NULL, &status)){ |
---|
114 | duchampFITSerror(status,"saveImage","Error writing BLANK header:"); |
---|
115 | } |
---|
116 | if(fits_set_imgnull(this->itsFptr, blank, &status)){ |
---|
117 | duchampFITSerror(status, "saveImage", "Error setting null value:"); |
---|
118 | } |
---|
119 | if(fits_set_bscale(this->itsFptr, bscale, bzero, &status)){ |
---|
120 | duchampFITSerror(status,"saveImage","Error setting scale:"); |
---|
121 | } |
---|
122 | } |
---|
123 | } |
---|
124 | |
---|
125 | delete [] naxes; |
---|
126 | |
---|
127 | return SUCCESS; |
---|
128 | |
---|
129 | } |
---|
130 | |
---|
131 | OUTCOME WriteArray::closeFile() |
---|
132 | { |
---|
133 | OUTCOME result=SUCCESS; |
---|
134 | int status = 0; |
---|
135 | if(this->itsFptr==0){ |
---|
136 | fits_close_file(this->itsFptr, &status); |
---|
137 | if (status){ |
---|
138 | DUCHAMPWARN("Reading Cube", "Error closing file " << this->itsFilename); |
---|
139 | fits_report_error(stderr, status); |
---|
140 | result=FAILURE; |
---|
141 | } |
---|
142 | } |
---|
143 | return result; |
---|
144 | } |
---|
145 | |
---|
146 | } |
---|