-
Notifications
You must be signed in to change notification settings - Fork 1
/
ColumnVectorData.cxx
329 lines (282 loc) · 12.5 KB
/
ColumnVectorData.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
// Astrophysics Science Division,
// NASA/ Goddard Space Flight Center
// HEASARC
// http://heasarc.gsfc.nasa.gov
// e-mail: [email protected]
//
// Original author: Ben Dorman
// ColumnVectorData
#include "ColumnVectorData.h"
namespace CCfits
{
#ifndef SPEC_TEMPLATE_IMP_DEFECT
#ifndef SPEC_TEMPLATE_DECL_DEFECT
// duplicated for each complex type to work around imagined or
// actual compiler deficiencies.
template <>
void ColumnVectorData<std::complex<float> >::readColumnData(long firstRow,
long nelements, long firstElem, std::complex<float>* null )
{
int status=0;
float nulval (0);
FITSUtil::auto_array_ptr<float> pArray(new float[2*nelements]);
float* array = pArray.get();
int anynul(0);
if (fits_read_col_cmp(fitsPointer(),index(),firstRow, firstElem,
nelements,nulval,array,&anynul,&status) ) throw FitsError(status);
if (m_data.size() != static_cast<size_t>(rows())) m_data.resize(rows());
std::valarray<std::complex<float> > readData(nelements);
for (long j = 0; j < nelements; ++j)
{
readData[j] = std::complex<float>(array[2*j],array[2*j+1]);
}
size_t countRead = 0;
const size_t ONE = 1;
if (m_data.size() != static_cast<size_t>(rows())) m_data.resize(rows());
size_t vectorSize(0);
if (!varLength())
{
vectorSize = std::max(repeat(),ONE); // safety check.
}
else
{
// assume that the user specified the correct length for
// variable columns. This should be ok since readVariableColumns
// uses fits_read_descripts to return this information from the
// fits pointer, and this is passed as nelements here.
vectorSize = nelements;
}
size_t n = nelements;
int i = firstRow;
int ii = i - 1;
while ( countRead < n)
{
std::valarray<complex<float> >& current = m_data[ii];
if (current.size() != vectorSize) current.resize(vectorSize,0.);
int elementsInFirstRow = vectorSize-firstElem + 1;
bool lastRow = ( (nelements - countRead) < vectorSize);
if (lastRow)
{
int elementsInLastRow = nelements - countRead;
std::copy(&readData[countRead],&readData[0]+nelements,¤t[0]);
countRead += elementsInLastRow;
}
// what to do with complete rows. if firstElem == 1 the
else
{
if (firstElem == 1 || (firstElem > 1 && i > firstRow) )
{
current = readData[std::slice(vectorSize*(ii-firstRow)+
elementsInFirstRow,vectorSize,1)];
++ii;
++i;
countRead += vectorSize;
}
else
{
if (i == firstRow)
{
std::copy(&readData[0],&readData[0]+elementsInFirstRow,
¤t[firstElem]);
countRead += elementsInFirstRow;
++i;
++ii;
}
}
}
}
}
#ifndef SPEC_TEMPLATE_DECL_DEFECT
template <>
void ColumnVectorData<complex<float> >::setDataLimits (complex<float>* limits)
{
m_minLegalValue = limits[0];
m_maxLegalValue = limits[1];
m_minDataValue = limits[2];
m_maxDataValue = limits[3];
}
template <>
void ColumnVectorData<complex<double> >::setDataLimits (complex<double>* limits)
{
m_minLegalValue = limits[0];
m_maxLegalValue = limits[1];
m_minDataValue = limits[2];
m_maxDataValue = limits[3];
}
#endif
template <>
void ColumnVectorData<complex<double> >::readColumnData (long firstRow,
long nelements,long firstElem,
complex<double>* nullValue)
{
// duplicated for each complex type to work around imagined or
// actual compiler deficiencies.
int status=0;
double nulval (0);
FITSUtil::auto_array_ptr<double> pArray(new double[2*nelements]);
double* array = pArray.get();
int anynul(0);
if (fits_read_col_dblcmp(fitsPointer(),index(),firstRow, firstElem,
nelements,nulval,array,&anynul,&status) ) throw FitsError(status);
if (m_data.size() != static_cast<size_t>(rows())) m_data.resize(rows());
std::valarray<std::complex<double> > readData(nelements);
for (long j = 0; j < nelements; ++j)
{
readData[j] = std::complex<double>(array[2*j],array[2*j+1]);
}
size_t countRead = 0;
const size_t ONE = 1;
if (m_data.size() != static_cast<size_t>(rows())) m_data.resize(rows());
size_t vectorSize(0);
if (!varLength())
{
vectorSize = std::max(repeat(),ONE); // safety check.
}
else
{
// assume that the user specified the correct length for
// variable columns. This should be ok since readVariableColumns
// uses fits_read_descripts to return this information from the
// fits pointer, and this is passed as nelements here.
vectorSize = nelements;
}
size_t n = nelements;
int i = firstRow;
int ii = i - 1;
while ( countRead < n)
{
std::valarray<std::complex<double> >& current = m_data[ii];
if (current.size() != vectorSize) current.resize(vectorSize,0.);
int elementsInFirstRow = vectorSize-firstElem + 1;
bool lastRow = ( (nelements - countRead) < vectorSize);
if (lastRow)
{
int elementsInLastRow = nelements - countRead;
std::copy(&readData[countRead],&readData[0]+nelements,¤t[0]);
countRead += elementsInLastRow;
}
// what to do with complete rows. if firstElem == 1 the
else
{
if (firstElem == 1 || (firstElem > 1 && i > firstRow) )
{
current = readData[std::slice(vectorSize*(ii-firstRow)+
elementsInFirstRow,vectorSize,1)];
++ii;
++i;
countRead += vectorSize;
}
else
{
if (i == firstRow)
{
std::copy(&readData[0],&readData[0]+elementsInFirstRow,
¤t[firstElem]);
countRead += elementsInFirstRow;
++i;
++ii;
}
}
}
}
}
template <>
void ColumnVectorData<complex<float> >::writeFixedArray
(complex<float>* data, long nElements, long nRows, long firstRow,
complex<float>* nullValue)
{
int status(0);
// check for sanity of inputs, then write to file.
// this function writes only complete rows to a table with
// fixed width rows.
if ( nElements < nRows*static_cast<long>(repeat()) )
{
#ifdef SSTREAM_DEFECT
std::ostrstream msgStr;
#else
std::ostringstream msgStr;
#endif
msgStr << " input array size: " << nElements
<< " required " << nRows*repeat();
#ifdef SSTREAM_DEFECT
msgStr << std::ends;
#endif
String msg(msgStr.str());
throw Column::InsufficientElements(msg);
}
FITSUtil::auto_array_ptr<float> realData(new float[2*nElements]);
for (int j = 0; j < nElements; ++j)
{
realData[2*j] = data[j].real();
realData[2*j+1] = data[j].imag();
}
if (fits_write_col_cmp(fitsPointer(),index(),firstRow,
1,nElements,realData.get(),&status)) throw FitsError(status);
parent()->updateRows();
}
template <>
void ColumnVectorData<complex<double> >::writeFixedArray
(complex<double>* data, long nElements, long nRows, long firstRow,
complex<double>* nullValue)
{
int status(0);
// check for sanity of inputs, then write to file.
// this function writes only complete rows to a table with
// fixed width rows.
if ( nElements < nRows*static_cast<long>(repeat()) )
{
#ifdef SSTREAM_DEFECT
std::ostrstream msgStr;
#else
std::ostringstream msgStr;
#endif
msgStr << " input array size: " << nElements
<< " required " << nRows*repeat();
String msg(msgStr.str());
throw Column::InsufficientElements(msg);
}
FITSUtil::auto_array_ptr<double> realData(new double[2*nElements]);
for (int j = 0; j < nElements; ++j)
{
realData[2*j] = data[j].real();
realData[2*j+1] = data[j].imag();
}
if (fits_write_col_dblcmp(fitsPointer(),index(),firstRow,
1,nElements,realData.get(),&status)) throw FitsError(status);
parent()->updateRows();
}
#endif
#endif
#ifndef SPEC_TEMPLATE_DECL_DEFECT
template <>
void
ColumnVectorData<std::complex<float> >::doWrite
(std::complex<float>* data, long row, long rowSize, long firstElem, std::complex<float>* nullValue )
{
int status(0);
FITSUtil::auto_array_ptr<float> carray( new float[2*rowSize]);
for ( long j = 0 ; j < rowSize; ++ j)
{
carray[2*j] = data[j].real();
carray[2*j + 1] = data[j].imag();
}
if (fits_write_col_cmp(fitsPointer(),index(),row,firstElem,rowSize,
carray.get(),&status)) throw FitsError(status);
}
template <>
void
ColumnVectorData<std::complex<double> >::doWrite
(std::complex<double>* data, long row, long rowSize, long firstElem, std::complex<double>* nullValue )
{
int status(0);
FITSUtil::auto_array_ptr<double> carray( new double[2*rowSize]);
for ( long j = 0 ; j < rowSize; ++ j)
{
carray[2*j] = data[j].real();
carray[2*j + 1] = data[j].imag();
}
if (fits_write_col_dblcmp(fitsPointer(),index(),row,firstElem,rowSize,
carray.get(),&status)) throw FitsError(status);
}
#endif
}