1 | #ifndef FITSBLKRW_H
|
---|
2 | #define FITSBLKRW_H
|
---|
3 |
|
---|
4 | #include "fitsinoutfile.h"
|
---|
5 |
|
---|
6 | namespace SOPHYA {
|
---|
7 |
|
---|
8 | /*!
|
---|
9 | \ingroup FitsIOServer
|
---|
10 | \brief Template class with static methods for handling bloc data
|
---|
11 | read from / write to fits files
|
---|
12 | */
|
---|
13 |
|
---|
14 | template <class T>
|
---|
15 | class FitsBlockRW {
|
---|
16 | public:
|
---|
17 | //! Write image HDU data to a fits file
|
---|
18 | static void WriteImageData(FitsInOutFile& fios, const T * d, size_t sz,
|
---|
19 | long * fpixel=NULL)
|
---|
20 | {
|
---|
21 | int status = 0;
|
---|
22 | long fpix[5] = {1,1,1,1,1};
|
---|
23 | if (fpixel == NULL) fpixel = fpix;
|
---|
24 | T * ncd = const_cast<T *>(d);
|
---|
25 | fits_write_pix(fios.FitsPtr(), FitsTypes::DataType(d[0]), fpixel,
|
---|
26 | sz, ncd, &status);
|
---|
27 | if ( status ) {
|
---|
28 | fits_report_error(stderr, status);
|
---|
29 | char buff[32];
|
---|
30 | fits_get_errstatus(status, buff);
|
---|
31 | string msg = "FitsBlockRW<T>::WriteImageData() Error: " ;
|
---|
32 | msg += buff;
|
---|
33 | throw FitsIOException(msg);
|
---|
34 | }
|
---|
35 | }
|
---|
36 |
|
---|
37 | //! Read image HDU data from a fits file
|
---|
38 | static void ReadImageData(FitsInOutFile& fios, T * d, size_t sz,
|
---|
39 | long * fpixel=NULL)
|
---|
40 | {
|
---|
41 | int status = 0;
|
---|
42 | long fpix[5] = {1,1,1,1,1};
|
---|
43 | if (fpixel == NULL) fpixel = fpix;
|
---|
44 | int anynul = 0;
|
---|
45 | fits_read_pix(fios.FitsPtr(), FitsTypes::DataType(d[0]), fpixel,
|
---|
46 | sz, NULL, d, &anynul, &status);
|
---|
47 | if ( status ) {
|
---|
48 | fits_report_error(stderr, status);
|
---|
49 | char buff[32];
|
---|
50 | fits_get_errstatus(status, buff);
|
---|
51 | string msg = "FitsBlockRW<T>::ReadImageData() Error: " ;
|
---|
52 | msg += buff;
|
---|
53 | throw FitsIOException(msg);
|
---|
54 | }
|
---|
55 |
|
---|
56 | }
|
---|
57 |
|
---|
58 |
|
---|
59 | /*!
|
---|
60 | Write binary/ascii HDU data to a fits file.
|
---|
61 | See cfitsio function fits_write_col() for more detail.
|
---|
62 | \param colnum : table column number (starting from 1)
|
---|
63 | \param firstrow : the write operation starting row (starting from 1)
|
---|
64 | \param firstelem : the firstelem (for vector type columns)
|
---|
65 | \param d : pointer to data to be written
|
---|
66 | \param sz : number of data elements to be written
|
---|
67 | */
|
---|
68 | static void WriteColumnData(FitsInOutFile& fios, int colnum, long firstrow,
|
---|
69 | long firstelem, const T * d, size_t sz)
|
---|
70 | {
|
---|
71 | int status = 0;
|
---|
72 | T * ncd = const_cast<T *>(d);
|
---|
73 | fits_write_col(fios.FitsPtr(), FitsTypes::DataType(d[0]), colnum,
|
---|
74 | firstrow, firstelem, sz, ncd, &status);
|
---|
75 | if ( status ) {
|
---|
76 | fits_report_error(stderr, status);
|
---|
77 | char buff[32];
|
---|
78 | fits_get_errstatus(status, buff);
|
---|
79 | string msg = "FitsBlockRW<T>::WriteColumnData() Error: " ;
|
---|
80 | msg += buff;
|
---|
81 | throw FitsIOException(msg);
|
---|
82 | }
|
---|
83 | return;
|
---|
84 | }
|
---|
85 |
|
---|
86 | /*!
|
---|
87 | Read binary/ascii HDU data from a fits file.
|
---|
88 | See cfitsio function fits_read_col() for more detail.
|
---|
89 | \param colnum : table column number (starting from 1)
|
---|
90 | \param firstrow : the read operation starting point (row) (starting from 1)
|
---|
91 | \param firstelem : the firstelem (for vector type columns)
|
---|
92 | \param d : pointer to data to be written
|
---|
93 | \param sz : number of data elements to be read
|
---|
94 | */
|
---|
95 | static void ReadColumnData(FitsInOutFile& fios, int colnum, long firstrow,
|
---|
96 | long firstelem, T * d, size_t sz)
|
---|
97 | {
|
---|
98 | int status = 0;
|
---|
99 | int anynul = 0;
|
---|
100 | fits_read_col(fios.FitsPtr(), FitsTypes::DataType(d[0]), colnum,
|
---|
101 | firstrow, firstelem, sz, NULL, d, &anynul, &status);
|
---|
102 | if ( status ) {
|
---|
103 | fits_report_error(stderr, status);
|
---|
104 | char buff[32];
|
---|
105 | fits_get_errstatus(status, buff);
|
---|
106 | string msg = "FitsBlockRW<T>::ReadColumnData() Error: " ;
|
---|
107 | msg += buff;
|
---|
108 | throw FitsIOException(msg);
|
---|
109 | }
|
---|
110 | return;
|
---|
111 | }
|
---|
112 |
|
---|
113 |
|
---|
114 | };
|
---|
115 |
|
---|
116 |
|
---|
117 | /*!
|
---|
118 | Write character string data to binary/ascii HDU data in a fits file.
|
---|
119 | See cfitsio function fits_write_col() for more detail.
|
---|
120 | \param colnum : table column number (starting from 1)
|
---|
121 | \param firstrow : the write operation starting row (starting from 1)
|
---|
122 | \param firstelem : the firstelem (for vector type columns)
|
---|
123 | \param d : pointer to string type array to be written
|
---|
124 | \param sz : number of data elements to be written
|
---|
125 | \param width : column width
|
---|
126 | */
|
---|
127 | void WriteStringColumnData(FitsInOutFile& fios, int colnum, long firstrow,
|
---|
128 | long firstelem, const string * d, size_t sz,
|
---|
129 | long width=0)
|
---|
130 | {
|
---|
131 | int status = 0;
|
---|
132 | if (width < 1) width = 16;
|
---|
133 | for(size_t kk=0; kk<sz; kk++) {
|
---|
134 | char * cp = const_cast<char *>(d[kk].c_str());
|
---|
135 | status = 0;
|
---|
136 | fits_write_col(fios.FitsPtr(), FitsTypes::DataType(cp), colnum,
|
---|
137 | firstrow+kk, firstelem, 1, &cp, &status);
|
---|
138 | if ( status ) {
|
---|
139 | fits_report_error(stderr, status);
|
---|
140 | char buff[32];
|
---|
141 | fits_get_errstatus(status, buff);
|
---|
142 | string msg = "WriteStringColumnData Error: " ;
|
---|
143 | msg += buff;
|
---|
144 | sprintf(buff," kk=%ld",kk); msg += buff;
|
---|
145 | throw FitsIOException(msg);
|
---|
146 | }
|
---|
147 | }
|
---|
148 | return;
|
---|
149 | }
|
---|
150 |
|
---|
151 | /*!
|
---|
152 | Read character string data to binary/ascii HDU data in a fits file.
|
---|
153 | See cfitsio function fits_read_col() for more detail.
|
---|
154 | \param colnum : table column number (starting from 1)
|
---|
155 | \param firstrow : the read operation starting point (row) (starting from 1)
|
---|
156 | \param firstelem : the firstelem (for vector type columns)
|
---|
157 | \param d : pointer to string type array to be read
|
---|
158 | \param sz : number of data elements to be read
|
---|
159 | \param width : column width
|
---|
160 | */
|
---|
161 | void ReadStringColumnData(FitsInOutFile& fios, int colnum, long firstrow,
|
---|
162 | long firstelem, string * d, size_t sz, long width)
|
---|
163 | {
|
---|
164 | int status = 0;
|
---|
165 | int anynul = 0;
|
---|
166 | char buff[1024];
|
---|
167 | for(size_t kk=0; kk<sz; kk++) {
|
---|
168 | fits_read_col(fios.FitsPtr(), FitsTypes::DataType(buff), colnum,
|
---|
169 | firstrow, firstelem+kk, 1, NULL, &buff, &anynul, &status);
|
---|
170 | d[kk] = buff;
|
---|
171 | if ( status ) {
|
---|
172 | fits_report_error(stderr, status);
|
---|
173 | char buff[32];
|
---|
174 | fits_get_errstatus(status, buff);
|
---|
175 | string msg = "ReadStringColumnData Error: " ;
|
---|
176 | msg += buff;
|
---|
177 | sprintf(buff," kk=%ld",kk); msg += buff;
|
---|
178 | throw FitsIOException(msg);
|
---|
179 | }
|
---|
180 | }
|
---|
181 | return;
|
---|
182 | }
|
---|
183 |
|
---|
184 | } // Fin du namespace
|
---|
185 |
|
---|
186 | #endif
|
---|