#ifndef FITSARRHAND_H #define FITSARRHAND_H #include "machdefs.h" #include #include "tarray.h" #include "tvector.h" #include "fitshandler.h" #include "fitsblkrw.h" namespace SOPHYA { /*! \ingroup FitsIOServer \brief FITS I/O handler for array objects */ template class FitsArrayHandler : public FitsHandlerInterface { public : FitsArrayHandler() { dobj=NULL; ownobj=true; } FitsArrayHandler(TArray< T > & obj) { dobj = &obj; ownobj=false; } virtual ~FitsArrayHandler() { if (ownobj && dobj) delete dobj; } virtual AnyDataObj* DataObj() { return(dobj); } virtual int CheckHandling(AnyDataObj & o) { if ( (typeid(o) == typeid(TArray)) || (typeid(o) == typeid(TMatrix)) || (typeid(o) == typeid(TVector)) ) return 2; TArray * po = dynamic_cast< TArray * >(& o); if (po == NULL) return 0; else return 1; } virtual void SetDataObj(AnyDataObj & o) { TArray * po = dynamic_cast< TArray * >(& o); if (po == NULL) { string msg = "FitsHandler::SetDataObj() Wrong object type: " ; msg += typeid(o).name(); throw TypeMismatchExc(msg); } if (ownobj && dobj) delete dobj; dobj = po; ownobj = false; } virtual int CheckReadability(FitsInOutFile& is) { if (is.CurrentHDUType() != IMAGE_HDU) return 0; T x = 0; LONGLONG naxes[BASEARRAY_MAXNDIMS]; int naxis=BASEARRAY_MAXNDIMS; int imgtyp = is.GetImageHDUInfo(naxis, naxes); if (naxis < 1) return 0; if (FitsTypes::ImageType(x) == imgtyp) return 2; else return 1; } virtual FitsHandlerInterface* Clone() { return new FitsArrayHandler< T >(); } inline operator T&() { return(*dobj); } //----- Ecriture virtual void Write(FitsInOutFile& os) { if ( dobj == NULL) throw NullPtrError("FitsArrayHandler::Write() dobj=NULL "); LONGLONG naxes[BASEARRAY_MAXNDIMS] = {0,0,0,0,0}; for(int_4 id=0; idNbDimensions(); id++) naxes[id] = dobj->Size(id); T x = 0; os.CreateImageHDU(FitsTypes::ImageType(x), dobj->NbDimensions(), naxes); os.WriteHeaderRecords(dobj->Info()); MuTyV mtv; mtv = "SOPHYA::TArray"; os.WriteKey("SOPCLSNM",mtv," Object class name "); FitsBlockRW::WriteImageData(os, dobj->Data(), dobj->Size()); } //----- Lecture virtual void Read(FitsInOutFile& is) { LONGLONG naxes[BASEARRAY_MAXNDIMS]; int naxis=BASEARRAY_MAXNDIMS; is.GetImageHDUInfo(naxis, naxes); if ( dobj == NULL) { if (naxis == 1) dobj = new TVector; else if (naxis == 2) { if ( (naxes[0] == 1) || (naxes[1] == 1) ) dobj = new TVector; else dobj = new TMatrix; } else dobj = new TArray; ownobj = true; } sa_size_t sz[BASEARRAY_MAXNDIMS]; if (naxis > BASEARRAY_MAXNDIMS) naxis = BASEARRAY_MAXNDIMS; for(int_4 id=0; idSetSize(naxis, sz, 1, false); FitsBlockRW::ReadImageData(is, dobj->Data(), dobj->Size()); is.GetHeaderRecords(dobj->Info()); } protected : TArray * dobj; bool ownobj; // True si dobj obtenu par new }; template inline FitsInOutFile& operator << (FitsInOutFile& os, TArray const & obj) { FitsArrayHandler fio(const_cast< TArray &>(obj)); fio.Write(os); return os; } template inline FitsInOutFile& operator >> (FitsInOutFile& is, TArray & obj) { FitsArrayHandler fio(obj); fio.Read(is); is.MoveToNextHDU(); return(is); } } // Fin du namespace #endif