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

Last change on this file since 4018 was 3520, checked in by ansari, 17 years ago

Ajout/codage methode P2DArrayAdapter::MeanVal() pour les classes adapter heritant de P2DArrayAdapter, Reza 11/09/2008

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