source: trunk/src/FitsIO/WriteMaskArray.cc

Last change on this file was 1355, checked in by MatthewWhiting, 10 years ago

Fixing build error with recent changes (on gcc-4.4.5 on debian)

File size: 6.2 KB
Line 
1#include <duchamp/FitsIO/WriteMaskArray.hh>
2#include <duchamp/FitsIO/WriteArray.hh>
3#include <duchamp/duchamp.hh>
4#include <duchamp/Cubes/cubes.hh>
5#include <fitsio.h>
6#include <string.h>
7
8namespace duchamp {
9
10  WriteMaskArray::WriteMaskArray():
11    WriteArray()
12  {
13    itsBitpix = SHORT_IMG;
14  }
15 
16  WriteMaskArray::WriteMaskArray(Cube *cube):
17    WriteArray(cube, SHORT_IMG)
18  {
19  }
20
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   
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");
97    if(fits_update_key(this->itsFptr, TSTRING, keyword, (char *)newunits.c_str(), comment, &status)){
98      duchampFITSerror(status,"writeMaskArray","Error writing BUNIT header:");
99      result = FAILURE;
100    }
101    delete [] comment;
102    delete [] keyword;
103
104    return result;
105
106  }
107
108  OUTCOME WriteMaskArray::writeData()
109  {
110    OUTCOME result = SUCCESS;
111     
112      if(this->itsCube->pars().getFlagMaskWithObjectNum()){
113          if(this->itsBitpix==SHORT_IMG)
114            result = writeDataObjNumMask_int();
115          else
116            result = writeDataObjNumMask_long();
117      }
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
147    }
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       
172    }
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    }
198
199
200}
201
Note: See TracBrowser for help on using the repository browser.