[949] | 1 | #include "machdefs.h"
|
---|
| 2 | #include <stdlib.h>
|
---|
[839] | 3 | #include "fitsfile.h"
|
---|
| 4 | #include "pexceptions.h"
|
---|
| 5 | #include "strutil.h"
|
---|
[903] | 6 | #include "anydataobj.h"
|
---|
| 7 | #include "fitsspherehealpix.h"
|
---|
[1136] | 8 |
|
---|
| 9 |
|
---|
| 10 | void FitsIOHandler::Read(char flnm[],int hdunum)
|
---|
[839] | 11 | {
|
---|
[1136] | 12 | FitsInFile ifts(flnm);
|
---|
| 13 | Read(ifts, hdunum);
|
---|
[839] | 14 | }
|
---|
[1136] | 15 | void FitsIOHandler::Read(FitsInFile& is, int hdunum)
|
---|
| 16 | {
|
---|
[1175] | 17 | if (hdunum == 0) is.moveToFollowingHeader();
|
---|
| 18 | else is.ReadFInit(hdunum);
|
---|
[1136] | 19 | ReadFromFits(is);
|
---|
| 20 | }
|
---|
| 21 |
|
---|
| 22 |
|
---|
| 23 | void FitsIOHandler::Write(char flnm[], bool OldFile)
|
---|
| 24 |
|
---|
| 25 | {
|
---|
| 26 |
|
---|
| 27 | FitsOutFile of(flnm, OldFile);
|
---|
| 28 | Write(of);
|
---|
| 29 | }
|
---|
| 30 |
|
---|
| 31 | void FitsIOHandler::Write(FitsOutFile& os)
|
---|
| 32 | {
|
---|
| 33 | WriteToFits(os);
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 |
|
---|
| 37 |
|
---|
[839] | 38 |
|
---|
| 39 | FitsFile::~FitsFile()
|
---|
| 40 | {
|
---|
| 41 | int status = 0;
|
---|
[1175] | 42 | if( fptr_ != NULL)
|
---|
[903] | 43 | {
|
---|
| 44 | fits_close_file(fptr_,&status);
|
---|
[1175] | 45 | // je ne fais pas delete fptr_, c'est la lib. fitsio qui a fait
|
---|
| 46 | // new...
|
---|
[903] | 47 | }
|
---|
[1175] | 48 | if( status ) printerror( status );
|
---|
[839] | 49 | }
|
---|
[903] | 50 |
|
---|
[1136] | 51 |
|
---|
| 52 | void FitsFile::printerror(int &status)
|
---|
| 53 | //*****************************************************/
|
---|
| 54 | //* Print out cfitsio error messages and exit program */
|
---|
| 55 | //*****************************************************/
|
---|
[1045] | 56 | {
|
---|
[1136] | 57 | if( status )
|
---|
| 58 | {
|
---|
| 59 | fits_report_error(stderr,status);
|
---|
| 60 | throw IOExc("FitsFile:: error FITSIO status");
|
---|
| 61 | }
|
---|
| 62 | return;
|
---|
| 63 | }
|
---|
[903] | 64 |
|
---|
[1136] | 65 | void FitsFile::printerror(int& status, char* texte)
|
---|
| 66 | //*****************************************************/
|
---|
| 67 | //* Print out cfitsio error messages and exit program */
|
---|
| 68 | //*****************************************************/
|
---|
| 69 | {
|
---|
| 70 | // print out cfitsio error messages and exit program
|
---|
| 71 | // print error report
|
---|
| 72 | fits_report_error(stderr, status);
|
---|
| 73 | cout << " erreur:: " << texte << endl;
|
---|
| 74 | throw IOExc("FitsFile:: error FITSIO status");
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | void FitsFile::ResetStatus(int& status)
|
---|
| 78 | {
|
---|
| 79 | fits_status_ = status;
|
---|
| 80 | status = 0;
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | string FitsFile::getErrStatus(int status)
|
---|
| 84 | {
|
---|
| 85 | char text[31];
|
---|
| 86 | fits_get_errstatus(status, text);
|
---|
| 87 | return string(text);
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | FitsInFile::FitsInFile()
|
---|
| 91 | {
|
---|
| 92 | InitNull();
|
---|
| 93 | }
|
---|
| 94 | //FitsInFile::FitsInFile(char flnm[], int hdunum)
|
---|
| 95 | FitsInFile::FitsInFile(char flnm[])
|
---|
| 96 | {
|
---|
[1175] | 97 | InitNull();
|
---|
| 98 | int status = 0;
|
---|
| 99 | fits_open_file(&fptr_,flnm,READONLY,&status);
|
---|
| 100 | if( status ) printerror( status );
|
---|
[1136] | 101 | }
|
---|
| 102 |
|
---|
| 103 |
|
---|
| 104 | void FitsInFile::InitNull()
|
---|
| 105 | {
|
---|
[1175] | 106 |
|
---|
[1045] | 107 | bitpix_ = 0;
|
---|
| 108 | naxis_ = 0;
|
---|
| 109 | nbData_ = 0;
|
---|
| 110 | nrows_ = 0;
|
---|
| 111 | nbcols_ = 0;
|
---|
| 112 | naxisn_.clear();
|
---|
| 113 | repeat_.clear();
|
---|
| 114 | noms_.clear();
|
---|
| 115 | taille_des_chaines_.clear();
|
---|
| 116 | dvl_.Clear();
|
---|
[1175] | 117 |
|
---|
| 118 |
|
---|
[1045] | 119 | }
|
---|
| 120 |
|
---|
| 121 |
|
---|
[1136] | 122 | int FitsInFile::NbBlocks(char flnm[])
|
---|
[903] | 123 | {
|
---|
| 124 | int status = 0;
|
---|
| 125 | int nbhdu = 0;
|
---|
| 126 | fitsfile* fileptr;
|
---|
| 127 | fits_open_file(&fileptr,flnm,READONLY,&status);
|
---|
| 128 | if( status ) printerror( status, "NbBlocks: erreur ouverture fichier" );
|
---|
| 129 | fits_get_num_hdus(fileptr, &nbhdu, &status);
|
---|
| 130 | fits_close_file(fileptr,&status);
|
---|
| 131 | return nbhdu;
|
---|
| 132 | }
|
---|
| 133 |
|
---|
[1136] | 134 | void FitsInFile::getBlockType(char flnm[], int hdunum, string& typeOfExtension, int& naxis, vector<int>& naxisn, string& dataType, DVList& dvl )
|
---|
[903] | 135 | {
|
---|
| 136 | int status = 0;
|
---|
| 137 | fitsfile* fileptr;
|
---|
| 138 | fits_open_file(&fileptr,flnm,READONLY,&status);
|
---|
| 139 | if( status ) printerror( status, "getBlockType: erreur ouverture fichier" );
|
---|
| 140 | // move to the specified HDU number
|
---|
| 141 | int hdutype = 0;
|
---|
| 142 | fits_movabs_hdu(fileptr,hdunum,&hdutype,&status);
|
---|
| 143 | if( status ) printerror( status,"getBlockType: erreur movabs");
|
---|
| 144 | if(hdutype == IMAGE_HDU)
|
---|
| 145 | {
|
---|
| 146 | typeOfExtension = "IMAGE";
|
---|
| 147 | int bitpix;
|
---|
| 148 | GetImageParameters (fileptr, bitpix, naxis, naxisn);
|
---|
| 149 | if(bitpix == DOUBLE_IMG) dataType = "double";
|
---|
| 150 | else
|
---|
| 151 | if(bitpix == FLOAT_IMG) dataType = "float";
|
---|
| 152 | else
|
---|
| 153 | if(bitpix == LONG_IMG || bitpix == SHORT_IMG ) dataType = "int";
|
---|
| 154 | else
|
---|
| 155 | {
|
---|
| 156 | cout << " bitpix= " << bitpix << endl;
|
---|
| 157 | throw PException(" FitsFile::getBlockType : unsupprted FITS data type");
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | }
|
---|
| 161 | else
|
---|
| 162 | if(hdutype == ASCII_TBL || hdutype == BINARY_TBL)
|
---|
| 163 | {
|
---|
| 164 | int nrows = 0;
|
---|
| 165 | vector<string> noms;
|
---|
| 166 | vector<char> types;
|
---|
| 167 | vector<int> taille_des_chaines;
|
---|
[971] | 168 | GetBinTabParameters(fileptr, naxis, nrows, naxisn, noms, types, taille_des_chaines);
|
---|
| 169 | int k;
|
---|
| 170 | for (k=0; k< naxisn.size(); k++) naxisn[k] *= nrows;
|
---|
[903] | 171 | if(hdutype == ASCII_TBL)
|
---|
| 172 | {
|
---|
| 173 | typeOfExtension = "ASCII_TBL";
|
---|
| 174 | dataType = "ASCII";
|
---|
| 175 | }
|
---|
| 176 | else
|
---|
| 177 | {
|
---|
| 178 | typeOfExtension = "BINARY_TBL";
|
---|
| 179 | if(types[0] == 'D') dataType = "double";
|
---|
| 180 | else
|
---|
| 181 | if(types[0] == 'E') dataType = "float";
|
---|
| 182 | else
|
---|
| 183 | if(types[0] == 'I' ) dataType = "int";
|
---|
| 184 | else
|
---|
| 185 | if(types[0] == 'S' ) dataType = "char*";
|
---|
| 186 | else
|
---|
| 187 | {
|
---|
| 188 | cout << " types[0]= " << types[0] << endl;
|
---|
| 189 | throw PException(" FitsFile::getBlockType : unsupprted FITS data type");
|
---|
| 190 | }
|
---|
| 191 | }
|
---|
| 192 | }
|
---|
| 193 | else
|
---|
| 194 | {
|
---|
| 195 | cout << " hdutype= " << hdutype << endl;
|
---|
| 196 | throw IOExc("FitsFile::getBlockType: this HDU type is unknown");
|
---|
| 197 | }
|
---|
| 198 |
|
---|
[971] | 199 | KeywordsIntoDVList(fileptr, dvl, hdunum);
|
---|
[903] | 200 | fits_close_file(fileptr,&status);
|
---|
| 201 | }
|
---|
| 202 |
|
---|
[1136] | 203 | void FitsInFile::GetImageParameters (fitsfile* fileptr,int& bitpix,int& naxis,vector<int>& naxisn)
|
---|
[839] | 204 | {
|
---|
[1136] | 205 | int hdunum=0;
|
---|
| 206 | cout << " Reading a FITS image in HDU : " << fits_get_hdu_num(fileptr,&hdunum) << endl;
|
---|
| 207 | int status= 0;
|
---|
| 208 |
|
---|
| 209 | // bits per pixels
|
---|
| 210 | fits_read_key(fileptr,TINT,"BITPIX",&bitpix,NULL,&status);
|
---|
| 211 | if( status ) printerror( status );
|
---|
| 212 |
|
---|
| 213 | // number of dimensions in the FITS array
|
---|
| 214 | naxis= 0;
|
---|
| 215 | fits_read_key(fileptr,TINT,"NAXIS",&naxis,NULL,&status);
|
---|
| 216 | if( status ) printerror( status );
|
---|
| 217 | // read the NAXISn keywords to get image size
|
---|
| 218 | long* naxes = new long[naxis] ;
|
---|
| 219 | int nfound;
|
---|
| 220 | fits_read_keys_lng(fileptr,"NAXIS",1,naxis,naxes,&nfound,&status);
|
---|
| 221 | if( status ) printerror( status );
|
---|
| 222 | if (nfound != naxis )
|
---|
| 223 | cout << " WARNING : " << nfound << " axes found, expected naxis= " << naxis << endl;
|
---|
| 224 | int k;
|
---|
| 225 | for (k=0; k<naxis; k++)
|
---|
| 226 | {
|
---|
| 227 | naxisn.push_back( (int)naxes[k] );
|
---|
| 228 | }
|
---|
| 229 | delete [] naxes;
|
---|
[971] | 230 | }
|
---|
[839] | 231 |
|
---|
[971] | 232 |
|
---|
[1136] | 233 | void FitsInFile::ReadFInit(int hdunum)
|
---|
[1045] | 234 | {
|
---|
[1143] | 235 | InitNull();
|
---|
[1136] | 236 | // int status = 0;
|
---|
| 237 |
|
---|
| 238 | // fits_open_file(&fptr_,flnm,READONLY,&status);
|
---|
| 239 | // if( status ) printerror( status );
|
---|
| 240 |
|
---|
[1045] | 241 | int status = 0;
|
---|
[1136] | 242 |
|
---|
[1045] | 243 | if (hdunum <= 1)
|
---|
| 244 | {
|
---|
| 245 | hdunum_ = 1;
|
---|
| 246 | // presence of image ?
|
---|
| 247 | int naxis= 0;
|
---|
| 248 | fits_read_key(fptr_,TINT,"NAXIS",&naxis,NULL,&status);
|
---|
| 249 | if( status ) printerror( status );
|
---|
| 250 | if (naxis > 0 ) // there is an image
|
---|
| 251 | {
|
---|
| 252 | hdutype_ = IMAGE_HDU;
|
---|
| 253 | GetImageParameters (fptr_, bitpix_, naxis_, naxisn_);
|
---|
| 254 | nbData_ = 1;
|
---|
| 255 | int k;
|
---|
| 256 | for (k=0; k<naxis_; k++) if (naxisn_[k] > 0) nbData_ *= naxisn_[k];
|
---|
| 257 | KeywordsIntoDVList(fptr_, dvl_,hdunum_);
|
---|
| 258 | }
|
---|
| 259 | else
|
---|
| 260 | {
|
---|
| 261 | throw PException(" first header : no image, probably error in hdunum");
|
---|
| 262 | }
|
---|
| 263 | //
|
---|
| 264 | }
|
---|
| 265 | else
|
---|
| 266 | {
|
---|
| 267 | hdunum_ = hdunum-1;
|
---|
| 268 | int hdutype;
|
---|
| 269 | fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
|
---|
| 270 | if( status ) printerror( status,":FitsFile::ReadF : erreur movabs");
|
---|
| 271 | moveToFollowingHeader();
|
---|
| 272 | }
|
---|
| 273 | }
|
---|
| 274 |
|
---|
[1143] | 275 | DVList FitsInFile::DVListFromPrimaryHeader() const
|
---|
| 276 | {
|
---|
| 277 | int status;
|
---|
| 278 | DVList dvl;
|
---|
| 279 | KeywordsIntoDVList(fptr_, dvl, 1);
|
---|
| 280 | int hdutype = 0;
|
---|
| 281 | if (hdunum_ > 0) fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
|
---|
| 282 | return dvl;
|
---|
| 283 | }
|
---|
[1136] | 284 |
|
---|
| 285 |
|
---|
| 286 | void FitsInFile::moveToFollowingHeader()
|
---|
[971] | 287 | {
|
---|
| 288 | int status = 0;
|
---|
[839] | 289 | int hdutype;
|
---|
[1143] | 290 | InitNull();
|
---|
[971] | 291 | fits_movrel_hdu(fptr_, 1,&hdutype,&status);
|
---|
| 292 | if( status ) printerror( status," lecture du header suivant" );
|
---|
| 293 | hdunum_++;
|
---|
[839] | 294 | hdutype_= hdutype;
|
---|
| 295 | if(hdutype_ == IMAGE_HDU)
|
---|
| 296 | {
|
---|
[903] | 297 | GetImageParameters (fptr_, bitpix_, naxis_, naxisn_);
|
---|
| 298 | nbData_ = 1;
|
---|
[971] | 299 | int k;
|
---|
| 300 | for (k=0; k<naxis_; k++) if (naxisn_[k] > 0) nbData_ *= naxisn_[k];
|
---|
| 301 | KeywordsIntoDVList(fptr_, dvl_,hdunum_);
|
---|
[839] | 302 | }
|
---|
| 303 | if(hdutype_ == ASCII_TBL || hdutype_ == BINARY_TBL)
|
---|
| 304 | {
|
---|
[903] | 305 | GetBinTabParameters(fptr_,nbcols_, nrows_,repeat_, noms_, types_, taille_des_chaines_);
|
---|
[971] | 306 | KeywordsIntoDVList(fptr_, dvl_, hdunum_);
|
---|
[839] | 307 | }
|
---|
[971] | 308 | }
|
---|
[839] | 309 |
|
---|
[1136] | 310 |
|
---|
| 311 | int FitsInFile::NbColsFromFits() const
|
---|
[839] | 312 | {
|
---|
[1136] | 313 | if(hdutype_ == BINARY_TBL) return nbcols_;
|
---|
[971] | 314 | else
|
---|
[1136] | 315 | if(hdutype_ == ASCII_TBL || hdutype_ == IMAGE_HDU) return 1;
|
---|
[839] | 316 | else
|
---|
| 317 | {
|
---|
[1136] | 318 | cout << " hdutype= " << hdutype_ << endl;
|
---|
| 319 | throw PException("FitsFile::NbColsFromFits, this HDU is unknown");
|
---|
[839] | 320 | }
|
---|
| 321 | }
|
---|
| 322 |
|
---|
[1136] | 323 | int FitsInFile::NentriesFromFits(int nocol) const
|
---|
[839] | 324 | {
|
---|
[1136] | 325 | if(hdutype_ == BINARY_TBL ) return nrows_*repeat_[nocol];
|
---|
| 326 | else
|
---|
| 327 | if(hdutype_ == ASCII_TBL) return nrows_;
|
---|
| 328 | else
|
---|
| 329 | if(hdutype_ == IMAGE_HDU) return nbData_;
|
---|
| 330 | else
|
---|
[839] | 331 | {
|
---|
[1136] | 332 | cout << "hdutype= " << hdutype_ << endl;
|
---|
| 333 | throw PException("FitsFile::NentriesFromFits, this HDU is unknown");
|
---|
[839] | 334 | }
|
---|
| 335 | }
|
---|
| 336 |
|
---|
[1136] | 337 | char FitsInFile::ColTypeFromFits(int nocol) const
|
---|
[839] | 338 | {
|
---|
[1136] | 339 | if(hdutype_ != ASCII_TBL && hdutype_ != BINARY_TBL)
|
---|
[839] | 340 | {
|
---|
[1136] | 341 | throw IOExc("FitsFile::TypeFromFits, this HDU is not an ASCII table nor a binary table");
|
---|
[839] | 342 | }
|
---|
[1136] | 343 | return types_[nocol];
|
---|
[839] | 344 | }
|
---|
[1136] | 345 | string FitsInFile::ColNameFromFits(int nocol) const
|
---|
[839] | 346 | {
|
---|
[1136] | 347 | if(hdutype_ != ASCII_TBL && hdutype_ != BINARY_TBL)
|
---|
[1045] | 348 | {
|
---|
[1136] | 349 | throw IOExc("FitsFile::TypeFromFits, this HDU is not an ASCII table nor a binary table");
|
---|
[1045] | 350 | }
|
---|
[1136] | 351 | return noms_[nocol];
|
---|
[839] | 352 | }
|
---|
| 353 |
|
---|
[1136] | 354 | int FitsInFile::ColStringLengthFromFits(int nocol) const
|
---|
[839] | 355 | {
|
---|
[1136] | 356 | if(hdutype_ != ASCII_TBL && hdutype_ != BINARY_TBL)
|
---|
| 357 | {
|
---|
| 358 | throw IOExc("FitsFile::TypeFromFits, this HDU is not an ASCII table nor a binary table");
|
---|
| 359 | }
|
---|
| 360 | int index=-1;
|
---|
| 361 | int k;
|
---|
| 362 | for (k=0; k<=nocol; k++)
|
---|
| 363 | {
|
---|
| 364 | if (types_[k] == 'S') index++;
|
---|
| 365 | }
|
---|
| 366 | return taille_des_chaines_[index];
|
---|
[839] | 367 | }
|
---|
[1136] | 368 | void FitsInFile::GetBinTabLine(int NoLine, double* ddata, float* fdata, int* idata, char ** cdata)
|
---|
[839] | 369 | {
|
---|
| 370 | int status= 0;
|
---|
[1136] | 371 | int anull;
|
---|
| 372 | double dnull= 0.;
|
---|
| 373 | float fnull= 0.;
|
---|
| 374 | int inull= 0;
|
---|
| 375 | char* cnull= "";
|
---|
| 376 | int dcount = 0.;
|
---|
| 377 | int fcount = 0.;
|
---|
| 378 | int icount = 0;
|
---|
| 379 | int ccount =0;
|
---|
| 380 | int ncol;
|
---|
| 381 | long nels=1;
|
---|
| 382 | for (ncol=0; ncol<nbcols_; ncol++)
|
---|
[861] | 383 | {
|
---|
[1136] | 384 | switch (types_[ncol])
|
---|
| 385 | {
|
---|
| 386 | case 'D' :
|
---|
| 387 | fits_read_col(fptr_,TDOUBLE,ncol+1,NoLine+1,1,1,&dnull,&ddata[dcount++],&anull,&status);
|
---|
| 388 | break;
|
---|
| 389 | case 'E' :
|
---|
| 390 | fits_read_col(fptr_,TFLOAT,ncol+1,NoLine+1,1,1,&fnull,&fdata[fcount++],&anull,&status);
|
---|
| 391 | break;
|
---|
| 392 | case 'I' :
|
---|
| 393 | fits_read_col(fptr_,TINT,ncol+1,NoLine+1,1,1,&inull,&idata[icount++],
|
---|
| 394 | &anull,&status);
|
---|
| 395 | break;
|
---|
| 396 | case 'S' :
|
---|
| 397 | fits_read_col(fptr_,TSTRING,ncol+1,NoLine+1,1,1,cnull,&cdata[ccount++],&anull,&status);
|
---|
| 398 | break;
|
---|
| 399 | }
|
---|
| 400 | if (status)
|
---|
| 401 | {
|
---|
| 402 | ResetStatus(status);
|
---|
| 403 | break;
|
---|
| 404 | }
|
---|
[861] | 405 | }
|
---|
[903] | 406 | }
|
---|
[839] | 407 |
|
---|
[1136] | 408 | void FitsInFile::GetBinTabLine(int NoLine, float* fdata)
|
---|
[903] | 409 | {
|
---|
[1136] | 410 | int status= 0;
|
---|
| 411 | int anull;
|
---|
| 412 | float fnull= 0.;
|
---|
| 413 | long nels=1;
|
---|
| 414 | int ncol;
|
---|
| 415 | for (ncol=0; ncol<nbcols_; ncol++)
|
---|
[861] | 416 | {
|
---|
[1136] | 417 | fits_read_col(fptr_,TFLOAT,ncol+1,NoLine+1,1,1,&fnull,&fdata[ncol],&anull,&status);
|
---|
| 418 | if (status)
|
---|
[903] | 419 | {
|
---|
[1136] | 420 | ResetStatus(status);
|
---|
| 421 | break;
|
---|
[903] | 422 | }
|
---|
[1136] | 423 | }
|
---|
| 424 | }
|
---|
[839] | 425 |
|
---|
[903] | 426 |
|
---|
[1136] | 427 | void FitsInFile::GetBinTabFCol(double* valeurs,int nentries, int NoCol) const
|
---|
[839] | 428 | {
|
---|
| 429 | int status= 0;
|
---|
| 430 | int DTYPE;
|
---|
| 431 | long repeat,width;
|
---|
| 432 | fits_get_coltype(fptr_, NoCol+1,&DTYPE,&repeat,&width,&status);
|
---|
| 433 | if( DTYPE != TDOUBLE)
|
---|
| 434 | {
|
---|
[1045] | 435 | if (DTYPE == TFLOAT) cout << " WARNING: reading double from float : conversion will be made by fitsio library" << endl;
|
---|
| 436 | else
|
---|
| 437 | throw IOExc("FitsFile::GetBinTabFCol, tentative de lecture non double");
|
---|
[839] | 438 | }
|
---|
| 439 | long nels=nentries;
|
---|
[971] | 440 | int anull;
|
---|
[839] | 441 | // no checking for undefined pixels
|
---|
[971] | 442 | double dnull= 0.;
|
---|
| 443 | // fits_read_key(fptr_,TDOUBLE,"BAD_DATA",&dnull,NULL,&status);
|
---|
| 444 | // if (status != 0)
|
---|
| 445 | // {
|
---|
| 446 | // dnull = -1.6375e30; // default value
|
---|
| 447 | // status = 0;
|
---|
| 448 | // }
|
---|
| 449 | if (nentries != nrows_*repeat)
|
---|
| 450 | {
|
---|
| 451 | cout << " found " << nentries << " pixels, expected: " << nrows_*repeat << endl;
|
---|
| 452 | throw PException(" FitsFile:::GetBinTabFCol ");
|
---|
| 453 | }
|
---|
[839] | 454 | fits_read_col(fptr_,TDOUBLE,NoCol+1,1,1,nels,&dnull,valeurs,
|
---|
| 455 | &anull,&status);
|
---|
| 456 | if( status ) printerror( status,"erreur lecture de colonne" );
|
---|
[971] | 457 |
|
---|
[839] | 458 | }
|
---|
| 459 |
|
---|
[1136] | 460 | void FitsInFile::GetBinTabFCol(float* valeurs,int nentries, int NoCol) const
|
---|
[839] | 461 | {
|
---|
| 462 | int status= 0;
|
---|
| 463 | int DTYPE;
|
---|
| 464 | long repeat,width;
|
---|
| 465 | fits_get_coltype(fptr_, NoCol+1,&DTYPE,&repeat,&width,&status);
|
---|
| 466 | if( DTYPE != TFLOAT)
|
---|
| 467 | {
|
---|
[1045] | 468 | if (DTYPE == TDOUBLE) cout << " WARNING: reading float from double : conversion will be made by fitsio library" << endl;
|
---|
| 469 | else
|
---|
| 470 | throw IOExc("FitsFile::GetBinTabFCol, tentative de lecture non float");
|
---|
[839] | 471 | }
|
---|
| 472 | long nels=nentries;
|
---|
[971] | 473 | int anull;
|
---|
[839] | 474 | // no checking for undefined pixels
|
---|
| 475 | float fnull= 0.;
|
---|
[971] | 476 | // fits_read_key(fptr_,TFLOAT,"BAD_DATA",&fnull,NULL,&status);
|
---|
| 477 | // if (status != 0)
|
---|
| 478 | // {
|
---|
| 479 | // fnull = -1.6375e30; // default value
|
---|
| 480 | // status = 0;
|
---|
| 481 | // }
|
---|
| 482 | if (nentries != nrows_*repeat)
|
---|
| 483 | {
|
---|
| 484 | cout << " found " << nentries << " pixels, expected: " << nrows_*repeat << endl;
|
---|
| 485 | throw PException(" FitsFile:::GetBinTabFCol ");
|
---|
| 486 | }
|
---|
[839] | 487 | fits_read_col(fptr_,TFLOAT,NoCol+1,1,1,nels,&fnull,valeurs,
|
---|
| 488 | &anull,&status);
|
---|
| 489 | if( status ) printerror( status,"erreur lecture de colonne" );
|
---|
| 490 | }
|
---|
[1136] | 491 |
|
---|
| 492 | void FitsInFile::GetBinTabFCol(int* valeurs,int nentries, int NoCol) const
|
---|
[839] | 493 | {
|
---|
| 494 | int status= 0;
|
---|
| 495 | int DTYPE;
|
---|
| 496 | long repeat,width;
|
---|
| 497 | fits_get_coltype(fptr_, NoCol+1,&DTYPE,&repeat,&width,&status);
|
---|
| 498 | if( DTYPE != TLONG && DTYPE != TINT && DTYPE != TSHORT )
|
---|
| 499 | {
|
---|
| 500 | throw IOExc("FitsFile::GetBinTabFCol, tentative de lecture non entier");
|
---|
| 501 | }
|
---|
| 502 | long nels=nentries;
|
---|
| 503 | // no checking for undefined pixels
|
---|
| 504 | int anull;
|
---|
| 505 | int inull= 0;
|
---|
[971] | 506 | // fits_read_key(fptr_,TINT,"BAD_DATA",&inull,NULL,&status);
|
---|
| 507 | // if (status != 0)
|
---|
| 508 | // {
|
---|
| 509 | // inull = -999999; // default value
|
---|
| 510 | // status = 0;
|
---|
| 511 | // }
|
---|
| 512 | if (nentries != nrows_*repeat)
|
---|
| 513 | {
|
---|
| 514 | cout << " found " << nentries << " pixels, expected: " << nrows_*repeat << endl;
|
---|
| 515 | throw PException(" FitsFile:::GetBinTabFCol ");
|
---|
| 516 | }
|
---|
[839] | 517 | fits_read_col(fptr_,TINT,NoCol+1,1,1,nels,&inull,valeurs,
|
---|
| 518 | &anull,&status);
|
---|
| 519 | if( status ) printerror( status,"erreur lecture de colonne" );
|
---|
| 520 | }
|
---|
[1136] | 521 |
|
---|
| 522 | void FitsInFile::GetBinTabFCol(char** valeurs, int nentries, int NoCol) const
|
---|
[839] | 523 | {
|
---|
| 524 | int status= 0;
|
---|
| 525 | int DTYPE;
|
---|
| 526 | long repeat,width;
|
---|
| 527 | fits_get_coltype(fptr_, NoCol+1,&DTYPE,&repeat,&width,&status);
|
---|
| 528 | if( DTYPE != TSTRING )
|
---|
| 529 | {
|
---|
| 530 | throw IOExc("FitsFile::GetBinTabFCol, tentative de lecture non float");
|
---|
| 531 | }
|
---|
| 532 | long nels=nentries;
|
---|
| 533 | // no checking for undefined pixels
|
---|
| 534 | int anull;
|
---|
[971] | 535 | char* cnull= "";
|
---|
| 536 | if (nentries != nrows_*repeat/width)
|
---|
| 537 | {
|
---|
| 538 | cout << " found " << nentries << " pixels, expected: " << nrows_*repeat/width << endl;
|
---|
| 539 | throw PException(" FitsFile:::GetBinTabFCol ");
|
---|
| 540 | }
|
---|
[839] | 541 | long frow=1;
|
---|
| 542 | long felem=1;
|
---|
| 543 | fits_read_col(fptr_,TSTRING,NoCol+1,frow,felem,nels,cnull,valeurs,
|
---|
| 544 | &anull,&status);
|
---|
| 545 | if( status ) printerror( status,"erreur lecture de colonne" );
|
---|
| 546 | }
|
---|
[1045] | 547 |
|
---|
[1136] | 548 | void FitsInFile::GetSingleColumn(double* map, int nentries) const
|
---|
| 549 | {
|
---|
| 550 | int status = 0;
|
---|
| 551 | if(hdutype_ == IMAGE_HDU)
|
---|
[1045] | 552 | {
|
---|
[1136] | 553 |
|
---|
| 554 | if(bitpix_ != DOUBLE_IMG)
|
---|
[1047] | 555 | {
|
---|
[1136] | 556 | cout << " The data type on fits file is not double...";
|
---|
| 557 | cout << " Conversion to double achieved by cfitsio lib" << endl;
|
---|
[1047] | 558 | }
|
---|
[1136] | 559 |
|
---|
| 560 | // no checking for undefined pixels
|
---|
| 561 | int anull;
|
---|
| 562 | double dnull= 0.;
|
---|
| 563 |
|
---|
| 564 | long nels= nentries;
|
---|
| 565 | fits_read_img(fptr_,TDOUBLE,1,nels,&dnull,map,&anull,&status);
|
---|
| 566 | if( status ) printerror( status );
|
---|
[1045] | 567 | }
|
---|
[1136] | 568 | else
|
---|
| 569 | if(hdutype_ == ASCII_TBL || hdutype_ == BINARY_TBL)
|
---|
| 570 | {
|
---|
| 571 | GetBinTabFCol(map,nentries, 0);
|
---|
| 572 | }
|
---|
| 573 | else
|
---|
| 574 | {
|
---|
| 575 | cout << " hdutype= " << hdutype_ << endl;
|
---|
| 576 | throw IOExc("FitsFile::GetSingleColumn this HDU is unknown");
|
---|
| 577 | }
|
---|
[1045] | 578 | }
|
---|
| 579 |
|
---|
[1136] | 580 | void FitsInFile::GetSingleColumn(float* map, int nentries) const
|
---|
[1045] | 581 | {
|
---|
[1136] | 582 | int status = 0;
|
---|
| 583 | if(hdutype_ == IMAGE_HDU)
|
---|
[1045] | 584 | {
|
---|
[1136] | 585 | if(bitpix_ != FLOAT_IMG)
|
---|
[1047] | 586 | {
|
---|
[1136] | 587 | cout << " The data type on fits file is not float ";
|
---|
| 588 | cout << " Conversion to float achieved by cfitsio lib" << endl;
|
---|
[1047] | 589 | }
|
---|
[1136] | 590 | // no checking for undefined pixels
|
---|
| 591 | int anull;
|
---|
| 592 | float fnull= 0.;
|
---|
| 593 |
|
---|
| 594 | long nels= nentries;
|
---|
| 595 | fits_read_img(fptr_,TFLOAT,1,nels,&fnull, map,&anull,&status);
|
---|
| 596 | if( status ) printerror( status );
|
---|
[1045] | 597 | }
|
---|
[839] | 598 | else
|
---|
[1136] | 599 | if(hdutype_ == ASCII_TBL || hdutype_ == BINARY_TBL)
|
---|
| 600 | {
|
---|
| 601 | GetBinTabFCol(map,nentries, 0);
|
---|
| 602 | }
|
---|
[839] | 603 | else
|
---|
| 604 | {
|
---|
[1136] | 605 | cout << " hdutype= " << hdutype_ << endl;
|
---|
| 606 | throw IOExc("FitsFile::GetSingleColumn this HDU is unknown");
|
---|
[839] | 607 | }
|
---|
| 608 | }
|
---|
| 609 |
|
---|
[1136] | 610 | void FitsInFile::GetSingleColumn( int* map, int nentries) const
|
---|
[839] | 611 | {
|
---|
[1136] | 612 | int status = 0;
|
---|
| 613 | if(hdutype_ == IMAGE_HDU)
|
---|
[839] | 614 | {
|
---|
[1136] | 615 | if(bitpix_ != LONG_IMG)
|
---|
| 616 | {
|
---|
| 617 | cout << " The data type on fits file is not int ";
|
---|
| 618 | cout << " Conversion to float achieved by cfitsio lib" << endl;
|
---|
| 619 | }
|
---|
| 620 | // no checking for undefined pixels
|
---|
| 621 | int anull;
|
---|
| 622 | float fnull= 0.;
|
---|
| 623 |
|
---|
| 624 | long nels= nentries;
|
---|
| 625 | fits_read_img(fptr_,TINT,1,nels,&fnull,map,&anull,&status);
|
---|
| 626 | if( status ) printerror( status );
|
---|
[839] | 627 | }
|
---|
| 628 | else
|
---|
[1136] | 629 | if(hdutype_ == ASCII_TBL || hdutype_ == BINARY_TBL)
|
---|
| 630 | {
|
---|
| 631 | GetBinTabFCol(map,nentries, 0);
|
---|
| 632 | }
|
---|
[839] | 633 | else
|
---|
[1136] | 634 | {
|
---|
| 635 | cout << " hdutype= " << hdutype_ << endl;
|
---|
| 636 | throw IOExc("FitsFile::GetSingleColumn this HDU is unknown");
|
---|
| 637 | }
|
---|
[839] | 638 | }
|
---|
| 639 |
|
---|
[1136] | 640 | void FitsInFile::GetBinTabParameters(fitsfile* fileptr, int& nbcols, int& nrows,
|
---|
[903] | 641 | vector<int>& repeat,
|
---|
| 642 | vector<string>& noms,
|
---|
| 643 | vector<char>& types,
|
---|
| 644 | vector<int>& taille_des_chaines)
|
---|
[839] | 645 | {
|
---|
| 646 | int status= 0;
|
---|
[903] | 647 | int hdunum=0;
|
---|
| 648 | int hdutype=0;
|
---|
| 649 | fits_get_hdu_num(fileptr,&hdunum);
|
---|
| 650 | fits_get_hdu_type(fileptr, &hdutype, &status);
|
---|
| 651 |
|
---|
| 652 | if(hdutype != ASCII_TBL && hdutype != BINARY_TBL)
|
---|
[839] | 653 | {
|
---|
[903] | 654 | throw IOExc("FitsFile::GetBinTabParameters this HDU is not an ASCII table nor a binary table");
|
---|
[839] | 655 | }
|
---|
[903] | 656 | if(hdutype == ASCII_TBL)
|
---|
| 657 | cout << " Reading a FITS ascii table in HDU : " << hdunum << endl;
|
---|
| 658 | if(hdutype == BINARY_TBL)
|
---|
| 659 | cout << " Reading a FITS binary table in HDU : " << hdunum << endl;
|
---|
[839] | 660 |
|
---|
| 661 | // get the number of columns
|
---|
[903] | 662 | fits_get_num_cols(fileptr, &nbcols,&status);
|
---|
[839] | 663 | if( status ) printerror( status );
|
---|
| 664 |
|
---|
| 665 | // get the number of rows
|
---|
| 666 | long naxis2= 0;
|
---|
[903] | 667 | fits_get_num_rows(fileptr,&naxis2,&status);
|
---|
[839] | 668 | if( status ) printerror( status );
|
---|
[903] | 669 | nrows = (int)naxis2;
|
---|
[839] | 670 |
|
---|
| 671 | // get the datatype, names and the repeat count
|
---|
[903] | 672 | noms.clear();
|
---|
| 673 | noms.reserve(nbcols);
|
---|
| 674 | types.clear();
|
---|
| 675 | types.reserve(nbcols);
|
---|
| 676 | repeat.clear();
|
---|
| 677 | repeat.reserve(nbcols);
|
---|
| 678 | taille_des_chaines.clear();
|
---|
[839] | 679 | char **ttype = new char*[nbcols];
|
---|
[923] | 680 | int ii;
|
---|
[1175] | 681 | //
|
---|
| 682 | //
|
---|
[923] | 683 | for (ii=0; ii < nbcols; ii++) ttype[ii]=new char[FLEN_VALUE];
|
---|
[839] | 684 | int nfound;
|
---|
[903] | 685 | fits_read_keys_str(fileptr, "TTYPE",1,nbcols,ttype,&nfound, &status);
|
---|
[839] | 686 | if( status ) printerror( status,"erreur lecture des noms de colonne");
|
---|
| 687 | int rept=0;
|
---|
[923] | 688 | for(ii = 0; ii < nbcols; ii++)
|
---|
[839] | 689 | {
|
---|
| 690 | int DTYPE;
|
---|
| 691 | long width;
|
---|
[903] | 692 | long repete = 0;
|
---|
| 693 | fits_get_coltype(fileptr,ii+1,&DTYPE,&repete,&width,&status);
|
---|
[839] | 694 | if( status ) printerror( status,"erreur lecture type de colonne");
|
---|
[903] | 695 | rept = repete;
|
---|
| 696 | noms.push_back(string(ttype[ii]));
|
---|
[839] | 697 | switch (DTYPE)
|
---|
| 698 | {
|
---|
| 699 | case TDOUBLE :
|
---|
[903] | 700 | types.push_back('D');
|
---|
[839] | 701 | break;
|
---|
| 702 | case TFLOAT :
|
---|
[903] | 703 | types.push_back('E');
|
---|
[839] | 704 | break;
|
---|
| 705 | case TLONG :
|
---|
[903] | 706 | types.push_back('I');
|
---|
[839] | 707 | break;
|
---|
| 708 | case TINT :
|
---|
[903] | 709 | types.push_back('I');
|
---|
[839] | 710 | break;
|
---|
| 711 | case TSHORT :
|
---|
[903] | 712 | types.push_back('I');
|
---|
[839] | 713 | break;
|
---|
| 714 | case TSTRING :
|
---|
[903] | 715 | types.push_back('S');
|
---|
| 716 | taille_des_chaines.push_back(width);
|
---|
[839] | 717 | rept/=width;
|
---|
| 718 | break;
|
---|
| 719 | default :
|
---|
| 720 | cout << " field " << ii+1 << " DTYPE= " << DTYPE << endl;
|
---|
| 721 | throw IOExc("FitsFile:: unknown type of field");
|
---|
| 722 | }
|
---|
[903] | 723 | repeat.push_back(rept);
|
---|
[1136] | 724 | }
|
---|
| 725 | for (ii=0; ii < nbcols; ii++) delete [] ttype[ii];
|
---|
| 726 | delete [] ttype;
|
---|
| 727 | }
|
---|
| 728 |
|
---|
| 729 | void FitsInFile::KeywordsIntoDVList(fitsfile* fileptr, DVList& dvl, int hdunum)
|
---|
| 730 | {
|
---|
| 731 | int status = 0;
|
---|
| 732 | int hdutype;
|
---|
| 733 | fits_movabs_hdu(fileptr,hdunum,&hdutype,&status);
|
---|
| 734 | if( status ) printerror( status,":KeywordsIntoDVList : erreur movabs");
|
---|
| 735 | // get number of keywords
|
---|
| 736 | int nkeys,keypos;
|
---|
| 737 | fits_get_hdrpos(fileptr,&nkeys,&keypos,&status);
|
---|
| 738 | if( status ) printerror( status );
|
---|
| 739 |
|
---|
| 740 | // put keywords in a DVList object
|
---|
| 741 | char keyname[LEN_KEYWORD]= "";
|
---|
| 742 | char strval[FLEN_VALUE]= "";
|
---|
| 743 | char dtype;
|
---|
| 744 | char card[FLEN_CARD];
|
---|
| 745 | char *comkey = "COMMENT";
|
---|
[1143] | 746 | char comment[FLEN_COMMENT];
|
---|
[1136] | 747 |
|
---|
| 748 | // shift with the number of mandatory keywords
|
---|
[1143] | 749 | // int num= 8;
|
---|
| 750 | int num= 0;
|
---|
| 751 | // primary header
|
---|
| 752 | if (hdunum == 1)
|
---|
| 753 | {
|
---|
| 754 | // read NAXIS
|
---|
| 755 | int naxis=0;
|
---|
| 756 | fits_read_key(fileptr,TINT,"NAXIS",&naxis,NULL,&status);
|
---|
| 757 | // number of mandatory keywords
|
---|
| 758 | num = naxis+3;
|
---|
| 759 | }
|
---|
| 760 | // extensions
|
---|
| 761 | else
|
---|
| 762 | {
|
---|
| 763 | if (hdutype == IMAGE_HDU)
|
---|
| 764 | {
|
---|
| 765 | // read NAXIS
|
---|
| 766 | int naxis=0;
|
---|
| 767 | fits_read_key(fileptr,TINT,"NAXIS",&naxis,NULL,&status);
|
---|
| 768 | // number of mandatory keywords
|
---|
| 769 | num = naxis+5;
|
---|
| 770 | }
|
---|
| 771 | else
|
---|
| 772 | if(hdutype == ASCII_TBL || hdutype == BINARY_TBL)
|
---|
| 773 | {
|
---|
| 774 | // number of mandatory keywords
|
---|
| 775 | num = 8;
|
---|
| 776 | }
|
---|
| 777 | }
|
---|
[1136] | 778 | int j;
|
---|
| 779 | for(j = num+1; j <= nkeys; j++)
|
---|
| 780 | {
|
---|
| 781 | fits_read_keyn(fileptr,j,card,strval,NULL,&status);
|
---|
| 782 | if(status) printerror(status);
|
---|
| 783 |
|
---|
| 784 | strncpy(keyname,card,LEN_KEYWORD-1);
|
---|
| 785 |
|
---|
| 786 | if(strncmp(keyname,comkey,LEN_KEYWORD-1) != 0 && strlen(keyname) != 0
|
---|
| 787 | && strlen(strval) != 0)
|
---|
| 788 | {
|
---|
| 789 | fits_get_keytype(strval,&dtype,&status);
|
---|
| 790 | if(status) printerror(status);
|
---|
| 791 |
|
---|
| 792 | strip(keyname, 'B',' ');
|
---|
| 793 | strip(strval, 'B',' ');
|
---|
| 794 | strip(strval, 'B','\'');
|
---|
| 795 |
|
---|
| 796 | switch( dtype )
|
---|
| 797 | {
|
---|
| 798 | case 'C':
|
---|
[1143] | 799 | fits_read_key(fileptr,TSTRING,keyname,strval,comment,&status);
|
---|
| 800 | dvl[keyname]= strval;
|
---|
| 801 | dvl.SetComment(keyname, comment);
|
---|
[1136] | 802 | break;
|
---|
| 803 | case 'I':
|
---|
| 804 | int ival;
|
---|
[1143] | 805 | fits_read_key(fileptr,TINT,keyname,&ival,comment,&status);
|
---|
[1136] | 806 | dvl[keyname]= (int_4) ival; // Portage mac DY
|
---|
[1143] | 807 | dvl.SetComment(keyname, comment);
|
---|
[1136] | 808 | break;
|
---|
| 809 | case 'L':
|
---|
| 810 | int ilog;
|
---|
[1143] | 811 | fits_read_key(fileptr,TLOGICAL,keyname,&ilog,comment,&status);
|
---|
[1136] | 812 | dvl[keyname]= (int_4) ilog;
|
---|
[1143] | 813 | dvl.SetComment(keyname, comment);
|
---|
[1136] | 814 | break;
|
---|
| 815 | case 'F':
|
---|
| 816 | double dval;
|
---|
[1143] | 817 | fits_read_key(fileptr,TDOUBLE,keyname,&dval,comment,&status);
|
---|
[1136] | 818 | dvl[keyname]= dval;
|
---|
[1143] | 819 | dvl.SetComment(keyname, comment);
|
---|
[1136] | 820 | break;
|
---|
| 821 | }
|
---|
| 822 |
|
---|
| 823 | }
|
---|
[839] | 824 | }
|
---|
[1136] | 825 | // dvl_.Print();
|
---|
| 826 | }
|
---|
| 827 |
|
---|
| 828 | FitsOutFile::FitsOutFile()
|
---|
| 829 | {
|
---|
| 830 | InitNull();
|
---|
[903] | 831 | }
|
---|
[839] | 832 |
|
---|
[1136] | 833 | FitsOutFile::FitsOutFile(char flnm[], bool OldFile)
|
---|
| 834 | {
|
---|
[839] | 835 |
|
---|
[1136] | 836 | InitNull();
|
---|
| 837 | int status = 0;
|
---|
[839] | 838 |
|
---|
[1136] | 839 | // create new FITS file
|
---|
| 840 | if (!OldFile)
|
---|
| 841 | {
|
---|
| 842 | fits_create_file(&fptr_,flnm,&status);
|
---|
| 843 | if( status ) printerror(status,"file already exists");
|
---|
| 844 | }
|
---|
| 845 | else
|
---|
| 846 | {
|
---|
| 847 | fits_open_file(&fptr_,flnm,READWRITE,&status);
|
---|
| 848 | if( status ) printerror(status,"file does not exist");
|
---|
| 849 | fits_get_num_hdus(fptr_, &hdunum_, &status);
|
---|
| 850 | int hdutype;
|
---|
| 851 | fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
|
---|
| 852 | if( status ) printerror( status,":FitsFile::WriteF : erreur movabs");
|
---|
| 853 |
|
---|
| 854 | }
|
---|
| 855 |
|
---|
| 856 | }
|
---|
| 857 |
|
---|
| 858 |
|
---|
| 859 |
|
---|
[1143] | 860 | void FitsOutFile::makeHeaderImageOnFits(char type, int nbdim, int* naxisn, DVList &dvl)
|
---|
[1136] | 861 | {
|
---|
| 862 | int status = 0;
|
---|
| 863 | long naxis = nbdim;
|
---|
| 864 | long* naxes = new long[nbdim];
|
---|
| 865 | if (hdunum_ == 1)
|
---|
| 866 | {
|
---|
[1143] | 867 | if (imageOnPrimary_ == false)
|
---|
| 868 | {
|
---|
| 869 | fits_create_img(fptr_,FLOAT_IMG,0,naxes,&status);
|
---|
| 870 | writeSignatureOnFits(); }
|
---|
[1136] | 871 | else hdunum_--;
|
---|
| 872 | }
|
---|
| 873 | int k;
|
---|
| 874 | for (k=0; k< nbdim; k++) naxes[k] = (long)naxisn[k];
|
---|
| 875 | if (type == 'D')
|
---|
| 876 | fits_create_img(fptr_,DOUBLE_IMG,naxis,naxes,&status);
|
---|
| 877 | else
|
---|
| 878 | if (type == 'E')
|
---|
| 879 | fits_create_img(fptr_,FLOAT_IMG,naxis,naxes,&status);
|
---|
| 880 | else
|
---|
| 881 | if (type == 'I')
|
---|
| 882 | fits_create_img(fptr_,LONG_IMG,naxis,naxes,&status);
|
---|
| 883 | else
|
---|
| 884 | {
|
---|
| 885 | cout << " type of data: " << type << endl;
|
---|
| 886 | throw PException("FitsFile:::makeHeaderImageOnFits:unprogrammed type of data ");
|
---|
| 887 | }
|
---|
| 888 |
|
---|
| 889 | hdunum_++;
|
---|
[1143] | 890 |
|
---|
| 891 | // write supplementary keywords
|
---|
| 892 | // dvl.Print();
|
---|
| 893 | addKeywordsOfDVList(dvl);
|
---|
| 894 |
|
---|
[1136] | 895 | delete [] naxes;
|
---|
| 896 | if( status ) printerror( status, "erreur creation HDU IMAGE" );
|
---|
| 897 |
|
---|
| 898 | }
|
---|
| 899 | void FitsOutFile::putImageToFits(int nbData, double* map) const
|
---|
| 900 | {
|
---|
| 901 | int status = 0;
|
---|
| 902 | long npix= nbData;
|
---|
| 903 | fits_write_img(fptr_,TDOUBLE,1,npix,map,&status);
|
---|
| 904 | if( status ) printerror( status, "erreur ecriture putImageToFits" );
|
---|
[1143] | 905 | // writeSignatureOnFits();
|
---|
[1136] | 906 | }
|
---|
| 907 |
|
---|
| 908 | void FitsOutFile::putImageToFits(int nbData, float* map) const
|
---|
| 909 | {
|
---|
| 910 | int status = 0;
|
---|
| 911 | long npix= nbData;
|
---|
| 912 | fits_write_img(fptr_,TFLOAT,1,npix, map,&status);
|
---|
| 913 | if( status ) printerror( status, "erreur ecriture putImageToFits" );
|
---|
[1143] | 914 | // writeSignatureOnFits();
|
---|
[1136] | 915 |
|
---|
| 916 | }
|
---|
| 917 | void FitsOutFile::putImageToFits( int nbData, int* map) const
|
---|
| 918 | {
|
---|
| 919 | int status = 0;
|
---|
| 920 |
|
---|
| 921 | long npix= nbData;
|
---|
| 922 | fits_write_img(fptr_,TINT,1,npix,map,&status);
|
---|
| 923 | if( status ) printerror( status, "erreur ecriture putImageToFits" );
|
---|
[1143] | 924 | // writeSignatureOnFits();
|
---|
[1136] | 925 | }
|
---|
| 926 |
|
---|
| 927 |
|
---|
| 928 |
|
---|
| 929 | void FitsOutFile::makeHeaderBntblOnFits( char* fieldType, char** Noms, int nentries, int tfields, DVList &dvl, char* extname, vector<int> taille_des_chaines)
|
---|
[839] | 930 | {
|
---|
| 931 | int status = 0;
|
---|
| 932 | long nrows;
|
---|
| 933 | if (strlen(fieldType) != tfields)
|
---|
| 934 | {
|
---|
| 935 | cout << " nombre de champs :" << tfields << "nombre de types: " << strlen(fieldType) << endl;
|
---|
[1136] | 936 | throw ParmError("FitsFile:: fields and types don't match");
|
---|
[839] | 937 |
|
---|
| 938 | }
|
---|
| 939 | char ** ttype= new char*[tfields];
|
---|
| 940 | char ** tform= new char*[tfields];
|
---|
| 941 | char largeur[FLEN_VALUE];
|
---|
| 942 | int noColString=0;
|
---|
[971] | 943 | int k;
|
---|
| 944 | for (k=0; k<tfields;k++)
|
---|
[839] | 945 | {
|
---|
| 946 | char format[FLEN_VALUE];
|
---|
| 947 |
|
---|
| 948 | if(nentries < 1024)
|
---|
| 949 | {
|
---|
| 950 | nrows= nentries;
|
---|
| 951 | if (fieldType[k] == 'A')
|
---|
| 952 | {
|
---|
| 953 | sprintf(largeur,"%d",taille_des_chaines[noColString++]);
|
---|
| 954 | strcpy(format,largeur);
|
---|
| 955 | }
|
---|
| 956 | else strcpy(format,"1");
|
---|
| 957 | }
|
---|
| 958 | else
|
---|
| 959 | {
|
---|
| 960 | nrows = nentries/1024;
|
---|
| 961 | if(nentries%1024 != 0) nrows++;
|
---|
| 962 | if (fieldType[k] == 'A')
|
---|
| 963 | {
|
---|
[1136] | 964 | char largaux[FLEN_VALUE];
|
---|
| 965 | sprintf(largeur,"%d",taille_des_chaines[noColString]);
|
---|
| 966 | sprintf(largaux,"%d",1024*taille_des_chaines[noColString]);
|
---|
| 967 | noColString++;
|
---|
| 968 | strcpy(format, largaux);
|
---|
[839] | 969 | }
|
---|
| 970 | else strcpy(format,"1024");
|
---|
| 971 | }
|
---|
| 972 | strncat(format,&fieldType[k],1);
|
---|
| 973 | if (fieldType[k] == 'A')
|
---|
| 974 | {
|
---|
| 975 | strcat(format,largeur);
|
---|
| 976 | }
|
---|
| 977 | ttype[k]= new char[FLEN_VALUE];
|
---|
| 978 | strcpy(ttype[k],Noms[k]);
|
---|
| 979 | tform[k]= new char[FLEN_VALUE];
|
---|
| 980 | strcpy(tform[k],format);
|
---|
| 981 | }
|
---|
| 982 | // value of the EXTNAME keyword
|
---|
| 983 | char extn[FLEN_VALUE];
|
---|
| 984 | strncpy(extn,extname,FLEN_VALUE);
|
---|
| 985 |
|
---|
| 986 | // create a new empty binary table onto the FITS file
|
---|
| 987 | // physical units if they exist, are defined in the DVList object
|
---|
| 988 | // so the NULL pointer is given for the tunit parameters.
|
---|
| 989 | nrows=0;
|
---|
| 990 | fits_create_tbl(fptr_,BINARY_TBL,nrows,tfields,ttype,tform,
|
---|
| 991 | NULL,extn,&status);
|
---|
| 992 | if( status ) printerror( status );
|
---|
[1026] | 993 | if ( hdunum_ == 0 ) hdunum_ = 2;
|
---|
| 994 | else hdunum_++;
|
---|
[971] | 995 | int ii;
|
---|
| 996 | for(ii = 0; ii < tfields; ii++)
|
---|
[839] | 997 | {
|
---|
| 998 | delete [] ttype[ii];
|
---|
| 999 | delete [] tform[ii];
|
---|
| 1000 | }
|
---|
| 1001 | delete [] ttype;
|
---|
| 1002 | delete [] tform;
|
---|
| 1003 | //
|
---|
| 1004 | // write supplementary keywords
|
---|
[1143] | 1005 | addKeywordsOfDVList(dvl);
|
---|
[839] | 1006 |
|
---|
| 1007 | }
|
---|
| 1008 |
|
---|
[1136] | 1009 | void FitsOutFile::putColToFits(int nocol, int nentries, double* donnees) const
|
---|
[839] | 1010 | {
|
---|
| 1011 | int status = 0;
|
---|
[971] | 1012 | int hdutype;
|
---|
[839] | 1013 | fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
|
---|
| 1014 | if( status ) printerror(status,"putColToFits: le movabs a foire");
|
---|
| 1015 | fits_get_hdu_type(fptr_, &hdutype, &status);
|
---|
[867] | 1016 | if(hdutype != ASCII_TBL && hdutype != BINARY_TBL)
|
---|
| 1017 | {
|
---|
| 1018 | cout << " hdunum= " << hdunum_ << " hdutype= " << hdutype << endl;
|
---|
| 1019 | throw IOExc("FitsFile::putColToFits, this HDU is not an ASCII table nor a binary table");
|
---|
| 1020 | }
|
---|
[839] | 1021 | int code;
|
---|
| 1022 | long repeat, width;
|
---|
| 1023 | fits_get_coltype(fptr_, nocol+1, &code, &repeat,&width, &status);
|
---|
| 1024 | if( code != TDOUBLE)
|
---|
| 1025 | {
|
---|
| 1026 | cout << " WARNING : types don't match (putColToFits) : on fits file= " << code << " to be written= DOUBLE " << endl;
|
---|
| 1027 | }
|
---|
| 1028 | fits_write_col(fptr_,TDOUBLE,nocol+1,1,1,nentries, donnees ,&status);
|
---|
| 1029 | if( status ) printerror( status,"erreur ecriture du fichier fits" );
|
---|
| 1030 | }
|
---|
[1136] | 1031 | void FitsOutFile::putColToFits(int nocol, int nentries, float* donnees) const
|
---|
[839] | 1032 | {
|
---|
| 1033 | int status = 0;
|
---|
| 1034 | int hdutype;
|
---|
| 1035 | fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
|
---|
| 1036 | if( status ) printerror(status,"putColToFits: le movabs a foire");
|
---|
| 1037 | fits_get_hdu_type(fptr_, &hdutype, &status);
|
---|
| 1038 | if(hdutype != ASCII_TBL && hdutype != BINARY_TBL)
|
---|
| 1039 | {
|
---|
| 1040 | cout << " hdunum= " << hdunum_ << " hdutype= " << hdutype << endl;
|
---|
| 1041 | throw IOExc("FitsFile::putColToFits, this HDU is not an ASCII table nor a binary table");
|
---|
| 1042 | }
|
---|
| 1043 | if(hdutype == ASCII_TBL && nocol>0)
|
---|
| 1044 | {
|
---|
| 1045 | throw IOExc("FitsFile::putColToFits, this HDU is an ASCII table, nocol>0 forbidden");
|
---|
| 1046 | }
|
---|
| 1047 | int code;
|
---|
| 1048 | long repeat, width;
|
---|
| 1049 | fits_get_coltype(fptr_, nocol+1, &code, &repeat,&width, &status);
|
---|
| 1050 | if( code != TFLOAT)
|
---|
| 1051 | {
|
---|
| 1052 | cout << " WARNING : types don't match (putColToFits) : on fits file= " << code << " (FITS code), to be written= FLOAT " << endl;
|
---|
| 1053 | }
|
---|
| 1054 | fits_write_col(fptr_,TFLOAT,nocol+1,1,1,nentries, donnees ,&status);
|
---|
| 1055 | if( status ) printerror( status,"erreur ecriture du fichier fits" );
|
---|
| 1056 | }
|
---|
[1136] | 1057 | void FitsOutFile::putColToFits(int nocol, int nentries, int* donnees) const
|
---|
[839] | 1058 | {
|
---|
| 1059 | int status = 0;
|
---|
| 1060 | int hdutype;
|
---|
| 1061 | fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
|
---|
| 1062 | if( status ) printerror(status,"putColToFits: le movabs a foire");
|
---|
| 1063 | fits_get_hdu_type(fptr_, &hdutype, &status);
|
---|
| 1064 | if(hdutype != ASCII_TBL && hdutype != BINARY_TBL)
|
---|
| 1065 | {
|
---|
| 1066 | cout << " hdunum= " << hdunum_ << " hdutype= " << hdutype << endl;
|
---|
| 1067 | throw IOExc("FitsFile::putColToFits, this HDU is not an ASCII table nor a binary table");
|
---|
| 1068 | }
|
---|
| 1069 | if(hdutype == ASCII_TBL && nocol>0)
|
---|
| 1070 | {
|
---|
| 1071 | throw IOExc("FitsFile::putColToFits, this HDU is an ASCII table, nocol>0 forbidden");
|
---|
| 1072 | }
|
---|
| 1073 | int code;
|
---|
| 1074 | long repeat, width;
|
---|
| 1075 | fits_get_coltype(fptr_, nocol+1, &code, &repeat,&width, &status);
|
---|
| 1076 | if( code != TLONG && code != TINT && code != TSHORT )
|
---|
| 1077 | {
|
---|
[971] | 1078 | cout << " WARNING : types don't match (putColToFits) : on fits file= " << code << " (FITS code), to be written= INT " << endl;
|
---|
[839] | 1079 | }
|
---|
| 1080 | fits_write_col(fptr_,TINT,nocol+1,1,1,nentries, donnees ,&status);
|
---|
[971] | 1081 | if( status ) printerror( status," ecriture du fichier fits" );
|
---|
[839] | 1082 | }
|
---|
[1136] | 1083 | void FitsOutFile::putColToFits(int nocol, int nentries, char** donnees) const
|
---|
[839] | 1084 | {
|
---|
| 1085 | int status = 0;
|
---|
| 1086 | int hdutype;
|
---|
| 1087 | fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
|
---|
| 1088 | if( status ) printerror(status,"putColToFits: le movabs a foire");
|
---|
| 1089 | fits_get_hdu_type(fptr_, &hdutype, &status);
|
---|
| 1090 | if(hdutype != ASCII_TBL && hdutype != BINARY_TBL)
|
---|
| 1091 | {
|
---|
| 1092 | cout << " hdunum= " << hdunum_ << " hdutype= " << hdutype << endl;
|
---|
| 1093 | throw IOExc("FitsFile::putColToFits, this HDU is not an ASCII table nor a binary table");
|
---|
| 1094 | }
|
---|
| 1095 | if(hdutype == ASCII_TBL && nocol>0)
|
---|
| 1096 | {
|
---|
| 1097 | throw IOExc("FitsFile::putColToFits, this HDU is an ASCII table, nocol>0 forbidden");
|
---|
| 1098 | }
|
---|
| 1099 | int code;
|
---|
| 1100 | long repeat, width;
|
---|
| 1101 | fits_get_coltype(fptr_, nocol+1, &code, &repeat,&width, &status);
|
---|
| 1102 | if( code != TSTRING)
|
---|
| 1103 | {
|
---|
| 1104 | cout << " WARNING : types don't match (putColToFits) : on fits file= " << code << " (FITS code), to be written= char** " << endl;
|
---|
| 1105 | }
|
---|
| 1106 | fits_write_col(fptr_,TSTRING,nocol+1,1,1,nentries, donnees ,&status);
|
---|
| 1107 | if( status ) printerror( status,"erreur ecriture du fichier fits" );
|
---|
| 1108 | }
|
---|
| 1109 |
|
---|
[1143] | 1110 | void FitsOutFile::DVListIntoPrimaryHeader(DVList& dvl) const
|
---|
| 1111 | {
|
---|
| 1112 | int status = 0;
|
---|
| 1113 | int hdutype;
|
---|
| 1114 | fits_movabs_hdu(fptr_,1,&hdutype,&status);
|
---|
| 1115 | addKeywordsOfDVList(dvl);
|
---|
| 1116 | fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
|
---|
| 1117 | }
|
---|
[839] | 1118 |
|
---|
[1143] | 1119 |
|
---|
[1136] | 1120 | void FitsOutFile::writeSignatureOnFits() const
|
---|
[839] | 1121 | {
|
---|
| 1122 | int status = 0;
|
---|
| 1123 | char keyname[LEN_KEYWORD];
|
---|
| 1124 | char strval[FLEN_VALUE];
|
---|
| 1125 | char comment[FLEN_COMMENT];
|
---|
[971] | 1126 | strncpy(keyname, "CREATOR", LEN_KEYWORD);
|
---|
| 1127 | keyname[LEN_KEYWORD-1] = '\0';
|
---|
| 1128 | strcpy(strval, "SOPHYA");
|
---|
| 1129 | strcpy(comment," SOPHYA Package - FITSIOServer ");
|
---|
| 1130 | fits_write_key(fptr_, TSTRING, keyname, &strval, comment, &status);
|
---|
| 1131 | if( status ) printerror( status );
|
---|
[1143] | 1132 | fits_write_date(fptr_, &status);
|
---|
[971] | 1133 | fits_write_comment(fptr_,"..............................................", &status);
|
---|
| 1134 | fits_write_comment(fptr_, " SOPHYA package - FITSIOSever ", &status);
|
---|
| 1135 | fits_write_comment(fptr_, " (C) LAL/IN2P3-CNRS Orsay, FRANCE 2000", &status);
|
---|
| 1136 | fits_write_comment(fptr_, " (C) DAPNIA/CEA Saclay, FRANCE 2000", &status);
|
---|
| 1137 | fits_write_comment(fptr_,"..............................................", &status);
|
---|
[1045] | 1138 | if( status ) printerror( status, "erreur writeSignatureOnFits" );
|
---|
[839] | 1139 | }
|
---|
| 1140 |
|
---|
[903] | 1141 |
|
---|
[1143] | 1142 | void FitsOutFile::addKeywordsOfDVList(DVList& dvl) const
|
---|
| 1143 | {
|
---|
| 1144 | int status = 0;
|
---|
| 1145 | fits_write_comment(fptr_,"---------- keywords from SOPHYA ---------", &status);
|
---|
| 1146 | if (hdunum_ == 1) writeSignatureOnFits();
|
---|
| 1147 | DVList::ValList::const_iterator it;
|
---|
| 1148 | for(it = dvl.Begin(); it != dvl.End(); it++)
|
---|
| 1149 | {
|
---|
| 1150 | char keytype= (*it).second.elval.typ;
|
---|
| 1151 | char keyname[10];
|
---|
| 1152 | strncpy(keyname,(*it).first.substr(0,64).c_str(),10);
|
---|
| 1153 | char comment[FLEN_COMMENT];
|
---|
| 1154 | char strval[FLEN_VALUE]= "";
|
---|
| 1155 | char *comkey = "COMMENT";
|
---|
| 1156 | fits_read_keyword(fptr_, keyname, strval, NULL, &status);
|
---|
| 1157 | if (status != 0 || strncmp(keyname,comkey,LEN_KEYWORD-1) == 0 )
|
---|
| 1158 | {
|
---|
| 1159 | status = 0;
|
---|
| 1160 | switch (keytype)
|
---|
| 1161 | {
|
---|
| 1162 | case 'I' :
|
---|
| 1163 | {
|
---|
| 1164 | int ival=(*it).second.elval.iv;
|
---|
| 1165 | strncpy(comment,(*it).second.elcomm.c_str(),FLEN_COMMENT );
|
---|
| 1166 | fits_write_key(fptr_,TINT,keyname,&ival,comment,&status);
|
---|
| 1167 | break;
|
---|
| 1168 | }
|
---|
| 1169 | case 'D' :
|
---|
| 1170 | {
|
---|
| 1171 | double dval=(*it).second.elval.dv;
|
---|
| 1172 | strncpy(comment,(*it).second.elcomm.c_str(),FLEN_COMMENT );
|
---|
| 1173 | fits_write_key(fptr_,TDOUBLE,keyname,&dval,comment,&status);
|
---|
| 1174 | break;
|
---|
| 1175 | }
|
---|
| 1176 | case 'S' :
|
---|
| 1177 | {
|
---|
| 1178 | char strval[128];
|
---|
| 1179 | strncpy(strval,(*it).second.elval.strv->c_str(),127);
|
---|
| 1180 | strncpy(comment,(*it).second.elcomm.c_str(),FLEN_COMMENT );
|
---|
| 1181 | fits_write_key(fptr_,TSTRING,keyname,&strval,comment,&status);
|
---|
| 1182 | break;
|
---|
| 1183 | }
|
---|
| 1184 | }
|
---|
| 1185 | }
|
---|
| 1186 | if( status ) printerror( status,"fitsfile: probleme ecriture mot-cle du dvlist" );
|
---|
| 1187 | }
|
---|
| 1188 | fits_write_comment(fptr_,"--------------------------------------", &status);
|
---|
| 1189 | }
|
---|
[903] | 1190 |
|
---|
| 1191 |
|
---|
[839] | 1192 |
|
---|