source: Sophya/trunk/SophyaExt/FitsIOServer/fitsarrhand.h@ 4025

Last change on this file since 4025 was 3887, checked in by ansari, 15 years ago

forcage NAXIS=1 lors ecriture TVector + retag en V2_2, Reza 24/09/2010

File size: 6.6 KB
RevLine 
[3493]1/*
2 --- SOPHYA software - FitsIOServer module ---
3 R. Ansari , 2005-2008
4 (C) UPS+LAL IN2P3/CNRS (C) DAPNIA-SPP/CEA
5*/
[2820]6#ifndef FITSARRHAND_H
7#define FITSARRHAND_H
8
9#include "machdefs.h"
10#include <string>
11#include "tarray.h"
[2864]12#include "tvector.h"
[2820]13
14#include "fitshandler.h"
[2864]15#include "fitsblkrw.h"
[2820]16
17namespace SOPHYA {
18
19/*!
20 \ingroup FitsIOServer
21 \brief FITS I/O handler for array objects
22*/
23
24template <class T>
25class FitsArrayHandler : public FitsHandlerInterface {
26public :
27 FitsArrayHandler() { dobj=NULL; ownobj=true; }
28 FitsArrayHandler(TArray< T > & obj) { dobj = &obj; ownobj=false; }
29 virtual ~FitsArrayHandler() { if (ownobj && dobj) delete dobj; }
30 virtual AnyDataObj* DataObj() { return(dobj); }
31
[2864]32 virtual int CheckHandling(AnyDataObj & o)
[2820]33 {
[2864]34 if ( (typeid(o) == typeid(TArray<T>)) ||
35 (typeid(o) == typeid(TMatrix<T>)) ||
36 (typeid(o) == typeid(TVector<T>)) ) return 2;
[2820]37 TArray<T> * po = dynamic_cast< TArray<T> * >(& o);
[2864]38 if (po == NULL) return 0;
39 else return 1;
[2820]40 }
[2864]41
[2820]42 virtual void SetDataObj(AnyDataObj & o)
43 {
44 TArray<T> * po = dynamic_cast< TArray<T> * >(& o);
45 if (po == NULL) {
46 string msg = "FitsHandler<T>::SetDataObj() Wrong object type: " ;
47 msg += typeid(o).name();
48 throw TypeMismatchExc(msg);
49 }
50 if (ownobj && dobj) delete dobj; dobj = po; ownobj = false;
51 }
52
[2864]53 virtual int CheckReadability(FitsInOutFile& is)
54 {
55 if (is.CurrentHDUType() != IMAGE_HDU) return 0;
56 T x = 0;
[3167]57 LONGLONG naxes[BASEARRAY_MAXNDIMS];
[2864]58 int naxis=BASEARRAY_MAXNDIMS;
[2898]59 int imgtyp = is.GetImageHDUInfo(naxis, naxes);
60 if (naxis < 1) return 0;
61 if (FitsTypes::ImageType(x) == imgtyp) return 2;
[2864]62 else return 1;
63 }
64
[2820]65 virtual FitsHandlerInterface* Clone()
66 { return new FitsArrayHandler< T >(); }
67
68 inline operator T&() { return(*dobj); }
69
70 //----- Ecriture
[3493]71 //! Writes the complete array as an IMAGE HDU
[2820]72 virtual void Write(FitsInOutFile& os)
73 {
[3493]74 if (( dobj == NULL) || (dobj->Size() < 1))
75 throw NullPtrError("FitsArrayHandler<T>::Write() dobj=NULL or dobj->Size()=0");
76
[3167]77 LONGLONG naxes[BASEARRAY_MAXNDIMS] = {0,0,0,0,0};
[2820]78 for(int_4 id=0; id<dobj->NbDimensions(); id++)
79 naxes[id] = dobj->Size(id);
[3887]80 int naxis=dobj->NbDimensions();
[2844]81 T x = 0;
[3887]82 TVector<T> * pvec = dynamic_cast< TVector<T> * >(dobj);
83 if (pvec!=NULL) { naxis=1; naxes[0]=pvec->NElts(); }
84 os.CreateImageHDU(FitsTypes::ImageType(x), naxis, naxes);
[2820]85 os.WriteHeaderRecords(dobj->Info());
[2864]86 MuTyV mtv;
87 mtv = "SOPHYA::TArray<T>";
88 os.WriteKey("SOPCLSNM",mtv," Object class name ");
[2820]89 FitsBlockRW<T>::WriteImageData(os, dobj->Data(), dobj->Size());
90 }
91
92 //----- Lecture
[3493]93 //! Resize the array and reads the complete IMAGE HDU to the array
[2820]94 virtual void Read(FitsInOutFile& is)
95 {
[3167]96 LONGLONG naxes[BASEARRAY_MAXNDIMS];
[2820]97 int naxis=BASEARRAY_MAXNDIMS;
98 is.GetImageHDUInfo(naxis, naxes);
[2864]99 if ( dobj == NULL) {
100 if (naxis == 1) dobj = new TVector<T>;
[2907]101 else if (naxis == 2) {
102 if ( (naxes[0] == 1) || (naxes[1] == 1) )
103 dobj = new TVector<T>;
104 else dobj = new TMatrix<T>;
105 }
[2864]106 else dobj = new TArray<T>;
107 ownobj = true;
108 }
[2820]109 sa_size_t sz[BASEARRAY_MAXNDIMS];
[3493]110 if (naxis > BASEARRAY_MAXNDIMS) {
111 cerr << "FitsArrayHandler<T>::Read()/Warning FITS NAXIS=" << naxis
112 << " > BASEARRAY_MAXNDIMS=" << BASEARRAY_MAXNDIMS << endl;
113 naxis = BASEARRAY_MAXNDIMS;
114 }
[2820]115 for(int_4 id=0; id<naxis; id++) sz[id] = naxes[id];
[3885]116 TMatrix<T> * pmx = dynamic_cast< TMatrix<T> * >(dobj);
117 if ((naxis==1)&&(pmx!=NULL)) {
118 TVector<T> * pvec = dynamic_cast< TVector<T> * >(dobj);
119 if (pvec!=NULL) pvec->SetSize(naxes[0]);
120 else pmx->SetSize(1,naxes[0]);
121 }
122 else dobj->SetSize(naxis, sz, 1, false);
[2820]123 FitsBlockRW<T>::ReadImageData(is, dobj->Data(), dobj->Size());
124 is.GetHeaderRecords(dobj->Info());
125 }
126
[3493]127 //----- Lecture avec specification d'offset et taille donnee par le tableau
128 /*! \brief Reads from the fits file into the array, at the specified offset
129
130 The array should be allocated and its size is used to define the number of
131 pixels to be read.
132 \warning The offset should be specified with SOPHYA/C array index convention
133 (starting at zero) - and NOT the cfitsio/fortran convention.
134 The offset should contain as many elements as the fits NAXIS keyword.
135 */
136 virtual void ReadAtOffset(FitsInOutFile& is, sa_size_t* offset)
137 {
138 if (( dobj == NULL) || (dobj->Size() < 1))
139 throw NullPtrError("FitsArrayHandler<T>::ReadAtOffset() dobj=NULL or dobj->Size()=0");
140
141 LONGLONG naxes[BASEARRAY_MAXNDIMS];
142 int naxis=BASEARRAY_MAXNDIMS;
143 is.GetImageHDUInfo(naxis, naxes);
144 LONGLONG fpix[15];
145 int namx = (naxis < 15) ? naxis : 15;
146 for(int i=0; i<namx; i++) fpix[i] = offset[i]+1;
147 FitsBlockRW<T>::ReadImageData(is, dobj->Data(), dobj->Size(), fpix);
148 is.GetHeaderRecords(dobj->Info());
149 }
150
151 //----- Ecriture avec specification d'offset et taille donnee par le tableau
152 /*! \brief Writes the array to the fits file, at the specified offset
153
154 The array should be allocated and its size is used to define the number of
[3850]155 pixels to be written.
[3493]156 \warning The offset should be specified with SOPHYA/C array index convention
157 (starting at zero) - and NOT the cfitsio/fortran convention.
158 The offset should contain as many elements as the fits NAXIS keyword.
159 \warning The IMAGE HDU should be already created using FitsInOutFile::CreateImageHDU().
160 */
161 virtual void WriteAtOffset(FitsInOutFile& os, sa_size_t* offset)
162 {
163 if (( dobj == NULL) || (dobj->Size() < 1))
164 throw NullPtrError("FitsArrayHandler<T>::ReadAtOffset() dobj=NULL or dobj->Size()=0");
165
166 LONGLONG naxes[BASEARRAY_MAXNDIMS];
167 int naxis=BASEARRAY_MAXNDIMS;
168 os.GetImageHDUInfo(naxis, naxes);
169 LONGLONG fpix[15];
170 int namx = (naxis < 15) ? naxis : 15;
171 for(int i=0; i<namx; i++) fpix[i] = offset[i]+1;
172 FitsBlockRW<T>::WriteImageData(os, dobj->Data(), dobj->Size(), fpix);
173 }
174
[2820]175protected :
176 TArray<T> * dobj;
177 bool ownobj; // True si dobj obtenu par new
178};
179
180
[3493]181//! operator << overload to write a TArray<T> to a fits IMAGE HDU
[2820]182template <class T>
183inline FitsInOutFile& operator << (FitsInOutFile& os, TArray<T> const & obj)
184{ FitsArrayHandler<T> fio(const_cast< TArray<T> &>(obj)); fio.Write(os); return os; }
185
[3493]186//! operator >> overload to read a TArray<T> from a fits IMAGE HDU
[2820]187template <class T>
188inline FitsInOutFile& operator >> (FitsInOutFile& is, TArray<T> & obj)
189{ FitsArrayHandler<T> fio(obj); fio.Read(is); is.MoveToNextHDU(); return(is); }
190
191
192} // Fin du namespace
193
194#endif
195
Note: See TracBrowser for help on using the repository browser.