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

Last change on this file since 2335 was 2335, checked in by ansari, 23 years ago

Compil sur SGI-CC avec LANG:std suite au remplacement xxstream.h par xxstream (gcc 3.1) / ajout template <> pour specialisation template - Reza 10/03/2003

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