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 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
|
// Astrophysics Science Division,
// NASA/ Goddard Space Flight Center
// HEASARC
// http://heasarc.gsfc.nasa.gov
// e-mail: ccfits@legacy.gsfc.nasa.gov
//
// Original author: Ben Dorman
#ifdef _MSC_VER
#include "MSconfig.h" // for truncation warning
#endif
// ColumnData
#include "ColumnData.h"
#include <algorithm>
namespace CCfits
{
#ifndef SPEC_TEMPLATE_DECL_DEFECT
template <>
void ColumnData<String>::writeData (const std::vector<String>& indata,
long firstRow, String* nullValue)
{
int status=0;
char** columnData=FITSUtil::CharArray(indata);
bool isError=false;
if (varLength())
{
const long nRows = static_cast<long>(indata.size());
for (long iRow=0; !isError && iRow<nRows; ++iRow)
{
if (fits_write_colnull(fitsPointer(), TSTRING, index(),
firstRow+iRow, 1, 1, &columnData[iRow], 0, &status))
isError=true;
}
}
else
{
if (fits_write_colnull(fitsPointer(), TSTRING, index(), firstRow,
1, indata.size(), columnData, 0, &status) != 0)
isError=true;
}
if (isError)
{
for (size_t i = 0; i < indata.size(); ++i)
delete [] columnData[i];
delete [] columnData;
throw FitsError(status);
}
unsigned long elementsToWrite (indata.size() + firstRow - 1);
std::vector<String> __tmp(m_data);
if (m_data.size() < elementsToWrite)
{
m_data.resize(elementsToWrite,"");
std::copy(__tmp.begin(),__tmp.end(),m_data.begin());
}
std::copy(indata.begin(),indata.end(),m_data.begin()+firstRow-1);
for (size_t i = 0; i < indata.size(); ++i)
{
delete [] columnData[i];
}
delete [] columnData;
}
#endif
#ifndef SPEC_TEMPLATE_IMP_DEFECT
#ifndef SPEC_TEMPLATE_DECL_DEFECT
template <>
void ColumnData<String>::readColumnData (long firstRow,
long nelements,
String* nullValue)
{
// nelements = nrows to read.
if (nelements < 1)
throw Column::InvalidNumberOfRows((int)nelements);
if (firstRow < 1 || (firstRow+nelements-1)>rows())
throw Column::InvalidRowNumber(name());
int status = 0;
int anynul = 0;
char** array = new char*[nelements];
// Initialize pointers to NULL so we can safely delete
// during error handling, even if they haven't been allocated.
for (long i=0; i<nelements; ++i)
array[i]=static_cast<char*>(0);
bool isError = false;
// Strings are unusual. The variable length case is still
// handled by a ColumnData class, not a ColumnVectorData.
char* nulval = 0;
if (nullValue)
{
nulval = const_cast<char*>(nullValue->c_str());
}
else
{
nulval = new char;
*nulval = '\0';
}
makeHDUCurrent();
if (varLength())
{
long* strLengths = new long[nelements];
long* offsets = new long[nelements];
if (fits_read_descripts(fitsPointer(), index(), firstRow,
nelements, strLengths, offsets, &status))
{
isError = true;
}
else
{
// For variable length cols, must read 1 and only 1 row
// at a time into array.
for (long j=0; j<nelements; ++j)
{
array[j] = new char[strLengths[j] + 1];
}
const long lastRow = firstRow+nelements-1;
for (long iRow=firstRow; !isError && iRow<=lastRow; ++iRow)
{
if (fits_read_col_str(fitsPointer(),index(), iRow, 1, 1,
nulval, &array[iRow-firstRow], &anynul,&status) )
isError=true;
}
}
delete [] strLengths;
delete [] offsets;
}
else
{
// Fixed length strings, length is stored in Column's m_width.
for (long j=0; j<nelements; ++j)
{
array[j] = new char[width() + 1];
}
if (fits_read_col_str(fitsPointer(),index(), firstRow,1,nelements,
nulval,array, &anynul,&status))
isError=true;
}
if (isError)
{
// It's OK to do this even if error occurred before
// array rows were allocated. In that case their pointers
// were set to NULL.
for (long j = 0; j < nelements; ++j)
{
delete [] array[j];
}
delete [] array;
delete nulval;
throw FitsError(status);
}
if (m_data.size() != static_cast<size_t>(rows()))
setData(std::vector<String>(rows(),String(nulval)));
for (long j=0; j<nelements; ++j)
{
m_data[j - 1 + firstRow] = String(array[j]);
}
for (long j=0; j<nelements; j++)
{
delete [] array[j];
}
delete [] array;
delete nulval;
if (nelements == rows()) isRead(true);
}
#endif
#endif
#ifndef SPEC_TEMPLATE_IMP_DEFECT
#ifndef SPEC_TEMPLATE_DECL_DEFECT
template <>
void ColumnData<complex<float> >::setDataLimits (complex<float>* limits)
{
m_minLegalValue = limits[0];
m_maxLegalValue = limits[1];
m_minDataValue = limits[2];
m_maxDataValue = limits[3];
}
template <>
void ColumnData<complex<double> >::setDataLimits (complex<double>* limits)
{
m_minLegalValue = limits[0];
m_maxLegalValue = limits[1];
m_minDataValue = limits[2];
m_maxDataValue = limits[3];
}
template <>
void ColumnData<complex<float> >::readColumnData (long firstRow,
long nelements,
complex<float>* nullValue)
{
// specialization for ColumnData<String>
int status(0);
int anynul(0);
FITSUtil::auto_array_ptr<float> pArray(new float[nelements*2]);
float* array = pArray.get();
float nulval(0);
makeHDUCurrent();
if (fits_read_col_cmp(fitsPointer(),index(), firstRow,1,nelements,
nulval,array, &anynul,&status) ) throw FitsError(status);
if (m_data.size() != static_cast<size_t>(rows())) m_data.resize(rows());
// the 'j -1 ' converts to zero based indexing.
for (int j = 0; j < nelements; ++j)
{
m_data[j - 1 +firstRow] = std::complex<float>(array[2*j],array[2*j+1]);
}
if (nelements == rows()) isRead(true);
}
template <>
void ColumnData<complex<double> >::readColumnData (long firstRow,
long nelements,
complex<double>* nullValue)
{
// specialization for ColumnData<complex<double> >
int status(0);
int anynul(0);
FITSUtil::auto_array_ptr<double> pArray(new double[nelements*2]);
double* array = pArray.get();
double nulval(0);
makeHDUCurrent();
if (fits_read_col_dblcmp(fitsPointer(), index(), firstRow,1,nelements,
nulval,array, &anynul,&status) ) throw FitsError(status);
if (m_data.size() != static_cast<size_t>(rows())) setData(std::vector<complex<double> >(rows(),nulval));
// the 'j -1 ' converts to zero based indexing.
for (int j = 0; j < nelements; j++)
{
m_data[j - 1 + firstRow] = std::complex<double>(array[2*j],array[2*j+1]);
}
if (nelements == rows()) isRead(true);
}
#endif
#endif
#ifndef SPEC_TEMPLATE_DECL_DEFECT
template <>
void ColumnData<complex<float> >::writeData (const std::vector<complex<float> >& inData,
long firstRow,
complex<float>* nullValue)
{
int status(0);
int nRows (inData.size());
FITSUtil::auto_array_ptr<float> pData(new float[nRows*2]);
float* Data = pData.get();
std::vector<complex<float> > __tmp(m_data);
for (int j = 0; j < nRows; ++j)
{
Data[ 2*j] = inData[j].real();
Data[ 2*j + 1] = inData[j].imag();
}
try
{
if (fits_write_col_cmp(fitsPointer(), index(), firstRow, 1,
nRows,Data, &status) != 0) throw FitsError(status);
long elementsToWrite(nRows + firstRow -1);
if (elementsToWrite > static_cast<long>(m_data.size()))
{
m_data.resize(elementsToWrite);
}
std::copy(inData.begin(),inData.end(),m_data.begin()+firstRow-1);
// tell the Table that the number of rows has changed
parent()->updateRows();
}
catch (FitsError) // the only thing that can throw here.
{
// reset to original content and rethrow the exception.
m_data.resize(__tmp.size());
m_data = __tmp;
}
}
#endif
#ifndef SPEC_TEMPLATE_DECL_DEFECT
template <>
void ColumnData<complex<double> >::writeData (const std::vector<complex<double> >& inData,
long firstRow,
complex<double>* nullValue)
{
int status(0);
int nRows (inData.size());
FITSUtil::auto_array_ptr<double> pData(new double[nRows*2]);
double* Data = pData.get();
std::vector<complex<double> > __tmp(m_data);
for (int j = 0; j < nRows; ++j)
{
pData[ 2*j] = inData[j].real();
pData[ 2*j + 1] = inData[j].imag();
}
try
{
if (fits_write_col_dblcmp(fitsPointer(), index(), firstRow, 1,
nRows,Data, &status) != 0) throw FitsError(status);
long elementsToWrite(nRows + firstRow -1);
if (elementsToWrite > static_cast<long>(m_data.size()))
{
m_data.resize(elementsToWrite);
}
std::copy(inData.begin(),inData.end(),m_data.begin()+firstRow-1);
// tell the Table that the number of rows has changed
parent()->updateRows();
}
catch (FitsError) // the only thing that can throw here.
{
// reset to original content and rethrow the exception.
m_data.resize(__tmp.size());
m_data = __tmp;
}
}
#endif
} // namespace CCfits
|