source: trunk/src/FitsIO/WriteMaskArray.cc @ 1123

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

Moving the code that reads from and writes to FITS files containing reconstructed, momentmap, mask etc arrays to the FitsIO directory, away from Cubes. Updating all include statements as well.

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