source: Sophya/trunk/SophyaPI/PIext/nomskymapadapter.cc@ 1451

Last change on this file since 1451 was 1321, checked in by ercodmgr, 25 years ago

Adaptateur de TArray et lecture FITS - Reza 13/11/2000

File size: 10.3 KB
Line 
1#include "machdefs.h"
2#include <stdlib.h>
3#include <math.h>
4#include <typeinfo>
5#include <iostream.h>
6#include <string>
7#include <complex>
8
9#include "datatype.h"
10
11#include "nomskymapadapter.h"
12#include "skymap.h"
13#include "pitvmaad.h"
14#include "complexios.h"
15
16#include "fitsspherehealpix.h"
17#include "fitslocalmap.h"
18
19
20// Classe array adapter pour localMap
21template <class T>
22class LocalMapArrAdapter : public P2DArrayAdapter {
23public:
24 LocalMapArrAdapter(LocalMap<T>* lm, bool d=false) :
25 P2DArrayAdapter(lm->SizeX(), lm->SizeY())
26 { ad = d; map = lm; }
27
28 virtual ~LocalMapArrAdapter() { if (ad) delete map; }
29 virtual double Value(int ix, int iy) { return((*map)(ix, iy)); }
30
31protected :
32 bool ad;
33 LocalMap<T>* map;
34};
35
36/* --Methode-- */
37double LocalMapArrAdapter< complex<float> >::Value(int ix, int iy)
38{
39double re,im;
40re = (*map)(iy, ix).real();
41im = (*map)(iy, ix).imag();
42return(sqrt(re*re+im*im));
43}
44/* --Methode-- */
45double LocalMapArrAdapter< complex<double> >::Value(int ix, int iy)
46{
47double re,im;
48re = (*map)(iy, ix).real();
49im = (*map)(iy, ix).imag();
50return(sqrt(re*re+im*im));
51}
52
53//----------------------------------------------------------------
54// Class Adaptateur d'objet (Pour NamedObjMgr) d'objet PixelMap<T>
55//----------------------------------------------------------------
56
57
58/* --Methode-- */
59template <class T>
60NOMAdapter_PixelMap<T>::NOMAdapter_PixelMap(PixelMap<T> * o)
61 : NObjMgrAdapter(o)
62{
63mMap = o;
64}
65
66/* --Methode-- */
67template <class T>
68NOMAdapter_PixelMap<T>::~NOMAdapter_PixelMap()
69{
70}
71
72/* --Methode-- */
73template <class T>
74NObjMgrAdapter* NOMAdapter_PixelMap<T>::Clone(AnyDataObj* o)
75{
76PixelMap<T>* m = dynamic_cast<PixelMap<T> *>(o);
77if (m) return ( new NOMAdapter_PixelMap<T>(m) );
78return ( new NObjMgrAdapter(o) );
79}
80
81/* --Methode-- */
82template <class T>
83string NOMAdapter_PixelMap<T>::GetDataObjType()
84{
85string type = "PixelMap< ";
86LocalMap<T>* lm = dynamic_cast< LocalMap<T> * >(mMap);
87if (lm != NULL) type = "LocalMap< ";
88SphereThetaPhi<T>* st = dynamic_cast< SphereThetaPhi<T> * >(mMap);
89if (st != NULL) type = "SphereThetaPhi< ";
90SphereHEALPix<T>* sg = dynamic_cast< SphereHEALPix<T> * >(mMap);
91if (sg != NULL) type = "SphereHEALPix< ";
92
93// type += DecodeTypeIdName(typeid(T).name());
94type += DataTypeInfo<T>::getTypeName();
95type += " > ";
96return(type);
97}
98
99/* --Methode-- */
100template <class T>
101AnyDataObj* NOMAdapter_PixelMap<T>::CloneDataObj(bool share)
102{
103LocalMap<T>* lm = dynamic_cast< LocalMap<T> * >(mMap);
104if (lm != NULL) return( new LocalMap<T>(*lm, share) );
105SphereThetaPhi<T>* st = dynamic_cast< SphereThetaPhi<T> * >(mMap);
106if (st != NULL) return( new SphereThetaPhi<T>(*st, share) );
107SphereHEALPix<T>* sg = dynamic_cast< SphereHEALPix<T> * >(mMap);
108if (sg != NULL) return( new SphereHEALPix<T>(*sg, share) );
109return(NULL);
110}
111
112/* --Methode-- */
113template <class T>
114void NOMAdapter_PixelMap<T>::ReadFits(string const & flnm)
115{
116LocalMap<T>* lm = dynamic_cast< LocalMap<T> * >(mMap);
117if (lm != NULL) {
118 FitsInFile fis(flnm);
119 fis >> (*lm);
120 return;
121}
122SphereHEALPix<T>* sg = dynamic_cast< SphereHEALPix<T> * >(mMap);
123if (sg != NULL) {
124 FitsInFile fis(flnm);
125 fis >> (*sg);
126 return;
127}
128string s = typeid(*mMap).name();
129cout << " NOMAdapter_PixelMap<T>::ReadFits() - Error "
130 << " Not supported for " << s << endl;
131return;
132}
133
134
135/* --Methode-- */
136template <class T>
137void NOMAdapter_PixelMap<T>::SaveFits(string const & flnm)
138{
139LocalMap<T>* lm = dynamic_cast< LocalMap<T> * >(mMap);
140if (lm != NULL) {
141 FitsOutFile fos(flnm);
142 fos << (*lm);
143 return;
144}
145SphereHEALPix<T>* sg = dynamic_cast< SphereHEALPix<T> * >(mMap);
146if (sg != NULL) {
147 FitsOutFile fos(flnm);
148 fos << (*sg);
149 return;
150}
151string s = typeid(*mMap).name();
152cout << " NOMAdapter_PixelMap<T>::SaveFits() - Error "
153 << " Not supported for " << s << endl;
154return;
155}
156
157// ---- Specialisation pour complexes -----
158void NOMAdapter_PixelMap< complex<r_4> >::ReadFits(string const & flnm)
159{
160cout << " void NOMAdapter_PixelMap< complex<r_4> >::ReadFits() - Error "
161 << " Not supported (complex data type)" << endl;
162}
163void NOMAdapter_PixelMap< complex<r_4> >::SaveFits(string const & flnm)
164{
165cout << " void NOMAdapter_PixelMap< complex<r_4> >::SaveFits() - Error "
166 << " Not supported (complex data type)" << endl;
167}
168void NOMAdapter_PixelMap< complex<r_8> >::ReadFits(string const & flnm)
169{
170cout << " void NOMAdapter_PixelMap< complex<r_4> >::ReadFits() - Error "
171 << " Not supported (complex data type)" << endl;
172}
173void NOMAdapter_PixelMap< complex<r_8> >::SaveFits(string const & flnm)
174{
175cout << " void NOMAdapter_PixelMap< complex<r_8> >::SaveFits() - Error "
176 << " Not supported (complex data type)" << endl;
177}
178
179
180/* --Methode-- */
181template <class T>
182void NOMAdapter_PixelMap<T>::SavePPF(POutPersist& pos, string const & nom)
183{
184LocalMap<T>* lm = dynamic_cast< LocalMap<T> * >(mMap);
185if (lm != NULL) {
186 FIO_LocalMap<T> fio(lm);
187 fio.Write(pos, nom);
188 return;
189 }
190SphereThetaPhi<T>* st = dynamic_cast< SphereThetaPhi<T> * >(mMap);
191if (st != NULL) {
192 FIO_SphereThetaPhi<T> fio(st);
193 fio.Write(pos, nom);
194 return;
195 }
196SphereHEALPix<T>* sg = dynamic_cast< SphereHEALPix<T> * >(mMap);
197if (sg != NULL) {
198 FIO_SphereHEALPix<T> fio(sg);
199 fio.Write(pos, nom);
200 return;
201 }
202string s = typeid(*mMap).name();
203cout << "NOMAdapter_PixelMap<T>::SavePPF() - Error : Not supported for " << s << endl;
204}
205
206/* --Methode-- */
207template <class T>
208void NOMAdapter_PixelMap<T>::Print(ostream& os)
209{
210string s = typeid(*mMap).name();
211T moy, sig;
212MeanSig(moy, sig);
213cout << "SkyMap Type: " << s << " NbPixels= " << mMap->NbPixels() << endl;
214cout << " Mean= " << moy << " Sig2= " << sig << endl;
215}
216
217
218/* --Methode-- */
219template <class T>
220P2DArrayAdapter* NOMAdapter_PixelMap<T>::Get2DArray(string &)
221{
222LocalMap<T>* lm = dynamic_cast< LocalMap<T> * >(mMap);
223if (lm != NULL) return(new LocalMapArrAdapter<T>(lm, false));
224int nr = 250;
225int nc = 500;
226SphericalMap<T>* sm = dynamic_cast< SphericalMap<T> *>(mMap);
227if (sm != NULL) { nr = sqrt(0.75*mMap->NbPixels()); nc = 2*nr; }
228TMatrix<T> * mtx = new TMatrix<T>(nr, nc);
229Project_Mol(*mtx);
230return (new POTMatrixAdapter<T>(mtx, true) );
231}
232
233/* --Methode-- */
234template <class T>
235NTupleInterface* NOMAdapter_PixelMap<T>::GetNTupleInterface(bool& adel)
236{
237adel = true;
238return( new NTupInt_PixelMap<T>(mMap) );
239}
240
241/* --Methode-- */
242template <class T>
243void NOMAdapter_PixelMap<T>::MeanSig(T& gmoy, T& gsig)
244{
245 gmoy=0.;
246 gsig = 0.;
247 T valok;
248 for(int k=0; k<mMap->NbPixels(); k++) {
249 valok = (*mMap)(k);
250 gmoy += valok; gsig += valok*valok;
251 }
252 gmoy /= (T)mMap->NbPixels();
253 gsig = gsig/(T)mMap->NbPixels() - gmoy*gmoy;
254
255}
256
257/* --Methode-- */
258template <class T>
259void NOMAdapter_PixelMap<T>::Project_Mol(TMatrix<T> & mtx, T defval)
260{
261 r_8 xa, yd, teta,phi, facteur;
262 int_4 l,c,k;
263 int_4 nl = mtx.NRows();
264 int_4 nc = mtx.NCols();
265 mtx = defval; // On met tout a defval
266// cout << " NRows= " << nl << " NCols= " << nc << endl;
267 for(l=0; l<nl; l++) {
268 yd = (r_8)(l+0.5)/(r_8)nl-0.5;
269 facteur=2.*M_PI/sin(acos((double)yd*2));
270 teta = (yd+0.5)*Pi;
271 // teta = (0.5-yd)*M_PI;
272 for(c=0; c<nc; c++) {
273 xa = (r_8)(c+0.5)/(r_8)nc-0.5;
274 phi = xa*facteur+M_PI;
275 if ( (phi <= 2*M_PI) && (phi >= 0.) ) {
276 k = mMap->PixIndexSph(teta, phi);
277 mtx(l,c) = (*mMap)(k);
278 }
279 }
280 }
281}
282
283// -------------------------------------------------------------
284
285/* --Methode-- */
286template <class T>
287NTupInt_PixelMap<T>::NTupInt_PixelMap(PixelMap<T>* m)
288{
289mMap = m;
290}
291
292/* --Methode-- */
293template <class T>
294NTupInt_PixelMap<T>::~NTupInt_PixelMap()
295{
296}
297
298/* --Methode-- */
299template <class T>
300uint_4 NTupInt_PixelMap<T>::NbLines() const
301{
302return( mMap->NbPixels() );
303}
304
305/* --Methode-- */
306template <class T>
307uint_4 NTupInt_PixelMap<T>::NbColumns() const
308{
309return(8);
310}
311
312/* --Methode-- */
313template <class T>
314r_8* NTupInt_PixelMap<T>::GetLineD(int n) const
315{
316int i;
317if ((n < 0) || (n >= (int)(mMap->NbPixels()) ))
318 for(i=0; i<8; i++) mRet[i] = 0.;
319else {
320 double teta,phi;
321 mMap->PixThetaPhi(n, teta, phi);
322 mRet[0] = n; mRet[1] = mMap->PixVal(n);
323 mRet[2] = mRet[1]; mRet[3] = 0.;
324 mRet[4] = mRet[1]; mRet[5] = 0.;
325 mRet[6] = teta; mRet[7] = phi;
326 }
327return(mRet);
328}
329
330/* --Methode-- */
331template <class T>
332string NTupInt_PixelMap<T>::VarList_C(const char* nx) const
333{
334string nomx;
335if (nx) nomx = nx;
336else nomx = "_xh_";
337string vardec = "double i,k,val,real,imag,mod,phas,teta,phi; \n";
338vardec += "i = " + nomx + "[0]; k = " + nomx + "[0]; val = " + nomx + "[1]; \n";
339vardec += "real = " + nomx + "[2]; imag = " + nomx + "[3]; \n";
340vardec += "mod = " + nomx + "[4]; phas = " + nomx + "[5]; \n";
341vardec += "teta = " + nomx + "[6]; phi = " + nomx + "[7]; \n";
342return(vardec);
343}
344
345r_8* NTupInt_PixelMap< complex<float> >::GetLineD(int n) const
346{
347int i;
348if ((n < 0) || (n >= (int)(mMap->NbPixels()) ))
349 for(i=0; i<8; i++) mRet[i] = 0.;
350else {
351 double teta,phi;
352 mMap->PixThetaPhi(n, teta, phi);
353 mRet[0] = n;
354 mRet[2] = mMap->PixVal(n).real(); mRet[3] = mMap->PixVal(n).imag();
355 mRet[1] = mRet[4] = sqrt(mRet[2]*mRet[2]+mRet[3]*mRet[3]);
356 mRet[5] = atan2(mRet[3], mRet[2]);
357 mRet[6] = teta; mRet[7] = phi;
358 }
359return(mRet);
360}
361
362r_8* NTupInt_PixelMap< complex<double> >::GetLineD(int n) const
363{
364int i;
365if ((n < 0) || (n >= (int)(mMap->NbPixels()) ))
366 for(i=0; i<8; i++) mRet[i] = 0.;
367else {
368 double teta,phi;
369 mMap->PixThetaPhi(n, teta, phi);
370 mRet[0] = n;
371 mRet[2] = mMap->PixVal(n).real(); mRet[3] = mMap->PixVal(n).imag();
372 mRet[1] = mRet[4] = sqrt(mRet[2]*mRet[2]+mRet[3]*mRet[3]);
373 mRet[5] = atan2(mRet[3], mRet[2]);
374 mRet[6] = teta; mRet[7] = phi;
375 }
376return(mRet);
377}
378
379
380#ifdef __CXX_PRAGMA_TEMPLATES__
381#pragma define_template NOMAdapter_PixelMap<r_4>
382#pragma define_template NOMAdapter_PixelMap<r_8>
383#pragma define_template NOMAdapter_PixelMap< complex<float> >
384#pragma define_template NOMAdapter_PixelMap< complex<double> >
385#pragma define_template NTupInt_PixelMap<r_4>
386#pragma define_template NTupInt_PixelMap<r_8>
387#pragma define_template NTupInt_PixelMap< complex<float> >
388#pragma define_template NTupInt_PixelMap< complex<double> >
389#endif
390
391#if defined(ANSI_TEMPLATES)
392template class NOMAdapter_PixelMap<r_4>;
393template class NOMAdapter_PixelMap<r_8>;
394template class NOMAdapter_PixelMap< complex<float> >;
395template class NOMAdapter_PixelMap< complex<double> >;
396template class NTupInt_PixelMap<r_4>;
397template class NTupInt_PixelMap<r_8>;
398template class NTupInt_PixelMap< complex<float> >;
399template class NTupInt_PixelMap< complex<double> >;
400#endif
Note: See TracBrowser for help on using the repository browser.