[1123] | 1 | #include <duchamp/FitsIO/WriteMaskArray.hh> |
---|
| 2 | #include <duchamp/FitsIO/WriteArray.hh> |
---|
[1115] | 3 | #include <duchamp/duchamp.hh> |
---|
| 4 | #include <duchamp/Cubes/cubes.hh> |
---|
| 5 | #include <fitsio.h> |
---|
[1129] | 6 | #include <string.h> |
---|
[1115] | 7 | |
---|
| 8 | namespace duchamp { |
---|
| 9 | |
---|
| 10 | WriteMaskArray::WriteMaskArray(): |
---|
[1355] | 11 | WriteArray() |
---|
[1115] | 12 | { |
---|
[1355] | 13 | itsBitpix = SHORT_IMG; |
---|
[1115] | 14 | } |
---|
| 15 | |
---|
| 16 | WriteMaskArray::WriteMaskArray(Cube *cube): |
---|
[1354] | 17 | WriteArray(cube, SHORT_IMG) |
---|
[1115] | 18 | { |
---|
| 19 | } |
---|
| 20 | |
---|
[1354] | 21 | void WriteMaskArray::setCorrectBitpix() |
---|
| 22 | { |
---|
| 23 | if(this->itsCube->pars().getFlagMaskWithObjectNum()){ |
---|
| 24 | long shortImgMax=32768; |
---|
| 25 | if(this->itsCube->getNumObj()>=shortImgMax) this->itsBitpix=LONG_IMG; |
---|
| 26 | } |
---|
| 27 | } |
---|
| 28 | |
---|
[1115] | 29 | WriteMaskArray::WriteMaskArray(const WriteMaskArray &other) |
---|
| 30 | { |
---|
| 31 | this->operator=(other); |
---|
| 32 | } |
---|
| 33 | |
---|
| 34 | WriteMaskArray::WriteMaskArray(const WriteArray &base) |
---|
| 35 | { |
---|
| 36 | this->operator=(base); |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | WriteMaskArray& WriteMaskArray::operator= (const WriteMaskArray& other) |
---|
| 40 | { |
---|
| 41 | if(this==&other) return *this; |
---|
| 42 | ((WriteArray &) *this) = other; |
---|
| 43 | return *this; |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | WriteMaskArray& WriteMaskArray::operator= (const WriteArray& base) |
---|
| 47 | { |
---|
| 48 | if(this==&base) return *this; |
---|
| 49 | ((WriteArray &) *this) = base; |
---|
| 50 | return *this; |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | OUTCOME WriteMaskArray::writeHeader() |
---|
| 54 | { |
---|
| 55 | /// @details |
---|
| 56 | /// A simple function that writes all the necessary keywords and comments |
---|
| 57 | /// to the FITS header pointed to by fptr. |
---|
| 58 | /// The keyword names and comments are taken from duchamp.hh |
---|
| 59 | |
---|
| 60 | OUTCOME result=SUCCESS; |
---|
| 61 | int status = 0; |
---|
| 62 | if(fits_write_history(this->itsFptr, (char *)header_maskHistory.c_str(), &status)){ |
---|
| 63 | duchampFITSerror(status,"writeMaskArray","Error : header I/O"); |
---|
| 64 | result=FAILURE; |
---|
| 65 | } |
---|
| 66 | status = 0; |
---|
| 67 | if(fits_write_history(this->itsFptr, (char *)header_maskHistory_input.c_str(),&status)){ |
---|
| 68 | duchampFITSerror(status,"writeMaskArray","Error : header I/O"); |
---|
| 69 | result=FAILURE; |
---|
| 70 | } |
---|
| 71 | status = 0; |
---|
| 72 | if(fits_write_history(this->itsFptr, (char *)this->itsCube->pars().getImageFile().c_str(), &status)){ |
---|
| 73 | duchampFITSerror(status,"writeMaskArray","Error : header I/O"); |
---|
| 74 | result=FAILURE; |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | if(this->itsCube->pars().getFlagSubsection()){ |
---|
| 78 | status = 0; |
---|
| 79 | if( fits_write_comment(this->itsFptr,(char *)header_maskSubsection_comment.c_str(), &status)){ |
---|
| 80 | duchampFITSerror(status,"writeMaskArray","Error : header I/O"); |
---|
| 81 | result=FAILURE; |
---|
| 82 | } |
---|
| 83 | status = 0; |
---|
| 84 | if( fits_write_key(this->itsFptr, TSTRING, (char *)keyword_subsection.c_str(), |
---|
| 85 | (char *)this->itsCube->pars().getSubsection().c_str(), |
---|
| 86 | (char *)comment_subsection.c_str(), &status) ){ |
---|
| 87 | duchampFITSerror(status,"writeMaskArray","Error : header I/O"); |
---|
| 88 | result=FAILURE; |
---|
| 89 | } |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | char *comment = new char[FLEN_COMMENT]; |
---|
| 93 | strcpy(comment,""); |
---|
| 94 | char *keyword = new char[FLEN_KEYWORD]; |
---|
| 95 | std::string newunits = (this->itsCube->pars().getFlagMaskWithObjectNum()) ? "Object ID" : "Detection flag"; |
---|
| 96 | strcpy(keyword,"BUNIT"); |
---|
[1116] | 97 | if(fits_update_key(this->itsFptr, TSTRING, keyword, (char *)newunits.c_str(), comment, &status)){ |
---|
[1120] | 98 | duchampFITSerror(status,"writeMaskArray","Error writing BUNIT header:"); |
---|
[1115] | 99 | result = FAILURE; |
---|
| 100 | } |
---|
| 101 | delete [] comment; |
---|
| 102 | delete [] keyword; |
---|
| 103 | |
---|
| 104 | return result; |
---|
| 105 | |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | OUTCOME WriteMaskArray::writeData() |
---|
| 109 | { |
---|
[1116] | 110 | OUTCOME result = SUCCESS; |
---|
[1354] | 111 | |
---|
| 112 | if(this->itsCube->pars().getFlagMaskWithObjectNum()){ |
---|
| 113 | if(this->itsBitpix==SHORT_IMG) |
---|
| 114 | result = writeDataObjNumMask_int(); |
---|
| 115 | else |
---|
| 116 | result = writeDataObjNumMask_long(); |
---|
[1115] | 117 | } |
---|
[1354] | 118 | else |
---|
| 119 | result = writeDataSimpleMask(); |
---|
| 120 | |
---|
| 121 | return result; |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | OUTCOME WriteMaskArray::writeDataSimpleMask() |
---|
| 125 | { |
---|
| 126 | OUTCOME result = SUCCESS; |
---|
| 127 | |
---|
| 128 | size_t size=this->itsCube->getSize(); |
---|
| 129 | int *mask = new int[size]; |
---|
| 130 | for(size_t i=0;i<size;i++) mask[i]=0; |
---|
| 131 | std::vector<Detection>::iterator obj; |
---|
| 132 | for(obj=this->itsCube->pObjectList()->begin();obj<this->itsCube->pObjectList()->end();obj++){ |
---|
| 133 | std::vector<PixelInfo::Voxel> voxlist = obj->getPixelSet(); |
---|
| 134 | std::vector<PixelInfo::Voxel>::iterator vox; |
---|
| 135 | for(vox=voxlist.begin();vox<voxlist.end();vox++){ |
---|
| 136 | size_t pixelpos = vox->getX() + this->itsCube->getDimX()*vox->getY() + this->itsCube->getDimX()*this->itsCube->getDimY()*vox->getZ(); |
---|
| 137 | mask[pixelpos] = 1; |
---|
| 138 | } |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | result = this->writeToFITS_int(size,mask); |
---|
| 142 | |
---|
| 143 | delete [] mask; |
---|
| 144 | |
---|
| 145 | return result; |
---|
| 146 | |
---|
[1116] | 147 | } |
---|
[1354] | 148 | |
---|
| 149 | OUTCOME WriteMaskArray::writeDataObjNumMask_int() |
---|
| 150 | { |
---|
| 151 | OUTCOME result = SUCCESS; |
---|
| 152 | |
---|
| 153 | size_t size=this->itsCube->getSize(); |
---|
| 154 | int *mask = new int[size]; |
---|
| 155 | for(size_t i=0;i<size;i++) mask[i]=0; |
---|
| 156 | std::vector<Detection>::iterator obj; |
---|
| 157 | for(obj=this->itsCube->pObjectList()->begin();obj<this->itsCube->pObjectList()->end();obj++){ |
---|
| 158 | std::vector<PixelInfo::Voxel> voxlist = obj->getPixelSet(); |
---|
| 159 | std::vector<PixelInfo::Voxel>::iterator vox; |
---|
| 160 | for(vox=voxlist.begin();vox<voxlist.end();vox++){ |
---|
| 161 | size_t pixelpos = vox->getX() + this->itsCube->getDimX()*vox->getY() + this->itsCube->getDimX()*this->itsCube->getDimY()*vox->getZ(); |
---|
| 162 | mask[pixelpos] = obj->getID(); |
---|
| 163 | } |
---|
| 164 | } |
---|
| 165 | |
---|
| 166 | result = this->writeToFITS_int(size,mask); |
---|
| 167 | |
---|
| 168 | delete [] mask; |
---|
| 169 | |
---|
| 170 | return result; |
---|
| 171 | |
---|
[1116] | 172 | } |
---|
[1354] | 173 | |
---|
| 174 | OUTCOME WriteMaskArray::writeDataObjNumMask_long() |
---|
| 175 | { |
---|
| 176 | OUTCOME result = SUCCESS; |
---|
| 177 | |
---|
| 178 | size_t size=this->itsCube->getSize(); |
---|
| 179 | long *mask = new long[size]; |
---|
| 180 | for(size_t i=0;i<size;i++) mask[i]=0; |
---|
| 181 | std::vector<Detection>::iterator obj; |
---|
| 182 | for(obj=this->itsCube->pObjectList()->begin();obj<this->itsCube->pObjectList()->end();obj++){ |
---|
| 183 | std::vector<PixelInfo::Voxel> voxlist = obj->getPixelSet(); |
---|
| 184 | std::vector<PixelInfo::Voxel>::iterator vox; |
---|
| 185 | for(vox=voxlist.begin();vox<voxlist.end();vox++){ |
---|
| 186 | size_t pixelpos = vox->getX() + this->itsCube->getDimX()*vox->getY() + this->itsCube->getDimX()*this->itsCube->getDimY()*vox->getZ(); |
---|
| 187 | mask[pixelpos] = obj->getID(); |
---|
| 188 | } |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | result = this->writeToFITS_long(size,mask); |
---|
| 192 | |
---|
| 193 | delete [] mask; |
---|
| 194 | |
---|
| 195 | return result; |
---|
| 196 | |
---|
| 197 | } |
---|
[1115] | 198 | |
---|
[1116] | 199 | |
---|
[1115] | 200 | } |
---|
| 201 | |
---|