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

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

Compilation avec SGI-CC -LANG:std - Reza 10/03/2003

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