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

Last change on this file since 2860 was 2844, checked in by ansari, 20 years ago

correction 2 petits bugs ds nouveau fits - Reza 18/11/2005

File size: 2.7 KB
RevLine 
[2820]1#ifndef FITSARRHAND_H
2#define FITSARRHAND_H
3
4#include "machdefs.h"
5#include <string>
6#include "tarray.h"
7
8#include "fitshandler.h"
9
10namespace SOPHYA {
11
12/*!
13 \ingroup FitsIOServer
14 \brief FITS I/O handler for array objects
15*/
16
17template <class T>
18class FitsArrayHandler : public FitsHandlerInterface {
19public :
20 FitsArrayHandler() { dobj=NULL; ownobj=true; }
21 FitsArrayHandler(TArray< T > & obj) { dobj = &obj; ownobj=false; }
22 virtual ~FitsArrayHandler() { if (ownobj && dobj) delete dobj; }
23 virtual AnyDataObj* DataObj() { return(dobj); }
24
25 virtual bool CheckHandling(AnyDataObj & o)
26 {
27 TArray<T> * po = dynamic_cast< TArray<T> * >(& o);
28 if (po == NULL) return false;
29 else return true;
30 }
31 virtual void SetDataObj(AnyDataObj & o)
32 {
33 TArray<T> * po = dynamic_cast< TArray<T> * >(& o);
34 if (po == NULL) {
35 string msg = "FitsHandler<T>::SetDataObj() Wrong object type: " ;
36 msg += typeid(o).name();
37 throw TypeMismatchExc(msg);
38 }
39 if (ownobj && dobj) delete dobj; dobj = po; ownobj = false;
40 }
41
42 virtual FitsHandlerInterface* Clone()
43 { return new FitsArrayHandler< T >(); }
44
45 inline operator T&() { return(*dobj); }
46
47 //----- Ecriture
48 virtual void Write(FitsInOutFile& os)
49 {
50 if ( dobj == NULL)
51 throw NullPtrError("FitsArrayHandler<T>::Write() dobj=NULL ");
52 long naxes[BASEARRAY_MAXNDIMS] = {0,0,0,0,0};
53 for(int_4 id=0; id<dobj->NbDimensions(); id++)
54 naxes[id] = dobj->Size(id);
[2844]55 T x = 0;
[2820]56 os.CreateImageHDU(FitsTypes::ImageType(x), dobj->NbDimensions(), naxes);
57 os.WriteHeaderRecords(dobj->Info());
58 FitsBlockRW<T>::WriteImageData(os, dobj->Data(), dobj->Size());
59 }
60
61 //----- Lecture
62 virtual void Read(FitsInOutFile& is)
63 {
64 if ( dobj == NULL)
65 throw NullPtrError("FitsArrayHandler<T>::Read() dobj=NULL ");
66 long naxes[BASEARRAY_MAXNDIMS];
67 int naxis=BASEARRAY_MAXNDIMS;
68 is.GetImageHDUInfo(naxis, naxes);
69 sa_size_t sz[BASEARRAY_MAXNDIMS];
70 if (naxis > BASEARRAY_MAXNDIMS) naxis = BASEARRAY_MAXNDIMS;
71 for(int_4 id=0; id<naxis; id++) sz[id] = naxes[id];
72 dobj->SetSize(naxis, sz, 1, false);
73 FitsBlockRW<T>::ReadImageData(is, dobj->Data(), dobj->Size());
74 is.GetHeaderRecords(dobj->Info());
75 }
76
77protected :
78 TArray<T> * dobj;
79 bool ownobj; // True si dobj obtenu par new
80};
81
82
83template <class T>
84inline FitsInOutFile& operator << (FitsInOutFile& os, TArray<T> const & obj)
85{ FitsArrayHandler<T> fio(const_cast< TArray<T> &>(obj)); fio.Write(os); return os; }
86
87template <class T>
88inline FitsInOutFile& operator >> (FitsInOutFile& is, TArray<T> & obj)
89{ FitsArrayHandler<T> fio(obj); fio.Read(is); is.MoveToNextHDU(); return(is); }
90
91
92} // Fin du namespace
93
94#endif
95
Note: See TracBrowser for help on using the repository browser.