source: branches/OptimisedGrowerTesting/src/FitsIO/WriteMaskArray.cc @ 1441

Last change on this file since 1441 was 1129, checked in by MatthewWhiting, 12 years ago

Another missing include...

File size: 3.9 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    this->itsBitpix=LONG_IMG;
14  }
15 
16  WriteMaskArray::WriteMaskArray(Cube *cube):
17    WriteArray(cube,LONG_IMG)
18  {
19  }
20
21  WriteMaskArray::WriteMaskArray(const WriteMaskArray &other)
22  {
23    this->operator=(other);
24  }
25
26  WriteMaskArray::WriteMaskArray(const WriteArray &base)
27  {
28    this->operator=(base);
29  }
30
31  WriteMaskArray& WriteMaskArray::operator= (const WriteMaskArray& other)
32  {
33    if(this==&other) return *this;
34    ((WriteArray &) *this) = other;
35    return *this;
36  }
37
38  WriteMaskArray& WriteMaskArray::operator= (const WriteArray& base)
39  {
40    if(this==&base) return *this;
41    ((WriteArray &) *this) = base;
42    return *this;
43  }
44
45  OUTCOME WriteMaskArray::writeHeader()
46  {
47    /// @details
48    ///   A simple function that writes all the necessary keywords and comments
49    ///    to the FITS header pointed to by fptr.
50    ///   The keyword names and comments are taken from duchamp.hh
51
52    OUTCOME result=SUCCESS;
53    int status = 0;
54    if(fits_write_history(this->itsFptr, (char *)header_maskHistory.c_str(), &status)){
55      duchampFITSerror(status,"writeMaskArray","Error : header I/O");
56      result=FAILURE;
57    }
58    status = 0;
59    if(fits_write_history(this->itsFptr, (char *)header_maskHistory_input.c_str(),&status)){
60      duchampFITSerror(status,"writeMaskArray","Error : header I/O");
61      result=FAILURE;
62    }
63    status = 0;
64    if(fits_write_history(this->itsFptr, (char *)this->itsCube->pars().getImageFile().c_str(), &status)){
65      duchampFITSerror(status,"writeMaskArray","Error : header I/O");
66      result=FAILURE;
67    }
68   
69    if(this->itsCube->pars().getFlagSubsection()){
70      status = 0;
71      if( fits_write_comment(this->itsFptr,(char *)header_maskSubsection_comment.c_str(), &status)){
72        duchampFITSerror(status,"writeMaskArray","Error : header I/O");
73        result=FAILURE;
74      }
75      status = 0;
76      if( fits_write_key(this->itsFptr, TSTRING, (char *)keyword_subsection.c_str(),
77                         (char *)this->itsCube->pars().getSubsection().c_str(),
78                         (char *)comment_subsection.c_str(), &status) ){
79        duchampFITSerror(status,"writeMaskArray","Error : header I/O");
80        result=FAILURE;
81      }
82    }
83
84    char *comment = new char[FLEN_COMMENT];
85    strcpy(comment,"");
86    char *keyword = new char[FLEN_KEYWORD];
87    std::string newunits = (this->itsCube->pars().getFlagMaskWithObjectNum()) ? "Object ID" : "Detection flag";
88    strcpy(keyword,"BUNIT");
89    if(fits_update_key(this->itsFptr, TSTRING, keyword, (char *)newunits.c_str(), comment, &status)){
90      duchampFITSerror(status,"writeMaskArray","Error writing BUNIT header:");
91      result = FAILURE;
92    }
93    delete [] comment;
94    delete [] keyword;
95
96    return result;
97
98  }
99
100  OUTCOME WriteMaskArray::writeData()
101  {
102    OUTCOME result = SUCCESS;
103    int *mask = new int[this->itsCube->getSize()];
104    for(size_t i=0;i<this->itsCube->getSize();i++) mask[i]=0;
105    std::vector<Detection>::iterator obj;
106    for(obj=this->itsCube->pObjectList()->begin();obj<this->itsCube->pObjectList()->end();obj++){
107      std::vector<PixelInfo::Voxel> voxlist = obj->getPixelSet();
108      std::vector<PixelInfo::Voxel>::iterator vox;
109      for(vox=voxlist.begin();vox<voxlist.end();vox++){
110        size_t pixelpos = vox->getX() + this->itsCube->getDimX()*vox->getY() +
111          this->itsCube->getDimX()*this->itsCube->getDimY()*vox->getZ();
112        if(this->itsCube->pars().getFlagMaskWithObjectNum()) mask[pixelpos] = obj->getID();
113        else mask[pixelpos] = 1;
114      }
115    }
116    int status=0;
117    long group=0;
118    LONGLONG first=1;
119    LONGLONG nelem=LONGLONG(this->itsCube->getSize());
120    if(fits_write_img_int(this->itsFptr, group, first, nelem, mask, &status)){
121      duchampFITSerror(status,"writeMaskArray","Error writing mask array:");
122      result = FAILURE;
123    }
124
125    return result;
126
127  }
128
129}
130
Note: See TracBrowser for help on using the repository browser.