1 | #include "machdefs.h"
|
---|
2 | #include <stdlib.h>
|
---|
3 | #include "fitsfile.h"
|
---|
4 | #include "pexceptions.h"
|
---|
5 | #include "strutil.h"
|
---|
6 | #include "anydataobj.h"
|
---|
7 | #include "fitsspherehealpix.h"
|
---|
8 |
|
---|
9 | /*!
|
---|
10 | \defgroup FitsIOServer FitsIOServer module
|
---|
11 | This module contains classes which handle FITS format I/O for
|
---|
12 | SOPHYA objects. This module uses cfitsio library.
|
---|
13 | */
|
---|
14 |
|
---|
15 | void BnTblLine::setFormat(int dc, int fc, int ic, int lc, int bc,int cc, vector<string> names)
|
---|
16 | {
|
---|
17 | int nbcols = dc + fc + ic + cc + lc + bc;
|
---|
18 | int maxName = names.size();
|
---|
19 | if (nbcols != maxName)
|
---|
20 | {
|
---|
21 | cout << " WARNING: BnTblLine:: length of vector of column names not equal to total number of columns" << endl;
|
---|
22 | maxName = nbcols < maxName ? nbcols : maxName;
|
---|
23 | }
|
---|
24 | ColName_ = vector<string>(nbcols);
|
---|
25 | for (int k=0; k < maxName; k++) ColName_[k] = names[k];
|
---|
26 | if (dc >0) ddata_ = vector<double>(dc);
|
---|
27 | if (fc >0) fdata_ = vector<float>(fc);
|
---|
28 | if (ic >0) idata_ = vector<int>(fc);
|
---|
29 | if (cc >0) cdata_ = vector<string>(fc);
|
---|
30 | if (lc >0) ldata_ = vector<long>(lc);
|
---|
31 | if (bc >0) bdata_ = vector<unsigned char>(bc);
|
---|
32 | }
|
---|
33 |
|
---|
34 | bool BnTblLine::sameFormat(const BnTblLine& btl) const
|
---|
35 | {
|
---|
36 | if (btl.ddata_.size() == ddata_.size() && btl.fdata_.size() == fdata_.size() && btl.idata_.size() == idata_.size() && btl.cdata_.size() == cdata_.size() && btl.ldata_.size() == ldata_.size() && btl.bdata_.size() == bdata_.size()) return true;
|
---|
37 | else return false;
|
---|
38 | }
|
---|
39 |
|
---|
40 | void BnTblLine::Print()
|
---|
41 | {
|
---|
42 | int k;
|
---|
43 | cout << " ********* ligne ************* " << endl;
|
---|
44 | cout << " *** noms de variables " << endl;
|
---|
45 | for (k=0; k < ColName_.size(); k++) cout << ColName_[k] << " ";
|
---|
46 | cout << endl;
|
---|
47 | cout << " *** variables doubles " << endl;
|
---|
48 | for (k=0; k < ddata_.size(); k++) cout << ddata_[k] << " ";
|
---|
49 | cout << endl;
|
---|
50 | cout << " *** variables float " << endl;
|
---|
51 | for (k=0; k < fdata_.size(); k++) cout << fdata_[k] << " ";
|
---|
52 | cout << endl;
|
---|
53 | cout << " *** variables int " << endl;
|
---|
54 | for (k=0; k < idata_.size(); k++) cout << idata_[k] << " ";
|
---|
55 | cout << endl;
|
---|
56 | cout << " *** variables string " << endl;
|
---|
57 | for (k=0; k < cdata_.size(); k++) cout << cdata_[k] << " ";
|
---|
58 | cout << endl;
|
---|
59 | cout << " *** variables long " << endl;
|
---|
60 | for (k=0; k < ldata_.size(); k++) cout << ldata_[k] << " ";
|
---|
61 | cout << endl;
|
---|
62 | cout << " *** variables byte " << endl;
|
---|
63 | for (k=0; k < bdata_.size(); k++) cout << (int)bdata_[k] << " ";
|
---|
64 | cout << endl;
|
---|
65 | cout << " ***************************** " << endl;
|
---|
66 | }
|
---|
67 |
|
---|
68 |
|
---|
69 |
|
---|
70 | /*!
|
---|
71 | \class SOPHYA::FitsIOHandler
|
---|
72 | \ingroup FitsIOServer
|
---|
73 | The class structure is analogous to Sophya-PPersist system :
|
---|
74 | Each SOPHYA object XXX is associated with a object of class FITS_XXX
|
---|
75 | (inheriting from FitsFileHandler), to which input/output operations with FITS
|
---|
76 | files are delegated (through a class Hierarchy : FitsFile (virtual),
|
---|
77 | FitsInFile, FitsOutFile) . A typical example of use is the following :
|
---|
78 |
|
---|
79 | \verbatim
|
---|
80 | int m=... ;
|
---|
81 | SphereHEALPix<r_8> sphere1(m); // definition of the SOPHYA object
|
---|
82 | .... fill the sphere ....
|
---|
83 |
|
---|
84 | FITS_SphereHEALPix<r_8> fits_sph1(sphere1);
|
---|
85 | // delegated object
|
---|
86 | fits_sph.Write("myfile.fits"); // writing on FITS file
|
---|
87 |
|
---|
88 | FITS_SphereHEALPix<r_8> fits_sph2("myfile.fits");
|
---|
89 | // load a delegated object
|
---|
90 | // from FITS file
|
---|
91 | SphereHEALPix<r_8> sphere2=(SphereHEALPix<r_8>)fits_sph2;
|
---|
92 | // casting the delegated object
|
---|
93 | // into a SOPHYA object
|
---|
94 | \endverbatim
|
---|
95 |
|
---|
96 |
|
---|
97 | */
|
---|
98 |
|
---|
99 | /*! \fn void SOPHYA::FitsIOHandler::Read(char flnm[],int hdunum)
|
---|
100 |
|
---|
101 | this method is called from inherited objects :
|
---|
102 |
|
---|
103 | opens a file 'flnm'
|
---|
104 |
|
---|
105 | gets parameters in extension-header (hdunum)
|
---|
106 |
|
---|
107 | calls the method 'ReadFromFits' from the inherited object
|
---|
108 | */
|
---|
109 | void FitsIOHandler::Read(char flnm[],int hdunum)
|
---|
110 | {
|
---|
111 | FitsInFile ifts(flnm);
|
---|
112 | Read(ifts, hdunum);
|
---|
113 | }
|
---|
114 |
|
---|
115 | /*! \fn void SOPHYA::FitsIOHandler::Read(FitsInFile& is, int hdunum)
|
---|
116 | Read the data on extension hdunum (or primary header, if hdunum=1) from FitsInFIle. If hdunum is not addressed, , one reads the next extension, with respect to the current position.
|
---|
117 | */
|
---|
118 | void FitsIOHandler::Read(FitsInFile& is, int hdunum)
|
---|
119 | {
|
---|
120 | is.ReadHeader(hdunum);
|
---|
121 | ReadFromFits(is);
|
---|
122 | }
|
---|
123 |
|
---|
124 |
|
---|
125 | /*! \fn void SOPHYA::FitsIOHandler::Write(char flnm[])
|
---|
126 | this method is called from inherited objects.
|
---|
127 |
|
---|
128 | for writing a new object in a new fits-extension :
|
---|
129 |
|
---|
130 | \warning By convention, primary header may contain fits-image data.
|
---|
131 | For switching off this convention (i.e. to make sure that all data will be on fits-extensions) use the method :
|
---|
132 |
|
---|
133 | firstImageOnPrimaryHeader() (see below)
|
---|
134 |
|
---|
135 | calls the method 'WriteToFits' from the inherited object
|
---|
136 |
|
---|
137 | */
|
---|
138 | void FitsIOHandler::Write(char flnm[])
|
---|
139 |
|
---|
140 | {
|
---|
141 | FitsOutFile of(flnm, FitsFile::unknown);
|
---|
142 | Write(of);
|
---|
143 | }
|
---|
144 |
|
---|
145 | void FitsIOHandler::Write(FitsOutFile& os)
|
---|
146 | {
|
---|
147 | WriteToFits(os);
|
---|
148 | }
|
---|
149 |
|
---|
150 |
|
---|
151 |
|
---|
152 |
|
---|
153 | FitsFile::~FitsFile()
|
---|
154 | {
|
---|
155 | int status = 0;
|
---|
156 | if( fptr_ != NULL)
|
---|
157 | {
|
---|
158 | fits_close_file(fptr_,&status);
|
---|
159 | // je ne fais pas delete fptr_, c'est la lib. fitsio qui a fait
|
---|
160 | // new...
|
---|
161 | }
|
---|
162 | if( status ) printerror( status );
|
---|
163 | }
|
---|
164 |
|
---|
165 |
|
---|
166 | void FitsFile::printerror(int &status)
|
---|
167 | //*****************************************************/
|
---|
168 | //* Print out cfitsio error messages and exit program */
|
---|
169 | //*****************************************************/
|
---|
170 | {
|
---|
171 | if( status )
|
---|
172 | {
|
---|
173 | fits_report_error(stderr,status);
|
---|
174 | throw IOExc("FitsFile:: error FITSIO status");
|
---|
175 | }
|
---|
176 | return;
|
---|
177 | }
|
---|
178 |
|
---|
179 | void FitsFile::printerror(int& status, char* texte)
|
---|
180 | //*****************************************************/
|
---|
181 | //* Print out cfitsio error messages and exit program */
|
---|
182 | //*****************************************************/
|
---|
183 | {
|
---|
184 | // print out cfitsio error messages and exit program
|
---|
185 | // print error report
|
---|
186 | fits_report_error(stderr, status);
|
---|
187 | cout << " erreur:: " << texte << endl;
|
---|
188 | throw IOExc("FitsFile:: error FITSIO status");
|
---|
189 | }
|
---|
190 | void FitsFile::printerrorAndContinue(int& status, char* texte)
|
---|
191 | //*****************************************************/
|
---|
192 | //* Print out cfitsio error messages and exit program */
|
---|
193 | //*****************************************************/
|
---|
194 | {
|
---|
195 | // print out cfitsio error messages and exit program
|
---|
196 | // print error report
|
---|
197 | fits_report_error(stderr, status);
|
---|
198 | cout << " erreur:: " << texte << endl;
|
---|
199 | // throw IOExc("FitsFile:: error FITSIO status");
|
---|
200 | }
|
---|
201 |
|
---|
202 | void FitsFile::ResetStatus(int& status)
|
---|
203 | {
|
---|
204 | fits_status_ = status;
|
---|
205 | status = 0;
|
---|
206 | fits_clear_errmsg();
|
---|
207 | }
|
---|
208 |
|
---|
209 | string FitsFile::GetErrStatus(int status)
|
---|
210 | {
|
---|
211 | char text[31];
|
---|
212 | fits_get_errstatus(status, text);
|
---|
213 | return string(text);
|
---|
214 | }
|
---|
215 |
|
---|
216 | /*!
|
---|
217 | \class SOPHYA::FitsInFile
|
---|
218 | \ingroup FitsIOServer
|
---|
219 | class for reading SOPHYA objects from FITS Format Files (uses cfitsio lib)
|
---|
220 | */
|
---|
221 |
|
---|
222 | FitsInFile::FitsInFile()
|
---|
223 | {
|
---|
224 | InitNull();
|
---|
225 | }
|
---|
226 |
|
---|
227 | FitsInFile::FitsInFile(string const & flnm)
|
---|
228 | {
|
---|
229 | InitNull();
|
---|
230 | int status = 0;
|
---|
231 | fits_open_file(&fptr_,flnm.c_str(),READONLY,&status);
|
---|
232 | if( status ) printerror( status );
|
---|
233 | }
|
---|
234 |
|
---|
235 | FitsInFile::FitsInFile(const char * flnm)
|
---|
236 | {
|
---|
237 | InitNull();
|
---|
238 | int status = 0;
|
---|
239 | fits_open_file(&fptr_,flnm,READONLY,&status);
|
---|
240 | if( status ) printerror( status );
|
---|
241 | }
|
---|
242 |
|
---|
243 |
|
---|
244 | void FitsInFile::InitNull()
|
---|
245 | {
|
---|
246 | imageDataType_ = FitsDataType_NULL;
|
---|
247 | naxis_ = 0;
|
---|
248 | nbData_ = 0;
|
---|
249 | nrows_ = 0;
|
---|
250 | nbcols_ = 0;
|
---|
251 | naxisn_.clear();
|
---|
252 | repeat_.clear();
|
---|
253 | noms_.clear();
|
---|
254 | taille_des_chaines_.clear();
|
---|
255 | dvl_.Clear();
|
---|
256 |
|
---|
257 | }
|
---|
258 |
|
---|
259 | //////////////////////////////////////////////////////////
|
---|
260 | // methods with general purpose
|
---|
261 | /////////////////////////////////////////////////////////
|
---|
262 |
|
---|
263 | int FitsInFile::NbBlocks(char flnm[])
|
---|
264 | {
|
---|
265 | int status = 0;
|
---|
266 | int nbhdu = 0;
|
---|
267 | fitsfile* fileptr;
|
---|
268 | fits_open_file(&fileptr,flnm,READONLY,&status);
|
---|
269 | if( status ) printerror( status, "NbBlocks: erreur ouverture fichier" );
|
---|
270 | fits_get_num_hdus(fileptr, &nbhdu, &status);
|
---|
271 | fits_close_file(fileptr,&status);
|
---|
272 | return nbhdu;
|
---|
273 | }
|
---|
274 | int FitsInFile::NbBlocks()
|
---|
275 | {
|
---|
276 | int status = 0;
|
---|
277 | int nbhdu = 0;
|
---|
278 | fits_get_num_hdus(fptr_, &nbhdu, &status);
|
---|
279 | return nbhdu;
|
---|
280 | }
|
---|
281 |
|
---|
282 | void FitsInFile::GetBlockType(char flnm[], int hdunum, FitsExtensionType& typeOfExtension, int& naxis, vector<int>& naxisn, FitsDataType& dataType, DVList& dvl )
|
---|
283 | {
|
---|
284 | int status = 0;
|
---|
285 | fitsfile* fileptr;
|
---|
286 | fits_open_file(&fileptr,flnm,READONLY,&status);
|
---|
287 | if( status ) printerror( status, "GetBlockType: erreur ouverture fichier" );
|
---|
288 | // move to the specified HDU number
|
---|
289 | int hdutype = 0;
|
---|
290 | fits_movabs_hdu(fileptr,hdunum,&hdutype,&status);
|
---|
291 | if( status ) printerror( status,"GetBlockType: erreur movabs");
|
---|
292 | if(hdutype == IMAGE_HDU)
|
---|
293 | {
|
---|
294 | typeOfExtension = FitsExtensionType_IMAGE;
|
---|
295 | GetImageParameters (fileptr, dataType, naxis, naxisn);
|
---|
296 | }
|
---|
297 | else
|
---|
298 | if(hdutype == ASCII_TBL || hdutype == BINARY_TBL)
|
---|
299 | {
|
---|
300 | int nrows = 0;
|
---|
301 | vector<string> noms;
|
---|
302 | vector<FitsDataType> types;
|
---|
303 | vector<int> taille_des_chaines;
|
---|
304 | GetBinTabParameters(fileptr, naxis, nrows, naxisn, noms, types, taille_des_chaines);
|
---|
305 | int k;
|
---|
306 | for (k=0; k< naxisn.size(); k++) naxisn[k] *= nrows;
|
---|
307 | if(hdutype == ASCII_TBL)
|
---|
308 | {
|
---|
309 | typeOfExtension = FitsExtensionType_ASCII_TBL;
|
---|
310 | dataType = FitsDataType_ASCII;
|
---|
311 | }
|
---|
312 | else
|
---|
313 | {
|
---|
314 | typeOfExtension = FitsExtensionType_BINARY_TBL;
|
---|
315 | dataType = types[0];
|
---|
316 | }
|
---|
317 | }
|
---|
318 | else
|
---|
319 | {
|
---|
320 | cout << " hdutype= " << hdutype << endl;
|
---|
321 | throw IOExc("FitsFile::GetBlockType: this HDU type is unknown");
|
---|
322 | }
|
---|
323 |
|
---|
324 | KeywordsIntoDVList(fileptr, dvl, hdunum);
|
---|
325 | fits_close_file(fileptr,&status);
|
---|
326 | }
|
---|
327 |
|
---|
328 |
|
---|
329 | void FitsInFile::ReadHeader(int hdunum)
|
---|
330 | {
|
---|
331 | // InitNull();
|
---|
332 | int status = 0;
|
---|
333 | if (hdunum<0)
|
---|
334 | {
|
---|
335 | throw PException(" FitsInFile::ReadHeader : hdu number must be not negative");
|
---|
336 | }
|
---|
337 | if (hdunum != 0 ) hdunum_ = hdunum;
|
---|
338 |
|
---|
339 | // si le numero de header non precise
|
---|
340 | else
|
---|
341 | {
|
---|
342 | // si c'est le premier objet a lire
|
---|
343 | if (hdunum_ == 0)
|
---|
344 | {
|
---|
345 | // on calcule le numero de header a lire
|
---|
346 | if (imageOnPrimary_ == true ) hdunum_ = 1;
|
---|
347 | else hdunum_ = 2;
|
---|
348 | }
|
---|
349 | // sinon objet suivant
|
---|
350 | else hdunum_++;
|
---|
351 | }
|
---|
352 | getHeader();
|
---|
353 | if ( hdutype_ == FitsExtensionType_NULL )
|
---|
354 | {
|
---|
355 | if (hdunum == 0 && hdunum_ == 1)
|
---|
356 | {
|
---|
357 | hdunum_++;
|
---|
358 | getHeader();
|
---|
359 | }
|
---|
360 | else
|
---|
361 | {
|
---|
362 | cout << " WARNING (FitsInFile::ReadHeader) : no SOPHYA object on HDU number : " << hdunum_ << endl;
|
---|
363 | }
|
---|
364 | }
|
---|
365 | if ( hdutype_ == FitsExtensionType_EOF )
|
---|
366 | {
|
---|
367 | throw PException("FitsFile::ReadHeader, attempt to read through EOF");
|
---|
368 | }
|
---|
369 | }
|
---|
370 |
|
---|
371 | string FitsInFile::getStringKeyword(int hdunum, string keyw, int& retStatus)
|
---|
372 | {
|
---|
373 | string s;
|
---|
374 | retStatus = 0;
|
---|
375 | int status = 0;
|
---|
376 | if (hdunum != hdunum_ )
|
---|
377 | {
|
---|
378 | int hdutype;
|
---|
379 | fits_movabs_hdu(fptr_,hdunum,&hdutype,&status);
|
---|
380 | }
|
---|
381 |
|
---|
382 | char value[FLEN_VALUE];
|
---|
383 | char* keyname= const_cast<char*>(keyw.c_str());
|
---|
384 | fits_read_key_str(fptr_,keyname,value,NULL,&status);
|
---|
385 | if (status == 0)
|
---|
386 | s = string(value);
|
---|
387 | else retStatus = status;
|
---|
388 | if (hdunum != hdunum_ )
|
---|
389 | {
|
---|
390 | int hdutype;
|
---|
391 | if (hdunum_ != 0)
|
---|
392 | fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
|
---|
393 | else fits_movabs_hdu(fptr_,1,&hdutype,&status);
|
---|
394 |
|
---|
395 | }
|
---|
396 | return s;
|
---|
397 | }
|
---|
398 | bool FitsInFile::hasKeyword(int hdunum, string keyw)
|
---|
399 | {
|
---|
400 | bool has=false;
|
---|
401 | int status = 0;
|
---|
402 | if (hdunum != hdunum_ )
|
---|
403 | {
|
---|
404 | int hdutype;
|
---|
405 | fits_movabs_hdu(fptr_,hdunum,&hdutype,&status);
|
---|
406 | }
|
---|
407 |
|
---|
408 | char value[FLEN_VALUE];
|
---|
409 | char* keyname= const_cast<char*>(keyw.c_str());
|
---|
410 | fits_read_keyword(fptr_,keyname,value,NULL,&status);
|
---|
411 | if (status == 0)
|
---|
412 | has = true;
|
---|
413 | else
|
---|
414 | if (status == KEY_NO_EXIST ) status =0;
|
---|
415 | else fits_report_error(stderr,status);
|
---|
416 | if (hdunum != hdunum_ )
|
---|
417 | {
|
---|
418 | int hdutype;
|
---|
419 | if (hdunum_ != 0)
|
---|
420 | fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
|
---|
421 | else fits_movabs_hdu(fptr_,1,&hdutype,&status);
|
---|
422 |
|
---|
423 | }
|
---|
424 | return has;
|
---|
425 | }
|
---|
426 |
|
---|
427 | void FitsInFile::GetImageParameters (fitsfile* fileptr,FitsDataType& dataType,int& naxis,vector<int>& naxisn)
|
---|
428 | {
|
---|
429 | int hdunum=0;
|
---|
430 | // cout << " Reading a FITS image in HDU : " << fits_get_hdu_num(fileptr,&hdunum) << endl;
|
---|
431 | int status= 0;
|
---|
432 |
|
---|
433 | // bits per pixels
|
---|
434 | int bitpix=0;
|
---|
435 | fits_read_key(fileptr,TINT,"BITPIX",&bitpix,NULL,&status);
|
---|
436 | if( status ) printerror( status );
|
---|
437 | if(bitpix == DOUBLE_IMG) dataType = FitsDataType_double;
|
---|
438 | else if(bitpix == FLOAT_IMG) dataType = FitsDataType_float;
|
---|
439 | else if(bitpix == LONG_IMG || bitpix == SHORT_IMG ) dataType = FitsDataType_int;
|
---|
440 | else if (bitpix == BYTE_IMG) dataType = FitsDataType_char;
|
---|
441 | else
|
---|
442 | {
|
---|
443 | cout << " bitpix= " << bitpix << endl;
|
---|
444 | throw PException(" FitsFile::GetImageParameters : unsupported FITS data type");
|
---|
445 | }
|
---|
446 |
|
---|
447 | // number of dimensions in the FITS array
|
---|
448 | naxis= 0;
|
---|
449 | fits_read_key(fileptr,TINT,"NAXIS",&naxis,NULL,&status);
|
---|
450 | if( status ) printerror( status );
|
---|
451 | // read the NAXISn keywords to get image size
|
---|
452 | long* naxes = new long[naxis] ;
|
---|
453 | int nfound;
|
---|
454 | fits_read_keys_lng(fileptr,"NAXIS",1,naxis,naxes,&nfound,&status);
|
---|
455 | if( status ) printerror( status );
|
---|
456 | if (nfound != naxis )
|
---|
457 | cout << " WARNING : " << nfound << " axes found, expected naxis= " << naxis << endl;
|
---|
458 | int k;
|
---|
459 | for (k=0; k<naxis; k++)
|
---|
460 | {
|
---|
461 | naxisn.push_back( (int)naxes[k] );
|
---|
462 | }
|
---|
463 | delete [] naxes;
|
---|
464 | }
|
---|
465 |
|
---|
466 |
|
---|
467 |
|
---|
468 |
|
---|
469 | /*! \fn DVList SOPHYA::FitsInFile::DVListFromPrimaryHeader() const
|
---|
470 |
|
---|
471 | \return the keywords of primary header in a DVList
|
---|
472 |
|
---|
473 | */
|
---|
474 | DVList FitsInFile::DVListFromPrimaryHeader() const
|
---|
475 | {
|
---|
476 | int status;
|
---|
477 | DVList dvl;
|
---|
478 | KeywordsIntoDVList(fptr_, dvl, 1);
|
---|
479 | int hdutype = 0;
|
---|
480 | if (hdunum_ > 0) fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
|
---|
481 | return dvl;
|
---|
482 | }
|
---|
483 |
|
---|
484 | void FitsInFile::getHeader()
|
---|
485 | {
|
---|
486 | // si hdunum_ > 1 lit le header correspondant
|
---|
487 | // si hdunum_ = 1 se positionne au (et lit le) premier header qui
|
---|
488 | // contient reellement un objet
|
---|
489 | int status=0;
|
---|
490 | if (hdunum_ < 1) throw PException(" attempt to read hdunum < 1");
|
---|
491 | InitNull();
|
---|
492 | if (hdunum_ == 1)
|
---|
493 | {
|
---|
494 | // presence of image ?
|
---|
495 | int naxis= 0;
|
---|
496 | fits_read_key(fptr_,TINT,"NAXIS",&naxis,NULL,&status);
|
---|
497 | if( status ) printerror( status );
|
---|
498 | if (naxis > 0 ) // there is an image
|
---|
499 | {
|
---|
500 | hdutype_ = FitsExtensionType_IMAGE;
|
---|
501 | GetImageParameters (fptr_, imageDataType_, naxis_, naxisn_);
|
---|
502 | nbData_ = 1;
|
---|
503 | int k;
|
---|
504 | for (k=0; k<naxis_; k++) if (naxisn_[k] > 0) nbData_ *= naxisn_[k];
|
---|
505 | KeywordsIntoDVList(fptr_, dvl_,hdunum_);
|
---|
506 | }
|
---|
507 | else
|
---|
508 | {
|
---|
509 | hdutype_ = FitsExtensionType_NULL;
|
---|
510 | KeywordsIntoDVList(fptr_, dvl_,hdunum_);
|
---|
511 | }
|
---|
512 | }
|
---|
513 | else
|
---|
514 | {
|
---|
515 | int hdutype;
|
---|
516 | fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
|
---|
517 |
|
---|
518 | if( status )
|
---|
519 | {
|
---|
520 | if (status == END_OF_FILE)
|
---|
521 | {
|
---|
522 | hdutype_= FitsExtensionType_EOF;
|
---|
523 | status =0;
|
---|
524 | return;
|
---|
525 | }
|
---|
526 | else
|
---|
527 | {
|
---|
528 | cout << "WARNING (FitsInFile::getHeader) : error during movabs" << endl;
|
---|
529 | hdutype_= FitsExtensionType_ERROR;
|
---|
530 | status =0;
|
---|
531 | return;
|
---|
532 | }
|
---|
533 | // printerror( status,":FitsInFile::getHeader : erreur movabs");
|
---|
534 | }
|
---|
535 | if(hdutype == IMAGE_HDU)
|
---|
536 | {
|
---|
537 | hdutype_= FitsExtensionType_IMAGE;
|
---|
538 | GetImageParameters (fptr_, imageDataType_, naxis_, naxisn_);
|
---|
539 | nbData_ = 1;
|
---|
540 | int k;
|
---|
541 | for (k=0; k<naxis_; k++) if (naxisn_[k] > 0) nbData_ *= naxisn_[k];
|
---|
542 | KeywordsIntoDVList(fptr_, dvl_,hdunum_);
|
---|
543 | }
|
---|
544 | else if(hdutype == ASCII_TBL)
|
---|
545 | {
|
---|
546 | hdutype_= FitsExtensionType_ASCII_TBL;
|
---|
547 | GetBinTabParameters(fptr_,nbcols_, nrows_,repeat_, noms_, types_, taille_des_chaines_);
|
---|
548 | KeywordsIntoDVList(fptr_, dvl_, hdunum_);
|
---|
549 | }
|
---|
550 | else if(hdutype == BINARY_TBL)
|
---|
551 | {
|
---|
552 | hdutype_= FitsExtensionType_BINARY_TBL;
|
---|
553 | GetBinTabParameters(fptr_,nbcols_, nrows_,repeat_, noms_, types_, taille_des_chaines_);
|
---|
554 | KeywordsIntoDVList(fptr_, dvl_, hdunum_);
|
---|
555 | }
|
---|
556 | else
|
---|
557 | {
|
---|
558 | hdutype_= FitsExtensionType_NULL;
|
---|
559 | KeywordsIntoDVList(fptr_, dvl_, hdunum_);
|
---|
560 | }
|
---|
561 | }
|
---|
562 | }
|
---|
563 |
|
---|
564 |
|
---|
565 | void FitsInFile::moveToFollowingHeader()
|
---|
566 | {
|
---|
567 | int status = 0;
|
---|
568 | hdunum_++;
|
---|
569 | getHeader();
|
---|
570 | if ( hdutype_ == FitsExtensionType_NULL )
|
---|
571 | {
|
---|
572 | cout << " WARNING (FitsInFile::ReadHeader) : no SOPHYA object on HDU number : " << hdunum_ << endl;
|
---|
573 |
|
---|
574 | }
|
---|
575 | }
|
---|
576 |
|
---|
577 |
|
---|
578 |
|
---|
579 |
|
---|
580 |
|
---|
581 | /*! \fn int SOPHYA::FitsInFile::NbColsFromFits() const
|
---|
582 | \return number of columns (return 1 if IMAGE)
|
---|
583 | */
|
---|
584 | int FitsInFile::NbColsFromFits() const
|
---|
585 | {
|
---|
586 | if(hdutype_ == FitsExtensionType_BINARY_TBL) return nbcols_;
|
---|
587 | else
|
---|
588 | if(hdutype_ == FitsExtensionType_ASCII_TBL || hdutype_ == FitsExtensionType_IMAGE) return 1;
|
---|
589 | else
|
---|
590 | {
|
---|
591 | cout << " hdutype= " << (int) hdutype_ << endl;
|
---|
592 | throw PException("FitsFile::NbColsFromFits, HDU not supported");
|
---|
593 | }
|
---|
594 | }
|
---|
595 |
|
---|
596 | /*! \fn int SOPHYA::FitsInFile::NentriesFromFits(int nocol) const
|
---|
597 | \return number of data in the current IMAGE extension on FITS file, or number
|
---|
598 | of data of column number 'nocol' of the current BINTABLE extension
|
---|
599 | */
|
---|
600 | int FitsInFile::NentriesFromFits(int nocol) const
|
---|
601 | {
|
---|
602 | if(hdutype_ == FitsExtensionType_BINARY_TBL) return nrows_*repeat_[nocol];
|
---|
603 | else
|
---|
604 | if(hdutype_ == FitsExtensionType_ASCII_TBL) return nrows_;
|
---|
605 | else
|
---|
606 | if(hdutype_ == FitsExtensionType_IMAGE) return nbData_;
|
---|
607 | else
|
---|
608 | {
|
---|
609 | cout << "hdutype= " << (int) hdutype_ << endl;
|
---|
610 | throw PException("FitsFile::NentriesFromFits, this HDU is not supported");
|
---|
611 | }
|
---|
612 | }
|
---|
613 |
|
---|
614 | /*! \fn char SOPHYA::FitsInFile::ColTypeFromFits(int nocol) const
|
---|
615 |
|
---|
616 | return a character denoting data type of column number 'nocol' in a BINTABLE :
|
---|
617 |
|
---|
618 | D : double
|
---|
619 |
|
---|
620 | E : float
|
---|
621 |
|
---|
622 | I : integer
|
---|
623 |
|
---|
624 | S : character string
|
---|
625 |
|
---|
626 | */
|
---|
627 |
|
---|
628 | FitsFile::FitsDataType FitsInFile::ColTypeFromFits(int nocol) const
|
---|
629 | {
|
---|
630 | if(hdutype_ != FitsExtensionType_ASCII_TBL && hdutype_ != FitsExtensionType_BINARY_TBL)
|
---|
631 | {
|
---|
632 | throw IOExc("FitsFile::TypeFromFits, this HDU is not an ASCII table nor a binary table");
|
---|
633 | }
|
---|
634 | return types_[nocol];
|
---|
635 | }
|
---|
636 |
|
---|
637 |
|
---|
638 | /*! \fn string SOPHYA::FitsInFile::ColNameFromFits(int nocol) const
|
---|
639 |
|
---|
640 | \return name of the column number 'nocol' of the current BINTABLE extension
|
---|
641 | */
|
---|
642 |
|
---|
643 | string FitsInFile::ColNameFromFits(int nocol) const
|
---|
644 | {
|
---|
645 | if(hdutype_ != FitsExtensionType_ASCII_TBL && hdutype_ != FitsExtensionType_BINARY_TBL)
|
---|
646 | {
|
---|
647 | throw IOExc("FitsFile::TypeFromFits, this HDU is not an ASCII table nor a binary table");
|
---|
648 | }
|
---|
649 | return noms_[nocol];
|
---|
650 | }
|
---|
651 |
|
---|
652 | /*! \fn int DSOPHYA::FitsInFile::ColStringLengthFromFits(int nocol) const
|
---|
653 |
|
---|
654 | \return number of characters of each data for the column number 'nocol' (if char* typed) of the current BINTABLE extension
|
---|
655 | */
|
---|
656 |
|
---|
657 | int FitsInFile::ColStringLengthFromFits(int nocol) const
|
---|
658 | {
|
---|
659 | if(hdutype_ != FitsExtensionType_ASCII_TBL && hdutype_ != FitsExtensionType_BINARY_TBL)
|
---|
660 | {
|
---|
661 | throw IOExc("FitsFile::TypeFromFits, this HDU is not an ASCII table nor a binary table");
|
---|
662 | }
|
---|
663 | int index=-1;
|
---|
664 | int k;
|
---|
665 | for (k=0; k<=nocol; k++)
|
---|
666 | {
|
---|
667 | if (types_[k] == FitsDataType_char) index++;
|
---|
668 | }
|
---|
669 | return taille_des_chaines_[index];
|
---|
670 | }
|
---|
671 |
|
---|
672 |
|
---|
673 |
|
---|
674 | /*! \fn void SOPHYA::FitsInFile::GetBinTabLine(int NoLine, double* ddata, float* fdata, int* idata, char ** cdata)
|
---|
675 |
|
---|
676 | Get the NoLine-th 'line' from the current BINTABLE extension on FITS file,
|
---|
677 | */
|
---|
678 |
|
---|
679 | void FitsInFile::GetBinTabLine(int NoLine, double* ddata, float* fdata, int* idata, char ** cdata)
|
---|
680 | {
|
---|
681 | int status= 0;
|
---|
682 | int anull;
|
---|
683 | double dnull= 0.;
|
---|
684 | float fnull= 0.;
|
---|
685 | int inull= 0;
|
---|
686 | char* cnull= "";
|
---|
687 | int dcount = 0.;
|
---|
688 | int fcount = 0.;
|
---|
689 | int icount = 0;
|
---|
690 | int ccount =0;
|
---|
691 | int ncol;
|
---|
692 | long nels=1;
|
---|
693 | int ligneAsolue = NoLine+1;
|
---|
694 | for (ncol=0; ncol<nbcols_; ncol++)
|
---|
695 | {
|
---|
696 | int repetition =repeat_[ncol];
|
---|
697 | int ligneALire = ligneAsolue/repetition;
|
---|
698 | int premierElement = ligneAsolue-ligneALire*repetition;
|
---|
699 | if (premierElement != 0 )
|
---|
700 | {
|
---|
701 | ligneALire++;
|
---|
702 | }
|
---|
703 | else premierElement = repetition;
|
---|
704 |
|
---|
705 | switch (types_[ncol])
|
---|
706 | {
|
---|
707 | case FitsDataType_double :
|
---|
708 | {
|
---|
709 | fits_read_col(fptr_,TDOUBLE,ncol+1,ligneALire,premierElement,1,&dnull,&ddata[dcount++],&anull,&status);
|
---|
710 | break;
|
---|
711 | }
|
---|
712 | case FitsDataType_float :
|
---|
713 | fits_read_col(fptr_,TFLOAT,ncol+1,ligneALire,premierElement,1,&fnull,&fdata[fcount++],&anull,&status);
|
---|
714 | break;
|
---|
715 | case FitsDataType_int :
|
---|
716 | fits_read_col(fptr_,TINT,ncol+1,ligneALire,premierElement,1,&inull,&idata[icount++],
|
---|
717 | &anull,&status);
|
---|
718 | break;
|
---|
719 | case FitsDataType_long :
|
---|
720 | fits_read_col(fptr_,TLONG,ncol+1,ligneALire,premierElement,1,&inull,&idata[icount++], &anull,&status);
|
---|
721 | case FitsDataType_byte :
|
---|
722 | {
|
---|
723 | unsigned char uschar = 0;
|
---|
724 | fits_read_col(fptr_,TBYTE,ncol+1,ligneALire,premierElement,1,&inull,&uschar, &anull,&status);
|
---|
725 | idata[icount++] = (int)uschar;
|
---|
726 | }
|
---|
727 | break;
|
---|
728 | case FitsDataType_char :
|
---|
729 | fits_read_col(fptr_,TSTRING,ncol+1,ligneALire,premierElement,1,cnull,&cdata[ccount++],&anull,&status);
|
---|
730 | break;
|
---|
731 | }
|
---|
732 | if (status)
|
---|
733 | {
|
---|
734 | ResetStatus(status);
|
---|
735 | break;
|
---|
736 | }
|
---|
737 | }
|
---|
738 | }
|
---|
739 |
|
---|
740 | /*! \fn void SOPHYA::FitsInFile::GetBinTabLine(long NoLine, BnTblLine& ligne)
|
---|
741 | Get the NoLine-th 'line' from the current BINTABLE extension on FITS file,
|
---|
742 | */
|
---|
743 | void FitsInFile::GetBinTabLine(long NoLine, BnTblLine& ligne)
|
---|
744 | {
|
---|
745 | int status= 0;
|
---|
746 | int anull;
|
---|
747 | double dnull= 0.;
|
---|
748 | float fnull= 0.;
|
---|
749 | int inull= 0;
|
---|
750 | char* cnull= "";
|
---|
751 | int dcount = 0.;
|
---|
752 | int fcount = 0.;
|
---|
753 | int icount = 0;
|
---|
754 | int ccount =0;
|
---|
755 | int ncol;
|
---|
756 | long nels=1;
|
---|
757 | for (ncol=0; ncol<nbcols_; ncol++)
|
---|
758 | {
|
---|
759 | switch (types_[ncol])
|
---|
760 | {
|
---|
761 | case FitsDataType_double :
|
---|
762 | fits_read_col(fptr_,TDOUBLE,ncol+1,NoLine+1,1,1,&dnull,&ligne.ddata_[dcount++],&anull,&status);
|
---|
763 | break;
|
---|
764 | case FitsDataType_float :
|
---|
765 | fits_read_col(fptr_,TFLOAT,ncol+1,NoLine+1,1,1,&fnull,&ligne.fdata_[fcount++],&anull,&status);
|
---|
766 | break;
|
---|
767 | case FitsDataType_int :
|
---|
768 | fits_read_col(fptr_,TINT,ncol+1,NoLine+1,1,1,&inull,&ligne.idata_[icount++], &anull,&status);
|
---|
769 | break;
|
---|
770 | case FitsDataType_long :
|
---|
771 | fits_read_col(fptr_,TLONG,ncol+1,NoLine+1,1,1,&inull,&ligne.ldata_[icount++], &anull,&status);
|
---|
772 | break;
|
---|
773 | case FitsDataType_byte :
|
---|
774 | fits_read_col(fptr_,TBYTE,ncol+1,NoLine+1,1,1,&inull,&ligne.bdata_[icount++], &anull,&status);
|
---|
775 | break;
|
---|
776 | case FitsDataType_char :
|
---|
777 | char* chaine = new char[taille_des_chaines_[ccount]];
|
---|
778 | fits_read_col(fptr_,TSTRING,ncol+1,NoLine+1,1,1,cnull,&chaine,&anull,&status);
|
---|
779 | ligne.cdata_[ccount++] = string(chaine);
|
---|
780 | break;
|
---|
781 | }
|
---|
782 | if (status)
|
---|
783 | {
|
---|
784 | ResetStatus(status);
|
---|
785 | break;
|
---|
786 | }
|
---|
787 | }
|
---|
788 | }
|
---|
789 |
|
---|
790 | /*! \fn void SOPHYA::FitsInFile::GetBinTabLine(int NoLine, float* fdata)
|
---|
791 |
|
---|
792 | Get the NoLine-th float 'line' from the current BINTABLE extension on FITS file,
|
---|
793 | */
|
---|
794 | void FitsInFile::GetBinTabLine(int NoLine, float* fdata)
|
---|
795 | {
|
---|
796 | int status= 0;
|
---|
797 | int anull;
|
---|
798 | float fnull= 0.;
|
---|
799 | long nels=1;
|
---|
800 | int ncol;
|
---|
801 | for (ncol=0; ncol<nbcols_; ncol++)
|
---|
802 | {
|
---|
803 | fits_read_col(fptr_,TFLOAT,ncol+1,NoLine+1,1,1,&fnull,&fdata[ncol],&anull,&status);
|
---|
804 | if (status)
|
---|
805 | {
|
---|
806 | ResetStatus(status);
|
---|
807 | break;
|
---|
808 | }
|
---|
809 | }
|
---|
810 | }
|
---|
811 |
|
---|
812 |
|
---|
813 | /*! \fn void SPOPHYA::FitsInFile::GetBinTabFCol(double* valeurs,int nentries, int NoCol) const
|
---|
814 |
|
---|
815 | fill the array 'valeurs' with double data from the current BINTABLE extension on FITS file, from column number 'NoCol'
|
---|
816 |
|
---|
817 | \param <nentries> number of data to be read
|
---|
818 | */
|
---|
819 | void FitsInFile::GetBinTabFCol(r_8* valeurs,int nentries, int NoCol) const
|
---|
820 | {
|
---|
821 | int status= 0;
|
---|
822 | int DTYPE;
|
---|
823 | long repeat,width;
|
---|
824 | fits_get_coltype(fptr_, NoCol+1,&DTYPE,&repeat,&width,&status);
|
---|
825 | if( DTYPE != TDOUBLE)
|
---|
826 | {
|
---|
827 | if (DTYPE == TFLOAT) cout << " WARNING: reading double from float : conversion will be made by fitsio library" << endl;
|
---|
828 | else
|
---|
829 | throw IOExc("FitsFile::GetBinTabFCol, tentative de lecture non double");
|
---|
830 | }
|
---|
831 | long nels=nentries;
|
---|
832 | int anull;
|
---|
833 | // no checking for undefined pixels
|
---|
834 | double dnull= 0.;
|
---|
835 | // fits_read_key(fptr_,TDOUBLE,"BAD_DATA",&dnull,NULL,&status);
|
---|
836 | // if (status != 0)
|
---|
837 | // {
|
---|
838 | // dnull = -1.6375e30; // default value
|
---|
839 | // status = 0;
|
---|
840 | // }
|
---|
841 | // if (nentries != nrows_*repeat)
|
---|
842 | // {
|
---|
843 | // cout << " found " << nentries << " pixels, expected: " << nrows_*repeat << endl;
|
---|
844 | // throw PException(" FitsFile:::GetBinTabFCol ");
|
---|
845 | // }
|
---|
846 | fits_read_col(fptr_,TDOUBLE,NoCol+1,1,1,nels,&dnull,valeurs,
|
---|
847 | &anull,&status);
|
---|
848 | if( status )
|
---|
849 | {
|
---|
850 | printerrorAndContinue( status,"erreur lecture de colonne" );
|
---|
851 | }
|
---|
852 | }
|
---|
853 |
|
---|
854 | /*! \fn void SOPHYA::FitsInFile::GetBinTabFCol(float* valeurs,int nentries, int NoCol) const
|
---|
855 |
|
---|
856 | same as previous method with float data
|
---|
857 | */
|
---|
858 | void FitsInFile::GetBinTabFCol(r_4* valeurs,int nentries, int NoCol) const
|
---|
859 | {
|
---|
860 | int status= 0;
|
---|
861 | int DTYPE;
|
---|
862 | long repeat,width;
|
---|
863 | fits_get_coltype(fptr_, NoCol+1,&DTYPE,&repeat,&width,&status);
|
---|
864 | if( DTYPE != TFLOAT)
|
---|
865 | {
|
---|
866 | if (DTYPE == TDOUBLE) cout << " WARNING: reading float from double : conversion will be made by fitsio library" << endl;
|
---|
867 | else
|
---|
868 | throw IOExc("FitsFile::GetBinTabFCol, tentative de lecture non float");
|
---|
869 | }
|
---|
870 | long nels=nentries;
|
---|
871 | int anull;
|
---|
872 | // no checking for undefined pixels
|
---|
873 | float fnull= 0.;
|
---|
874 | // fits_read_key(fptr_,TFLOAT,"BAD_DATA",&fnull,NULL,&status);
|
---|
875 | // if (status != 0)
|
---|
876 | // {
|
---|
877 | // fnull = -1.6375e30; // default value
|
---|
878 | // status = 0;
|
---|
879 | // }
|
---|
880 | // if (nentries != nrows_*repeat)
|
---|
881 | // {
|
---|
882 | // cout << " found " << nentries << " pixels, expected: " << nrows_*repeat << endl;
|
---|
883 | // throw PException(" FitsFile:::GetBinTabFCol ");
|
---|
884 | // }
|
---|
885 | fits_read_col(fptr_,TFLOAT,NoCol+1,1,1,nels,&fnull,valeurs,
|
---|
886 | &anull,&status);
|
---|
887 | if( status ) printerrorAndContinue( status,"erreur lecture de colonne" );
|
---|
888 | }
|
---|
889 |
|
---|
890 | /*! \fn void SOPHYA::FitsInFile::GetBinTabFCol(int* valeurs,int nentries, int NoCol) const
|
---|
891 |
|
---|
892 | same as previous method with int data
|
---|
893 | */
|
---|
894 |
|
---|
895 | void FitsInFile::GetBinTabFCol(int_4* valeurs,int nentries, int NoCol) const
|
---|
896 | {
|
---|
897 | int status= 0;
|
---|
898 | int DTYPE;
|
---|
899 | long repeat,width;
|
---|
900 | fits_get_coltype(fptr_, NoCol+1,&DTYPE,&repeat,&width,&status);
|
---|
901 | cout << " en lecture fits trouve le type " << DTYPE << endl;
|
---|
902 | if( DTYPE != TLONG && DTYPE != TINT)
|
---|
903 | {
|
---|
904 | throw IOExc("FitsFile::GetBinTabFCol, probleme de lecture d'entiers");
|
---|
905 | }
|
---|
906 | long nels=nentries;
|
---|
907 | // no checking for undefined pixels
|
---|
908 | int anull;
|
---|
909 | int inull= 0;
|
---|
910 | // fits_read_key(fptr_,TINT,"BAD_DATA",&inull,NULL,&status);
|
---|
911 | // if (status != 0)
|
---|
912 | // {
|
---|
913 | // inull = -999999; // default value
|
---|
914 | // status = 0;
|
---|
915 | // }
|
---|
916 | // if (nentries != nrows_*repeat)
|
---|
917 | // {
|
---|
918 | // cout << " found " << nentries << " pixels, expected: " << nrows_*repeat << endl;
|
---|
919 | // throw PException(" FitsFile:::GetBinTabFCol ");
|
---|
920 | // }
|
---|
921 |
|
---|
922 |
|
---|
923 |
|
---|
924 | // voir commentaire dans putColToFits()
|
---|
925 | fits_read_col(fptr_,TINT,NoCol+1,1,1,nels,&inull,valeurs,
|
---|
926 | &anull,&status);
|
---|
927 | if( status ) printerrorAndContinue( status,"erreur lecture de colonne" );
|
---|
928 | }
|
---|
929 |
|
---|
930 | /*! \fn void SOPHYA::FitsInFile::GetBinTabFCol(char** valeurs, int nentries, int NoCol) const
|
---|
931 |
|
---|
932 | same as previous method with char* data
|
---|
933 | */
|
---|
934 |
|
---|
935 | void FitsInFile::GetBinTabFCol(char** valeurs, int nentries, int NoCol) const
|
---|
936 | {
|
---|
937 | int status= 0;
|
---|
938 | int DTYPE;
|
---|
939 | long repeat,width;
|
---|
940 | fits_get_coltype(fptr_, NoCol+1,&DTYPE,&repeat,&width,&status);
|
---|
941 | if( DTYPE != TSTRING && DTYPE != TBYTE)
|
---|
942 | {
|
---|
943 | throw IOExc("FitsFile::GetBinTabFCol, tentative de lecture non string");
|
---|
944 | }
|
---|
945 | long nels=nentries;
|
---|
946 | // no checking for undefined pixels
|
---|
947 | int anull;
|
---|
948 | char* cnull= "";
|
---|
949 | // if (nentries != nrows_*repeat/width)
|
---|
950 | // {
|
---|
951 | // cout << " found " << nentries << " pixels, expected: " << nrows_*repeat/width << endl;
|
---|
952 | // throw PException(" FitsFile:::GetBinTabFCol ");
|
---|
953 | // }
|
---|
954 | long frow=1;
|
---|
955 | long felem=1;
|
---|
956 | fits_read_col(fptr_,TSTRING,NoCol+1,frow,felem,nels,cnull,valeurs,
|
---|
957 | &anull,&status);
|
---|
958 | if( status ) printerrorAndContinue( status,"erreur lecture de colonne" );
|
---|
959 | }
|
---|
960 |
|
---|
961 | /*! \fn void SOPHYA::FitsInFile::GetSingleColumn(double* map, int nentries) const
|
---|
962 | fill the array 'map' with double data from the current extension on FITS file.
|
---|
963 | If the extension is BINTABLE, the first column is provided.
|
---|
964 |
|
---|
965 | \param <nentries> number of data to be read
|
---|
966 | */
|
---|
967 | void FitsInFile::GetSingleColumn(r_8* map, int nentries) const
|
---|
968 | {
|
---|
969 | int status = 0;
|
---|
970 | if(hdutype_ == FitsExtensionType_IMAGE)
|
---|
971 | {
|
---|
972 |
|
---|
973 | if(imageDataType_ != FitsDataType_double)
|
---|
974 | {
|
---|
975 | cout << " The data type on fits file is not double...";
|
---|
976 | cout << " Conversion to double achieved by cfitsio lib" << endl;
|
---|
977 | }
|
---|
978 |
|
---|
979 | // no checking for undefined pixels
|
---|
980 | int anull;
|
---|
981 | double dnull= 0.;
|
---|
982 |
|
---|
983 | long nels= nentries;
|
---|
984 | fits_read_img(fptr_,TDOUBLE,1,nels,&dnull,map,&anull,&status);
|
---|
985 | if( status ) printerror( status );
|
---|
986 | }
|
---|
987 | else
|
---|
988 | if(hdutype_ == FitsExtensionType_ASCII_TBL || hdutype_ == FitsExtensionType_BINARY_TBL)
|
---|
989 | {
|
---|
990 | GetBinTabFCol(map,nentries, 0);
|
---|
991 | }
|
---|
992 | else
|
---|
993 | {
|
---|
994 | cout << " hdutype= " << (int) hdutype_ << endl;
|
---|
995 | throw IOExc("FitsFile::GetSingleColumn, this HDU is unknown");
|
---|
996 | }
|
---|
997 | }
|
---|
998 |
|
---|
999 | /*! \fn void SOPHYA::FitsInFile::GetSingleColumn(float* map, int nentries) const
|
---|
1000 | same as above with float data
|
---|
1001 | */
|
---|
1002 | void FitsInFile::GetSingleColumn(r_4* map, int nentries) const
|
---|
1003 | {
|
---|
1004 | int status = 0;
|
---|
1005 | if(hdutype_ == FitsExtensionType_IMAGE)
|
---|
1006 | {
|
---|
1007 | if(imageDataType_ != FitsDataType_float)
|
---|
1008 | {
|
---|
1009 | cout << " The data type on fits file is not float ";
|
---|
1010 | cout << " Conversion to float achieved by cfitsio lib" << endl;
|
---|
1011 | }
|
---|
1012 | // no checking for undefined pixels
|
---|
1013 | int anull;
|
---|
1014 | float fnull= 0.;
|
---|
1015 |
|
---|
1016 | long nels= nentries;
|
---|
1017 | fits_read_img(fptr_,TFLOAT,1,nels,&fnull, map,&anull,&status);
|
---|
1018 | if( status ) printerror( status );
|
---|
1019 | }
|
---|
1020 | else
|
---|
1021 | if(hdutype_ == FitsExtensionType_ASCII_TBL || hdutype_ == FitsExtensionType_BINARY_TBL)
|
---|
1022 | {
|
---|
1023 | GetBinTabFCol(map,nentries, 0);
|
---|
1024 | }
|
---|
1025 | else
|
---|
1026 | {
|
---|
1027 | cout << " hdutype= " << (int) hdutype_ << endl;
|
---|
1028 | throw IOExc("FitsFile::GetSingleColumn this HDU is unknown");
|
---|
1029 | }
|
---|
1030 | }
|
---|
1031 |
|
---|
1032 | /*! \fn void SOPHYA::FitsInFile::GetSingleColumn( int* map, int nentries) const
|
---|
1033 | same as above with int data
|
---|
1034 | */
|
---|
1035 | void FitsInFile::GetSingleColumn( int_4* map, int nentries) const
|
---|
1036 | {
|
---|
1037 | int status = 0;
|
---|
1038 | if(hdutype_ == FitsExtensionType_IMAGE)
|
---|
1039 | {
|
---|
1040 | if(imageDataType_ != FitsDataType_int)
|
---|
1041 | {
|
---|
1042 | cout << " The data type on fits file is not int ";
|
---|
1043 | cout << " Conversion to float achieved by cfitsio lib" << endl;
|
---|
1044 | }
|
---|
1045 | // no checking for undefined pixels
|
---|
1046 | int anull;
|
---|
1047 | float fnull= 0.;
|
---|
1048 |
|
---|
1049 | long nels= nentries;
|
---|
1050 | fits_read_img(fptr_,TINT,1,nels,&fnull,map,&anull,&status);
|
---|
1051 | if( status ) printerror( status );
|
---|
1052 | }
|
---|
1053 | else
|
---|
1054 | if(hdutype_ == FitsExtensionType_ASCII_TBL || hdutype_ == FitsExtensionType_BINARY_TBL)
|
---|
1055 | {
|
---|
1056 | GetBinTabFCol(map,nentries, 0);
|
---|
1057 | }
|
---|
1058 | else
|
---|
1059 | {
|
---|
1060 | cout << " hdutype= " << (int) hdutype_ << endl;
|
---|
1061 | throw IOExc("FitsFile::GetSingleColumn this HDU is unknown");
|
---|
1062 | }
|
---|
1063 | }
|
---|
1064 |
|
---|
1065 | void FitsInFile::GetBinTabParameters(fitsfile* fileptr, int& nbcols, int& nrows,
|
---|
1066 | vector<int>& repeat,
|
---|
1067 | vector<string>& noms,
|
---|
1068 | vector<FitsDataType>& types,
|
---|
1069 | vector<int>& taille_des_chaines)
|
---|
1070 | {
|
---|
1071 | int status= 0;
|
---|
1072 | int hdunum=0;
|
---|
1073 | int hdutype=0;
|
---|
1074 | fits_get_hdu_num(fileptr,&hdunum);
|
---|
1075 | fits_get_hdu_type(fileptr, &hdutype, &status);
|
---|
1076 |
|
---|
1077 | if(hdutype != ASCII_TBL && hdutype != BINARY_TBL)
|
---|
1078 | {
|
---|
1079 | throw IOExc("FitsFile::GetBinTabParameters this HDU is not an ASCII table nor a binary table");
|
---|
1080 | }
|
---|
1081 | // if(hdutype == ASCII_TBL)
|
---|
1082 | // cout << " Reading a FITS ascii table in HDU : " << hdunum << endl;
|
---|
1083 | // if(hdutype == BINARY_TBL)
|
---|
1084 | // cout << " Reading a FITS binary table in HDU : " << hdunum << endl;
|
---|
1085 |
|
---|
1086 | // get the number of columns
|
---|
1087 | fits_get_num_cols(fileptr, &nbcols,&status);
|
---|
1088 | if( status ) printerror( status );
|
---|
1089 |
|
---|
1090 | // get the number of rows
|
---|
1091 | long naxis2= 0;
|
---|
1092 | fits_get_num_rows(fileptr,&naxis2,&status);
|
---|
1093 | if( status ) printerror( status );
|
---|
1094 | nrows = (int)naxis2;
|
---|
1095 |
|
---|
1096 | // get the datatype, names and the repeat count
|
---|
1097 | noms.clear();
|
---|
1098 | noms.reserve(nbcols);
|
---|
1099 | types.clear();
|
---|
1100 | types.reserve(nbcols);
|
---|
1101 | repeat.clear();
|
---|
1102 | repeat.reserve(nbcols);
|
---|
1103 | taille_des_chaines.clear();
|
---|
1104 | char **ttype = new char*[nbcols];
|
---|
1105 | int ii;
|
---|
1106 | //
|
---|
1107 | //
|
---|
1108 | for (ii=0; ii < nbcols; ii++) ttype[ii]=new char[FLEN_VALUE];
|
---|
1109 | int nfound;
|
---|
1110 | fits_read_keys_str(fileptr, "TTYPE",1,nbcols,ttype,&nfound, &status);
|
---|
1111 | if( status ) printerror( status,"erreur lecture des noms de colonne");
|
---|
1112 | int rept=0;
|
---|
1113 | if(hdutype == ASCII_TBL)
|
---|
1114 | {
|
---|
1115 | for(ii = 0; ii < nbcols; ii++)
|
---|
1116 | {
|
---|
1117 | int DTYPE;
|
---|
1118 | long width;
|
---|
1119 | long repete = 0;
|
---|
1120 | fits_get_coltype(fileptr,ii+1,&DTYPE,&repete,&width,&status);
|
---|
1121 | if( status ) printerror( status,"erreur lecture type de colonne");
|
---|
1122 | rept = repete;
|
---|
1123 | noms.push_back(string(ttype[ii]));
|
---|
1124 | switch (DTYPE)
|
---|
1125 | {
|
---|
1126 | case TDOUBLE :
|
---|
1127 | types.push_back(FitsDataType_double);
|
---|
1128 | break;
|
---|
1129 | case TFLOAT :
|
---|
1130 | types.push_back(FitsDataType_float);
|
---|
1131 | break;
|
---|
1132 | case TLONG :
|
---|
1133 | types.push_back(FitsDataType_long);
|
---|
1134 | break;
|
---|
1135 | case TSHORT :
|
---|
1136 | types.push_back(FitsDataType_int);
|
---|
1137 | break;
|
---|
1138 | case TSTRING :
|
---|
1139 | types.push_back(FitsDataType_char);
|
---|
1140 | taille_des_chaines.push_back(width);
|
---|
1141 | rept/=width;
|
---|
1142 | break;
|
---|
1143 | default :
|
---|
1144 | cout << " field " << ii+1 << " DTYPE= " << DTYPE << endl;
|
---|
1145 | throw IOExc("FitsFile::GetBinTabParameters, unsupported data type of field, for ASCII table");
|
---|
1146 | }
|
---|
1147 | repeat.push_back(rept);
|
---|
1148 | }
|
---|
1149 | }
|
---|
1150 | else
|
---|
1151 | {
|
---|
1152 | for(ii = 0; ii < nbcols; ii++)
|
---|
1153 | {
|
---|
1154 | int DTYPE;
|
---|
1155 | long width;
|
---|
1156 | long repete = 0;
|
---|
1157 | fits_get_coltype(fileptr,ii+1,&DTYPE,&repete,&width,&status);
|
---|
1158 | if( status ) printerror( status,"erreur lecture type de colonne");
|
---|
1159 | rept = repete;
|
---|
1160 | noms.push_back(string(ttype[ii]));
|
---|
1161 | switch (DTYPE)
|
---|
1162 | {
|
---|
1163 | case TDOUBLE :
|
---|
1164 | types.push_back(FitsDataType_double);
|
---|
1165 | break;
|
---|
1166 | case TFLOAT :
|
---|
1167 | types.push_back(FitsDataType_float);
|
---|
1168 | break;
|
---|
1169 | case TLONG :
|
---|
1170 | types.push_back(FitsDataType_long);
|
---|
1171 | break;
|
---|
1172 | case TINT :
|
---|
1173 | types.push_back(FitsDataType_int);
|
---|
1174 | break;
|
---|
1175 | case TSHORT :
|
---|
1176 | types.push_back(FitsDataType_int);
|
---|
1177 | break;
|
---|
1178 | case TSTRING :
|
---|
1179 | types.push_back(FitsDataType_char);
|
---|
1180 | taille_des_chaines.push_back(width);
|
---|
1181 | rept/=width;
|
---|
1182 | break;
|
---|
1183 | case TBYTE :
|
---|
1184 | types.push_back(FitsDataType_byte);
|
---|
1185 | break;
|
---|
1186 | default :
|
---|
1187 | cout << " field " << ii+1 << " DTYPE= " << DTYPE << endl;
|
---|
1188 | throw IOExc("FitsFile::GetBinTabParameters, unsupported data type of field, for BINTABLE");
|
---|
1189 | }
|
---|
1190 | repeat.push_back(rept);
|
---|
1191 | }
|
---|
1192 | }
|
---|
1193 | for (ii=0; ii < nbcols; ii++) delete [] ttype[ii];
|
---|
1194 | delete [] ttype;
|
---|
1195 | }
|
---|
1196 |
|
---|
1197 | void FitsInFile::KeywordsIntoDVList(fitsfile* fileptr, DVList& dvl, int hdunum)
|
---|
1198 | {
|
---|
1199 | int status = 0;
|
---|
1200 | int hdutype;
|
---|
1201 | fits_movabs_hdu(fileptr,hdunum,&hdutype,&status);
|
---|
1202 | if( status ) printerror( status,":KeywordsIntoDVList : erreur movabs");
|
---|
1203 | // get number of keywords
|
---|
1204 | int nkeys,keypos;
|
---|
1205 | fits_get_hdrpos(fileptr,&nkeys,&keypos,&status);
|
---|
1206 | if( status ) printerror( status );
|
---|
1207 |
|
---|
1208 | // put keywords in a DVList object
|
---|
1209 | char keyname[LEN_KEYWORD]= "";
|
---|
1210 | char strval[FLEN_VALUE]= "";
|
---|
1211 | char dtype;
|
---|
1212 | char card[FLEN_CARD];
|
---|
1213 | char *comkey = "COMMENT";
|
---|
1214 | char comment[FLEN_COMMENT];
|
---|
1215 |
|
---|
1216 | // shift with the number of mandatory keywords
|
---|
1217 | // int num= 8;
|
---|
1218 | int num= 0;
|
---|
1219 | // primary header
|
---|
1220 | if (hdunum == 1)
|
---|
1221 | {
|
---|
1222 | // read NAXIS
|
---|
1223 | int naxis=0;
|
---|
1224 | fits_read_key(fileptr,TINT,"NAXIS",&naxis,NULL,&status);
|
---|
1225 | // number of mandatory keywords
|
---|
1226 | num = naxis+3;
|
---|
1227 | }
|
---|
1228 | // extensions
|
---|
1229 | else
|
---|
1230 | {
|
---|
1231 | if (hdutype == IMAGE_HDU)
|
---|
1232 | {
|
---|
1233 | // read NAXIS
|
---|
1234 | int naxis=0;
|
---|
1235 | fits_read_key(fileptr,TINT,"NAXIS",&naxis,NULL,&status);
|
---|
1236 | // number of mandatory keywords
|
---|
1237 | num = naxis+5;
|
---|
1238 | }
|
---|
1239 | else
|
---|
1240 | if(hdutype == ASCII_TBL || hdutype == BINARY_TBL)
|
---|
1241 | {
|
---|
1242 | // number of mandatory keywords
|
---|
1243 | num = 8;
|
---|
1244 | }
|
---|
1245 | }
|
---|
1246 | int j;
|
---|
1247 | for(j = num+1; j <= nkeys; j++)
|
---|
1248 | {
|
---|
1249 | fits_read_keyn(fileptr,j,card,strval,NULL,&status);
|
---|
1250 | if(status) printerror(status);
|
---|
1251 |
|
---|
1252 | strncpy(keyname,card,LEN_KEYWORD-1);
|
---|
1253 | if(strncmp(keyname,comkey,LEN_KEYWORD-1) != 0 && strlen(keyname) != 0
|
---|
1254 | && strlen(strval) != 0)
|
---|
1255 | {
|
---|
1256 | fits_get_keytype(strval,&dtype,&status);
|
---|
1257 | if(status) printerror(status);
|
---|
1258 |
|
---|
1259 | strip(keyname, 'B',' ');
|
---|
1260 | strip(strval, 'B',' ');
|
---|
1261 | strip(strval, 'B','\'');
|
---|
1262 |
|
---|
1263 | switch( dtype )
|
---|
1264 | {
|
---|
1265 | case 'C':
|
---|
1266 | fits_read_key(fileptr,TSTRING,keyname,strval,comment,&status);
|
---|
1267 | dvl[keyname]= strval;
|
---|
1268 | dvl.SetComment(keyname, comment);
|
---|
1269 | break;
|
---|
1270 | case 'I':
|
---|
1271 | int ival;
|
---|
1272 | fits_read_key(fileptr,TINT,keyname,&ival,comment,&status);
|
---|
1273 | dvl[keyname]= (int_4) ival; // Portage mac DY
|
---|
1274 | dvl.SetComment(keyname, comment);
|
---|
1275 | break;
|
---|
1276 | case 'L':
|
---|
1277 | int ilog;
|
---|
1278 | fits_read_key(fileptr,TLOGICAL,keyname,&ilog,comment,&status);
|
---|
1279 | dvl[keyname]= (int_4) ilog;
|
---|
1280 | dvl.SetComment(keyname, comment);
|
---|
1281 | break;
|
---|
1282 | case 'F':
|
---|
1283 | double dval;
|
---|
1284 | fits_read_key(fileptr,TDOUBLE,keyname,&dval,comment,&status);
|
---|
1285 | dvl[keyname]= dval;
|
---|
1286 | dvl.SetComment(keyname, comment);
|
---|
1287 | break;
|
---|
1288 | }
|
---|
1289 |
|
---|
1290 | }
|
---|
1291 | }
|
---|
1292 | // dvl_.Print();
|
---|
1293 | }
|
---|
1294 |
|
---|
1295 |
|
---|
1296 | /*!
|
---|
1297 | \class SOPHYA::FitsOutFile
|
---|
1298 | \ingroup FitsIOServer
|
---|
1299 | Class for loading SOPHYA objects from FITS Format Files (uses cfitsio lib)
|
---|
1300 | */
|
---|
1301 |
|
---|
1302 | FitsOutFile::FitsOutFile()
|
---|
1303 | {
|
---|
1304 | InitNull();
|
---|
1305 | }
|
---|
1306 |
|
---|
1307 | /*! \fn SOPHYA::FitsOutFile::FitsOutFile(char flnm[], WriteMode wrm)
|
---|
1308 |
|
---|
1309 | \param <WriteMode> enum , WriteMode = clear -> if alreadyy exists, the file will be overwritten (else created) ; WriteMode = append -> further objects will be appended to the file if it exists (else : file created). WriteMode = unknown -> file created if does not exist, else : exception. (the last situation is the default)
|
---|
1310 |
|
---|
1311 | */
|
---|
1312 |
|
---|
1313 | FitsOutFile::FitsOutFile(string const & flnm, WriteMode wrm)
|
---|
1314 | {
|
---|
1315 | InitNull();
|
---|
1316 | openoutputfitsfile(flnm.c_str(), wrm);
|
---|
1317 | }
|
---|
1318 |
|
---|
1319 | FitsOutFile::FitsOutFile(const char * flnm, WriteMode wrm)
|
---|
1320 | {
|
---|
1321 | InitNull();
|
---|
1322 | openoutputfitsfile(flnm, wrm);
|
---|
1323 | }
|
---|
1324 |
|
---|
1325 | void FitsOutFile::openoutputfitsfile(const char * flnm, WriteMode wrm)
|
---|
1326 | {
|
---|
1327 | int status = 0;
|
---|
1328 |
|
---|
1329 | // create new FITS file
|
---|
1330 | fits_create_file(&fptr_,flnm,&status);
|
---|
1331 | if( status )
|
---|
1332 | {
|
---|
1333 |
|
---|
1334 | switch (wrm)
|
---|
1335 | {
|
---|
1336 | // si on veut ecrire a la fin de ce fichier
|
---|
1337 | case append :
|
---|
1338 | status = 0;
|
---|
1339 | fits_clear_errmsg();
|
---|
1340 | fits_open_file(&fptr_,flnm,READWRITE,&status);
|
---|
1341 | if( status )
|
---|
1342 | {
|
---|
1343 | cout << " error opening file: " << flnm << endl;
|
---|
1344 | printerror(status, "failure opening a file supposed to exist");
|
---|
1345 | }
|
---|
1346 | else cout << " file " << flnm << " opened, new objects will be appended " << endl;
|
---|
1347 | fits_get_num_hdus(fptr_, &hdunum_, &status);
|
---|
1348 | int hdutype;
|
---|
1349 | fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
|
---|
1350 | if( status ) printerror( status,":FitsFile::WriteF : erreur movabs");
|
---|
1351 | break;
|
---|
1352 |
|
---|
1353 | case clear :
|
---|
1354 | {
|
---|
1355 | status = 0;
|
---|
1356 | fits_clear_errmsg();
|
---|
1357 | char* newname = new char[strlen(flnm)+1];
|
---|
1358 | //
|
---|
1359 | newname[0] = '!';
|
---|
1360 | newname[1] = '\0';
|
---|
1361 | strcat(newname, flnm);
|
---|
1362 | fits_create_file(&fptr_,newname,&status);
|
---|
1363 | delete [] newname;
|
---|
1364 | if (status)
|
---|
1365 | {
|
---|
1366 | cout << " error opening file: " << flnm << endl;
|
---|
1367 | printerror(status, "unable to open file, supposed to exist");
|
---|
1368 | }
|
---|
1369 | else cout << " WARNING : file " << flnm << " is overwritten " << endl;
|
---|
1370 | break;
|
---|
1371 | }
|
---|
1372 | case unknown :
|
---|
1373 | printerror(status, " file seems already to exist");
|
---|
1374 | break;
|
---|
1375 |
|
---|
1376 | }
|
---|
1377 | }
|
---|
1378 | }
|
---|
1379 |
|
---|
1380 |
|
---|
1381 |
|
---|
1382 | /*! \fn void SOPHYA::FitsOutFile::makeHeaderImageOnFits(char type, int nbdim, int* naxisn, DVList &dvl)
|
---|
1383 |
|
---|
1384 | create an IMAGE header on FITS file.
|
---|
1385 | \param <type> type of data (see method ColTypeFromFits)
|
---|
1386 | \param <nbdim> number of dimensions : 1D, 2D, 3D etc. = NAXIS
|
---|
1387 | \param <naxisn> array containind sizes of the different dimensions
|
---|
1388 | */
|
---|
1389 | void FitsOutFile::makeHeaderImageOnFits(char type, int nbdim, int* naxisn, DVList* ptr_dvl)
|
---|
1390 | {
|
---|
1391 | int status = 0;
|
---|
1392 | long naxis = nbdim;
|
---|
1393 | long* naxes = new long[nbdim];
|
---|
1394 | bool hdunfirst= (hdunum_ == 0);
|
---|
1395 | if (hdunfirst)
|
---|
1396 | {
|
---|
1397 | if (imageOnPrimary_ == false)
|
---|
1398 | {
|
---|
1399 | hdunum_ = 1;
|
---|
1400 | fits_create_img(fptr_,FLOAT_IMG,0,naxes,&status);
|
---|
1401 | }
|
---|
1402 | }
|
---|
1403 | int k;
|
---|
1404 | for (k=0; k< nbdim; k++) naxes[k] = (long)naxisn[k];
|
---|
1405 | if (type == 'D')
|
---|
1406 | fits_create_img(fptr_,DOUBLE_IMG,naxis,naxes,&status);
|
---|
1407 | else
|
---|
1408 | if (type == 'E')
|
---|
1409 | fits_create_img(fptr_,FLOAT_IMG,naxis,naxes,&status);
|
---|
1410 | else
|
---|
1411 | if (type == 'I')
|
---|
1412 | fits_create_img(fptr_,LONG_IMG,naxis,naxes,&status);
|
---|
1413 | else
|
---|
1414 | {
|
---|
1415 | cout << " type of data: " << type << endl;
|
---|
1416 | throw PException("FitsFile:::makeHeaderImageOnFits:unprogrammed type of data ");
|
---|
1417 | }
|
---|
1418 |
|
---|
1419 | // on ajoute eventuellement un dvlist prepare et la doc SOPHYA
|
---|
1420 | hdunum_++;
|
---|
1421 | if (hdunfirst)
|
---|
1422 | {
|
---|
1423 | addDVListOnPrimary();
|
---|
1424 | writeSignatureOnFits(1);
|
---|
1425 | }
|
---|
1426 |
|
---|
1427 | // header format FITS
|
---|
1428 |
|
---|
1429 | writeAppendedHeaderOnFits();
|
---|
1430 |
|
---|
1431 | // write supplementary keywords (from SOPHYA)
|
---|
1432 | // dvl.Print();
|
---|
1433 | if (ptr_dvl != NULL) addKeywordsOfDVList(*ptr_dvl);
|
---|
1434 |
|
---|
1435 | delete [] naxes;
|
---|
1436 | if( status ) printerror( status, "erreur creation HDU IMAGE" );
|
---|
1437 |
|
---|
1438 | }
|
---|
1439 |
|
---|
1440 |
|
---|
1441 | /*! \fn void SOPHYA::FitsOutFile::PutImageToFits(int nbData, double* map) const
|
---|
1442 |
|
---|
1443 | write double data from array 'map'on an IMAGE extension
|
---|
1444 | \param <nbData> number of data to be written
|
---|
1445 | */
|
---|
1446 | void FitsOutFile::PutImageToFits(int nbData, r_8* map) const
|
---|
1447 | {
|
---|
1448 | int status = 0;
|
---|
1449 | long npix= nbData;
|
---|
1450 | fits_write_img(fptr_,TDOUBLE,1,npix,map,&status);
|
---|
1451 | if( status ) printerror( status, "erreur ecriture PutImageToFits" );
|
---|
1452 | }
|
---|
1453 |
|
---|
1454 | /*! \fn void SOPHYA::FitsOutFile::PutImageToFits(int nbData, float* map) const
|
---|
1455 |
|
---|
1456 | same as previous method with float data
|
---|
1457 | */
|
---|
1458 | void FitsOutFile::PutImageToFits(int nbData, r_4* map) const
|
---|
1459 | {
|
---|
1460 | int status = 0;
|
---|
1461 | long npix= nbData;
|
---|
1462 | fits_write_img(fptr_,TFLOAT,1,npix, map,&status);
|
---|
1463 | if( status ) printerror( status, "erreur ecriture PutImageToFits" );
|
---|
1464 |
|
---|
1465 | }
|
---|
1466 |
|
---|
1467 | /*! \fn void SOPHYA::FitsOutFile::PutImageToFits( int nbData, int* map) const
|
---|
1468 |
|
---|
1469 | same as previous method with int data */
|
---|
1470 | void FitsOutFile::PutImageToFits( int nbData, int_4* map) const
|
---|
1471 | {
|
---|
1472 | int status = 0;
|
---|
1473 |
|
---|
1474 | long npix= nbData;
|
---|
1475 | fits_write_img(fptr_,TINT,1,npix,map,&status);
|
---|
1476 | if( status ) printerror( status, "erreur ecriture PutImageToFits" );
|
---|
1477 | }
|
---|
1478 |
|
---|
1479 |
|
---|
1480 |
|
---|
1481 | /*! \fn void SOPHYA::FitsOutFile::makeHeaderBntblOnFits( string fieldType, vector<string> Noms, int nentries, int tfields, DVList &dvl, string extname, vector<int> taille_des_chaines)
|
---|
1482 |
|
---|
1483 | create an BINTABLE header on FITS file.
|
---|
1484 | \param <fieldType> array conta
|
---|
1485 | ining characters denoting types of the different column (see method ColTypeFromFits)
|
---|
1486 | \param <Noms> array of the names of columns
|
---|
1487 | \param <nentries> number of data of each column
|
---|
1488 | \param <tfields> number of columns
|
---|
1489 | \param <dvl> a SOPHYA DVList containing keywords to be appended
|
---|
1490 | \param <extname> keyword EXTNAME for FITS file
|
---|
1491 | \param <taille_des_chaines> vector containing the number of characters of data for each char* typed column, with order of appearance in 'fieldType'
|
---|
1492 | */
|
---|
1493 | void FitsOutFile::makeHeaderBntblOnFits(string fieldType, vector<string> Noms, int nentries, int tfields, DVList* ptr_dvl, string extname, vector<int> taille_des_chaines)
|
---|
1494 | {
|
---|
1495 | int k;
|
---|
1496 | int status = 0;
|
---|
1497 | long nrows;
|
---|
1498 | // verifications de coherences
|
---|
1499 |
|
---|
1500 | if (fieldType.length() != tfields)
|
---|
1501 | {
|
---|
1502 | cout << " nombre de champs :" << tfields << "nombre de types: " << fieldType.length() << endl;
|
---|
1503 | throw ParmError("FitsFile:: fields and types don't match");
|
---|
1504 |
|
---|
1505 | }
|
---|
1506 | if (tfields > Noms.size())
|
---|
1507 | {
|
---|
1508 | cout << " WARNING: FitsOutFile::makeHeaderBntblOnFits, length of vector of column names not equal to total number of columns" << endl;
|
---|
1509 | for (k=0; k<(tfields-Noms.size()); k++) Noms.push_back( string(" "));
|
---|
1510 | }
|
---|
1511 |
|
---|
1512 | // nombre de variables "chaines de caracteres"
|
---|
1513 | int nbString = 0;
|
---|
1514 | for (k=0; k<tfields;k++) if (fieldType[k] == 'A') nbString++;
|
---|
1515 | // coherence de la longueur du vecteur des tailles
|
---|
1516 | if (nbString > taille_des_chaines.size())
|
---|
1517 | {
|
---|
1518 | cout << " WARNING: FitsOutFile::makeHeaderBntblOnFits, length of vector of string lengths not equal to total number of columns" << endl;
|
---|
1519 | int strSz=0;
|
---|
1520 | for (k=0; k<taille_des_chaines.size(); k++) if ( taille_des_chaines[k] > strSz) strSz = taille_des_chaines[k];
|
---|
1521 | for (k=0; k<(nbString-taille_des_chaines.size()); k++) taille_des_chaines.push_back(strSz);
|
---|
1522 | }
|
---|
1523 | char ** ttype= new char*[tfields];
|
---|
1524 | char ** tform= new char*[tfields];
|
---|
1525 | char largeur[FLEN_VALUE];
|
---|
1526 | int noColString=0;
|
---|
1527 | for (k=0; k<tfields;k++)
|
---|
1528 | {
|
---|
1529 | char format[FLEN_VALUE];
|
---|
1530 |
|
---|
1531 | if(nentries < 1024)
|
---|
1532 | {
|
---|
1533 | nrows= nentries;
|
---|
1534 | if (fieldType[k] == 'A')
|
---|
1535 | {
|
---|
1536 | sprintf(largeur,"%d",taille_des_chaines[noColString++]);
|
---|
1537 | strcpy(format,largeur);
|
---|
1538 | }
|
---|
1539 | else strcpy(format,"1");
|
---|
1540 | }
|
---|
1541 | else
|
---|
1542 | {
|
---|
1543 | nrows = nentries/1024;
|
---|
1544 | if(nentries%1024 != 0) nrows++;
|
---|
1545 | if (fieldType[k] == 'A')
|
---|
1546 | {
|
---|
1547 | char largaux[FLEN_VALUE];
|
---|
1548 | sprintf(largeur,"%d",taille_des_chaines[noColString]);
|
---|
1549 | sprintf(largaux,"%d",1024*taille_des_chaines[noColString]);
|
---|
1550 | noColString++;
|
---|
1551 | strcpy(format, largaux);
|
---|
1552 | }
|
---|
1553 | else strcpy(format,"1024");
|
---|
1554 | }
|
---|
1555 | strncat(format,&fieldType[k],1);
|
---|
1556 | if (fieldType[k] == 'A')
|
---|
1557 | {
|
---|
1558 | strcat(format,largeur);
|
---|
1559 | }
|
---|
1560 | ttype[k] = const_cast<char*>(Noms[k].c_str());
|
---|
1561 | tform[k]= new char[FLEN_VALUE];
|
---|
1562 | strcpy(tform[k],format);
|
---|
1563 | }
|
---|
1564 | char* extn = const_cast<char*>(extname.c_str());
|
---|
1565 |
|
---|
1566 | // create a new empty binary table onto the FITS file
|
---|
1567 | // physical units if they exist, are defined in the DVList object
|
---|
1568 | // so the NULL pointer is given for the tunit parameters.
|
---|
1569 | nrows=0;
|
---|
1570 | fits_create_tbl(fptr_,BINARY_TBL,nrows,tfields,ttype,tform,
|
---|
1571 | NULL,extn,&status);
|
---|
1572 | if( status ) printerror( status );
|
---|
1573 |
|
---|
1574 | int ii;
|
---|
1575 | for(ii = 0; ii < tfields; ii++)
|
---|
1576 | {
|
---|
1577 | delete [] tform[ii];
|
---|
1578 | }
|
---|
1579 | delete [] ttype;
|
---|
1580 | delete [] tform;
|
---|
1581 |
|
---|
1582 | // on ajoute eventuellement des mots-cles
|
---|
1583 |
|
---|
1584 | if ( hdunum_ == 0 )
|
---|
1585 | {
|
---|
1586 | hdunum_ = 2;
|
---|
1587 | addDVListOnPrimary();
|
---|
1588 | writeSignatureOnFits(1);
|
---|
1589 | }
|
---|
1590 | else hdunum_++;
|
---|
1591 |
|
---|
1592 | // header format FITS
|
---|
1593 |
|
---|
1594 | writeAppendedHeaderOnFits();
|
---|
1595 |
|
---|
1596 | // write SOPHYA keywords
|
---|
1597 | if (ptr_dvl != NULL) addKeywordsOfDVList(*ptr_dvl);
|
---|
1598 | }
|
---|
1599 |
|
---|
1600 |
|
---|
1601 |
|
---|
1602 | /*! \fn void SOPHYA::FitsOutFile::PutColToFits(int nocol, int nentries, double* donnees) const
|
---|
1603 |
|
---|
1604 | write double data from array 'donnees ' on column number 'nocol' of a BINTABLE extension.
|
---|
1605 | \param <nentries> number of data to be written
|
---|
1606 | */
|
---|
1607 |
|
---|
1608 | void FitsOutFile::PutColToFits(int nocol, int nentries, r_8* donnees) const
|
---|
1609 | {
|
---|
1610 | int status = 0;
|
---|
1611 | int hdutype;
|
---|
1612 | fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
|
---|
1613 | if( status ) printerror(status,"PutColToFits: le movabs a foire");
|
---|
1614 | fits_get_hdu_type(fptr_, &hdutype, &status);
|
---|
1615 | if(hdutype != ASCII_TBL && hdutype != BINARY_TBL)
|
---|
1616 | {
|
---|
1617 | cout << " hdunum= " << hdunum_ << " hdutype= " << hdutype << endl;
|
---|
1618 | throw IOExc("FitsFile::PutColToFits, this HDU is not an ASCII table nor a binary table");
|
---|
1619 | }
|
---|
1620 | int code;
|
---|
1621 | long repeat, width;
|
---|
1622 | fits_get_coltype(fptr_, nocol+1, &code, &repeat,&width, &status);
|
---|
1623 | if( code != TDOUBLE)
|
---|
1624 | {
|
---|
1625 | cout << " WARNING : types don't match (PutColToFits) : on fits file= " << code << " to be written= DOUBLE " << endl;
|
---|
1626 | }
|
---|
1627 | // cout << " 10 elements de colonne " << endl;
|
---|
1628 | // for (int toto=0; toto < 10; toto++) cout << donnees[toto] << endl;
|
---|
1629 | fits_write_col(fptr_,TDOUBLE,nocol+1,1,1,nentries, donnees ,&status);
|
---|
1630 | if( status ) printerror( status,"erreur ecriture col. double, dans fichier fits" );
|
---|
1631 | }
|
---|
1632 |
|
---|
1633 |
|
---|
1634 |
|
---|
1635 | /*! \fn void SOPHYA::FitsOutFile::PutColToFits(int nocol, int nentries, float* donnees) const
|
---|
1636 |
|
---|
1637 | same as previous method with float data
|
---|
1638 | */
|
---|
1639 | void FitsOutFile::PutColToFits(int nocol, int nentries, r_4* donnees) const
|
---|
1640 | {
|
---|
1641 | int status = 0;
|
---|
1642 | int hdutype;
|
---|
1643 | fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
|
---|
1644 | if( status ) printerror(status,"PutColToFits: le movabs a foire");
|
---|
1645 | fits_get_hdu_type(fptr_, &hdutype, &status);
|
---|
1646 | if(hdutype != ASCII_TBL && hdutype != BINARY_TBL)
|
---|
1647 | {
|
---|
1648 | cout << " hdunum= " << hdunum_ << " hdutype= " << hdutype << endl;
|
---|
1649 | throw IOExc("FitsFile::PutColToFits, this HDU is not an ASCII table nor a binary table");
|
---|
1650 | }
|
---|
1651 | if(hdutype == ASCII_TBL && nocol>0)
|
---|
1652 | {
|
---|
1653 | throw IOExc("FitsFile::PutColToFits, this HDU is an ASCII table, nocol>0 forbidden");
|
---|
1654 | }
|
---|
1655 | int code;
|
---|
1656 | long repeat, width;
|
---|
1657 | fits_get_coltype(fptr_, nocol+1, &code, &repeat,&width, &status);
|
---|
1658 | if( code != TFLOAT)
|
---|
1659 | {
|
---|
1660 | cout << " WARNING : types don't match (PutColToFits) : on fits file= " << code << " (FITS code), to be written= FLOAT " << endl;
|
---|
1661 | }
|
---|
1662 | fits_write_col(fptr_,TFLOAT,nocol+1,1,1,nentries, donnees ,&status);
|
---|
1663 | if( status ) printerror( status,"erreur ecriture col. floats, dans fichier fits" );
|
---|
1664 | }
|
---|
1665 |
|
---|
1666 |
|
---|
1667 | /*! \fn void FitsOutFile::PutColToFits(int nocol, int nentries, int* donnees) const
|
---|
1668 |
|
---|
1669 | same as previous method with int data
|
---|
1670 | */
|
---|
1671 | void FitsOutFile::PutColToFits(int nocol, int nentries, int_4* donnees) const
|
---|
1672 | {
|
---|
1673 | int status = 0;
|
---|
1674 | int hdutype;
|
---|
1675 | fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
|
---|
1676 | if( status ) printerror(status,"PutColToFits: le movabs a foire");
|
---|
1677 | fits_get_hdu_type(fptr_, &hdutype, &status);
|
---|
1678 | if(hdutype != ASCII_TBL && hdutype != BINARY_TBL)
|
---|
1679 | {
|
---|
1680 | cout << " hdunum= " << hdunum_ << " hdutype= " << hdutype << endl;
|
---|
1681 | throw IOExc("FitsFile::PutColToFits, this HDU is not an ASCII table nor a binary table");
|
---|
1682 | }
|
---|
1683 | if(hdutype == ASCII_TBL && nocol>0)
|
---|
1684 | {
|
---|
1685 | throw IOExc("FitsFile::PutColToFits, this HDU is an ASCII table, nocol>0 forbidden");
|
---|
1686 | }
|
---|
1687 | int code;
|
---|
1688 | long repeat, width;
|
---|
1689 | fits_get_coltype(fptr_, nocol+1, &code, &repeat,&width, &status);
|
---|
1690 |
|
---|
1691 |
|
---|
1692 | // la logique voudrait qu'on distingue TLONG et TINT. Mais si j'ecris
|
---|
1693 | // et relis immediatement quelque chose en TLONG l'experience montre
|
---|
1694 | // que ca foire. Donc, je fais tout en TINT, d'ailleurs cfitsio n'a pas
|
---|
1695 | // (apparemment) d'entiers de longueur superieure a 32 bits.
|
---|
1696 | // En fait, je n'y comprend rien. A suivre (GLM).
|
---|
1697 | if (code == TINT || code == TLONG )
|
---|
1698 | {
|
---|
1699 | cout << " j'ecris des TINT" << endl;
|
---|
1700 | fits_write_col(fptr_,TINT,nocol+1,1,1,nentries, donnees ,&status);
|
---|
1701 | }
|
---|
1702 | else if (code == TSHORT)
|
---|
1703 | {
|
---|
1704 | cout << " j'ecris des TSHORT " << endl;
|
---|
1705 | fits_write_col(fptr_,TSHORT,nocol+1,1,1,nentries, donnees ,&status);
|
---|
1706 | }
|
---|
1707 | else
|
---|
1708 | {
|
---|
1709 | cout << " WARNING : types don't match (PutColToFits) : on fits file= " << code << " (FITS code), to be written= integers " << endl;
|
---|
1710 | }
|
---|
1711 | if( status ) printerror( status,"erreur ecriture col. entiers, dans fichier fits" );
|
---|
1712 | }
|
---|
1713 |
|
---|
1714 |
|
---|
1715 | /*! \fn void SOPHYA::FitsOutFile::PutColToFits(int nocol, int nentries, char** donnees) const
|
---|
1716 | same as previous method with char* data
|
---|
1717 | */
|
---|
1718 | void FitsOutFile::PutColToFits(int nocol, int nentries, char** donnees) const
|
---|
1719 | {
|
---|
1720 | int status = 0;
|
---|
1721 | int hdutype;
|
---|
1722 | fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
|
---|
1723 | if( status ) printerror(status,"PutColToFits: le movabs a foire");
|
---|
1724 | fits_get_hdu_type(fptr_, &hdutype, &status);
|
---|
1725 | if(hdutype != ASCII_TBL && hdutype != BINARY_TBL)
|
---|
1726 | {
|
---|
1727 | cout << " hdunum= " << hdunum_ << " hdutype= " << hdutype << endl;
|
---|
1728 | throw IOExc("FitsFile::PutColToFits, this HDU is not an ASCII table nor a binary table");
|
---|
1729 | }
|
---|
1730 | if(hdutype == ASCII_TBL && nocol>0)
|
---|
1731 | {
|
---|
1732 | throw IOExc("FitsFile::PutColToFits, this HDU is an ASCII table, nocol>0 forbidden");
|
---|
1733 | }
|
---|
1734 | int code;
|
---|
1735 | long repeat, width;
|
---|
1736 | fits_get_coltype(fptr_, nocol+1, &code, &repeat,&width, &status);
|
---|
1737 | if( code != TSTRING)
|
---|
1738 | {
|
---|
1739 | cout << " WARNING : types don't match (PutColToFits) : on fits file= " << code << " (FITS code), to be written= char** " << endl;
|
---|
1740 | }
|
---|
1741 | fits_write_col(fptr_,TSTRING,nocol+1,1,1,nentries, donnees ,&status);
|
---|
1742 | if( status ) printerror( status,"erreur ecriture col. chars, dans fichier fits" );
|
---|
1743 | }
|
---|
1744 |
|
---|
1745 | void FitsOutFile::PutBinTabLine(long NoLine, BnTblLine& ligne) const
|
---|
1746 | {
|
---|
1747 | // on ne fait pas de verification de type, ni de dimension ici, pour
|
---|
1748 | // des raisons de performances
|
---|
1749 | int k;
|
---|
1750 | int status= 0;
|
---|
1751 | int anull;
|
---|
1752 | int ncol=0;
|
---|
1753 | long nels=1;
|
---|
1754 | // int nbcols;
|
---|
1755 | // fits_get_num_cols(fptr_, &nbcols,&status);
|
---|
1756 | for (k=0; k<ligne.ddata_.size(); k++, ncol++)
|
---|
1757 | {
|
---|
1758 | fits_write_col(fptr_,TDOUBLE,ncol+1,NoLine+1,1,1, &ligne.ddata_[k] ,&status);
|
---|
1759 | if( status ) printerror( status, "PutBinTabLine : erreur ecriture double" );
|
---|
1760 | }
|
---|
1761 | for (k=0; k<ligne.fdata_.size(); k++, ncol++)
|
---|
1762 | {
|
---|
1763 | fits_write_col(fptr_,TFLOAT,ncol+1,NoLine+1,1,1, &ligne.fdata_[k] ,&status);
|
---|
1764 | if( status ) printerror( status, "PutBinTabLine : erreur ecriture float" );
|
---|
1765 | }
|
---|
1766 | for (k=0; k<ligne.idata_.size(); k++, ncol++)
|
---|
1767 | {
|
---|
1768 | fits_write_col(fptr_,TINT,ncol+1,NoLine+1,1,1, &ligne.idata_[k] ,&status);
|
---|
1769 | if( status ) printerror( status, "PutBinTabLine : erreur ecriture entier" );
|
---|
1770 | }
|
---|
1771 | for (k=0; k<ligne.ldata_.size(); k++, ncol++)
|
---|
1772 | {
|
---|
1773 | fits_write_col(fptr_,TLONG,ncol+1,NoLine+1,1,1, &ligne.ldata_[k] ,&status);
|
---|
1774 | if( status ) printerror( status, "PutBinTabLine : erreur ecriture entier long" );
|
---|
1775 | }
|
---|
1776 | for (k=0; k<ligne.bdata_.size(); k++, ncol++)
|
---|
1777 | {
|
---|
1778 | fits_write_col(fptr_,TBYTE,ncol+1,NoLine+1,1,1, &ligne.bdata_[k] ,&status);
|
---|
1779 | if( status ) printerror( status, "PutBinTabLine : erreur ecriture byte" );
|
---|
1780 | }
|
---|
1781 |
|
---|
1782 | for (k=0; k<ligne.cdata_.size(); k++, ncol++)
|
---|
1783 | {
|
---|
1784 | fits_write_col(fptr_,TSTRING,ncol+1,NoLine+1,1,1, (void*)ligne.cdata_[k].c_str() ,&status);
|
---|
1785 | if( status ) printerror( status, "PutBinTabLine : erreur ecriture caracteres" );
|
---|
1786 | }
|
---|
1787 | }
|
---|
1788 |
|
---|
1789 |
|
---|
1790 | /* \fn void SOPHYA::FitsOutFile::DVListIntoPrimaryHeader(DVList& dvl) const
|
---|
1791 |
|
---|
1792 | Put keywords from a DVList into the primary header of the fits-file
|
---|
1793 | */
|
---|
1794 | void FitsOutFile::DVListIntoPrimaryHeader(DVList& dvl)
|
---|
1795 | {
|
---|
1796 | int status = 0;
|
---|
1797 | int hdutype;
|
---|
1798 | if (hdunum_ == 0)
|
---|
1799 | {
|
---|
1800 | if (dvlToPrimary_ == NULL) dvlToPrimary_ = new DVList(dvl);
|
---|
1801 | else dvlToPrimary_->Merge(dvl);
|
---|
1802 | }
|
---|
1803 | else
|
---|
1804 | {
|
---|
1805 | fits_movabs_hdu(fptr_,1,&hdutype,&status);
|
---|
1806 | addKeywordsOfDVList(dvl);
|
---|
1807 | fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
|
---|
1808 | }
|
---|
1809 | }
|
---|
1810 |
|
---|
1811 |
|
---|
1812 | void FitsOutFile::writeSignatureOnFits(int hdunum) const
|
---|
1813 | {
|
---|
1814 | int status = 0;
|
---|
1815 | int hdutype;
|
---|
1816 | char keyname[LEN_KEYWORD];
|
---|
1817 | char strval[FLEN_VALUE];
|
---|
1818 | char comment[FLEN_COMMENT];
|
---|
1819 | if (hdunum_ == 0)
|
---|
1820 | {
|
---|
1821 | cerr << " WARNING : can't write keywords on non existing primary header" << endl;
|
---|
1822 | return;
|
---|
1823 | }
|
---|
1824 | fits_movabs_hdu(fptr_,1,&hdutype,&status);
|
---|
1825 | //
|
---|
1826 | strncpy(keyname, "CREATOR", LEN_KEYWORD);
|
---|
1827 | keyname[7] = '\0';
|
---|
1828 | strcpy(strval, "SOPHYA");
|
---|
1829 | strcpy(comment," SOPHYA Package - FITSIOServer ");
|
---|
1830 | fits_write_key(fptr_, TSTRING, keyname, &strval, comment, &status);
|
---|
1831 | if( status ) printerror( status );
|
---|
1832 | fits_write_date(fptr_, &status);
|
---|
1833 | fits_write_comment(fptr_,"..............................................", &status);
|
---|
1834 | fits_write_comment(fptr_, " SOPHYA package - FITSIOSever ", &status);
|
---|
1835 | fits_write_comment(fptr_, " (C) LAL/IN2P3-CNRS Orsay, FRANCE 2000", &status);
|
---|
1836 | fits_write_comment(fptr_, " (C) DAPNIA/CEA Saclay, FRANCE 2000", &status);
|
---|
1837 | fits_write_comment(fptr_,"..............................................", &status);
|
---|
1838 | if( status ) printerror( status, "erreur writeSignatureOnFits" );
|
---|
1839 | //
|
---|
1840 | fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
|
---|
1841 | }
|
---|
1842 |
|
---|
1843 |
|
---|
1844 | void FitsOutFile::addKeywordsOfDVList( DVList& dvl) const
|
---|
1845 | {
|
---|
1846 | int status = 0;
|
---|
1847 | fits_write_comment(fptr_,"---------- keywords from SOPHYA ---------", &status);
|
---|
1848 | DVList::ValList::const_iterator it;
|
---|
1849 | for(it = dvl.Begin(); it != dvl.End(); it++)
|
---|
1850 | {
|
---|
1851 | MuTyV::MTVType keytype= (*it).second.elval.Type();
|
---|
1852 | char keyname[LEN_KEYWORD];
|
---|
1853 | strncpy(keyname,(*it).first.substr(0,64).c_str(),LEN_KEYWORD);
|
---|
1854 | int bout = ((*it).first.substr(0,64).length() < LEN_KEYWORD) ? (*it).first.substr(0,64).length() : LEN_KEYWORD-1;
|
---|
1855 | keyname[bout] = '\0';
|
---|
1856 | string key((*it).first.substr(0,64));
|
---|
1857 | // string key(keyname);
|
---|
1858 | char comment[FLEN_COMMENT];
|
---|
1859 | char strval[FLEN_VALUE]= "";
|
---|
1860 | char *comkey = "COMMENT";
|
---|
1861 | // fits_read_keyword(fptr_, keyname, strval, NULL, &status);
|
---|
1862 | // if (status != 0 || strncmp(keyname,comkey,LEN_KEYWORD-1) == 0 )
|
---|
1863 | {
|
---|
1864 | string coco = dvl.GetComment(key);
|
---|
1865 | coco.copy( comment, FLEN_COMMENT-1);
|
---|
1866 | int bout = (coco.length() < FLEN_COMMENT) ? coco.length() : FLEN_COMMENT-1;
|
---|
1867 | comment[bout]= '\0';
|
---|
1868 | status = 0;
|
---|
1869 | switch (keytype)
|
---|
1870 | {
|
---|
1871 | case MuTyV::MTVInteger :
|
---|
1872 | {
|
---|
1873 | int ival = (int)dvl.GetI(key);
|
---|
1874 | fits_write_key(fptr_,TINT,keyname,&ival, comment,&status);
|
---|
1875 | break;
|
---|
1876 | }
|
---|
1877 | case MuTyV::MTVFloat :
|
---|
1878 | {
|
---|
1879 | double dval= (double)dvl.GetD(key);
|
---|
1880 | fits_write_key(fptr_,TDOUBLE,keyname,&dval,comment,&status);
|
---|
1881 | break;
|
---|
1882 | }
|
---|
1883 | case MuTyV::MTVString :
|
---|
1884 | {
|
---|
1885 | char strvaleur[FLEN_VALUE]= "";
|
---|
1886 | string valChaine = dvl.GetS(key);
|
---|
1887 | valChaine.copy(strvaleur, FLEN_VALUE-1);
|
---|
1888 | int fin = (valChaine.length() < FLEN_VALUE) ? valChaine.length() : FLEN_VALUE-1;
|
---|
1889 | strvaleur[fin]= '\0';
|
---|
1890 |
|
---|
1891 | fits_write_key(fptr_,TSTRING,keyname,&strvaleur,comment,&status);
|
---|
1892 | break;
|
---|
1893 | }
|
---|
1894 | }
|
---|
1895 | }
|
---|
1896 | if( status ) printerror( status,"fitsfile: probleme ecriture mot-cle du dvlist" );
|
---|
1897 | }
|
---|
1898 | fits_write_comment(fptr_,"--------------------------------------", &status);
|
---|
1899 | }
|
---|
1900 |
|
---|
1901 |
|
---|
1902 | void FitsOutFile::addDVListOnPrimary()
|
---|
1903 | {
|
---|
1904 | int status = 0;
|
---|
1905 | int hdutype;
|
---|
1906 | if (hdunum_ == 0)
|
---|
1907 | {
|
---|
1908 | cerr << " WARNING : can't write keywords on non existing primary header" << endl;
|
---|
1909 | return;
|
---|
1910 | }
|
---|
1911 | if (dvlToPrimary_ != NULL)
|
---|
1912 | {
|
---|
1913 | fits_movabs_hdu(fptr_,1,&hdutype,&status);
|
---|
1914 | addKeywordsOfDVList(*dvlToPrimary_);
|
---|
1915 | delete dvlToPrimary_;
|
---|
1916 | dvlToPrimary_ = NULL;
|
---|
1917 | fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
|
---|
1918 | }
|
---|
1919 | }
|
---|
1920 |
|
---|
1921 |
|
---|
1922 | /*! \fn void FitsOutFile::appendInHeader(FitsInFile& infits, int hdunum)
|
---|
1923 |
|
---|
1924 | get a header from FitsInFile and append to the header beeing built
|
---|
1925 | (shifting mandatory keywords)
|
---|
1926 | */
|
---|
1927 |
|
---|
1928 | void FitsOutFile::appendInputHeader(FitsInFile& infits, int hdunum)
|
---|
1929 | {
|
---|
1930 | int status = 0;
|
---|
1931 | int hdutype;
|
---|
1932 | fitsfile* fptr=infits.fitsfilePtr();
|
---|
1933 | fits_movabs_hdu(fptr,hdunum,&hdutype,&status);
|
---|
1934 | if( status ) fits_report_error(stderr,status);
|
---|
1935 |
|
---|
1936 | // get number of keywords
|
---|
1937 | int nkeys,keypos;
|
---|
1938 | fits_get_hdrpos(fptr,&nkeys,&keypos,&status);
|
---|
1939 | if( status ) fits_report_error(stderr,status);
|
---|
1940 | // shift with the number of mandatory keywords
|
---|
1941 | int num= 0;
|
---|
1942 | // if primary header
|
---|
1943 | if (hdunum == 1)
|
---|
1944 | {
|
---|
1945 | // read NAXIS
|
---|
1946 | int naxis=0;
|
---|
1947 | fits_read_key(fptr,TINT,"NAXIS",&naxis,NULL,&status);
|
---|
1948 | // number of mandatory keywords
|
---|
1949 | num = naxis+3;
|
---|
1950 | }
|
---|
1951 | // extensions
|
---|
1952 | else
|
---|
1953 | {
|
---|
1954 | if (hdutype == IMAGE_HDU)
|
---|
1955 | {
|
---|
1956 | // read NAXIS
|
---|
1957 | int naxis=0;
|
---|
1958 | fits_read_key(fptr,TINT,"NAXIS",&naxis,NULL,&status);
|
---|
1959 | // number of mandatory keywords
|
---|
1960 | num = naxis+5;
|
---|
1961 | }
|
---|
1962 | else
|
---|
1963 | if(hdutype == ASCII_TBL || hdutype == BINARY_TBL)
|
---|
1964 | {
|
---|
1965 | // number of mandatory keywords
|
---|
1966 | num = 8;
|
---|
1967 | }
|
---|
1968 | }
|
---|
1969 | int j;
|
---|
1970 | char keyname[LEN_KEYWORD];
|
---|
1971 | char value[FLEN_VALUE];
|
---|
1972 | char comment[FLEN_COMMENT];
|
---|
1973 | for(j = num+1; j <= nkeys; j++)
|
---|
1974 | {
|
---|
1975 | char dtype;
|
---|
1976 | fits_read_keyn(fptr,j,keyname,value,comment,&status);
|
---|
1977 | if(status)
|
---|
1978 | {
|
---|
1979 | fits_report_error(stderr,status);
|
---|
1980 | status=0;
|
---|
1981 | }
|
---|
1982 | string kn(keyname);
|
---|
1983 | string cm(comment);
|
---|
1984 | string val(value);
|
---|
1985 | FitsKeyword kw(kn, val, cm);
|
---|
1986 | mots_cles_.push_back(kw);
|
---|
1987 | }
|
---|
1988 | }
|
---|
1989 | void FitsOutFile::writeAppendedHeaderOnFits()
|
---|
1990 | {
|
---|
1991 | for (list<FitsKeyword>::iterator it=mots_cles_.begin(); it !=mots_cles_.end(); it++)
|
---|
1992 | {
|
---|
1993 | (*it).writeOnFits(fptr_);
|
---|
1994 | }
|
---|
1995 | mots_cles_.clear();
|
---|
1996 | }
|
---|
1997 |
|
---|
1998 | void FitsOutFile::insertKeywordOnHeader(string keyname, double value, string comment)
|
---|
1999 | {
|
---|
2000 | char cvalue[16];
|
---|
2001 | sprintf(cvalue,"%e",value);
|
---|
2002 | FitsKeyword kw(keyname, string(cvalue), comment, 'F');
|
---|
2003 | mots_cles_.push_back(kw);
|
---|
2004 | }
|
---|
2005 | void FitsOutFile::insertKeywordOnHeader(string keyname, int value, string comment)
|
---|
2006 | {
|
---|
2007 | char cvalue[16];
|
---|
2008 | sprintf(cvalue,"%d",value);
|
---|
2009 | FitsKeyword kw(keyname, string(cvalue), comment, 'I');
|
---|
2010 | mots_cles_.push_back(kw);
|
---|
2011 | }
|
---|
2012 | void FitsOutFile::insertKeywordOnHeader(string keyname, string value, string comment)
|
---|
2013 | {
|
---|
2014 | FitsKeyword kw(keyname, value , comment, 'C');
|
---|
2015 | mots_cles_.push_back(kw);
|
---|
2016 | }
|
---|
2017 |
|
---|
2018 | void FitsOutFile::insertCommentLineOnHeader(string comment)
|
---|
2019 | {
|
---|
2020 | FitsKeyword kw(comment);
|
---|
2021 | mots_cles_.push_back(kw);
|
---|
2022 | }
|
---|
2023 |
|
---|
2024 | void FitsOutFile::PrintHeaderToBeAppended()
|
---|
2025 | {
|
---|
2026 | cout << " contenu du header en cours de fabrication " << endl;
|
---|
2027 | for (list<FitsKeyword>::iterator it=mots_cles_.begin(); it !=mots_cles_.end(); it++)
|
---|
2028 | {
|
---|
2029 | (*it).Print();
|
---|
2030 | }
|
---|
2031 | }
|
---|
2032 |
|
---|
2033 |
|
---|
2034 | FitsKeyword::FitsKeyword()
|
---|
2035 | {
|
---|
2036 | datatype_=' ';
|
---|
2037 | keyname_ = string("");
|
---|
2038 | dvalue_=0.;
|
---|
2039 | ivalue_=1;
|
---|
2040 | svalue_=string("");
|
---|
2041 | comment_=string("");
|
---|
2042 | }
|
---|
2043 |
|
---|
2044 | FitsKeyword::FitsKeyword(string comment)
|
---|
2045 | {
|
---|
2046 | datatype_=' ';
|
---|
2047 | keyname_=string("COMMENT");
|
---|
2048 | comment_=comment;
|
---|
2049 | }
|
---|
2050 |
|
---|
2051 | FitsKeyword::FitsKeyword(string keyname, string value, string comment) : keyname_(keyname), comment_(comment)
|
---|
2052 | {
|
---|
2053 | int status=0;
|
---|
2054 | char dtype;
|
---|
2055 | const char* val= value.c_str();
|
---|
2056 | char* valk = const_cast<char*>(val);
|
---|
2057 | fits_get_keytype(valk,&dtype,&status);
|
---|
2058 | if(status)
|
---|
2059 | {
|
---|
2060 | status=0;
|
---|
2061 | if (status == VALUE_UNDEFINED) cout << "WARNING (FitsKeyword) : undefined keyword value " << endl;
|
---|
2062 | datatype_=' ';
|
---|
2063 | }
|
---|
2064 | else datatype_=dtype;
|
---|
2065 |
|
---|
2066 | switch( datatype_ )
|
---|
2067 | {
|
---|
2068 | case 'C':
|
---|
2069 | {
|
---|
2070 | strip(valk, 'B','\'');
|
---|
2071 | svalue_ = string(valk);
|
---|
2072 | break;
|
---|
2073 | }
|
---|
2074 | case 'I':
|
---|
2075 | {
|
---|
2076 | ivalue_ = atoi(val);
|
---|
2077 | break;
|
---|
2078 | }
|
---|
2079 | case 'L':
|
---|
2080 | {
|
---|
2081 | bool bb = value.c_str();
|
---|
2082 | ivalue_ = (int)bb;
|
---|
2083 | break;
|
---|
2084 | }
|
---|
2085 | case 'F':
|
---|
2086 | {
|
---|
2087 | dvalue_ = atof(val);
|
---|
2088 | break;
|
---|
2089 | }
|
---|
2090 | case 'X':
|
---|
2091 | {
|
---|
2092 | throw IOExc("FitsKeyword , complex keyword value not supported");
|
---|
2093 | }
|
---|
2094 | }
|
---|
2095 | }
|
---|
2096 |
|
---|
2097 | // constructeur pour les mots-cles maison (ne prvenant pas de la lecture d'un fichier fits)
|
---|
2098 | FitsKeyword::FitsKeyword(string keyname, string value, string comment, char type) : keyname_(keyname), comment_(comment), datatype_(type)
|
---|
2099 | {
|
---|
2100 | char dtype;
|
---|
2101 | const char* val= value.c_str();
|
---|
2102 | char* valk = const_cast<char*>(val);
|
---|
2103 | switch( datatype_ )
|
---|
2104 | {
|
---|
2105 | case 'C':
|
---|
2106 | {
|
---|
2107 | strip(valk, 'B','\'');
|
---|
2108 | svalue_ = string(valk);
|
---|
2109 | break;
|
---|
2110 | }
|
---|
2111 | case 'I':
|
---|
2112 | {
|
---|
2113 | ivalue_ = atoi(val);
|
---|
2114 | break;
|
---|
2115 | }
|
---|
2116 | case 'L':
|
---|
2117 | {
|
---|
2118 | bool bb = value.c_str();
|
---|
2119 | ivalue_ = (int)bb;
|
---|
2120 | break;
|
---|
2121 | }
|
---|
2122 | case 'F':
|
---|
2123 | {
|
---|
2124 | dvalue_ = atof(val);
|
---|
2125 | break;
|
---|
2126 | }
|
---|
2127 | case 'X':
|
---|
2128 | {
|
---|
2129 | throw IOExc("FitsKeyword , complex keyword value not supported");
|
---|
2130 | }
|
---|
2131 | }
|
---|
2132 | }
|
---|
2133 |
|
---|
2134 | void FitsKeyword::writeOnFits(fitsfile* ptr)
|
---|
2135 | {
|
---|
2136 | int status=0;
|
---|
2137 | char keyname[LEN_KEYWORD];
|
---|
2138 | char comment[FLEN_COMMENT];
|
---|
2139 | keyname_.copy(keyname, LEN_KEYWORD);
|
---|
2140 | int bout = (keyname_.length() < LEN_KEYWORD) ? keyname_.length() : LEN_KEYWORD-1;
|
---|
2141 | keyname[bout] = '\0';
|
---|
2142 | comment_.copy( comment, FLEN_COMMENT);
|
---|
2143 | bout = (comment_.length() < FLEN_COMMENT) ? comment_.length() : FLEN_COMMENT-1;
|
---|
2144 | comment[bout]= '\0';
|
---|
2145 |
|
---|
2146 | int nkeys,keypos;
|
---|
2147 | fits_get_hdrpos(ptr,&nkeys,&keypos,&status);
|
---|
2148 | switch( datatype_ )
|
---|
2149 | {
|
---|
2150 | case 'C':
|
---|
2151 | {
|
---|
2152 | char value[FLEN_VALUE]="";
|
---|
2153 | svalue_.copy(value, FLEN_VALUE-1);
|
---|
2154 | int fin = (svalue_.length() < FLEN_VALUE) ? svalue_.length() : FLEN_VALUE-1;
|
---|
2155 | value[fin]= '\0';
|
---|
2156 | fits_write_key(ptr,TSTRING,keyname,&value, comment,&status);
|
---|
2157 | fits_report_error(stderr,status);
|
---|
2158 | break;
|
---|
2159 | }
|
---|
2160 | case 'I':
|
---|
2161 | {
|
---|
2162 | fits_write_key(ptr,TINT,keyname,&ivalue_, comment,&status);
|
---|
2163 | fits_report_error(stderr,status);
|
---|
2164 | break;
|
---|
2165 | }
|
---|
2166 | case 'L':
|
---|
2167 | {
|
---|
2168 | fits_write_key(ptr,TLOGICAL,keyname,&ivalue_, comment,&status);
|
---|
2169 | fits_report_error(stderr,status);
|
---|
2170 | break;
|
---|
2171 | }
|
---|
2172 | case 'F':
|
---|
2173 | {
|
---|
2174 | fits_write_key(ptr,TDOUBLE,keyname,&dvalue_, comment,&status);
|
---|
2175 | fits_report_error(stderr,status);
|
---|
2176 | break;
|
---|
2177 | }
|
---|
2178 | case 'X':
|
---|
2179 | {
|
---|
2180 | cout << "FitsKeyword : complex keyword value not supported" << endl;;
|
---|
2181 | }
|
---|
2182 | default :
|
---|
2183 | {
|
---|
2184 | char *comkey = "COMMENT";
|
---|
2185 | if(strncmp(keyname,comkey,LEN_KEYWORD-1) == 0)
|
---|
2186 | {
|
---|
2187 | fits_write_comment(ptr,comment,&status);
|
---|
2188 | fits_report_error(stderr,status);
|
---|
2189 | }
|
---|
2190 | else
|
---|
2191 | {
|
---|
2192 | cout << " WARNING (FitsKeyword::writeOnFits) : unrecognized keyword : " << keyname_ << endl;
|
---|
2193 | }
|
---|
2194 | }
|
---|
2195 | }
|
---|
2196 | }
|
---|
2197 |
|
---|
2198 | void FitsKeyword::Print()
|
---|
2199 | {
|
---|
2200 | switch( datatype_ )
|
---|
2201 | {
|
---|
2202 | case 'C':
|
---|
2203 | {
|
---|
2204 | cout << " mot cle : " << keyname_ << " valeur : " << svalue_ << " commentaire : " << comment_ <<endl;
|
---|
2205 | break;
|
---|
2206 | }
|
---|
2207 | case 'I':
|
---|
2208 | {
|
---|
2209 | cout << " mot cle : " << keyname_ << " valeur : " << ivalue_ << " commentaire : " << comment_ <<endl;
|
---|
2210 | break;
|
---|
2211 | }
|
---|
2212 | case 'L':
|
---|
2213 | {
|
---|
2214 | cout << " mot cle : " << keyname_ << " valeur : " << ivalue_ << " commentaire : " << comment_ <<endl;
|
---|
2215 | break;
|
---|
2216 | }
|
---|
2217 | case 'F':
|
---|
2218 | {
|
---|
2219 | cout << " mot cle : " << keyname_ << " valeur : " << dvalue_ << " commentaire : " << comment_ <<endl;
|
---|
2220 | break;
|
---|
2221 | }
|
---|
2222 | case 'X':
|
---|
2223 | {
|
---|
2224 | cout << "FitsKeyword : complex keyword value not supported" << endl;;
|
---|
2225 | }
|
---|
2226 | default :
|
---|
2227 | {
|
---|
2228 | cout << " mot cle : " << keyname_ << " commentaire : " << comment_ <<endl;
|
---|
2229 | }
|
---|
2230 | }
|
---|
2231 | }
|
---|