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

Last change on this file since 2594 was 2594, checked in by ansari, 21 years ago

ajout valeur par defaut pour mollweide projection, en dehors de la zone de projection - Reza , 9 Aout 2004

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